Module Nx_core.View
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.
Construction
val create :
?offset:int ->
?strides:int array ->
?mask:(int * int) array ->
int array ->
tcreate ?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
val shape : t -> int arrayshape v is v's shape.
val strides : t -> int arraystrides v is v's stride vector.
val offset : t -> intoffset v is v's linear base offset.
val ndim : t -> intndim v is Array.length (shape v).
val numel : t -> intnumel v is the product of dimensions in shape v.
numel of a scalar (ndim v = 0) is 1.
val dim : int -> t -> intdim axis v is dimension axis of v.
Raises Invalid_argument if axis is outside [0; ndim v - 1].
val stride : int -> t -> intstride axis v is stride axis of v.
Raises Invalid_argument if axis is outside [0; ndim v - 1].
val mask : t -> (int * int) array optionmask v is v's optional validity mask.
A mask entry (b, e) means b <= index < e on the corresponding axis.
val is_c_contiguous : t -> boolis_c_contiguous v is true iff v is recognized as C-contiguous.
val strides_opt : t -> int array optionstrides_opt v is Some s if v can be represented as a standard strided view without partial masking, and None otherwise.
val can_get_strides : t -> boolcan_get_strides v is true iff strides_opt v is Some _.
val is_materializable : t -> boolis_materializable v is true iff can_get_strides v is true.
Indexing
val linear_index : t -> int array -> intlinear_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.
val is_valid : t -> int array -> boolis_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.