Helper to compute strides for creating linear indices into multidimensional data from the dimension lengths and the layout. Returns a new vector of size lengths.size() + 1. The last element of the returned vector at index [lengths.size()] contains the product of all dimension lengths.
| 948 | // lengths.size() + 1. The last element of the returned vector at index |
| 949 | // [lengths.size()] contains the product of all dimension lengths. |
| 950 | std::vector<int64> ComputeStrides(const absl::Span<const int64> lengths, |
| 951 | const Layout& layout) { |
| 952 | const int64 num_dimensions = lengths.size(); |
| 953 | |
| 954 | // Make sure that the layout length matches the number of dimensions. |
| 955 | CHECK_EQ(num_dimensions, layout.minor_to_major_size()); |
| 956 | |
| 957 | // Calculate strides using layout-specified ordering of the dimensions and |
| 958 | // place the stride for axis 0 at index 0, for axis 1 at index 1, etc. |
| 959 | std::vector<int64> strides(num_dimensions + 1); |
| 960 | int64 stride = 1; |
| 961 | for (int64 i = 0; i < num_dimensions; i++) { |
| 962 | // Reverse the ordering of the dimensions in the layout. |
| 963 | const int64 index = (num_dimensions - 1) - layout.minor_to_major(i); |
| 964 | strides[index] = stride; |
| 965 | stride *= lengths[index]; |
| 966 | } |
| 967 | strides[num_dimensions] = stride; |
| 968 | |
| 969 | return strides; |
| 970 | } |
| 971 | |
| 972 | // Compute strides as above using the default layout. |
| 973 | std::vector<int64> ComputeStrides(const absl::Span<const int64> lengths) { |
no test coverage detected