From add6ff4aa07511e553fd666ed8a9584f38c4f89c Mon Sep 17 00:00:00 2001 From: PyTorch MergeBot Date: Sun, 23 Aug 2026 01:14:00 +0000 Subject: [PATCH] Revert "Remove caffe2/core/timer.h (#194228)" This reverts commit 7744b78e00ee0c4d136d59b01892f7ab7d77c336. 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)) --- binaries/compare_models_torch.cc | 1 + binaries/load_benchmark_torch.cc | 7 ++- binaries/speed_benchmark_torch.cc | 10 ++-- caffe2/core/timer.h | 48 +++++++++++++++++++ torch/csrc/jit/runtime/static/impl.cpp | 34 ++++++------- torch/nativert/executor/GraphExecutorBase.cpp | 11 ++--- 6 files changed, 76 insertions(+), 35 deletions(-) create mode 100644 caffe2/core/timer.h diff --git a/binaries/compare_models_torch.cc b/binaries/compare_models_torch.cc index 60ee619f7f97..c8338fe546a5 100644 --- a/binaries/compare_models_torch.cc +++ b/binaries/compare_models_torch.cc @@ -19,6 +19,7 @@ #include #include +#include #include #include #include diff --git a/binaries/load_benchmark_torch.cc b/binaries/load_benchmark_torch.cc index fc7eb88056e8..330955657ece 100644 --- a/binaries/load_benchmark_torch.cc +++ b/binaries/load_benchmark_torch.cc @@ -18,6 +18,7 @@ #include #include +#include "caffe2/core/timer.h" #include "caffe2/utils/string_utils.h" #include #include @@ -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 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( - high_resolution_clock::now() - bench_start) - .count(); + const double micros = static_cast(timer.MicroSeconds()); if (FLAGS_report_pep) { for (auto t : times) { std::cout << R"(PyTorchObserver {"type": "NET", "unit": "us", )" diff --git a/binaries/speed_benchmark_torch.cc b/binaries/speed_benchmark_torch.cc index 9e3396224d42..00b17ddd4748 100644 --- a/binaries/speed_benchmark_torch.cc +++ b/binaries/speed_benchmark_torch.cc @@ -18,6 +18,7 @@ #include #include +#include "caffe2/core/timer.h" #include "caffe2/utils/string_utils.h" #include #include @@ -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 times(FLAGS_iter, 0); + caffe2::Timer timer; + std::vector 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(stop - start); times.push_back(duration.count()); } - const double micros = duration( - 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; diff --git a/caffe2/core/timer.h b/caffe2/core/timer.h new file mode 100644 index 000000000000..a0384b0dbdbd --- /dev/null +++ b/caffe2/core/timer.h @@ -0,0 +1,48 @@ +#ifndef CAFFE2_CORE_TIMER_H_ +#define CAFFE2_CORE_TIMER_H_ + +#include + +#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( + std::chrono::duration_cast(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 start_time_; + C10_DISABLE_COPY_AND_ASSIGN(Timer); +}; +} + +#endif // CAFFE2_CORE_TIMER_H_ diff --git a/torch/csrc/jit/runtime/static/impl.cpp b/torch/csrc/jit/runtime/static/impl.cpp index 7d37f699de65..4f9612b66d1f 100644 --- a/torch/csrc/jit/runtime/static/impl.cpp +++ b/torch/csrc/jit/runtime/static/impl.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -22,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -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(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( - std::chrono::high_resolution_clock::now() - start) - .count(); + float millis = timer.MilliSeconds(); return millis / (static_cast(main_runs) * static_cast(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; - 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(nodes_.size()); for (const auto k : c10::irange(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; } } diff --git a/torch/nativert/executor/GraphExecutorBase.cpp b/torch/nativert/executor/GraphExecutorBase.cpp index bac68bc62151..5cec883376e1 100644 --- a/torch/nativert/executor/GraphExecutorBase.cpp +++ b/torch/nativert/executor/GraphExecutorBase.cpp @@ -1,9 +1,8 @@ #include #include -#include - #include +#include 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( - std::chrono::high_resolution_clock::now() - start) - .count(); + float millis = timer.MilliSeconds(); results.timePerNode[nodeIdx] += millis; } }