fusedops

package
v0.1.23 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: Apache-2.0 Imports: 16 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

Fused Operations

Fused operations are operations that are fused together for performance.

Since the Go backend doesn't have a JIT-compiler, for the most common cases we simply implement fused operations, that could yield most of the gain in most cases.

Reminder: often it's not about reducing the number of operations, but rather improve the locality of data. E.g.: instead of looping over a large buffer each time for a different operation, execute many operations and loop over the buffer only once -- this can be orders of magnitude faster if the buffer doesn't fit into the vaious CPU caches.

Adding new fused ops

If your model has a bottleneck, and you think you could improve things by fusing operations, here's a template to implement your own fused op:

  • (Recommended) Measure performance of the computation you are fusing before you start your work, as well as the performance of the model where it is used. See support/backendtest/benchmarks.go for benchmarks of benchmarking individual operations (and their composed counterparts).
  • Add the signature of the new fused op in compute.FusedOps interface, along with a new compute.OpType.
  • Add the new op to the list of ops to generate registration for, in internal/cmd/gobackend_opsregistration.
  • Regenerate code with go generate ./... (enumerations, registrations, notimplemented entries)
  • Create a new file in this directory (internal/gobackend/fusedops) to implement the fused op: follow the pattern from the other files here.
  • Add compliance tests in support/backendtest/: these are important, and helps other backends to reuse your tests.
  • Modify GoMLX pkg/core/graph (or pkg/ml/nn, pkg/ml/layers/... or somewhere else) ML libraries to use the fused op if available (and if not training: if training, you may want to keep the non-fused op, so that you can do the gradient, see how other functions like nn.Dense do it)
  • (Recommende) Measure performance of the computation you are fusing after you start your work, as well as the performance of the model where it is used. Add a comment in the source code about the performance improvements ( include the impact on only the computation, and the "diluted impact" on the model as well).

Sounds like a lot, but it's straight forward -- except the actual implementation of the fused op, which in some cases is not trivial.

To Do's

  • Add FusedActivation: and split it from FusedDense, since it is not fused there.
  • The implicit broadcasting is much slower than first doing an explicit broadcast. You can see in the benchmarks go test ./gobackend -bench=Standard/Softmax. This deserves some attention.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FusedActivation added in v0.1.9

func FusedActivation(f *gobackend.Function, x compute.Value, cfg compute.ActivationConfig) (compute.Value, error)

FusedActivation applies the configured activation function.

func FusedActivationVJP added in v0.1.9

func FusedActivationVJP(f *gobackend.Function, y, x, dOutput compute.Value, cfg compute.ActivationConfig) (compute.Value, error)

FusedActivationVJP computes the vector-jacobian product of the configured activation.

func FusedAttentionQKVProjection

func FusedAttentionQKVProjection(f *gobackend.Function, x, wQKV, biasQ, biasK, biasV compute.Value, queryDim, keyValueDim int) (queryOut, keyOut, valueOut compute.Value, err error)

FusedAttentionQKVProjection performs fused Query-Key-Value projection.

The matmul (x @ wQKV) is delegated to DotGeneral, which selects the optimal execution path by a combination of build and runtime. The fused executor then splits the result into Q/K/V and adds biases.

func FusedLayerNorm

func FusedLayerNorm(f *gobackend.Function, x compute.Value, axes []int, epsilon float64, gamma, beta compute.Value) (compute.Value, error)

FusedLayerNorm applies layer normalization.

func FusedQuantizedDense

func FusedQuantizedDense(
	f *gobackend.Function,
	x, weights, bias compute.Value,
	weightsQuantization *compute.Quantization,
	activation compute.ActivationType) (compute.Value, error)

FusedQuantizedDense performs fused dequantization + matmul + optional bias + optional activation.

Unlike FusedDense, this does not create a DotGeneral sub-node — the quantized matmul is fundamentally different (mixed-dtype with per-group scales). The inputs to the executor are [x, weights, scales, zeroPoints?, bias?] directly.

Weights should have their dtype set to reflect the actual storage type (e.g. Int4, Int8). For sub-byte types, the caller should Bitcast packed byte data to the correct dtype.

func FusedQuantizedDenseGGML

func FusedQuantizedDenseGGML(f *gobackend.Function, x, weights, bias compute.Value,
	wq *compute.Quantization,
	activation compute.ActivationType) (compute.Value, error)

FusedQuantizedDenseGGML handles the GGML path for FusedQuantizedDense. GGML weights are [N, bytesPerRow] Uint8 with native block layout. Scales and zero points are embedded in the blocks.

func FusedScaledDotProductAttention

func FusedScaledDotProductAttention(
	f *gobackend.Function,
	query, key, value compute.Value,
	axesLayout compute.AttentionAxesLayout,
	options *compute.ScaledDotProductAttentionConfig) (output compute.Value, statesForVJP []compute.Value, err error)

FusedScaledDotProductAttention computes multi-head scaled dot-product attention.

output = softmax(query @ key^T * scale + mask) @ value, computed per-head with GQA support.

Inputs:

  • query, key, value: 4D tensors whose axis ordering is determined by axesLayout. For AttentionAxesLayoutBHSD: query [batch, numHeads, seqLen, headDim], key/value [batch, numKVHeads, kvLen, headDim]. For AttentionAxesLayoutBSHD: query [batch, seqLen, numHeads, headDim], key/value [batch, kvLen, numKVHeads, headDim].
  • mask: [seqLen, kvLen] (seqLen is the query sequence length): optional (can be nil) mask that can be either boolean or additive (any dtype other than Bool). See also causal below. Boolean mask: true = attend, false = ignore. Float/additive mask: added to scores before softmax. Must be broadcastable to the score tensor shape.

Parameters:

  • numHeads: number of query attention heads
  • numKVHeads: number of key/value attention heads (for GQA; numHeads must be divisible by numKVHeads)
  • axesLayout: determines the axis ordering of query/key/value tensors
  • scale: scaling factor applied to query @ key^T (typically 1/sqrt(headDim))
  • causal: if true, apply causal (lower-triangular) mask. Callers (e.g. attention.Core) treat causal and mask as mutually exclusive, folding causal into the mask before calling this method when both are needed. Backends may assume they won't both be set.
  • options: optional optimization hints (nil uses defaults). See ScaledDotProductAttentionConfig.

Output: same shape as query.

func FusedSoftmax

func FusedSoftmax(f *gobackend.Function, x compute.Value, axis int) (compute.Value, error)

FusedSoftmax computes softmax along the specified axis. The axis must be non-negative (the caller normalizes negative indices).

func QuantizedEmbeddingLookup

func QuantizedEmbeddingLookup(f *gobackend.Function, data, indices compute.Value,
	wq *compute.Quantization) (compute.Value, error)

QuantizedEmbeddingLookup performs a quantized embedding lookup (row gather) with on-the-fly dequantization. data: [vocabSize, bytesPerRow] Uint8 with native GGML block layout. indices: integer tensor with last dim = 1 (same as Gather convention). Output: [batch..., K] Float32 where K is derived from the block format.

Types

This section is empty.

Directories

Path Synopsis
Package avx2 provides AVX2 SIMD implementations for fused operations.
Package avx2 provides AVX2 SIMD implementations for fused operations.
Package avx512 provides AVX2 SIMD implementations for fused operations.
Package avx512 provides AVX2 SIMD implementations for fused operations.

Jump to

Keyboard shortcuts

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