# `.zynfer` artifact format (v1)

Native model weights for the Zig runtime. Hot path never parses Safetensors;
conversion is a development-time step (`tools/checkpoint/`).

## Layout (little-endian)

```text
[Header 88 bytes]
[Meta 116 bytes]
[TensorEntry × N]   N = Header.tensor_count; each entry 120 bytes
[padding to 64-byte alignment]
[payload bytes]     each tensor starts on a 64-byte boundary
```

### Header

| Field | Type | Notes |
| --- | --- | --- |
| magic | `[4]u8` | `ZYNF` |
| version | `u16` | `1` |
| endian | `u8` | `1` = little |
| flags | `u8` | `0` (reserved) |
| header_bytes | `u32` | `88` |
| meta_offset | `u32` | `88` |
| meta_bytes | `u32` | `116` |
| dir_offset | `u32` | `204` |
| dir_bytes | `u32` | `N * 120` |
| tensor_count | `u32` | |
| reserved0 | `u32` | `0` |
| payload_offset | `u64` | 64-byte aligned |
| payload_bytes | `u64` | |
| sha256 | `[32]u8` | hash of whole file with this field zeroed |

### Meta

Architecture parameters (Qwen3-0.6B values match HF `config.json`):
`model_id` (NUL-padded 64), vocab/hidden/intermediate/layers/heads/kv/head_dim/
max_position/bos/eos, `rope_theta`, `rms_norm_eps`, `tie_word_embeddings`.

### TensorEntry

NUL-padded `name[64]`, optional numeric `tensor_id`, `dtype` (`0=f32`,
`1=f16`, `2=bf16`, `3=i8`), `rank`, `shape[8]`, `offset` (relative to payload),
`nbytes`. Stage M5 int8 projections use dtype `3` with a sibling
`{name}.qscale` f32 vector (`tools/checkpoint/quantize_zynfer_int8.py`).

## Commands

```bash
zynfer artifact-compile --out stage10-fixture.zynfer
zynfer inspect stage10-fixture.zynfer
zynfer stage10
```

### Optional: real Qwen3-0.6B weights

`models/` is gitignored. Full prerequisites and troubleshooting:
`tools/checkpoint/README.md`. Use the Hugging Face `hf` CLI (not the
deprecated `huggingface-cli`):

```bash
pip install -U "huggingface_hub[cli]"

hf download Qwen/Qwen3-0.6B --local-dir models/Qwen3-0.6B

python3 tools/checkpoint/safetensors_to_zynfer.py \
  --config models/Qwen3-0.6B/config.json \
  --weights models/Qwen3-0.6B \
  --out models/qwen3-0.6b.zynfer

zig build -Dhip=off
./zig-out/bin/zynfer inspect models/qwen3-0.6b.zynfer
```

`--weights` may be a file, a directory (shards / `model.safetensors.index.json`),
or multiple shard paths. The converter copies raw Safetensors bytes
(including BF16 and F16) without NumPy and reports a dtype summary on
stderr. Stage M4’s Metal half path can upload those half payloads to the
GPU without re-promoting through host f32.

### Tensor ids

Directory entries carry a stable `tensor_id` (`u32`, non-zero). The converter
assigns **1..N in sorted tensor-name order**. Zig hot path should use
`Artifact.findById` / `tensorBytesById`; `findByName` is for inspect/debug.

### Loading

`Artifact.loadFile` memory-maps the file (read-only, `MAP_PRIVATE`) on
POSIX hosts and falls back to a heap copy otherwise. `inspect` prints
`storage: mmap` or `heap`.

## Stage boundary

| Stage | Delivers |
| --- | --- |
| **10** | `.zynfer` format, validate, load, inspect, optional local convert |
| **11** | Full Qwen forward + golden logits ([`docs/stages/11-qwen-forward.md`](stages/11-qwen-forward.md)) |
| **12** | Tokenizer, sampling, TTFT / tok/s |

Not in CI: Hugging Face weight download (use tiny fixture or local artifacts).

