{"slug": "fucina-a-cpu-first-tensor-autograd-library-in-zig", "title": "Fucina: a CPU-first tensor/autograd library in Zig", "summary": "Matteo Grella released Fucina v0.1.0, a CPU-first tensor/autograd library written in pure Zig 0.16, featuring compile-time axis names in tensor types that make misaligned contractions a compile error. The library supports eager execution for both inference and training, with optional BLAS and Metal/CUDA offload, and includes implementations of LLM inference (Qwen3, DeepSeek, GLM, Gemma), speech-to-text, TTS, and a guitar amp simulator, all validated against reference implementations.", "body_md": "Fucina: a CPU-first tensor/autograd library in Zig\n\nHi all. For the past months I’ve been building Fucina (Italian for “forge”), a tensor/autograd library written in pure Zig 0.16. Whenever I try a new language, I somehow go through the same routine and doing the same thing \"I’ll just play around with it a bit” and then, before I know it, I’m trying to build a whole library. And now Fucina is at the point where I almost like it, and it can be consumed as an ordinary package, so I’d like to show it here.\n\nPer the forum’s policy, the disclosure: this project is developed using agentic coding tools, with me owning (“controlling”) the ideas, the architecture decisions, and also writing code first hand (I feel like I don’t really know how to write code the way I used to but I’m so glad Zig itself is written entirely by hand).\n\nThe part that I think is genuinely interesting for this forum is what comptime does to tensor code, even though I’ve already had feedback from Zig programmers who don’t really see the benefit at all, that is, axis names are part of the type: `Tensor(.{ .batch, .in })`\n\nis a different type from `Tensor(.{ .in, .batch })`\n\n, contraction is by axis name, the result’s tag set is computed at compile time, and a misaligned contraction is a compile error rather than a runtime crash. The tags (and the rank) exist only in the type system and compile away entirely:\n\n``` js\nfn forward(ctx: *ExecContext, m: *const Model, x: *const Tensor(.{ .batch, .in })) !Tensor(.{ .batch, .class }) {\n    var z1 = try x.dot(ctx, &m.w1, .in); // contract .in -> .{ .batch, .h1 }\n    defer z1.deinit();\n    // ...\n}\n```\n\nExecution is eager (no explicit graph object), and the same forward code runs for inference and training. There is no C/C++ build system and no Python runtime dependency; BLAS providers and a Metal/CUDA GEMM (matmul operations) offload are opt-in build options.\n\nUsing it:\n\n```\nzig fetch --save git+https://github.com/matteo-grella/fucina#v0.1.0\n```\n\nTo develop and test the library against real workloads I decided to implement LLM inference and serving (Qwen3 dense and MoE, DeepSeek, GLM, Gemma, and others), speech-to-text, TTS and a guitar amp simulator. Each is validated against its reference implementation. These applications are not the library itself, and they will eventually move to their own repos but so far has been very handy having all in a single repo.\n\nThere is also an entirely AI generated book-length course in the docs, “Forging Deep Learning in Zig”, where I asked an LLM pretending to rebuild the library from scratch reversing engeneering my development journey and the big REFERENCE (also this AI generated and updated).\n\nThe API is pre-1.0 and will surely change. Feedback I’d particularly value from this community are API ergonomics of the tagged tensor surface, the package consumption experience, anything that feels that feels not idiomatic Zig (I tried to follow the std) but also come up with a lot of things from my previous Go project, plus some influence from Fortran.\n\nAlso, I’d love feedback if you can execute and benchmark the implemented deep learning models, particularly LLMs, on your computers. Thank you!\n\nThe toolchain is pinned to 0.16.0 exactly.\n\nRepo: [GitHub - matteo-grella/fucina: CPU-first, eager-execution tensor/autograd runtime and LLM inference engine written in pure Zig · GitHub](https://github.com/matteo-grella/fucina)\n\nReference: [Fucina Reference - Fucina](https://matteo-grella.github.io/fucina/docs/reference/)", "url": "https://wpnews.pro/news/fucina-a-cpu-first-tensor-autograd-library-in-zig", "canonical_source": "https://ziggit.dev/t/fucina-a-cpu-first-tensor-autograd-library-in-zig/17273#post_1", "published_at": "2026-08-17 15:54:33+00:00", "updated_at": "2026-08-17 16:10:59.109567+00:00", "lang": "en", "topics": ["machine-learning", "developer-tools"], "entities": ["Matteo Grella", "Fucina", "Zig", "Qwen3", "DeepSeek", "GLM", "Gemma", "GitHub"], "alternates": {"html": "https://wpnews.pro/news/fucina-a-cpu-first-tensor-autograd-library-in-zig", "markdown": "https://wpnews.pro/news/fucina-a-cpu-first-tensor-autograd-library-in-zig.md", "text": "https://wpnews.pro/news/fucina-a-cpu-first-tensor-autograd-library-in-zig.txt", "jsonld": "https://wpnews.pro/news/fucina-a-cpu-first-tensor-autograd-library-in-zig.jsonld"}}