Repository navigation
Conversation
Signed-off-by: Aarush Shrikanth <aarush.shrikanth@gmail.com>
ammesatyajit
self-requested a review
October 27, 2026 00:33
ammesatyajit
approved these changes
Oct 27, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #816.
When running a quantized (2-bit or 3-bit) model on Metal, the first token outputted is garbage, while running the same model on CPU or a quantized 4-bit model on Metal work properly.
When the final prefill token is being processed on 2/3-bit quantization (with
prefill_consistentset), it callscactus_metal_encode_quant_matmul_m, which rejects 2/3-bit quantization so the matmul falls back to CPU (ascactus_metal_encode_quant_matmul_monly accepts 4-bit quantization). Because the final RMS_NORM in the fused kernel (rule 3) running before the matmul was labeled -2 (covered by cluster),metal_liveis not set and therefore Metal is not synced before the CPU matmul is run. As a result, the CPU reads from the output buffer of the RMS_NORM node before it had been written to. Chunked prefill is not affected by this issue because it is multi-row, which doesn't trigger rule 3 and the existing norms run as normal Metal nodes (-1) and properly triggermetal_live. Additionally, decode is not affected becauseprefill_consistentis set to false, so it usescactus_metal_encode_quant_matmul, which is able to run on Metal for 2/3-bit quantized models.Solution
Modified one line in
metal_plan.cpp, specifying that when rule 3's fused kernel includes the next norm (c.u0 == 1), that node (c.a5) is marked as action = -3 (read outside cluster) and therefore triggersmetal_liveto be set. Followed the standard set by other existing rules (12, 16) where afteradd_clustersucceeds, covered (-2) nodes read outside their cluster are marked -3.Verification
parity_cpu_reader_after_fused_norm()unit test incactus-graph/tests/test_metal_parity.cpp.rms_norm -> add_clipped -> view -> rms_norm -> reshape -> view -> matmul). It then forces the final matmul toComputeBackend::CPUand runs that graph on both CPU and Metal and confirms the outputs are within tolerance.Notes
parity_cpu_reader_after_fused_norm,c.check()was not used because it does not properly handle NaNs (as comparisons involving NaNs always output false), causing the test to always pass even without the fix (I am happy to make a PR fixing this too)cactus_metal_encode_quant_matmul_monly accepting 4-bit quantization (not addressed here)