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 AVX512-specific implementations, even if the runtime architecture allows it.
- GOMLX_GO_SIMD_AVX2: set to false to disable AVX2-specific implementations, 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 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
- func (d *NodeData) BindConstantData(inputIdx int, cd *gobackend.ConstantData)
- func (d *NodeData) CacheSignature(inputIdx int) string
- func (d *NodeData) EqualNodeData(other gobackend.NodeDataComparable) bool
- func (d *NodeData) IsInputCached(inputIdx int) bool
- func (d *NodeData) Recompute(backend *gobackend.Backend, resolvedNodes []*gobackend.Node, ...) (any, error)
- func (d *NodeData) SetSizes(lhsShape, rhsShape shapes.Shape) error
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 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 transposes the axes of lhs and rhs so that their semantic groups (batch, cross, and contracting axes) form contiguous blocks matching the target layout:
- LHS: [Batch..., Cross..., Contracting...] (LayoutTransposed)
- RHS: [Batch..., Cross..., Contracting...] (LayoutTransposed) or [Batch..., Contracting..., Cross...] (LayoutNonTransposed)
Memory Layout Invariant: In row-major format, any sequence of contiguous dimensions of the same semantic category (e.g. B0, B1 or N0, N1, N2 or K0, K1) has flat memory offsets that are identical to a single collapsed dimension with size equal to their product. The GEMM implementation operates directly on the flat data slices using the total combined sizes (batchSize, crossSize, contractingSize). Therefore, no physical or logical MergeAxes reshape is needed; keeping the original (unmerged) dimensions avoids reshape overhead and seamlessly supports dynamic dimensions.
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,
nodeData *NodeData)
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.
func (*ImplementationRegistration) ImplFn ¶ added in v0.1.9
func (r *ImplementationRegistration) ImplFn() any
ImplFn returns the registered implementation function.
func (*ImplementationRegistration) Name ¶ added in v0.1.9
func (r *ImplementationRegistration) Name() string
Name returns the name of the registered implementation.
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.
Memory Layout Expectations: The underlying GEMM implementations (CallRegisteredImplementation) operate on flat 1D memory buffers. In row-major memory order, adjacent dimensions of the same semantic type (batch, cross, or contracting) have linear strides identical to their scalar product. Therefore, tensors with any rank can be executed directly by the GEMM kernels as long as:
- All batch axes are leading and sequential: [0, 1, ..., numBatchAxes-1] on both LHS and RHS.
- Contracting axes match in count and are contiguous in memory.
- For LHS: contracting axes are trailing: [B..., N..., K...].
- For RHS LayoutTransposed: contracting axes are trailing: [B..., M..., K...].
- For RHS LayoutNonTransposed: contracting axes immediately follow batch axes: [B..., K..., M...].
No reshape or axis merging is required when these conditions are met.
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
// LHSConstantData and RHSConstantData store the ConstantData (and its opaque cachedForm of packed panels)
// for LHS and RHS matrices when they are constant. They are non-nil if and only if
// the corresponding matrix input is constant and eligible for panel caching.
LHSConstantData *gobackend.ConstantData
RHSConstantData *gobackend.ConstantData
// 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) BindConstantData ¶ added in v0.1.20
func (d *NodeData) BindConstantData(inputIdx int, cd *gobackend.ConstantData)
BindConstantData implements gobackend.ConstantCacheNodeData.
func (*NodeData) CacheSignature ¶ added in v0.1.20
CacheSignature implements gobackend.ConstantCacheNodeData.
func (*NodeData) EqualNodeData ¶
func (d *NodeData) EqualNodeData(other gobackend.NodeDataComparable) bool
EqualNodeData implements nodeDataComparable for dotGeneralNodeData.
func (*NodeData) IsInputCached ¶ added in v0.1.20
IsInputCached implements gobackend.ConstantCacheNodeData.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
package matmul provides the base implementations of matrx multiply for DotGeneral.
|
package matmul provides the base implementations of matrx multiply for DotGeneral. |
|
avx2
Package avx2 provides AVX2 accelerated matrix multiplication kernels.
|
Package avx2 provides AVX2 accelerated matrix multiplication kernels. |
|
avx512
Package avx512 provides AVX-512 accelerated matrix multiplication kernels.
|
Package avx512 provides AVX-512 accelerated matrix multiplication kernels. |
|
matmultest
Package matmultest provides test helpers for verifying matrix multiplication packing and output routines.
|
Package matmultest provides test helpers for verifying matrix multiplication packing and output routines. |
|
neon
Package neon implements ARM64 NEON accelerated matrix multiplication (DotGeneral) kernels for the Go backend.
|
Package neon implements ARM64 NEON accelerated matrix multiplication (DotGeneral) kernels for the Go backend. |