strides.h 884 B

1234567891011121314151617181920212223242526272829
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #include <c10/util/ArrayRef.h>
  4. #include <c10/util/DimVector.h>
  5. #include <algorithm>
  6. namespace c10 {
  7. // Computes the contiguous strides of a tensor, given its sizes.
  8. inline DimVector contiguous_strides(const IntArrayRef sizes) {
  9. using Int = IntArrayRef::value_type;
  10. const Int dims = static_cast<Int>(sizes.size());
  11. // With this initialisation we get the case dim == 0 or 1 right
  12. DimVector strides(dims, 1);
  13. for (auto i = dims - 2; i >= 0; --i) {
  14. // Strides can't be 0 even if sizes are 0.
  15. strides[i] = strides[i + 1] * std::max(sizes[i + 1], Int{1});
  16. }
  17. return strides;
  18. }
  19. } // namespace c10
  20. #else
  21. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  22. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)