TracerMode.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include <c10/core/impl/LocalDispatchKeySet.h>
  3. #include <c10/macros/Macros.h>
  4. #include <c10/macros/Export.h>
  5. // NOTE [Tracing Mode Switches]
  6. //
  7. // Historically, tracing function was controlled by two switches:
  8. //
  9. // - `AutoDispatchBelowADInplaceOrView` guard
  10. //
  11. // Tracing function used to be script-generated inside `VariableType_*.cpp`
  12. // kernels, sharing the same `Autograd` dispatch key with autograd function.
  13. // Therefore, before tracing function was moved out of VariableType,
  14. // `AutoDispatchBelowADInplaceOrView` guard can also disable tracing as a side effect
  15. // of disabling `Autograd` dispatching.
  16. //
  17. // - `setTracingState()` API in `torch/csrc/jit/frontend/tracer.h`
  18. //
  19. // It stores tracing data in a `TracingState` object in TLS. If the
  20. // `TracingState` object in TLS is `null`, then tracing is paused.
  21. //
  22. // The `TracingState` object is created in `tracer::trace()` - the main
  23. // entrance of tracing function. It's temporarily set to `null` inside
  24. // generated VariableType (now TraceType) to bypass tracing for intermediate
  25. // ops (ops being called by other ops). After the intermediate op call
  26. // finishes it's set back to the original `TracingState` object.
  27. //
  28. // The `TracingState` obect in TLS can also be read/written via its Python
  29. // binding in `python_tracer.cpp`, and `get/setTracingState()` C++ APIs,
  30. // which are also exposed as `TORCH_API`.
  31. //
  32. // Two new switches were introduced since tracing function was moved out of
  33. // VariableType:
  34. //
  35. // - `tracer::impl::set_dispatch_enabled()` API
  36. //
  37. // Unlike the special `Autograd` dispatch key which is included in dispatch
  38. // key set by default, `Tracer` dispatch key is off by default. The
  39. // dispatching switch can be toggled via this new API.
  40. //
  41. // - `tracer::impl::NoTracerDispatchMode` guard
  42. //
  43. // It's used to cover the old semantics of `AutoDispatchBelowADInplaceOrView` after
  44. // tracing was moved out of VariableType.
  45. //
  46. // Before tracing function was moved out of VariableType, tracing was enabled
  47. // when the following conditions are satisfied:
  48. //
  49. // 1) `TracingState` object in TLS != null;
  50. // - Either inside the execution scope of `tracer::trace()`, or
  51. // - Eagerly called `setTracingState()` with non-null object.
  52. // 2) Not inside `AutoDispatchBelowADInplaceOrView` scope;
  53. //
  54. // After:
  55. //
  56. // 1) `TracingState` object in TLS != null;
  57. // 2) Has called `tracer::impl::set_dispatch_enabled(true)`;
  58. // 3) Not inside `tracer::impl::NonDispatchGuard` scope;
  59. //
  60. // [TODOs]
  61. //
  62. // - `setTracingState()` v.s. `tracer::impl::set_dispatch_enabled()`
  63. //
  64. // Currently `set_dispatch_enabled()` is set/unset inside `setTracingState()`
  65. // to keep the semantics exactly the same as before - it's confusing to keep
  66. // both switches, though. We should consider simplifying/limiting the exposed
  67. // `setTracingState()` Python/C++ APIs (and other APIs calling it) so that
  68. // these two can be unified.
  69. //
  70. // - `AutoDispatchBelowADInplaceOrView` v.s. `tracer::impl::NoTracerDispatchMode`
  71. //
  72. // We don't need to always set both guards together to keep semantics
  73. // unchanged. For the follow use cases of `AutoDispatchBelowADInplaceOrView` we don't
  74. // need set the new tracer guard:
  75. //
  76. // * Script-generated VariableType kernels. The guard is not necessary as
  77. // tracing is already disabled explicitly by `setTracingState(null)` in
  78. // generated TraceType kernels - we could keep it as is or use the new guard
  79. // instead.
  80. //
  81. // * Custom ops. Will be handled by fallback kernel for `Tracer`.
  82. //
  83. // * Functions that are not likely to be called in tracing context (no python
  84. // binding / not an operator), e.g.: all mobile forward() wrappers, test
  85. // binaries, and etc.
  86. //
  87. // * Where new threads are spawned, e.g.: ATen/native/ConvolutionMM2d.cpp.
  88. // It's not necessary as tracing is off by default.
  89. //
  90. // For the rest of cases we might need have both:
  91. //
  92. // * Functions that might be reachable from eager mode python (especially
  93. // factory methods), e.g.:
  94. // `internal_new_from_data()` in `torch/csrc/utils/tensor_new.cpp`.
  95. // Without the new guard it will add `aten::empty` to the traced graph.
  96. //
  97. // * Some manually maintained functions, e.g.:
  98. // `torch/csrc/autograd/VariableTypeManual.cpp`.
  99. // Set the new guard if it's not obvious whether `setTracingState(null)`
  100. // has been called before it reaches the `AutoDispatchBelowADInplaceOrView` guard.
  101. //
  102. // We might need tweak the usage of the new guard to optimize/fix things.
  103. // It should only affect the correctness of tracing function, because the
  104. // guard is essentially no-op when the master `setTracingState()` switch is
  105. // off.
  106. namespace at {
  107. // TODO: move this from `at::` to `jit::torch::` after
  108. // `aten/src/ATen/cpp_custom_type_hack.h` is removed.
  109. namespace tracer {
  110. namespace impl {
  111. static inline bool is_dispatch_enabled() {
  112. return c10::impl::tls_is_dispatch_key_included(at::DispatchKey::Tracer) &&
  113. !c10::impl::tls_is_dispatch_key_excluded(at::DispatchKey::Tracer);
  114. }
  115. static inline void set_dispatch_enabled(bool enabled) {
  116. TORCH_INTERNAL_ASSERT(
  117. !c10::impl::tls_is_dispatch_key_excluded(at::DispatchKey::Tracer),
  118. "Cannot enable tracing within the scope of NoTracerDispatchMode!");
  119. c10::impl::tls_set_dispatch_key_included(at::DispatchKey::Tracer, enabled);
  120. }
  121. struct NoTracerDispatchMode {
  122. c10::impl::ExcludeDispatchKeyGuard guard_{at::DispatchKey::Tracer};
  123. };
  124. } // namespace impl
  125. } // namespace tracer
  126. } // namespace at