rope

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 3 Imported by: ? 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.

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

func InvFreq

func InvFreq(theta float64, dim int) ([]float32, error)

InvFreq returns the dim/2 default inverse frequencies 1/θ^(2i/dim) in float32. As in transformers, the exponent is a float32 quotient, the power is rounded to float32, and its reciprocal is rounded again.

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.

func (Scaling) Apply

func (s Scaling) Apply(inv []float32) error

Apply rescales the inverse frequencies inv in place as s says. Type "" and "default" leave inv unchanged and "llama3" applies Smooth. Apply returns an error, leaving inv unchanged, for any other type or for invalid llama3 parameters.

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

func (s Smooth) Scale(inv []float32)

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).

func (Smooth) Validate

func (s Smooth) Validate() error

Validate reports whether s describes a usable scaling: a positive Factor and LowFreqFactor, a HighFreqFactor above LowFreqFactor, and a positive OriginalMaxPositionEmbeddings.

Jump to

Keyboard shortcuts

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