The result addresses the same storage with the same strides, so no data moves. Per-axis quantization is cut to the same range.
196{
197 if (origin.size() !=
shape.size() || extent.size() !=
shape.size())
198 return std::nullopt;
199
200 bool whole = true;
201 for (
size_t i = 0; i <
shape.size(); ++i) {
202 if (extent[i] == 0 || origin[i] >=
shape[i] || extent[i] >
shape[i] - origin[i])
203 return std::nullopt;
204 whole = whole && origin[i] == 0 && extent[i] ==
shape[i];
205 }
206 if (whole)
207 return *this;
208 if (
dtype.is_sub_byte())
209 return std::nullopt;
210
212 const uint64_t element =
dtype.element_bytes();
213 uint64_t forward = 0;
214 uint64_t backward = 0;
215 for (
size_t i = 0; i <
shape.size(); ++i) {
216 if (origin[i] == 0)
217 continue;
218 uint64_t step = 0;
219 if (!magnitude_times(stride_magnitude(resolved[i]), element, step)
220 || !magnitude_times(step, origin[i], step))
221 return std::nullopt;
222 uint64_t& side = resolved[i] < 0 ? backward : forward;
223 if (step > max_signed - side)
224 return std::nullopt;
225 side += step;
226 }
228 return std::nullopt;
230 if (backward > up)
231 return std::nullopt;
232
233 TensorSpec result = *this;
234 result.shape.assign(extent.begin(), extent.end());
235 result.strides = resolved;
236 result.byte_offset = up - backward;
237
238 if (result.quantization && result.quantization->axis) {
239 const size_t axis = *result.quantization->axis;
240 auto& q = *result.quantization;
241 if (axis >=
shape.size() || q.scale.size() !=
shape[axis]
242 || (!q.zero_point.empty() && q.zero_point.size() !=
shape[axis]))
243 return std::nullopt;
244 q.scale = cut(q.scale, origin[axis], extent[axis]);
245 if (!q.zero_point.empty())
246 q.zero_point = cut(q.zero_point, origin[axis], extent[axis]);
247 }
248 return result;
249}
std::vector< uint64_t > shape
std::vector< int64_t > resolved_strides() const
Element strides, with C order filled in when none are stored.