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 -> 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 -> 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.

Sourceval numel : t -> int

numel view returns total elements.

Sourceval dim : int -> t -> int

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.

Sourceval is_valid : t -> int array -> bool

is_valid view indices checks mask bounds.

Returns true if no mask or indices within all bounds.

Transformations

Sourceval reshape : t -> 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 -> 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.

OCaml

Innovation. Community. Security.