Skip to content

index ​

Types ​

TrainedLora ​

What a finished run produced.

ts
/** What a finished run produced. */
export type TrainedLora = {
  path: string;
  steps: number;
  images: number;
  minutes: number;
  samples: string[]
}

(source)

LoraInfo ​

What an adapter's file says about how it was trained.

ts
/** What an adapter's file says about how it was trained. */
export type LoraInfo = {
  base: string;
  trigger: string;
  rank: number;
  steps: number;
  sizeBytes: number
}

(source)

Effects ​

lora::train ​

ts
@alwaysUnder(outDir, imagesDir)
effect lora::train {
  imagesDir: string;
  base: string;
  outDir: string;
  outFilename: string;
  steps: number;
  estimatedMinutes: number
}

(source)

lora::info ​

ts
@alwaysUnder(dir)
effect lora::info {
  dir: string;
  filename: string
}

(source)

Constants ​

STYLE_TAGS ​

The booru tags that describe a drawing's style rather than its subject. A caption that names the style teaches the model that the style belongs to those words and not to the trigger word, so a caption loop drops them before writing a sidecar. A caller training a character rather than a style keeps them.

ts
/** The booru tags that describe a drawing's style rather than its subject.
A caption that names the style teaches the model that the style belongs
to those words and not to the trigger word, so a caption loop drops them
before writing a sidecar. A caller training a character rather than a
style keeps them. */
export static const STYLE_TAGS = [
  "monochrome",
  "greyscale",
  "white background",
  "simple background",
  "traditional media",
  "sketch",
  "lineart",
  "signature",
  "text focus",
  "no humans",
]

(source)

Functions ​

trainLora ​

ts
trainLora(
  imagesDir: string,
  trigger: string,
  base: string,
  outPath: string,
  steps: number = 1000,
  rank: number = 16,
  learningRate: number = 0.0001,
  resolution: number = 1024,
  flip: boolean = false,
  seed: number = 1,
  samplePrompts: string[] = [],
  sampleEvery: number = 250,
): Result<TrainedLora> raises <lora::train>

Train a LoRA adapter for an SDXL image model from a folder of images, each with an optional caption file (a.txt beside a.png, comma-separated tags). The trigger word is put in front of every caption. Takes minutes on a Mac GPU and writes the adapter as a .safetensors file, plus a before-and-after sample grid at every sampleEvery steps beside it. Everything runs on this machine, with the Python agency local serve uses. Returns the adapter's path and the sample grids.

Parameters:

NameTypeDefaultDescription
imagesDirstringThe folder of training images, PNG or JPEG, with optional .txt captions beside them
triggerstringThe word the adapter answers to. A real phrase learns faster and keeps the base model's idea of it; a nonsense word owns the token
basestringThe SDXL model to train on: a catalog name, a diffusers: URI, or a model directory, already downloaded
outPathstringThe .safetensors file to write. It must not exist yet
stepsnumber1000How long to train. Too few and the style is faint; too many and every output is a training image. Judge by the sample grids
ranknumber16How much the adapter can hold: 8 for a style, 16 for a character, 32 for a character with a wardrobe. Doubling the rank doubles the file
learningRatenumber0.0001Halve it if the grids get worse after getting better
resolutionnumber1024The training size. 768 trains twice as fast for a first look; 1024 for the real run
flipbooleanfalseAlso train on mirrored copies, which doubles a small set. Off for an asymmetric character
seednumber1Fixes the randomness, so a run is repeatable
samplePromptsstring[][]Prompts to render before and after at each checkpoint, to judge the run. Use the trigger word in them
sampleEverynumber250Steps between sample grids. 0 for none

Returns: Result<TrainedLora>

Throws: lora::train

(source)

loraInfo ​

ts
loraInfo(path: string): Result<LoraInfo> raises <lora::info>

What an adapter file says about itself: the base model it was trained for, its trigger word, its rank, the steps it trained, and its size. Reads the file's header only.

Parameters:

NameTypeDefaultDescription
pathstringA .safetensors adapter written by trainLora

Returns: Result<LoraInfo>

Throws: lora::info

(source)