package nx
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=96d35ce03dfbebd2313657273e24c2e2d20f9e6c7825b8518b69bd1d6ed5870f
sha512=90c5053731d4108f37c19430e45456063e872b04b8a1bbad064c356e1b18e69222de8bfcf4ec14757e71f18164ec6e4630ba770dbcb1291665de5418827d1465
doc/nx.core/Nx_core/View/index.html
Module Nx_core.ViewSource
Strided tensor views.
Strided tensor views.
A view describes how a linear buffer is interpreted as an n-dimensional tensor through shape, strides, offset, and an optional validity mask. View operations are metadata transformations: they do not copy element storage.
The type for tensor views.
Construction
create ?offset ?strides ?mask shape is a view over shape.
Defaults:
offsetdefaults to0.stridesdefaults to C-contiguous strides derived fromshape.maskdefaults toNone(all indices valid).
Mask bounds are half-open intervals (start, end) per dimension.
If shape has a zero-size dimension, the resulting view has offset = 0 and no mask.
Warning. If explicit strides or mask lengths do not match Array.length shape, downstream array checks may raise Invalid_argument.
Accessors
numel v is the product of dimensions in shape v.
numel of a scalar (ndim v = 0) is 1.
dim axis v is dimension axis of v.
Raises Invalid_argument if axis is outside [0; ndim v - 1].
stride axis v is stride axis of v.
Raises Invalid_argument if axis is outside [0; ndim v - 1].
mask v is v's optional validity mask.
A mask entry (b, e) means b <= index < e on the corresponding axis.
strides_opt v is Some s if v can be represented as a standard strided view without partial masking, and None otherwise.
Indexing
linear_index v idx is offset v + sum_i (idx.(i) * strides v.(i)).
Raises Invalid_argument if Array.length idx <> ndim v.
Note. This function does not validate index bounds or masks.
is_valid v idx is true iff idx is valid with respect to mask v.
If mask v = None, the result is true for any idx.
If mask v = Some m, idx must have the same rank and satisfy each masked interval bound.
Transformations
reshape v new_shape returns a view over the same storage with new_shape when stride-compatible.
Supported cases include:
- C-contiguous reshape.
- Reshape by adding/removing singleton dimensions.
- Certain merge/split patterns on compatible strided layouts.
- All-zero-stride broadcast layouts.
Raises Invalid_argument if reshape cannot be represented, including size mismatches (except zero-size special cases), masked views, or incompatible stride patterns.
expand v new_shape broadcasts singleton dimensions to new_shape by setting corresponding strides to 0.
Scalars (ndim v = 0) may expand to any rank.
Raises Invalid_argument if ranks are incompatible for non-scalars, or if a non-singleton dimension would need expansion.
permute v axes reorders dimensions according to axes.
Raises Invalid_argument if axes is not a valid permutation of [0; ndim v - 1].
shrink v bounds restricts v to per-axis half-open intervals (start, end).
Bounds must satisfy 0 <= start < end <= size for each dimension.
Raises Invalid_argument if bounds are malformed or rank mismatches.
pad v padding adds virtual padding (before, after) per axis.
The resulting view keeps data in place and records valid original regions via a mask.
Raises Invalid_argument if:
paddingrank mismatchesndim v.- A padding component is negative.