MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ window() [2/2]

std::optional< TensorSpec > MayaFlux::Kakshya::TensorSpec::window ( std::span< const uint64_t > origin,
std::span< const uint64_t > extent ) const
nodiscard

Spec of the box that starts at origin and spans extent elements per axis.

The result addresses the same storage with the same strides, so no data moves. Per-axis quantization is cut to the same range.

Returns
The window, or std::nullopt when a rank differs, an extent is zero, the box leaves the shape, the offset leaves the addressable range, or the type is sub-byte and the box is not the whole tensor.

Definition at line 194 of file TensorSpec.cpp.

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
211 const auto resolved = resolved_strides();
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 }
227 if (byte_offset > max_signed || forward > max_signed - byte_offset)
228 return std::nullopt;
229 const uint64_t up = byte_offset + forward;
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.

References byte_offset, dtype, quantization, resolved_strides(), shape, and strides.

Referenced by window().

Here is the call graph for this function:
Here is the caller graph for this function: