/usr/local/lib64/python3.6/site-packages/torch/include/caffe2/core
NameSizeModeActions
allocator.h1360644editdlrm
blob.h41680644editdlrm
blob_serialization.h107910644editdlrm
blob_serializer_base.h39050644editdlrm
blob_stats.h11270644editdlrm
common.h43290644editdlrm
common_cudnn.h98930644editdlrm
common_gpu.h214140644editdlrm
common_omp.h1560644editdlrm
context.h61740644editdlrm
context_base.h43820644editdlrm
context_gpu.h110140644editdlrm
cudnn_wrappers.h69560644editdlrm
db.h93520644editdlrm
distributions_stubs.h21610644editdlrm
event.h124200644editdlrm
event_cpu.h11920644editdlrm
export_c10_op_to_caffe2.h94870644editdlrm
export_caffe2_op_to_c10.h111010644editdlrm
flags.h740644editdlrm
graph.h52580644editdlrm
init.h64960644editdlrm
logging.h750644editdlrm
macros.h34260644editdlrm
memonger.h8170644editdlrm
module.h24730644editdlrm
net.h46340644editdlrm
net_async_base.h73970644editdlrm
net_async_scheduling.h9930644editdlrm
net_async_task.h8330644editdlrm
net_async_task_future.h19250644editdlrm
net_async_task_graph.h22530644editdlrm
net_async_tracing.h50930644editdlrm
net_dag_utils.h21460644editdlrm
net_parallel.h21440644editdlrm
net_simple.h26060644editdlrm
net_simple_refcount.h20970644editdlrm
numa.h720644editdlrm
observer.h38090644editdlrm
operator.h588720644editdlrm
operator_gradient.h102220644editdlrm
operator_schema.h184770644editdlrm
plan_executor.h2190644editdlrm
prof_dag_counters.h27510644editdlrm
qtensor.h66150644editdlrm
qtensor_serialization.h26240644editdlrm
scope_guard.h46750644editdlrm
static_tracepoint.h3980644editdlrm
static_tracepoint_elfx86.h55550644editdlrm
stats.h103650644editdlrm
storage.h7330644editdlrm
tensor.h186680644editdlrm
tensor_impl.h3510644editdlrm
tensor_int8.h4500644editdlrm
test_utils.h62850644editdlrm
timer.h12180644editdlrm
transform.h57410644editdlrm
types.h22480644editdlrm
workspace.h113050644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/torch/include/caffe2/core/stats.h (10365B)
#pragma once #include #include #include #include #include #include #include "caffe2/core/logging.h" #include "caffe2/core/static_tracepoint.h" namespace caffe2 { class TORCH_API StatValue { std::atomic v_{0}; public: int64_t increment(int64_t inc) { return v_ += inc; } int64_t reset(int64_t value = 0) { return v_.exchange(value); } int64_t get() const { return v_.load(); } }; struct TORCH_API ExportedStatValue { std::string key; int64_t value; std::chrono::time_point ts; }; /** * @brief Holds names and values of counters exported from a StatRegistry. */ using ExportedStatList = std::vector; using ExportedStatMap = std::unordered_map; TORCH_API ExportedStatMap toMap(const ExportedStatList& stats); /** * @brief Holds a map of atomic counters keyed by name. * * The StatRegistry singleton, accessed through StatRegistry::get(), holds * counters registered through the macro CAFFE_EXPORTED_STAT. Example of usage: * * struct MyCaffeClass { * MyCaffeClass(const std::string& instanceName): stats_(instanceName) {} * void run(int numRuns) { * try { * CAFFE_EVENT(stats_, num_runs, numRuns); * tryRun(numRuns); * CAFFE_EVENT(stats_, num_successes); * } catch (std::exception& e) { * CAFFE_EVENT(stats_, num_failures, 1, "arg_to_usdt", e.what()); * } * CAFFE_EVENT(stats_, usdt_only, 1, "arg_to_usdt"); * } * private: * struct MyStats { * CAFFE_STAT_CTOR(MyStats); * CAFFE_EXPORTED_STAT(num_runs); * CAFFE_EXPORTED_STAT(num_successes); * CAFFE_EXPORTED_STAT(num_failures); * CAFFE_STAT(usdt_only); * } stats_; * }; * * int main() { * MyCaffeClass a("first"); * MyCaffeClass b("second"); * for (int i = 0; i < 10; ++i) { * a.run(10); * b.run(5); * } * ExportedStatList finalStats; * StatRegistry::get().publish(finalStats); * } * * For every new instance of MyCaffeClass, a new counter is created with * the instance name as prefix. Everytime run() is called, the corresponding * counter will be incremented by the given value, or 1 if value not provided. * * Counter values can then be exported into an ExportedStatList. In the * example above, considering "tryRun" never throws, `finalStats` will be * populated as follows: * * first/num_runs 100 * first/num_successes 10 * first/num_failures 0 * second/num_runs 50 * second/num_successes 10 * second/num_failures 0 * * The event usdt_only is not present in ExportedStatList because it is declared * as CAFFE_STAT, which does not create a counter. * * Additionally, for each call to CAFFE_EVENT, a USDT probe is generated. * The probe will be set up with the following arguments: * - Probe name: field name (e.g. "num_runs") * - Arg #0: instance name (e.g. "first", "second") * - Arg #1: For CAFFE_EXPORTED_STAT, value of the updated counter * For CAFFE_STAT, -1 since no counter is available * - Args ...: Arguments passed to CAFFE_EVENT, including update value * when provided. * * It is also possible to create additional StatRegistry instances beyond * the singleton. These instances are not automatically populated with * CAFFE_EVENT. Instead, they can be populated from an ExportedStatList * structure by calling StatRegistry::update(). * */ class TORCH_API StatRegistry { std::mutex mutex_; std::unordered_map> stats_; public: /** * Retrieve the singleton StatRegistry, which gets populated * through the CAFFE_EVENT macro. */ static StatRegistry& get(); /** * Add a new counter with given name. If a counter for this name already * exists, returns a pointer to it. */ StatValue* add(const std::string& name); /** * Populate an ExportedStatList with current counter values. * If `reset` is true, resets all counters to zero. It is guaranteed that no * count is lost. */ void publish(ExportedStatList& exported, bool reset = false); ExportedStatList publish(bool reset = false) { ExportedStatList stats; publish(stats, reset); return stats; } /** * Update values of counters contained in the given ExportedStatList to * the values provided, creating counters that don't exist. */ void update(const ExportedStatList& data); ~StatRegistry(); }; struct TORCH_API Stat { std::string groupName; std::string name; Stat(const std::string& gn, const std::string& n) : groupName(gn), name(n) {} template int64_t increment(Unused...) { return -1; } }; class TORCH_API ExportedStat : public Stat { StatValue* value_; public: ExportedStat(const std::string& gn, const std::string& n) : Stat(gn, n), value_(StatRegistry::get().add(gn + "/" + n)) {} int64_t increment(int64_t value = 1) { return value_->increment(value); } template int64_t increment(T value, Unused1, Unused...) { return increment(value); } }; class TORCH_API AvgExportedStat : public ExportedStat { private: ExportedStat count_; public: AvgExportedStat(const std::string& gn, const std::string& n) : ExportedStat(gn, n + "/sum"), count_(gn, n + "/count") {} int64_t increment(int64_t value = 1) { count_.increment(); return ExportedStat::increment(value); } template int64_t increment(T value, Unused1, Unused...) { return increment(value); } }; class TORCH_API StdDevExportedStat : public ExportedStat { // Uses an offset (first_) to remove issue of cancellation // Variance is then (sumsqoffset_ - (sumoffset_^2) / count_) / (count_ - 1) private: ExportedStat count_; ExportedStat sumsqoffset_; ExportedStat sumoffset_; std::atomic first_{std::numeric_limits::min()}; int64_t const_min_{std::numeric_limits::min()}; public: StdDevExportedStat(const std::string& gn, const std::string& n) : ExportedStat(gn, n + "/sum"), count_(gn, n + "/count"), sumsqoffset_(gn, n + "/sumsqoffset"), sumoffset_(gn, n + "/sumoffset") {} int64_t increment(int64_t value = 1) { first_.compare_exchange_strong(const_min_, value); int64_t offset_value = first_.load(); int64_t orig_value = value; value -= offset_value; count_.increment(); sumsqoffset_.increment(value * value); sumoffset_.increment(value); return ExportedStat::increment(orig_value); } template int64_t increment(T value, Unused1, Unused...) { return increment(value); } }; class TORCH_API DetailedExportedStat : public ExportedStat { private: std::vector details_; public: DetailedExportedStat(const std::string& gn, const std::string& n) : ExportedStat(gn, n) {} void setDetails(const std::vector& detailNames) { details_.clear(); for (const auto& detailName : detailNames) { details_.emplace_back(groupName, name + "/" + detailName); } } template int64_t increment(T value, size_t detailIndex, Unused...) { if (detailIndex < details_.size()) { details_[detailIndex].increment(value); } return ExportedStat::increment(value); } }; class TORCH_API StaticStat : public Stat { private: StatValue* value_; public: StaticStat(const std::string& groupName, const std::string& name) : Stat(groupName, name), value_(StatRegistry::get().add(groupName + "/" + name)) {} int64_t increment(int64_t value = 1) { return value_->reset(value); } template int64_t increment(T value, Unused1, Unused...) { return increment(value); } }; namespace detail { template struct _ScopeGuard { T f_; std::chrono::high_resolution_clock::time_point start_; explicit _ScopeGuard(T f) : f_(f), start_(std::chrono::high_resolution_clock::now()) {} ~_ScopeGuard() { using namespace std::chrono; auto duration = high_resolution_clock::now() - start_; int64_t nanos = duration_cast(duration).count(); f_(nanos); } // Using implicit cast to bool so that it can be used in an 'if' condition // within CAFFE_DURATION macro below. /* implicit */ operator bool() { return true; } }; template _ScopeGuard ScopeGuard(T f) { return _ScopeGuard(f); } } // namespace detail #define CAFFE_STAT_CTOR(ClassName) \ ClassName(std::string name) : groupName(name) {} \ std::string groupName #define CAFFE_EXPORTED_STAT(name) \ ExportedStat name { \ groupName, #name \ } #define CAFFE_AVG_EXPORTED_STAT(name) \ AvgExportedStat name { \ groupName, #name \ } #define CAFFE_STDDEV_EXPORTED_STAT(name) \ StdDevExportedStat name { \ groupName, #name \ } #define CAFFE_DETAILED_EXPORTED_STAT(name) \ DetailedExportedStat name { \ groupName, #name \ } #define CAFFE_STAT(name) \ Stat name { \ groupName, #name \ } #define CAFFE_STATIC_STAT(name) \ StaticStat name { \ groupName, #name \ } #define CAFFE_EVENT(stats, field, ...) \ { \ auto __caffe_event_value_ = stats.field.increment(__VA_ARGS__); \ CAFFE_SDT( \ field, \ stats.field.groupName.c_str(), \ __caffe_event_value_, \ ##__VA_ARGS__); \ } #define CAFFE_DURATION(stats, field, ...) \ if (auto g = ::caffe2::detail::ScopeGuard([&](int64_t nanos) { \ CAFFE_EVENT(stats, field, nanos, ##__VA_ARGS__); \ })) } // namespace caffe2