Revert "Remove caffe2/core/timer.h (#194228)"

This reverts commit 7744b78e00.

Reverted https://github.com/pytorch/pytorch/pull/194228 on behalf of https://github.com/jeanschmidt due to internally many systems still depends on the deleted timer.h header, so a follow up is required to remove this dependency before merging this PR, @janeyx99 can you follow up on this one? - see D116988880 ([comment](https://github.com/pytorch/pytorch/pull/194228#issuecomment-5383528270))
This commit is contained in:
PyTorch MergeBot
2026-08-23 01:14:00 +00:00
parent f300d5a4f1
commit add6ff4aa0
6 changed files with 76 additions and 35 deletions
+1
View File
@@ -19,6 +19,7 @@
#include <vector>
#include <ATen/ATen.h>
#include <caffe2/core/timer.h>
#include <caffe2/utils/string_utils.h>
#include <torch/csrc/autograd/grad_mode.h>
#include <torch/csrc/jit/serialization/import.h>
+3 -4
View File
@@ -18,6 +18,7 @@
#include <vector>
#include <ATen/ATen.h>
#include "caffe2/core/timer.h"
#include "caffe2/utils/string_utils.h"
#include <torch/csrc/autograd/grad_mode.h>
#include <torch/csrc/jit/mobile/module.h>
@@ -56,7 +57,7 @@ int main(int argc, char** argv) {
FLAGS_iter,
".");
const auto bench_start = high_resolution_clock::now();
caffe2::Timer timer;
std::vector<long> times;
for (int i = 0; i < FLAGS_iter; ++i) {
@@ -73,9 +74,7 @@ int main(int argc, char** argv) {
times.push_back(duration.count());
}
const double micros = duration<double, std::micro>(
high_resolution_clock::now() - bench_start)
.count();
const double micros = static_cast<double>(timer.MicroSeconds());
if (FLAGS_report_pep) {
for (auto t : times) {
std::cout << R"(PyTorchObserver {"type": "NET", "unit": "us", )"
+5 -5
View File
@@ -18,6 +18,7 @@
#include <vector>
#include <ATen/ATen.h>
#include "caffe2/core/timer.h"
#include "caffe2/utils/string_utils.h"
#include <torch/csrc/autograd/grad_mode.h>
#include <torch/csrc/jit/mobile/module.h>
@@ -314,8 +315,9 @@ int main(int argc, char** argv) {
"Number of main runs should be non negative, provided ",
FLAGS_iter,
".");
const auto bench_start = high_resolution_clock::now();
std::vector<float> times(FLAGS_iter, 0);
caffe2::Timer timer;
std::vector<float> times;
auto micros = timer.MicroSeconds();
for (int i = 0; i < FLAGS_iter; ++i) {
auto start = high_resolution_clock::now();
runner->run(module, inputs);
@@ -323,9 +325,7 @@ int main(int argc, char** argv) {
auto duration = duration_cast<microseconds>(stop - start);
times.push_back(duration.count());
}
const double micros = duration<double, std::micro>(
high_resolution_clock::now() - bench_start)
.count();
micros = timer.MicroSeconds();
if (FLAGS_report_pep) {
for (auto t : times) {
std::cout << "PyTorchObserver {\"type\": \"NET\", \"unit\": \"us\", \"metric\": \"latency\", \"value\": \"" << t << "\"}" << std::endl;
+48
View File
@@ -0,0 +1,48 @@
#ifndef CAFFE2_CORE_TIMER_H_
#define CAFFE2_CORE_TIMER_H_
#include <chrono>
#include "caffe2/core/common.h"
namespace caffe2 {
/**
* @brief A simple timer object for measuring time.
*
* This is a minimal class around a std::chrono::high_resolution_clock that
* serves as a utility class for testing code.
*/
class Timer {
public:
typedef std::chrono::high_resolution_clock clock;
typedef std::chrono::nanoseconds ns;
Timer() { Start(); }
/**
* @brief Starts a timer.
*/
inline void Start() { start_time_ = clock::now(); }
inline float NanoSeconds() {
return static_cast<float>(
std::chrono::duration_cast<ns>(clock::now() - start_time_).count());
}
/**
* @brief Returns the elapsed time in milliseconds.
*/
inline float MilliSeconds() { return NanoSeconds() / 1000000.f; }
/**
* @brief Returns the elapsed time in microseconds.
*/
inline float MicroSeconds() { return NanoSeconds() / 1000.f; }
/**
* @brief Returns the elapsed time in seconds.
*/
inline float Seconds() { return NanoSeconds() / 1000000000.f; }
protected:
std::chrono::time_point<clock> start_time_;
C10_DISABLE_COPY_AND_ASSIGN(Timer);
};
}
#endif // CAFFE2_CORE_TIMER_H_
+15 -19
View File
@@ -7,6 +7,7 @@
#include <c10/macros/Macros.h>
#include <c10/util/MaybeOwned.h>
#include <c10/util/irange.h>
#include <caffe2/core/timer.h>
#include <torch/csrc/jit/ir/alias_analysis.h>
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/passes/add_if_then_else.h>
@@ -22,7 +23,6 @@
#include <torch/csrc/jit/runtime/static/ops.h>
#include <torch/csrc/jit/runtime/static/passes.h>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <initializer_list>
#include <iostream>
@@ -1660,7 +1660,7 @@ float BlockRunner::benchmark_model(
}
}
}
const auto start = std::chrono::high_resolution_clock::now();
caffe2::Timer timer;
for ([[maybe_unused]] const auto _n_run : c10::irange(main_runs)) {
const auto num_args = static_cast<uint32_t>(args_list.size());
for (const auto j : c10::irange(num_args)) {
@@ -1670,9 +1670,7 @@ float BlockRunner::benchmark_model(
}
}
}
const float millis = std::chrono::duration<float, std::milli>(
std::chrono::high_resolution_clock::now() - start)
.count();
float millis = timer.MilliSeconds();
return millis /
(static_cast<float>(main_runs) * static_cast<float>(args_list.size()));
}
@@ -1798,23 +1796,21 @@ BlockRunner::IndividualMetrics BlockRunner::benchmark_individual_ops(
c10::InferenceMode mode;
// setup time
using hr_clock = std::chrono::high_resolution_clock;
using float_millis = std::chrono::duration<float, std::milli>;
auto start = hr_clock::now();
caffe2::Timer timer;
set_inputs(args_list[0], is_kwargs_empty ? empty_kwargs : kwargs_list[0]);
results.setup_time = float_millis(hr_clock::now() - start).count();
results.setup_time = timer.MilliSeconds();
// The first iteration profiles each node's output Tensors' sizes and
// initializes the memory planner with the profile information. Following
// iterations just use the already established memory planning.
start = hr_clock::now();
timer.Start();
operator()(args_list[0], is_kwargs_empty ? empty_kwargs : kwargs_list[0]);
if (manage_output_tensors) {
deallocateOutputTensors();
}
results.first_iter_time = float_millis(hr_clock::now() - start).count();
results.first_iter_time = timer.MilliSeconds();
// warmup runs
for ([[maybe_unused]] const auto _n_run : c10::irange(warmup_runs)) {
@@ -1833,21 +1829,21 @@ BlockRunner::IndividualMetrics BlockRunner::benchmark_individual_ops(
for (const auto j : c10::irange(num_args)) {
set_inputs(args_list[j], is_kwargs_empty ? empty_kwargs : kwargs_list[j]);
start = hr_clock::now();
timer.Start();
if (planner_) {
planner_->allocate();
}
float millis = float_millis(hr_clock::now() - start).count();
float millis = timer.MilliSeconds();
results.memory_alloc_time += millis;
const auto num_nodes = static_cast<uint32_t>(nodes_.size());
for (const auto k : c10::irange<uint32_t>(num_nodes)) {
start = hr_clock::now();
timer.Start();
nodes_[k].run();
millis = float_millis(hr_clock::now() - start).count();
millis = timer.MilliSeconds();
results.time_per_node[k] += millis;
verify_and_correct_memory_overlap(nodes_[k]);
}
start = hr_clock::now();
timer.Start();
create_memory_planner();
planner_->deallocate();
// clean up owning refs of input tensors
@@ -1855,10 +1851,10 @@ BlockRunner::IndividualMetrics BlockRunner::benchmark_individual_ops(
if (manage_output_tensors) {
deallocateOutputTensors();
}
millis = float_millis(hr_clock::now() - start).count();
millis = timer.MilliSeconds();
results.memory_dealloc_time += millis;
start = hr_clock::now();
timer.Start();
// no need to keep references of outputs in static runtime anymore
c10::IValue output;
if (static_module_.num_outputs() > 1) {
@@ -1871,7 +1867,7 @@ BlockRunner::IndividualMetrics BlockRunner::benchmark_individual_ops(
output = std::move(*outputs_[0]);
// release outputs explicitly to measure the time it takes
output = IValue();
millis = float_millis(hr_clock::now() - start).count();
millis = timer.MilliSeconds();
results.output_dealloc_time += millis;
}
}
@@ -1,9 +1,8 @@
#include <ATen/record_function.h>
#include <torch/nativert/executor/GraphExecutorBase.h>
#include <chrono>
#include <c10/util/Logging.h>
#include <caffe2/core/timer.h>
namespace torch::nativert {
@@ -96,6 +95,7 @@ ProfileMetrics GraphExecutorBase::benchmarkIndividualNodes(
}
// Execute kernels
caffe2::Timer timer;
executionFrame.withManagedMemory([&](auto) {
for (uint32_t i = 0; i < mainRuns; i++) {
for (auto inputs : inputsList) {
@@ -106,12 +106,9 @@ ProfileMetrics GraphExecutorBase::benchmarkIndividualNodes(
executionFrame.setIValue(inputValues[j]->id(), std::move(inputs[j]));
}
for (NodeIndex nodeIdx = 0; nodeIdx < nodeKernels_.size(); ++nodeIdx) {
const auto start = std::chrono::high_resolution_clock::now();
timer.Start();
nodeKernels_[nodeIdx]->compute(executionFrame);
const float millis =
std::chrono::duration<float, std::milli>(
std::chrono::high_resolution_clock::now() - start)
.count();
float millis = timer.MilliSeconds();
results.timePerNode[nodeIdx] += millis;
}
}