DynamicLayer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) Facebook, Inc. and its affiliates.
  2. // All rights reserved.
  3. //
  4. // This source code is licensed under the BSD-style license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. #pragma once
  7. #include <ATen/functorch/Macros.h>
  8. #include <c10/core/DispatchKey.h>
  9. #include <ATen/core/function_schema.h>
  10. #include <c10/util/Optional.h>
  11. #include <c10/core/impl/LocalDispatchKeySet.h>
  12. #include <ATen/functorch/Interpreter.h>
  13. #include <ATen/functorch/VmapInterpreter.h>
  14. #include <ATen/functorch/ADInterpreters.h>
  15. #include <ATen/functorch/FunctionalizeInterpreter.h>
  16. // Forward declared
  17. namespace c10 { struct AutogradMetaInterface; }
  18. namespace at::functorch {
  19. // This file contains the implementation of functorch's interpreter stack.
  20. // See NOTE: [functorch interpreter stack] first before reading on.
  21. //
  22. // NB: the functorch interpreter stack is also referred to as:
  23. // - the "dynamic layer stack" -- an older name for "interpreter" was
  24. // "dynamic layer".
  25. // - the "functorch mode stack". You can think of each functorch transform as a
  26. // "mode" (in the same sense as torch_dispatch mode or torch_function mode),
  27. // and functorch being an implementation of a "mode stack" where the modes
  28. // may be arbitrary composed.
  29. // DynamicLayer is basically the same thing as an Interpreter.
  30. // It represents a functorch transform and it holds an Interpreter,
  31. // which contains metadata related to the transform and instructions on
  32. // how to perform the transform.
  33. //
  34. // TODO: we can excise DynamicLayer in favor of Interpreter,
  35. // But I am going to leave it for now as a compatiblity shim to avoid
  36. // needing to refactor a lot of callsites...
  37. struct TORCH_API DynamicLayer {
  38. explicit DynamicLayer(
  39. TransformType transform_type,
  40. int64_t layerId,
  41. optional<c10::SymInt> batchSize = nullopt,
  42. optional<RandomnessType> randomness = nullopt,
  43. optional<bool> prev_grad_mode = nullopt,
  44. optional<bool> pre_fwd_grad_mode = nullopt,
  45. optional<bool> functionalize_add_back_views = nullopt);
  46. TransformType key() const;
  47. int64_t layerId() const;
  48. const Interpreter& interpreter() const { return interpreter_; }
  49. Interpreter& interpreter() { return interpreter_; }
  50. // Only valid for vmap
  51. c10::SymInt batchSize() const;
  52. RandomnessType randomness() const;
  53. private:
  54. Interpreter interpreter_;
  55. };
  56. TORCH_API int64_t initAndPushDynamicLayer(
  57. TransformType transform_type,
  58. optional<c10::SymInt> batch_size = nullopt,
  59. optional<RandomnessType> randomness = nullopt,
  60. optional<bool> prev_grad_mode = nullopt,
  61. optional<bool> prev_fwd_grad_mode = nullopt,
  62. optional<bool> functionalize_add_back_views = nullopt);
  63. TORCH_API DynamicLayer popDynamicLayerAndDeleteMetadata();
  64. TORCH_API std::optional<DynamicLayer> maybeCurrentDynamicLayer();
  65. TORCH_API const std::vector<DynamicLayer>& getDynamicLayerStack();
  66. TORCH_API void setDynamicLayerStack(const std::vector<DynamicLayer>& stack);
  67. TORCH_API void setDynamicLayerFrontBackKeysIncluded(bool included);
  68. // NOTE: [Life handles and lexically scoped transforms]
  69. // functorch transforms are lexically scoped.
  70. // Given a level, we store a "life handle" that is a boolean that tells us if the
  71. // transform with that level is active or not.
  72. //
  73. // functorch's TensorWrapper (for grad transforms) stores a life handle.
  74. // If a TensorWrapper escapes from the scope of the transform, then somehow
  75. // it must know it escaped; it can tell by querying the life handle.
  76. TORCH_API const std::shared_ptr<bool>& getLifeHandleForLevel(int64_t level);
  77. // Returns if an operator is in-place. An operator is inplace if:
  78. // 1. The first argument is a Tensor and it is being written to
  79. // 2. The first argument is being returned
  80. // 3. No other arguments are aliased
  81. // Here is an example of an in-place operator:
  82. // add_(Tensor(a!) self, Tensor other, *, Scalar alpha=1) -> Tensor(a!)
  83. TORCH_API bool isInplaceOp(const c10::FunctionSchema& schema);
  84. // Given the indices of unwrapped inputs and the schema, this returns the indices of any outputs that should remain unwrapped
  85. TORCH_API std::optional<size_t> findAliasedOutput(const FunctionSchema& schema, const int64_t immutable_input);
  86. TORCH_API Tensor unwrapIfDead(const Tensor& tensor);
  87. TORCH_API bool isDeadTensorWrapper(const Tensor& tensor);
  88. // Pretty printers
  89. TORCH_API std::ostream& operator<<(std::ostream& os, const DynamicLayer& layer);
  90. TORCH_API std::ostream& operator<<(std::ostream& os, const std::vector<DynamicLayer>& dynamicLayerStack);
  91. // While a functorch transform is active, torch.autograd.function._SingleLevelFunction
  92. // is disabled by default. The following two APIs are APIs for enabling
  93. // it. These are not user-facing APIs. We can delete this in the future, but
  94. // it is useful for debugging when something goes wrong with the
  95. // autograd.Function <> functorch interaction, which uses _SingleLevelFunction,
  96. // because it leads to loud errors if something is incorrect.
  97. TORCH_API void setSingleLevelAutogradFunctionAllowed(bool allowed);
  98. TORCH_API bool getSingleLevelAutogradFunctionAllowed();
  99. // While a functorch grad transform is active, Tensor.requires_grad_() gets
  100. // disabled. These two functions are the mechanism to controlling that.
  101. TORCH_API void setInplaceRequiresGradAllowed(bool allowed);
  102. TORCH_API bool getInplaceRequiresGradAllowed();
  103. TORCH_API DynamicLayer popDynamicLayer();
  104. TORCH_API int64_t pushDynamicLayer(DynamicLayer&& layer);
  105. } // namespace at::functorch