kernels

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package kernels implement Go kernels for GX.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Allocator

func Allocator() platform.Allocator

Allocator returns an allocator allocating memory using Go.

Types

type Array

type Array interface {

	// Factory returns the kernels available for the value.
	Factory() Factory

	// Shape returns the shape of the value.
	Shape() *shape.Shape

	// Buffer returns the data of the array as a generic []uint8 buffer.
	Buffer() []byte

	// ToAtom returns the atomic value contained in the array.
	// It returns an error if the value is not atomic, that is if the array
	// contains more than one value.
	ToAtom() (any, error)

	// ToFloatNumber returns the atomic value as a float.
	// It returns an error if the value is not atomic, that is if the array
	// contains more than one value.
	ToFloatNumber() (*big.Float, error)

	// String representation of the array.
	String() string
	// contains filtered or unexported methods
}

Array is a value (tuple, atomic, or array) managed by the backend.

func NewArrayFromRaw

func NewArrayFromRaw(data []byte, sh *shape.Shape) (Array, error)

NewArrayFromRaw returns a new array from raw data.

func ToAlgebraicArray

func ToAlgebraicArray[T dtype.Float | dtype.IntegerType](values []T, dims []int) Array

ToAlgebraicArray converts values and a shape into a native multi-dimensional array owned by a backend.

func ToBfloat16Array

func ToBfloat16Array(values []dtype.Bfloat16T, dims []int) Array

ToBfloat16Array converts values and a shape into a native multi-dimensional array owned by a backend.

func ToBfloat16Atom

func ToBfloat16Atom(val dtype.Bfloat16T) Array

ToBfloat16Atom converts a value into an atom owned by a backend.

func ToBoolArray

func ToBoolArray(values []bool, dims []int) Array

ToBoolArray converts values and a shape into a native multi-dimensional array owned by a backend.

func ToBoolAtom

func ToBoolAtom(val bool) Array

ToBoolAtom converts a value into an atom owned by a backend.

func ToFloatArray

func ToFloatArray[T dtype.Float](values []T, dims []int) Array

ToFloatArray converts values and a shape into a native multi-dimensional array owned by a backend.

func ToFloatAtom

func ToFloatAtom[T dtype.Float](val T) Array

ToFloatAtom converts a value into an atom owned by a backend.

func ToIntegerArray

func ToIntegerArray[T dtype.IntegerType](values []T, dims []int) Array

ToIntegerArray converts values and a shape into a native multi-dimensional array owned by a backend.

func ToIntegerAtom

func ToIntegerAtom[T dtype.IntegerType](val T) Array

ToIntegerAtom converts a value into an atom owned by a backend.

func Zero

func Zero(sh *shape.Shape) (Array, error)

Zero returns an array of zeros given a shape.

type Binary

type Binary func(Array, Array) (Array, error)

Binary like +, -, *, /.

type Buffer

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

Buffer managed by Go.

func NewBuffer

func NewBuffer(a Array) *Buffer

NewBuffer returns a Go backend array as a generic buffer to use for GX values.

func ToBuffer

func ToBuffer[T dtype.GoDataType](vals []T, sh *shape.Shape) *Buffer

ToBuffer converts Go values into a GX array.

func (*Buffer) Acquire

func (buf *Buffer) Acquire() []byte

Acquire locks the buffer and returns it. The buffer can be read or written by the caller. All other access is locked. Returns nil if the handle has been freed.

func (*Buffer) Free

func (buf *Buffer) Free()

Free the memory occupied by the buffer. The handle is invalid after calling this function.

func (*Buffer) KernelValue

func (buf *Buffer) KernelValue() Array

KernelValue returns the underlying Go backend array storing the data for the buffer.

func (*Buffer) Release

func (buf *Buffer) Release()

Release the buffer. The caller of that function should not read or write data from the buffer.

func (*Buffer) Shape

func (buf *Buffer) Shape() *shape.Shape

Shape of the underlying array.

func (*Buffer) ToDevice

func (buf *Buffer) ToDevice(dev platform.Device) (platform.DeviceHandle, error)

ToDevice transfers the handle to a device.

func (*Buffer) ToHost

func (buf *Buffer) ToHost(target platform.HostBuffer) error

ToHost fetches the data from the handle and write it to buffer.

type Factory

type Factory interface {
	Concat(dtype.DataType, int) (NAry, *shape.Shape, error)

	Cast(target dtype.DataType, dims []int) (Unary, *shape.Shape, Factory, error)

	Slice(*shape.Shape, int) (Unary, *shape.Shape, error)

	Reshape(*shape.Shape, []int) (Unary, *shape.Shape, error)

	BroadcastInDim(*shape.Shape, []int) (Unary, *shape.Shape, error)

	UnaryOp(token.Token, *shape.Shape) (Unary, *shape.Shape, error)

	BinaryOp(token.Token, *shape.Shape, *shape.Shape) (Binary, *shape.Shape, error)

	Math() MathFactory
}

Factory creates kernels for arrays for all supported types.

func FactoryFor

func FactoryFor(dt dtype.DataType) (Factory, error)

FactoryFor returns a factory given a data type.

type Handle

type Handle interface {
	KernelValue() Array
}

Handle is a value owned by the Go backend.

type MathFactory

type MathFactory interface {
	// Kernelize turns a unary Go math function into a unary kernel.
	Kernelize(func(float64) float64) Unary
}

MathFactory returns a factory to implement functions from the math package.

type NAry

type NAry func([]Array) (Array, error)

NAry operator like Concat.

type Unary

type Unary func(Array) (Array, error)

Unary like - or reshape.

Jump to

Keyboard shortcuts

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