Skip to content

[EtsyML] Device-resident TRIE advance+mask kernel#2

Open
orsonadams wants to merge 1 commit into
oadams/native-trie-guided-decodingfrom
oadams/trie-device-kernel
Open

[EtsyML] Device-resident TRIE advance+mask kernel#2
orsonadams wants to merge 1 commit into
oadams/native-trie-guided-decodingfrom
oadams/trie-device-kernel

Conversation

@orsonadams

Copy link
Copy Markdown
Owner

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

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant