Skip to content

Commit 7b97eae

Browse files
xcheng16facebook-github-bot
authored andcommitted
Add module level qpl logging. (pytorch#30906)
Summary: Pull Request resolved: pytorch#30906 Add mobile module observer to measure performance of each method run. ghstack-source-id: 95120194 Test Plan: Run pytext model through BI cloaking flow on lite-interpreter and verify logs are sent: 1. buck install -r fb4a 2. Go to internal setting and find MobileConfig, search for android_bi_infra_cloaking_iab_models and set the following params: a. sample_rate: 1.0 b. enabled: true c. use_bytedoc_pytorch_model: true d. use_bytedoc_caffe2_model: false e. use_full_jit: false 3. Go back to new feed and scroll down until find an ads which will direct you to offsite webpage; 4. Click on the ads, wait for the offsite ads loads; 5. Click back to news feed; 6. Go to scuba table: https://fburl.com/scuba/4fghwp0b and see all the operator runs have been logged: {F223456981} Reviewed By: ljk53 Differential Revision: D18702116 fbshipit-source-id: a9f07eee684e3022cef5ba3c5934f30f20192a85
1 parent 118f1c6 commit 7b97eae

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

torch/csrc/jit/mobile/module.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,24 @@ c10::IValue Module::run_method(const std::string& method_name, Stack stack) {
2828
debug_info->setModelName(name());
2929
debug_info->setMethodName(method_name);
3030
at::setThreadLocalDebugInfo(debug_info);
31+
32+
auto observer = torch::observerConfig().getModuleObserver();
33+
if (observer) {
34+
observer->onEnter();
35+
}
3136
#endif
3237

3338
auto m = find_method(method_name);
3439
stack.insert(stack.begin(), object_);
3540
m->run(stack);
36-
return stack.front();
41+
c10::IValue result = stack.front();
42+
43+
#if defined(PYTORCH_MOBILE_OBSERVER)
44+
if (observer) {
45+
observer->onExit();
46+
}
47+
#endif
48+
return result;
3749
}
3850

3951
Function* Module::find_method(const std::string& basename) const {

torch/csrc/jit/mobile/observer.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <torch/csrc/jit/mobile/observer.h>
2+
3+
namespace torch {
4+
5+
MobileObserverConfig& observerConfig() {
6+
static MobileObserverConfig instance;
7+
return instance;
8+
}
9+
10+
} // namespace torch

torch/csrc/jit/mobile/observer.h

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <string>
44
#include <ATen/ThreadLocalDebugInfo.h>
5+
56
namespace torch {
67

78
class MobileDebugInfo
@@ -39,4 +40,27 @@ class MobileDebugInfo
3940
size_t op_idx_ = 0;
4041
};
4142

43+
class MobileModuleObserver {
44+
public:
45+
virtual ~MobileModuleObserver() = default;
46+
47+
virtual void onEnter() {}
48+
virtual void onExit() {}
49+
};
50+
51+
class MobileObserverConfig {
52+
public:
53+
void setModuleObserver(std::unique_ptr<MobileModuleObserver> reporter) {
54+
module_observer_ = std::move(reporter);
55+
}
56+
MobileModuleObserver* getModuleObserver() {
57+
return module_observer_.get();
58+
}
59+
60+
private:
61+
std::unique_ptr<MobileModuleObserver> module_observer_;
62+
};
63+
64+
MobileObserverConfig& observerConfig();
65+
4266
} // namespace torch

0 commit comments

Comments
 (0)