-
Notifications
You must be signed in to change notification settings - Fork 295
Add FP8 support for the ONNX backend #4072
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: develop
Are you sure you want to change the base?
Changes from 2 commits
cd0f9fb
1dc817f
13c9502
8cb036b
f00b450
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 |
|---|---|---|
|
|
@@ -71,6 +71,7 @@ class ONNXWeightCompressionAlgoBackend(WeightCompressionAlgoBackend): | |
| CompressWeightsMode.INT8_ASYM: onnx.TensorProto.UINT8, | ||
| CompressWeightsMode.INT4_SYM: onnx.TensorProto.INT4, | ||
| CompressWeightsMode.INT4_ASYM: onnx.TensorProto.UINT4, | ||
| CompressWeightsMode.FP8_E4M3: onnx.TensorProto.FLOAT8E4M3FN, | ||
| } | ||
|
|
||
| def __init__(self, model: onnx.ModelProto): | ||
|
|
@@ -363,8 +364,14 @@ def _add_dequantize_linear_layer( | |
| zero_point = pack_4_bits(zero_point) | ||
|
|
||
| # Create initializers for the quantized weights, scale, and zero point | ||
| if weight_dtype == onnx.TensorProto.FLOAT8E4M3FN: | ||
| np_dtype = helper.tensor_dtype_to_np_dtype(weight_dtype) | ||
| vals = onnx.numpy_helper.saturate_cast(np.asarray(quantized_weights), np_dtype).flatten() | ||
| else: | ||
| vals = quantized_weights | ||
|
Collaborator
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. Two similar code blocks, maybe worth a private method?
Contributor
Author
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. I've rewritten it slightly. Given that it's only two lines, I don't think introducing a separate method provides much value. |
||
|
|
||
| quantized_weights_initializer = onnx.helper.make_tensor( | ||
| quantized_weight_name, weight_dtype, orig_shape, quantized_weights.tobytes(), raw=True | ||
| quantized_weight_name, weight_dtype, orig_shape, vals.tobytes(), raw=True | ||
| ) | ||
| scale_initializer = numpy_helper.from_array( | ||
| np.array(scale, dtype=helper.tensor_dtype_to_np_dtype(scale_dtype)), name=scale_name | ||
|
|
@@ -374,8 +381,15 @@ def _add_dequantize_linear_layer( | |
|
|
||
| if zero_point is not None: | ||
| deq_inputs.append(weight_name + "_zero_point") | ||
|
|
||
| if weight_dtype == onnx.TensorProto.FLOAT8E4M3FN: | ||
| np_dtype = helper.tensor_dtype_to_np_dtype(weight_dtype) | ||
| vals = onnx.numpy_helper.saturate_cast(np.asarray(zero_point), np_dtype).flatten() | ||
| else: | ||
| vals = zero_point | ||
|
|
||
| zero_point_initializer = onnx.helper.make_tensor( | ||
| weight_name + "_zero_point", weight_dtype, orig_zero_point_shape, zero_point.tobytes(), raw=True | ||
| weight_name + "_zero_point", weight_dtype, orig_zero_point_shape, vals.tobytes(), raw=True | ||
| ) | ||
| new_initializers.append(zero_point_initializer) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.