| 110 | } |
| 111 | |
| 112 | Result<int64_t> ComputeTensorSize(std::span<const int64_t> shape, |
| 113 | std::span<const int64_t> strides, int64_t elem_size) { |
| 114 | // Check the largest offset can be computed without overflow |
| 115 | const size_t ndim = shape.size(); |
| 116 | int64_t largest_offset = elem_size; |
| 117 | for (size_t i = 0; i < ndim; ++i) { |
| 118 | if (shape[i] == 0) continue; |
| 119 | if (strides[i] < 0) { |
| 120 | // TODO(mrkn): Support negative strides for sharing views |
| 121 | return Status::Invalid("negative strides not supported"); |
| 122 | } |
| 123 | |
| 124 | int64_t dim_offset = 0; |
| 125 | if (!internal::MultiplyWithOverflow(shape[i] - 1, strides[i], &dim_offset)) { |
| 126 | if (!internal::AddWithOverflow(largest_offset, dim_offset, &largest_offset)) { |
| 127 | continue; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return Status::Invalid( |
| 132 | "offsets computed from shape and strides would not fit in 64-bit integer"); |
| 133 | } |
| 134 | |
| 135 | // A dimension with no element means empty for which the preceding does not apply. |
| 136 | if (std::find(shape.begin(), shape.end(), 0) != shape.end()) { |
| 137 | return 0; |
| 138 | } |
| 139 | return largest_offset; |
| 140 | } |
| 141 | |
| 142 | namespace { |
| 143 | bool IsTensorStridesRowMajor(const std::shared_ptr<DataType>& type, |
no test coverage detected