---
name: llm-lora-writing-style lesson 05
description: >-
  Agent recipe for lesson 05: merge a writing-style LoRA into the base, export
  with coreai.llm.export or torch.export plus coreai-torch, and keep the
  tokenizer sidecar.
---
# Merge and export to Core AI

**Lesson id:** `05`  
**One-line goal:** Bake the adapter into the instruct base and export a Core AI `.aimodel` plus tokenizer.

## On-device job

The baked language file is stored on the phone. Core AI has no documented hot-swap LoRA. Platform presets differ.

## Prerequisites

Reviewed adapter from lesson 04. Pinned base. Installed export tools.

## Inputs

Base plus adapter, review prompts, `--platform` choice.

## Outputs and artifacts

`artifacts/merged-instruct`, untouched adapter, `.aimodel`, tokenizer sidecar, checksums.

## Executable steps

1. Recreate the `uv` environment. Run `coreai.llm.export --help` and `--list-presets`.
2. `PeftModel.from_pretrained`, `merge_and_unload`, save a new directory, keep the adapter.
3. Re-run review prompts on the merged model.
4. Export with a positional model argument and an explicit `--platform`. Confirm with `--dry-run`.
5. If needed, fall back to `torch.export` plus `TorchConverter`.
6. Bundle and checksum the tokenizer sidecar.

## Constraints

Do not invent flags. Do not claim a runtime LoRA API. No CUDA, quizzes, Mermaid, or em dashes.

## Failure modes

Exporting PEFT, overwriting the adapter, invented `--model` flag, wrong platform preset, missing tokenizer.

## Key concepts

- Merge with `merge_and_unload()` into a new directory. Keep the adapter untouched.
- Export with `coreai.llm.export` and `--platform iOS` or macOS, or with `torch.export` plus `coreai-torch`.
- Bundle the `.aimodel` with the tokenizer sidecar.
- Re-run review prompts on the merged model before export.

## Takeaways

- A macOS quantization preset does not apply on iOS.
- Exporting a PEFT wrapper fails graph capture.
- The model argument is positional. Do not invent a `--model` flag.

## Done when

Merged model loads without PEFT, adapter remains, export used a documented path, tokenizer sits beside the `.aimodel`.

**Human lesson:** [Merge and export to Core AI](05-merge-and-export-coreai.html)
