trainer package - github.com/keeandrews/loradex-cli/internal/trainer - Go Packages

trainer

package
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package trainer abstracts local LoRA training backends. v1 ships one adapter (ai-toolkit). The CLI never trains itself — adapters orchestrate a subprocess.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure(home, python string)

Configure sets the ai-toolkit location for the adapter.

func NewRunID

func NewRunID() string

NewRunID returns a sortable, unique run id.

func ResolvePython

func ResolvePython(cfg Config) string

ResolvePython returns the python interpreter (default <home>/venv/bin/python).

Types

type AIToolkit

type AIToolkit struct{}

AIToolkit orchestrates the ai-toolkit trainer as a subprocess.

func (AIToolkit) Detect

func (a AIToolkit) Detect(cfg Config) (Capabilities, error)

Detect verifies ai-toolkit is installed (never installs anything).

func (AIToolkit) Generate added in v0.1.13

func (a AIToolkit) Generate(ctx context.Context, req GenerateRequest, onProgress func(GenerateProgress)) (GenerateResult, error)

Generate renders images from a trained LoRA by driving ai-toolkit's "generate" job (same model loader as training, LoRA fused via model.lora_path). It writes a generate config, runs run.py, relays output, and returns the rendered image paths. Honors ctx cancellation.

func (AIToolkit) GenerateConfig added in v0.1.13

func (a AIToolkit) GenerateConfig(req GenerateRequest) ([]byte, error)

GenerateConfig renders the ai-toolkit generate-job config for a request, without running anything (used by `loradex generate --dry-run`).

func (AIToolkit) Name

func (AIToolkit) Name() string

func (AIToolkit) Plan

func (a AIToolkit) Plan(req Request) (Plan, error)

Plan computes the (side-effect-free) plan.

func (AIToolkit) Train

func (a AIToolkit) Train(ctx context.Context, plan Plan, onProgress func(Progress)) (Result, error)

Train runs ai-toolkit via argv subprocess (no shell), streams progress, and collects + validates the output. Honors ctx cancellation by forwarding SIGINT.

type Capabilities

type Capabilities struct {
	Device     string // mps | cpu | cuda
	DeviceName string
	MemoryGB   int
	Version    string // backend version (best effort)
}

Capabilities describes the resolved training device.

func DetectDevice

func DetectDevice(requested string) Capabilities

DetectDevice resolves the training device (auto → MPS on Apple Silicon).

type Config

type Config struct {
	Home   string
	Python string
}

Config locates a backend install.

type GenerateProgress added in v0.1.13

type GenerateProgress struct {
	Raw    string
	Loaded bool // true once the model has finished loading and rendering begins
}

GenerateProgress is a streamed generation update (raw trainer output line).

type GenerateRequest added in v0.1.13

type GenerateRequest struct {
	Name           string
	Base           string
	BaseCheckpoint string   // name_or_path (the base model dir, as in training)
	LoRAPath       string   // trained .safetensors to fuse
	Prompts        []string // inline prompts (ignored when PromptFile is set)
	PromptFile     string   // path to a newline-delimited prompts file
	Negative       string
	Width          int
	Height         int
	Steps          int     // diffusion steps (sample_steps)
	Guidance       float64 // guidance scale
	Seed           int     // -1 = random per image
	Count          int     // images per prompt (num_repeats)
	Sampler        string  // "" → flowmatch for FLUX, else ddpm
	Precision      string  // dtype, e.g. bf16
	Quantize       bool
	Device         string
	OutputDir      string
}

GenerateRequest is a fully-resolved image-generation request. It loads the same base the LoRA trained on and fuses the LoRA at load.

type GenerateResult added in v0.1.13

type GenerateResult struct {
	Images []string
}

GenerateResult lists the rendered images (absolute paths).

type Metrics

type Metrics struct {
	FinalLoss       float64  `json:"final_loss"`
	StepsCompleted  int      `json:"steps_completed"`
	DurationSeconds int      `json:"duration_seconds"`
	PeakMemoryBytes int64    `json:"peak_memory_bytes"`
	Checkpoints     []string `json:"checkpoints"`
	Samples         []string `json:"samples"`
}

Metrics are the final training metrics.

type PerfPlan added in v0.1.16

type PerfPlan struct {
	CacheLatents        bool // VAE-encode images once to disk, then free the VAE
	CacheTextEmbeddings bool // encode captions once, then unload the text encoder
	DisableSampling     bool // skip sampling, including the pre-train baseline sample
}

PerfPlan summarizes the memory/speed optimizations applied to a training run.

func PerfPlanFor added in v0.1.16

func PerfPlanFor(req Request) PerfPlan

PerfPlanFor derives the optimizations enabled for a request — the single source of truth shared by config generation and the build plan. Latent caching is always safe (no augmentations are configured). Text-embedding caching unloads the TE but freezes the embeds, so it is gated on self-contained captions (no dynamic trigger_word) and not training the TE. Sampling is skipped when the run requested no samples.

type Plan

type Plan struct {
	Req        Request
	Trainer    string
	Device     string
	DeviceName string
	MemoryGB   int
	Steps      int
	ConfigPath string
	OutputPath string // final collected .safetensors path
	Quantize   bool
}

Plan is the displayable, side-effect-free training plan.

type Progress

type Progress struct {
	Step       int
	TotalSteps int
	Loss       float64
	Raw        string
}

Progress is a streamed training update.

type Request

type Request struct {
	Name           string
	Base           string
	BaseCheckpoint string
	Trigger        string
	DatasetDir     string
	CaptionMode    string
	// CaptionsHaveTrigger: the per-image .txt captions already include the
	// trigger (we generated them), so the trainer must not inject it again.
	CaptionsHaveTrigger bool
	Profile             profile.Profile
	Device              string
	CacheDir            string // .loradex/cache/<run-id>
	OutputDir           string // version dir to collect results into
	OutputFile          string // final .safetensors filename
	RawConfig           string // --config escape hatch (path); skips generation
	Samples             int
	RunID               string
}

Request is a fully-resolved training request.

type Result

type Result struct {
	WeightsPath string
	NetworkRank int
	NetworkDim  int
	Metrics     Metrics
	Stopped     bool // ctrl-c → resumable
}

Result is the outcome of a training run.

type Trainer

type Trainer interface {
	Name() string
	Detect(cfg Config) (Capabilities, error)
	Plan(req Request) (Plan, error)
	Train(ctx context.Context, plan Plan, onProgress func(Progress)) (Result, error)
}

Trainer is the backend interface. cmd/build depends only on this.

Jump to

Keyboard shortcuts

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