~/fba-lab/lab/qwen-c/02-load-weights

FBALab

Code · architecture · study mode

AboutRoadmapSpeedrun
FBALab

Study mode — no GPU required.

Interactive LLM training & inference lab.

Qwen CAboutContactTermsPrivacyCookiesCommunity

© 2026 FBA Lab

Contact · contact@bubblspace.com · +91 75061 55016

INFERENCE SIMULATION

Qwen3 Pure C — 2 · Load weights

running
◷run_viz.c▸File size◎learner⌁02-load-weights$no GPU
active
weights
Load the model
The 4 GB weight file is mapped into memory in milliseconds. No copying — the OS pages in only what inference needs.
run_viz.c · stage 01–02
load
runtime
Run inference
Your prompt flows through 28 layers of attention and computation, one token at a time.
stage 03–04
forward
output
Generate text
The model scores all 151,936 possible next words and picks one. Then repeats.
stage 05–06
1/6
Blocks
Quick summary

Before the OS can map the weight file into memory, the program needs to know exactly how many bytes to map — this is just measuring the file size without reading a single weight.

Full explanation below the code →

fba-lab — run_viz.c · File sizeexecuting
$ fopen(checkpoint, "rb")$ fseek(SEEK_END); ftell()file_size ≈ 2.4 GBbyte count known — ready for mmap ✓
Explanation

mmap (which we'll see in the next block) requires one critical piece of information before it can work: the exact size of the file in bytes. There's no way to ask "map this file" without saying "map this many bytes of this file." So the program measures first.

The sequence is straightforward — open the file in read-only mode, jump to the very end, ask "where am I?" (that position is the file size), then close it. No data is read, no memory is allocated. For Qwen3-0.6B, this returns roughly 2.4 GB — that number becomes the length argument for mmap.

The fseek/ftell sequence: 1. fopen(checkpoint, "rb") — open in binary read-only mode 2. fseek(file, 0, SEEK_END) — jump to the last byte 3. ftell(file) — return the current offset, which equals the total file size 4. fclose(file) — close it; mmap needs its own separate file descriptor

The file is closed after measuring because mmap needs its own separate file descriptor. We're just getting the dimensions before reserving the space.

Why it matters

mmap requires an exact byte length; ftell gives it without reading a single weight.

Think about

What information does fseek + ftell give us, and why do we need it before calling mmap?

// architecture

Live diagram

100%
GGUF file → mmapGGUF fileon diskfopenfile sizemmapMAP_PRIVATEheader ~5.9 MBtensor blobskip header+ 5951648weights_ptr→ map fnmemory_map_weights — pointer walkptr = (float*)ptcastwclsvocab×dimrms_finaldimembedvocab×dimwk…w1per layerbuild_transformerdoneeach w->field = ptr; ptr += tensor_num_floats
← 1 · Blueprint3 · Buffers →
Step 2 of 10Skill: Model & Memory
01Blueprint02Load weights03Buffers04Forward pass05Tokenizer06Generate loop07GEMV, not GEMM08Every op a kernel09Attention & memory10Prefill vs decode