Skip to content

Commit

Permalink
Merge branch 'itikhono/bug_fix/envvar_typo' of https://github.com/iti…
Browse files Browse the repository at this point in the history
…khono/openvino into itikhono/bug_fix/envvar_typo
  • Loading branch information
itikhono committed Sep 9, 2024
2 parents 865b992 + a2c8634 commit 300dea0
Show file tree
Hide file tree
Showing 92 changed files with 46,621 additions and 688 deletions.
60 changes: 58 additions & 2 deletions .github/workflows/dev_cpu_linux_snippets_libxsmm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,65 @@ jobs:
path: ${{ env.BUILD_DIR }}/openvino_tests.tar.gz
if-no-files-found: 'error'

CPU_Unit_Tests:
name: C++ CPU unit tests
needs: [ Docker, Build, Smart_CI ]
timeout-minutes: 30
runs-on: aks-linux-8-cores-32gb
container:
image: ${{ fromJSON(needs.docker.outputs.images).ov_test.ubuntu_20_04_x64 }}
defaults:
run:
shell: bash
env:
DEBIAN_FRONTEND: noninteractive # to prevent apt-get from waiting user input
INSTALL_DIR: ${{ github.workspace }}/install
INSTALL_TEST_DIR: ${{ github.workspace }}/install/tests
steps:
- name: Download OpenVINO package
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: openvino_package
path: ${{ env.INSTALL_DIR }}

- name: Download OpenVINO tests package
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: openvino_tests
path: ${{ env.INSTALL_TEST_DIR }}


# Needed as ${{ github.workspace }} is not working correctly when using Docker
- name: Setup Variables
run: |
echo "INSTALL_DIR=$GITHUB_WORKSPACE/install" >> "$GITHUB_ENV"
echo "INSTALL_TEST_DIR=$GITHUB_WORKSPACE/install/tests" >> "$GITHUB_ENV"
echo "SETUPVARS_COMMAND=source $GITHUB_WORKSPACE/install/setupvars.sh" >> "$GITHUB_ENV"
- name: Extract OpenVINO packages
run: |
pushd $INSTALL_DIR
tar -xzf openvino_package.tar.gz -C $INSTALL_DIR
popd
pushd $INSTALL_TEST_DIR
tar -xzf openvino_tests.tar.gz -C $INSTALL_DIR
popd
- name: Snippets func tests
if: fromJSON(needs.smart_ci.outputs.affected_components).CPU.test
run: |
${{ env.SETUPVARS_COMMAND }}
${{ env.INSTALL_TEST_DIR }}/ov_snippets_func_tests --gtest_print_time=1 --gtest_output=xml:${{ env.INSTALL_TEST_DIR }}/TEST-SnippetsFuncTests.xml
- name: CPU plugin unit tests
if: fromJSON(needs.smart_ci.outputs.affected_components).CPU.test
run: |
${{ env.SETUPVARS_COMMAND }}
${{ env.INSTALL_TEST_DIR }}/ov_cpu_unit_tests --gtest_print_time=1 --gtest_output=xml:${{ env.INSTALL_TEST_DIR }}/TEST-CPUUnitTests.xml
CPU_Functional_Tests:
name: CPU functional tests
if: ${{ github.event_name == 'workflow_dispatch' || contains(github.event.pull_request.title, 'GHA-SNIPPETS') }}
needs: [ Docker, Build, Smart_CI ]
timeout-minutes: 30
runs-on: aks-linux-8-cores-32gb
Expand Down Expand Up @@ -265,7 +321,7 @@ jobs:
# Needed as ze_loader.so is under INSTALL_TEST_DIR
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${INSTALL_TEST_DIR}
python3 ${PARALLEL_TEST_SCRIPT} -e ${INSTALL_TEST_DIR}/ov_cpu_func_tests -w ${INSTALL_TEST_DIR} -s suite -rf 0 -- --gtest_filter=*smoke_Snippets* --gtest_print_time=1
python3 ${PARALLEL_TEST_SCRIPT} -e ${INSTALL_TEST_DIR}/ov_cpu_func_tests -w ${INSTALL_TEST_DIR} -s suite -rf 0 -- --gtest_filter=*smoke_Snippets*:*smoke_MHA* --gtest_print_time=1
timeout-minutes: 25

- name: Upload Test Results
Expand Down
31 changes: 31 additions & 0 deletions docs/articles_en/assets/snippets/ov_caching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,43 @@ bool cachingSupported = std::find(caps.begin(), caps.end(), ov::device::capabili
}
}

void part4() {
std::string modelPath = "/tmp/myModel.xml";
std::string device = "CPU";
ov::Core core; // Step 1: create ov::Core object
core.set_property(ov::cache_dir("/path/to/cache/dir")); // Step 1b: Enable caching
auto model = core.read_model(modelPath); // Step 2: Read Model
//! [ov:caching:part4]
ov::AnyMap config;
ov::EncryptionCallbacks encryption_callbacks;
static const char codec_key[] = {0x30, 0x60, 0x70, 0x02, 0x04, 0x08, 0x3F, 0x6F, 0x72, 0x74, 0x78, 0x7F};
auto codec_xor = [&](const std::string& source_str) {
auto key_size = sizeof(codec_key);
int key_idx = 0;
std::string dst_str = source_str;
for (char& c : dst_str) {
c ^= codec_key[key_idx % key_size];
key_idx++;
}
return dst_str;
};
encryption_callbacks.encrypt = codec_xor;
encryption_callbacks.decrypt = codec_xor;
config.insert(ov::cache_encryption_callbacks(encryption_callbacks)); // Step 4: Set device configuration
auto compiled = core.compile_model(model, device, config); // Step 5: LoadNetwork
//! [ov:caching:part4]
if (!compiled) {
throw std::runtime_error("error");
}
}

int main() {
try {
part0();
part1();
part2();
part3();
part4();
} catch (...) {
}
return 0;
Expand Down
17 changes: 17 additions & 0 deletions docs/articles_en/assets/snippets/ov_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@
# Find 'EXPORT_IMPORT' capability in supported capabilities
caching_supported = 'EXPORT_IMPORT' in core.get_property(device_name, device.capabilities)
# ! [ov:caching:part3]

# ! [ov:caching:part4]
import base64

def encrypt_base64(src):
return base64.b64encode(bytes(src, "utf-8"))

def decrypt_base64(src):
return base64.b64decode(bytes(src, "utf-8"))

core = ov.Core()
core.set_property({props.cache_dir: path_to_cache_dir})
config_cache = {}
config_cache["CACHE_ENCRYPTION_CALLBACKS"] = [encrypt_base64, decrypt_base64]
model = core.read_model(model=model_path)
compiled_model = core.compile_model(model=model, device_name=device_name, config=config_cache)
# ! [ov:caching:part4]
95 changes: 95 additions & 0 deletions docs/articles_en/assets/snippets/ov_matcher_pass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# ! [matcher_pass:ov_matcher_pass_py]
'''
``MatcherPass`` is used for pattern-based transformations.
To create transformation, you need to:
1. Create a pattern.
2. Implement a callback.
3. Register the pattern and ``Matcher``.
The next example defines transformation that searches for the ``Relu`` layer and inserts after it another
``Relu`` layer.
'''

from openvino.runtime.passes import MatcherPass
from snippets import get_model

class PatternReplacement(MatcherPass):
def __init__(self):
MatcherPass.__init__(self)
relu = WrapType("opset13::Relu")

def callback(matcher: Matcher) -> bool:
root = matcher.get_match_root()
new_relu = ops.relu(root.input(0).get_source_output())

"""Use new operation for additional matching
self.register_new_node(new_relu)
Input->Relu->Result => Input->Relu->Relu->Result
"""
root.input(0).replace_source_output(new_relu.output(0))
return True

self.register_matcher(Matcher(relu, "PatternReplacement"), callback)


'''
After running this code, you will see the next:
model ops :
parameter
result
relu
model ops :
parameter
result
relu
new_relu
In order to run this script, you need to export PYTHONPATH as the path to binary OpenVINO python models.
'''
from openvino.runtime.passes import Manager
from openvino import Model, PartialShape
from openvino.runtime import opset13 as ops
from openvino.runtime.passes import ModelPass, Matcher, MatcherPass, WrapType

class PatternReplacement(MatcherPass):
def __init__(self):
MatcherPass.__init__(self)
relu = WrapType("opset13::Relu")

def callback(matcher: Matcher) -> bool:
root = matcher.get_match_root()
new_relu = ops.relu(root.input(0).get_source_output())
new_relu.set_friendly_name('new_relu')

"""Use new operation for additional matching
self.register_new_node(new_relu)
Input->Relu->Result => Input->Relu->Relu->Result
"""
root.input(0).replace_source_output(new_relu.output(0))
return True

self.register_matcher(Matcher(relu, "PatternReplacement"), callback)


def print_model_ops(model):
print('model ops : ')
for op in model.get_ops():
print(op.get_friendly_name())
print('')


manager = Manager()
manager.register_pass(PatternReplacement())


model = get_model()
print_model_ops(model)
manager.run_passes(model)
print_model_ops(model)

# ! [matcher_pass:ov_matcher_pass_py]
47 changes: 47 additions & 0 deletions docs/articles_en/assets/snippets/ov_model_pass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# ! [model_pass:ov_model_pass_py]

'''
``ModelPass`` can be used as a base class for transformation classes that take entire ``Model`` and proceed with it.
To create transformation, you need to:
1. Define a class with ``ModelPass`` as a parent.
2. Redefine the run_on_model method that will receive ``Model`` as an argument.
'''

from openvino.runtime.passes import ModelPass
from snippets import get_model

class MyModelPass(ModelPass):
def __init__(self):
super().__init__()

def run_on_model(self, model):
for op in model.get_ops():
print(op.get_friendly_name())


'''
This example defines transformation that prints all model operation names.
The next example shows ModelPass-based transformation usage.
You create ``Model`` with ``Relu``, ``Parameter`` and ``Result`` nodes. After running this code, you will see the names of the nodes.
In order to run this script, you need to export PYTHONPATH as the path to binary OpenVINO python models.
'''

from openvino.runtime.passes import Manager, GraphRewrite, BackwardGraphRewrite, Serialize
from openvino import Model, PartialShape
from openvino.runtime import opset13 as ops
from openvino.runtime.passes import ModelPass, Matcher, MatcherPass, WrapType


class MyModelPass(ModelPass):
def __init__(self):
super().__init__()

def run_on_model(self, model):
for op in model.get_ops():
print(op.get_friendly_name())


manager = Manager()
manager.register_pass(MyModelPass())
manager.run_passes(get_model())
# ! [model_pass:ov_model_pass_py]
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@ Template for MatcherPass transformation class
:language: cpp
:fragment: [graph_rewrite:template_transformation_hpp]

.. doxygensnippet:: docs/articles_en/assets/snippets/template_pattern_transformation.cpp
:language: cpp
:fragment: [graph_rewrite:template_transformation_cpp]
.. tab-set::

.. tab-item:: C++
:sync: cpp

.. doxygensnippet:: docs/articles_en/assets/snippets/template_pattern_transformation.cpp
:language: cpp
:fragment: [graph_rewrite:template_transformation_cpp]

.. tab-item:: Python
:sync: py

.. doxygensnippet:: docs/articles_en/assets/snippets/ov_matcher_pass.py
:language: py
:fragment: [matcher_pass:ov_matcher_pass_py]

To use ``ov::pass::MatcherPass``, you need to complete these steps:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@ Template for ModelPass transformation class
:language: cpp
:fragment: [model_pass:template_transformation_hpp]

.. doxygensnippet:: docs/articles_en/assets/snippets/template_model_transformation.cpp
:language: cpp
:fragment: [model_pass:template_transformation_cpp]
.. tab-set::

.. tab-item:: C++
:sync: cpp

.. doxygensnippet:: docs/articles_en/assets/snippets/template_model_transformation.cpp
:language: cpp
:fragment: [model_pass:template_transformation_cpp]

.. tab-item:: Python
:sync: py

.. doxygensnippet:: docs/articles_en/assets/snippets/ov_model_pass.py
:language: py
:fragment: [model_pass:ov_model_pass_py]

Using ``ov::pass::ModelPass``, you need to override the ``run_on_model`` method where you will write the transformation code.
Return value is ``true`` if the original model has changed during transformation (new operation was added, or operations replacement was made, or node attributes were changed); otherwise, it is ``false``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Quantize the model using the :doc:`Post-Training Quantization <../quantizing-mod
2. Fine-tune the Model
########################

This step assumes applying fine-tuning to the model the same way it is done for the baseline model. For QAT, it is required to train the model for a few epochs with a small learning rate, for example, 10e-5.
This step assumes applying fine-tuning to the model the same way it is done for the baseline model. For QAT, it is required to train the model for a few epochs with a small learning rate, for example, 1e-5.
Quantized models perform all computations in floating-point precision during fine-tuning by modeling quantization errors in both forward and backward passes.

.. doxygensnippet:: docs/optimization_guide/nncf/code/qat_torch.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,29 @@ To check in advance if a particular device supports model caching, your applicat
:language: cpp
:fragment: [ov:caching:part3]

Set "cache_encryption_callbacks" config option to enable cache encryption
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

With model caching enabled, model topology in cache can be encrypted/decrypted when saving/loading model cache.
This property can currently only be set in ``compile_model``.

.. tab-set::

.. tab-item:: Python
:sync: py

.. doxygensnippet:: docs/articles_en/assets/snippets/ov_caching.py
:language: py
:fragment: [ov:caching:part4]

.. tab-item:: C++
:sync: cpp

.. doxygensnippet:: docs/articles_en/assets/snippets/ov_caching.cpp
:language: cpp
:fragment: [ov:caching:part4]

.. important::

Currently, this property is only supported by CPU plugin. For other HW plugins, setting this property will perform
normally but do not encrypt/decrypt the model topology in cache.
Loading

0 comments on commit 300dea0

Please sign in to comment.