[EtsyML] Device-resident TRIE advance+mask kernel#2
Open
orsonadams wants to merge 1 commit into
Open
Conversation
Replace the per-step host machinery of the per-beam TRIE constraint
(gatherTree of beam histories, host trie re-walk over all beams, per-beam
bitmask staging, ~1 MB H2D) with GPU-resident state advanced by a single
fused kernel.
- New kernels/trieMask.{h,cu}: one block per (request, beam) advances the
trie node via the decoder's parent pointers,
node[b] = child(nodeStatePrev[parent[b]], token[b]), reading only the
latest (beam-aligned) token position, builds the node's allowed-token set
as a shared-memory bitmask from the flat CSR, and applies -inf. Node state
is double-buffered so the parent gather reads pre-reshuffle state.
- GuidedTrie::uploadToDevice() mirrors the CSR trie on the GPU once.
- GuidedDecoder::execute() takes the decoder parent/token tensors and runs
the kernel; the build() TRIE branch becomes a no-op; the per-step
postProcessRequest(streamingGather) is removed from forwardAsync.
Outputs are byte-identical to the host path (exact-diff at beam 8 and 128);
beam=128 640/640 valid and 128 unique, beam=1 5/5, beam_threshold 17/128 OK.
Cuts per-beam TRIE overhead ~30% at beam=128 (26.2 -> 21.7 ms/query).
xgrammar path unchanged.
Signed-off-by: Orson Adams <oadams@etsy.com>
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.
Stacked on the native-TRIE PR. Replaces the per-step host machinery (gatherTree + host trie re-walk + per-beam bitmask staging + ~1 MB H2D) with GPU-resident state advanced by a single fused kernel.
kernels/trieMask.{h,cu}: one block per (request, beam) advances the trie node via the decoder's parent pointers (node[b] = child(nodeStatePrev[parent[b]], token[b])), builds the allowed set as a shared-mem bitmask from the CSR, applies -inf. Double-buffered for the parent gather.GuidedTrie::uploadToDevice();build()TRIE branch becomes a no-op; per-step gather removed.Byte-identical outputs to the host-staging path (exact-diff at beam 8/128). Cuts per-beam overhead ~30% at beam=128 (26.2 -> 21.7 ms/query); the residual is beam-search top-k, analyzed here: https://docs.google.com/document/d/1l6SqxLiypYP06v5ehACeYy47_0HZl6XYbrc3tmJNVzs/edit
Feature overview: https://docs.google.com/document/d/1OsciNfXKMkCgfpI-8jkN7qo_yeLnidknRbGQPvCIBlw/edit