Skip to content

HIP: give top-k a hipCUB path for rows wider than 1024 - #27974

Closed
uncrayon wants to merge 1 commit into
ggml-org:masterfrom
uncrayon:topk-hipcub
Closed

HIP: give top-k a hipCUB path for rows wider than 1024#27974
uncrayon wants to merge 1 commit into
ggml-org:masterfrom
uncrayon:topk-hipcub

Conversation

@uncrayon

@uncrayon uncrayon commented Aug 29, 2026

Copy link
Copy Markdown

Overview

Hey everyone! This is my first contribution, so please, be as hard as needed for me to learn.

The problem that this PR attack is quite easy to solve but for AMD Users (I run my models in an Evo-X2 128G Ubuntu).
When using DFlash2 for Qwen 3.8 27B Q8 my computer was using the CPU and GPU when using HIP, but not with Vulkan. Looking in my logs I realized that top-k was falling back to the CPU, which copies the whole logits tensor to the host on every call!

Looking in HF I found this blob that took me to learn what hipCUB is and therefore I tried to apply it here. For me was just as simple to do an if.

test-backend-ops -o TOP_K on gfx1151 (the Halo chip) goes from 159 OK / 347 unsupported to 441 OK / 0 unsupported / 0 failures, and the DFlash2 draft graph drops from 3 splits to 1. On this host it does not change DFlash2 throughput: 25.52 vs 25.62 tok/s, with byte-identical draft/accept counters.

Additional information

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. I was not familiar fully with the backend so I was guided by claude/opus-5-ultracode and code review by openAI/chatgpt-5.6-sol-ultra.

The HIP backend has no CUB, so the shared-memory bitonic argsort is the only
top-k path and supports_op caps rows at 1024 elements. Vocabulary-width top-k
therefore falls back to the CPU, which copies the whole logits tensor to the
host on every call. DFlash2's candidate selector hits this on every draft step:
k = 16 over 248,320 logits.

Adds a hipCUB DeviceSegmentedRadixSort path for wide rows, behind
GGML_HIP_TOPK_HIPCUB (on when hipCUB is found), and lifts the width limit in
supports_op for top-k only. ARGSORT keeps its existing limit.

test-backend-ops -o TOP_K on gfx1151 goes from 159 OK / 347 unsupported to
441 OK / 0 unsupported / 0 failures, and the DFlash2 draft graph drops from
3 splits to 1. On this host it does not change DFlash2 throughput: 25.52 vs
25.62 tok/s, with byte-identical draft/accept counters.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@uncrayon
uncrayon requested review from a team and IMbackK as code owners August 29, 2026 17:06
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Aug 29, 2026
@ggml-gh-bot

ggml-gh-bot Bot commented Aug 29, 2026

Copy link
Copy Markdown

Hi @uncrayon, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@ggml-gh-bot ggml-gh-bot Bot added the draft PR will be changed to draft by github-actions bot label Aug 29, 2026
@github-actions
github-actions Bot marked this pull request as draft August 29, 2026 17:11
@github-actions github-actions Bot removed the draft PR will be changed to draft by github-actions bot label Aug 29, 2026
@uncrayon
uncrayon marked this pull request as ready for review August 29, 2026 21:14
@IMbackK

IMbackK commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

dupe of #26592

@IMbackK IMbackK closed this Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants