mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
This PR moves `ATen/core/DimVector.h`, as suggested in: https://github.com/pytorch/pytorch/pull/76812#discussion_r866875924 The changes can be summarized as: - Changing includes from `ATen/core/DimVector.h` to `c10/util/DimVector.h` - Re-declaring both the type and constant size in `at` namespace - Making `c10::contiguous_strides` return a `DimVector` Pull Request resolved: https://github.com/pytorch/pytorch/pull/77045 Approved by: https://github.com/peterbell10, https://github.com/ezyang
28 lines
699 B
C++
28 lines
699 B
C++
#pragma once
|
|
#include <c10/util/ArrayRef.h>
|
|
#include <c10/util/DimVector.h>
|
|
|
|
namespace c10 {
|
|
|
|
// Computes the contiguous strides of a tensor, given its sizes.
|
|
static inline DimVector contiguous_strides(const IntArrayRef sizes) {
|
|
using Int = IntArrayRef::value_type;
|
|
const Int dims = static_cast<Int>(sizes.size());
|
|
|
|
DimVector strides;
|
|
|
|
if (dims > 0) {
|
|
strides.assign(dims, 0);
|
|
// Start by populating the last dimension: its strides is always 1.
|
|
strides[dims - 1] = 1;
|
|
for (auto i = dims - 2; i >= 0; --i) {
|
|
// Strides can't be 0 even if sizes are 0.
|
|
strides[i] = strides[i + 1] * std::max(sizes[i + 1], Int{1});
|
|
}
|
|
}
|
|
|
|
return strides;
|
|
}
|
|
|
|
} // namespace c10
|