-
Notifications
You must be signed in to change notification settings - Fork 581
[6508436] Fix BF16 FP8 ONNX export #2314
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?
Changes from all commits
f82d8e8
db92c5b
51c3af4
2261b29
a2afc68
9730260
b53ef03
4c6a904
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| import shutil | ||
| import tempfile | ||
| from contextlib import nullcontext | ||
| from itertools import chain | ||
| from typing import Any | ||
|
|
||
| import onnx | ||
|
|
@@ -527,6 +528,13 @@ def get_onnx_bytes_and_metadata( | |
| if isinstance(model, (DataParallel, DistributedDataParallel)): | ||
| model = model.module | ||
|
|
||
| source_floating_dtypes = { | ||
| tensor.dtype | ||
| for tensor in chain(model.parameters(), model.buffers()) | ||
| if tensor.is_floating_point() | ||
| } | ||
| source_floating_dtype_names = ", ".join(sorted(map(str, source_floating_dtypes))) or "none" | ||
|
|
||
| # Standardize model args and also tensorize them so they also appear in the onnx graph! | ||
| # Floats/ints are tensorized when they are provided, but not tensorized when they are not | ||
| # provided which is somewhat inconsistent (we always tensorize them!) | ||
|
|
@@ -634,14 +642,28 @@ def get_onnx_bytes_and_metadata( | |
| if dq_only: | ||
| onnx_opt_graph = qdq_to_dq(onnx_opt_graph) | ||
|
|
||
| if weights_dtype in ["fp16", "bf16"]: | ||
| if ( | ||
| is_int4_quantized(model) | ||
| or is_mxfp8_quantized(model) | ||
| or is_fp8_quantized(model) | ||
| or is_int8_quantized(model) | ||
| ): | ||
| assert weights_dtype == "fp16", "BF16 + MXFP8/INT4 mixed precision is not supported yet" | ||
| uses_fp8 = is_fp8_quantized(model) | ||
| uses_other_unsupported_quantizer = ( | ||
| is_int4_quantized(model) or is_mxfp8_quantized(model) or is_int8_quantized(model) | ||
| ) | ||
| if weights_dtype == "fp16" and uses_fp8 and torch.bfloat16 in source_floating_dtypes: | ||
| raise AssertionError( | ||
| "Converting a BF16 FP8 ONNX graph to FP16 is not supported yet " | ||
| f"(source floating dtypes: {source_floating_dtype_names})" | ||
| ) | ||
|
Comment on lines
+650
to
+653
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Clean up the temporary export directory before this error. A BF16 FP8 export with Move compatibility validation before temporary-path creation, or wrap the export flow in 🤖 Prompt for AI Agents |
||
|
|
||
| is_bf16_fp8_noop = ( | ||
| weights_dtype == "bf16" | ||
| and source_floating_dtypes == {torch.bfloat16} | ||
| and uses_fp8 | ||
| and not uses_other_unsupported_quantizer | ||
| ) | ||
| if weights_dtype in ["fp16", "bf16"] and not is_bf16_fp8_noop: | ||
| if uses_other_unsupported_quantizer or uses_fp8: | ||
| assert weights_dtype == "fp16", ( | ||
| "Converting a quantized ONNX graph to BF16 is not supported yet " | ||
| f"(source floating dtypes: {source_floating_dtype_names})" | ||
| ) | ||
| onnx_opt_graph = convert_float_to_float16( | ||
| onnx_opt_graph, | ||
| keep_io_types=False, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.