pytorch/caffe2/observers
Shai Szulanski 0ddaaf6a92 [codemod][caffe2] Run clang-format - 5/7
Summary:
This directory is opted-in to clang-format but is not format-clean. This blocks continuous formatting from being enabled on fbcode, and causes hassle for other codemods that leave inconsistent formatting. This diff runs clang-format, which is widely used and considered safe.

If you are unhappy with the formatting of a particular block, please *accept this diff* and then in a stacked commit undo the change and wrap that code in `// clang-format off` and `// clang-format on`, or `/* clang-format off */` and `/* clang-format on */`.

drop-conflicts

Test Plan: sandcastleit

Reviewed By: jerryzh168

Differential Revision: D22311706

fbshipit-source-id: 1ca59a82e96156a4a5dfad70ba3e64d44c5e762a
2020-06-30 15:45:11 -07:00
..
CMakeLists.txt
operator_attaching_net_observer.h Replace c10::guts::stuff with std::stuff (#30915) 2019-12-16 13:57:19 -08:00
profile_observer.cc
profile_observer.h
README.md
runcnt_observer.cc preprocessor cleanup (#33957) 2020-03-02 13:37:19 -08:00
runcnt_observer.h [codemod][caffe2] Run clang-format - 5/7 2020-06-30 15:45:11 -07:00
time_observer_test.cc Replace c10::guts::stuff with std::stuff (#30915) 2019-12-16 13:57:19 -08:00
time_observer.cc
time_observer.h [codemod][caffe2] Run clang-format - 5/7 2020-06-30 15:45:11 -07:00

Observers

Usage

Observers are a small framework that allow users to attach code to the execution of SimpleNets and Operators.

An example of an Observer is the TimeObserver, used as follows:

C++

unique_ptr<TimeObserver<NetBase>> net_ob =
    make_unique<TimeObserver<NetBase>>(net.get());
auto* ob = net->AttachObserver(std::move(net_ob));
net->Run();
LOG(INFO) << "av time children: " << ob->average_time_children();
LOG(INFO) << "av time: " << ob->average_time();

Python

model.net.AttachObserver("TimeObserver")
ws.RunNet(model.net)
ob = model.net.GetObserver("TimeObserver")

print("av time children:", ob.average_time_children())
print("av time:", ob.average_time())

Histogram Observer

Creates a histogram for the values of weights and activations

model.net.AddObserver("HistogramObserver",
                      "histogram.txt", # filename
                      2014, # number of bins in histogram
                      32 # Dumping frequency
                      )
ws.RunNet(model.net)

This will generate a histogram for the activations and store it in histogram.txt

Implementing An Observer

To implement an observer you must inherit from ObserverBase and implement the Start and Stop functions.

Observers are instantiated with a subject of a generic type, such as a Net or Operator. The observer framework is built to be generic enough to "observe" various other types, however.