SparseCsrTensorImpl.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #pragma once
  2. #include <ATen/Tensor.h>
  3. #include <c10/core/TensorImpl.h>
  4. #include <c10/core/impl/TorchDispatchModeTLS.h>
  5. #include <c10/util/Exception.h>
  6. namespace at {
  7. // Struct implementing a sparse CSR tensor. It uses three 1-D tensors for
  8. // denoting the data: `crow_indices_`, `col_indices_` and `values_`.
  9. // The `crow_indices_` tensor is a integer tensor of shape `(size(0) + 1)`
  10. // that represents the compressed row indices of the CSR tensor. The
  11. // `col_indices_` tensor is an integer tensor of shape `(nnz())`
  12. // that explicitly stores the column indices of each value of the sparse
  13. // tensor. The `values_` tensor can be of any pytorch-supported data type
  14. // and has shape `(nnz())`.
  15. //
  16. // Since the main advantage of the CSR format over the COO format is speed of
  17. // computation, care must be taken to facilitate smooth interfacing of
  18. // these data structures with optimized libraries such as MKL and MAGMA.
  19. // Since the MKL interface for pytorch currently uses indexing with int32
  20. // type, it is important to make sure that the `crow_indices` and `col_indices`
  21. // are of type int32 when calling MKL routines such as SPMM or SPMV.
  22. //
  23. // If not calling MKL, it should be alright to use 64 bit integer tensors
  24. // for indexing.
  25. struct TORCH_API SparseCsrTensorImpl : public TensorImpl {
  26. Tensor crow_indices_;
  27. Tensor col_indices_;
  28. Tensor values_;
  29. Layout layout_;
  30. public:
  31. explicit SparseCsrTensorImpl(
  32. at::DispatchKeySet,
  33. at::Device device,
  34. Layout layout,
  35. const caffe2::TypeMeta);
  36. void resize_(int64_t nnz, IntArrayRef size);
  37. void resize_and_clear_(
  38. int64_t sparse_dim,
  39. int64_t dense_dim,
  40. IntArrayRef size);
  41. void resize_as_sparse_compressed_tensor_(const Tensor& src);
  42. void set_member_tensors(
  43. const Tensor& crow_indices,
  44. const Tensor& col_indices,
  45. const Tensor& values,
  46. c10::SymIntArrayRef size);
  47. void set_member_tensors(
  48. const Tensor& crow_indices,
  49. const Tensor& col_indices,
  50. const Tensor& values,
  51. IntArrayRef size);
  52. const Tensor& compressed_indices() const {
  53. return crow_indices_;
  54. }
  55. const Tensor& plain_indices() const {
  56. return col_indices_;
  57. }
  58. const Tensor& values() const {
  59. return values_;
  60. }
  61. int64_t nnz() {
  62. return col_indices_.size(-1);
  63. }
  64. inline int64_t batch_dim() const noexcept {
  65. return crow_indices_.dim() - 1;
  66. }
  67. inline int64_t sparse_dim() const noexcept {
  68. return 2;
  69. }
  70. inline int64_t dense_dim() const noexcept {
  71. return values_.dim() - batch_dim() - block_dim() - 1;
  72. }
  73. private:
  74. inline int64_t block_dim() const noexcept {
  75. return (layout_ == kSparseBsr || layout_ == kSparseBsc ? 2 : 0);
  76. }
  77. protected:
  78. IntArrayRef strides_custom() const override;
  79. SymIntArrayRef sym_strides_custom() const override;
  80. bool is_contiguous_custom(MemoryFormat) const override;
  81. public:
  82. void set_size(int64_t dim, int64_t new_size) override;
  83. void set_stride(int64_t dim, int64_t new_stride) override;
  84. void set_storage_offset(int64_t storage_offset) override;
  85. Layout layout_impl() const override {
  86. return layout_;
  87. }
  88. void set_layout(Layout layout) {
  89. switch (layout) {
  90. case kSparseCsr:
  91. case kSparseCsc:
  92. case kSparseBsr:
  93. case kSparseBsc:
  94. layout_ = layout;
  95. break;
  96. default:
  97. TORCH_CHECK(false, "unsupported layout ", layout);
  98. }
  99. }
  100. template <typename VariableVersion>
  101. c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach_core(
  102. VariableVersion&& version_counter,
  103. bool allow_tensor_metadata_change) const {
  104. const auto mode_stack_len = c10::impl::TorchDispatchModeTLS::stack_len();
  105. c10::impl::PyInterpreter&& interpreter = nullptr;
  106. if (mode_stack_len > 0 &&
  107. !c10::impl::tls_is_dispatch_key_excluded(DispatchKey::Python)) {
  108. const auto& cur_torch_dispatch_mode_state =
  109. c10::impl::TorchDispatchModeTLS::get_stack_at(mode_stack_len - 1);
  110. interpreter = cur_torch_dispatch_mode_state->pyinterpreter();
  111. } else if (
  112. key_set_.has(DispatchKey::Python) &&
  113. !c10::impl::tls_is_dispatch_key_excluded(DispatchKey::Python)) {
  114. interpreter = pyobj_slot_.load_pyobj_interpreter();
  115. } else {
  116. // otherwise just copy the SparseTensorImpl and not the PyObject.
  117. auto impl = c10::make_intrusive<SparseCsrTensorImpl>(
  118. key_set(), device(), layout_impl(), dtype());
  119. copy_tensor_metadata(
  120. /*src_sparse_impl=*/this,
  121. /*dest_sparse_impl=*/impl.get(),
  122. /*version_counter=*/version_counter,
  123. /*allow_tensor_metadata_change=*/allow_tensor_metadata_change);
  124. impl->refresh_numel();
  125. return impl;
  126. }
  127. auto r = interpreter->detach(this);
  128. r->set_version_counter(std::forward<VariableVersion>(version_counter));
  129. r->set_allow_tensor_metadata_change(allow_tensor_metadata_change);
  130. return r;
  131. }
  132. /**
  133. * Return a TensorImpl that is a shallow-copy of this TensorImpl.
  134. *
  135. * For usage of `version_counter` and `allow_tensor_metadata_change`,
  136. * see NOTE [ TensorImpl Shallow-Copying ].
  137. */
  138. c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach(
  139. const c10::VariableVersion& version_counter,
  140. bool allow_tensor_metadata_change) const override {
  141. return shallow_copy_and_detach_core(
  142. version_counter, allow_tensor_metadata_change);
  143. }
  144. /**
  145. * Return a TensorImpl that is a shallow-copy of this TensorImpl.
  146. *
  147. * For usage of `version_counter` and `allow_tensor_metadata_change`,
  148. * see NOTE [ TensorImpl Shallow-Copying ].
  149. */
  150. c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach(
  151. c10::VariableVersion&& version_counter,
  152. bool allow_tensor_metadata_change) const override {
  153. return shallow_copy_and_detach_core(
  154. std::move(version_counter), allow_tensor_metadata_change);
  155. }
  156. private:
  157. explicit SparseCsrTensorImpl(
  158. at::DispatchKeySet key_set,
  159. const caffe2::TypeMeta data_type,
  160. at::Tensor crow_indices,
  161. at::Tensor col_indices,
  162. at::Tensor values,
  163. at::Layout layout);
  164. const char* tensorimpl_type_name() const override;
  165. /**
  166. * Copy the tensor metadata fields (e.g. sizes / strides / storage pointer /
  167. * storage_offset) from one TensorImpl to another TensorImpl.
  168. *
  169. * For usage of `version_counter` and `allow_tensor_metadata_change`, see NOTE
  170. * [ TensorImpl Shallow-Copying ].
  171. */
  172. static void copy_tensor_metadata(
  173. const SparseCsrTensorImpl* src_sparse_impl,
  174. SparseCsrTensorImpl* dest_sparse_impl,
  175. c10::VariableVersion version_counter,
  176. bool allow_tensor_metadata_change) {
  177. TensorImpl::copy_tensor_metadata(
  178. src_sparse_impl,
  179. dest_sparse_impl,
  180. std::move(version_counter),
  181. allow_tensor_metadata_change);
  182. // Sparse-specific fields
  183. dest_sparse_impl->crow_indices_ = src_sparse_impl->compressed_indices();
  184. dest_sparse_impl->col_indices_ = src_sparse_impl->plain_indices();
  185. dest_sparse_impl->values_ = src_sparse_impl->values();
  186. dest_sparse_impl->layout_ = src_sparse_impl->layout_impl();
  187. }
  188. };
  189. } // namespace at