Fix TL2 kernel batch output stride bug causing NaN for batch_size > 1#521
Open
vladimirmoushkov wants to merge 2 commits intomicrosoft:mainfrom
Open
Fix TL2 kernel batch output stride bug causing NaN for batch_size > 1#521vladimirmoushkov wants to merge 2 commits intomicrosoft:mainfrom
vladimirmoushkov wants to merge 2 commits intomicrosoft:mainfrom
Conversation
The generated TL2 LUT kernels (three_qgemm_lut_* and two_qgemm_lut_*) wrote all batch elements' results to C[i] instead of C[bs*out_stride+i], so each batch element overwrote the previous one. This caused NaN/garbage output for any multi-token prompt evaluation (batch_size > 1). Only single-token generation (batch_size=1) worked correctly. Fix: - Add `int out_stride` parameter to all generated kernel functions - Use `C[bs * out_stride + i]` indexing for output writes - Pass `m` (ne01) from ggml_qgemm_lut dispatcher as the stride - Add NaN guard in per_tensor_quant: prevent 127/0 division when max activation is zero - Add NaN guard in scale application: handle zero act_scales safely The nan-guards-ggml.patch contains corresponding defensive fixes for quantize_row_i8_s() in ggml-quants.c and the I2_S scale application paths in ggml.c (submodule: 3rdparty/llama.cpp). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Build script for Windows x86_64 targeting Intel Alder Lake (12700K) with AVX2 support. Includes TL2 kernel generation, NaN patch application, and usage instructions for CLI, interactive, and server modes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@vladimirmoushkov please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
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.
Summary
Fixes a critical bug in the TL2 LUT kernel codegen where multi-token prompt
evaluation (batch_size > 1) produces NaN/garbage output.
Bug
In
utils/codegen_tl2.py, the generatedthree_qgemm_lut_*andtwo_qgemm_lut_*functions write all batch elements to
C[i]instead ofC[bs * out_stride + i],overwriting previous batch results. Single-token generation works; any multi-token
prompt crashes with NaN.
Fix
utils/codegen_tl2.py: Addedint out_strideto kernel signatures,fixed output indexing to
C[bs * out_stride + i], passmas stride from dispatcherper_tensor_quant()now handles division by zero when max activation is 0nan-guards-ggml.patch: Defensive fixes forquantize_row_i8_s()and I2_Sscale application in the submodule
Testing