Skip to content

Tune quant dispatch - #2031

Merged
awni merged 1 commit into
mainfrom
tune_quant
Apr 3, 2025
Merged

awni merged 1 commit into
mainfrom
tune_quant

Conversation

@awni

@awni awni commented Apr 1, 2025

Copy link
Copy Markdown
Member

Tune quant dispatch conditions to qmv vs qmm.

The optimal is both machine and matrix size dependent unfortunately so this is only an approximation. It's conservative in that we continue to route to the qmm unless it's pretty clear that there is a speedup to be had.

In the plots below:

  • x-axis is batch size
  • y-axis is time in milliseconds
  • dashed line is qmm
  • solid line is qmv

Here's a benchmark comparing the two at size = 4096 on a few machines:

Screenshot 2025-04-01 at 8 08 00 AM

Another benchmark for just M2 Ultra for 3 different sized matrices:

Screenshot 2025-04-01 at 8 09 12 AM

Same benchmark on M3 Max:

Screenshot 2025-04-01 at 8 09 35 AM

@awni
awni requested a review from angeloskath April 1, 2025 15:28
@awni

awni commented Apr 1, 2025 •

Copy link
Copy Markdown
Member Author

For Mistral 7B batched we get a nice speedup on M2 Ultra especially at small batch sizes:

Batch size Pre ms Post ms
6 31.971 19.400
8 31.817 25.431
10 31.416 31.274

Benchmark:

import time
import mlx.core as mx
from mlx_lm import load

model, _ = load("mlx-community/mistral-7B-Instruct-v0.3-4bit")

for batch_size in [6, 8, 10]:
    prompt = mx.array([[0] * batch_size])

    for _ in range(10):
        mx.async_eval(model(prompt))

    tic = time.time()
    for _ in range(10):
        mx.async_eval(model(prompt))
    toc = time.time()
    ms = 1000 * (toc - tic) / 10
    print(f"{ms:.3f}")

@awni
awni requested a review from barronalex April 1, 2025 15:30

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! I doubt that the limits transfer from qmv_fast to qmv and qmv_quad but I think these are also rare enough that it wouldn't matter too much.

@awni

awni commented Apr 1, 2025

Copy link
Copy Markdown
Member Author

It's true I didn't tune for those at all. I just checked qmv and on m4 max the crossing point is 14 (for size == 4352), so close to qmv_fast. I'm gues quad will be quite different. If anything I would expect the crossing point to be further out since the matrices are probably quite small with that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants