TracerMode.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma once
  2. #include <c10/core/impl/LocalDispatchKeySet.h>
  3. #include <c10/macros/Export.h>
  4. #include <c10/macros/Macros.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
  15. // side effect 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`
  44. // after 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.
  71. // `tracer::impl::NoTracerDispatchMode`
  72. //
  73. // We don't need to always set both guards together to keep semantics
  74. // unchanged. For the follow use cases of `AutoDispatchBelowADInplaceOrView`
  75. // we don't need set the new tracer guard:
  76. //
  77. // * Script-generated VariableType kernels. The guard is not necessary as
  78. // tracing is already disabled explicitly by `setTracingState(null)` in
  79. // generated TraceType kernels - we could keep it as is or use the new guard
  80. // instead.
  81. //
  82. // * Custom ops. Will be handled by fallback kernel for `Tracer`.
  83. //
  84. // * Functions that are not likely to be called in tracing context (no python
  85. // binding / not an operator), e.g.: all mobile forward() wrappers, test
  86. // binaries, and etc.
  87. //
  88. // * Where new threads are spawned, e.g.: ATen/native/ConvolutionMM2d.cpp.
  89. // It's not necessary as tracing is off by default.
  90. //
  91. // For the rest of cases we might need have both:
  92. //
  93. // * Functions that might be reachable from eager mode python (especially
  94. // factory methods), e.g.:
  95. // `internal_new_from_data()` in `torch/csrc/utils/tensor_new.cpp`.
  96. // Without the new guard it will add `aten::empty` to the traced graph.
  97. //
  98. // * Some manually maintained functions, e.g.:
  99. // `torch/csrc/autograd/VariableTypeManual.cpp`.
  100. // Set the new guard if it's not obvious whether `setTracingState(null)`
  101. // has been called before it reaches the `AutoDispatchBelowADInplaceOrView`
  102. // guard.
  103. //
  104. // We might need tweak the usage of the new guard to optimize/fix things.
  105. // It should only affect the correctness of tracing function, because the
  106. // guard is essentially no-op when the master `setTracingState()` switch is
  107. // off.
  108. namespace at {
  109. // TODO: move this from `at::` to `jit::torch::` after
  110. // `aten/src/ATen/cpp_custom_type_hack.h` is removed.
  111. namespace tracer {
  112. namespace impl {
  113. static inline bool is_dispatch_enabled() {
  114. return c10::impl::tls_is_dispatch_key_included(at::DispatchKey::Tracer) &&
  115. !c10::impl::tls_is_dispatch_key_excluded(at::DispatchKey::Tracer);
  116. }
  117. static inline void set_dispatch_enabled(bool enabled) {
  118. TORCH_INTERNAL_ASSERT(
  119. !c10::impl::tls_is_dispatch_key_excluded(at::DispatchKey::Tracer),
  120. "Cannot enable tracing within the scope of NoTracerDispatchMode!");
  121. c10::impl::tls_set_dispatch_key_included(at::DispatchKey::Tracer, enabled);
  122. }
  123. struct NoTracerDispatchMode {
  124. c10::impl::ExcludeDispatchKeyGuard guard_{at::DispatchKey::Tracer};
  125. };
  126. } // namespace impl
  127. } // namespace tracer
  128. } // namespace at