Skip to content

feat(adapters): read PEFT LoRA adapters — harness-hub layout, VLM-style keys, rsLoRA, named refusals (#675 P0) - #730

Merged
goodmai merged 1 commit into
mainfrom
feat/675-peft-adapters
Nov 23, 2026
Merged

goodmai merged 1 commit into
mainfrom
feat/675-peft-adapters

Conversation

@goodmai

@goodmai goodmai commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Summary

#675 P0: basalt reads PEFT LoRA adapters, the format of the harness-hub checkpoints (adapter_config.json + adapter_model.safetensors).

PEFTLoRAReader (Sources/BasaltInference/Adapters/PEFTLoRAReader.swift):

  • Detection. A directory whose adapter_config.json has peft_type takes this path. Anything else keeps the mlx-lm path.
  • Scale. lora_alpha / r, or lora_alpha / √r with use_rslora.
  • Refusals, with the field named:
    • peft_type other than LORA;
    • use_dora;
    • non-empty modules_to_save;
    • non-empty rank_pattern or alpha_pattern;
    • a key that is neither lora_A nor lora_B;
    • a key with no linear layer in the model;
    • a lone lora_A or lora_B;
    • a wrong rank or width (through the existing checkShapes).
  • Key mapping. After base_model.model., the reader tries the path as is, then the variants [P2] feat(adapters): harness adapters — PEFT LoRA import, per-harness routing, fused export (HF harness-adapters-at-scale) #675 lists. The first one that names a Linear of the loaded model wins:
    • model.language_model. → language_model.model., model. or language_model.;
    • model. → language_model.model..
  • Weights. PEFT lora_A [r, in] / lora_B [out, r] are transposed to mlx-lm lora_a [in, r] / lora_b [r, out] and cast to the layer's dtype. That is the scales' dtype for a quantized layer; the files ship in F32.

The fork already has LoRAContainer.fromPEFT from upstream. It is not used here because it has no rsLoRA, no refusals, keeps F32, and keeps PEFT paths that do not match basalt's module paths.

One difference from the issue text. Layers are wrapped per layer-relative key across all layers (LoRAContainer.load), not per module path. A layer that has the key but no weights gets a zero lora_b, so its output does not change; it only costs one more small matmul per token. For the harness-hub adapters this never happens: Qwen 3.8's self_attn.* exist only in full-attention layers, LFM's conv.* only in conv layers, and mlp.* / feed_forward.* are adapted wherever they exist. A PEFT adapter with layers_to_transform would pay it. If needed, a per-path wrapper is a follow-up.

Tests (PEFTAdapterTests, #228)

Test Fixture Check
PEFT vs merged weights .qwen35 (hybrid: full-attention q_proj/o_proj, all-layer down_proj), .lfm2Moe (conv.in_proj, self_attn.q_proj, dense feed_forward.w1) PEFT keys in VLM style; logits equal the dense W + s·B·A model within 1e-4, same greedy ids; exactly the targeted modules are wrapped
PEFT vs mlx-lm layout .qwen35 bit-identical logits
rsLoRA config scale = α/√r
Refusals (6) .qwen35 DoRA, modules_to_save, rank_pattern, an unmatched key, a lone lora_A, a wrong rank; nothing stays wrapped
Served by name .qwen35 through ModelOrchestratorActor.loadAdapters the adapter's greedy text equals the merged model's, differs from base, and base comes back

PEFTAdapterTests|AdapterServingTests|LabGateTests|PHMAdapterTests|OFTAdapterTests: 40 tests in 7 suites pass locally. swift build -Xswiftc -warnings-as-errors, check_no_new_comments and check_size_limits --impact: clean.

Swift 6.4.2 crashed in SILGen on try Self.merged(...)(tokens, cache: nil), a call on the existential returned by a throwing call. The test binds the model first instead.

Not in this PR (later phases of #675)

  • P1: repo ids for --adapter, harness routing (HarnessRoute), thinking off by default with a harness adapter.
  • P2: fused export (models convert --adapter).
  • P3: real-model gate on Qwen3.8-27B and LFM2.5-24B-A2B with the harness-hub adapters. No real adapter was loaded for this PR.

Part of #675.

🤖 Generated with Claude Code

https://claude.ai/code/session_012cicguZMN3jhNGmf5327oh

…-style key mapping, rsLoRA and named refusals (#675 P0)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012cicguZMN3jhNGmf5327oh
@goodmai
goodmai merged commit 7cdaf11 into main Nov 23, 2026
8 checks passed
@goodmai
goodmai deleted the feat/675-peft-adapters branch November 23, 2026 10:24
goodmai added a commit that referenced this pull request Nov 23, 2026
…pter check before download, base-changing inits refused, float LoRA on microscaled bases (#749)

* fix(adapters): only hf:org/repo names a Hugging Face repo, and its file list must show an adapter before anything downloads (review)

* fix(adapters): refuse base-changing PEFT inits and a non-positive alpha, keep LoRA weights in a float type on microscaled bases, separate config and weights errors (review)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant