Skip to content

[MLX] Gemma4-31B ondevice sampling#20561

Draft
kiymetakdemir wants to merge 1 commit into
pytorch:mainfrom
kiymetakdemir:gemma-ondevice-sampling
Draft

[MLX] Gemma4-31B ondevice sampling#20561
kiymetakdemir wants to merge 1 commit into
pytorch:mainfrom
kiymetakdemir:gemma-ondevice-sampling

Conversation

@kiymetakdemir

Copy link
Copy Markdown
Contributor

Summary

Lets the MLX-exported Gemma 4 31B model sample the next token on-device instead of returning logits for host-side sampling. Sampling is opt-in at export (--sample); temperature, top_p, and seed are runtime inputs, and the runner increments the seed per token.

Changes

  • export.py --sample flag wraps the model so forward(tokens, input_pos, temperature,
    top_p, seed) → int64 token; records a use_sampling constant-method flag. Non-sample export unchanged.
  • gemma4_31b_engine.cpp reads use_sampling from metadata; when set, consumes the int64 token directly instead of logits_to_token, feeds the scalar inputs in prefill/decode (across the min/max prefill chunking), and manages the per-token seed schedule. top_k is still rejected; top_p is range-checked to (0, 1]; top_p/seed are rejected on non-sample models.
  • main.cpp --top_p / --seed flags wired into SamplingConfig; an unset seed is randomized
    only for sampling models (non-sample keep seed 0, so they don't trip the guard).
  • tests/test_mlx_pipeline.py adds test_export_to_pte_with_sampling: tiny-model MLX export with --sample, asserting the use_sampling flag, int64 token output, and same-seed reproducibility.

@pytorch-bot

pytorch-bot Bot commented Jun 27, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/20561

Note: Links to docs will display an error until the docs builds have been completed.

❌ 2 Pending, 2 Unclassified Failures

As of commit 373b79d with merge base 825bd30 (image):

UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 27, 2026
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant