Skip to content

hexagon: improve ARGSORT performance for small tensors - #25512

Merged
max-krasnyansky merged 4 commits into
ggml-org:masterfrom
qualcomm:hexagon-argsort
Jul 10, 2026
Merged

max-krasnyansky merged 4 commits into
ggml-org:masterfrom
qualcomm:hexagon-argsort

Conversation

@max-krasnyansky

Copy link
Copy Markdown
Member

Overview

I was looking at the ggml-hexagon profiles for Gemma-4-26B on X2 Elite and realized that ARGSORT was a major bottleneck. This PR adds an optimized implementation for common sizes 32,64...1024.

Additional information

Details
Profile before

| Op                      | Dims                 | DTypes       | Tot usec  | Avg usec | Count | 
| ----------------------- | -------------------- | ------------ | --------- | -------- | ----- | 
| MUL_MAT_ID              | 2816:1408:128:1 x 28 | q4_0 x f32 x | 429795.00 | 14326.50 | 30    | 
| ARGSORT                 | 128:741 -> 128:741   | f32 -> i32   | 310949.00 | 10364.97 | 30    |  <<<
| MUL_MAT_ID              | 704:2816:128:1 x 704 | q4_0 x f32 x | 263113.00 | 8770.43  | 30    | 
| ADD                     | 2816:741 x 2816:741  | f32 x f32 -> | 67325.00  | 224.42   | 300   | 
| MUL                     | 2816:8:741:1 x 1:8:7 | f32 x f32 -> | 65634.00  | 1093.90  | 60    | 
| FLASH_ATTN_EXT          | 256:741:16:1 x 256:7 | f32 x f16 x  | 64733.00  | 2589.32  | 25    | 
| REPEAT                  | 1:128 -> 1:128:741:1 | f32 -> f32   | 50361.00  | 1678.70  | 30    | 
| MUL_MAT                 | 2816:2112 x 2816:741 | q4_0 x f32 - | 40877.00  | 681.28   | 60    | 
| MUL_MAT                 | 2816:2048 x 2816:741 | q4_0 x f32 - | 33617.00  | 672.34   | 50    | 
| RMS_NORM+MUL            | 2816:741 x 2816:1 -> | f32 x f32 -> | 32772.00  | 155.32   | 211   | 
| MUL_MAT_ID              | 2816:1408:128:1 x 28 | q4_0 x f32 x | 32117.00  | 208.55   | 154   | 
| FLASH_ATTN_EXT          | 256:1:16:1 x 256:768 | f32 x f16 x  | 28154.00  | 216.57   | 130   | 
| MUL_MAT                 | 2816:4096 x 2816:741 | q4_0 x f32 - | 25129.00  | 1005.16  | 25    | 
| MUL_MAT                 | 4096:2816 x 4096:741 | q4_0 x f32 - | 21212.00  | 848.48   | 25    | 
| MUL_MAT+MUL_MAT+MUL_MAT | 2816:2048 x 2816:1 x | q4_0 x f32 x | 19212.00  | 147.78   | 130   | 
| MUL_MAT_ID              | 704:2816:128:1 x 704 | q4_0 x f32 x | 18792.00  | 122.03   | 154   | 
| ARGSORT                 | 128:1 -> 128:1       | f32 -> i32   | 15395.00  | 99.32    | 155   |  <<<

Profile after

| Op                      | Dims                 | DTypes       | Tot usec  | Avg usec | Count |
| ----------------------- | -------------------- | ------------ | --------- | -------- | ----- |
| MUL_MAT_ID              | 2816:1408:128:1 x 28 | q4_0 x f32 x | 472880.00 | 15762.67 | 30    |
| MUL_MAT_ID              | 704:2816:128:1 x 704 | q4_0 x f32 x | 271630.00 | 9054.33  | 30    |
| FLASH_ATTN_EXT          | 256:741:16:1 x 256:7 | f32 x f16 x  | 64072.00  | 2562.88  | 25    |
....
| MUL_MAT                 | 8192:2816 x 8192:741 | q4_0 x f32 - | 8877.00   | 1775.40  | 5     |
| ARGSORT                 | 128:741 -> 128:741   | f32 -> i32   | 8178.00   | 272.60   | 30    | <<<
...
| ARGSORT                 | 128:1 -> 128:1       | f32 -> i32   | 977.00    | 6.30     | 155   | <<<

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes, Antigravity for refactoring and adding generic macros

@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning Hexagon labels Jul 10, 2026
@max-krasnyansky
max-krasnyansky marked this pull request as ready for review July 10, 2026 02:03
@max-krasnyansky
max-krasnyansky requested a review from a team as a code owner July 10, 2026 02:03
@max-krasnyansky

Copy link
Copy Markdown
Member Author

@ggml-org/maintainers may I get the review/approvals please

@max-krasnyansky

max-krasnyansky commented Jul 10, 2026 •

Copy link
Copy Markdown
Member Author

@ggml-org/maintainers may I get the second approval please

@max-krasnyansky
max-krasnyansky merged commit 67776ea into ggml-org:master Jul 10, 2026
29 of 30 checks passed
satindergrewal pushed a commit to satindergrewal/llama.cpp that referenced this pull request Aug 12, 2026
* hex-sort: add efficient bitomic sort in hvx regs up to 1024 elements

* hex-sort: fix inverted vrors

* hex-sort: specialize sort functions for the common cases

* hex-sort: add tracing and local context
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
* hex-sort: add efficient bitomic sort in hvx regs up to 1024 elements

* hex-sort: fix inverted vrors

* hex-sort: specialize sort functions for the common cases

* hex-sort: add tracing and local context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning Hexagon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants