ops

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: Apache-2.0 Imports: 21 Imported by: 0 modules, 0 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 ops has the base implementation for most operations for the Go backend.

It works by registration, so it simply needs to be imported to be made available.

Index

Constants

View Source
const MaxArgMinMaxReductionSize = 0x8000_0000

Variables

View Source
var ConvertDTypePairMap = gobackend.NewDTypePairMap("ConvertDType")
View Source
var TransposeDTypeMap = gobackend.NewDTypeMap("Transpose")

TransposeDTypeMap is used to dispatch Transpose to type-specific implementations. gobackend:dtypemap execTransposeGeneric ints,uints,floats,half,bool

Functions

func Abs

func Abs(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Abs implements the compute.Builder interface.

func Add

func Add(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

Add implements the compute.Builder interface.

func ArgMinMax

func ArgMinMax(f *gobackend.Function,
	operandOp compute.Value,
	axis int,
	outputDType dtypes.DType,
	isMin bool,
) (compute.Value, error)

ArgMinMax calculates the "argmin" or "argmax" across an axis of the given input array x. outputDType defines the output of the argmin/argmax, it doesn't need to be the same as the input. It's a form of reduction on the given axis, and that axis goes away. So the rank of the result is one less than the rank of x. Examples:

ArgMinMax(x={{2, 0, 7}, {-3, 4, 2}}, axis=1, isMin=true) -> {1, 0}  // (it chooses the 0 and the -3)
ArgMinMax(x={{2, 0, 7}, {-3, 4, 2}}, axis=0, isMin=false) -> {0, 1, 0} // (it choose the 2, 4 and 7)

func Atan2

func Atan2(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

Atan2 implements the compute.Builder interface.

func BitCount

func BitCount(f *gobackend.Function, operand compute.Value) (compute.Value, error)

BitCount implements the compute.Builder interface.

func Bitcast

func Bitcast(f *gobackend.Function, operandOp compute.Value, targetDType dtypes.DType) (compute.Value, error)

Bitcast performs an elementwise bitcast operation from a dtype to another dtype.

The Bitcast doesn't "convert", rather it just reinterprets the bits from operand.DType() to the targetDType.

If the element sizes (in bytes/bits) differ, the last dimension is adjusted:

  • Smaller target: a new trailing axis of size (srcBits / dstBits) is appended, so rank is increased by 1.
  • Larger target: the last axis must be equal to (dstBits / srcBits), and the resultign rank is decreased by 1 ("squeezed").

E.g:

Bitcast([1]uint32{0xdeadbeef}, dtypes.UInt16) -> [1][2]uint16{{0xbeef, 0xdead}} // Little-endian encoding.
Bitcast([1][2]uint16{{0xbeef, 0xdead}}, dtypes.UInt32) -> [1]uint32{0xdeadbeef}

func BitwiseAnd

func BitwiseAnd(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

BitwiseAnd implements the compute.Builder interface.

func BitwiseNot

func BitwiseNot(f *gobackend.Function, operand compute.Value) (compute.Value, error)

BitwiseNot implements the compute.Builder interface.

func BitwiseOr

func BitwiseOr(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

BitwiseOr implements the compute.Builder interface.

func BitwiseXor

func BitwiseXor(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

BitwiseXor implements the compute.Builder interface.

func BroadcastInDim

func BroadcastInDim(
	f *gobackend.Function,
	operandValue compute.Value,
	outputShape shapes.Shape,
	broadcastAxes []int,
) (compute.Value, error)

BroadcastInDim broadcasts x to an output with the given shape.

  • outputShape will be the new shape after x is broadcast.
  • broadcastAxes maps x-axes to the corresponding outputShape axes (len(broadcastAxes) == x.Shape.Rank()), the i-th axis of x is mapped to the broadcastAxes[i]-th dimension of the output. broadcastAxes must be also increasing: this operation cannot be used to transpose axes, it will only broadcast and introduce new axes in-between. -

This also requires that the i-th input axis is either 1 or is the same as the output dimension it's broadcasting into. For example, say operand `x = (s32)[2]{1, 2}`; outputShape = `(s32)[2,2]`:

  • Specifying []int{1} as broadcastAxes will generate output {{1, 2}, {1, 2}}
  • On the other hand, specifying []int{0} as broadcastAxes will generate output {{1 , 1}, {2 , 2}}

func Call

func Call(f *gobackend.Function, target compute.Function, inputs ...compute.Value) ([]compute.Value, error)

func Ceil

func Ceil(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Ceil implements the compute.Builder interface.

func Clamp

func Clamp(f *gobackend.Function, minV, x, maxV compute.Value) (compute.Value, error)

Clamp returns the element-wise clamping operation.

The values max and min can either be a scalar or have the same shape as x.

func Clz

func Clz(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Clz implements the compute.Builder interface.

func Concatenate

func Concatenate(f *gobackend.Function, axis int, operandOps ...compute.Value) (compute.Value, error)

Concatenate joins a sequence of tensors along the given axis (it must exist already). All input tensors must have the same shape, except potentially in the concatenation dimension. They must also have the same data type (DType). It returns an error if inputs are invalid (e.g., no inputs, mismatched graphs, shapes, dtypes, or invalid dimension).

func ConvGeneral

func ConvGeneral(f *gobackend.Function, inputOp, kernelOp compute.Value, axes compute.ConvolveAxesConfig,
	strides []int, paddings [][2]int,
	inputDilations, kernelDilations []int,
	channelGroupCount, batchGroupCount int) (compute.Value, error)

ConvGeneral is a generic Convolution operation with support for:

- Arbitrary number of spatial axes. - Arbitrary transposition of axes. - Strides and padding. - Dilations of the input. - Dilations of the kernel, aka. atrous convolution. - Feature grouping (on the input channels). - Batch grouping.

Some details in https://www.tensorflow.org/xla/operation_semantics#convwithgeneralpadding_convolution. There operand and filter are called lhs and rhs. (XLA documentation is unfortunately poor, much is guess-work). Also useful, https://arxiv.org/pdf/1603.07285v1.pdf.

Note: input is aka. operand; kernel is aka. "filters". The input and output "channels" are also known as "features dimensions".

func ConvertDType

func ConvertDType(f *gobackend.Function, operandOp compute.Value, dtype dtypes.DType) (compute.Value, error)

func Cos

func Cos(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Cos implements the compute.Builder interface.

func Div

func Div(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

Div implements the compute.Builder interface.

func DynamicDimensionSize

func DynamicDimensionSize(f *gobackend.Function, operandValue compute.Value, axis int) (compute.Value, error)

DynamicDimensionSize returns the size of the given dimension of x as a scalar Node.

func DynamicReshape added in v0.1.1

func DynamicReshape(f *gobackend.Function, operandValue compute.Value, specs ...compute.DynamicDimensionSpec) (compute.Value, error)

DynamicReshape reshapes the operand to target dimensions specified by specs.

func DynamicShape

func DynamicShape(f *gobackend.Function, operandValue compute.Value) (compute.Value, error)

DynamicShape returns the shape of x as a 1D tensor.

func Equal

func Equal(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

Equal implements the compute.Builder interface.

func Erf

func Erf(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Erf implements the compute.Builder interface.

func Exp

func Exp(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Exp implements the compute.Builder interface.

func Expm1

func Expm1(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Expm1 implements the compute.Builder interface. It returns e(x)-1.

func Floor

func Floor(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Floor implements the compute.Builder interface.

func Gather

func Gather(
	f *gobackend.Function,
	operandValue, startIndicesValue compute.Value,
	indexVectorAxis int,
	offsetOutputAxes, collapsedSliceAxes, startIndexMap, sliceSizes []int,
	indicesAreSorted bool,
) (compute.Value, error)

Gather implements the compute.Builder. It's a complex operation, fully described in the compute.Builder.Gather documentation.

func GreaterOrEqual

func GreaterOrEqual(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

GreaterOrEqual implements the compute.Builder interface.

func GreaterThan

func GreaterThan(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

GreaterThan implements the compute.Builder interface.

func Identity

func Identity(f *gobackend.Function, operandOp compute.Value) (compute.Value, error)

Identity implements the compute.Identity interface. This operation is not de-duplicated: if you issue it twice, it will not reuse the previous instance.

func If

func If(f *gobackend.Function, pred compute.Value, trueBranch, falseBranch compute.Function) ([]compute.Value, error)

func Iota

func Iota(f *gobackend.Function, shape shapes.Shape, iotaAxis int) (compute.Value, error)

Iota creates a constant of the given shape with increasing numbers (starting from 0) on the given axis. So Iota([2,2], 1) returns [[0 1][0 1]], while Iota([2,2], 0) returns [[0 0][1 1]].

func IsFinite

func IsFinite(f *gobackend.Function, operandOp compute.Value) (compute.Value, error)

IsFinite implements the compute.Builder interface.

func IsNaN

IsNaN implements compute.Builder interface. Notice there is no executor because it uses f.NotEqual instead.

func LessOrEqual

func LessOrEqual(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

LessOrEqual implements the compute.Builder interface.

func LessThan

func LessThan(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

LessThan implements the compute.Builder interface.

func Log

func Log(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Log implements the compute.Builder interface.

func Log1p

func Log1p(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Log1p implements the compute.Builder interface.

func LogicalAnd

func LogicalAnd(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

LogicalAnd implements the compute.Builder interface.

func LogicalNot

func LogicalNot(f *gobackend.Function, operand compute.Value) (compute.Value, error)

LogicalNot implements the compute.Builder interface.

func LogicalOr

func LogicalOr(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

LogicalOr implements the compute.Builder interface.

func LogicalXor

func LogicalXor(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

LogicalXor implements the compute.Builder interface.

func Logistic

func Logistic(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Logistic implements the compute.Builder interface. Aka as sigmoid. It returns 1/(1+exp(-x)).

func Max

func Max(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

Max implements the compute.Builder interface.

func Min

func Min(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

Min implements the compute.Builder interface.

func Mul

func Mul(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

Mul implements the compute.Builder interface.

func Neg

func Neg(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Neg implements the compute.Builder interface.

func NotEqual

func NotEqual(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

NotEqual implements the compute.Builder interface.

func OptimizationBarrier

func OptimizationBarrier(f *gobackend.Function, operands ...compute.Value) ([]compute.Value, error)

OptimizationBarrier implements the compute.OptimizationBarrier interface. This operation is not de-duplicated: if you issue it twice, it will not reuse the previous instance.

func Pad

func Pad(f *gobackend.Function, operandOp, fillValueOp compute.Value, axesConfig ...compute.PadAxis) (compute.Value, error)

Pad implements the compute.Builder interface.

func Pow

func Pow(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

Pow implements the compute.Builder interface.

func RNGBitGenerator

func RNGBitGenerator(f *gobackend.Function, stateOp compute.Value, shape shapes.Shape) (newState, values compute.Value, err error)

RNGBitGenerator generates the given shape filled with random bits. It takes as input the current random number generator (RNG) state, see RNGState or RNGStateFromSeed. The algorithm is hard-coded to use Philox algorithm for now.

It returns the new state of the RNG and the generated values (with random bits) with the given shape.

func ReduceBitwiseAnd

func ReduceBitwiseAnd(f *gobackend.Function, operand compute.Value, axis ...int) (compute.Value, error)

ReduceBitwiseAnd reduces the operand by taking the bitwise AND of values along the given axes.

func ReduceBitwiseOr

func ReduceBitwiseOr(f *gobackend.Function, operand compute.Value, axis ...int) (compute.Value, error)

ReduceBitwiseOr reduces the operand by taking the bitwise OR of values along the given axes.

func ReduceBitwiseXor

func ReduceBitwiseXor(f *gobackend.Function, operand compute.Value, axis ...int) (compute.Value, error)

ReduceBitwiseXor reduces the operand by taking the bitwise XOR of values along the given axes.

func ReduceLogicalAnd

func ReduceLogicalAnd(f *gobackend.Function, operand compute.Value, axis ...int) (compute.Value, error)

ReduceLogicalAnd reduces the operand by taking the logical AND of values along the given axes.

func ReduceLogicalOr

func ReduceLogicalOr(f *gobackend.Function, operand compute.Value, axis ...int) (compute.Value, error)

ReduceLogicalOr reduces the operand by taking the logical OR of values along the given axes.

func ReduceLogicalXor

func ReduceLogicalXor(f *gobackend.Function, operand compute.Value, axis ...int) (compute.Value, error)

ReduceLogicalXor reduces the operand by taking the logical XOR of values along the given axes.

func ReduceMax

func ReduceMax(f *gobackend.Function, operand compute.Value, axis ...int) (compute.Value, error)

ReduceMax reduces the operand by taking the maximum value along the given axes.

func ReduceMin

func ReduceMin(f *gobackend.Function, operand compute.Value, axis ...int) (compute.Value, error)

ReduceMin reduces the operand by taking the minimum value along the given axes.

func ReduceProduct

func ReduceProduct(f *gobackend.Function, operand compute.Value, axis ...int) (compute.Value, error)

ReduceProduct reduces the operand by taking the product of values along the given axes.

func ReduceSum

func ReduceSum(f *gobackend.Function, operand compute.Value, axis ...int) (compute.Value, error)

ReduceSum reduces the operand by taking the sum of values along the given axes.

func ReduceWindow

func ReduceWindow(f *gobackend.Function,
	operandOp compute.Value,
	reductionType compute.ReduceOpType,
	windowDimensions, strides, inputDilations, windowDilations []int,
	paddings [][2]int,
) (compute.Value, error)

ReduceWindow runs a reduction function of the type given by reductionType, it can be either ReduceMaxNode, ReduceSumNode, or ReduceMultiplyNode.

  • reductionType: the type of reduction to perform. E.g.: [ReduceOpMax], [ReduceOpSum],...
  • windowDimensions: the dimensions of the window, must be defined for each axis.
  • strides: stride over elements in each axis for each window reduction. If nil, it's assume to be the same as the windowDimensions -- that is, the strides jump a window at a time.
  • inputDilations: "virtually" expand the input by introducing "holes" between elements. I.e. if inputDilations are 2, then the input is expanded by inserting `2-1` copies of `0` (or whatever is the reduciton "zero" value) between the elements in each dimension. If nil, it's assumed to be 1 (no dilation) for each axis. Values must be >= 1.
  • windowDilations: "virtually" expand the window by inserting `2-1` copies of `0` between the elements in each dimension. If nil, it's assumed to be 1 (no dilation) for each axis. Values must be >= 1.
  • paddings: virtual padding to be added to the input at the edges (start and end) of each axis. If nil, it's assumed to be 0 for each axis.

func Rem

func Rem(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

Rem implements the compute.Builder interface.

func Reshape

func Reshape(f *gobackend.Function, operandValue compute.Value, dims ...int) (compute.Value, error)

Reshape reshapes the operand to the given dimensions.

func Reverse

func Reverse(f *gobackend.Function, operandValue compute.Value, axes ...int) (compute.Value, error)

Reverse returns x with the values for the given dimensions reversed, that is, the value indexed at `i` will be swapped with the value at indexed `(dimension_size - 1 - i)`. The shape remains the same.

func Round

func Round(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Round implements the compute.Builder interface.

func Rsqrt

func Rsqrt(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Rsqrt implements the compute.Builder interface.

func ScatterMax

func ScatterMax(f *gobackend.Function,
	operandOp, scatterIndicesOp, updatesOp compute.Value,
	indexVectorAxis int,
	updateWindowAxes, insertedWindowAxes, scatterAxesToOperandAxes []int,
	indicesAreSorted, uniqueIndices bool,
) (compute.Value, error)

func ScatterMin

func ScatterMin(f *gobackend.Function,
	operandOp, scatterIndicesOp, updatesOp compute.Value,
	indexVectorAxis int,
	updateWindowAxes, insertedWindowAxes, scatterAxesToOperandAxes []int,
	indicesAreSorted, uniqueIndices bool,
) (compute.Value, error)

func ScatterSum

func ScatterSum(f *gobackend.Function,
	operandOp, scatterIndicesOp, updatesOp compute.Value,
	indexVectorAxis int,
	updateWindowAxes, insertedWindowAxes, scatterAxesToOperandAxes []int,
	indicesAreSorted, uniqueIndices bool,
) (compute.Value, error)

func SchedulingBarrier

func SchedulingBarrier(f *gobackend.Function, operand compute.Value, dependencies ...compute.Value) (compute.Value, error)

SchedulingBarrier implements the compute.SchedulingBarrier interface. This operation is not de-duplicated: if you issue it twice, it will not reuse the previous instance.

func ShiftLeft

func ShiftLeft(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

ShiftLeft implements the compute.Builder interface.

func ShiftRightArithmetic

func ShiftRightArithmetic(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

ShiftRightArithmetic implements the compute.Builder interface.

func ShiftRightLogical

func ShiftRightLogical(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

ShiftRightLogical implements the compute.Builder interface.

func Sign

func Sign(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Sign implements the compute.Builder interface.

func Sin

func Sin(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Sin implements the compute.Builder interface.

func Slice

func Slice(f *gobackend.Function, operandOp compute.Value, starts, limits, strides []int) (compute.Value, error)

Slice extracts a subarray from the input array.

The subarray is of the same rank as the input and contains the values inside a bounding box within the input array where the dimensions and indices of the bounding box are given as arguments to the slice operation.

The strides set the input stride of the slice in each axis and must be >= 1. It is optional, and if missing, it is assumed to be 1 for every dimension.

The limits are defined on the x axes, and they are exclusive upper bounds, i.e. the slice includes elements from starts up to (but not including) limits.

Examples:

Slice(x={0, 1, 2, 3, 4}, starts={2}, limits={4}, strides=nil) -> {2, 3}
Slice(x={0, 1, 2, 3, 4}, starts={2}, limits={5}, strides={2}) -> {2, 4}

func Sort

func Sort(
	f *gobackend.Function,
	comparator compute.Function,
	axis int,
	isStable bool,
	inputs ...compute.Value,
) ([]compute.Value, error)

Sort sorts one or more tensors along the specified axis using a comparator closure.

The comparator closure takes 2*N scalar parameters (lhs_0, rhs_0, lhs_1, rhs_1, ...) where N is the number of input tensors, and returns a scalar boolean indicating whether lhs should come before rhs.

func Sqrt

func Sqrt(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Sqrt implements the compute.Builder interface.

func Sub

func Sub(f *gobackend.Function, lhsOp, rhsOp compute.Value) (compute.Value, error)

Sub implements the compute.Builder interface.

func Tanh

func Tanh(f *gobackend.Function, operand compute.Value) (compute.Value, error)

Tanh implements the compute.Builder interface.

func Transpose

func Transpose(f *gobackend.Function, operandValue compute.Value, permutations ...int) (compute.Value, error)

Transpose axes of x. There must be one value in permutations for each axis in the operand. The output will have: output.Shape.Dimension[ii] = operand.Shape.Dimension[permutations[i]].

func Where

func Where(f *gobackend.Function, conditionOp, onTrueOp, onFalseOp compute.Value) (compute.Value, error)

Where implements the compute.Builder interface.

func While

func While(f *gobackend.Function, cond, body compute.Function, initialState ...compute.Value) ([]compute.Value, error)

While executes a loop while a condition is true.

The condition closure takes the current state values and returns a scalar boolean. The body closure takes the current state values and returns new state values. Both must have the same number of parameters matching the initialState count.

Types

type ConvertFnType

type ConvertFnType = func(operand, output *gobackend.Buffer)

type GenericReduceFn

type GenericReduceFn = func(operand, output *gobackend.Buffer, it *ReduceOutputIterator, dtype dtypes.DType) error

GenericReduceFn is the type for the reduction functions.

type ReduceOutputIterator

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

ReduceOutputIterator is a dynamic iterator that yields output flat indices for the corresponding flat index on the input operand, assuming the operand flat index is moving incrementally.

func NewReduceOutputIterator

func NewReduceOutputIterator(dimensions []int, reduceAxes []int) *ReduceOutputIterator

NewReduceOutputIterator creates a dynamic iterator that yields output flat indices for the corresponding flat index on the input operand, assuming the operand flat index is moving incrementally.

func (*ReduceOutputIterator) Next

func (it *ReduceOutputIterator) Next() int

Next returns the next flat index in the output.

type TransposeIterator

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

TransposeIterator creates a dynamic iterator that yields output flat indices for the corresponding flat index on the input operand, assuming the operand flat index is moving incrementally.

func NewTransposeIterator

func NewTransposeIterator(operand shapes.Shape, permutations []int) *TransposeIterator

NewTransposeIterator creates a dynamic iterator that yields output flat indices for the corresponding flat index on the input operand, assuming the operand flat index is moving incrementally.

func (*TransposeIterator) Next

func (it *TransposeIterator) Next() int

Next returns the next flat index in the output.

Jump to

Keyboard shortcuts

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