Skip to content
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

[PTQ][OV] BF16 support #2307

Open
wants to merge 30 commits into
base: develop
Choose a base branch
from

Conversation

KodiaqQ
Copy link
Collaborator

@KodiaqQ KodiaqQ commented Dec 7, 2023

Changes

  • Added BF16 type support
  • Added FQ parameters generation based on type
  • Extended the list of the supported types for OpenVINO input data with ov.Tensor

Reason for changes

  • BF16 support

Related tickets

  • 126782

Tests

  • Updated existing tests with BF16.
  • Edit: not updated due to lack of precision support in the OpenVINO

@github-actions github-actions bot added the NNCF OpenVINO Pull requests that updates NNCF OpenVINO label Dec 7, 2023
Copy link

codecov bot commented Dec 7, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.20%. Comparing base (58b0c64) to head (7dfd1c1).
Report is 1 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #2307      +/-   ##
===========================================
+ Coverage    91.18%   91.20%   +0.01%     
===========================================
  Files          483      483              
  Lines        46412    46435      +23     
===========================================
+ Hits         42323    42352      +29     
+ Misses        4089     4083       -6     
Files Coverage Δ
nncf/openvino/engine.py 96.55% <ø> (ø)
nncf/openvino/graph/model_transformer.py 94.84% <100.00%> (+0.82%) ⬆️
nncf/openvino/graph/node_utils.py 98.80% <100.00%> (+0.57%) ⬆️
nncf/openvino/graph/transformations/commands.py 97.67% <100.00%> (+0.11%) ⬆️
nncf/openvino/quantization/quantize_ifmodel.py 100.00% <100.00%> (ø)
.../algorithms/weight_compression/openvino_backend.py 98.84% <100.00%> (-0.01%) ⬇️

... and 4 files with indirect coverage changes

Flag Coverage Δ
COMMON 41.93% <0.00%> (+0.05%) ⬆️
ONNX 34.19% <0.00%> (+0.03%) ⬆️
OPENVINO 40.96% <100.00%> (+0.05%) ⬆️
TENSORFLOW 29.39% <0.00%> (+<0.01%) ⬆️
TORCH 65.38% <7.54%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
common 93.55% <ø> (+<0.01%) ⬆️
torch 93.65% <ø> (ø)
tensorflow 93.26% <ø> (ø)
onnx 93.06% <ø> (ø)
openvino 94.62% <100.00%> (+0.10%) ⬆️
ptq 90.50% <100.00%> (+<0.01%) ⬆️

Comment on lines 249 to 252
input_low = opset.constant(value=fq_params.input_low.data, dtype=dtype)
input_high = opset.constant(value=fq_params.input_high.data, dtype=dtype)
output_low = opset.constant(value=fq_params.output_low.data, dtype=dtype)
output_high = opset.constant(value=fq_params.output_high.data, dtype=dtype)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you sure that we don't need clip FQ params before creating the constant node?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch. I'll look at this.

@KodiaqQ
Copy link
Collaborator Author

KodiaqQ commented Dec 12, 2023

@alexsu52, @l-bat, please, review.

@KodiaqQ KodiaqQ marked this pull request as draft December 13, 2023 11:04
@github-actions github-actions bot added the NNCF PTQ Pull requests that updates NNCF PTQ label Jan 23, 2024
@KodiaqQ KodiaqQ marked this pull request as ready for review June 17, 2024 15:07
@KodiaqQ KodiaqQ requested a review from l-bat June 17, 2024 15:07
@KodiaqQ
Copy link
Collaborator Author

KodiaqQ commented Jun 17, 2024

@l-bat, @alexsu52, @andrey-churkin, review, please.

@KodiaqQ KodiaqQ requested a review from kshpv June 19, 2024 06:34
Comment on lines 203 to 204
if node_output.get_element_type() != outputs_type:
node_output = opset.convert(output, destination_type=outputs_type)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This affects outputs for the then-else bodies of the if model. The point is that we should collect all statistics using fp32 precision. But some outputs may have a boolean format, not a numeric.
What should we do in this case, @kshpv, @alexsu52?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest to add flexibility. I mean the insert output transformation command should has dtype parameter to specify return type. By default the return type is FP32.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@KodiaqQ
Copy link
Collaborator Author

KodiaqQ commented Jun 19, 2024

Compression time comparison. Numbers were collected on local i9-10980XE.

Model Backend Compr. Time (develop) Compr. Time (bf16 branch)
hf/bert-base-uncased OV 00:00:15 00:00:15
timm/crossvit_9_240 OV 00:00:20 00:00:20
timm/darknet53 OV 00:00:44 00:00:32
timm/deit3_small_patch16_224 OV 00:00:25 00:00:25
timm/dla34 OV 00:00:15 00:00:15
timm/dpn68 OV 00:00:16 00:00:16
timm/efficientnet_b0_BC OV 00:00:20 00:00:20
timm/efficientnet_b0 OV 00:00:12 00:00:12
timm/efficientnet_lite0 OV 00:00:11 00:00:11
timm/hrnet_w18 OV 00:01:20 00:01:11
timm/inception_resnet_v2 OV 00:01:23 00:01:13
timm/levit_128 OV 00:00:18 00:00:18
timm/mobilenetv2_050_BC OV 00:00:14 00:00:14
timm/mobilenetv2_050 OV 00:00:09 00:00:10
timm/mobilenetv3_small_050_BC OV 00:00:09 00:00:09
timm/mobilenetv3_small_050 OV 00:00:07 00:00:06
timm/regnetx_002 OV 00:00:09 00:00:08
timm/resnest14d OV 00:00:16 00:00:15
timm/resnet18 OV 00:00:10 00:00:10
timm/swin_base_patch4_window7_224 OV 00:01:18 00:01:18
timm/tf_inception_v3 OV 00:00:31 00:00:31
timm/vgg11 OV 00:00:31 00:00:30
timm/visformer_small OV 00:00:18 00:00:17
timm/wide_resnet50_2 OV 00:00:39 00:00:39

No degradations were observed.

@@ -102,16 +97,16 @@ def _get_activation_node_names(model: ov.Model) -> List[str]:
return list(activation_nodes)

@staticmethod
def _update_tensor_name(tensors: List[DescriptorTensor], name: str) -> None:
def _update_tensor_name(tensors: List[DescriptorTensor], names: List[str]) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
def _update_tensor_name(tensors: List[DescriptorTensor], names: List[str]) -> None:
def _update_tensor_names(tensors: List[DescriptorTensor], names: List[str]) -> None:

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@@ -162,7 +161,7 @@ def transform_model(
should_add_convert_node = True
break

weight = Tensor(get_const_value(const_node, np.float32 if const_dtype == ov.Type.bf16 else None))
weight = Tensor(get_const_value(const_node, const_dtype))
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm not mistaken, then you removed the bf16 to fp32 conversion.

Suggested change
weight = Tensor(get_const_value(const_node, const_dtype))
weight = Tensor(get_const_value(const_node))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@KodiaqQ KodiaqQ requested a review from alexsu52 June 19, 2024 15:18
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 NNCF PTQ Pull requests that updates NNCF PTQ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants