# Language model formats

This page lists every format that the Gizlang language model stack (`gizai/llm`) reads, runs and writes. It also
specifies the Gizlang model folder (`gizlang.llm.2`) byte by byte, so that another tool can read or check one.

The [language model guide](../guides/llm.md) explains how to run, serve, convert and measure a model. The source of
truth is the code. Where this page and the code differ, the code is right and this page has a defect:

| Subject | Module |
| --- | --- |
| Architectures (`config.json` to `Arch`) | `gizai/llm/recipes`, `gizai/llm/arch` |
| Source tensors and the plan of each linear | `gizai/llm/plan` (`WeightView`, `planned`, `FORMAT`, `runsOn`) |
| Weight formats, `QuantSpec`, tensor layouts | `gizai/quant` (`specOf`, `QuantSpec.text`, `parseSpec`, `piecesOf`) |
| Writing a folder | `gizai/llm/convert` (`convert`, `convertDrafter`, `checkpoint`, `layoutOf`) |
| Reading a folder or a source | `gizai/llm/model` (`tensorsOf`, `specsOf`, `described`), `gizlang/safetensors`, `gizlang/gguf` |
| KV formats | `gizai/llm/kvformat` |

## Overview

| Kind | Formats | Read | Run | Written |
| --- | --- | --- | --- | --- |
| Model containers | Hugging Face folder (safetensors, one file or shards); GGUF file; Gizlang model folder; NPU folder (`model.q4nx`) | Yes | Yes | Gizlang model folder; standard NVFP4 checkpoint |
| Source weights | F32, F16, BF16; FP8 E4M3 (a scale for each tensor, row or 128 × 128 block); NVFP4 (ModelOpt, compressed-tensors); GGUF block types | Yes | As [weight formats](#weight-formats) | — |
| Weight formats in memory and on disk | `q2` to `q8` (codebooks), `bf16`, `f16`, `f8`, `nv4`, `k4`, `k6`, `k8` | Yes | By device ([Devices](#devices)) | Yes |
| KV cache formats | `16`, `8`, `4`, `4/2`, `nv4/8`, `nv4/4`, `nv4`, one for each layer | — | By device | — |
| Drafters | MTP layer in the model; DFlash and DFlash 2 block drafters; lookup | Yes | Yes | Converted DFlash drafter (`drafter/`) |
| Adapters | PEFT LoRA (`adapter_config.json`, `adapter_model.safetensors`), rsLoRA | Yes | Merged at conversion | — |
| Vision towers | `model.visual.*` of Qwen2-VL, Qwen2.5-VL, Qwen3-VL, Qwen3.5 and 3.8 | Yes | Yes | `visual.safetensors` |
| Tokenizers and templates | `tokenizer.json`, `tokenizer_config.json`, `chat_template.jinja` or the template in `tokenizer_config.json`; the GGUF tokenizer and template | Yes | Yes | Copied as they are |

## Sources

### Containers

| Container | How it runs | Notes |
| --- | --- | --- |
| Hugging Face folder or repository (`owner/name`) | As it is stored: each linear is laid out in memory as a conversion would write it, just before it goes to the device. A catalog name or repository is converted once on its first use and kept ([the conversion cache](../guides/llm.md#the-conversion-cache)). | A model with experts or n-gram tables, or with a vision tower, runs converted. |
| GGUF file | As it is stored: supported K blocks stay quantized and are laid out in memory. No converted weight file is written. Tuning, KV costs and a drafter go to `FILE.gguf.gizlang/`. | Llama, Qwen2, Qwen3, Qwen3.5 and Gemma 4 files, with their tokenizer and chat template. Other architectures are refused by name. A Qwen3.5 GGUF has no MTP layer. |
| Gizlang model folder | As it is stored: a load copies each tensor to the device with no work on the host. | [The Gizlang model folder](#the-gizlang-model-folder). A repository that holds one runs as it is, with its `drafter/` and `tune/`. |
| NPU folder (`model.q4nx`, `xclbins/`) | On an AMD XDNA 2 NPU | Gemma 4 E2B and Qwen3.5-9B. `convertQwenNpu` makes one from a BF16 Qwen3.5-9B ([guide](../guides/llm.md#before-you-start)). |

### Architectures

`gizai/llm/recipes` reads `config.json` by its `model_type`. A renamed `model_type` with no code of its own is read
by the transformers class that its `architectures` names (for example `Qwen3ForCausalLM`).

| Family | `model_type` |
| --- | --- |
| Llama, InternLM 3, Seed-OSS | `llama`, `internlm3`, `seed_oss` |
| Mistral | `mistral` |
| Qwen 2, 2.5 and their vision models | `qwen2`, `qwen2_vl`, `qwen2_5_vl` |
| Qwen 3 | `qwen3` |
| Qwen3-VL | `qwen3_vl` |
| Qwen 3.5 and Qwen3.8 (delta-rule and attention layers, an MTP layer, vision) | `qwen3_5` |
| Qwen3.8 Flash (experts between hyper-connections) | `qwen4_exp` |
| Gemma, Gemma 3, Gemma 4 | `gemma`, `gemma3`, `gemma4`, `gemma4_unified` |
| Phi-3, Granite, GLM-4, SmolLM3, ERNIE 4.5, EXAONE 4 | `phi3`, `granite`, `glm4`, `smollm3`, `ernie4_5`, `exaone4` |
| LFM2 and LFM2.5 (short convolution and attention layers) | `lfm2` |
| BERT, RoBERTa, XLM-RoBERTa encoders (embedding and rerank models) | `gizai/llm/encoders` |
| DFlash and DFlash 2 block drafters | `architectures` starting with `DFlash` |

A supported architecture does not prove every variant or size. The catalog (`giz ai models`) lists the models that
are checked.

### Source weights

Every source enters by one path (`gizai/llm/plan`). Each tensor becomes a `WeightView`: its name, shape, dtype, its
own quantization and scales, and its place in its file. The planner then gives each linear one `QuantSpec`
([How a source becomes a format](#how-a-source-becomes-a-format)).

| Source | Tensors | Stored as | Values |
| --- | --- | --- | --- |
| Float | F32, F16, BF16 | The format asked for (`--weights`, `--head`) | Rounded once |
| NVFP4, NVIDIA Model Optimizer | `NAME.weight` U8 `[n, k/2]` (two E2M1 codes a byte, low nibble first), `NAME.weight_scale` E4M3 (one for each 16 inputs), `NAME.weight_scale_2` F32 | `nv4` | Kept |
| NVFP4, compressed-tensors (llm-compressor) | `NAME.weight_packed`, `NAME.weight_scale`, `NAME.weight_global_scale` (the inverse of the scale) | `nv4` | Kept |
| FP8 E4M3 with a scale for each tensor or row | `NAME.weight`, `NAME.weight_scale` | `f8` | Kept |
| FP8 E4M3 with 128 × 128 block scales (Qwen and DeepSeek FP8 releases) | `NAME.weight`, `NAME.weight_scale_inv` | `f8` | Rounded to F8 again |
| GGUF Q4_K, Q6_K (inputs a multiple of 256) | Blocks | `k4`, `k6` | Kept |
| GGUF Q8_0 (inputs a multiple of 128) | Blocks | `k8` | Kept |
| GGUF Q2_K, Q3_K, Q4_0, Q4_1, Q5_0, Q5_1, Q5_K, Q8_K, IQ1_S to IQ4_XS | Blocks | Decoded, then as float weights | Rounded again |

- A Model Optimizer checkpoint's `input_scale` (an activation scale) is not used. Each input row is rounded to NVFP4
  on the device with its own scale.
- A linear that mixes NVFP4 and other sources is refused.
- A fused projection whose GGUF sources use different K formats keeps one physical linear for each run of one format.
  No source is quantized again to combine them.
- A GGUF embedding keeps its block type.

### Adapters

`--lora DIR[:STRENGTH]` merges a PEFT LoRA adapter into the weights before they are quantized:
`W + alpha / r · B·A` (`alpha / √r` with rsLoRA), times STRENGTH. Give it once for each adapter; they add up. An
adapter merges only into a float source that converts to a codebook. A GGUF file with an adapter keeps the rotation
([Rotation](#rotation)), and its untouched K blocks read their inputs turned back.

## Weight formats

| Format | A weight is | Bits a weight | Products |
| --- | --- | --- | --- |
| `q2` … `q8` | `book[code] × scale`: a code of 2 to 8 bits into the linear's codebook, an F16 scale for each row and block of 128 inputs, after a Hadamard rotation | 2 to 8, plus 1/8 for the scales | Decoded in the kernel, on every device |
| `bf16`, `f16` | The value | 16 | BF16 or F16 matrix cores |
| `f8` | FP8 E4M3 × an F16 scale for each row and block of 128 (the block's largest value at 448) | 8, plus 1/8 | FP8 matrix cores where the device has them, else decoded |
| `nv4` | NVFP4: E2M1 × the E4M3 scale of its block of 16 inputs × the F32 scale of its row | 4.5 | FP4 matrix cores (W4A4: inputs rounded to NVFP4 by row) |
| `k4`, `k6` | GGUF Q4_K and Q6_K, kept: `d × a × q − dmin × b` | 4.5, 6.6 | Decoded in the kernel |
| `k8` | GGUF Q8_0, kept: `(code − 128) × scale`, an F16 scale for each 32 inputs | 8.5 | Decoded in the kernel |

`giz ai convert --weights FORMAT --head FORMAT` chooses the formats of the layers and of the output layer (default
`4` and `6`). `--sensitive BITS` gives BITS to the linears nearest the ends of the layer stack, one weight in eight
([mixed precision](../guides/llm.md#mixed-precision)). A catalog chat model of 4-bit linears takes `nv4` on an NVIDIA
Blackwell GPU and 4-bit codebooks elsewhere. `NAME:FORMAT` names a format yourself (`qwen3.8-27b:q8`).

## KV formats

Each attention layer keeps the keys and values of every position in pages of 16 positions. A format is the bits of a
key and of a value. The engine chooses one for each layer by the folder's measured costs (`kv-costs`), or you name
them with `--kv`.

| Format | Size against 16 bits | What it is |
| --- | --- | --- |
| `16` | 1 | F16: the reference |
| `8` | about 1/2 | 8-bit codes of the rotated head |
| `4` | about 1/4 | 4-bit codes |
| `4/2` | about 1/5 | Keys in 4 bits, values in 2 |
| `nv4/8`, `nv4/4` | about 3/8, 1/4 | Keys in NVFP4 (FP4 products with the queries), values in 8 or 4 bits |
| `nv4` | about 1/4 | Keys and values in NVFP4 (heads of 256 dimensions; other heads take 4 bits) |
| `8,8/4,4,...` | for each layer | One format for each pool layer, in order |

A code holds `2^bits` even steps over each head's range, after a rotation of each block of the head. A head of a size
other than 64, 128, 256 or 512 stays in F16. [Keys and values](../guides/llm.md#keys-and-values-kv) in the guide has
the details and the measured costs.

## Devices

| Device | Weight formats | KV formats |
| --- | --- | --- |
| NVIDIA sm_80 and later (CUDA) | All but `nv4`. BF16 and INT8 products; codebook and K formats decoded in the kernel | All but `nv4*` |
| NVIDIA sm_89 and later | Also FP8 products for `f8` | Also 8-bit attention values in FP8 |
| NVIDIA sm_120, sm_121 (RTX 50, GB10) | Also `nv4` (FP4 products) | Also `nv4/8`, `nv4/4`, `nv4` |
| AMD GPU (HIP) | All but `nv4`. Matrix-core products on gfx11 and gfx120 | All but `nv4*` |
| Apple GPU (Metal) | All but `nv4` | All but `nv4*` |
| CPU | All but `nv4` (slower). Not Qwen3.8 Flash | 8 bits by default |
| AMD NPU (XDNA 2) | The NPU folder's Q4K projections, Q8 head, BF16 embedding | Its own |

NVFP4 weights and KV run only on a device with native FP4 matrix cores. Other devices refuse them before anything
runs (`requireOn`). Use a codebook conversion of the float source, or a GGUF file. Conversion and inspection need no
GPU.

## Outputs

| Output | Command | What it is |
| --- | --- | --- |
| Gizlang model folder | `giz ai convert FROM TO [--weights F] [--head F] [--sensitive B] [--layers N] [--lora DIR]` | [The Gizlang model folder](#the-gizlang-model-folder) |
| Converted drafter | `giz ai convert DRAFTER MODEL/drafter` | [The drafter](#the-drafter) |
| Standard NVFP4 checkpoint | `giz ai convert FROM TO --checkpoint nvfp4` | A Hugging Face folder of the float model's layer linears in compressed-tensors NVFP4 (`NAME_packed`, `NAME_scale`, `NAME_global_scale`: one scale a tensor); the rest as it is. Other runtimes can read it, and `convert` takes it as an NVFP4 source with its values kept. It does not write a `quantization_config` into `config.json` yet. |
| KV costs | `giz ai kvcosts FOLDER TEXT --tokens N` | `FOLDER/kv-costs` ([KV costs](../guides/llm.md#measure-the-kv-costs-of-a-model)) |

## Why a folder format of its own

A Hugging Face folder and a GGUF file run as they are stored, so a converted folder is never required. It is the
better choice for production serving, for these reasons:

- **No layout work at load.** Each linear is stored in the order the matrix cores read it (the
  [lane order](#lane-order)). A load copies the bytes to the device as they are. A standard checkpoint stores
  row-major weights, so a runtime must repack them, or its kernels must pay for the other order on every step.
- **One statement of every linear.** The metadata names each linear's `QuantSpec`. The loader checks the dtype, shape
  and size of every tensor against it before anything runs. It never guesses a format. A file that does not match
  fails at load and names the tensor and the reason.
- **Formats that standard checkpoints do not have.** These are codebooks of 2 to 8 bits after a Hadamard rotation,
  and GGUF K blocks kept as they are. Each linear can have its own format: mixed precision, and a head of another
  format.
- **The parts a server needs, in one folder.** These are the block drafter, the measured KV costs, the kernel
  tunings and the vision tower.

It has these costs:

- Only Gizlang reads it. vLLM, SGLang, TensorRT-LLM, transformers and llama.cpp cannot. To publish weights for those
  runtimes, publish the source or a standard checkpoint (`--checkpoint nvfp4`).
- A new layout version means that you must convert again. A converted folder is not a source: keep the source.

### Compared with a standard NVFP4 checkpoint

| | Standard NVFP4 checkpoint (ModelOpt, compressed-tensors) | Gizlang folder (`nv4`) |
| --- | --- | --- |
| Values | E2M1 codes, an E4M3 scale for each 16 inputs, an F32 scale for each tensor | The same codes and block scales, unchanged. The tensor scale goes into each row's scale. |
| Order | Row-major: `[n, k/2]` bytes | Lane order of the FP4 matrix cores (`mma m16n8k64` with block scales): [NV4](#lane-order) |
| Format of each linear | `hf_quant_config.json`, or `quantization_config` in `config.json` | A `QuantSpec` in the safetensors metadata, checked against each tensor |
| Activation scales (`input_scale`) | Stored; a runtime may use them | Not used: each input row is rounded to NVFP4 with its own scale |
| Linears that the checkpoint does not quantize | BF16 or FP8 as they are | `f8`, or `nv4` when you ask `--weights nv4` |
| Runtimes | vLLM, SGLang, TensorRT-LLM | Gizlang; Gizlang also runs the standard checkpoint itself |

## The Gizlang model folder

The rest of this page specifies the folder that `giz ai convert` writes, format **`gizlang.llm.2`**.

### Files

| File | Required | What it holds |
| --- | --- | --- |
| `model.safetensors` | Yes | The embedding, every linear in its format, the norms and the small tensors. Its metadata names the format and each linear's spec ([The weight file](#the-weight-file)). |
| `config.json` | Yes | The source's model configuration, as it is. The architecture (`gizai/llm/recipes`) is read from it at every load. |
| `tokenizer.json`, `tokenizer_config.json` | Yes | The source's tokenizer, as it is. |
| `generation_config.json` | No | The source's sampling defaults and end tokens. |
| `chat_template.jinja` | No | The chat template, where the source keeps it in its own file. |
| `visual.safetensors` | No | The vision tower (`model.visual.*`) with its values as the source stores them. It stays in host memory. A picture moves it to the device. |
| `experts.safetensors` | No | Qwen3.8 Flash only: the experts, read from disk into a cache on the device when the router picks them. |
| `ngrams.safetensors` | No | Qwen3.8 Flash only: the n-gram embedding tables. |
| `drafter/` | No | A converted block drafter (DFlash) for this model ([The drafter](#the-drafter)). |
| `kv-costs` | No | The measured cost of each attention layer in each KV format (`giz ai kvcosts`). Without it, the engine uses the costs measured on Qwen3.8-27B. |
| `tune/` | No | The kernel tunings measured on this machine. They are written on the first run. |
| `conversion.json` | Only in the conversion cache | `{"key": KEY, "bytes": SIZE}`. It is written last. A cached folder is used only when its key is the key asked for and `model.safetensors` has that size. |

A folder of a GGUF file read directly (`FILE.gguf.gizlang/`) holds only `tune/`, `kv-costs` and `drafter/`. It holds
no weights.

### The weight file

`model.safetensors` is a standard [safetensors](https://github.com/huggingface/safetensors) file: an 8-byte
little-endian header length, a JSON header, then the tensor data. Any safetensors reader can list its tensors. Only
this format gives the bytes of a linear their meaning.

#### Metadata

Safetensors metadata values are strings. The header's `__metadata__` holds:

| Key | Value |
| --- | --- |
| `format` | `"gizlang.llm.2"`. A loader of this version refuses every other value and asks you to convert again. |
| `quant` | The text of a JSON object: for each linear's name, its `QuantSpec` text ([QuantSpec](#quantspec)). |
| `plain` | `"true"` when the linears read their inputs as the model computes them (no rotation). It is not there when the inputs are rotated ([Rotation](#rotation)). |

An example (shortened):

```json
{
  "__metadata__": {
    "format": "gizlang.llm.2",
    "plain": "true",
    "quant": "{\"0.in\":\"nv4;bits=4;group=16;scale=e4m3;book=0;rotation=0;layout=lanes;version=1\",\"head\":\"nv4;...\",\"mtp.fc\":\"f8;bits=8;group=128;scale=f16;book=0;rotation=0;layout=lanes;version=1\"}"
  },
  "embed": {"dtype": "BF16", "shape": [...], "data_offsets": [0, ...]},
  "0.n1": {"dtype": "F32", "shape": [5120], "data_offsets": [...]},
  "0.in.codes": {"dtype": "U8", "shape": [...], "data_offsets": [...]}
}
```

Version 1 (`gizlang.llm.1`) had no `quant` key. A loader had to infer each linear's format. Version 2 removed that
reader. Convert a version 1 folder again from its source.

#### Tensor names

Tensors are named by part, not by the source's names. `gizai/llm/arch` (`partsOf`) maps each family's source names
(Qwen3, Qwen3.5 and 3.8, Llama, Gemma 4, LFM2, Qwen3.8 Flash and others) to these names:

| Name | Kind | What it is |
| --- | --- | --- |
| `embed` | Stored | The token embedding, with the dtype and values of its source (a GGUF embedding keeps its block type). |
| `I.n1` … `I.n4` | Small | Layer I's norms: before attention, before the MLP, after attention, after the MLP (only those that the family has). |
| `I.qkv` | Linear | Attention: the query, key and value projections, stacked by rows in that order. |
| `I.o` | Linear | Attention output. |
| `I.qn`, `I.kn` | Small | Query and key norms. |
| `I.bias` | Small | The query, key and value biases, where the family has them. |
| `I.in`, `I.out` | Linear | A linear-attention (Gated DeltaNet) layer's input projections (`qkv`, `z`, `b`, `a`, stacked) and its output. A short-convolution layer (LFM2) uses the same two names. |
| `I.conv`, `I.A`, `I.dt`, `I.gn` | Small | Linear attention's convolution, decay (`A`), time-step bias and gated norm. |
| `I.up` | Linear | The MLP's gate and up projections, stacked. |
| `I.down` | Linear | The MLP's down projection. |
| `I.ls`, `I.li.*` | Small or linear | Gemma 4's layer scalar and per-layer inputs. |
| `norm` | Small | The final norm. |
| `head` | Linear | The output layer. A model with tied embeddings stores it again as a linear. |
| `mtp.fc`, `mtp.ne`, `mtp.nh`, `mtp.I.*`, `mtp.norm` | Linear and small | The multi-token prediction layer, where the source has one. |
| `layer_input.*`, `mix.*`, `I.ah.*`, `I.mh.*`, `I.router`, `I.ple.*` | | The parts of Gemma 4's per-layer inputs and Qwen3.8 Flash's hyper-connections, router and PLE. |

A linear named `NAME` is stored as several tensors `NAME.PIECE` ([Linears](#linears)).

#### Small tensors

Every small tensor (norm, bias, convolution, scalar) is stored as `F32` of its source's element count, flat. A
conversion applies one change, so that the engine reads a value directly:

- A family whose norms scale by `1 + w` (Qwen3.5 and 3.8) stores `1 + w`.
- Linear attention's `A_log` is stored as `-exp(A_log)`.

A source whose changes are already applied keeps its values.

### Linears

#### QuantSpec

Each linear has one `QuantSpec`. Its text is eight fields separated by semicolons, in this order:

```
FORMAT;bits=B;group=G;scale=S;book=N;rotation=R;layout=L;version=V
```

| Field | Values | Meaning |
| --- | --- | --- |
| `FORMAT` | `q2` … `q8`, `bf16`, `f16`, `f8`, `nv4`, `k4`, `k6`, `k8` | The format ([Weight formats](#weight-formats)). |
| `bits` | 2 … 16 | The bits of one code. |
| `group` | 0, 16, 32, 128 | How many inputs one scale covers (0: no scale). |
| `scale` | `f16`, `e4m3`, `k`, empty | The kind of the scales: F16 for each row and block of 128; NVFP4's E4M3 with a scale for each row; a K block's scales and sub-scales; none. |
| `book` | 0 or 2^bits | The size of the codebook (0: no codebook). |
| `rotation` | 0 or 128 | The block of the Hadamard rotation of the inputs (0: none). |
| `layout` | `lanes` | The order of the codes ([Lane order](#lane-order)). |
| `version` | 1 | The version of the layouts. A change to any layout increases it. |

Every format has exactly one valid spec in each version (`specOf`). The loader compares a file's spec with its own and
refuses a different one (`QuantSpec.refusal`). Then it checks each piece's dtype, shape and size against `piecesOf`.

#### The spec of each format

| Format | A weight is | Spec |
| --- | --- | --- |
| `q2` … `q8` | `book[code] × scale`: a code of 2 to 8 bits, an F16 scale for each row and block of 128 inputs, after the rotation | `qB;bits=B;group=128;scale=f16;book=2^B;rotation=128` |
| `bf16`, `f16` | The value | `bf16;bits=16;group=0;scale=;book=0;rotation=128` (`rotation=0` in a plain model) |
| `f8` | FP8 E4M3 × an F16 scale for each row and block of 128 (the block's largest value at 448) | `f8;bits=8;group=128;scale=f16;book=0;rotation=…` |
| `nv4` | E2M1 × the E4M3 scale of its block of 16 inputs × the F32 scale of its row | `nv4;bits=4;group=16;scale=e4m3;book=0;rotation=0` |
| `k4`, `k6` | GGUF Q4_K and Q6_K blocks, kept: `d × a × q − dmin × b` | `k4;bits=4;group=32;scale=k;…` |
| `k8` | GGUF Q8_0, kept: `(code − 128) × scale`, an F16 scale for each 32 inputs | `k8;bits=8;group=32;scale=k;…` |

`nv4` and the K formats never rotate their inputs: their values are kept from the source.

#### Pieces

A linear of `n` rows and `k` inputs is stored as these tensors, in this order. `n` is padded to a multiple of 16. In
the codebook and float formats, `k` is padded to a multiple of 128 (`w` below).

| Format | `NAME.codes` | `NAME.scales` | `NAME.book` | `NAME.subs` |
| --- | --- | --- | --- | --- |
| `qB` | U8 `[n/16, w/64, 128·B]` | F16 `[n/16, w/128, 16]` | F32 `[2^B]`, sorted | — |
| `f8` | F8_E4M3 `[n/16, w/64, 1024]` | F16 `[n/16, w/128, 16]` | — | — |
| `bf16`, `f16` | BF16 or F16 `[n/16, w/64, 1024]` | — | — | — |
| `nv4` (`k` a multiple of 64) | U8 `[n/16, k/64, 512]` | F8_E4M3 `[n/16, k/64, 64]` | F32 `[n]`: each row's scale | — |
| `k4`, `k6` (`k` a multiple of 256) | U8 `[n/16, k/64, 128·bits]` | F16 `[n/16, k/256, 2, 16]`: `d`, `dmin` | — | K4: U8 `[n/16, k/32, 2, 16]`; K6: I8 `[n/16, k/16, 16]` |
| `k8` (`k` a multiple of 128) | U8 `[n/16, k/64, 1024]` (signed byte xor 128) | F16 `[n/16, k/32, 16]` | — | — |

A linear made of several source matrices (`qkv`, `up`, `in`) stacks their rows in the order of [Tensor
names](#tensor-names). The sources must have the same width. Padding rows and inputs hold zeros.

#### Lane order

The first two dimensions of every piece are *chunks*: 16 rows by 64 inputs. A chunk holds exactly the codes that one
warp of matrix cores loads for one tile, in the order of the registers.

**Codebook, float and FP8 formats (`m16n8k16` A fragment).** A chunk is 32 lanes, and each lane holds 32 codes. Lane
`l` (`g = l / 4`, `q = l % 4`) holds the codes of the chunk's four 16×16 tiles. Value `j` (0 … 7) of tile `t` belongs
to row `g + 8·((j >> 1) & 1)` and input `16·t + 4·q + (j & 1) + 2·(j >> 2)`. Code `v = 8·t + j` of a lane starts at bit
`v·bits` of the lane's `4·bits` bytes, little-endian. Float and FP8 values are the codes, 16 or 8 bits wide.
`gizai/quant` (`tileRow`, `tileCol`, `laneSpot`) states the same map.

**NV4 (`m16n8k64` with block scales).** A chunk is 32 lanes of 16 bytes. Lane `l` (`g = l / 4`, `q = l % 4`) holds 4
words. Word `w` holds the E2M1 codes of row `g + 8·(w & 1)`, inputs `32·(w >> 1) + 8·q` to `32·(w >> 1) + 8·q + 7`.
Input `i` of those 8 inputs is in nibble `i`, low nibble first. The chunk's scales are 64 bytes: for each of its 16
rows, the 4 E4M3 scales of its 4 blocks of 16 inputs, in order (the matrix cores' scale word).

#### Rotation

In a rotated model, every linear reads its input rotated in blocks of 128: `x' = H·D·x / √128`. `H` is the
Walsh–Hadamard matrix. `D` is a fixed diagonal of signs, the same for every block of every model (`gizai/quant`
`signs`). The rotation is orthogonal, so the conversion stores each row's blocks rotated in the same way, and
`W·x = (W·D·H/√128)·x'`. It spreads outliers over the block, so that a small codebook is enough.

A model is **plain** (`"plain": "true"`, `rotation=0` in every spec) when any of these is true:

- Its source holds quantized values (FP8, NVFP4, GGUF K blocks), because rotating them would round them again.
- All its linears are asked in a float format, because a rotation then gains nothing.
- The layers are asked in `nv4`.

#### How a source becomes a format

For each linear, `gizai/llm/plan` (`planned`) chooses the spec in this order of preference:

1. A GGUF K block keeps its blocks (`k4`, `k6`, `k8`).
2. In a plain model:
   1. An NVFP4 source keeps its codes and scales (`nv4`).
   2. A float linear asked in a float format keeps its values, rounded to that format.
   3. Every other linear is FP8. When the layers are asked in `nv4`, the layers' linears and the head (if it is asked
      in `nv4`) are NVFP4. The MTP layer stays FP8: its drafts are checked, but fewer of them would be kept.
3. In a rotated model, a linear takes the format that is asked for it: `--head` for the head, `--sensitive` for the
   linears nearest the ends of the layer stack, and `--weights` for the others.


### The drafter

`drafter/` is a converted block drafter (DFlash or DFlash 2: `architectures` starting with `DFlash`). It has the same
format as the model folder: `config.json` as the source has it, and `model.safetensors` with `format`, `quant` and
`plain` metadata. Its tensors are named by `draftParts`: `I.*` for its layers, `fc.J` for each tapped layer's input,
`ctx.kv`, `hn`, `norm`, and `sel.proj`, `sel.pred` and `sel.succ` for a selector. The drafter must fit its model: the
same width, a vocabulary that is not larger, taps within the model's layers, and `num_target_layers` equal to the
model's layer count. Otherwise the load fails. Convert it with `giz ai convert DRAFTER MODEL/drafter`.

### Versions and compatibility

| Change | Effect |
| --- | --- |
| A new `format` (`gizlang.llm.N`) | The loader refuses a folder of another version and asks you to convert again. |
| A new layout `version` in `QuantSpec` | The loader refuses a linear of another version, by name. |
| A new conversion algorithm (a new codebook fit, say) | The conversion cache key changes, so a cached folder converts again. A folder that you placed still loads. |

A converted folder is an output, not a source. Keep the source (the Hugging Face repository at its pinned commit, or
the GGUF file) so that you can convert again when a version changes.

### Check a folder

Any safetensors reader lists the tensors and the metadata. These commands check a folder against this version:

```sh
giz ai chat /models/q27-nv4 "17*23?"   # loads it: every spec and every tensor is checked first
giz ai serve /models/q27-nv4           # the same checks, then the OpenAI API
```

A failed check names the tensor and the reason. For example: `0.in.codes: F8_E4M3 [..] is not U8 [..]`, or
`model.safetensors is not a converted model this version loads (its format: "gizlang.llm.1")`.
