Skip to content

[NPU] NZ for non-quantized MOE, Qwen3 MOE double memory consumption fix#15904

Merged
iforgetmyname merged 57 commits intosgl-project:mainfrom
OrangeRedeng:memory_and_nz_fix
Jan 28, 2026
Merged

[NPU] NZ for non-quantized MOE, Qwen3 MOE double memory consumption fix#15904
iforgetmyname merged 57 commits intosgl-project:mainfrom
OrangeRedeng:memory_and_nz_fix

Conversation

@OrangeRedeng
Copy link
Copy Markdown
Contributor

@OrangeRedeng OrangeRedeng commented Dec 26, 2025

Motivation

The part of closed PR #11984.
Adding weight conversion from ND to FRACTAL_NZ speeds up the GroupedMatmul kernel

Modifications

  1. Add NZ conversion for non-quantized MOE models (11% speedup an average), update NZ documentaion
  2. Move transpose(1,2) from forward_npu() to process_weights_after_loading()
  3. Fix double memory consumption bug for Qwen MOE models on ascend, remove old workaround

Accuracy Tests

NZ acceleration:

image

Benchmarking and Profiling

NZ acceleration:

Server:

python -m sglang.launch_server --model-path ./Qwen3-30B-A3B --port 30088 --trust-remote-code --tp-size 4 --mem-fraction-static 0.8 --attention-backend ascend --device npu --quantization modelslim --base-gpu-id 0 --cuda-graph-bs 64 --disable-radix-cache

Client:

python -m sglang.bench_serving --backend sglang --random-range-ratio 1.0 --dataset-path /data/data/ShareGPT_Vicuna_unfiltered/ShareGPT_V3_unfiltered_cleaned_split.json --dataset-name random --flush-cache --num-prompts * --max-concurrency * --random-input-len * --random-output-len * --host 127.0.0.1 --port 30088

image

Checklist

@github-actions github-actions bot added the quant LLM Quantization label Dec 26, 2025
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @OrangeRedeng, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the efficiency of Mixture-of-Experts (MoE) models, particularly Qwen3, by integrating NPU-specific optimizations for weight handling and introducing lazy loading for expert weights. These changes aim to reduce memory consumption and improve performance when running MoE models on NPU hardware, ensuring more efficient resource utilization during model initialization.

Highlights

  • NPU Support for MoE: Introduced support for Neural Processing Units (NPUs) in non-quantized Mixture-of-Experts (MoE) models by applying NPU-specific fractal format casting to expert weights (w13_weight and w2_weight) to optimize memory layout.
  • Qwen3 MoE Memory Optimization: Implemented lazy initialization for the routed_experts_weights_of_layer in Qwen3 MoE models, preventing immediate memory allocation and addressing potential double memory consumption issues during model loading.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces two main changes: adding NPU support for non-quantized Mixture-of-Experts (MoE) layers and fixing a memory consumption issue in Qwen3 MoE models. The NPU support is well-implemented by casting weights to the required NPU format within a conditional block. The memory fix correctly uses LazyValue to defer the loading of expert weights, which should effectively address the double memory consumption problem. The changes are clear and well-targeted. I have one suggestion to improve code clarity by replacing a magic number with an existing enum.

@github-actions github-actions bot added documentation Improvements or additions to documentation npu labels Jan 13, 2026
@ping1jing2
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@iforgetmyname iforgetmyname merged commit b77b0ff into sgl-project:main Jan 28, 2026
246 of 258 checks passed
charlesHsuGG pushed a commit to charlesHsuGG/sglang that referenced this pull request Jan 30, 2026
…ix (sgl-project#15904)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Chen-0210 pushed a commit to Chen-0210/sglang that referenced this pull request Jan 30, 2026
…ix (sgl-project#15904)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
dsingal0 pushed a commit to dsingal0/sglang that referenced this pull request Feb 1, 2026
…ix (sgl-project#15904)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
sfiisf pushed a commit to sfiisf/sglang that referenced this pull request Feb 5, 2026
…ix (sgl-project#15904)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Johnsonms pushed a commit to Johnsonms/sglang that referenced this pull request Feb 14, 2026
…ix (sgl-project#15904)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deepseek documentation Improvements or additions to documentation npu quant LLM Quantization run-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants