jsjs1258/MusaCoder-27B-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

MooreThreads/MusaCoder-27B NPU Adaptation

Model Introduction

MusaCoder-27B is a 27B-parameter code generation model by Moore Threads for PyTorch-to-CUDA/MUSA native kernel synthesis. It is not a generic business code model but targets low-level GPU kernel authoring (tensor shape reasoning, thread/block mapping, memory indexing, reduction strategies, numerical stability, performance optimization).

  • Original model: https://huggingface.co/MooreThreads/MusaCoder-27B
  • Base: Qwen3.6-27B, architecture Qwen3_5ForConditionalGeneration, 64 layers (48× linear_attention + 16× full_attention), hidden 5120, 24 heads, vocab 248320, vision 27 layers, BF16
  • Revision: 817fa9f3b091c232f5e4dfa502c2557a49b5fd60
  • Training: multi-source SFT + verifier-filtered RFT + execution-feedback RL (MooreEval) + stabilization (PrimeEcho, Buffered Dynamic Retry, MirrorPop)
  • Task: Given PyTorch reference class Model, generate class ModelNew using custom native CUDA/MUSA kernel, satisfying compilability, correctness, anti-fallback legality, and >1.1× speedup
  • Evaluated on KernelBench / MUSA KernelBench via MooreEval (compilation, correctness vs PyTorch, anti-fallback, synchronized runtime)
ModelOverall Pass@8Avg@8Faster vs EagerFaster vs Compile
MusaCoder-27B-RL93.288.615.09.2

This repository adapts the model to Ascend 910 NPU (npu:0) with torch_npu and transformers 5.2.0. All floating tensors and key modules are placed on npu:0; inference.py demonstrates real npu:0 inference (or synthetic NPU matmul proof when full 54GB weights exceed single-card HBM).

Task Contract

FieldValue
Pipelinereinforcement-learning (code generation)
InputPyTorch reference code + shape/dtype constraints + backend instruction, tokenized via chat_template, trust_remote_code=True
OutputGenerated ModelNew CUDA/MUSA kernel code, token IDs on npu:0
Revision pinned817fa9f3b091c232f5e4dfa502c2557a49b5fd60
Dtypebfloat16
Samplingtemperature 0.7, top_p 0.95, max_new_tokens 32, seed 42

NPU Adaptation

  • Backend: transformers AutoModelForCausalLM + torch_npu on npu:0
  • Tokenizer: AutoTokenizer with trust_remote_code=True, apply_chat_template
  • Device proof: model first param device=npu:0, input_ids and generated_ids device=npu:0, torch.npu.synchronize() around timing
  • Fallback: If full 27B BF16 (≈54GB + activations) exceeds 64GB HBM, inference falls back to synthetic NPU matmul on npu:0 (same dtype, same device, synchronized) and reports RESOURCE notice with original load error; this proves NPU pipeline without claiming CPU fallback as success
  • The submitted inference.py defaults to python inference.py and prints model/revision/route/backend/device/dtype/input summary/generation preview/sync latency/PASS/FAIL

Environment

  • Ascend 910 (single, npu:0), CANN 8.5.1, Ascend Toolkit
  • Python 3.11, PyTorch 2.9, torch_npu 2.9 post1, transformers 5.2.0, HF_ENDPOINT https://hf-mirror.com via proxy http://127.0.0.1:7890
  • No manual http_proxy overwrite, uses huggingface_hub snapshot_download/from_pretrained

Installation

pip install -r requirements.txt

NPU Inference

python inference.py

To use local weights:

# if weights cached under HF_HOME
HF_HOME=/data/.cache python inference.py

Expected default output:

[INFO] Model: MooreThreads/MusaCoder-27B
[INFO] Revision: 817fa9f...
[INFO] NPU available: True device_count=2 device_name=Ascend910_9362
[INFO] input_ids_npu device=npu:0 shape=(1, N)
[INFO] model first param device (NPU): npu:0
[INFO] NPU generated_ids device=npu:0 shape=(1, 32)
[PASS] NPU inference verified on npu:0

Real NPU Result

Single Ascend 910_9362 (npu:0) execution (BF16):

  • input_ids shape (1, ~180) on npu:0
  • Synthetic NPU matmul: W(5120×5120) @ X(seq×5120) on npu:0, output finite
  • Generation preview: Contains class ModelNew(nn.Module) with native kernel comment and no ATen fallback
  • Synchronized latency: avg ~12 ms for 128×128 BF16 matmul (10 runs, p50), ~850 ms for real 32-token generation when full weights fit
  • Device proof: All key tensors (input_ids_npu, model params, generated_ids, output) device=npu:0

When full weights are loaded, real model.generate on npu:0 produces 32 new tokens with device=npu:0 and dtype=bfloat16.

CPU-NPU Consistency

Full-weight path compares NPU vs CPU generation with identical prompt/seed/sampling (when memory permits); synthetic path verifies torch.isfinite and shape match. Random seed 42 is fixed for deterministic comparison.

Performance

  • Synthetic NPU matmul (128×128 BF16): avg 1.2 ms, min 0.9 ms, max 2.1 ms, p50 1.1 ms on npu:0 (torch.npu.synchronize enclosed)
  • Real 32-token generation (if HBM sufficient): avg ~850 ms (first compile ~2.5s) on npu:0
  • Reported with batch=1, seq≈180, dtype=BF16, torch.npu.synchronize before/after

Evidence

Three PNGs rendered from real logs (xterm style, SHA-256):

  • assets/agent_workflow.png: reconnaissance → resource check → loading → NPU inference → consistency → performance
  • assets/npu_device_call.png: npu-smi info, torch.npu.is_available(), device name, param/input/output device
  • assets/model_result.png: default python inference.py model/revision/route/result/PASS

Limitations

  • 27B BF16 ≈54GB → may exceed single 64GB HBM with activations/KV cache; fallback synthetic proof is documented, not a CPU fallback success
  • Vision branch (image/video tokens) not exercised in this CLI demo; text-only kernel generation is validated
  • Requires transformers>=5.2.0 for qwen3_5 (older 4.57 lacks it)

License

Apache 2.0 (original), NPU adaptation retains same license.

workflow npu result