package nx
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=96d35ce03dfbebd2313657273e24c2e2d20f9e6c7825b8518b69bd1d6ed5870f
sha512=90c5053731d4108f37c19430e45456063e872b04b8a1bbad064c356e1b18e69222de8bfcf4ec14757e71f18164ec6e4630ba770dbcb1291665de5418827d1465
doc/nx.buffer/Nx_buffer/index.html
Module Nx_bufferSource
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 types
Standard 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.
Unsigned 32-bit integer.
Unsigned 64-bit integer.
Kind GADT
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 properties
kind_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.
Creation
create kind n allocates a zero-initialized buffer of n elements.
Properties
Element access
get buf i is the element at index i.
Raises Invalid_argument if i is out of bounds.
set buf i v sets the element at index i to v.
Raises Invalid_argument if i is out of bounds.
unsafe_set buf i v is like set without bounds checking.
Bulk operations
blit ~src ~dst copies all elements from src to dst.
Raises Invalid_argument if dimensions differ.
blit_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.
blit_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 conversions
of_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 utilities
Operations on Bigarray.Genarray.t that handle extended kinds. Used by I/O modules (npy, safetensors, images).
val genarray_create :
('a, 'b) kind ->
'c Bigarray.layout ->
int array ->
('a, 'b, 'c) Bigarray.Genarray.tgenarray_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.
val genarray_blit :
('a, 'b, 'c) Bigarray.Genarray.t ->
('a, 'b, 'c) Bigarray.Genarray.t ->
unitgenarray_blit src dst copies src to dst. Handles extended kinds.
val genarray_change_layout :
('a, 'b, 'c) Bigarray.Genarray.t ->
'd Bigarray.layout ->
('a, 'b, 'd) Bigarray.Genarray.tgenarray_change_layout ga layout changes the layout of ga.