ai newsDi., 1. Dez.
EilmeldungThe Model Proposes, Your Code Disposes: A Tool-Calling Agent Loop for Android with Gemini Nano

LLM Inference in Pure Assembly: How alesha-pro/llm.asm Runs LLMs Without Python/C++ Runtime

A 7B-parameter model doesn't need a 2 GB Python environment to emit one token — it needs a matrix-vector product, a softmax, and somewhere to park the KV cache. alesha-pro/llm.asm implements exactly that in hand-written x86-64 NASM, and pulled 4,175 stars in under seven days. TL;DR: llm.asm is a fr

dev.toDienstag 00:00 · 01.12.4 Min. Lesezeit

A 7B-parameter model doesn't need a 2 GB Python environment to emit one token — it needs a matrix-vector product, a softmax, and somewhere to park the KV cache. alesha-pro/llm.asm implements exactly that in hand-written x86-64 NASM, and pulled 4,175 stars in under seven days.

TL;DR: llm.asm is a from-scratch transformer inference engine in pure x86-64 assembly: no Python, no C++ runtime, no BLAS, no libc dependency — just Linux syscalls, mmap 'd weights, and AVX2/FMA kernels for attention and FFN. You get a tiny static binary and near-zero cold start. You give up GPU execution, tuned prefill throughput, and portability. Read it as a reference implementation of what inference actually requires, not as a llama.cpp replacement.

The pain isn't model quality, it's the dependency substrate. A minimal "just run inference" stack today is Python 3.11 + torch (CUDA-tagged wheel, ~2.5 GB) + transformers + safetensors + tokenizers, and the failure modes are almost never mathematical. They're libcudnn version drift, ABI mismatches between a Rust tokenizer wheel and your glibc, or a container image that takes six minutes to pull before the first token appears.

For edge boxes, CI smoke tests, air-gapped hosts, and serverless cold paths, that overhead dominates. Decode-phase inference for a single sequence is memory-bandwidth bound, not compute bound — you're streaming weights through the memory hierarchy and doing a GEMV per layer. The orchestration layer contributes nothing to that loop, yet it's most of your artifact size and all of your supply-chain surface.

Strip inference to its syscall-level reality and the graph is short:

argv / stdin (prompt bytes) | v +---------------------------+ | tokenizer: BPE merges | flat sorted tables, binary search | (mmap'd vocab + merges) | -> token id array in.bss buffer +---------------------------+ | v +---------------------------+ weights: one mmap, MAP_PRIVATE | embedding lookup | <-- page-aligned, no malloc, no copy +---------------------------+ | v +==========================================+ | N x transformer block (loop in asm) | | | | RMSNorm -> rsqrt + vfmadd reduction | | | | | Q/K/V GEMV -> AVX2 int8 dot products | | | | | RoPE -> sincos pairs, in-register | | | | | attention: Q.K^T / sqrt(d), mask, | | softmax,.V (KV cache read) | | | | | O proj GEMV | | | | | residual add | | | | | RMSNorm -> SwiGLU FFN (up/gate/down) | | | | | residual add | +==========================================+ | ^ | | KV cache: contiguous per-layer v | [layer][head][pos][dim] arena +---------------------------+ | final norm + lm_head GEMV | +---------------------------+ | v logits -> argmax / temp+top-p -> token id -> write(1,...) | +---> loop back into block stack until EOS or max_tokens Enter fullscreen mode Exit fullscreen mode Component notes worth stealing:

; illustrative int8 GEMV inner loop (unrolled x4 in the real kernel). loop: vmovdqu ymm0, [ rsi ]; activations (broadcast/quantized) vpmaddubsw ymm1, ymm0, [ rdi ]; int8 * uint8 -> int16 pairs vpmaddwd ymm1, ymm1, [ ones ]; int16 pairs -> int32 vpaddd ymm8, ymm8, ymm1; accumulate, no horizontal op here add rsi, 32 add rdi, 32 sub rcx, 32 jnz. loop Enter fullscreen mode Exit fullscreen mode KV cache layout is a correctness and speed decision. Contiguous [layer][head][pos][dim] means attention scores read sequential memory; a naive [pos][layer] layout thrashes TLB entries at long context. Softmax uses max-subtraction for stability and a polynomial exp approximation — no libm call, no denormal surprises.

Quick Start (Copy-Paste Setup) Requires Linux x86-64 with AVX2 + FMA ( grep -o 'avx2\|fma' /proc/cpuinfo | sort -u ). Check the repo README for exact target names — the shape is:

sudo apt-get install -y nasm binutils make git clone https://github.com/alesha-pro/llm.asm.git cd llm.asm make # nasm -f elf64... && ld -static -o llm file llm # ELF 64-bit, statically linked, no interpreter ldd./llm # "not a dynamic executable" <- the point ls -lh llm # tens of KB, not GB # fetch/convert weights per repo docs, then:./llm --model./models/model.bin --prompt "Explain KV caching:" --tokens 128 Enter fullscreen mode Exit fullscreen mode Sanity-check that the hot loop is what you think it is:

objdump -d llm | grep -c vfmadd # FMA density perf stat -e cycles,instructions,cache-misses./llm --model... --tokens 64 Enter fullscreen mode Exit fullscreen mode If cache-misses is low and IPC is modest, you're bandwidth-bound — expected for batch-1 decode.

Use it to learn, to embed, and to benchmark the floor. Don't migrate your production serving fleet to it.

📡 Daily open-source AI drops I curate finds like this one in VibeNode AI on Telegram: t.me/vibenode_ai — one repo per day, plus Claude Code hooks and ready-to-import n8n JSON workflows. Search #Drop06 inside the channel for this write-up's companion notes.

Discussion: if batch-1 decode is bandwidth-bound anyway, how much of the modern inference stack is genuinely earning its complexity — and what would you keep if you had to rebuild it from syscalls up?

Quelle: dev.to (dev.to). Original öffnen