Files
pytorch/torch/csrc/jit/runtime/logging.cpp
cyyever 24ca7e91e6 [1/N] Use internal linkage in torch/csrc C++ files. (#150930)
Turn more functions and variables into static if they are not used outside the cpp files. Unused functions are removed.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/150930
Approved by: https://github.com/Skylion007

Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
2025-04-11 02:19:31 +00:00

71 lines
1.9 KiB
C++

#include <torch/csrc/jit/runtime/logging.h>
#include <atomic>
#include <chrono>
#include <mutex>
#include <stdexcept>
#include <unordered_map>
namespace torch::jit::logging {
// TODO: multi-scale histogram for this thing
void LockingLogger::addStatValue(const std::string& stat_name, int64_t val) {
std::unique_lock<std::mutex> lk(m);
auto& raw_counter = raw_counters[stat_name];
raw_counter.sum += val;
raw_counter.count++;
}
int64_t LockingLogger::getCounterValue(const std::string& name) const {
std::unique_lock<std::mutex> lk(m);
if (!raw_counters.count(name)) {
return 0;
}
AggregationType type =
agg_types.count(name) ? agg_types.at(name) : AggregationType::SUM;
const auto& raw_counter = raw_counters.at(name);
switch (type) {
case AggregationType::SUM: {
return raw_counter.sum;
} break;
case AggregationType::AVG: {
return raw_counter.sum / raw_counter.count;
} break;
}
throw std::runtime_error("Unknown aggregation type!");
}
void LockingLogger::setAggregationType(
const std::string& stat_name,
AggregationType type) {
agg_types[stat_name] = type;
}
static std::atomic<LoggerBase*> global_logger{new NoopLogger()};
LoggerBase* getLogger() {
return global_logger.load();
}
LoggerBase* setLogger(LoggerBase* logger) {
LoggerBase* previous = global_logger.load();
while (!global_logger.compare_exchange_strong(previous, logger)) {
previous = global_logger.load();
}
return previous;
}
JITTimePoint timePoint() {
return JITTimePoint{std::chrono::high_resolution_clock::now()};
}
void recordDurationSince(const std::string& name, const JITTimePoint& tp) {
auto end = std::chrono::high_resolution_clock::now();
// Measurement in microseconds.
auto seconds = std::chrono::duration<double>(end - tp.point).count() * 1e9;
logging::getLogger()->addStatValue(name, seconds);
}
} // namespace torch::jit::logging