pytorch/caffe2/observers
Shashank Chaudhry 06d1be2447 [NOOP][clangformat][codemod] Enable CLANGFORMAT for caffe2/caffe2/* (#67624)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/67624

Test Plan: Visual inspection. Sandcastle.

Reviewed By: malfet

Differential Revision: D31986628

fbshipit-source-id: c872bded7325997a2945dbf5d4d052628dcb3659
2021-11-02 22:14:04 -07:00
..
CMakeLists.txt Remove GPU dependency from ProfileObserver (#17592) 2019-03-04 10:00:46 -08:00
operator_attaching_net_observer.h Replace c10::guts::stuff with std::stuff (#30915) 2019-12-16 13:57:19 -08:00
profile_observer.cc Remove GPU dependency from ProfileObserver (#17592) 2019-03-04 10:00:46 -08:00
profile_observer.h [NOOP][clangformat][codemod] Enable CLANGFORMAT for caffe2/caffe2/* (#67624) 2021-11-02 22:14:04 -07:00
README.md
runcnt_observer.cc preprocessor cleanup (#33957) 2020-03-02 13:37:19 -08:00
runcnt_observer.h [NOOP][clangformat][codemod] Enable CLANGFORMAT for caffe2/caffe2/* (#67624) 2021-11-02 22:14:04 -07:00
time_observer_test.cc Disable avoid-non-const-global-variables lint check (#62008) 2021-07-22 18:04:40 -07:00
time_observer.cc Make PyTorch code-base clang-tidy compliant (#56892) 2021-04-28 14:10:25 -07:00
time_observer.h [NOOP][clangformat][codemod] Enable CLANGFORMAT for caffe2/caffe2/* (#67624) 2021-11-02 22:14:04 -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.