mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/69567 This exposes torch.monitor events and stats via pybind11 to the underlying C++ implementation. * The registration interface is a tad different since it takes a lambda function in Python where as in C++ it's a full class. * This has a small amount of changes to the counter interfaces since there's no way to create an initializer list at runtime so they now also take a vector. * Only double based stats are provided in Python since it's intended more for high level stats where float imprecision shouldn't be an issue. This can be changed down the line if need arises. ``` events = [] def handler(event): events.append(event) handle = register_event_handler(handler) log_event(Event(type="torch.monitor.TestEvent", timestamp=datetime.now(), metadata={"foo": 1.0})) ``` D32969391 is now included in this diff. This cleans up the naming for events. type is now name, message is gone, and metadata is renamed data. Test Plan: buck test //caffe2/test:monitor //caffe2/test/cpp/monitor:monitor Reviewed By: kiukchung Differential Revision: D32924141 fbshipit-source-id: 563304c2e3261a4754e40cca39fc64c5a04b43e8
71 lines
1.5 KiB
C++
71 lines
1.5 KiB
C++
#include <torch/csrc/monitor/counters.h>
|
|
#include <torch/csrc/monitor/events.h>
|
|
|
|
#include <sstream>
|
|
#include <unordered_set>
|
|
|
|
namespace torch {
|
|
namespace monitor {
|
|
|
|
const char* aggregationName(Aggregation agg) {
|
|
switch (agg) {
|
|
case Aggregation::NONE:
|
|
return "none";
|
|
case Aggregation::VALUE:
|
|
return "value";
|
|
case Aggregation::MEAN:
|
|
return "mean";
|
|
case Aggregation::COUNT:
|
|
return "count";
|
|
case Aggregation::SUM:
|
|
return "sum";
|
|
case Aggregation::MAX:
|
|
return "max";
|
|
case Aggregation::MIN:
|
|
return "min";
|
|
default:
|
|
throw std::runtime_error(
|
|
"unknown aggregation: " + std::to_string(static_cast<int>(agg)));
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
struct Stats {
|
|
std::mutex mu;
|
|
|
|
std::unordered_set<Stat<double>*> doubles;
|
|
std::unordered_set<Stat<int64_t>*> int64s;
|
|
};
|
|
|
|
Stats& stats() {
|
|
static Stats stats;
|
|
return stats;
|
|
}
|
|
} // namespace
|
|
|
|
namespace detail {
|
|
void registerStat(Stat<double>* stat) {
|
|
std::lock_guard<std::mutex> guard(stats().mu);
|
|
|
|
stats().doubles.insert(stat);
|
|
}
|
|
void registerStat(Stat<int64_t>* stat) {
|
|
std::lock_guard<std::mutex> guard(stats().mu);
|
|
|
|
stats().int64s.insert(stat);
|
|
}
|
|
void unregisterStat(Stat<double>* stat) {
|
|
std::lock_guard<std::mutex> guard(stats().mu);
|
|
|
|
stats().doubles.erase(stat);
|
|
}
|
|
void unregisterStat(Stat<int64_t>* stat) {
|
|
std::lock_guard<std::mutex> guard(stats().mu);
|
|
|
|
stats().int64s.erase(stat);
|
|
}
|
|
} // namespace detail
|
|
|
|
} // namespace monitor
|
|
} // namespace torch
|