[ET-VK] Scale softmax workers with the row length and reduce as a tree - #22349
Open
msluszniak wants to merge 2 commits into
Open
[ET-VK] Scale softmax workers with the row length and reduce as a tree#22349msluszniak wants to merge 2 commits into
msluszniak wants to merge 2 commits into
Conversation
softmax_buffer.glsl hardcoded #define NWORKERS 4 #define MAX_NTHREADS 16 and pick_softmax_gwg() matched it with lwg_extents[reduce_dim] = 4u, so four threads normalised a row no matter how long it was. On all-MiniLM-L6-v2 at its published 254-token shape, that is six dispatches of 633 us each: 21.2% of GPU time on an Adreno 840. Widening alone only recovered a quarter of it, because the aggregation after each barrier was a serial walk of all NWORKERS shared entries executed by EVERY thread, not just thread 0 - so its cost grew with the worker count. Replacing both walks (max, then sum) with a shared-memory tree is the larger half of the win. The loop bound is uniform and barrier() sits outside the guard, so every thread still reaches every barrier. all-MiniLM-L6-v2 @254 on an Adreno 840, four interleaved order-reversed rounds, best of 20 executions each: baseline 18.30 ms + workers scaled 17.40 ms + tree aggregation 15.53 ms (-15.1%) softmax itself 3.80 ms -> 2.91 ms. Output matches the XNNPACK build to cosine 0.9999975 and is 60/60 bit-identical across runs. Note that reduce_dim is a WHCN/xyz index while size_at() counts back from the end, hence the -(reduce_dim + 1) in softmax_nworkers(). Buffer storage only; the texture path uses a different shader and grouping scheme and is left alone.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/22349
Note: Links to docs will display an error until the docs builds have been completed.
|
This PR needs a
|
…with Same defect as the reduction change in pytorch#22348. softmax_nworkers() feeds both the NWORKERS specialization constant, which is baked in when the node is built and therefore reflects the dynamic upper bound, and pick_softmax_global_wg_size(), which runs after every resize and sees the actual extent. When they disagree the buffer softmax launches fewer threads than its shared-memory tree reduction indexes over, so the tree folds in slots no thread ever wrote. Static shapes always agree, so this only affects dynamic shapes below the bound. Compute the count once in the node builder and pass it through the resize args. Only the buffer path is affected; the texture path uses a fixed 4 that matches the shader's #define.
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.
softmax_buffer.glslhardcodesand
pick_softmax_gwg()matches it withlwg_extents[reduce_dim] = 4u, so four threads normalise a row no matter how long it is. Onall-MiniLM-L6-v2at its published 254-token shape that is six dispatches of 633 us each: 21.2% of GPU time on an Adreno 840.Widening alone only recovered a quarter of it. The aggregation after each barrier was a serial walk of all
NWORKERSshared entries executed by every thread, not just thread 0, so its cost grew with the worker count and cancelled most of the benefit. Replacing both walks (max, then sum) with a shared-memory tree is the larger half of the win. The loop bound is uniform andbarrier()sits outside the guard, so every thread still reaches every barrier.Measurements
all-MiniLM-L6-v2@254 on an Adreno 840, four interleaved order-reversed rounds, best of 20 executions each:softmax_bufferitself goes 3.80 -> 2.91 ms. Output matches the XNNPACK build to cosine 0.9999975 and is 60/60 bit-identical across runs.Notes
reduce_dimis a WHCN/xyz index (0 = x = last dim) whilesize_at()counts back from the end, hence the-(reduce_dim + 1)insoftmax_nworkers().Buffer storage only. The texture path uses a different shader and grouping scheme and is left alone; it has the same hardcoded constants and is worth a follow-up.
Fixes #22351