Package index
nx
Library nx.buffer
Nx_buffer
Module
Module Nx_buffer Source Flat buffers for tensor storage.
Flat, C-layout, one-dimensional buffers with support for both standard Bigarray element types and extended types (bfloat16, bool, int4, float8, uint32, uint64).
The buffer type t is abstract in this interface. Conversions to and from Bigarray are explicit via of_bigarray1 , to_bigarray1 , of_genarray , and to_genarray .
Element typesStandard element types are aliases from Bigarray . Extended types are defined here.
Brain floating-point 16-bit.
Boolean stored as a byte.
Signed 4-bit integer (two values packed per byte).
Unsigned 4-bit integer (two values packed per byte).
8-bit float with 4 exponent and 3 mantissa bits.
8-bit float with 5 exponent and 2 mantissa bits.
Kind GADTSource type ('a, 'b) kind = | Float16 : (float, float16_elt ) kind | Float32 : (float, float32_elt ) kind | Float64 : (float, float64_elt ) kind | Bfloat16 : (float, bfloat16_elt ) kind | Float8_e4m3 : (float, float8_e4m3_elt ) kind | Float8_e5m2 : (float, float8_e5m2_elt ) kind | Int8_signed : (int, int8_signed_elt ) kind | Int8_unsigned : (int, int8_unsigned_elt ) kind | Int16_signed : (int, int16_signed_elt ) kind | Int16_unsigned : (int, int16_unsigned_elt ) kind | Int32 : (int32, int32_elt ) kind | Uint32 : (int32, uint32_elt ) kind | Int64 : (int64, int64_elt ) kind | Uint64 : (int64, uint64_elt ) kind | Int4_signed : (int, int4_signed_elt ) kind | Int4_unsigned : (int, int4_unsigned_elt ) kind | Complex32 : (Complex.t , complex32_elt ) kind | Complex64 : (Complex.t , complex64_elt ) kind | Bool : (bool, bool_elt ) kind The type for element kinds. Nineteen constructors covering standard Bigarray kinds and extended types.
Kind values Kind propertiesSource val kind_size_in_bytes : ('a , 'b ) kind -> intkind_size_in_bytes k is the storage size in bytes per element for kind k. For Int4_signed and Int4_unsigned this is 1 (two values packed per byte).
to_stdlib_kind k is the standard Bigarray.kind for k, or None for extended types.
Buffer type and operations('a, 'b) t is a flat, C-layout, one-dimensional buffer.
Creationcreate kind n allocates a zero-initialized buffer of n elements.
Propertieskind buf is the element kind of buf.
Source val length : ('a , 'b ) t -> intlength buf is the number of elements in buf.
Element accessSource val get : ('a , 'b ) t -> int -> 'a get buf i is the element at index i.
Raises Invalid_argument if i is out of bounds.
Source val set : ('a , 'b ) t -> int -> 'a -> unitset buf i v sets the element at index i to v.
Raises Invalid_argument if i is out of bounds.
Source val unsafe_get : ('a , 'b ) t -> int -> 'a unsafe_get buf i is like get without bounds checking.
Source val unsafe_set : ('a , 'b ) t -> int -> 'a -> unitunsafe_set buf i v is like set without bounds checking.
Bulk operationsSource val fill : ('a , 'b ) t -> 'a -> unitfill buf v sets every element of buf to v.
Source val blit : src :('a , 'b ) t -> dst :('a , 'b ) t -> unitblit ~src ~dst copies all elements from src to dst.
Raises Invalid_argument if dimensions differ.
Source val blit_from_bytes :
?src_off :int ->
?dst_off :int ->
?len :int ->
bytes ->
('a , 'b ) t ->
unitblit_from_bytes ?src_off ?dst_off ?len bytes buf copies len elements from bytes into buf. Offsets and length are in elements. src_off and dst_off default to 0. len defaults to length buf - dst_off.
Source val blit_to_bytes :
?src_off :int ->
?dst_off :int ->
?len :int ->
('a , 'b ) t ->
bytes ->
unitblit_to_bytes ?src_off ?dst_off ?len buf bytes copies len elements from buf into bytes. Offsets and length are in elements. src_off and dst_off default to 0. len defaults to length buf - src_off.
Bigarray conversionsof_bigarray1 ba is ba viewed as a buffer. Zero-copy for standard kinds.
to_bigarray1 buf is buf viewed as a one-dimensional bigarray. Zero-copy.
to_genarray buf shape reshapes buf into a genarray with shape. The product of shape must equal length buf.
of_genarray ga flattens ga into a one-dimensional buffer.
Genarray utilitiesOperations on Bigarray.Genarray.t that handle extended kinds. Used by I/O modules (npy, safetensors, images).
genarray_create kind layout dims allocates a genarray. Handles both standard and extended kinds.
genarray_kind ga is the kind of ga, including extended kinds.
genarray_dims ga is the dimensions of ga.
genarray_blit src dst copies src to dst. Handles extended kinds.
genarray_change_layout ga layout changes the layout of ga.