Skip to content

[codex] Fix Kunlun Qwen2.5 greedy inference#476

Draft
zhangyue207 wants to merge 1 commit into
mainfrom
codex/kunlun-qwen25-7b-inference
Draft

[codex] Fix Kunlun Qwen2.5 greedy inference#476
zhangyue207 wants to merge 1 commit into
mainfrom
codex/kunlun-qwen25-7b-inference

Conversation

@zhangyue207

Copy link
Copy Markdown
Collaborator

Summary

This PR fixes the InfiniLM-side issues found while bringing up Qwen2.5-7B-Instruct single-card inference on Kunlun XPU.

Changes

  • Propagate CLI dtype into LLM / InferEngine / HF config loading so --dtype float32 and other overrides are honored.
  • Convert floating safetensors to the requested dtype during model load.
  • Map the Python-side kunlun device string to the underlying InfiniCore CUDA-compatible device mapping used by the Kunlun backend.
  • Use int32 position IDs from the basic LLM processor for Kunlun RoPE compatibility.
  • Replace the generic Qwen2 attention and MLP fused linears with explicit q_proj/k_proj/v_proj and gate_proj/up_proj modules, keeping checkpoint parameter names direct.
  • Fix static attention RoPE by applying Q RoPE in-place instead of writing into a non-contiguous permuted output tensor.
  • Make VideoNSA follow the updated common Attention members.
  • Add a Kunlun greedy decoding fallback: when top_k == 1, copy logits to CPU and compute argmax there to avoid the incorrect Kunlun random sampling result.

Root Cause

The main correctness issue was in static attention: Q RoPE wrote into a non-contiguous permuted tensor. Embedding and input RMSNorm matched Hugging Face exactly, but layer 0 attention diverged immediately. Switching Q RoPE to the in-place single-tensor path made layer 0 match Hugging Face within about 1e-6 and restored the expected first generated token.

Kunlun sampling also selected the wrong token even when logits were correct, so greedy decoding now uses a CPU argmax fallback for top_k=1 on Kunlun.

Validation

Validated with Docker image fixing:v1, local InfiniCore/InfiniLM builds, and model /workspace/models/Qwen2.5-7B-Instruct.

  • Built and installed _infinilm.
  • CPU FP32 full prompt matched Hugging Face first-token reference:
    • top token 114602
    • score about 31.068
    • decoded token 昆仑
  • Kunlun FP32 single-card max-new-tokens=1:
    • CPU argmax token 114602
    • score about 31.06
    • response 昆仑
  • Kunlun FP32 single-card max-new-tokens=2:
    • response 昆仑芯

Runtime note: the verified path uses DISABLE_XPYTORCH=1 to avoid loading an incompatible XPU runtime from torch_xmlir. FP32 decode is currently slow, about 29 seconds per generated token in this setup.

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