# Profile the clothing inference path

- **Lesson ID:** 06
- **Goal:** Separate one-time model setup from later Core AI inference using Xcode and Instruments on real hardware.
- **Human lesson:** [06-profile-in-xcode.html](06-profile-in-xcode.html)

## Prerequisites

- A working SwiftUI app and a physical iPhone, iPad, or Mac target.
- The Core AI model runs more than once with the bundled sample.

## Inputs, outputs, and artifacts

- **Inputs:** Running app; the same bundled sample each time; picked image; real-device trace.
- **Outputs:** Gauge notes and a Core AI Instruments recording that shows specialisation, load, setup, and inference as separate events.
- **Artifacts:** A saved Instruments trace or notes with first-run and later-run measurements.

## Agent build steps

1. Run on a real device. Use the simulator only to check wiring, not to judge timing.
2. Open Xcode's Debug navigator and Core AI gauge. Trigger the bundled sample several times.
3. Inspect Inference, Load, and Specialization activity. Use Core AI Debugger/graph and tensor views if a run fails.
4. Choose Product → Profile and the Core AI Instruments template. Record first setup and several later picks.
5. Stop recording and inspect `clothing.aimodel`/`main`. Compare setup, specialisation, load, and later inference.
6. If setup repeats for every pick, move model loading/specialisation back into `.task`/startup and rerun.

## Constraints

Use Core AI's gauge and Core AI Instruments template, not Core ML tooling. Preparation must happen once. Inference must use the fixed float32 `[1,1,28,28]` `image` input and `logits` output. Hardware timings are meaningful only on the target device.

## Key concepts

- Use a real iPhone, iPad, or Mac for timing. The simulator only checks wiring.
- The Core AI debug gauge shows Inference, Load, and Specialization live.
- Instruments event colors: green Specialization, cyan Load, magenta Setup, blue Inference.
- A good trace specialises and loads once, then shows steady inference on later picks.

## Takeaways

- The first pick should look different from later picks in the trace.
- Green specialisation or cyan load on every photo pick is a prepare-once bug.
- Expand tracks to confirm later intervals belong to `clothing.aimodel` function `main`.

## Acceptance checks

- A real-device trace exists with one first-run setup sequence and several later inferences.
- The gauge/trace shows no repeated specialisation or model load on every photo pick.
- The trace identifies the clothing model/function and separates setup from later work.
- Continue to [evaluate the classifier](07-evaluate-classifier.llms.md), then [the final checklist](08-end-to-end-checklist.llms.md).

## Environment setup

Use Xcode 27 on macOS. Create an iOS or macOS SwiftUI App target, add the exported `.aimodel` to the target's model/resources build phase, add the **Core AI** framework under Frameworks, Libraries, and Embedded Content, and install the **Metal Toolchain** in Xcode Settings > Components. Confirm the installed toolchain with:

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

Build the bundled model once before you add UI. Core AI and `NDArray` names can change between SDK versions, so check the exact names in the Xcode docs you have installed.
