Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix(issue-1227): fail fast on torch LoRA training on Apple Silicon
[._.]/ Adam Eivy46e4fbdb6436
diff --git a/docs/research/2026-07-18-lora-training-e2e-validation.md b/docs/research/2026-07-18-lora-training-e2e-validation.mdnew file mode 100644index 0000000000..73734bbeac--- /dev/null+++ b/docs/research/2026-07-18-lora-training-e2e-validation.md@@ -0,0 +1,116 @@+# Validation: Character LoRA training — end-to-end, both runtimes++**Status:** Closed — mflux runtime validated end-to-end incl. inference round-trip; torch fallback confirmed non-viable on Apple Silicon (guarded)+**Date:** 2026-07-18+**Hardware:** Apple M-series, 128 GB unified memory, macOS `Darwin 25.5.0`+**Closes:** the "needs a real training run" blocker on issue #1227 (character LoRA training follow-ups)++This records the real end-to-end training run #1227 had been waiting on (previously+blocked on a GPU bench session + disk headroom). It validates the **mflux (MLX)**+runtime — the preferred Apple-Silicon path — from config through a trained adapter+and back through inference, and it establishes that the **torch/diffusers fallback**+cannot train on Apple Silicon (a PyTorch MPS backend limitation), which is now+guarded at request time.++## Environment rebuilt++The prior `/opt/miniconda3` mflux install from the 2026-06 verification was gone.+Rebuilt the pinned stack (`setup-image-video.sh`'s pins) into a dedicated venv,+since this machine's system `python3` is a broken/too-old pyenv:++- `mflux==0.17.5 · mlx==0.31.2 · mlx-metal==0.31.2` in a `uv`-provisioned Python+ 3.11 venv (`~/.portos/venv-mflux`), with `settings.imageGen.local.pythonPath`+ pointed at it so `isMfluxTrainAvailable` finds `mflux-train` beside it.+- FLUX.2-klein base weights were already in the HF cache (no ~16 GB download —+ the old disk-space blocker is gone).++> Follow-up filed: `setup-image-video.sh` installs mflux via `pip install --user`+> against the system `python3`; on a machine whose system python is unsuitable+> (too old / broken) that path fails, and a dedicated venv (as the ltx/wan/hunyuan+> runtimes already use) is needed. Tracked separately.++## mflux (MLX) runtime — VALIDATED end-to-end++Dataset: an original character (25 ready images, all captioned with the trigger+word). Smoke config built by the real `buildMfluxTrainConfig`/`buildMfluxTrainArgs`+service code, staged exactly as `runTraining` does (`NNNN.png`+`NNNN.txt`), 4B base,+segmentation ON.++| Step | Result |+|---|---|+| Config schema vs installed mflux 0.17.5 | `mflux-train --dry-run` → `✅ Training config validated` |+| Entrypoint probe | `mflux-train` console script found + launched |+| Output regexes | `STEP:` / `STAGE:` / `CHECKPOINT:` / `RESULT:` all parsed |+| Segmentation + GPU teardown/cooldown/resume | 5 segments, each resume re-applied the LoRA cleanly (`200/200 keys matched`) |+| Checkpoint-zip layout | `checkpoints/NNNNNNN_checkpoint.zip` at 12/24/36/48/50 |+| Adapter discovery | `*_adapter.safetensors` cracked from the newest zip (200 diffusers-style keys) |+| RESULT JSON | emitted with `adapter_path`, `steps` |+| Watchdog mitigation | `AGX_RELAX_CDM_CTXSTORE_TIMEOUT=1` set; run stayed well under the 150–300-step panic window; no reboot |++Exit 0 in ~7.4 min (50 steps).++### Inference round-trip — VALIDATED++The trained adapter loaded through `scripts/flux2_macos.py --lora-paths` on the SDNQ+4-bit inference pipeline (`~/.portos/venv-flux2`, torch 2.12 / diffusers):++```+🎚️ loading LoRA: 0000050_adapter.safetensors (adapter=lora_0)+🔁 mflux adapter: remapped 10 double-block to_out key(s) → diffusers .0 form+✅ active LoRA adapters: [('lora_0', 1.0)]+✅ flux2 saved roundtrip.png (seed=42)+```++The mflux→diffusers `to_out` key remap (`scripts/lora_utils.py` `_remap_mflux_flux2_keys`)+fired for exactly the 10 double-block `attn.to_out` keys the adapter carries — the+load-bearing compat step — and produced a coherent 512-px character render (no+collapse-to-black). 50 steps is far too few to bind identity; the goal was pipeline+mechanics + a clean LoRA load, both confirmed.++### Note: `nan` loss++The wrapper reported `STEP:N:50:nan` and `final_loss: null`. mflux's base-model+training does not surface a parseable per-step loss to stdout (`TQDM_RE` only+captures step/total), so the wrapper honestly reports `nan`/`null`. Training and+the adapter are valid — the round-trip render proves it. Benign; not a defect.++## torch/diffusers fallback runtime — NOT viable on Apple Silicon++The vendored `scripts/train_flux2_lora.py` (torch/diffusers+peft, the fallback for+non-mflux machines) was smoke-tested directly. Pipeline load and+`STAGE:precompute-latents` succeed, but training dies at the **first**+`loss.backward()` — three successive PyTorch **MPS** backend limitations:++1. `Input type (MPSBFloat16Type) and weight type (CPUBFloat16Type) should be the+ same` — reentrant gradient-checkpointing recompute mis-tracks device on MPS.+2. With checkpointing off: `mps_linear_backward: unsupported weights data type:+ BFloat16` — MPS has no bf16 Linear backward.+3. With an fp32 base: `mps_linear_backward: unsupported weights data type: Float`+ — MPS rejects fp32 Linear backward for these layers too.++These are PyTorch MPS backend gaps, not PortOS logic bugs, and they land at the+optimizer step regardless of dtype — so the torch trainer cannot complete a LoRA+step on Apple Silicon. This is exactly why mflux (MLX-native) is *the*+Apple-Silicon runtime and the torch path is scoped to "a non-mflux machine"+(CUDA/CPU). CUDA has bf16 `linear_backward`; that path is unchanged and remains to+be validated on a CUDA box (follow-up).++### Fix: fail fast instead of crash-after-load++Rather than queue a torch run on Apple Silicon that loads a ~16 GB base and+precomputes latents for minutes only to crash, the fallback now refuses up front:++- **Request time (`server/services/loraTraining/index.js`):** on darwin, a+ flux2-runtime `startTrainingRun` throws `412 TRAINING_MPS_UNSUPPORTED` pointing+ the user at mflux (`scripts/setup-image-video.sh`), before any run record or job+ is created. Covered by `mpsGuard.test.js`.+- **Trainer (`scripts/train_flux2_lora.py`):** defense-in-depth `USER_ERROR:+ TRAINING_UNSUPPORTED_DEVICE` when `--device` resolves to `mps`, before loading+ the base. `--device cpu` is still allowed (fp32 CPU backward works, just slow).++## Outcome++- mflux runtime: training + adapter extraction + inference round-trip validated on+ the current pinned stack. The user's real character LoRAs train on this hardware.+- torch fallback: confirmed non-viable on MPS and now guarded with actionable+ guidance; CUDA/CPU e2e validation deferred to appropriate hardware.diff --git a/scripts/train_flux2_lora.py b/scripts/train_flux2_lora.pyindex 19adb85dc8..286a71fab0 100644--- a/scripts/train_flux2_lora.py+++ b/scripts/train_flux2_lora.py@@ -190,6 +190,24 @@ def main():from peft.utils import get_peft_model_state_dictdevice = pick_device(args.device)+ # Apple-Silicon guard: this torch/diffusers trainer can't complete a LoRA+ # backward on Metal. PyTorch's MPS backend has no `linear_backward` for the+ # FLUX.2 transformer's Linear layers (raises "mps_linear_backward:+ # unsupported weights data type" for both bf16 AND fp32 at the first+ # loss.backward()). The preferred Apple-Silicon runtime is mflux (MLX-native,+ # trains bf16 directly); this torch path is the fallback for CUDA/CPU boxes.+ # Fail fast BEFORE loading the ~16 GB base + minutes of latent precompute+ # rather than crashing at the first optimizer step. A forced `--device cpu`+ # is still allowed (fp32 CPU backward works, just slow).+ if device == "mps":+ print(+ "USER_ERROR:TRAINING_UNSUPPORTED_DEVICE:the torch FLUX.2 trainer can't train on "+ "Apple Silicon (Metal/MPS) — PyTorch has no MPS linear_backward for these layers. "+ "Use the mflux runtime instead: install mflux ≥0.17 via scripts/setup-image-video.sh. "+ "(Force --device cpu to train on CPU, but that is impractically slow.)",+ file=sys.stderr, flush=True,+ )+ sys.exit(1)dtype = torch.bfloat16 if device != "cpu" else torch.float32torch.manual_seed(args.seed)diff --git a/server/services/loraTraining/index.js b/server/services/loraTraining/index.jsindex c6a29699b3..ef3e205f72 100644--- a/server/services/loraTraining/index.js+++ b/server/services/loraTraining/index.js@@ -156,6 +156,23 @@ export async function startTrainingRun({const { dataset } = await validateDatasetReady(datasetId, { acknowledgeCaptionLeak });if (routing.runtime === TRAINING_RUNTIMES.FLUX2) {+ // The torch/diffusers fallback trainer can't train on Apple Silicon:+ // PyTorch's MPS backend has no `linear_backward` for the FLUX.2 transformer+ // Linear layers, so a LoRA backward dies at the first optimizer step+ // ("mps_linear_backward: unsupported weights data type", for both bf16 and+ // fp32 — validated on an M-series box, issue #1227). mflux (MLX-native) is+ // THE Apple-Silicon runtime. Refuse at request time so we never queue a run+ // that loads a ~16 GB base and precomputes latents for minutes before+ // crashing. `--device cpu` would work but is impractically slow, so the+ // actionable guidance is to install mflux. (Linux/CUDA installs are the+ // torch path's real target and are unaffected.)+ if (platform() === 'darwin') {+ throw new ServerError(+ 'LoRA training on Apple Silicon requires the mflux runtime — the torch fallback can\'t train on this Mac\'s GPU (Metal/MPS). '+ + 'Install mflux ≥0.17 in your local image-gen python (Settings → Image Gen) via `bash scripts/setup-image-video.sh`.',+ { status: 412, code: 'TRAINING_MPS_UNSUPPORTED' },+ );+ }const healthy = await isFlux2VenvHealthy();if (!healthy) {throw new ServerError(diff --git a/server/services/loraTraining/mpsGuard.test.js b/server/services/loraTraining/mpsGuard.test.jsnew file mode 100644index 0000000000..725ac4a762--- /dev/null+++ b/server/services/loraTraining/mpsGuard.test.js@@ -0,0 +1,67 @@+import { describe, it, expect, vi, beforeEach } from 'vitest';++// Guard: startTrainingRun must REFUSE the torch/diffusers fallback trainer on+// Apple Silicon at request time. PyTorch's MPS backend has no linear_backward+// for the FLUX.2 transformer layers, so a torch LoRA backward dies at the first+// optimizer step — mflux (MLX) is the Apple-Silicon runtime (issue #1227). The+// refusal must happen before any run record is created or job enqueued, so we+// never load a ~16 GB base + precompute latents only to crash. On non-darwin+// (the torch path's real CUDA/CPU target) the same request proceeds.+let mockPlatform = 'darwin';+vi.mock('os', async (importActual) => {+ const actual = await importActual();+ return { ...actual, platform: () => mockPlatform };+});++const createRunMock = vi.fn(async () => {});+const updateRunMock = vi.fn(async () => {});+const enqueueJobMock = vi.fn(() => ({ jobId: 'job-1', position: 0 }));+const validateDatasetReadyMock = vi.fn(async () => ({+ dataset: { character: { entryId: 'c1', universeId: 'u1', name: 'Test Subject' }, triggerWord: 'test_subject' },+ manifest: { images: [] },+}));+const isFlux2VenvHealthyMock = vi.fn(async () => true);++vi.mock('./db.js', () => ({+ createRun: (...a) => createRunMock(...a),+ updateRun: (...a) => updateRunMock(...a),+ getRun: vi.fn(), getRunRequired: vi.fn(), listRuns: vi.fn(), deleteRun: vi.fn(),+}));+vi.mock('./dataset.js', () => ({ validateDatasetReady: (...a) => validateDatasetReadyMock(...a) }));+vi.mock('../settings.js', () => ({ getSettings: vi.fn(async () => ({})) }));+vi.mock('../loraDatasets.js', () => ({ updateDataset: vi.fn(async () => {}) }));+vi.mock('../mediaJobQueue/index.js', () => ({+ enqueueJob: (...a) => enqueueJobMock(...a),+ getJob: vi.fn(),+ mediaJobEvents: { emit: vi.fn(), on: vi.fn() },+}));+vi.mock('../../lib/pythonSetup.js', () => ({+ resolveFlux2Python: () => '/fake/venv-flux2/bin/python3',+ isFlux2VenvHealthy: (...a) => isFlux2VenvHealthyMock(...a),+}));++const { startTrainingRun } = await import('./index.js');++// getSettings → {} means pythonPath is null, so isMfluxTrainAvailable(null) is+// false and routing resolves to the torch (flux2) runtime — the branch the+// guard protects. flux2-klein-4b is a real registry model.+const BASE = 'flux2-klein-4b';++beforeEach(() => { vi.clearAllMocks(); mockPlatform = 'darwin'; });++describe('startTrainingRun — Apple Silicon torch-runtime guard', () => {+ it('refuses torch training on darwin before creating a run or queuing a job', async () => {+ await expect(startTrainingRun({ datasetId: 'ds1', baseModelId: BASE }))+ .rejects.toMatchObject({ code: 'TRAINING_MPS_UNSUPPORTED', status: 412 });+ expect(createRunMock).not.toHaveBeenCalled();+ expect(enqueueJobMock).not.toHaveBeenCalled();+ });++ it('does NOT fire the guard on non-darwin (torch path proceeds to queue)', async () => {+ mockPlatform = 'linux';+ const res = await startTrainingRun({ datasetId: 'ds1', baseModelId: BASE });+ expect(res).toMatchObject({ status: 'queued' });+ expect(createRunMock).toHaveBeenCalled();+ expect(enqueueJobMock).toHaveBeenCalled();+ });+});