Documentation
¶
Overview ¶
Package dot implements a general-purpose "dot product" ("Einsum") computation.
It provides a registration system for the underlying "MatMul" (matrix multiplication) implementations -- to it allows pluggability of different implementations, so one can easily experiment with it. The sub-package `matmul` provides the base implementation (including some SIMD variants)
The actual "MatMul" implementation used is in part selected at build-time (depending on the architecture and tags), at initialization time (based on presence of SIMD features) and during graph-build time, based on the input shapes, layout and dtypes.
Environment variables that can be used to disable certain features:
- GOMLX_GO_DOT_MATMUL: set to false to disable the default matmul implementation. if you haven't added other plugin implementations, it will effectively disable DotGeneral.
- GOMLX_GO_SIMD_AVX512: set to false to disable the AVX512 implementation, even if the runtime architecture allows it.
- GOMLX_GO_SIMD_AVX2: set to false to disable the AVX2 implementation, even if the runtime architecture allows it.
Index ¶
- func CallRegisteredImplementation(backend *gobackend.Backend, implementation *ImplementationRegistration, ...)
- func DotGeneral(f *gobackend.Function, lhsValue compute.Value, ...) (compute.Value, error)
- func LayoutStrings() []string
- func MergeAxes(f *gobackend.Function, lhs *gobackend.Node, ...) (newLhs *gobackend.Node, newLhsContractingAxes, newLhsBatchAxes []int, ...)
- func RegisterImplementation[I, O interface{ ... }](name string, layout Layout, inputDType, outputDType dtypes.DType, ...)
- func ResetTestRegistrations()
- func TransposeToLayout(f *gobackend.Function, lhs *gobackend.Node, ...) (newLhs *gobackend.Node, newLhsContractingAxes, newLhsBatchAxes []int, ...)
- type AxisType
- type DotGeneralExecFn
- type ImplementationKey
- type ImplementationRegistration
- type Layout
- type NodeData
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CallRegisteredImplementation ¶
func CallRegisteredImplementation( backend *gobackend.Backend, implementation *ImplementationRegistration, lhs, rhs, output *gobackend.Buffer, params *NodeData)
CallRegisteredImplementation calls the registered implementation for the given layout and dtypes.
func DotGeneral ¶
func DotGeneral(f *gobackend.Function, lhsValue compute.Value, lhsContractingAxes, lhsBatchAxes []int, rhsValue compute.Value, rhsContractingAxes, rhsBatchAxes []int, config compute.DotGeneralConfig) (compute.Value, error)
DotGeneral takes as input lhs (left-hand-side) and rhs (right-hand-side) specifications for a general vector product -- a generalized "Einsum". Each axis can be:
- Just aligned (batch axes), so the output has the same axes as the inputs. The dimensions must match in lhs and rhs.
- Crossed (default), in which case the output is the combination (concatenation) of the dimensions.
- Contracted (contracting axes), where the output does multiply the values and reduce sum those dimensions.
The resulting shape is [batchIndices..., <lhs cross indices...>, <rhs cross indices...>], the indices come in the order they were provided. The output dtype is by default the same as the input, except if configured otherwise in config.OutputDType.
This is the graph building part of DotGeneral. It reshapes and transposes the inputs as needed to transform them into one of the two layouts the implementation functions (see execDotGeneral) know how to handle.
func LayoutStrings ¶
func LayoutStrings() []string
LayoutStrings returns a slice of all String values of the enum
func MergeAxes ¶
func MergeAxes(f *gobackend.Function, lhs *gobackend.Node, lhsContractingAxes, lhsBatchAxes []int, rhs *gobackend.Node, rhsContractingAxes, rhsBatchAxes []int) ( newLhs *gobackend.Node, newLhsContractingAxes, newLhsBatchAxes []int, newRhs *gobackend.Node, newRhsContractingAxes, newRhsBatchAxes []int, err error)
MergeAxes checks if adjacent axes in lhs and rhs are used for the same purpose (batch, contracting or cross), and if so, merges them by reshaping. This simplifies the shape of the tensors, making it more likely that the operation can be matched to a fast execution path (like SmallMatMul or Packgemm).
It returns the reshaped lhs and rhs, the new contracting and batch axes.
func RegisterImplementation ¶
func RegisterImplementation[I, O interface {
gotype.Numeric | gotype.AnyHalfPrecision
}](
name string,
layout Layout, inputDType, outputDType dtypes.DType,
implFn DotGeneralExecFn[I, O], priority gobackend.RegisterPriority,
forTests bool)
RegisterImplementation registers a DotGeneral algorithm implementation for a specific layout and pair of dtypes.
The name is simply a human-readable identifier for the algorithm, used in logging.
This should only be called during initialization.
func ResetTestRegistrations ¶
func ResetTestRegistrations()
ResetTestRegistrations resets the test registrations.
func TransposeToLayout ¶
func TransposeToLayout(f *gobackend.Function, lhs *gobackend.Node, lhsContractingAxes, lhsBatchAxes []int, rhs *gobackend.Node, rhsContractingAxes, rhsBatchAxes []int, layout Layout) ( newLhs *gobackend.Node, newLhsContractingAxes, newLhsBatchAxes []int, newRhs *gobackend.Node, newRhsContractingAxes, newRhsBatchAxes []int, err error)
TransposeToLayout attempts to transpose the axes of lhs and rhs to match a the given layout: LayoutTransposed or LayoutNonTranposed.
Consider running MergeAxes first, it will make the transposing faster.
Types ¶
type AxisType ¶
type AxisType int
AxisType represents the type of an axis, based on its function in the DotGeneral.
type DotGeneralExecFn ¶
type DotGeneralExecFn[I, O interface {
gotype.Numeric | gotype.AnyHalfPrecision
}] func(
backend *gobackend.Backend, layout Layout,
lhs, rhs []I,
batchSize, lhsCrossSize, rhsCrossSize, contractingSize int,
output []O)
DotGeneralExecFn is the signature of a function that implements a specific ImplementationKey for DotGeneral.
The lhs and rhs inputs must be shaped according to the layout (batchSize, cross, contracting) and will be provided in the specified order.
The output will always be shaped [batchSize, lhsCross, rhsCross], and the required space must have been pre-allocated.
type ImplementationKey ¶
ImplementationKey reprsents a unique key for a DotGeneral algorithm implementation.
func (*ImplementationKey) String ¶
func (key *ImplementationKey) String() string
type ImplementationRegistration ¶
type ImplementationRegistration struct {
// contains filtered or unexported fields
}
func FindRegisteredImplementation ¶
func FindRegisteredImplementation(layout Layout, inputDType, outputDType dtypes.DType) *ImplementationRegistration
FindRegisteredImplementation returns the registered implementation for the given layout and dtypes, or nil if no implementation is registered.
This can be used during the building of the graph, and the result cached for execution.
type Layout ¶
type Layout int
Layout for inputs of DotGeneral.
const ( // LayoutNonTransposed, "MatMul", "Row-Major" or "Normal-Transposed" layout, // with lhs shaped [B, N, K]; rhs shaped [B, K, M] and output shaped [B, N, M]. // B is the optional batch axis; N is the lhs "cross" axis; M is the rhs // "cross" axis; K is the contracting axis. LayoutNonTransposed Layout = iota // LayoutTransposed with lhs shaped [B, N, K]; rhs shaped [B, M, K] and // output shaped [B, N, M]. // B is the optional batch axis; N is the lhs "cross" axis; M is the rhs // "cross" axis; K is the contracting axis. LayoutTransposed // LayoutIncompatible indicates that the layout is incompatible with the // underlying basic DotGeneral algorithms, and will require a Reshape or // Transpose operation before the DotGeneral operation. LayoutIncompatible )
func LayoutForDotGeneral ¶
func LayoutForDotGeneral(lhsShape shapes.Shape, lhsContractingAxes, lhsBatchAxes []int, rhsShape shapes.Shape, rhsContractingAxes, rhsBatchAxes []int) Layout
LayoutForDotGeneral returns the layout for DotGeneral given the shapes of the inputs and the axes to contract and batch over.
func LayoutString ¶
LayoutString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.
type NodeData ¶
type NodeData struct {
InputDType, OutputDType dtypes.DType
Config compute.DotGeneralConfig
Layout Layout
LHSContractingAxes, LHSBatchAxes []int
RHSContractingAxes, RHSBatchAxes []int
BatchSize, LHSCrossSize, RHSCrossSize, ContractingSize int
// contains filtered or unexported fields
}
NodeData associated to a DotGeneral Node: gathered during graph building, it should include all the information needed to execute it.
func (*NodeData) EqualNodeData ¶
func (d *NodeData) EqualNodeData(other gobackend.NodeDataComparable) bool
EqualNodeData implements nodeDataComparable for dotGeneralNodeData.