onnx_light.onnx_core.tensor_statistics#
Computes descriptive tensor statistics using the C++ runtime.
- class onnx_light.onnx_core.tensor_statistics.TensorStatistics#
Describes a numeric or quantized tensor.
- property bin_edges#
(self) -> list[float]
- property count#
(self) -> int
- property distinct_count#
(self) -> int | None
- property distribution#
(self) -> list[int]
- property dtype#
(self) -> int
- property max#
(self) -> float
- property mean#
(self) -> float
- property median#
(self) -> float | None
- property min#
(self) -> float
- property quantiles#
(self) -> list[float]
- property rectangle_map#
(self) -> list[float]
- property rectangle_shape#
(self) -> list[int]
- property shape#
(self) -> list[int]
- property size_bytes#
(self) -> int
- onnx_light.onnx_core.tensor_statistics.compute_tensor_statistics(value: ndarray | Tensor | TensorProto | EncodedValueProto | RuntimeValue, *, bins: int = 0, quantiles: Sequence[float] = (), median: bool = False, rectangle_shape: Sequence[int] = (), model: ModelProto | None = None, max_output_bytes: int = 67108864) TensorStatistics#
Computes descriptive statistics and optional summaries in C++.
- Parameters:
value – A NumPy array, runtime
Tensor,TensorProto,EncodedValueProto, or tensor/encodedRuntimeValue.SharedQuantizedValueis an alias ofRuntimeValueand is also accepted. Supports real numeric and boolean tensors, including noncontiguous NumPy arrays; strings and complex types are unsupported. Encoded inputs describe reconstructed numeric values.bins – Nonnegative histogram bin count, default 0 (automatic: 50 bins for more than 500 elements, 10 otherwise). A positive value overrides the automatic count. No histogram is computed for fewer than 10 elements, regardless of this option.
quantiles – Probabilities in [0, 1], default () (no quantiles). Computes values in the requested order using linear interpolation at
q * (count - 1), preserving exact endpoints.median – Whether to compute the median independently of
quantiles, default False.rectangle_shape – Positive [height, width], default () (no rectangle). Reshapes the tensor to
[shape[0], product(shape[1:])](or[1, count]for a vector or scalar), then resizes using fractional cell overlap to produce area averages.model – Optional
ModelProto, default None. Resolves the type catalogue and shared parameters of encoded values. A sharedRuntimeValuecan also carry its own parameter ownership.max_output_bytes – Per-call allocation budget, default 64 MiB, for the combined option-driven numeric output payloads:
8*bins + 8*(bins+1)histogram bytes,8*height*widthrectangle bytes, 8 bytes per quantile, and 8 bytes for the median.bins=0charges 50 bins (808 bytes), the largest automatic histogram. The budget can be raised or lowered explicitly.
- Returns:
Statistics with
min,max,mean, ONNXdtype,shape, elementcount, andsize_bytes. Integer, boolean, and encoded inputs also reportdistinct_count; other inputs leave it as None. Encoded inputs report the logical tensor dtype and shape, and their original encoded payload length insize_bytes, excluding descriptor metadata and shared parameters.distributioncontains histogram counts andbin_edgesthe corresponding boundaries. Bins are left-inclusive and right-exclusive, except for the final bin, which includes the maximum. Only finite values enter the histogram; no finite values produce no histogram. Absent histograms have empty counts and edges.quantilescontains the requested values, or an empty list.medianis None unless requested.rectangle_mapis a flat row-major list with the requestedrectangle_shape; both are empty when no map is produced.- Return type:
- Raises:
TypeError – If an input or option cannot be converted to its native type.
ValueError – If a tensor dtype or runtime value kind is unsupported, storage or encoded metadata is malformed, required encoded types or shared parameters cannot be resolved,
binsis negative or too large, a quantile is nonfinite or outside [0, 1], orrectangle_shapeis neither empty nor two positive dimensions with a representable output size, or the combined requested payload exceedsmax_output_bytes.
Notes
NaNs propagate to the minimum, maximum, mean, and requested quantiles and median. Infinities follow numeric ordering and IEEE arithmetic. Nonfinite values propagate through overlapping rectangle cells. Empty tensors return NaN numeric statistics (including each requested quantile and the median if requested), no histogram or rectangle map, and a zero distinct count when applicable. Options are validated even for empty tensors.
All requested outputs are charged even when no histogram or rectangle would be produced. Oversized options are rejected before output allocation or input conversion/dequantization. This is not a total-memory limit: input storage, decoding, input-dependent workspaces, container capacity/metadata, and Python result copies are excluded.
For unencoded tensors, basic reductions and histograms read the tensor directly; only order statistics and rectangle maps materialize a full numeric copy. Sorting is performed only when quantiles or the median are requested. Exact distinct counts use storage proportional to the number of distinct values.
For example:
import numpy
from onnx_light.onnx_core.tensor_statistics import compute_tensor_statistics
statistics = compute_tensor_statistics(
numpy.arange(24, dtype=numpy.float32).reshape(4, 6),
quantiles=[0.25, 0.75],
median=True,
rectangle_shape=[2, 3],
)
assert statistics.mean == 11.5
assert statistics.median == 11.5
rectangle = numpy.array(statistics.rectangle_map).reshape(statistics.rectangle_shape)
For quantized values, statistics describe reconstructed values, while
size_bytes reports encoded payload storage rather than a temporary
floating-point tensor. Quantization scales and other descriptor metadata are
not included in this payload size.