9 constexpr uint64_t max_signed =
static_cast<uint64_t
>(std::numeric_limits<int64_t>::max());
11 bool magnitude_times(uint64_t a, uint64_t b, uint64_t& out)
13 if (a != 0 && b > max_signed / a)
19 uint64_t stride_magnitude(int64_t stride)
21 return stride < 0 ? static_cast<uint64_t>(-(stride + 1)) + 1 :
static_cast<uint64_t
>(stride);
25 std::vector<T> cut(
const std::vector<T>& values, uint64_t first, uint64_t count)
27 return { values.begin() +
static_cast<std::ptrdiff_t
>(first),
28 values.begin() +
static_cast<std::ptrdiff_t
>(first +
count) };
45 for (
const auto extent :
shape) {
46 if (!magnitude_times(count, extent, count))
60 if (count && *count == 0)
64 for (
size_t i =
shape.size(); i-- > 0;) {
67 if (
shape[i] > max_signed /
static_cast<uint64_t
>(std::max<int64_t>(expected, 1)))
69 expected *=
static_cast<int64_t
>(
shape[i]);
79 std::vector<int64_t> result(
shape.size(), 1);
81 for (
size_t i =
shape.size(); i-- > 0;) {
83 running *=
static_cast<int64_t
>(std::max<uint64_t>(
shape[i], 1));
96 if (
dtype.is_sub_byte()) {
97 const auto bytes =
dtype.storage_bytes(*count);
98 if (!bytes || *bytes > std::numeric_limits<uint64_t>::max() -
byte_offset)
106 for (
size_t i = 0; i <
shape.size(); ++i) {
109 const int64_t stride = resolved[i];
110 const uint64_t magnitude = stride < 0 ? static_cast<uint64_t>(-(stride + 1)) + 1 :
static_cast<uint64_t
>(stride);
112 if (!magnitude_times(
shape[i] - 1, magnitude, reach))
114 uint64_t& side = stride < 0 ? low : high;
115 if (reach > max_signed - side)
120 const uint64_t element =
dtype.element_bytes();
121 uint64_t low_bytes = 0;
122 uint64_t high_bytes = 0;
123 if (!magnitude_times(low, element, low_bytes) || !magnitude_times(high, element, high_bytes))
129 const uint64_t extent = high_bytes + low_bytes + element;
130 if (extent < element || extent > std::numeric_limits<uint64_t>::max() - first)
132 return std::pair<uint64_t, uint64_t> { first, first + extent };
137 const std::string label =
name.empty() ? std::string(
"tensor") :
"tensor '" +
name +
"'";
140 return label +
": element type has zero width";
142 return label +
": unknown element type kind";
144 return label +
": element count overflows";
146 return label +
": strides rank " + std::to_string(
strides.size())
147 +
" differs from shape rank " + std::to_string(
shape.size());
149 return label +
": axes rank " + std::to_string(
axes.size())
150 +
" differs from shape rank " + std::to_string(
shape.size());
152 return label +
": " +
dtype.name() +
" is sub-byte and must be contiguous";
154 return label +
": layout addresses memory outside its storage";
159 return label +
": quantization has no scale";
160 const size_t expected = q.axis ? (*q.axis <
shape.size() ?
shape[*q.axis] : 0) : 1;
161 if (q.axis && *q.axis >=
shape.size())
162 return label +
": quantization axis " + std::to_string(*q.axis) +
" is out of range";
163 if (q.scale.size() != expected)
164 return label +
": quantization has " + std::to_string(q.scale.size())
165 +
" scales, expected " + std::to_string(expected);
166 if (!q.zero_point.empty() && q.zero_point.size() != q.scale.size())
167 return label +
": quantization zero points differ in length from scales";
176 std::vector<DataDimension> result;
177 result.reserve(
shape.size());
179 for (
size_t i = 0; i <
shape.size(); ++i) {
180 const int64_t stride = resolved[i];
181 const uint64_t magnitude = stride < 0 ? static_cast<uint64_t>(-(stride + 1)) + 1 :
static_cast<uint64_t
>(stride);
182 if (i <
axes.size()) {
184 axis.size =
shape[i];
185 axis.stride = magnitude;
186 result.push_back(std::move(axis));
195 std::span<const uint64_t> origin, std::span<const uint64_t> extent)
const
197 if (origin.size() !=
shape.size() || extent.size() !=
shape.size())
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])
204 whole = whole && origin[i] == 0 && extent[i] ==
shape[i];
208 if (
dtype.is_sub_byte())
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) {
219 if (!magnitude_times(stride_magnitude(resolved[i]), element, step)
220 || !magnitude_times(step, origin[i], step))
222 uint64_t& side = resolved[i] < 0 ? backward : forward;
223 if (step > max_signed - side)
234 result.
shape.assign(extent.begin(), extent.end());
241 if (axis >=
shape.size() || q.scale.size() !=
shape[axis]
242 || (!q.zero_point.empty() && q.zero_point.size() !=
shape[axis]))
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]);
257 std::vector<uint64_t> origin(
shape.size(), 0);
258 std::vector<uint64_t> extent =
shape;
259 for (
size_t axis = 0; axis < named; ++axis) {
262 if (first > last || last >=
shape[axis])
264 origin[axis] = first;
265 extent[axis] = last - first + 1;
267 return window(origin, extent);
std::vector< double > count(std::span< const double > data, size_t n_windows, uint32_t hop_size, uint32_t window_size)
Sample count per window (as double for pipeline uniformity).
@ CUSTOM
User-defined or application-specific.
std::vector< uint64_t > end_coordinates
Ending frame index (inclusive).
std::vector< uint64_t > start_coordinates
Starting frame index (inclusive).
Represents a point or span in N-dimensional space.
Element type as kind, bit width and lane count.
std::vector< DataDimension > axes
std::optional< uint64_t > element_count() const noexcept
Product of the shape, or std::nullopt on overflow.
std::optional< std::pair< uint64_t, uint64_t > > byte_range() const noexcept
Storage bytes [first, last) the spec addresses, from storage start.
bool is_contiguous() const noexcept
Whether elements are laid out densely in C order.
std::string diagnose() const
Empty when the spec is self-consistent, otherwise the reason it is not.
std::optional< TensorQuantization > quantization
std::vector< int64_t > strides
static TensorSpec contiguous(TensorDType dtype, std::vector< uint64_t > shape, std::string name={})
C-ordered spec with no offset, axes or quantization.
std::vector< DataDimension > dimensions() const
One DataDimension per axis, with size and stride taken from shape and strides.
std::optional< TensorSpec > window(std::span< const uint64_t > origin, std::span< const uint64_t > extent) const
Spec of the box that starts at origin and spans extent elements per axis.
std::vector< uint64_t > shape
std::vector< int64_t > resolved_strides() const
Element strides, with C order filled in when none are stored.
Complete description of a tensor: type, shape, layout, meaning.