Skip to content

fp8 weight compression into 4 bit types#3501

Merged
alexsu52 merged 11 commits into
openvinotoolkit:developfrom
alexsu52:as/fp8_compression
Jul 28, 2025
Merged

fp8 weight compression into 4 bit types#3501
alexsu52 merged 11 commits into
openvinotoolkit:developfrom
alexsu52:as/fp8_compression

Conversation

@alexsu52

@alexsu52 alexsu52 commented May 19, 2025

Copy link
Copy Markdown
Contributor

Changes

  • Added support for compression of f8e4m3 and f8e5m2 weights into 4-bit types.
  • Added skipping compression of f8e4m3 and f8e5m2 weights into 8-bit types.
  • Added support of f8e4m3 and f8e5m2 in optimized OpenVINO functions.
  • Added weight_dtype in WeightCompressionParameters.
  • Refactored WeightCompression algorithm to minimize number of collected statistics.
  • Refactored AWQ algorithm by removing nodes_to_compress argument in the apply function.
  • Removed the redundant Convert in the compression subgraph for f8e4m3 and f8e5m2 weights.

Reason for changes

Support for DeepSeek-R1

Related tickets

CVS-162315

Tests

tests/openvino/native/quantization/test_weights_compression.py
https://github.com/openvinotoolkit/nncf/actions/runs/15563432412

@github-actions github-actions Bot added NNCF OpenVINO Pull requests that updates NNCF OpenVINO NNCF PTQ Pull requests that updates NNCF PTQ labels May 19, 2025
@alexsu52 alexsu52 force-pushed the as/fp8_compression branch 4 times, most recently from 49c2d1e to b29a677 Compare May 21, 2025 12:42
@alexsu52 alexsu52 marked this pull request as ready for review May 22, 2025 15:22
@alexsu52 alexsu52 requested a review from a team as a code owner May 22, 2025 15:22
@ljaljushkin

Copy link
Copy Markdown
Contributor

Thanks, good to see a lot of improvements in the weight compression algorithm!
Suggest adding the following tests for them:

Added support of f8e4m3 and f8e5m2 in optimized OpenVINO functions.

Removed the redundant Convert in the compression subgraph for f8e4m3 and f8e5m2 weights.

  • Quite tricky change, tracking number of convert in some test would be helpful.

Refactored WeightCompression algorithm to minimize number of collected statistics.

  • It would be helpful to have a test checking number of collected statistics.

Comment thread nncf/quantization/algorithms/weight_compression/algorithm.py Outdated
Comment thread nncf/openvino/optimized_functions/models.py Outdated
@alexsu52 alexsu52 force-pushed the as/fp8_compression branch from 187c06a to a2bf6da Compare June 7, 2025 14:20
@alexsu52

alexsu52 commented Jun 7, 2025

Copy link
Copy Markdown
Contributor Author
* Tests for optimized functions can be extended https://github.com/openvinotoolkit/nncf/tree/develop/tests/openvino/optimized_functions

Done: tests/openvino/optimized_functions/test_compression_functions.py

* Quite tricky change, tracking number of convert in some test would be helpful.

This was covered in tests/openvino/native/quantization/test_weights_compression.py

* It would be helpful to have a test checking number of collected statistics.

Done via checking that all statistics are used: tests/cross_fw/test_templates/template_test_weights_compression.py

Comment thread nncf/quantization/algorithms/weight_compression/config.py
Comment thread nncf/quantization/algorithms/weight_compression/algorithm.py
Comment thread tests/openvino/native/quantization/test_weights_compression.py Outdated
Comment thread tests/openvino/native/quantization/test_weights_compression.py Outdated
Comment thread tests/openvino/optimized_functions/test_compression_functions.py Outdated
@alexsu52 alexsu52 force-pushed the as/fp8_compression branch from 68b65ce to 6948773 Compare July 25, 2025 19:13
@github-actions github-actions Bot removed the NNCF PTQ Pull requests that updates NNCF PTQ label Jul 25, 2025

@nikita-savelyevv nikita-savelyevv left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Overall looks good, some minor comments below

Comment thread src/nncf/quantization/algorithms/weight_compression/algorithm.py Outdated
if weight_name in weight_names:
if i == n - 1:
is_last_layer_shared = True
is_last_layer_skipped = is_last_layer

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I have a concern that previously this variable was named is_last_layer_shared and from that it was clear why the node is skipped. Now it is less obvious. Perhaps, some comment would help.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a comment to the code.

Comment on lines 845 to +846
else:
mode = (
CompressWeightsMode.INT8_ASYM
if self._backup_mode == BackupMode.INT8_ASYM
else CompressWeightsMode.INT8_SYM
is_last_layer_skipped = is_last_layer

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I have difficulties matching this logic with the previous one. Where does this come from / what's the logic behind it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a fix to the previous logic. The previous logic was that if the last layer is skipped for some reason, the layer preceding the last one is also skipped. The current logic only skips the last layer.

Comment thread src/nncf/quantization/algorithms/weight_compression/algorithm.py
Comment thread src/nncf/quantization/algorithms/weight_compression/algorithm.py Outdated
Comment thread src/nncf/quantization/algorithms/weight_compression/algorithm.py Outdated
Comment thread src/nncf/quantization/algorithms/weight_compression/algorithm.py Outdated
Comment thread src/nncf/quantization/algorithms/weight_compression/algorithm.py Outdated
model: TModel,
graph: NNCFGraph,
all_weight_params: list[WeightCompressionParameters],
nodes_to_compress: list[NNCFNode],

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread src/nncf/tensor/definitions.py Outdated
Comment on lines +69 to +83
def itemsize(self) -> int:
"""
Returns the size of a single item in bits for the tensor data type.

:return: The item size in bits.
"""
if self in [TensorDataType.nf4, TensorDataType.uint4, TensorDataType.int4]:
return 4
if self in [TensorDataType.f8e4m3, TensorDataType.f8e5m2, TensorDataType.int8, TensorDataType.uint8]:
return 8
if self in [TensorDataType.float16, TensorDataType.bfloat16]:
return 16
if self in [TensorDataType.float32, TensorDataType.int32]:
return 32
return 64

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
def itemsize(self) -> int:
"""
Returns the size of a single item in bits for the tensor data type.
:return: The item size in bits.
"""
if self in [TensorDataType.nf4, TensorDataType.uint4, TensorDataType.int4]:
return 4
if self in [TensorDataType.f8e4m3, TensorDataType.f8e5m2, TensorDataType.int8, TensorDataType.uint8]:
return 8
if self in [TensorDataType.float16, TensorDataType.bfloat16]:
return 16
if self in [TensorDataType.float32, TensorDataType.int32]:
return 32
return 64
def itemsize(self) -> int:
"""
Returns the size of a single item in bits for the tensor data type.
:return: The item size in bits.
"""
return ITEMSIZE_BITS[self]
ITEMSIZE_BITS = {
TensorDataType.nf4: 4,
TensorDataType.uint4: 4,
TensorDataType.int4: 4,
TensorDataType.f8e4m3: 8,
TensorDataType.f8e5m2: 8,
TensorDataType.int8: 8,
TensorDataType.uint8: 8,
TensorDataType.float16: 16,
TensorDataType.bfloat16: 16,
TensorDataType.float32: 32,
TensorDataType.int32: 32,
TensorDataType.float64: 64,
TensorDataType.int64: 64,
}

What do you think about this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@alexsu52 alexsu52 merged commit 3885439 into openvinotoolkit:develop Jul 28, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

NNCF OpenVINO Pull requests that updates NNCF OpenVINO

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants