Experimental logging/counters API (#18235)
authorJames Reed <jamesreed@fb.com>
Sat, 30 Mar 2019 00:06:08 +0000 (17:06 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sat, 30 Mar 2019 00:14:03 +0000 (17:14 -0700)
commit85f36014e2628fe291e94be8e5d156b4e6015afd
treed76d0f458e6c529a5025562bcc134483312936ad
parente2fd1d966fdf9ff384ba9d9e302f56146029f9f2
Experimental logging/counters API (#18235)

Summary:
This defines a generic counters API that users can utilize to provide monitoring functionality in e.g. a production service. We expose both counters for runtime internals as well as a TorchScript API to create user-defined counters. Synopsis of the API:

- `torch/csrc/jit/script/logging.h` specifies the externally-facing API in C++
- `torch/jit/_logging.py` specifies the Python API

We use an interface, `LoggerBase`, to define the interactions between users and a logging backend. Implementing a subclass of `LoggerBase` allows the user to handle these events in a custom way, such as logging into a DB or calling into an infra-specific counters API.

From the frontend perspective, we can create log events in two ways:
1. We provide an `add_stat_value(name, val)` function. This calls into the Logger backend with a key/value pair. For example, we might call `add_stat_value('foo', 1)` to bump an event counter.
2. We provide a `time_point()` function to record a timestamp in nanoseconds. This can be used in conjunction with `add_stat_value` to record runtime wall clock durations.

Examples of frontend usage can be found in `test_jit.py TestLogging`.

We provide a trivial `LockingLogger` implementation as an example and for testing purposes. It is likely not ready for production usage. It demonstrates that a backend implementing the API can do things like specify aggregation types and report these aggregate stats via the `get_counters()` API.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18235

Differential Revision: D14545060

Pulled By: jamesr66a

fbshipit-source-id: 04099543a1898cfdd411511e46e03d5dce9b4881
13 files changed:
.gitignore
aten/src/ATen/core/interned_strings.h
test/test_jit.py
tools/build_variables.py
torch/CMakeLists.txt
torch/csrc/jit/graph_executor.cpp
torch/csrc/jit/interpreter.cpp
torch/csrc/jit/ir.cpp
torch/csrc/jit/register_prim_ops.cpp
torch/csrc/jit/script/init.cpp
torch/csrc/jit/script/logging.cpp [new file with mode: 0644]
torch/csrc/jit/script/logging.h [new file with mode: 0644]
torch/jit/_logging.py [new file with mode: 0644]