Notebooks · lib/gguf/tests/tensors
lib/ gguf/ tests/ tensors.viv
lib/gguf/tests/tensors.viv · 1 code cellOpen in the workbenchDownload .vivLook inside this program
- Atlas
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.
[1]lines 1–47
# lib/gguf/tensors.viv on tests/tiny.gguf; the expected values are gguf-py's # GGUFReader's (tools/gguf/ref.py tensors). Paths are from the repository root. #! file lib/gguf/tests/tiny.gguf #! file lib/gguf/tests/tiny.ref from gguf.tensors import read_gguf, find_tensor, float_rows, f32_values, GGML_NAMES TINY = "lib/gguf/tests/tiny.gguf" # gguf-py's dequantize of each tensor of tiny.gguf, in file order, as binary32s REF = "lib/gguf/tests/tiny.ref" g = read_gguf(TINY) for t in g.tensors { print(t.name, GGML_NAMES[t.ggml_type], t.offset, t.n_bytes) } test "the tensor table gguf-py reads" { assert_true(len(g.tensors) == 8) # general.alignment is 64 in this file; the table ends at 1387, so the # data starts at the next multiple of 64 assert_true(g.alignment == 64 and g.data == 1472) q = g.tensors[find_tensor(g, "t.q4_k")] assert_true(q.dims[0] == 256 and q.dims[1] == 8 and q.n_elems == 2048) # eight Q4_K super-blocks of 144 bytes, 20992 bytes into the data assert_true(q.n_bytes == 1152 and q.offset == 1472 + 20992) assert_true(find_tensor(g, "no such tensor") == -1) } #| What gguf-py's dequantize gives for the tensor called `name`. def ref_values(g, name: Str) -> Arr { k = find_tensor(g, name) n = g.tensors[k].n_elems four: Int = 4 return f32_values(read_bytes(REF, k * n * four, n * four), n) } test "F32, F16 and BF16 tensors, bit for bit as gguf-py loads them" { for name in ["t.f32", "t.f16", "t.bf16"] { t = g.tensors[find_tensor(g, name)] x = float_rows(TINY, t, 0, t.dims[1]) assert_allclose(x, ref_values(g, name), 0.0) } # one row on its own: the second t = g.tensors[find_tensor(g, "t.f16")] assert_allclose(float_rows(TINY, t, 1, 1), ref_values(g, "t.f16")[t.dims[0]:2 * t.dims[0]], 0.0) } # SPDX-License-Identifier: MIT OR Apache-2.0 # SPDX-FileCopyrightText: 2026 The Viv authors