-
Notifications
You must be signed in to change notification settings - Fork 283
add NVFP4 formal document #2321
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
Open
xin3he
wants to merge
4
commits into
master
Choose a base branch
from
xinhe/doc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| NVFP4 Quantization | ||
| ================== | ||
|
|
||
| 1. [Introduction](#introduction) | ||
| 2. [Get Started with NVFP4 Quantization API](#get-started-with-nvfp4-quantization-api) | ||
| 3. [Reference](#reference) | ||
|
|
||
| ## Introduction | ||
|
|
||
| Large language models (LLMs) have revolutionized fields such as natural language understanding, generation, and multimodal processing. As these models grow, their computational and memory requirements increase, making efficient deployment challenging. To address these issues, quantization methods are employed to reduce model size and accelerate inference with minimal loss in accuracy. | ||
|
|
||
| NVFP4 is a specialized 4-bit floating-point format (FP4) developed by NVIDIA for deep learning workloads. Compared to traditional INT8 or FP16 formats, NVFP4 offers further reductions in memory footprint and computational resource use, enabling efficient inference for LLMs and other neural networks on supported hardware. | ||
|
|
||
| The following table summarizes the NVFP4 quantization format: | ||
|
|
||
| <table> | ||
| <tr> | ||
| <th>Format Name</th> | ||
| <th>Element Data type</th> | ||
| <th>Element Bits</th> | ||
| <th>Scaling Block Size</th> | ||
| <th>Scale Data Type</th> | ||
| <th>Scale Bits</th> | ||
| <th>Global Scale Data Type</th> | ||
| <th>Global Scale Bits</th> | ||
| </tr> | ||
| <tr> | ||
| <td>NVFP4</td> | ||
| <td>E2M1</td> | ||
| <td>4</td> | ||
| <td>16</td> | ||
| <td>E4M3</td> | ||
xin3he marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <td>8</td> | ||
| <td>FP32</td> | ||
| <td>32</td> | ||
| </tr> | ||
| </table> | ||
|
|
||
| At similar accuracy levels, NVFP4 can deliver lower memory usage and improved compute efficiency for multiply-accumulate operations compared to higher-precision formats. Neural Compressor supports post-training quantization to NVFP4, providing recipes and APIs for users to quantize LLMs easily. To provide the best performance, the global scale for activation is static. | ||
|
|
||
| ## Get Started with NVFP4 Quantization API | ||
|
|
||
| To quantize a model to the NVFP4 format, use the AutoRound Quantization API as shown below. | ||
|
|
||
| ```python | ||
| from neural_compressor.torch.quantization import AutoRoundConfig, prepare, convert | ||
| from transformers import AutoModelForCausalLM, AutoTokenizer | ||
|
|
||
| fp32_model = AutoModelForCausalLM.from_pretrained( | ||
| "facebook/opt-125m", | ||
| device_map="auto", | ||
| ) | ||
| tokenizer = AutoTokenizer.from_pretrained("facebook/opt-125m", trust_remote_code=True) | ||
| output_dir = "./saved_inc" | ||
|
|
||
| # quantization configuration | ||
| quant_config = AutoRoundConfig( | ||
| tokenizer=tokenizer, | ||
| nsamples=32, | ||
| seqlen=32, | ||
| iters=20, | ||
| scheme="NVFP4", # NVFP4 format | ||
| export_format="llm_compressor", | ||
| output_dir=output_dir, # default is "temp_auto_round" | ||
| ) | ||
|
|
||
| # quantize the model and save to output_dir | ||
| model = prepare(model=fp32_model, quant_config=quant_config) | ||
| model = convert(model) | ||
|
|
||
| # loading | ||
| model = AutoModelForCausalLM.from_pretrained(output_dir, torch_dtype="auto", device_map="auto") | ||
|
|
||
| # inference | ||
| text = "There is a girl who likes adventure," | ||
| inputs = tokenizer(text, return_tensors="pt").to(model.device) | ||
| print(tokenizer.decode(model.generate(**inputs, max_new_tokens=10)[0])) | ||
| ``` | ||
|
|
||
| ## Reference | ||
|
|
||
| [1]: NVIDIA, Introducing NVFP4 for efficient and accurate low-precision inference,NVIDIA Developer Blog, Jun. 2025. [Online]. Available: https://developer.nvidia.com/blog/introducing-nvfp4-for-efficient-and-accurate-low-precision-inference/ | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.