Skip to content

Add GPU bitmask expansion for constrained sampling - #141

Merged
stikves merged 3 commits into
apple:mainfrom
stikves:sukru/constrained-part2-gpu
Aug 4, 2026
Merged

Add GPU bitmask expansion for constrained sampling#141
stikves merged 3 commits into
apple:mainfrom
stikves:sukru/constrained-part2-gpu

Conversation

@stikves

@stikves stikves commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

MPSGraph subgraph that expands packed Int32 xgrammar bitmasks into Float16 logits masks on Metal GPU.

Blocked tokens get -65504 (Float16 min), ensuring they are never selected by argmax or topK.

Both MPSGraphArgmaxSampler and MPSGraphCompositeSampler gain:

  • bitmaskBuffer (lazy): shared-memory MTLBuffer, allocated on first use
  • constrainedExecutable (lazy): compiled graph with bitmask input, built on first applyBitmask: true call
  • encode(..., applyBitmask:) overloads on all encode paths

Existing protocol methods and behavior unchanged.

Part 2 of 4 for GPU-based constrained sampling (#114).

MPSGraph subgraph that expands packed Int32 xgrammar bitmasks into
Float16 logits masks entirely on GPU. Blocked tokens get -65504
(Float16 min), ensuring they are never selected by argmax or topK.

Both MPSGraphArgmaxSampler and MPSGraphCompositeSampler gain:
- bitmaskBuffer (lazy): shared-memory MTLBuffer, allocated on first use
- constrainedExecutable (lazy): compiled graph with bitmask input,
  built on first applyBitmask: true call
- encode(..., applyBitmask:) overloads on all encode paths

Zero overhead for unconstrained generation — no bitmask buffer
or constrained graph is compiled until the first constrained call.

Existing protocol methods and behavior unchanged.

Part 2 of 4 for GPU-based constrained sampling (apple#114).
@stikves
stikves merged commit af98e4d into apple:main Aug 4, 2026
3 checks passed
@stikves
stikves deleted the sukru/constrained-part2-gpu branch August 4, 2026 16:25
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.

2 participants