9. Experimental GGUF export¶
NNx can serialize TransformerNN weights and tokenizer metadata into a
GGUF container.
The writer is useful for inspecting the model with GGUF tooling, archiving a
self-describing artifact, or handing it to a runtime explicitly patched for
NNx.
This support is experimental. NNx writes
general.architecture = "nnx_transformer", which stock llama.cpp, Ollama, and
LM Studio do not implement. A structurally valid GGUF file is not automatically
an executable model for every GGUF consumer.
1. Install¶
Install the upstream Python GGUF writer with the optional extra:
pip install "thekaveh-nnx[gguf-write]"
The extra provides gguf>=0.19.0. Language-model examples also need the lm
extra for the Hugging Face tokenizers package:
pip install "thekaveh-nnx[gguf-write,lm]"
2. Public surface¶
| Symbol | Notes |
|---|---|
nnx.interop.write_gguf |
Write an NNx-tagged .gguf from a TransformerNN and tokenizer. F16, F32, and BF16 are written directly. |
nnx.interop.export_ollama_modelfile |
Generate model.gguf plus a syntactically valid Modelfile. This does not add stock Ollama runtime support. |
nnx.interop.gguf.SUPPORTED_QUANTIZATIONS |
Direct writer modes: ("F32", "F16", "BF16"). |
3. Write and inspect a GGUF¶
from nnx.interop import write_gguf
# `net` is a trained TransformerNN and `tokenizer` is its matching
# NNTokenizerParams.
write_gguf(net, tokenizer, "out/model.gguf") # F16 by default
NNx records model dimensions, RoPE and normalization metadata, tokenizer
metadata, and stable tensor names. The fused QKV projection is split into
attn_q, attn_k, and attn_v; SwiGLU projections are similarly mapped to
separate gate, up, and down tensors.
The writer reads each module's stored weight; it does not call the module. Bake or remove forward-pre-hook reparameterizations before exporting, and export a tied net whose head or embedding is parametrized as an untied copy:
- An unbaked prune mask (
magnitude_prune(bake=False)or atorch.nn.utils.prunemethod) recomputes.weightfromweight_origandweight_maskonly when the module is called. After aload_state_dictor an optimizer step, the stored weight is stale. A masked tiedlm_headhas no GGUF slot of its own, becausetoken_embdholds the unmasked embedding. Bake the masks withtorch.nn.utils.prune.remove, or prune withmagnitude_prune(bake=True). - The legacy
torch.nn.utils.weight_normandspectral_normhooks have the same problem. Remove them withtorch.nn.utils.remove_weight_norm/remove_spectral_norm, or use thetorch.nn.utils.parametrizationsforms, which compute the weight on every read. Export aspectral_normparametrization in eval mode: in training mode, each read takes one more power-iteration step. A norm on a tiedlm_headunties it, removed or parametrized: export an untied copy (next item). - With tied embeddings (
tie_embeddings=True, the default), thelm_headhas no GGUF slot of its own. Readers take it fromtoken_embd, and nooutput.weightis written. The head must therefore still read the embedding's weight. - A
torch.nn.utils.parametrizeparametrization on thelm_headortok_embedweight computes that module's weight. The other module still reads the raw tensor, so the two can disagree. - An
lm_head.weightreassigned to another tensor is refused when its values differ from the embedding's. So is a head left untied byremove_parametrizationswith different values. The file would drop the head's weight. - A distinct tensor with the embedding's exact values exports, as
load_state_dict(assign=True)leaves it. - To keep the outputs the model computes, export an untied copy (below).
Removing a parametrization and re-tying the head
(
net.lm_head.weight = net.tok_embed.weight) also exports. It keeps only one of the two weights, so the exported model's outputs change. - An untied model exports a parametrized head or embedding as its forward reads it.
write_gguf and export_ollama_modelfile refuse such a hook on any module,
and a tied head that no longer reads the embedding's weight, with a
TypeError that names the affected tensors and the fix.
They also refuse PEFT adapters and factorized projections. A refused export
writes no .gguf, model.gguf or Modelfile.
An untied copy of a tied net holds both of its weights, so its file computes
what the net computes. Copy in the weights the net's forward reads (a
parametrized module's .weight is the computed one); net is left as it
was. Remove a legacy norm, and bake a prune mask, first.
import operator
import torch
from nnx import NNTransformerParams, TransformerNN
net.eval() # a spectral_norm parametrization steps on each read in training mode
untied = TransformerNN(
params=NNTransformerParams.from_state({**net.params.state(), "tie_embeddings": False})
)
with torch.no_grad():
for name, tensor in untied.state_dict().items():
tensor.copy_(operator.attrgetter(name)(net))
write_gguf(untied, tokenizer, "out/model.gguf") # output.weight holds the head
The upstream Python reader can inspect the resulting container:
import gguf
reader = gguf.GGUFReader("out/model.gguf")
print(reader.get_field("general.architecture").contents())
print(len(reader.tensors))
4. Quantization¶
| Label | Path |
|---|---|
F32 / F16 / BF16 |
Written directly with the Python gguf package. |
Q8_0 / Q6_K / Q5_K_M / Q4_K_M / other llama.cpp modes |
Write F16 first, then use a compatible llama-quantize binary. |
Build the official llama.cpp tools from source to obtain llama-quantize:
git clone https://github.com/ggml-org/llama.cpp.git
cmake -B llama.cpp/build -S llama.cpp -DLLAMA_BUILD_TESTS=OFF
cmake --build llama.cpp/build --config Release -j
llama.cpp/build/bin/llama-quantize \
out/model.gguf out/model.Q4_K_M.gguf Q4_K_M
Quantization support for a custom architecture depends on the chosen
llama.cpp revision or patch set. write_gguf(..., quantization="Q4_K_M")
raises an ImportError containing the two-step guidance instead of invoking an
external binary implicitly.
5. Architecture compatibility¶
Do not relabel an NNx artifact as architecture="llama" merely to get past a
consumer's architecture check. TransformerNN uses an interleaved RoPE layout,
while stock LLaMA loaders expect the LLaMA split-half convention; metadata and
tensor-shape similarity do not make those computations equivalent. A relabeled
file can load yet produce incorrect output.
Use the default nnx_transformer tag and one of these supported purposes:
- Inspect metadata and tensors with a generic GGUF parser.
- Archive or exchange a self-describing NNx model artifact.
- Load it with a runtime that explicitly implements the NNx architecture and tensor layout.
6. Modelfile bundle generation¶
export_ollama_modelfile generates a GGUF and adjacent Modelfile for testing,
inspection, or a patched Ollama/runtime integration:
from nnx.interop import export_ollama_modelfile
export_ollama_modelfile(
net,
tokenizer,
out_dir="out/ollama_bundle",
system="You are a tiny storytelling model.",
parameters={
"temperature": 0.8,
"top_k": 40,
"top_p": 0.95,
"stop": ["<eos>"],
},
template="{{ if .System }}{{ .System }}\n{{ end }}{{ .Prompt }}",
)
A SYSTEM message reaches the model only through {{ .System }} in the
template, so a template without it drops the system prompt.
The generated directory contains model.gguf and Modelfile; it may also
contain tokenizer.json when the tokenizer was saved there.
The helper validates the Ollama v0.32.2 tagged-source parameter set:
- integer
num_ctx,repeat_last_n,seed,num_predict,draft_num_predict, andtop_k; - finite numeric
repeat_penalty,temperature,top_p, andmin_p; - scalar or repeated string
stop.
Unknown names and mismatched types fail before GGUF generation.
A string parameter value may not contain a double quote, carriage return,
line feed or NUL byte. NNx rejects these rather than escaping them; other
characters pass unchanged. The helper writes the standard FROM, PARAMETER, TEMPLATE, and
SYSTEM directives. Stock ollama create still rejects or cannot execute the
nnx_transformer architecture.
7. Examples¶
| Example | Demonstrates |
|---|---|
examples/17_export_transformer_to_gguf.py |
Write F16 GGUF and inspect it with gguf.GGUFReader. |
examples/18_export_ollama_bundle.py |
Generate and inspect a GGUF + Modelfile bundle while documenting the stock-runtime limitation. |
8. Scope¶
The writer covers NNx's decoder-only TransformerNN. Feed-forward, graph,
convolutional, diffusion, and other NNx networks are outside its scope. GGUF
import back into NNx is also not implemented.