FunctionalStorageImpl.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #include <ATen/Tensor.h>
  3. namespace at {
  4. namespace functionalization {
  5. // See Note [Functionalization Pass In Core]
  6. // ViewMeta is a class used by the functionalization pass to navigate between
  7. // a base tensor and a view tensor.
  8. // For example, if I call `b = a.view1(...)`
  9. // the functionalization pass will generate and store a ViewMeta on b that looks
  10. // like:
  11. //
  12. // ViewMeta(
  13. // [<captures>](const Tensor& base, int64_t mutated_view_idx) {
  14. // return base.view1(...);
  15. // },
  16. // [<captures>](const at::Tensor& base, const at::Tensor& mutated_view,
  17. // int64_t mutated_view_idx) -> at::Tensor {
  18. // return at::functionalization::impl::view1_inverse(base, mutated_view,
  19. // ...);
  20. // }
  21. //
  22. // The forward_fn lambda describes how to replay view1 on a tensor.
  23. //
  24. // The reverse_fn lambda describes how, given a tensor that is already a view,
  25. // how to get the corresponding base tensor. See Note [Functionalization Pass:
  26. // View Inverses] for details.
  27. struct ViewMeta {
  28. ViewMeta(
  29. std::function<Tensor(const Tensor&, int64_t)> forward,
  30. std::function<Tensor(const Tensor&, const Tensor&, int64_t)> reverse,
  31. int64_t out_idx = 0)
  32. : forward_fn(std::move(forward)),
  33. reverse_fn(std::move(reverse)),
  34. out_index(out_idx) {}
  35. std::function<Tensor(const Tensor&, int64_t)> forward_fn;
  36. std::function<Tensor(const Tensor&, const Tensor&, int64_t)> reverse_fn;
  37. // See Note [out_idx in ViewMeta]
  38. int64_t out_index;
  39. // Returns a copy of the current ViewMeta, if out_idx matches the current
  40. // out_index. Otherwise, returns a new ViewMeta with the same forward/reverse
  41. // functions, but a new out index.
  42. ViewMeta to_out_idx(int64_t out_idx);
  43. };
  44. // FunctionalStorageImpl is a subclass of StorageImpl used by the
  45. // functionalization pass. It has no underlying data (similar to meta storage).
  46. // It also knows how to reflect mutations to tensors in the absence of a valid
  47. // data pointer.
  48. //
  49. // A storage represents the state shared by (potentially multiple) views of the
  50. // same tensor. For example, in the following code:
  51. //
  52. // b = a.view1(...)
  53. // c = b.view2(...)
  54. // b.add_(1)
  55. // --> storage.add_update(b, {view1_meta})
  56. //
  57. // The call to add_(1) will result in a call to alias.add_update(b,
  58. // {view1_meta}), queueing up the mutation from b onto the alias. Later, suppose
  59. // c is used in an expression (e.g. you try to print c, or pass it to an
  60. // operator). Doing so will involve "syncing" c. First we apply any pending
  61. // updates to the alias, and then we regenerate c by replaying its views off of
  62. // the updated alias. E.g:
  63. //
  64. // print(str(c))
  65. // --> c.sync_()
  66. // --> alias.apply_updates() // after this, the alias will be updated to
  67. // reflect the mutation to b
  68. struct TORCH_API FunctionalStorageImpl : public c10::StorageImpl {
  69. public:
  70. struct Update {
  71. const at::Tensor new_val;
  72. const std::vector<ViewMeta> view_metas;
  73. };
  74. explicit FunctionalStorageImpl(const Tensor& value);
  75. void add_update(
  76. const Tensor& updated_val,
  77. const std::vector<ViewMeta>& view_metas);
  78. bool apply_updates();
  79. const Tensor& base() {
  80. return base_;
  81. }
  82. size_t generation() const {
  83. return generation_;
  84. }
  85. void freeze() {
  86. frozen_ = true;
  87. }
  88. ~FunctionalStorageImpl() override = default;
  89. private:
  90. // NB: base_ should always point to a tensor BELOW the current
  91. // functionalization layer. This is mainly to avoid reference cycles. e.g.
  92. // given `b = a.view(...)` Both a.storage_ and b.storage_ are a
  93. // FunctionStorageImpl containing an Walualias, with contains a Tensor
  94. // `base_`. In this case (where a and b are FunctionalTensorWrapper's), base_
  95. // should point not to a, but to a's unwrapped value, a.value_` See Note
  96. // [Functionalization: Walualias Removal] for a diagram that shows this
  97. // visually.
  98. at::Tensor base_;
  99. std::vector<Update> updates_;
  100. // generation_ gets incremented every time a mutation is queued onto the
  101. // alias. It is used to determine if a given tensor is "up to date", or if it
  102. // needs to be regenerated from the alias.
  103. size_t generation_ = 0;
  104. // If frozen, no more mutations are allowed on this storage. Once frozen, a
  105. // storage cannot be unfrozen.
  106. bool frozen_ = false;
  107. };
  108. } // namespace functionalization
  109. } // namespace at