packgemm

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: Apache-2.0 Imports: 7 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.

README

This package implements the GEMM (General Matrix Multiplication) used by the simplego backend.

EXPERIMENTAL: this is current a straw-man implementation, later we want to rewrite it using Go-highway.

Performance before for Float32 (Using AVX512):

Test Name LHS Dims RHS Dims DType BatchSize Time/Run Num Ops GOps/Sec
NoBatch-Medium {128, 128} {128, 256} Float32 1 119.59µs 8,388,608 70.1
NoBatch-Large {1536, 1920} {1920, 1024} Float32 1 17.49ms 6,039,797,760 345.4
Batched-Large {16, 1536, 1920} {16, 1920, 1024} Float32 16 236.51ms 96,636,764,160 408.6

Performance after for Float32 (Using AVX512):

Test Name LHS Dims RHS Dims DType BatchSize Time/Run Num Ops GOps/Sec
NoBatch-Medium {128, 128} {128, 256} Float32 1 8.50µs 8,388,608 986.4
NoBatch-Large {1536, 1920} {1920, 1024} Float32 1 3.50ms 6,039,797,760 1726.4
Batched-Large {16, 1536, 1920} {16, 1920, 1024} Float32 16 58.90ms 96,636,764,160 1640.8

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DTypeToGEMM is a map of DType to GEMM function.
	// Used for registration, use the generic GEMM[TInput, TOutput] to actually call it.
	DTypeToGEMM = make(map[DTypePair][]GEMMRegistration, 100)
)
View Source
var (
	// NoSIMD32Params are generic assumptions for L1/L2/L3 cache sizes for 32 bits dtypes (float32, int32, uint32)
	//
	// These values are somewhat arbitrary, assuming "standard" modern cache sizes.
	// They are parameterized so they can be tuned or determined dynamically later.
	NoSIMD32Params = CacheParams{

		LHSL1KernelRows: 2,
		RHSL1KernelCols: 4,

		PanelContractingSize: 512,
		LHSPanelCrossSize:    2,
		RHSPanelCrossSize:    512,
	}
)

Functions

func ForceVariant

func ForceVariant(v Variant)

ForceVariant forces the use of the small/large variant. Used for testing only.

func GEMM

func GEMM[TInput, TOutput dtypes.Supported](alpha, beta TOutput, lhsFlat, rhsFlat []TInput, batchSize,
	lhsCrossSize, rhsCrossSize, contractingSize int, outputFlat []TOutput,
	bufAllocFn BufAllocFn[TInput], bufReleaseFn BufReleaseFn, pool *workerspool.Pool) error

GEMM[TInput, TOutput dtypes.DType] implements the matrix multiplication for the given dtypes. It returns an error if a GEMM function is not registered for the given dtypes.

func GEMMDynamic

func GEMMDynamic(inputDType, outputDType dtypes.DType,
	alpha, beta float64, lhsFlat, rhsFlat any, batchSize,
	lhsCrossSize, rhsCrossSize, contractingSize int, outputFlat any,
	bufAllocAnyFn BufAllocAnyFn, bufReleaseFn BufReleaseFn, pool *workerspool.Pool) error

GEMMDynamic dispatches the GEMM function for the given dtypes. It is a dynamic switch around GEMM[TInput, TOutput].

The lhsFlat, rhsFlat and outputFlat parameters must be slices of the corresponding DType. The buffAllocAnyFn must yield a slice of the configured input DType, but cast as "any".

func HasDTypeSupport

func HasDTypeSupport(input, output dtypes.DType) bool

HasDTypeSupport returns true if a GEMM function is registered for the given dtypes.

func RegisterGEMM

func RegisterGEMM[TInput, TOutput dtypes.Supported](
	name string,
	gemmFn func(alpha, beta TOutput, lhsFlat, rhsFlat []TInput, batchSize,
		lhsCrossSize, rhsCrossSize, contractingSize int, outputFlat []TOutput,
		bufAllocFn BufAllocFn[TInput], bufReleaseFn BufReleaseFn, pool *workerspool.Pool) error,
	params *CacheParams,
	priority Priority)

RegisterGEMM registers a GEMM function for the given dtypes with the given priority. If the priority is lower than the one already registered, it does nothing.

Types

type BufAllocAnyFn

type BufAllocAnyFn func(size int) (ref any, data any)

BufAllocAnyFn is a function that allocates a buffer (a slice) of some pre-agreed type.

type BufAllocFn

type BufAllocFn[T any] func(size int) (ref any, data []T)

BufAllocFn is a function that allocates a buffer (a slice) of type T, of the given size.

type BufReleaseFn

type BufReleaseFn func(ref any)

type CacheParams

type CacheParams struct {
	LHSL1KernelRows int // or Mr: number of lhs kernel rows going to registers.
	RHSL1KernelCols int // or Nr: Register Block Width

	PanelContractingSize int // Kc: LHS cols or RHS rows to fit in L2/L3
	LHSPanelCrossSize    int // Mc: L2 rows
	RHSPanelCrossSize    int // Nc: L3 cols
}

Block/packs parameters for current architecture.

type DTypePair

type DTypePair struct {
	Input, Output dtypes.DType
}

DTypePair represents the input/output types.

func GetDTypePair

func GetDTypePair[TInput, TOutput dtypes.Supported]() DTypePair

GetDTypePair returns the DTypePair for the given types.

type GEMMRegistration

type GEMMRegistration struct {
	Name      string
	DTypePair DTypePair
	GEMMFn    any // Typed GEMM function
	Priority  Priority
	Params    *CacheParams
}

GEMMRegistration is a registration of a GEMM function for the given dtype pair.

type Priority

type Priority int

Priority is used to determine the priority of a gemm version, when setting the DTypeToGEMM map.

const (
	PriorityBase      Priority = 0
	PriorityDType     Priority = 10 // Version for a specific dtype (instead of generic).
	PrioritySIMD      Priority = 20 // Version specialized for a SIMD architecture.
	PriorityDTypeSIMD Priority = 30 // Version specialized for a dtype and SIMD architecture.
)

type Variant

type Variant int

Variant of algorithms: usually just one for small matrices and the other for large matrices.

const (
	VariantNone Variant = iota
	VariantSmall
	VariantLarge
)

Jump to

Keyboard shortcuts

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