0
Fork 0
mirror of https://github.com/zml/zml.git synced 2026-11-16 19:11:09 +00:00
Any model. Any hardware. Zero compromise. Built with @ziglang / @openxla / MLIR / @bazelbuild https://docs.zml.ai
  • Zig 83.3%
  • C++ 10.5%
  • Starlark 2.8%
  • C 2.5%
  • Python 0.6%
  • Other 0.3%
Find a file
Benjamin Rabier 2accfb1515 docs: add the loader guide and a loading-weights tutorial step
docs/learn/loader.md is the guide to zml.io.Loader: the recipe over a
VFS with Remotes, placing tensors on a mesh or in host memory,
composite tensors built from Source's shape ops, programs bound with
bindProgram, and what each handle borrows and how a load fails. The
errors get a short summary that points at Loader's doc comment, which
holds the contract. The docs index, the README's taste of ZML,
Buffer's doc and the headers of zml/io.zig and zml/io/loader.zig link
it.

write_first_model gains a last step, 'Loading weights': a few lines of
Python write the layer's weight and bias to a .safetensors file, the
layer binds them through a TensorStore view, and main loads them over
a VFS with Remotes through the loader before calling the executable.
The TensorStore paragraph now says the store holds the shapes and the
loader fills the buffers.

zml.io.next goes: nothing in zml or llmd names it any more.

The tutorial's program and every snippet of the guide were built and run
on 4 CPU devices from a scratch target that is not committed: the
tutorial prints {6,11,16} from the Python-written checkpoint, and the
placement (host_mapped included) and composite snippets load the values
they describe. The program snippet stands alone: it defines its sources
and its forward dequantizes by block and places its result as the output
is, without which the load fails with InvalidBinding; on an 8x256 weight
with one scale per row and 128 columns it loads every value as expected,
row-sharded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
History 2026-11-08 07:35:32 +01:00
.github ci: add supported platforms with bumped and audited deps (#967) 2026-10-20 14:06:49 +02:00
.vscode zml: remove hftokenizers (#518) 2026-04-29 18:15:26 +02:00
.zed zml: sharding (#407) 2026-03-10 11:17:57 +01:00
bazel zml/bazel: simplify patchelf execution and fix RPATH removal (#916) 2026-10-20 11:49:41 +02:00
bin/zml-smi zig 0.17 (second attempt) (#923) 2026-10-13 17:11:38 +02:00
docs docs: add the loader guide and a loading-weights tutorial step 2026-11-08 07:35:32 +01:00
dsl zml/moe: CuTe MoE nvfp4 on sm103 (#981) 2026-11-02 14:46:41 +01:00
examples examples: load with zml.io.Loader 2026-11-08 07:35:32 +01:00
ffi zml/profiler: post-process saved xplane profiles and clean up profiler FFI (#522) 2026-05-05 14:52:15 +02:00
mlir dsl/metal: add the Metal DSL (#989) 2026-10-29 12:52:20 +01:00
pjrt zml/io: run binding programs in the loader 2026-11-08 07:35:32 +01:00
platforms zml/io: run binding programs in the loader 2026-11-08 07:35:32 +01:00
stdx zml: delete TensorReader, the DMA allocators, LimitedGroup and the unloadBuffers helpers 2026-11-08 07:35:32 +01:00
third_party zml/moe: CuTe MoE nvfp4 on sm103 (#981) 2026-11-02 14:46:41 +01:00
tools zml/Tensor: explicit sharding in Tensor code (#868) 2026-10-06 15:37:22 +02:00
upb ci: rework pipeline + add remote with buildbuddy 2026-03-23 16:07:40 +01:00
vfs vfs: delete parallel_read and document the remote error contract 2026-11-08 07:35:32 +01:00
zml docs: add the loader guide and a loading-weights tutorial step 2026-11-08 07:35:32 +01:00
.bazelignore workspace: remove custom registry (#531) 2026-05-12 10:32:51 +02:00
.bazelrc ci: remote build (#924) 2026-10-20 09:31:03 +02:00
.bazelversion bazel: update to 9.3.0 (#964) 2026-10-20 09:30:12 +02:00
.editorconfig runtimes/rocm: implement zmlxrocm in Zig (#312) 2025-08-21 18:15:30 +02:00
.gitignore Add devenv files 2026-03-03 17:41:40 +01:00
.nvim.lua workspace: fix typo in nvim config (#927) 2026-10-12 17:55:27 +02:00
AGENTS.md zml/Compiler: remove conditionals from command buffer (#913) 2026-10-08 12:33:34 +02:00
bazel.sh zml: remove examples workspace (#308) 2025-08-19 19:42:50 +02:00
BUILD.bazel rbe: use GCR image registry instead of Docker Hub (#962) 2026-10-19 12:45:42 +02:00
build.zig zml: remove examples workspace (#308) 2025-08-19 19:42:50 +02:00
CONTRIBUTING.md zml: initial release 2024-09-17 11:11:18 +02:00
devenv.lock zml/Sharding: Rename Placement.Shard to Placement (#549) 2026-05-26 09:29:49 +02:00
devenv.nix examples/llm: Common model example (#425) 2026-03-24 16:55:29 +01:00
devenv.yaml Add devenv files 2026-03-03 17:41:40 +01:00
LICENSE zml: initial release 2024-09-17 11:11:18 +02:00
MODULE.bazel workspace: bump ZML plugins and CUDA/ROCm runtimes (#942) 2026-10-16 10:31:34 +02:00
MODULE.bazel.lock third_party/zls: bump to 0.17.0 (#966) 2026-10-20 11:36:07 +02:00
README.md docs: add the loader guide and a loading-weights tutorial step 2026-11-08 07:35:32 +01:00

About

ZML is a production inference stack, purpose-built to decouple AI workloads from proprietary hardware.

Any model, many hardwares, one codebase, peak performance.

Compiled directly to NVIDIA, AMD, INTEL, TPU, Trainium for peak hardware performance on any accelerator. No rewriting.

It is built using the Zig language, MLIR, and Bazel.

Getting Started

Prerequisites

We use bazel to build ZML and its dependencies. The only prerequisite is bazel, which we recommend installing through bazelisk.

macOS

brew install bazelisk

Linux

curl -L -o /usr/local/bin/bazel 'https://github.com/bazelbuild/bazelisk/releases/download/v1.28.0/bazelisk-linux-amd64'
chmod +x /usr/local/bin/bazel

30-Second Smoke Test

Run the MNIST example:

bazel run //examples/mnist

This downloads a small pretrained MNIST model, compiles it, loads the weights, and classifies a random handwritten digit.

LLM Quickstart

The main LLM example is //examples/llm. It currently supports:

  • Llama 3.1 / 3.2
  • Qwen 3.5
  • LFM 2.5

Authenticate with Hugging Face if you want to load gated repos such as Meta Llama:

bazel run //tools/hf -- auth login

Alternatively, set the HF_TOKEN environment variable.

Then run a prompt directly:

bazel run //examples/llm -- --model=hf://meta-llama/Llama-3.2-1B-Instruct --prompt="What is the capital of France?"

Open the interactive chat loop by omitting --prompt:

bazel run //examples/llm -- --model=hf://meta-llama/Llama-3.2-1B-Instruct

You can also load from:

  • a local directory: --model=/var/models/meta-llama/Llama-3.2-1B-Instruct
  • S3: --model=s3://bucket/path/to/model

Running Models on GPU / TPU

Append one or more platform flags when compiling or running:

  • NVIDIA CUDA: --@zml//platforms:cuda=true
  • AMD RoCM: --@zml//platforms:rocm=true
  • Intel OneAPI: --@zml//platforms:oneapi=true
  • Google TPU: --@zml//platforms:tpu=true
  • AWS Trainium / Inferentia 2: --@zml//platforms:neuron=true
  • Disable CPU compilation: --@zml//platforms:cpu=false

Example on CUDA:

bazel run //examples/llm --@zml//platforms:cuda=true -- --model=hf://meta-llama/Llama-3.2-1B-Instruct --prompt="Write a haiku about Zig"

Example on ROCm:

bazel run //examples/llm --@zml//platforms:rocm=true -- --model=hf://meta-llama/Llama-3.2-1B-Instruct --prompt="Write a haiku about Zig"

Example on Intel OneAPI:

bazel run //examples/llm --@zml//platforms:cpu=false --@zml//platforms:oneapi=true -- --model=hf://meta-llama/Llama-3.2-1B-Instruct --prompt="Write a haiku about Zig"

Run Tests

bazel test //zml:test

Examples

A Taste Of ZML

const Mnist = struct {
    fc1: Layer,
    fc2: Layer,

    const Layer = struct {
        weight: zml.Tensor,
        bias: zml.Tensor,

        pub fn init(store: zml.io.TensorStore.View) Layer {
            return .{
                .weight = store.createReplicatedTensor("weight", .{ .d_out, .d }),
                .bias = store.createReplicatedTensor("bias", .{.d_out}),
            };
        }

        pub fn forward(self: Layer, input: zml.Tensor) zml.Tensor {
            return self.weight.dot(input, .d).add(self.bias).relu().withTags(.{.d});
        }
    };

    pub fn init(store: zml.io.TensorStore.View) Mnist {
        return .{
            .fc1 = .init(store.withPrefix("fc1")),
            .fc2 = .init(store.withPrefix("fc2")),
        };
    }

    /// just two linear layers + relu activation
    pub fn forward(self: Mnist, input: zml.Tensor) zml.Tensor {
        var x = input.flatten().convert(.f32).withTags(.{.d});
        const layers: []const Layer = &.{ self.fc1, self.fc2 };
        for (layers) |layer| {
            x = layer.forward(x);
        }
        return x.argMax(0).indices.convert(.u8);
    }
};

pub fn main(init: std.process.Init) !void {
    const allocator = init.gpa;
    const path = ...;

    // Local paths. `zml.io.VFS.Remotes` adds hf://, s3://, gs:// and https://.
    var vfs: zml.io.VFS = try .init(allocator, init.io);
    defer vfs.deinit();
    const io = vfs.io();

    const platform: *zml.Platform = try .auto(allocator, io, .{});
    defer platform.deinit(allocator, io);

    // Returns at once: the loader calibrates while the model is compiled.
    var loader: zml.io.Loader = try .init(allocator, io, platform, .{ .checkpoint = path });
    defer loader.deinit();

    var registry: zml.safetensors.TensorRegistry = try .fromPath(allocator, io, path);
    defer registry.deinit();
    var store: zml.io.TensorStore = .init(allocator, &registry, .{});
    defer store.deinit();
    const model: Mnist = .init(store.view());

    const input: zml.Tensor = .init(.{ 28, 28 }, .u8);
    var exe = try platform.compile(allocator, io, model, .forward, .{input}, .{});
    defer exe.deinit();

    var weights = try zml.mem.bufferize(allocator, &model);
    defer zml.mem.deinitBufferized(allocator, &weights);
    try loader.start(&model, &weights, &store, .none);
    try loader.wait(); // logs "Loaded weights"

    // ... call `exe` on the weights and an image
}

The first start waits for whatever calibration is left. Loading checkpoints gives the full recipe, with remote checkpoints, placement, lifetimes and errors.

For a full walkthrough, see:

Where To Go Next

Contributing

See here.

License

ZML is licensed under the Apache 2.0 license.

Thanks To Our Contributors