Skip to content

*Major T/s improvement* Use the Metal qmatmul MM kernels #2615

Merged
greenrazer merged 7 commits into
huggingface:mainfrom
EricLBuehler:metal_qmatmul_mm_upstreaming
Jul 18, 2025
Merged

*Major T/s improvement* Use the Metal qmatmul MM kernels #2615
greenrazer merged 7 commits into
huggingface:mainfrom
EricLBuehler:metal_qmatmul_mm_upstreaming

Conversation

@EricLBuehler

Copy link
Copy Markdown
Member

This PR adds the automatic usage of Metal GGML quantized mat-mat kernels instead of always using the mat-vec kernels and upstreams a few related/necessary changes.

Before this change, Candle's Metal decoding performance was on-par with MLX and llama.cpp but the prompt performance was insufficient. After this change, the prompt performance (on the benchmark) was increased to a factor of about 2.5x faster than MLX and within 10% of llama.cpp - a performance boost by a factor of almost 6x.

This PR switches to only using the MV kernels when D::Minus2 of the xs input tensor is equal to 1. This mirrors the logic in GGML.

Besides utilizing the MM kernels, this PR also upstreams some required changes:

EricLBuehler and others added 5 commits November 14, 2024 14:13
* Add GGUF bf16 type support

* Add non avx impl for vec_dot_bf16

* Fix from_u32

* Fix loading

* Fix dequant of bf16
* Update kernels for metal bf16

* Fix typo

* Check if have bfloat
* Test passes

* All tests pass

* Now all the tests really pass

* Try out always using mm

* Mirror llama.cpp metric

* Mirror llama.cpp metric

* Update test
@EricLBuehler

Copy link
Copy Markdown
Member Author

@LaurentMazare if you could review, that would be great!

More benchmarks with some smaller models can be found here: EricLBuehler/mistral.rs#903 (comment)

@lucasjinreal

Copy link
Copy Markdown

Why is this still not close/?

@ghost

ghost commented Apr 2, 2025

Copy link
Copy Markdown

Without merging this MR, is candle still slower than llama.cpp/ggml right now? Or has this improvement already been implemented in other code submissions?

@EricLBuehler

EricLBuehler commented Apr 2, 2025

Copy link
Copy Markdown
Member Author

@null-define without this, Candle Metal prompt performance is significantly reduced. This is because we aren't using the specialized Matrix-Matrix kernels, instead using Matrix-Vector kernels repeatedly which is slower.

@lucasjinreal

Copy link
Copy Markdown

wondering why it isn't merged into main? Does candle is now not maintained well?

@greenrazer

Copy link
Copy Markdown
Contributor

This is an amazing improvement!

After testing across 11 GGUF LLMs, the new code is 73% faster than the current version, exceeding llama.cpp speeds on my M3 Max.

M3 Max GGUF Candle Benchmarking

Data
  • Candle (CPU): 2.58 avg tokens/sec
  • Candle (Metal): 27.07 avg tokens/sec
  • MLX: 62.96 avg tokens/sec
  • Llama.cpp: 82.78 avg tokens/sec
  • Candle (Metal) + PR 2615: 100.60 avg tokens/sec
Computer Specs
  • M3 Max
  • 36GB RAM
  • Mac OS 15.3.2 (24D81)

@LaurentMazare What would it take to get this merged?

@lucasjinreal

Copy link
Copy Markdown

@meg-huggingface Please consider merge it

@AlpineVibrations

AlpineVibrations commented Apr 23, 2025

Copy link
Copy Markdown

wow. this sounds amazing. we sure could use any speed boost we can get on metal. This original PR is from almost 5 months ago. Why is there no discussion as the reason its not merged yet?

@AlpineVibrations

Copy link
Copy Markdown

@LaurentMazare would this help with inference speed of Metal for Flux and SD3 image generation?

@lucasjinreal

Copy link
Copy Markdown

The candle team seems abundant in this lib?

@AlpineVibrations

Copy link
Copy Markdown

just checking in again on this hanging PR. is there anyone out there that can review? do we need to do it different or fix something? thanks

@AlpineVibrations

Copy link
Copy Markdown

@LaurentMazare sorry to bug you but is there someone else we can ping to get this approved or at least some comment on why it's still sitting here for so many months? thanks

@lucasjinreal

Copy link
Copy Markdown

I think HuggingFace abandoned the Candle project.

@AlpineVibrations

Copy link
Copy Markdown

is that real?

@lucasjinreal

Copy link
Copy Markdown

I think it is now mainly community driven, and the core developers are lazy at merging new features, not even supporting new features, such as many low level ONNX ops. I couldn't see any response or support for it.

@AlpineVibrations

Copy link
Copy Markdown

maybe they should add some more admins that have merge authority. it seams like there are many people ready to work.

@greenrazer

Copy link
Copy Markdown
Contributor

LGTM

@greenrazer greenrazer merged commit 1ef1341 into huggingface:main Jul 18, 2025
9 checks passed
john-sharratt pushed a commit to john-sharratt/candle that referenced this pull request May 7, 2026
…#2615)

* Add GGUF BF16 support (huggingface#17)

* Add GGUF bf16 type support

* Add non avx impl for vec_dot_bf16

* Fix from_u32

* Fix loading

* Fix dequant of bf16

* Update kernels for metal bf16 (huggingface#19)

* Update kernels for metal bf16

* Fix typo

* Check if have bfloat

* Sync ggml metal kernels (huggingface#33)

* Metal qmatmul mat-mat product (huggingface#39)

* Test passes

* All tests pass

* Now all the tests really pass

* Try out always using mm

* Mirror llama.cpp metric

* Mirror llama.cpp metric

* Update test

* Update test

* fixed merge error

---------

Co-authored-by: keighbee <kb@huggingface.co>
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.

4 participants