package nx

  1. Overview
  2. Docs

Module Nx_core.ViewSource

Tensor view: strided view of tensor data.

Views describe how to interpret a linear buffer as a multi-dimensional array through shape, strides, and offset. Supports non-contiguous layouts and masked regions.

Sourcetype t

View encapsulating tensor layout information.

Creation

Sourceval create : ?offset:int -> ?strides:int array -> ?mask:(int * int) array -> Symbolic_shape.t -> t

create ?offset ?strides ?mask shape constructs view.

Default offset is 0. Default strides are C-contiguous. Mask specifies valid ranges per dimension as (start, end) pairs.

Properties

Sourceval shape : t -> Symbolic_shape.t

shape view returns dimension sizes.

Sourceval strides : t -> int array

strides view returns element strides per dimension.

Sourceval offset : t -> int

offset view returns starting position in buffer.

Sourceval ndim : t -> int

ndim view returns number of dimensions.

numel view returns total elements (may be symbolic).

Sourceval dim : int -> t -> Symbolic_shape.dim

dim axis view returns size of dimension axis.

Sourceval stride : int -> t -> int

stride axis view returns stride of dimension axis.

Sourceval mask : t -> (int * int) array option

mask view returns valid bounds per dimension if masked.

Sourceval is_c_contiguous : t -> bool

is_c_contiguous view tests for row-major contiguous layout.

Index Operations

Sourceval linear_index : t -> int array -> int

linear_index view indices computes buffer position.

Includes view's offset in result.

  • raises Failure

    if shape is symbolic and non-contiguous

Sourceval is_valid : t -> int array -> bool

is_valid view indices checks mask bounds.

Returns true if no mask or indices within all bounds.

Sourceval can_be_strided : t -> bool

Transformations

Sourceval reshape : t -> Symbolic_shape.t -> t

reshape view new_shape changes dimensions.

Returns view if possible, fails if requires reordering. Handles -1 dimensions and size-1 squeezing/unsqueezing.

  • raises Failure

    if cannot reshape strided view

Sourceval expand : t -> Symbolic_shape.t -> t

expand view new_shape broadcasts singleton dimensions.

Size-1 dimensions become size-n with stride 0.

Sourceval permute : t -> int array -> t

permute view axes reorders dimensions.

Sourceval shrink : t -> (int * int) array -> t

shrink view bounds restricts to sub-region.

Bounds are (start, end) pairs per dimension.

Sourceval pad : t -> (int * int) array -> t

pad view padding extends dimensions virtually.

Padding is (before, after) pairs. Creates mask for valid region.

Sourceval flip : t -> bool array -> t

flip view axes_to_flip reverses specified dimensions.

Adjusts strides to negative and updates offset.

View Composition

Sourceval merge : t -> t -> t option

merge view1 view2 attempts to compose two views.

Returns a single view representing the composition of view1 followed by view2, or None if the views cannot be merged.

Sourceval simplify : t -> t

simplify view attempts to simplify the view representation.

May return the same view if no simplification is possible.