xref: /aosp_15_r20/external/pytorch/torch/csrc/profiler/stubs/base.cpp (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #include <torch/csrc/profiler/stubs/base.h>
2 
3 #include <c10/util/Exception.h>
4 
5 namespace torch {
6 namespace profiler {
7 namespace impl {
8 
9 ProfilerStubs::~ProfilerStubs() = default;
10 
11 namespace {
12 struct DefaultStubs : public ProfilerStubs {
DefaultStubstorch::profiler::impl::__anon725146a60111::DefaultStubs13   DefaultStubs(const char* name) : name_{name} {}
14 
recordtorch::profiler::impl::__anon725146a60111::DefaultStubs15   void record(c10::DeviceIndex*, ProfilerVoidEventStub*, int64_t*)
16       const override {
17     fail();
18   }
elapsedtorch::profiler::impl::__anon725146a60111::DefaultStubs19   float elapsed(const ProfilerVoidEventStub*, const ProfilerVoidEventStub*)
20       const override {
21     fail();
22     return 0.f;
23   }
marktorch::profiler::impl::__anon725146a60111::DefaultStubs24   void mark(const char*) const override {
25     fail();
26   }
rangePushtorch::profiler::impl::__anon725146a60111::DefaultStubs27   void rangePush(const char*) const override {
28     fail();
29   }
rangePoptorch::profiler::impl::__anon725146a60111::DefaultStubs30   void rangePop() const override {
31     fail();
32   }
enabledtorch::profiler::impl::__anon725146a60111::DefaultStubs33   bool enabled() const override {
34     return false;
35   }
onEachDevicetorch::profiler::impl::__anon725146a60111::DefaultStubs36   void onEachDevice(std::function<void(int)>) const override {
37     fail();
38   }
synchronizetorch::profiler::impl::__anon725146a60111::DefaultStubs39   void synchronize() const override {
40     fail();
41   }
42   ~DefaultStubs() override = default;
43 
44  private:
failtorch::profiler::impl::__anon725146a60111::DefaultStubs45   void fail() const {
46     AT_ERROR(name_, " used in profiler but not enabled.");
47   }
48 
49   const char* const name_;
50 };
51 } // namespace
52 
53 #define REGISTER_DEFAULT(name, upper_name)                                   \
54   namespace {                                                                \
55   const DefaultStubs default_##name##_stubs{#upper_name};                    \
56   constexpr const DefaultStubs* default_##name##_stubs_addr =                \
57       &default_##name##_stubs;                                               \
58                                                                              \
59   /* Constant initialization, so it is guaranteed to be initialized before*/ \
60   /* static initialization calls which may invoke register<name>Methods*/    \
61   inline const ProfilerStubs*& name##_stubs() {                              \
62     static const ProfilerStubs* stubs_ =                                     \
63         static_cast<const ProfilerStubs*>(default_##name##_stubs_addr);      \
64     return stubs_;                                                           \
65   }                                                                          \
66   } /*namespace*/                                                            \
67                                                                              \
68   const ProfilerStubs* name##Stubs() {                                       \
69     return name##_stubs();                                                   \
70   }                                                                          \
71                                                                              \
72   void register##upper_name##Methods(ProfilerStubs* stubs) {                 \
73     name##_stubs() = stubs;                                                  \
74   }
75 
76 REGISTER_DEFAULT(cuda, CUDA)
77 REGISTER_DEFAULT(itt, ITT)
78 REGISTER_DEFAULT(privateuse1, PrivateUse1)
79 #undef REGISTER_DEFAULT
80 
81 } // namespace impl
82 } // namespace profiler
83 } // namespace torch
84