strides.h 630 B

123456789101112131415161718192021222324
  1. #pragma once
  2. #include <c10/util/ArrayRef.h>
  3. #include <c10/util/DimVector.h>
  4. #include <algorithm>
  5. namespace c10 {
  6. // Computes the contiguous strides of a tensor, given its sizes.
  7. inline DimVector contiguous_strides(const IntArrayRef sizes) {
  8. using Int = IntArrayRef::value_type;
  9. const Int dims = static_cast<Int>(sizes.size());
  10. // With this initialisation we get the case dim == 0 or 1 right
  11. DimVector strides(dims, 1);
  12. for (auto i = dims - 2; i >= 0; --i) {
  13. // Strides can't be 0 even if sizes are 0.
  14. strides[i] = strides[i + 1] * std::max(sizes[i + 1], Int{1});
  15. }
  16. return strides;
  17. }
  18. } // namespace c10