Skip to content

Conversation

@krishnateja95
Copy link

SUMMARY:
"Recipe for FP8 Block quantization of Granite4-small model (https://huggingface.co/ibm-granite/granite-4.0-h-small)"

@github-actions
Copy link

github-actions bot commented Nov 6, 2025

👋 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.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @krishnateja95, 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 enables FP8 block quantization for the Granite4-small model by introducing a new example and essential architectural adaptations. It provides a mechanism to convert the model's Mixture-of-Experts (MoE) layers into a format amenable to quantization and then repackages the resulting weights for optimized storage and compatibility with high-performance inference frameworks like vLLM. This work streamlines the process of deploying quantized Granite models.

Highlights

  • FP8 Block Quantization Example: A new example script has been added to demonstrate FP8 block quantization for the ibm-granite/granite-4.0-h-small model, showcasing the full quantization workflow from model loading to saving and sample generation.
  • MoE Expert Conversion: Introduced a utility function replace_granite_moe_with_linear that converts GraniteMoeHybridParallelExperts modules into individual nn.Linear layers, which is a necessary step to enable proper quantization of these complex MoE architectures.
  • 3D Expert Packing for vLLM Compatibility: A new pack_3d_experts function has been implemented to transform the model's expert weights from a per-expert storage format to a stacked 3D tensor representation. This restructuring is crucial for compatibility with inference engines like vLLM and for more efficient storage.
  • Dynamic Config Update: The pack_3d_experts function also includes logic to automatically update the config.json file, specifically renaming mamba.in_proj and mamba.out_proj entries to mixer.in_proj and mixer.out_proj within the quantization_config.ignore list, ensuring consistency for quantized models.
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
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 FP8 block quantization for the Granite4-small model, including a new example script and utility functions. The core logic involves replacing MoE expert layers with standard linear layers for quantization and then packing them back into a 3D tensor format for compatibility. My review focuses on improving code quality, robustness, and fixing a bug that prevents the example from running. I've suggested removing unused imports, renaming a function for consistency and correctness, using isinstance for more robust type checking, and refactoring a large function for better maintainability. Overall, the changes are a good addition, and with these adjustments, the code will be more robust and easier to maintain.

@krishnateja95 krishnateja95 changed the title Granit4 FP8 Block Quantization Granite4 FP8 Block Quantization Nov 6, 2025
Copy link
Collaborator

@dsikka dsikka left a comment

Choose a reason for hiding this comment

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

Couple of key issues:

  1. We should be using the moe_context: https://github.com/vllm-project/llm-compressor/blob/main/src/llmcompressor/modeling/moe_context.py. We have many examples you can use that apply the context
  2. All saving logic should be applied by compressed-tensors. Seems like you're doing a lot of custom logic

import torch
import json
import os
import shutil
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

@dsikka This saving logic is specific to granite4 small model to ensure the fp8 block quantized model is compatible with vLLM.

krishnateja95 and others added 2 commits November 6, 2025 09:51
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: Krishna Teja Chitty-Venkata <44275589+krishnateja95@users.noreply.github.com>
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