3 #include "caffe2/core/operator.h" 4 #include "caffe2/core/stats.h" 5 #include "caffe2/core/tensor.h" 14 bool RunOnDevice()
override {
15 *OperatorBase::Output<std::unique_ptr<StatRegistry>>(0) =
25 reset_(GetSingleArgument<bool>(
"reset",
true)) {}
27 bool RunOnDevice()
override {
28 auto registry = InputSize() > 0
29 ? OperatorBase::Input<std::unique_ptr<StatRegistry>>(0).
get()
31 auto* keys = Output(0);
32 auto* values = Output(1);
33 auto* timestamps = Output(2);
34 auto data = registry->publish(reset_);
35 keys->Resize(data.size());
36 values->Resize(data.size());
37 timestamps->Resize(data.size());
38 auto* pkeys = keys->mutable_data<std::string>();
39 auto* pvals = values->mutable_data<int64_t>();
40 auto* ptimestamps = timestamps->mutable_data<int64_t>();
42 for (
const auto& stat : data) {
43 pkeys[i] = std::move(stat.key);
44 pvals[i] = stat.value;
46 std::chrono::nanoseconds(stat.ts.time_since_epoch()).count();
61 bool RunOnDevice()
override {
62 const auto& keys = Input(0);
63 const auto& values = Input(1);
64 auto registry = InputSize() == 3
65 ? OperatorBase::Input<std::unique_ptr<StatRegistry>>(2).
get()
67 CAFFE_ENFORCE_EQ(keys.size(), values.size());
69 auto* pkeys = keys.data<std::string>();
70 auto* pvals = values.data<int64_t>();
72 for (
auto& stat : data) {
74 stat.value = pvals[i];
77 registry->update(data);
85 : running_(
false), stat_(name) {}
88 CAFFE_ENFORCE(!running_,
"Called TimerBegin on an already running timer.");
90 start_ = std::chrono::high_resolution_clock::now();
94 CAFFE_ENFORCE(running_,
"Called TimerEnd on a stopped timer.");
95 using namespace std::chrono;
96 auto duration = high_resolution_clock::now() - start_;
97 auto nanos = duration_cast<nanoseconds>(duration).count();
98 CAFFE_EVENT(stat_, time_ns, nanos);
103 CAFFE_ENFORCE(running_,
"Called TimerGet on a stopped timer.");
104 using namespace std::chrono;
105 auto duration = high_resolution_clock::now() - start_;
106 auto nanos = duration_cast<nanoseconds>(duration).count();
112 std::chrono::high_resolution_clock::time_point start_;
115 CAFFE_STAT_CTOR(TimerStat);
116 CAFFE_AVG_EXPORTED_STAT(time_ns);
123 given_name_(GetSingleArgument<std::string>(
125 operator_def.output().Get(0))),
126 timer_([
this]() {
return given_name_; }()) {}
128 bool RunOnDevice()
override {
129 *OperatorBase::Output<TimerInstance*>(0) = &timer_;
135 const std::string given_name_;
143 bool RunOnDevice()
override {
144 OperatorBase::Input<TimerInstance*>(0)->end();
153 bool RunOnDevice()
override {
154 int64_t nanos = OperatorBase::Input<TimerInstance*>(0)->get_ns();
155 OperatorBase::Input<TimerInstance*>(0)->end();
156 auto* res = OperatorBase::Output<TensorCPU>(0);
158 res->template mutable_data<int64_t>()[0] = nanos;
167 bool RunOnDevice()
override {
168 int64_t nanos = OperatorBase::Input<TimerInstance*>(0)->get_ns();
169 auto* res = OperatorBase::Output<TensorCPU>(0);
171 res->template mutable_data<int64_t>()[0] = nanos;
179 statsName_(GetSingleArgument<std::string>(
"stats_name",
"utilization")),
180 stat_([
this]() {
return statsName_; }()) {}
182 bool RunOnDevice()
override {
183 float utilization = Input(0).template data<float>()[0];
186 CAFFE_EVENT(stat_, cpu_utilization, (
int)(utilization * 100));
191 std::string statsName_;
193 CAFFE_STAT_CTOR(CpuStats);
194 CAFFE_EXPORTED_STAT(cpu_utilization);
208 OPERATOR_SCHEMA(StatRegistryCreate)
212 Create a StatRegistry object that will contain a map of performance counters 213 keyed by name. A StatRegistry is used to gather and retrieve performance 214 counts throughout the caffe2 codebase. 216 .Output(0, "handle",
"A Blob pointing to the newly created StatRegistry.");
218 OPERATOR_SCHEMA(StatRegistryUpdate)
222 Update the given StatRegistry, or the global StatRegistry, 223 with the values of counters for the given keys. 225 .Input(0, "keys",
"1D string tensor with the key names to update.")
226 .Input(1,
"values",
"1D int64 tensor with the values to update.")
230 "If provided, update the given StatRegistry. " 231 "Otherwise, update the global singleton.");
233 OPERATOR_SCHEMA(StatRegistryExport)
239 "If provided, export values from given StatRegistry." 240 "Otherwise, export values from the global singleton StatRegistry.")
241 .Output(0,
"keys",
"1D string tensor with exported key names")
242 .Output(1,
"values",
"1D int64 tensor with exported values")
243 .Output(2,
"timestamps",
"The unix timestamp at counter retrieval.")
246 "(default true) Whether to atomically reset the counters afterwards.");
248 OPERATOR_SCHEMA(TimerBegin)
252 Start a wallclock timer, returning a pointer to it. 253 The timer is stopped by calling TimerEnd)DOC") 254 .Arg("counter_name",
"Name of the timer. If not provided, use output name.")
255 .Output(0,
"timer",
"Pointer to timer, to be passed to TimerEnd.");
257 OPERATOR_SCHEMA(TimerEnd)
260 .SetDoc(
"Stop a timer started with TimerBegin, publishing a CAFFE_EVENT")
261 .Input(0,
"timer",
"Pointer to timer, obtained from TimerBegin.");
263 OPERATOR_SCHEMA(TimerGetAndEnd)
266 .SetDoc(R
"DOC(Queries the current time of a timer in nanos, stops the timer 267 publishing a CAFFE_EVENT)DOC") 268 .Input(0, "timer",
"Pointer to timer, obtained from TimerBegin.")
269 .Output(0,
"nanos",
"nanoseconds in int64");
271 OPERATOR_SCHEMA(TimerGet)
274 .SetDoc(R
"DOC(Queries the current time of a timer in nanos)DOC") 275 .Input(0, "timer",
"Pointer to timer, obtained from TimerBegin.")
276 .Output(0,
"nanos",
"nanoseconds in int64");
278 OPERATOR_SCHEMA(CpuUtilizationReport)
281 .SetDoc(R
"DOC(Report the delta in max CPU utilization observed so far in the 286 "Delta in max CPU utilization observed, in percentage as a float value")
287 .Arg(
"stats_name",
"String name of the stat entry holding CPU utilization");
290 CAFFE_KNOWN_TYPE(std::unique_ptr<caffe2::StatRegistry>);
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
std::vector< ExportedStatValue > ExportedStatList
Holds names and values of counters exported from a StatRegistry.
static StatRegistry & get()
Retrieve the singleton StatRegistry, which gets populated through the CAFFE_EVENT macro...
Holds a map of atomic counters keyed by name.