mlx-quantization-metal-kernels: fix batched inputs in host dispatch#1038
Closed
fenilsonani wants to merge 1 commit into
Closed
mlx-quantization-metal-kernels: fix batched inputs in host dispatch#1038fenilsonani wants to merge 1 commit into
fenilsonani wants to merge 1 commit into
Conversation
set_strides passed the scales/biases row stride as the batch stride, so batch element z read scales shifted by z rows (only element 0 correct). Also fix affine_qmv batch/row dispatch (batch dims are ndim-2, rows are covered by grid.x), enforce row-contiguous matrix dims, and collapse dense batched inputs into a single 2D GEMM (the grid.z path costs ~Bx at M=1; lm_head [32,1,K]: 60ms -> 2.2ms). Batch-1 outputs stay bitwise identical to the shipped builds; verified on torch 2.10 and 2.13. Adds batched tests. Fixes huggingface/transformers#47331
|
Hi @fenilsonani, thanks for your interest in contributing! This project requires that pull request authors are vouched, and you are not in the list of vouched users. This PR will be closed automatically. See https://github.com/huggingface/kernels-community/blob/main/CONTRIBUTING.md for more details. |
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.
Related issue
huggingface/transformers#47331 (batched generate with MetalConfig silently corrupts every sequence after the first). @SunMarc asked there for the fix to happen kernel side instead of a transformers workaround so this is that.
Closes # huggingface/transformers#47331
What does this PR do?
fixes batched inputs in the mlx-quantization-metal-kernels host dispatch. the set_strides helper passes the row stride of scales/biases where the kernel expects the batch stride so batch element z reads scales shifted by z rows and past the end of the tensor for high rows that's why row 0 is always correct and the rest is garbage. i verified this before changing any code by predicting the garbage output exactly with row shifted scales and it matched the actual kernel output to 0.4%.
Motivation
any batched generate through transformers MetalConfig on mps is silently wrong today and the fix belongs in the kernel package so every consumer gets it not just transformers. testing also surfaced that affine_qmv batched was separately broken (host passed ndim-1 batch dims but the kernel expects ndim-2 and reads M from x_shape[batch_ndim] which was out of bounds and rows are covered by tid.x which the grid never set) and that the batched grid.z path pays a full 32 row tile per batch element so [32,1,K] decode on a lm_head sized weight was 60ms vs 2.2ms as a flattened gemm.
Changes
Testing
Checklist