tensors

package
v0.28.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 20 Imported by: 12 modules, 43 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tensors implement a `Tensor`, a representation of a multidimensional array.

Tensors are multidimensional arrays (from scalar with 0 dimensions, to arbitrarily large dimensions), defined by their shape (a data type and its axes' dimensions) and their actual content. As a special case, a Tensor can also be a tuple of multiple tensors.

The main use of tensors are to be used as input and output of GoMLX computation graphs.

There are various ways to construct a Tensor from local data:

  • FromShape(shape shapes.Shape): creates a tensor with the given shape, and zero values.

  • FromShapeForBackend(backend compute.Backend, shape shapes.Shape): creates a tensor with the given shape, using a shared buffer if the backend supports it.

  • FromScalarAndDimensions[T shapes.Supported](value T, dimensions ...int): creates a Tensor with the given dimensions, filled with the scalar value given. `T` must be one of the supported types.

  • FromFlatDataAndDimensions[T shapes.Supported](data []T, dimensions ...int): creates a Tensor with the given dimensions and set the flattened values with the given data. `T` must be one of the supported types. Example:

    t := FromFlatDataAndDimensions([]int8{1, 2, 3, 4}, 2, 2}) // Tensor with [[1,2], [3,4]]

  • FromValue[S MultiDimensionSlice](value S): Generic conversion works with the scalar supported `DType`s as well as with any arbitrary multidimensional slice of them. Slices of rank > 1 must be regular, that is all the sub-slices must have the same shape. Example:

    t := FromValue([][]float{{1,2}, {3, 5}, {7, 11}})`

  • FromAnyValue(value any): same as FromValue but non-generic, it takes an anonymous type `any`. The exception is if `value` is already a tensor, then it is a no-op, and it returns the tensor itself.

Behind the scenes (as much as possible Tensor tries to hide all the details), Tensor is a container that keeps in sync different materialization's of value:

  • `local`: a copy of the values stored in CPU, as a Go flat array of the underlying dtype.
  • `onDevices`: a copy of the values stored in the accelerator device(s) (CPU, GPU, TPU, etc.), a wrapper for whatever the backend uses as buffer managed by the lower levels (see github.com/gomlx/gopjrt for the XLA backend). There can be multiple `Device` backings of a tensor if there are multiple devices (like a multi-GPU setup).
  • And "on-device" Tensor can also be "shared" if the backend allows it, in which case the local and "on-device" share the same memory allocation.

The Tensor container is lazy in nature: it won't transfer data from local storage to "on device" until needed. And if/when it can, it will make it "shared" (generally, when running on CPUs). If not "shared", when one (local or on-device) is updated, the others are immediately invalidated.

Transferring tensors to/from local/device areas has a cost and should be avoided. For example, while training weights of an ML model, one generally does not need to transfer those weights to local -- just at the end of training to save the model weights. But the Tensor will keep the (local/device) copies cached, so they can be used multiple times, and transfer only occurs once.

Tensors used across multiple backends:

There is not an easy interface to share tensors across backends (instances) yet, even if they are the same type of backends (if you created two instances of `xla:cpu` backend, for instance).

The recommendation is to keep tensors used for each backend in separate variables and copy when needed: You can use `Tensor.LocalClone()` to copy a tensor from one backend to a new local tensor. Or you can use `Tensor.OnDeviceClone()` to copy a tensor from one backend directly into another backend.

Alternatively, after using a tensor as input to a Backend computation, or a tensor returned from the Backend, call Tensor.ToLocal(): it will remove any links to the backend (by copying all the data locally), and it can then be used by other compute.

Index

Constants

View Source
const SummarySampleHalfSizeEnv = "GOMLX_TENSOR_SUMMARY_SAMPLE_SIZE"
View Source
const TensorStringDefaultPrecision = 4

TensorStringDefaultPrecision used by Tensor.String.

Variables

View Source
var SummarySampleHalfSize = 3

SummarySampleHalfSize holds the default number of elements to print from the beginning and from the end of each dimension, by Tensor.Summary() (same as Tensor.String()) The default is 3, so for a 1D tensor of size 10, the first 3 and last 3 elements will be printed, in between the first and last 3 elements, "..." will be printed.

It can be changed by setting the SummarySampleHalfSize parameter in the context, or by setting the environment variable GOMLX_TENSOR_SUMMARY_SAMPLE_SIZE.

Functions

func AssignFlatData

func AssignFlatData[T gotype.Supported](toTensor *Tensor, fromFlat []T) error

AssignFlatData will copy over the values in fromFlat to the storage used by toTensor. If the dtypes are not compatible or if the size is wrong, it will panic.

func ConstFlatData

func ConstFlatData[T gotype.Supported](t *Tensor, accessFn func(flat []T)) error

ConstFlatData calls accessFn with the flattened data as a slice of the Go type corresponding to the DType type. Even scalar values have a flattened data representation of one element. It locks the Tensor until accessFn returns.

It is the "generics" version of Tensor.ConstFlatData(),

This provides accessFn with the actual Tensor data (not a copy), and it's owned by the Tensor, but it should not be changed -- the contents of the corresponding "on device" tensors could go out-of-sync. See Tensor.MutableFlatData to access a mutable version of the flat data.

See Tensor.Size for the number of elements, and Tensor.LayoutStrides to calculate the offset of individual positions, given the indices at each axis.

func CopyFlatData

func CopyFlatData[T gotype.Supported](t *Tensor) ([]T, error)

CopyFlatData returns a copy of the flat data of the Tensor.

It triggers a synchronous transfer from device to local if the tensor is only on-device.

It returns an error if the given generic type doesn't match the DType of the tensor.

func MustConstFlatData

func MustConstFlatData[T gotype.Supported](t *Tensor, accessFn func(flat []T))

MustConstFlatData calls accessFn with the flattened data as a slice of the Go type corresponding to the DType type. Even scalar values have a flattened data representation of one element. It locks the Tensor until accessFn returns.

It is the "generics" version of Tensor.ConstFlatData(),

This provides accessFn with the actual Tensor data (not a copy), and it's owned by the Tensor, but it should not be changed -- the contents of the corresponding "on device" tensors could go out-of-sync. See Tensor.MutableFlatData to access a mutable version of the flat data.

See Tensor.Size for the number of elements, and Tensor.LayoutStrides to calculate the offset of individual positions, given the indices at each axis.

It panics if the tensor is in an invalid state (if it was finalized), or if it is a tuple.

func MustCopyFlatData

func MustCopyFlatData[T gotype.Supported](t *Tensor) []T

MustCopyFlatData returns a copy of the flat data of the Tensor.

It triggers a synchronous transfer from device to local if the tensor is only on-device.

It will panic if the given generic type doesn't match the DType of the tensor.

func MustMutableFlatData

func MustMutableFlatData[T gotype.Supported](t *Tensor, accessFn func(flat []T))

MustMutableFlatData calls accessFn with a flat slice pointing to the Tensor data. The type of the slice corresponds to the DType of the tensor. The contents of the slice itself can be changed until accessFn returns. During this time the Tensor is locked.

It is the "generics" version of Tensor.MutableFlatData(), see its description for more details.

This returns the actual Tensor data (not a copy), and the data owned by the Tensor, and should only be changed inside accessFn.

It panics if the tensor is in an invalid state (if it was finalized), or if it is a tuple.

func MutableFlatData

func MutableFlatData[T gotype.Supported](t *Tensor, accessFn func(flat []T)) error

MutableFlatData calls accessFn with a flat slice pointing to the Tensor data. The type of the slice corresponds to the DType of the tensor. The contents of the slice itself can be changed until accessFn returns. During this time the Tensor is locked.

It is the "generics" version of Tensor.MutableFlatData(), see its description for more details.

This returns the actual Tensor data (not a copy), and the data owned by the Tensor, and should only be changed inside accessFn.

func ToScalar

func ToScalar[T gotype.Supported](t *Tensor) T

ToScalar returns the scalar value of the Tensor.

It triggers a synchronous transfer from device to local if the tensor is only on-device.

It will panic if the given generic type doesn't match the DType of the tensor.

func UnpackSubByteAt

func UnpackSubByteAt(packed []uint8, dtype dtypes.DType, unpackedIdx int) int8

UnpackSubByteAt returns the packed value at the given upacked position -- only one value is unpacked. The packed slice represents sub-byte data (Uint1, Int1, Uint2, Int2, Uint4, Int4, packed into []uint8). It returns the value as an int8, enough bits for both unsigned and signed values.

It can panic if non-packed dtype is given, or an out-of-bound unpackedIdx.

func UnpackSubBytes

func UnpackSubBytes(packed []uint8, dtype dtypes.DType, numValues int) []int8

UnpackSubBytes unpacks packed sub-byte data (Uint1, Int1, Uint2, Int2, Uint4, Int4, packed into []uint8) into a one-value-per-element []int8 slice -- enough bits to represent the unsigned types as well.

It unpacks only the first numValues. If numValues is <= 0, it will unpack all the values in packed.

It can panic if a non-packed dtype is given.

Types

type MultiDimensionSlice

type MultiDimensionSlice interface {
	bool | float32 | float64 | int | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | complex64 | complex128 | bfloat16.BFloat16 | float16.Float16 |
		[]bool | []float32 | []float64 | []int | []int8 | []int16 | []int32 | []int64 | []uint8 | []uint16 | []uint32 | []uint64 | []complex64 | []complex128 | []bfloat16.BFloat16 | []float16.Float16 |
		[][]bool | [][]float32 | [][]float64 | [][]int | [][]int8 | [][]int16 | [][]int32 | [][]int64 | [][]uint8 | [][]uint16 | [][]uint32 | [][]uint64 | [][]complex64 | [][]complex128 | [][]bfloat16.BFloat16 | [][]float16.Float16 |
		[][][]bool | [][][]float32 | [][][]float64 | [][][]int | [][][]int8 | [][][]int16 | [][][]int32 | [][][]int64 | [][][]uint8 | [][][]uint16 | [][][]uint32 | [][][]uint64 | [][][]complex64 | [][][]complex128 | [][][]bfloat16.BFloat16 | [][][]float16.Float16 |
		[][][][]bool | [][][][]float32 | [][][][]float64 | [][][][]int | [][][][]int8 | [][][][]int16 | [][][][]int32 | [][][][]int64 | [][][][]uint8 | [][][][]uint16 | [][][][]uint32 | [][][][]uint64 | [][][][]complex64 | [][][][]complex128 | [][][][]bfloat16.BFloat16 | [][][][]float16.Float16
}

Generated by `github.com/gomlx/gomlx/internal/cmd/constraints_generator`.

type Tensor

type Tensor struct {
	// contains filtered or unexported fields
}

Tensor represents a multidimensional array (from scalar with 0 dimensions, to arbitrarily large dimensions), defined by their shape, a data type (dtypes.DType) and its axes' dimensions, and their actual content stored as a flat (1D) array of values.

The main use of tensors is to be used as GoMLX computation graphs inputs and outputs.

It is a container for "local" (host CPU) and "on-device" backing of the tensor -- they can be the same ("shared") in some cases. It is always stored as a flat slice of the underlying DType.

Tensor manages caching of Local and Device copies. There is a transferring cost that one needs to be aware of when using it for large data -- LLM models can have hundreds of GB in size. There is a cache system to prevent duplicate transfers, but it requires some care from the user (see ConstFlatData and MutableFlatData).

Tensors used across multiple backends:

There is not an easy interface to share tensors across backends (instances) yet, even if they are the same type of backends (if you created two instances of `xla:cpu` backend, for instance).

The recommendation is to keep tensors used for each backend in separate variables and copy when needed: You can use `Tensor.LocalClone()` to copy a tensor from one backend to a new local tensor. Or you can use `Tensor.OnDeviceClone()` to copy a tensor from one backend directly into another backend.

Alternatively, after using a tensor as input to a Backend computation, or a tensor returned from the Backend, call Tensor.ToLocal(): it will remove any links to the backend (by copying all the data locally), and it can then be used by other compute.

More details in the `tensor` package documentation.

func FromAnyValue

func FromAnyValue(value any) (*Tensor, error)

MustFromAnyValue is a non-generic version of FromValue that returns a *tensors.Tensor (not specified if local or on device). The input is expected to be either a scalar or a slice of slices with homogeneous dimensions. If the input is a tensor already (Local or Device), it is simply returned. If value is anything but a Device tensor, it will return a Local tensor.

It panics with an error if the value type is unsupported or the shape is not regular.

func FromBuffer

func FromBuffer(buffer compute.Buffer) (*Tensor, error)

FromBuffer creates a Tensor from a backend's buffer. The ownership of the buffer is transferred to the new Tensor.

This doesn't work for shared buffers, so it assumes the buffer is not shared.

func FromFlatDataAndDimensions

func FromFlatDataAndDimensions[T gotype.Supported](data []T, dimensions ...int) *Tensor

FromFlatDataAndDimensions creates a tensor with the given dimensions, filled with the flattened values given in `data`. The data is copied to the Tensor. The `DType` is inferred from the `data` type.

It panics if the size of data is wrong for the shape.

func FromRaw

func FromRaw(backend compute.Backend, deviceNum compute.DeviceNum, shape shapes.Shape, data []byte) (*Tensor, error)

FromRaw returns a tensor with the given shape and dtype, initialized with the raw bytes given in data -- it's up to the user to use the correct endianess for the backend.

It panics if the size of data is wrong for the shape.

If the backend is nil, it creates a local tensor and data is copied to a newly allocated local tensor storage.

If the backend supports shared buffers, it creates the shared buffer tensor and copies over the data.

For backends with accelerators, this is the most performant way to upload a backend tensor with some data, avoiding unnecessary copies. It works well with mmap (memory-mapped) files, where data can be accessed directly from kernel buffers (saving a copy to a temporary buffer that is initialized to 0 in Go, etc.)

func FromScalar

func FromScalar[T gotype.Supported](value T) (t *Tensor)

FromScalar creates a local tensor with the given scalar. The `DType` is inferred from the value.

func FromScalarAndDimensions

func FromScalarAndDimensions[T gotype.Supported](value T, dimensions ...int) *Tensor

FromScalarAndDimensions creates a local tensor with the given dimensions, filled with the given scalar value replicated everywhere. The `DType` is inferred from the value.

func FromShape

func FromShape(shape shapes.Shape) *Tensor

FromShape returns a new Tensor with the given shape with local (host) memory, with the data initialized with zeros.

If you know the backend you're going to use, prefer FromShapeForBackend, which will leverage shared buffers if available.

It panics if you provide an invalid shape.

func FromShapeForBackend

func FromShapeForBackend(backend compute.Backend, deviceNum compute.DeviceNum, shape shapes.Shape) (*Tensor, error)

FromShapeForBackend creates a new tensor with the given shape, to be used with the given backend and deviceNum (usually 0 if you only have one device).

If the backend supports shared buffers, it creates the tensor using a shared buffer.

If the backend is nil or if the backend does not support shared buffers, it creates a local copy of the tensor, and works just like FromShape().

Notice, there is no point in simply creating a tensor on-device without shared buffers, since one cannot edit data directly on-device (simply copy the whole tensor over). See FromShapeAndBytesForBackend instead if you have data to transfer directly to the backend.

func FromValue

func FromValue[S MultiDimensionSlice](value S) *Tensor

FromValue returns a `Local` tensor constructed from the given multi-dimension slice (or scalar). If the rank of the `value` is larger than 1, the shape of all sub-slices must be the same.

It panics if the shape is not regular.

Notice that FromFlatDataAndDimensions is much faster if speed here is a concern.

func GobDeserialize

func GobDeserialize(decoder *gob.Decoder) (*Tensor, error)

GobDeserialize a Tensor from the reader.

If the tensor is only going to be directly consumed by the execution of a graph (an ML model), use GoDeserializeOnDevice instead; it works faster for some compute.

func GobDeserializeToDevice

func GobDeserializeToDevice(
	decoder *gob.Decoder,
	backend compute.Backend,
	deviceNum compute.DeviceNum,
) (*Tensor, error)

GobDeserializeToDevice deserialize a Tensor from the reader directly to on-device memory. If the tensor is expected to be consumed by graph execution, this may work faster by avoiding an unnecessary copy.

Returns new Tensor (with shared or onDevice storage or an error).

func Load

func Load(filePath string) (*Tensor, error)

Load a Local tensor from the file path given.

func MustFromAnyValue

func MustFromAnyValue(value any) *Tensor

MustFromAnyValue converts a value to a Tensor, or panics if it is not able. See deatils in FromAnyValue.

func (*Tensor) AssertValid

func (t *Tensor) AssertValid()

AssertValid panics if it's nil, has been finalized, or if its shape is invalid.

func (*Tensor) Backend

func (t *Tensor) Backend() (compute.Backend, error)

Backend returns the backend of the on-device copy of the Tensor, if any.

It returns an error is the tensor is stored locally.

func (*Tensor) Buffer

func (t *Tensor) Buffer(backend compute.Backend, deviceNum compute.DeviceNum) (compute.Buffer, error)

Buffer returns the backend buffer for the tensor. It triggers the transfer from local to the backend device if the tensor is not already stored on the device.

Careful not to finalize the tensor while the buffer is in use -- e.g.: during the execution that uses the buffer as input.

func (*Tensor) ByteSize

func (t *Tensor) ByteSize() int64

ByteSize returns the number of bytes used to store the tensor. An alias to Tensor.Shape().ByteSize().

func (*Tensor) CheckValid

func (t *Tensor) CheckValid() error

CheckValid returns an error if it's nil, has been finalized, or if its shape is invalid.

func (*Tensor) Clone

func (t *Tensor) Clone() (*Tensor, error)

Clone creates a clone of the Tensor value with shared backing with the backend -- if it supports -- or it falls back to LocalClone.

If cloning a tensor on-device or with shared buffers, the returned tensor will be cloned to the same device.

For more fine control, consider using LocalClone or OnDeviceClone.

func (*Tensor) ConstBytes

func (t *Tensor) ConstBytes(accessFn func(data []byte)) error

ConstBytes calls accessFn with the data as a bytes slice. Even scalar values have a bytes data representation of one element. It locks the Tensor until accessFn returns.

This provides accessFn with the actual Tensor data (not a copy), and it's owned by the Tensor, and it should not be changed -- the contents of the corresponding "on device" tensors would go out-of-sync. See Tensor.MutableBytes to access a mutable version of the data as bytes.

It panics if the tensor is in an invalid state (if it was finalized), or if it is a tuple.

func (*Tensor) ConstFlatData

func (t *Tensor) ConstFlatData(accessFn func(flat any)) error

ConstFlatData calls accessFn with the flattened data as a slice of the Go type corresponding to the DType type. Even scalar values have a flattened data representation of one element. It locks the Tensor until accessFn returns.

It triggers a synchronous transfer from device to local if the tensor is only on-device.

This provides accessFn with the actual Tensor data (not a copy), and it's owned by the Tensor, but it should not be changed -- the contents of the corresponding "on device" tensors would go out-of-sync. See Tensor.MutableFlatData to access a mutable version of the flat data.

See Tensor.Size for the number of elements, and Tensor.LayoutStrides to calculate the offset of individual positions, given the indices at each axis.

Even scalar values have a flattened data representation of one element.

Notice that for packed sub-byte packed values (Int1, Uint1, Int2, Uint2, Int4, Uint4), this gives access to the raw bytes, a []uint8, and it's up to the caller to interpret it correctly.

func (*Tensor) CopyFrom

func (t *Tensor) CopyFrom(tFrom *Tensor) error

CopyFrom will copy the contents from tFrom. The tensors t and tFrom must have the same shape.

This is efficient if tFrom is on-device only, in which case the device values are materialized locally into t, the receiving tensor.

func (*Tensor) DType

func (t *Tensor) DType() dtypes.DType

DType returns the DType of the tensor's shape. It is a shortcut to `Tensor.Shape().DType`.

func (*Tensor) Device

func (t *Tensor) Device() (compute.DeviceNum, error)

Device returns the deviceNum of the on-device copy of the Tensor, if any.

It returns an error is the tensor is stored locally.

func (*Tensor) DonateBuffer

func (t *Tensor) DonateBuffer(backend compute.Backend, deviceNum compute.DeviceNum) (compute.Buffer, error)

DonateBuffer returns the backend buffer for the tensor and transfers the ownership of the buffer to the caller. This may invalidate the tensor if there is no local storage -- in particular this is true if using "shared buffers" (generally true for CPU-based plugins), since they don't have a separate local copy.

Mostly used internally -- by graph.Graph.Run and graph.Exec when the value in the buffer is no longer needed after execution.

It triggers the transfer from local to the backend device if the tensor is not already stored on the device.

It doesn't finalize(release) the local tensor value (if there is one).

func (*Tensor) Equal

func (t *Tensor) Equal(otherTensor *Tensor) bool

Equal checks weather t == otherTensor. If they are the same pointer, they are considered equal. If the shapes are different, it returns false. If either side is invalid (nil), it panics.

Slow implementation: fine for small tensors, but write something specialized for the DType if speed is desired.

func (*Tensor) FinalizeAll

func (t *Tensor) FinalizeAll() error

FinalizeAll immediately frees all associated data and leave Tensor in an invalid state.

It's the caller's responsibility to ensure the tensor buffers are not being used elsewhere (like in the middle of an execution).

func (*Tensor) FinalizeLocal

func (t *Tensor) FinalizeLocal()

FinalizeLocal immediately frees the local storage copy of the tensor. If there are no on-device copies of the tensor, it becomes invalid.

If the storage is shared (see Tensor.IsShared), this is a no-op.

If the tensor is invalid (already finalized), this is a no-op.

func (*Tensor) GoStr

func (t *Tensor) GoStr() string

GoStr converts to string, using a Go-syntax representation that can be copied&pasted back to code.

Sub-byte packed values (Int1, Uint1, Int2, Uint2, Int4, Uint4) are unpacked to int8 before being printed as Go values.

func (*Tensor) GobSerialize

func (t *Tensor) GobSerialize(encoder *gob.Encoder) error

GobSerialize Tensor in binary format.

It triggers a synchronous transfer from device to local if the tensor is only on-device.

It returns an error for I/O errors. It panics for invalid tensors.

func (*Tensor) HasLocal

func (t *Tensor) HasLocal() bool

HasLocal returns whether there is an up-to-date copy of the Tensor on local storage. If false, any access to the data (e.g.: Tensor.ConstFlatData) will require a transfer (Tensor.MaterializeToLocal).

func (*Tensor) InDelta

func (t *Tensor) InDelta(otherTensor *Tensor, delta float64) bool

InDelta checks weather Abs(t - otherTensor) < delta for every element. If they are the same pointer, they are considered equal. If the shapes are different, it returns false. If either is invalid (nil), it panics. If the DType is not a float or complex, it also panics.

Slow implementation: fine for small tensors, but write something specialized for the DType if speed is desired.

func (*Tensor) InvalidateOnDevice

func (t *Tensor) InvalidateOnDevice() error

InvalidateOnDevice destroys all on-device copies of the Tensor, so the local copy becomes the source of truth. It does nothing if the tensor is shared -- see the Tensor.IsShared method.

It's the caller's responsibility to ensure this buffer is not being used elsewhere (like in the middle of an execution).

This is automatically called if the Tensor is mutated (e.g.: Tensor.MutableFlatData) or when the on-device value is donated to the execution of a graph.

If there is no local copy of the Tensor, this will invalidate the whole tensor.

Usually, this is called automatically. Mostly for internal use.

func (*Tensor) IsLocal

func (t *Tensor) IsLocal() bool

IsLocal returns true if there is a local storage copy of the tensor. If the tensor is shared (see Tensor.IsShared), it also returns true.

See MaterializeLocal to trigger a transfer/copy to the local storage.

func (*Tensor) IsOnAnyDevice

func (t *Tensor) IsOnAnyDevice() bool

IsOnAnyDevice checks whether the Tensor has an on-device copy on any device.

func (*Tensor) IsOnDevice

func (t *Tensor) IsOnDevice(deviceNum compute.DeviceNum) bool

IsOnDevice checks whether the Tensor has an on-device copy on the given deviceNum.

See MustMaterializeOnDevice to trigger a transfer/copy to the given device.

func (*Tensor) IsScalar

func (t *Tensor) IsScalar() bool

IsScalar returns whether the tensor represents a scalar value. It is a shortcut to `Tensor.Shape().IsScalar()`.

func (*Tensor) IsShared

func (t *Tensor) IsShared() bool

IsShared returns whether the underlying tensor storage is shared with the backend engine.

In most cases, an end-user doesn't need to use this.

If true, one shouldn't access it (ConstFlatData or MutableFlatData) during the execution of a computation graph that uses it.

The Tensor implementation will try to use shared tensors where possible, since they save an extra copy.

func (*Tensor) LayoutStrides

func (t *Tensor) LayoutStrides() (strides []int)

LayoutStrides return the strides for each axis. This can be handy when manipulating the flat data.

func (*Tensor) LocalClone

func (t *Tensor) LocalClone() (*Tensor, error)

LocalClone creates a clone of the Tensor value with local backing. It will trigger a transfer from on-device data to local if the value is not present in local memory yet.

func (*Tensor) MaterializeLocal

func (t *Tensor) MaterializeLocal()

MaterializeLocal will make sure there is a local storage of the tensor. If there isn't already a local copy, this triggers a transfer from an on-device storage to a local copy.

It's a "no-op" if using shared buffers with the accelerator. If you want to force a local copy, use LocalClone instead.

func (*Tensor) MaterializeOnDevice

func (t *Tensor) MaterializeOnDevice(backend compute.Backend, share bool, deviceNum compute.DeviceNum) error

MaterializeOnDevice will transfer a Tensor from local storage to the given device, if needed, or transfer from one device to another (freeing the source device copy).

Generally the user doesn't need to call this function, it is called by the libraries executing GoMLX computations automatically when needed (e.g.: graph.Exec or model.Exec).

If share is true, and if the backend allows for shared buffers, this will create a shared buffer, which can be more economic.

- If an updated copy of the Tensor is already on the device, this is a no-op. - If the Tensor has already been used with a different client, this panics: one cannot mix clients on the same Tensor.

func (*Tensor) Memory deprecated

func (t *Tensor) Memory() uintptr

Memory returns the number of bytes used to store the tensor. An alias to Tensor.Shape().ByteSize().

Deprecated: use ByteSize() instead.

func (*Tensor) MustConstFlatData

func (t *Tensor) MustConstFlatData(accessFn func(flat any))

MustConstFlatData calls accessFn with the flattened data as a slice of the Go type corresponding to the DType type. Even scalar values have a flattened data representation of one element. It locks the Tensor until accessFn returns.

It triggers a synchronous transfer from device to local if the tensor is only on-device.

This provides accessFn with the actual Tensor data (not a copy), and it's owned by the Tensor, but it should not be changed -- the contents of the corresponding "on device" tensors would go out-of-sync. See Tensor.MutableFlatData to access a mutable version of the flat data.

See Tensor.Size for the number of elements, and Tensor.LayoutStrides to calculate the offset of individual positions, given the indices at each axis.

Even scalar values have a flattened data representation of one element.

It panics if the tensor is in an invalid state (if it was finalized), or if it is a tuple.

func (*Tensor) MustFinalizeAll

func (t *Tensor) MustFinalizeAll()

MustFinalizeAll immediately frees all associated data and leave Tensor in an invalid state.

It's the caller's responsibility to ensure the tensor buffers are not being used elsewhere (like in the middle of an execution).

Panics on error.

func (*Tensor) MustMaterializeOnDevice

func (t *Tensor) MustMaterializeOnDevice(backend compute.Backend, share bool, deviceNum compute.DeviceNum)

MustMaterializeOnDevice will transfer a Tensor from local storage to the given device, if needed, or transfer from one device to another (freeing the source device copy).

Generally the user doesn't need to call this function, it is called by the libraries executing GoMLX computations automatically when needed (e.g.: graph.Exec or model.Exec).

If share is true, and if the backend allows for shared buffers, this will create a shared buffer, which can be more economic.

- If an updated copy of the Tensor is already on the device, this is a no-op. - If the Tensor has already been used with a different client, this panics: one cannot mix clients on the same Tensor.

It panics on a backend error.

func (*Tensor) MustMutableFlatData

func (t *Tensor) MustMutableFlatData(accessFn func(flat any))

MustMutableFlatData calls accessFn with a flat slice pointing to the Tensor data. The type of the slice corresponds to the DType of the tensor. The contents of the slice itself can be changed until accessFn returns. During this time the Tensor is locked.

If the data is not shared with the backend (usually, only available for CPU), it invalidates and frees any copy of the data on-device (e.g., GPU). It also triggers a synchronous transfer from device to local if the tensor is only on-device and not shared.

Even scalar values have a flattened data representation of one element.

This returns the actual Tensor data (not a copy), and the slice is owned by the Tensor -- but its contents can be changed while inside accessFn.

See Tensor.ConstFlatData to access a mutable version of the flat data.

See Tensor.Size for the number of elements, and Tensor.LayoutStrides to calculate the offset of individual positions, given the indices at each axis.

It panics if the tensor is in an invalid state (if it was finalized), or if it is a tuple.

func (*Tensor) MutableBytes

func (t *Tensor) MutableBytes(accessFn func(data []byte)) error

MutableBytes gives mutable access to the local storage of the values for the tensor. It's similar to MutableFlatData but provides a bytes view to the same data.

If the tensor is not shared, it triggers a synchronous transfer from device to local if the tensor is only on-device, and it invalidates the device storage, since it's assumed they will be out-of-date.

This returns the actual Tensor data (not a copy), and the bytes slice is owned by the Tensor -- but its contents can be changed while inside accessFn.

See Tensor.ConstBytes for constant access to the data as bytes -- that doesn't invalidate the device storage.

func (*Tensor) MutableFlatData

func (t *Tensor) MutableFlatData(accessFn func(flat any)) error

MutableFlatData calls accessFn with a flat slice pointing to the Tensor data. The type of the slice corresponds to the DType of the tensor. The contents of the slice itself can be changed until accessFn returns. During this time the Tensor is locked.

If the data is not shared with the backend (usually, only available for CPU), it invalidates and frees any copy of the data on-device (e.g., GPU). It also triggers a synchronous transfer from device to local if the tensor is only on-device and not shared.

Even scalar values have a flattened data representation of one element.

This returns the actual Tensor data (not a copy), and the slice is owned by the Tensor -- but its contents can be changed while inside accessFn.

See Tensor.ConstFlatData to access a mutable version of the flat data.

See Tensor.Size for the number of elements, and Tensor.LayoutStrides to calculate the offset of individual positions, given the indices at each axis.

It returns an error if the tensor is in an invalid state (if it was finalized), or if it is a tuple.

func (*Tensor) Ok

func (t *Tensor) Ok() bool

Ok returns whether the Tensor is in a valid state: it is not nil, and it hasn't been finalized.

func (*Tensor) OnDeviceClone

func (t *Tensor) OnDeviceClone(backend compute.Backend, deviceNum compute.DeviceNum) (*Tensor, error)

OnDeviceClone creates a clone of the tensor t that has backend storage. It also works to copy tensors to a different backend.

func (*Tensor) Rank

func (t *Tensor) Rank() int

Rank returns the rank of the tensor's shape. It is a shortcut to `Tensor.Shape().Rank()`.

func (*Tensor) Save

func (t *Tensor) Save(filePath string) error

Save the Local tensor to the given file path.

func (*Tensor) Shape

func (t *Tensor) Shape() shapes.Shape

Shape of Local, includes DType.

func (*Tensor) Size

func (t *Tensor) Size() int

Size returns the number of elements in the tensor.

func (*Tensor) StorageSize

func (t *Tensor) StorageSize() int

StorageSize returns the number of storage units used to store the tensor. This in most cases is equal to Size(), but for packed dtypes it is smaller.

func (*Tensor) String

func (t *Tensor) String() string

String converts to string, if not too large. It uses t.Summary(precision=4).

func (*Tensor) Summary

func (t *Tensor) Summary(precision int) string

Summary returns a multi-line summary of the Tensor's content. Inspired by numpy output.

func (*Tensor) ToDevice

func (t *Tensor) ToDevice(backend compute.Backend, deviceNum compute.DeviceNum) error

ToDevice is a convenience function to make the tensor available only on device, freeing the local (on-host) storage.

It calls MaterializeOnDevice and using shared buffers if possible and frees the local tensor storage by calling FinalizeLoca().

It's a no-op if the tensor is already on the device.

func (*Tensor) ToLocal

func (t *Tensor) ToLocal() error

ToLocal forces the tensor to move its data to local (host CPU) storage and detaches itself from the backend. It returns itself to allow for cascading calls.

If the tensor already has a local storage, there is no copy involved.

Any on-device storage is freed.

func (*Tensor) Value

func (t *Tensor) Value() any

Value returns a multidimensional slice (except if the shape is a scalar) containing a copy of the values stored in the tensor. This is expensive and usually only used for smaller tensors in tests and to print results.

If the local tensor is empty, it panics with the corresponding error.

Sub-byte packed values (Int1, Uint1, Int2, Uint2, Int4, Uint4) are unpacked to int8 before being returned, so they can more easily be printed or used.

func (*Tensor) ValueSafe

func (t *Tensor) ValueSafe() (any, error)

ValueSafe returns a multidimensional slice (except if the shape is a scalar) containing a copy of the values stored in the tensor. This is expensive and usually only used for smaller tensors in tests and to print results.

Sub-byte packed values (Int1, Uint1, Int2, Uint2, Int4, Uint4) are unpacked to int8 before being returned, so they can more easily be printed or used.

Directories

Path Synopsis
Package bucketing provides strategies for bucketing tensor dimensions to reduce the number of unique compiled graphs when input sizes vary.
Package bucketing provides strategies for bucketing tensor dimensions to reduce the number of unique compiled graphs when input sizes vary.
Package distributed defines a dtensor.Tensor, a logical tensor distributed across multiple devices organized as a distributed.DeviceMesh.
Package distributed defines a dtensor.Tensor, a logical tensor distributed across multiple devices organized as a distributed.DeviceMesh.
Package images provides several functions to transform images back and forth from tensors.
Package images provides several functions to transform images back and forth from tensors.
Package numpy allows one to read/write tensors to Python's NumPy npy and npz file formats.
Package numpy allows one to read/write tensors to Python's NumPy npy and npz file formats.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL