Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/npu.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,10 @@ image cache, P packed (W written for the next serve) in S s`. `ONEBIT_HEAD_IMAGE
the cache off (nothing is read or written). The files are `kd × nd` bytes plus `nd` scales per
tile, so a head costs about `kpad × vocabulary` bytes on disk per kernel set; nothing prunes them,
the same as the device-form copies next to them. Ternary (`pb16i`) heads and the gemv head do not
use it: their image already is a file. The behaviour is pinned host-side in
`tests/npu_design_grid_test.c` with a counter on the gather.
use it: their image already is a file. Each writer builds the file under its own name
(`<file>.tmp.<pid>`) and renames it into place, so two serves that start cold on the same pack both
finish their write and a reader only ever sees a whole image. The behaviour is pinned host-side in
`tests/npu_design_grid_test.c` with a counter on the gather and a two-process write arm.

**Q8 has two code layouts under the same chunk size.** Qwen3.5-4B's lm_head (tied to the
embedding) stores code (row r, column k) at byte `k*32 + r`. Qwen3.6-35B-A3B's Q8 tensors
Expand Down
14 changes: 13 additions & 1 deletion npu/forward/src/q4nx_head_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

#include <stdio.h>
#include <string.h>
#if !defined(_WIN32)
#include <unistd.h>
#endif

// "Q4HI" then the version as four ASCII digits, so a hexdump of the first eight bytes says what the file is.
#define Q4NX_HEAD_IMAGE_MAGIC "Q4HI"
Expand Down Expand Up @@ -68,8 +71,17 @@ int q4nx_head_image_write(const char *path, const int8_t *codes, const float *sc
h.hash = q4nx_head_image_hash(codes, scales, kd, nd);
// Written beside the destination and renamed into place: a truncated image must never be readable as a valid one,
// because the reader would then copy it into a weight BO and the model would answer wrongly with no message.
// The temp name carries the writer's pid: two serves that start cold on the same pack write the same image at the
// same time, and with one shared name the second open truncated the first writer's file under it and one of the two
// renames found nothing to rename. Each writer now renames its own complete file; the last rename wins, and both
// files are the same image.
char tmp[4096];
if (snprintf(tmp, sizeof tmp, "%s.tmp", path) <= 0) return 0;
#if !defined(_WIN32)
const int tn = snprintf(tmp, sizeof tmp, "%s.tmp.%ld", path, (long)getpid());
#else
const int tn = snprintf(tmp, sizeof tmp, "%s.tmp", path);
#endif
if (tn <= 0 || (size_t)tn >= sizeof tmp) return 0;
FILE *f = fopen(tmp, "wb");
if (!f) return 0;
int ok = fwrite(&h, sizeof h, 1, f) == 1 && fwrite(codes, (size_t)kd * (size_t)nd, 1, f) == 1 &&
Expand Down
42 changes: 42 additions & 0 deletions tests/npu_design_grid_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -599,6 +601,46 @@ static void test_head_image(void) {
"head image: a truncated image is refused");
remove(path);
}
/* (e) TWO WRITERS, ONE IMAGE. Two serves that start cold on the same pack write the same file at the same
* time. Each must end with a successful write and the path must read back intact after every one of them:
* with one shared temp name the second open truncated the first writer's file and one rename found nothing. */
{
const int bk = 2048, bn = 2048; /* 4 MiB, so a write is long enough for the two to overlap */
int8_t* big = malloc((size_t)bk * bn);
int8_t* big_back = malloc((size_t)bk * bn);
float* bsc = malloc((size_t)bn * sizeof(float));
float* bsc_back = malloc((size_t)bn * sizeof(float));
check(big && big_back && bsc && bsc_back, "head image: the two-writer fixture allocates");
if (big && big_back && bsc && bsc_back) {
for (size_t i = 0; i < (size_t)bk * bn; i++) big[i] = (int8_t)((i * 31u + 7u) % 251u - 125);
for (int n = 0; n < bn; n++) bsc[n] = 0.001f * (float)(n + 1);
const char* shared = "head_image_two_writers.bin";
remove(shared);
const int rounds = 40;
fflush(stdout);
const pid_t child = fork();
if (child == 0) {
int bad = 0;
for (int i = 0; i < rounds; i++) bad += q4nx_head_image_write(shared, big, bsc, bk, bn) != 1;
_exit(bad > 255 ? 255 : bad);
}
int bad_w = 0, bad_r = 0;
for (int i = 0; i < rounds; i++) {
bad_w += q4nx_head_image_write(shared, big, bsc, bk, bn) != 1;
bad_r += q4nx_head_image_read(shared, big_back, bk * bn, bsc_back, bn, &info, why, (int)sizeof why) != 1;
}
int st = 0, bad_c = -1;
if (child > 0 && waitpid(child, &st, 0) == child && WIFEXITED(st)) bad_c = WEXITSTATUS(st);
check_int(bad_w, 0, "head image: with a second writer on the same path, every write of ours lands");
check_int(bad_c, 0, "... and so does every write of the other process");
check_int(bad_r, 0, "... and the path reads back intact after each of them");
check(q4nx_head_image_read(shared, big_back, bk * bn, bsc_back, bn, &info, why, (int)sizeof why) == 1 &&
memcmp(big, big_back, (size_t)bk * bn) == 0,
"... and what is left is the image both wrote");
remove(shared);
}
free(big); free(big_back); free(bsc); free(bsc_back);
}
}
free(bt); free(codes); free(back); free(scales); free(sc_back);
}
Expand Down
Loading