Documentation
¶
Overview ¶
Package attnres implements attention over residual sources on the host and CUDA. Each query scores the RMS-normalized sources for a token. A softmax across sources supplies the weights for mixing those same normalized rows.
This package is outside the compatibility promise of github.com/tmc/lib/nvidia. Its API may change or be removed in patch releases.
Index ¶
Examples ¶
Constants ¶
const Ordered2304Entry = "attnres_ordered_2304"
Ordered2304Entry is the entry point in Ordered2304Source.
const Ordered2304Width = 2304
Ordered2304Width is the width of every row in the ordered CUDA mix.
Variables ¶
This section is empty.
Functions ¶
func Mix ¶
func Mix(sources, queries []float64, sourceCount, tokens, width int, epsilon float64) ([]float64, error)
Mix computes a mixture for each query and token. Sources are contiguous in [sourceCount, tokens, width] order, queries in [queryCount, width] order, and the returned slice in [queryCount, tokens, width] order. There must be at least one source, token, query and column. Every input value must be finite, and epsilon must be finite and positive.
For each token, Mix normalizes each source by sqrt(mean(x²) + epsilon), scores each normalized source by dot(query, source)/sqrt(width), and averages the normalized sources using a max-shifted softmax over scores. Queries are supplied unscaled.
Mix uses float64 arithmetic, accumulating columns and sources in increasing index order with separate multiply and add rounding. It returns an error if a source norm or score overflows. Device reduction trees and low-precision conversions can differ from this host result.
Example ¶
package main
import (
"fmt"
"github.com/tmc/lib/nvidia/x/nn/attnres"
)
func main() {
out, err := attnres.Mix([]float64{1, 0, 0, 1}, []float64{0, 0}, 2, 1, 2, 0.5)
fmt.Println(out, err)
}
Output: [0.5 0.5] <nil>
func Ordered2304Source ¶
func Ordered2304Source() string
Ordered2304Source returns CUDA C++ source for the single-token ordered mix. With a grid of [queryCount, 1, 1] and blocks of [256, 1, 1], the entry takes:
stack, last, addend, queries, norm, fp32_out, bf16_out, num_stack (uint32), epsilon (float32), norm_epsilon (float32)
See Ordered2304 for element types, layouts and arithmetic.
Example ¶
fmt.Println(Ordered2304Entry, len(Ordered2304Source()) > 0)
Output: attnres_ordered_2304 true
Types ¶
type Ordered2304 ¶
type Ordered2304 struct {
Stack, Last, Addend cuda.Slice[uint16]
Queries cuda.Slice[float32]
Norm cuda.Slice[uint16]
FP32 cuda.Slice[float32]
BF16 cuda.Slice[uint16]
Epsilon float32
NormEpsilon float32
}
Ordered2304 contains device operands for the ordered, single-token mix. All rows are contiguous with width Ordered2304Width. The source list contains zero to four BF16 Stack rows followed by one required BF16 Last row. Optional BF16 Addend is added to Last in FP32 before scoring and mixing. Each FP32 Queries row already includes the caller's score scale.
Exactly one output is required, sized like Queries. With FP32 output, Norm is empty. With BF16 output, Norm contains a BF16 RMS scale row per query, and NormEpsilon is used for the output normalization.
The kernel uses FP32 with an ordered 2048+256 column reduction, approximate reciprocal square root, max-shifted exponential weights, a padded tree for the softmax denominator and fused weighted accumulation. BF16 output applies the row RMS scales and rounds once to nearest even.
func (Ordered2304) Launch ¶
Launch enqueues the kernel on stream. Kernel must be the entry from Ordered2304Source. Buffers must belong to stream's device, and the output must not overlap any input. The caller must keep the module and buffers alive through completion and supply finite device values.