Skip to content

Commit c802754

Browse files
0songHan0songHanxmhubj
authored
[Kunlunxin] Fix duplicate generation (#332)
<!-- Copyright 2026 FlagOS Contributors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> ### PR Category Vendor ### PR Type Bug Fixes ### Description This PR fixes the duplicate generation issue on Kunlunxin by patching the sampler RNG implementation to ensure consistent sampling behavior during inference. ### Related Issues N/A ### Changes * Patch the sampler RNG implementation for Kunlunxin. * Fix the duplicate generation issue during model inference. * Keep the implementation transparent to existing inference workflows. ### Testing Validated on Kunlunxin hardware with: * Qwen3.5-35B-A3B * Qwen3.6-27B Verified that the duplicate generation issue is resolved on both models. ### Checklist * [x] I have run the existing tests and they pass * [ ] I have added tests for my changes (if applicable) * [ ] I have updated the documentation (if applicable) Co-authored-by: 0songHan <345279710@qq.com> Co-authored-by: XMing <xmhubj@gmail.com>
1 parent e049130 commit c802754

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

  • vllm_fl/dispatch/backends/vendor/kunlunxin

vllm_fl/dispatch/backends/vendor/kunlunxin/patch.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,46 @@ def apply_kunlunxin_patches():
3232
patch_fused_gdn_gating()
3333
patch_op_cls()
3434
patch_ssm_cache_update()
35+
patch_sampler_rng()
3536
logger.info("Applied all Kunlunxin patches")
3637

3738

39+
# ── sampler RNG (TP-consistent unseeded sampling) ──
40+
def patch_sampler_rng():
41+
try:
42+
import torch
43+
import vllm.v1.sample.ops.topk_topp_sampler as _sampler_mod
44+
45+
def _tp_consistent_random_sample(probs, generators):
46+
q = torch.empty_like(probs)
47+
if len(generators) != probs.shape[0]:
48+
q.uniform_()
49+
q = -torch.log(1-q)
50+
q = q.clamp(min=1e-12)
51+
if generators:
52+
# TODO(woosuk): This can be slow because we handle each request
53+
# one by one. Optimize this.
54+
if os.getenv("FAST_RANDOM_SAMPLE") == "1":
55+
for i, generator in generators.items():
56+
q[i].uniform_(generator=generator)
57+
q = -torch.log(1-q)
58+
q = q.clamp(min=1e-12)
59+
else:
60+
for i, generator in generators.items():
61+
q[i].uniform_(generator=generator)
62+
q[i] = -torch.log(1-q[i])
63+
q[i] = q[i].clamp(min=1e-12)
64+
65+
return probs.div_(q).argmax(dim=-1).view(-1)
66+
67+
_sampler_mod.random_sample = _tp_consistent_random_sample
68+
logger.info(
69+
"Patched sampler random_sample for TP-consistent sampling on Kunlunxin"
70+
)
71+
except Exception as e:
72+
logger.warning("Failed to patch sampler RNG: %s", e)
73+
74+
3875
# ── causal_conv1d ──
3976
def patch_causal_conv1d():
4077
"""Replace causal_conv1d_fn / causal_conv1d_update with Kunlunxin impls.

0 commit comments

Comments
 (0)