Open
Conversation
Contributor
Author
|
cc @guschmue |
xenova
commented
Mar 5, 2026
Co-authored-by: Joshua Lochner <admin@xenova.com>
Contributor
|
this gets stuck on ut, ie TopKOperator.SmallArrayTopKSorted. |
Contributor
Author
|
Was able to reproduce. Sure, let me update |
Contributor
Author
|
Opus helped cook up a bitonic sort, and I just had to get it working with some edge cases (e.g., with duplicate values). |
Contributor
|
'lintrunner -a' please :) |
Contributor
Author
|
lintrunner installation seems to be broken in my env :/ I'll just apply the patch manually |
Contributor
Author
Contributor
|
BigArrayBigTopKSorted unit test is failing. |
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.

Description
This PR adds WebGPU support for TopK. Needed by several onnx-community models, like https://huggingface.co/onnx-community/Trinity-Nano-Preview-ONNX (which I converted and have been testing recently).
Motivation and Context
While testing onnx-community/Trinity-Nano-Preview-ONNX in Transformers.js, I noticed it was exceptionally slow... achieving only ~8-10 tps on my M4 Max. After a bit of profiling, it became obvious that the bottleneck was the CPU <-> GPU memory copies (MemcpyToHost)... happening before and after each TopK op (needed by the model to correctly select expert... not taken into account by the current QMoE op because Trinity-Nano uses a sigmoid scoring function, while other QMoE models like GPT-OSS use softmax scoring functions, taken into account in the op itself).
The algorithm is basically copied exactly from the QMoE TopK implementation. Maybe we can deduplicate some code here by templating?
Summary of PR:
PROFILING BEFORE:
PROFILING AFTER: