Skip to content

Commit 0430a44

Browse files
authored
Merge branch 'openvinotoolkit:develop' into add-tensorflow-tensor-support
2 parents 5768fb6 + 72936ab commit 0430a44

File tree

70 files changed

+2381
-474
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2381
-474
lines changed

.github/workflows/api_changes_check.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
issues: write
2727
steps:
2828
- name: Download built HTML doc as artifact from previous step
29-
uses: actions/download-artifact@cc203385981b70ca67e1cc392babf9cc229d5806 # v4.1.9
29+
uses: actions/download-artifact@95815c38cf2ff2164869cbab79da8d1f422bc89e # v4.2.1
3030
with:
3131
name: html_doc_artifact
3232
- run: |
@@ -63,7 +63,7 @@ jobs:
6363
echo '{"pr_number": "${{ github.event.pull_request.number }}", "action": "none"}' > api_status.json
6464
6565
- name: Upload artifact
66-
uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 #v4.6.1
66+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 #v4.6.2
6767
with:
6868
name: api_status
6969
path: api_status.json

.github/workflows/build_and_publish_doc.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2929

3030
- name: Download HTML doc build artifact
31-
uses: actions/download-artifact@cc203385981b70ca67e1cc392babf9cc229d5806 # v4.1.9
31+
uses: actions/download-artifact@95815c38cf2ff2164869cbab79da8d1f422bc89e # v4.2.1
3232
with:
3333
name: html_doc_artifact
3434
- name: Extract artifact
@@ -38,7 +38,7 @@ jobs:
3838
rm artifact.tar
3939
4040
- name: Download schema doc build artifact
41-
uses: actions/download-artifact@cc203385981b70ca67e1cc392babf9cc229d5806 # v4.1.9
41+
uses: actions/download-artifact@95815c38cf2ff2164869cbab79da8d1f422bc89e # v4.2.1
4242
with:
4343
name: schema_doc_artifact
4444
path: html_build/html

.github/workflows/build_html_doc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
- name: Archive built HTMLs
3030
shell: bash
3131
run: tar -czf artifact.tar html_build/html
32-
- uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 #v4.6.1
32+
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 #v4.6.2
3333
with:
3434
name: html_doc_artifact
3535
path: artifact.tar

.github/workflows/build_schema_page.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
run: tar -czf artifact.tar schema
3232

3333
- name: Upload result as artifact
34-
uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 #v4.6.1
34+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 #v4.6.2
3535
with:
3636
name: schema_doc_artifact
3737
path: artifact.tar

.github/workflows/conformance_weight_compression.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
run: column -s, -t < tmp/results.csv || echo "no file"
6363
- name: Upload artifact
6464
if: ${{ !cancelled() }}
65-
uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 #v4.6.1
65+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 #v4.6.2
6666
with:
6767
name: wc_results_${{ matrix.group }}
6868
path: tmp/results.csv

.github/workflows/nightly.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
timeout-minutes: 10
2222
steps:
2323
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
24-
- uses: AlexanderDokuchaev/md-dead-link-check@c7210ef8a38c194a119834e39d212387d19b512c # v1.1.0
24+
- uses: AlexanderDokuchaev/md-dead-link-check@d5a37e0b14e5918605d22b34562532762ccb2e47 # v1.2.0
2525

2626
tensorflow:
2727
runs-on: ubuntu-latest-8-cores

.github/workflows/pre-commit-linters.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ jobs:
2424
runs-on: ubuntu-latest
2525
steps:
2626
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
27-
- uses: AlexanderDokuchaev/md-dead-link-check@c7210ef8a38c194a119834e39d212387d19b512c # v1.1.0
27+
- uses: AlexanderDokuchaev/md-dead-link-check@d5a37e0b14e5918605d22b34562532762ccb2e47 # v1.2.0
2828

.github/workflows/sdl.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ jobs:
4242
with:
4343
lfs: true
4444
- name: Initialize CodeQL
45-
uses: github/codeql-action/init@b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d # v3.28.10
45+
uses: github/codeql-action/init@5f8171a638ada777af81d42b55959a643bb29017 # v3.28.12
4646
with:
4747
languages: python
4848
- name: Perform CodeQL Analysis
49-
uses: github/codeql-action/analyze@b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d # v3.28.10
49+
uses: github/codeql-action/analyze@5f8171a638ada777af81d42b55959a643bb29017 # v3.28.12
5050
with:
5151
category: "/language:python"
5252

@@ -66,7 +66,7 @@ jobs:
6666
mv "report.pdf" "codeql_nncf_report_${DATE}_${REF_NAME//\//-}_${{ github.sha }}.pdf"
6767
- name: Upload CodeQL Artifacts
6868
if: ${{ github.event_name != 'pull_request' }}
69-
uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 #v4.6.1
69+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 #v4.6.2
7070
with:
7171
name: codeql-scan-results
7272
path: "./codeql*.pdf"
@@ -81,7 +81,7 @@ jobs:
8181
- name: Checkout repository
8282
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
8383
- name: Run trivy
84-
uses: aquasecurity/trivy-action@18f2510ee396bbf400402947b394f2dd8c87dbb0 # v0.29.0
84+
uses: aquasecurity/trivy-action@6c175e9c4083a92bbca2f9724c8a5e33bc2d97a5 # v0.30.0
8585
with:
8686
scan-type: "fs"
8787
scan-ref: .
@@ -102,7 +102,7 @@ jobs:
102102
mv "trivy_report.html" "trivy_report_${DATE}_${REF_NAME//\//-}_${{ github.sha }}.html"
103103
- name: Upload Scan Results
104104
if: ${{ !cancelled() }}
105-
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 #v4.6.0
105+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 #v4.6.2
106106
with:
107107
name: trivy-report
108108
path: "./trivy_report*.html"

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ Here is an example of Accuracy Aware Quantization pipeline where model weights a
245245

246246
```python
247247
import nncf
248+
import nncf.torch
248249
import torch
249250
from torchvision import datasets, models
250251

@@ -271,7 +272,7 @@ quantized_model = nncf.quantize(model, calibration_dataset)
271272
# Save quantization modules and the quantized model parameters
272273
checkpoint = {
273274
'state_dict': model.state_dict(),
274-
'nncf_config': model.nncf.get_config(),
275+
'nncf_config': nncf.torch.get_config(model),
275276
... # the rest of the user-defined objects to save
276277
}
277278
torch.save(checkpoint, path_to_checkpoint)

docs/usage/training_time_compression/quantization_aware_training/Usage.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,19 @@ ov_quantized_model = ov.convert_model(stripped_model)
7575

7676
The complete information about compression is defined by a compressed model and a NNCF config.
7777
The model characterizes the weights and topology of the network. The NNCF config - how to restore additional modules introduced by NNCF.
78-
The NNCF config can be obtained by `quantized_model.nncf.get_config()` on saving and passed to the
78+
The NNCF config can be obtained by `nncf.torch.get_config` on saving and passed to the
7979
`nncf.torch.load_from_config` helper function to load additional modules from the given NNCF config.
8080
The quantized model saving allows to load quantized modules to the target model in a new python process and
8181
requires only example input for the target module, corresponding NNCF config and the quantized model state dict.
8282

8383
```python
84+
import nncf.torch
85+
8486
# save part
8587
quantized_model = nncf.quantize(model, calibration_dataset)
8688
checkpoint = {
87-
'state_dict':quantized_model.state_dict(),
88-
'nncf_config': quantized_model.nncf.get_config(),
89+
'state_dict': quantized_model.state_dict(),
90+
'nncf_config': nncf.torch.get_config(quantized_model),
8991
...
9092
}
9193
torch.save(checkpoint, path)

examples/quantization_aware_training/torch/resnet18/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@ def transform_fn(data_item):
278278
print(f"Train epoch: {epoch}")
279279
train_epoch(train_loader, quantized_model, criterion, optimizer, device=device)
280280
acc1_int8 = validate(val_loader, quantized_model, device)
281-
print(f"Accyracy@1 of INT8 model after {epoch} epoch finetuning: {acc1_int8:.3f}")
281+
print(f"Accuracy@1 of INT8 model after {epoch} epoch finetuning: {acc1_int8:.3f}")
282282
# Save the compression checkpoint for model with the best accuracy metric.
283283
if acc1_int8 > acc1_int8_best:
284284
state_dict = quantized_model.state_dict()
285-
compression_config = quantized_model.nncf.get_config()
285+
compression_config = nncf.torch.get_config(quantized_model)
286286
torch.save(
287287
{
288288
"model_state_dict": state_dict,

nncf/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from nncf.parameters import ModelType as ModelType
4141
from nncf.parameters import QuantizationMode as QuantizationMode
4242
from nncf.parameters import SensitivityMetric as SensitivityMetric
43+
from nncf.parameters import StripFormat as StripFormat
4344
from nncf.parameters import TargetDevice as TargetDevice
4445
from nncf.quantization import QuantizationPreset as QuantizationPreset
4546
from nncf.quantization import compress_weights as compress_weights

nncf/api/compression.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from nncf.common.statistics import NNCFStatistics
2020
from nncf.common.utils.api_marker import api
2121
from nncf.common.utils.backend import copy_model
22+
from nncf.parameters import StripFormat
2223

2324
TModel = TypeVar("TModel")
2425

@@ -236,14 +237,17 @@ def statistics(self, quickly_collected_only: bool = False) -> NNCFStatistics:
236237
need to keep track of statistics on each training batch/step/iteration.
237238
"""
238239

239-
def strip_model(self, model: TModel, do_copy: bool = False) -> TModel:
240+
def strip_model(
241+
self, model: TModel, do_copy: bool = False, strip_format: StripFormat = StripFormat.NATIVE
242+
) -> TModel:
240243
"""
241244
Strips auxiliary layers that were used for the model compression, as it's
242245
only needed for training. The method is used before exporting the model
243246
in the target format.
244247
245248
:param model: The compressed model.
246249
:param do_copy: Modify copy of the model, defaults to False.
250+
:param strip format: Describes the format in which model is saved after strip.
247251
:return: The stripped model.
248252
"""
249253
if do_copy:
@@ -256,16 +260,17 @@ def prepare_for_export(self) -> None:
256260
"""
257261
self._model = self.strip_model(self._model)
258262

259-
def strip(self, do_copy: bool = True) -> TModel: # type: ignore[type-var]
263+
def strip(self, do_copy: bool = True, strip_format: StripFormat = StripFormat.NATIVE) -> TModel: # type: ignore[type-var]
260264
"""
261-
Returns the model object with as much custom NNCF additions as possible removed
262-
while still preserving the functioning of the model object as a compressed model.
265+
Removes auxiliary layers and operations added during the compression process, resulting in a clean
266+
model ready for deployment. The functionality of the model object is still preserved as a compressed model.
263267
264268
:param do_copy: If True (default), will return a copy of the currently associated model object. If False,
265269
will return the currently associated model object "stripped" in-place.
270+
:param strip format: Describes the format in which model is saved after strip.
266271
:return: The stripped model.
267272
"""
268-
return self.strip_model(self.model, do_copy) # type: ignore
273+
return self.strip_model(self.model, do_copy, strip_format) # type: ignore
269274

270275
@abstractmethod
271276
def export_model(

nncf/common/composite_compression.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from nncf.common.utils.backend import BackendType
2424
from nncf.common.utils.backend import copy_model
2525
from nncf.common.utils.backend import get_backend
26+
from nncf.parameters import StripFormat
2627

2728

2829
class CompositeCompressionLoss(CompressionLoss):
@@ -276,12 +277,12 @@ def prepare_for_export(self) -> None:
276277
stripped_model = ctrl.strip_model(stripped_model)
277278
self._model = stripped_model
278279

279-
def strip(self, do_copy: bool = True) -> TModel: # type: ignore
280+
def strip(self, do_copy: bool = True, strip_format: StripFormat = StripFormat.NATIVE) -> TModel: # type: ignore
280281
model = self.model
281282
if do_copy:
282283
model = copy_model(model)
283284
for ctrl in self.child_ctrls:
284-
model = ctrl.strip_model(model, do_copy=False)
285+
model = ctrl.strip_model(model, do_copy=False, strip_format=strip_format)
285286
return model # type: ignore
286287

287288
@property

nncf/common/strip.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from nncf.common.utils.api_marker import api
1717
from nncf.common.utils.backend import BackendType
1818
from nncf.common.utils.backend import get_backend
19+
from nncf.parameters import StripFormat
1920
from nncf.telemetry.decorator import tracked_function
2021
from nncf.telemetry.events import MODEL_BASED_CATEGORY
2122
from nncf.telemetry.extractors import FunctionCallTelemetryExtractor
@@ -25,25 +26,26 @@
2526

2627
@api(canonical_alias="nncf.strip")
2728
@tracked_function(category=MODEL_BASED_CATEGORY, extractors=[FunctionCallTelemetryExtractor("nncf.strip")])
28-
def strip(model: TModel, do_copy: bool = True) -> TModel:
29+
def strip(model: TModel, do_copy: bool = True, strip_format: StripFormat = StripFormat.NATIVE) -> TModel:
2930
"""
30-
Returns the model object with as much custom NNCF additions as possible removed
31-
while still preserving the functioning of the model object as a compressed model.
31+
Removes auxiliary layers and operations added during the compression process, resulting in a clean
32+
model ready for deployment. The functionality of the model object is still preserved as a compressed model.
3233
3334
:param model: The compressed model.
3435
:param do_copy: If True (default), will return a copy of the currently associated model object. If False,
3536
will return the currently associated model object "stripped" in-place.
37+
:param strip format: Describes the format in which model is saved after strip.
3638
:return: The stripped model.
3739
"""
3840
model_backend = get_backend(model)
3941
if model_backend == BackendType.TORCH:
4042
from nncf.torch.strip import strip as strip_pt
4143

42-
return strip_pt(model, do_copy) # type: ignore
44+
return strip_pt(model, do_copy, strip_format) # type: ignore
4345
elif model_backend == BackendType.TENSORFLOW:
4446
from nncf.tensorflow.strip import strip as strip_tf
4547

46-
return strip_tf(model, do_copy) # type: ignore
48+
return strip_tf(model, do_copy, strip_format) # type: ignore
4749

48-
msg = f"Method `strip` does not support for {model_backend.value} backend."
50+
msg = f"Method `strip` does not support {model_backend.value} backend."
4951
raise nncf.UnsupportedBackendError(msg)

nncf/experimental/tensorflow/quantization/algorithm.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from nncf.experimental.tensorflow.quantization.init_range import RangeInitializerV2
3636
from nncf.experimental.tensorflow.quantization.init_range import TFRangeInitParamsV2
3737
from nncf.experimental.tensorflow.quantization.quantizers import create_quantizer
38+
from nncf.parameters import StripFormat
3839
from nncf.tensorflow.algorithm_selector import TF_COMPRESSION_ALGORITHMS
3940
from nncf.tensorflow.graph.metatypes.tf_ops import TFOpWithWeightsMetatype
4041
from nncf.tensorflow.graph.transformations.commands import TFInsertionCommand
@@ -353,7 +354,9 @@ def apply_to(self, model: NNCFNetwork) -> NNCFNetwork:
353354

354355

355356
class QuantizationControllerV2(QuantizationController):
356-
def strip_model(self, model: NNCFNetwork, do_copy: bool = False) -> NNCFNetwork:
357+
def strip_model(
358+
self, model: NNCFNetwork, do_copy: bool = False, strip_format: StripFormat = StripFormat.NATIVE
359+
) -> NNCFNetwork:
357360
if do_copy:
358361
model = copy_model(model)
359362
return model

nncf/experimental/torch2/function_hook/graph/build_graph_mode.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,6 @@ def build_graph(model: nn.Module, *args: Any, **kwargs: Any) -> nx.MultiDiGraph:
373373
with GraphBuilderMode(model=model, hook_storage=get_hook_storage(model)) as ctx:
374374
args, kwargs = ctx.process_model_inputs(args, kwargs)
375375
wrapped_forward = cast(ForwardWithHooks, model.forward)
376-
outputs = wrapped_forward._func(*args, **kwargs)
376+
outputs = wrapped_forward.orig_forward(*args, **kwargs)
377377
outputs = ctx.process_model_outputs(outputs)
378378
return ctx.graph

nncf/experimental/torch2/function_hook/hook_executor_mode.py

+16
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
"size",
4242
"is_floating_point",
4343
"_set_grad_enabled",
44+
"_parse_to",
45+
"_has_compatible_shallow_copy_type",
4446
]
4547

4648

@@ -517,3 +519,17 @@ def disable(self) -> Iterator[None]:
517519
self.enabled = False
518520
yield
519521
self.enabled = ret
522+
523+
524+
@contextmanager
525+
def disable_function_hook_mode() -> Iterator[None]:
526+
"""
527+
Temporarily disables the function tracing and execution hooks within a context.
528+
"""
529+
enabled_modes = torch.overrides._get_current_function_mode_stack() # type: ignore[no-untyped-call]
530+
state = {(mode, mode.enabled) for mode in enabled_modes if isinstance(mode, FunctionHookMode)}
531+
for mode, _ in state:
532+
mode.enabled = False
533+
yield
534+
for mode, enabled in state:
535+
mode.enabled = enabled

0 commit comments

Comments
 (0)