Documentation
¶
Overview ¶
Package rope computes rotary position embedding inverse frequencies the way Hugging Face transformers does.
InvFreq returns the default frequencies θ^(-2i/dim). Scaling.Apply applies a checkpoint's rope_scaling to them; it implements rope_type "llama3" as Smooth. Both evaluate in float32 in the operation order of transformers' _compute_default_rope_parameters and _compute_llama3_parameters, so a table built here matches the inv_freq buffer of a transformers model to within the rounding of torch's powf.
inv, err := rope.InvFreq(500000, 64)
if err != nil {
return err
}
s := rope.Scaling{Type: "llama3", Factor: 32, LowFreqFactor: 1, HighFreqFactor: 4, OriginalMaxPositionEmbeddings: 8192}
if err := s.Apply(inv); err != nil {
return err
}
This package is outside the compatibility promise of github.com/tmc/lib/nvidia. Its API may change or be removed in patch releases.
Example ¶
Example builds the inverse frequencies of Llama-3.2-1B.
package main
import (
"fmt"
"github.com/tmc/lib/nvidia/x/host/shape/rope"
)
func main() {
inv, err := rope.InvFreq(500000, 64)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("default %.6g %.6g %.6g\n", inv[8], inv[16], inv[31])
s := rope.Scaling{Type: "llama3", Factor: 32, LowFreqFactor: 1, HighFreqFactor: 4, OriginalMaxPositionEmbeddings: 8192}
if err := s.Apply(inv); err != nil {
fmt.Println(err)
return
}
fmt.Printf("scaled %.6g %.6g %.6g\n", inv[8], inv[16], inv[31])
}
Output: default 0.037606 0.00141421 3.01386e-06 scaled 0.037606 0.000429557 9.41831e-08
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Scaling ¶
type Scaling struct {
// Type is the rope_type: "" or "default" for none, or "llama3".
Type string
Factor float64
LowFreqFactor float64
HighFreqFactor float64
OriginalMaxPositionEmbeddings int
}
Scaling is the rope_scaling block of a checkpoint config.
type Smooth ¶
type Smooth struct {
Factor float64
LowFreqFactor float64
HighFreqFactor float64
OriginalMaxPositionEmbeddings int
}
Smooth is smooth wavelength scaling, the rope_type transformers calls llama3: wavelengths longer than OriginalMaxPositionEmbeddings/LowFreqFactor are stretched by Factor, those shorter than OriginalMaxPositionEmbeddings/HighFreqFactor are kept, and those between are blended smoothly.
func (Smooth) Scale ¶
Scale rescales the inverse frequencies inv in place. It follows transformers' _compute_llama3_parameters in float32: torch evaluates a scalar divided by a tensor as the tensor's reciprocal times the scalar, and the band edges and the smoothing denominator are float64 quotients rounded to float32. A wavelength equal to a band edge is in the smooth band. Scale assumes s is valid (see Smooth.Validate).