MCPcopy Create free account
hub / github.com/apache/arrow / CheckTensorStridesValidity

Function CheckTensorStridesValidity

cpp/src/arrow/tensor.cc:157–194  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

155}
156
157Status CheckTensorStridesValidity(const std::shared_ptr<Buffer>& data,
158 const std::vector<int64_t>& shape,
159 const std::vector<int64_t>& strides,
160 const std::shared_ptr<DataType>& type) {
161 if (strides.size() != shape.size()) {
162 return Status::Invalid("strides must have the same length as shape");
163 }
164 if (data->size() == 0 && std::find(shape.begin(), shape.end(), 0) != shape.end()) {
165 return Status::OK();
166 }
167
168 // Check the largest offset can be computed without overflow
169 const size_t ndim = shape.size();
170 int64_t largest_offset = 0;
171 for (size_t i = 0; i < ndim; ++i) {
172 if (shape[i] == 0) continue;
173 if (strides[i] < 0) {
174 // TODO(mrkn): Support negative strides for sharing views
175 return Status::Invalid("negative strides not supported");
176 }
177
178 int64_t dim_offset;
179 if (!internal::MultiplyWithOverflow(shape[i] - 1, strides[i], &dim_offset)) {
180 if (!internal::AddWithOverflow(largest_offset, dim_offset, &largest_offset)) {
181 continue;
182 }
183 }
184
185 return Status::Invalid(
186 "offsets computed from shape and strides would not fit in 64-bit integer");
187 }
188
189 const int byte_width = type->byte_width();
190 if (largest_offset > data->size() - byte_width) {
191 return Status::Invalid("strides must not involve buffer over run");
192 }
193 return Status::OK();
194}
195
196} // namespace
197

Callers 1

ValidateTensorParametersFunction · 0.85

Calls 8

MultiplyWithOverflowFunction · 0.85
AddWithOverflowFunction · 0.85
InvalidFunction · 0.70
OKFunction · 0.70
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
byte_widthMethod · 0.45

Tested by

no test coverage detected