---
name: diffusion-lora-coreai lesson 04
description: >-
  Agent recipe for lesson 04: merge a diffusion LoRA into the base, then export
  a baked Core AI resource with coreai.diffusion.export or torch.export plus
  coreai-torch.
---
# Merge, then export

**Lesson id:** `04`  
**One-line goal:** Bake the adapter into the base (merge the LoRA into the base and export one model file) so Core AI sees one ordinary diffusion resource.

## Why this matters on a phone

The baked `.aimodel` folder is stored on the phone. Core AI has no documented hot-swap LoRA. Merge is a build step.

## Prerequisites

A reviewed LoRA directory from lesson 03, the pinned base revision, and the `uv` project.

## Inputs

Base plus adapter, holdout prompts, installed `coreai.diffusion.export` and `coreai-torch`.

## Outputs and artifacts

`artifacts/merged-pipeline`, the untouched adapter, and `artifacts/personalized` resource folder.

## Executable steps

1. Recreate the `uv` environment if needed. Run `uv run coreai.diffusion.export --help`.
2. Reload the exact base revision. Attach the LoRA. Call `merge_and_unload` (or the recipe's fuse). Save a new directory.
3. Re-run holdout prompts on the merged pipeline. Compare to adapter-attached previews.
4. Export with `uv run coreai.diffusion.export <short-name-or-merged-path> --output-dir artifacts/personalized --overwrite`. Confirm flags. The model argument is positional.
5. If the recipe path is unavailable, export each named component with `torch.export`, `get_decomp_table()`, and `TorchConverter`.
6. Record checksums. Do not flatten the multi-component folder.

## Constraints

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

## Failure modes

Exporting PEFT modules, overwriting the adapter, invented flags, a single guessed file, skipped merged holdout.

## Key concepts

- Bake means `merge_and_unload()` into the UNet, then export one ordinary graph.
- Keep the unmerged adapter. Merge is not reversible from the baked file.
- Export with `coreai.diffusion.export` or `torch.export` plus `coreai-torch`.
- SD 1.x needs a multi-component folder: text encoder, UNet, and VAE parts.

## Takeaways

- Merge on CPU. Do not overwrite the adapter directory.
- Confirm flags with `--help` and `--dry-run`. Do not invent a `--model` flag.
- Exporting an unmerged PEFT wrapper will fail graph capture.

## Done when

Merged pipeline loads without PEFT, adapter still exists, export used a documented path, resource folder matches the recipe's component list.

**Human lesson:** [Merge, then export](04-merge-then-export.html)
