-
Notifications
You must be signed in to change notification settings - Fork 306
Linearize gpt_oss model and add separate example to qunatize it to w4a8 #2091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @isharif168, 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 introduces new modeling utilities to enable the quantization of GPT-OSS Mixture-of-Experts (MoE) models. It provides a mechanism to transform the MoE architecture from a fused-expert design to a sequential, per-expert structure, which is a crucial step for applying certain quantization techniques. The changes aim to improve compatibility and efficiency when compressing these advanced language models. Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this 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 a new file, src/llmcompressor/modeling/gpt_oss.py, to facilitate the quantization of GPT-OSS models by patching their Mixture-of-Experts (MoE) layers into a more quantization-friendly sequential format. The overall implementation is solid, but I've identified a few areas for improvement regarding code quality, correctness, and performance. My feedback focuses on removing unused imports, ensuring CUDA-specific code is handled safely, replacing magic numbers with constants, and optimizing the MoE forward pass for better efficiency. Please review the detailed comments for specific suggestions.
| import os | ||
| from transformers import AutoModelForCausalLM, AutoTokenizer | ||
|
|
||
| from llmcompressor import oneshot | ||
| from llmcompressor.utils.dev import skip_weights_initialize | ||
| from llmcompressor.modifiers.quantization import QuantizationModifier |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are several unused imports in this file: os, AutoModelForCausalLM, AutoTokenizer, oneshot, and QuantizationModifier. These should be removed to keep the code clean and maintainable.
| import os | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| from llmcompressor import oneshot | |
| from llmcompressor.utils.dev import skip_weights_initialize | |
| from llmcompressor.modifiers.quantization import QuantizationModifier | |
| from llmcompressor.utils.dev import skip_weights_initialize |
|
👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review. Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed. |
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
- Add gpt_oss_20b_example.py which does the convert and quantization - Clean up the gpt_oss.py from the test code Signed-off-by: Sharif Inamdar <[email protected]>
91089fe to
201d06c
Compare
| del m | ||
| if to_delete: | ||
| gc.collect() | ||
| try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this script expected to be used on GPUs? Has it been tested on GPU?
|
|
||
| dtype = gup.dtype | ||
| parent, child_name = _get_parent_and_child(model, name) | ||
| top_k = int(max(1, min(_get_top_k(model.config) or 1, E))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why cant topk just be defined as per the original definition https://github.com/huggingface/transformers/blob/390dca67e554b2b8f131064d4b6d991bf3ab3105/src/transformers/models/gpt_oss/modeling_gpt_oss.py#L154
| x = hidden_states.reshape(-1, H) | ||
|
|
||
| # Use the original router (it returns scores and indices already softmaxed over top-k) | ||
| router_scores, router_indices = self.router(x) # scores: [tokens, E], indices: [tokens, k] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original definition of the router, returns router logits https://github.com/huggingface/transformers/blob/390dca67e554b2b8f131064d4b6d991bf3ab3105/src/transformers/models/gpt_oss/modeling_gpt_oss.py#L177
is this right?
SUMMARY:
Add test to convert the gpt_oss model and then quantize model to w8a8
TEST PLAN:
Tested with gpt_oss model and run using vllm
Sample output
=== Prompt 0 ===
<|start|>system<|message|>You are ChatGPT, a large language model trained by OpenAI.
Knowledge cutoff: 2024-06
Current date: 2025-12-04
Reasoning: medium
Valid channels: analysis, commentary, final. Channel must be included for every message.<|end|><|start|>developer<|message|># Instructions
You are a helpful assistant.
<|end|><|start|>user<|message|>Give 3 reasons to use AI.<|end|><|start|>assistant
--- Candidate 0 ---
analysisWe need to answer with three reasons to use AI. Should be short. Provide 3 reasons. Let's do it.assistantcommentaryHere are three reasons to use AI:
We should provide a short answer.assistantfinalHere are three reasons to use AI:
finish_reason: stop
num_tokens: 226