| 67 | } |
| 68 | |
| 69 | Result<std::vector<int64_t>> ComputeStrides(const std::shared_ptr<DataType>& value_type, |
| 70 | std::span<const int64_t> shape, |
| 71 | std::span<const int64_t> permutation) { |
| 72 | const auto ndim = shape.size(); |
| 73 | const int byte_width = value_type->byte_width(); |
| 74 | |
| 75 | // Use identity permutation if none provided |
| 76 | std::vector<int64_t> perm; |
| 77 | if (permutation.empty()) { |
| 78 | perm.resize(ndim); |
| 79 | std::iota(perm.begin(), perm.end(), 0); |
| 80 | } else { |
| 81 | perm.assign(permutation.begin(), permutation.end()); |
| 82 | } |
| 83 | |
| 84 | int64_t remaining = 0; |
| 85 | if (!shape.empty() && shape[0] > 0) { |
| 86 | remaining = byte_width; |
| 87 | for (auto i : perm) { |
| 88 | if (i > 0) { |
| 89 | if (MultiplyWithOverflow(remaining, shape[i], &remaining)) { |
| 90 | return Status::Invalid( |
| 91 | "Strides computed from shape would not fit in 64-bit integer"); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | std::vector<int64_t> strides; |
| 98 | if (remaining == 0) { |
| 99 | strides.assign(ndim, byte_width); |
| 100 | return strides; |
| 101 | } |
| 102 | |
| 103 | strides.push_back(remaining); |
| 104 | for (auto i : perm) { |
| 105 | if (i > 0) { |
| 106 | remaining /= shape[i]; |
| 107 | strides.push_back(remaining); |
| 108 | } |
| 109 | } |
| 110 | Permute(perm, &strides); |
| 111 | |
| 112 | return strides; |
| 113 | } |
| 114 | |
| 115 | Result<std::shared_ptr<Buffer>> SliceTensorBuffer(const Array& data_array, |
| 116 | const DataType& value_type, |