You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[MLX] Make SamplingHead directly exportable; drop sampler wrapper; wire runtime top-k (#20612)
Summary
SamplingHead.forward now takes the sampling params as trailing
positional args (temperature, top_k, top_p, seed), so torch.export
drives it directly and the per-model _MLXSampleWrapper is removed. As
part of going positional, top_k becomes a runtime input threaded through
export, run.py, and the C++ engine; qwen now supports per-request top-k,
and the op-level "not implemented" rejections are dropped.
Changes
- backends/mlx/llm/sampling.py — SamplingHead.forward(self, *args)
unpacks *model_args, temperature, top_k, top_p, seed; samples (B) from
(B, vocab). No wrapper needed (the params are positional, so it's
directly exportable).
- examples/models/qwen3_5_moe/export.py — delete _MLXSampleWrapper;
model = SamplingHead(model); top_k added to example_args; dynamic_shapes
nested to mirror the single *args parameter.
- examples/models/qwen3_5_moe/run.py — _sampling_scalars feeds top_k
(<=0 → keep-all maxint); --top-k flag; the non-sample guard rejects
top_k/top_p/seed.
- backends/mlx/test/test_ops.py, test_sample.py — fixtures call
SamplingHead positionally, baking _KEEP_ALL_TOP_K/_TOP_P_OFF constants
for params they don't expose as runtime inputs.
- examples/models/qwen3_5_moe/main.cpp — --top_k flag →
SamplingConfig.top_k.
- examples/models/qwen3_5_moe/qwen35_moe_engine.cpp — top_k scalar wired
through prefill/decode (fed between temp and top_p, matching the new
forward order); 0→INT64_MAX keep-all mapping; the two "top_k is not
implemented" rejections removed.
Testing
- Re-exported qwen, greedy output matches a pre-refactor baseline,
seeded runs are reproducible, and --top-k restricts sampling.
- MLX sample op tests pass; test_sample.py passes (incl. top-k
end-to-end).
- C++ MLX runner builds and runs the sample path; seeded reproducible
and --top_k works on-device.
0 commit comments