OpaqueTensorImpl.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #pragma once
  2. #include <c10/core/MemoryFormat.h>
  3. #include <c10/core/SymIntArrayRef.h>
  4. #include <c10/core/TensorImpl.h>
  5. #include <c10/util/Exception.h>
  6. namespace at {
  7. // An "Opaque" TensorImpl -- there are no strides and (for now)
  8. // even data() is not supported (thus no pointer arithmetic).
  9. // NOTE: We could allow data() in the future, but would have to ensure pointer
  10. // arithmetic code is properly guarded.
  11. //
  12. // NOTE: This does not support resize_ (and other metadata-changing ops) because
  13. // of `shallow_copy_and_detach`. We would need to define an interface to
  14. // "shallow copy" in order to add support.
  15. template <typename OpaqueHandle>
  16. struct TORCH_API OpaqueTensorImpl : public TensorImpl {
  17. // public constructor for now...
  18. OpaqueTensorImpl(
  19. at::DispatchKeySet key_set,
  20. const caffe2::TypeMeta data_type,
  21. c10::Device device,
  22. OpaqueHandle opaque_handle,
  23. c10::IntArrayRef sizes,
  24. bool is_non_overlapping_and_dense = true)
  25. : TensorImpl(key_set, data_type, device),
  26. opaque_handle_(std::move(opaque_handle)) {
  27. constructor_impl(sizes, is_non_overlapping_and_dense);
  28. }
  29. OpaqueTensorImpl(
  30. TensorImpl::ImplType impl_type,
  31. c10::Storage&& storage,
  32. at::DispatchKeySet key_set,
  33. const caffe2::TypeMeta data_type,
  34. OpaqueHandle opaque_handle,
  35. c10::IntArrayRef sizes,
  36. bool is_non_overlapping_and_dense = true)
  37. : TensorImpl(impl_type, std::move(storage), key_set, data_type),
  38. opaque_handle_(std::move(opaque_handle)) {
  39. constructor_impl(sizes, is_non_overlapping_and_dense);
  40. }
  41. // Destructor doesn't call release_resources because it's
  42. // unnecessary; don't forget to change that if needed!
  43. void release_resources() override {
  44. TensorImpl::release_resources();
  45. opaque_handle_ = {};
  46. }
  47. void set_size(int64_t dim, int64_t new_size) override {
  48. TORCH_CHECK(false, "opaque tensors do not have set_size");
  49. }
  50. void set_stride(int64_t dim, int64_t new_stride) override {
  51. TORCH_CHECK(false, "opaque tensors do not have set_stride");
  52. }
  53. void set_storage_offset(int64_t storage_offset) override {
  54. TORCH_CHECK(false, "opaque tensors do not have set_storage_offset");
  55. }
  56. #ifdef DEBUG
  57. bool has_storage() const override {
  58. TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
  59. !storage_, "OpaqueTensorImpl assumes that storage_ is never set");
  60. return false;
  61. }
  62. #endif
  63. /**
  64. * Return a TensorImpl that is a shallow-copy of this TensorImpl.
  65. *
  66. * For usage of `version_counter` and `allow_tensor_metadata_change`,
  67. * see NOTE [ TensorImpl Shallow-Copying ].
  68. */
  69. c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach(
  70. const c10::VariableVersion& version_counter,
  71. bool allow_tensor_metadata_change) const override {
  72. auto impl = c10::make_intrusive<OpaqueTensorImpl<OpaqueHandle>>(
  73. key_set(),
  74. dtype(),
  75. device(),
  76. opaque_handle_,
  77. sizes_and_strides_.sizes_arrayref());
  78. copy_tensor_metadata(
  79. /*src_opaque_impl=*/this,
  80. /*dest_opaque_impl=*/impl.get(),
  81. /*version_counter=*/version_counter,
  82. /*allow_tensor_metadata_change=*/allow_tensor_metadata_change);
  83. impl->refresh_numel();
  84. return impl;
  85. }
  86. /**
  87. * Return a TensorImpl that is a shallow-copy of this TensorImpl.
  88. *
  89. * For usage of `version_counter` and `allow_tensor_metadata_change`,
  90. * see NOTE [ TensorImpl Shallow-Copying ].
  91. */
  92. c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach(
  93. c10::VariableVersion&& version_counter,
  94. bool allow_tensor_metadata_change) const override {
  95. auto impl = c10::make_intrusive<OpaqueTensorImpl<OpaqueHandle>>(
  96. key_set(),
  97. dtype(),
  98. device(),
  99. opaque_handle_,
  100. sizes_and_strides_.sizes_arrayref());
  101. copy_tensor_metadata(
  102. /*src_opaque_impl=*/this,
  103. /*dest_opaque_impl=*/impl.get(),
  104. /*version_counter=*/std::move(version_counter),
  105. /*allow_tensor_metadata_change=*/allow_tensor_metadata_change);
  106. impl->refresh_numel();
  107. return impl;
  108. }
  109. /**
  110. * Shallow-copies data from another TensorImpl into this TensorImpl.
  111. *
  112. * For why this function doesn't check this TensorImpl's
  113. * `allow_tensor_metadata_change_`, see NOTE [ TensorImpl Shallow-Copying ].
  114. */
  115. void shallow_copy_from(const c10::intrusive_ptr<TensorImpl>& impl) override {
  116. AT_ASSERT(has_compatible_shallow_copy_type(impl->key_set()));
  117. auto opaque_impl =
  118. static_cast<const OpaqueTensorImpl<OpaqueHandle>*>(impl.get());
  119. copy_tensor_metadata(
  120. /*src_impl=*/opaque_impl,
  121. /*dest_impl=*/this,
  122. /*version_counter=*/version_counter(),
  123. /*allow_tensor_metadata_change=*/allow_tensor_metadata_change());
  124. refresh_numel();
  125. }
  126. const OpaqueHandle& opaque_handle() const {
  127. return opaque_handle_;
  128. }
  129. OpaqueHandle& unsafe_opaque_handle() {
  130. return opaque_handle_;
  131. }
  132. protected:
  133. /**
  134. * Copy the tensor metadata fields (e.g. sizes / strides / storage pointer /
  135. * storage_offset) from one TensorImpl to another TensorImpl.
  136. *
  137. * For usage of `version_counter` and `allow_tensor_metadata_change`, see NOTE
  138. * [ TensorImpl Shallow-Copying ].
  139. */
  140. static void copy_tensor_metadata(
  141. const OpaqueTensorImpl<OpaqueHandle>* src_opaque_impl,
  142. OpaqueTensorImpl<OpaqueHandle>* dest_opaque_impl,
  143. const c10::VariableVersion& version_counter,
  144. bool allow_tensor_metadata_change) {
  145. TensorImpl::copy_tensor_metadata(
  146. src_opaque_impl,
  147. dest_opaque_impl,
  148. version_counter,
  149. allow_tensor_metadata_change);
  150. // OpaqueTensorImpl-specific fields.
  151. dest_opaque_impl->opaque_handle_ = src_opaque_impl->opaque_handle_;
  152. }
  153. static void copy_tensor_metadata(
  154. const OpaqueTensorImpl<OpaqueHandle>* src_opaque_impl,
  155. OpaqueTensorImpl<OpaqueHandle>* dest_opaque_impl,
  156. c10::VariableVersion&& version_counter,
  157. bool allow_tensor_metadata_change) {
  158. TensorImpl::copy_tensor_metadata(
  159. src_opaque_impl,
  160. dest_opaque_impl,
  161. std::move(version_counter),
  162. allow_tensor_metadata_change);
  163. // OpaqueTensorImpl-specific fields.
  164. dest_opaque_impl->opaque_handle_ = src_opaque_impl->opaque_handle_;
  165. }
  166. private:
  167. const char* tensorimpl_type_name() const override {
  168. return "OpaqueTensorImpl";
  169. }
  170. void constructor_impl(
  171. c10::IntArrayRef sizes,
  172. bool is_non_overlapping_and_dense) {
  173. set_storage_access_should_throw();
  174. set_custom_sizes_strides(SizesStridesPolicy::CustomStrides);
  175. sizes_and_strides_.set_sizes(sizes);
  176. refresh_numel();
  177. // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
  178. is_non_overlapping_and_dense_ = is_non_overlapping_and_dense;
  179. }
  180. OpaqueHandle opaque_handle_;
  181. };
  182. } // namespace at