peft

package
v0.28.21 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 13 Imported by: 1 module, 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 peft implements parameter-efficient fine-tuning layers for GoMLX.

Index

Constants

View Source
const (
	// ParamAdapter is the name of the LoRA adapter to use.
	// If empty (""), PEFT is inactive and base layers are trainable as normal.
	// If set to "off", "disabled", "false", or "none", base layers are frozen without adding any LoRA adapters.
	// Otherwise, it defines the sub-scope name for the LoRA adapter variables (e.g. "lora", "task_a").
	ParamAdapter = "peft_adapter"

	// ParamTargetModules specifies substrings of scope paths to which LoRA adapters should be applied.
	// Can be a []string or a comma/semicolon/space-separated string (e.g. "query,value").
	// Unmatched linear projections have their base weights frozen, but no adapter added.
	// If empty, all linear projections with peft_adapter set will receive adapters.
	ParamTargetModules = "peft_target_modules"

	// ParamRank is the LoRA rank. Zero disables the adapter, which is the default.
	ParamRank = "peft_rank"
	// ParamAlpha is the LoRA scaling numerator. It defaults to ParamRank.
	ParamAlpha = "peft_alpha"
	// ParamDropout is the LoRA dropout rate. It defaults to zero.
	ParamDropout = "peft_dropout"
	// ParamBias controls whether the base projection bias is trainable.
	ParamBias = "peft_bias"
)

Variables

This section is empty.

Functions

func Apply

func Apply(scope *model.Scope, input *Node, weight, bias *model.Variable) *Node

Apply is shorthand for New(scope, input, weight, bias).Done().

func IsAdapterDisabled

func IsAdapterDisabled(adapterName string) bool

IsAdapterDisabled returns true if the adapter name explicitly disables adapter creation (i.e. "off", "disabled", "false", or "none", case-insensitive). Note: An empty adapter name means PEFT is inactive, not disabled/freeze-only.

func MatchesTargetModule

func MatchesTargetModule(scopePath string, targets []string) bool

MatchesTargetModule returns true if targets is empty or if any target is contained within scopePath.

func ParseTargetModules

func ParseTargetModules(val any) []string

ParseTargetModules converts a target modules value (either []string or string) into a normalized string slice.

Types

type BiasMode

type BiasMode uint8

BiasMode controls whether a projection bias is trainable with its LoRA weights.

const (
	BiasNone BiasMode = iota
	BiasAll
	// BiasLoRAOnly is equivalent to BiasAll for a directly constructed PEFT layer.
	BiasLoRAOnly
)

type Linear

type Linear struct {
	// contains filtered or unexported fields
}

Linear is a graph-building LoRA projection. Configure it with scope hyperparameters, then call Done.

func New

func New(scope *model.Scope, input *Node, weight, bias *model.Variable) *Linear

New configures a LoRA projection around an existing dense weight. The base projection remains unchanged when ParamRank is zero.

func (*Linear) Done

func (l *Linear) Done() *Node

Done builds the frozen base projection plus its LoRA update. A and B live in scope/<adapterName>, use the Store RNG, and are included automatically in normal trainable-variable selection.

func (*Linear) WithWeightLayout

func (l *Linear) WithWeightLayout(layout compute.DenseLayout) *Linear

WithWeightLayout configures the layout of the existing base weight.

type NF4

type NF4 struct {
	// contains filtered or unexported fields
}

NF4 is a graph-building QLoRA projection. Configure its LoRA update with the same scope parameters as New, then call Done.

func NewNF4

func NewNF4(scope *model.Scope, input *Node, weight *NF4Weight, bias *model.Variable) *NF4

NewNF4 configures a QLoRA projection around a packed NF4 base weight.

func (*NF4) Done

func (l *NF4) Done() *Node

Done builds the frozen NF4 base projection plus the optional LoRA update.

type NF4Weight

type NF4Weight struct {
	InputFeatures, OutputFeatures int
	BlockSize                     int
	Packed                        []byte
	Scales                        []float32
	ScaleCodes                    []byte
	ScaleScales                   []float32
	ScaleBlockSize                int
}

NF4Weight stores a frozen [input, output] NormalFloat4 matrix. Packed holds two NF4 codes per byte. Scales are per-input, per-output-block absolute-max scales; ScaleCodes/ScaleScales replace Scales when double quantization is on.

func QuantizeNF4

func QuantizeNF4(inputFeatures, outputFeatures, blockSize int, values []float32) (*NF4Weight, error)

QuantizeNF4 packs a [input, output] F32 matrix. output must be even because GoMLX's packed Uint4 graph representation cannot safely drop one nibble.

func QuantizeNF4Double

func QuantizeNF4Double(inputFeatures, outputFeatures, blockSize, scaleBlockSize int, values []float32) (*NF4Weight, error)

QuantizeNF4Double additionally stores primary scales as uint8 values grouped by an F32 second-level scale.

Jump to

Keyboard shortcuts

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