h3.c icon indicating copy to clipboard operation
h3.c copied to clipboard

A potential nondeterministic issue

Open yfyang86 opened this issue 3 months ago • 1 comments

I am using this project to enhance other VLM (A Qwen2 variant) and found a potential nondeterministic issue, i.e. the causal-GQA kernel proved nondeterministic under repeated identical dispatches (29/30 runs differed at sequence 662). Fix this will greatly reduce the performance. In my case 47 ms vs 5.5 ms at sequence length of 2,013.

If deterministic is required in some case, some one may find this short patch useful, but it needs a re-design to get better performance.

--- a/h3_shaders.metal	2026-08-20 17:01:10
+++ b/h3_shaders.metal	2026-08-21 10:54:11
@@ -4003,6 +4003,11 @@
         threadgroup_barrier(mem_flags::mem_threadgroup);
     }
     float maximum = reductions[0];
+    /* fix: WAR hazard —  reuses reductions[]; without this
+     * barrier one thread can overwrite reductions[0] before another thread
+     * has read the max, making the softmax nondeterministic at long
+     * sequences (observed: 29/30 identical-input runs differed at seq 662). */
+    threadgroup_barrier(mem_flags::mem_threadgroup);
     float local_sum = 0.0f;
     for (uint key_row = tid; key_row < key_count; key_row += threads) {
         float probability = exp(scores[key_row] - maximum);

yfyang86 avatar Aug 27 '26 02:08 yfyang86

We hit the same race independently and can add three data points.

1. It affects end-to-end video renders, not only long text sequences. On an M5 Ultra, every first-frame (FL2VA) and Ref2VA render differed run to run with identical parameters and seed. The vision span makes the Qwen prompt sequence long, so the race in h3_gqa_causal_bf16 almost always triggers. A plain T2V render with a ~600-token prompt was also irreproducible. Short-prompt T2V happened to be stable, which is why it is easy to miss. We located it by hashing each pipeline stage: conditioning pixels, VAE latents and vision rows matched across runs, and the text context first diverged after layer 0.

2. The video VAE encoder's group norm has the same read-then-overwrite pattern. In h3_vae_encoder_group_norm_silu_f32, mean is read from reductions[0], and the slot is then reused for the variance reduction without a barrier in between:

     float mean = reductions[0] / float(elements);
+    threadgroup_barrier(mem_flags::mem_threadgroup);   /* read before reuse */
     local = 0.0f;

3. On our setup the barrier did not cost measurable speed. With the barrier in both kernels (the h3_gqa_causal_bf16 change is the same as the patch above), a 512² T2V render took 26 s against 24–27 s before. First-frame, Ref2VA and the 600-token T2V prompt now render byte-identically run to run, and the short-prompt T2V output is unchanged. This is end-to-end wall time, so I can't speak to the isolated 47 ms vs 5.5 ms kernel figure above. In a full render, the text encoder is a small part of the total.

Fix (both barriers, plus an H3_DEBUG_HASH=1 per-stage hash that helped find it): https://github.com/ain-soph/h3.c/commit/0fa3634323c8580eb5d0f27dafc6713d0a3e67ae

I'm happy to open a PR with just the two barriers if that's useful.

ain-soph avatar Nov 17 '26 16:11 ain-soph