---
name: LLM LoRA for your writing style
description: >-
  Teach the on-device path for writing style: clean your private writing, train
  a LoRA on a Mac (MPS, or CPU if MPS is missing), merge it into a small
  instruct LLM, export a baked Core AI .aimodel plus tokenizer, and chat on
  device. No documented hot-swap LoRA.
---
# LLM LoRA for your writing style

This course is a practical skill for running a personalized model on a phone. The job is local adaptation of writing style, a baked file, and on-device chat. Ownership, privacy, latency, and offline use come from that baked model plus tokenizer. They do not come from a cloud adapter swap.

## The real path

Core AI has no documented hot-swap LoRA. Do not invent a runtime that attaches a new writing adapter on the device.

1. Prepare your local writing. Strip secrets. Keep the raw writing off the network.
2. Train the LoRA on a Mac. Use MPS when `torch.backends.mps.is_available()` is true. Use CPU otherwise. Never add CUDA.
3. Merge or fuse the adapter into the small instruct base.
4. Export the ordinary merged graph with `uv run coreai.llm.export <short-name-or-path> --output-dir ...`, or with `torch.export` plus `coreai-torch` `TorchConverter`. Keep the tokenizer sidecar.
5. Load the baked model with current Core AI language APIs in Xcode 27. Treat course Swift names as illustrative.

## End-to-end agent recipe

1. Read [lesson 01](01-what-style-lora-means.llms.md) to separate style from facts and set the no-hot-swap boundary.
2. Use [lesson 02](02-data-prep.llms.md) to clean, format, split, and inspect your writing.
3. Use [lesson 03](03-pick-base-llm.llms.md) to choose `qwen3-0.6b` or a `smollm2` instruct preset and record context limits.
4. Use [lesson 04](04-train-lora-on-mac.llms.md) for PEFT training, checkpoints, and sample review.
5. Use [lesson 05](05-merge-and-export-coreai.llms.md) to bake, export, and keep the tokenizer.
6. Use [lesson 06](06-on-device-chat.llms.md) for on-device chat with prepare-once.
7. Use [lesson 07](07-evaluate-writing-style.llms.md) to read traces and write short notes before you automate a judge.
8. Use [lesson 08](08-checklist.llms.md) before declaring the delivery complete.

## Constraints

- Course examples are `qwen3-0.6b` and `smollm2` instruct presets. Confirm the current [apple/coreai-models](https://github.com/apple/coreai-models) recipe. macOS and iOS presets differ.
- The export CLI takes a positional model (registry short-name, Hugging Face id, or a local merged folder). Do not invent a `--model` flag. Distinguish `--platform macOS` from `--platform iOS`.
- Keep private writing local. No CUDA, quizzes, Mermaid, or em dashes. Use progressive HTML `.flow` diagrams that grow one box at a time.
- Cross-link [PyTorch to Core AI in Xcode](../pytorch-core-ai-xcode/), [From the metal to the model](../core-ai-from-metal-back/), [Core AI models, typed](../coreai-models-typed/), [Model architectures in plain English](../model-architectures-plain/), and [Diffusion LoRA to Core AI on device](../diffusion-lora-coreai/).

## Key concepts

- Clean private writing, train a LoRA on a Mac, merge it into a small instruct LLM, export `.aimodel` plus tokenizer, chat offline.
- Core AI has no documented hot-swap LoRA.
- Train on MPS or CPU only. Never CUDA.
- Course examples are `qwen3-0.6b` and `smollm2` instruct presets.

## Takeaways

- You can bake your writing style into a model file that stays on the phone.
- You export with `coreai.llm.export` or `torch.export` plus `coreai-torch`.
- The tokenizer sidecar ships with the model. Token ids are part of the contract.

## Acceptance checks

- Every human lesson HTML has **Key concepts** and **Takeaways** sections before lesson-nav. The course home has the same two headings for the whole course.
- Eight numbered human lessons and eight matching `.llms.md` briefs exist.
- Every lesson has an on-device job frame, progressive `.flow` diagrams, a worked path, Failure modes or When this goes wrong, and Done when.
- Every lesson links its own brief and the five related courses.
- Merge-before-export, tokenizer sidecar, illustrative SDK wording, and the absence of a documented runtime LoRA swap are explicit.

## Environment setup

Use macOS with Python 3.11+ and one `uv` project. Install the official export entry points from [apple/coreai-models](https://github.com/apple/coreai-models) so `coreai.llm.export` is available, plus `coreai-torch` for the `torch.export` fallback.

```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
mkdir -p writing-style-lora && cd writing-style-lora
uv init --python 3.11
uv venv
uv add torch torchvision transformers peft datasets accelerate safetensors coreai-torch
uv run python -c "import torch; print(torch.__version__); print('MPS:', torch.backends.mps.is_available())"
uv run coreai.llm.export --help
uv run python train_lora.py
```

Use `uv run` rather than a global interpreter. Keep `uv.lock`, the tokenizer, the writing list (the corpus manifest), and base revision with the artifacts. Do not add CUDA.

## Xcode setup

Use Xcode 27. Create an iOS or macOS SwiftUI App target. Add the exported `.aimodel` and tokenizer sidecar to the target's resources. Add the Core AI framework. Install the Metal Toolchain.

```bash
xcode-select --install
xcodebuild -version
```

Confirm current class names in the installed SDK docs. Prepare the model once. The app loads the baked merged resource. It does not attach a LoRA at runtime.
