# Agent brief: Convert so the runtime gets a graph it can run

- **Lesson id:** 05-conversion-exists-for-runtime
- **Goal:** Explain `torch.export`, `get_decomp_table()`, and `TorchConverter` as the path to IR Core AI and Metal can run.
- **Prerequisites:** Complete 01 through 04; know the original conversion script.
- **Inputs:** CPU `ClothingCNN`, fixed example tensor, exported program, Core AI decomposition table.
- **Outputs/artifacts:** Conversion order diagram, concise code sketch, `clothing.aimodel` interface contract.
- **Concrete steps:**
  1. Export with float32 `[1,1,28,28]` example.
  2. Run `get_decomp_table()` before conversion.
  3. Explain `TorchConverter`, `main`, `image`, and `logits`.
  4. Place custom-kernel registration before `add_exported_program`, with version-sensitive signatures.
  5. Require inspection of the asset in Xcode's model viewer.
- **Constraints:** Do not invent APIs; no CUDA; no Mermaid, quizzes, or em dashes; `.aimodel` is the portable file.
- **Acceptance checks:** Order is unambiguous, custom-kernel timing is correct, names and shapes are stated, and the page links original conversion.
- **Human lesson:** [05-conversion-exists-for-runtime.html](05-conversion-exists-for-runtime.html)

## Environment setup

Use macOS with Python 3.11+ and `uv`:

```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
mkdir -p clothing-coreai && cd clothing-coreai
uv init --python 3.11
uv venv
uv add torch torchvision coreai-torch
uv run python -c "import torch, torchvision; print(torch.__version__, torchvision.__version__); print('MPS:', torch.backends.mps.is_available())"
uv run python train.py
```

Run lesson scripts with `uv run`. Prefer MPS on Apple silicon, and use CPU as the fallback. Do not add CUDA.

## Key concepts

- Conversion exists so the runtime gets a graph it can run.
- The path is `ClothingCNN` to `torch.export` to `get_decomp_table()` to `TorchConverter` to `clothing.aimodel`.
- Export with a float32 `[1, 1, 28, 28]` example. Names `image`, `logits`, and `main` are part of the contract.
- Inspect the saved asset in the Xcode model viewer before you debug Swift.

## Takeaways

- Successful Python conversion does not prove the app passes the right array.
- Fixed example shapes in export must match what the app will pass.
- Only supported lowerings or clearly registered custom kernels should reach the file.
