Files
openvino/docs/snippets/ov_extensions.py
Anastasia Kuporosova e2534eb9d6 Akup/cherry pick python snippets (#19480)
* first snippet

* part1

* update model state snippet

* add temp dir

* CPU snippets update (#134)

* snippets CPU 1/6

* snippets CPU 2/6

* snippets CPU 3/6

* snippets CPU 4/6

* snippets CPU 5/6

* snippets CPU 6/6

* make  module TODO: REMEMBER ABOUT EXPORTING PYTONPATH ON CIs ETC

* Add static model creation in snippets for CPU

* export_comp_model done

* leftovers

* apply comments

* apply comments -- properties

* small fixes

* add serialize

* rempve debug info

* return IENetwork instead of Function

* apply comments

* revert precision change in common snippets

* update opset

* [PyOV] Edit docs for the rest of plugins (#136)

* modify main.py

* GNA snippets

* GPU snippets

* AUTO snippets

* MULTI snippets

* HETERO snippets

* Added properties

* update gna

* more samples

* Update docs/OV_Runtime_UG/model_state_intro.md

* Update docs/OV_Runtime_UG/model_state_intro.md

---------

Co-authored-by: Jan Iwaszkiewicz <jan.iwaszkiewicz@intel.com>
Co-authored-by: Karol Blaszczak <karol.blaszczak@intel.com>
2023-08-30 14:32:53 +02:00

74 lines
2.2 KiB
Python

# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#
import openvino as ov
#! [py_frontend_extension_ThresholdedReLU_header]
import openvino.runtime.opset12 as ops
from openvino.frontend import ConversionExtension
#! [py_frontend_extension_ThresholdedReLU_header]
#! [add_extension]
# Not implemented
#! [add_extension]
#! [add_frontend_extension]
# Not implemented
#! [add_frontend_extension]
from utils import get_path_to_extension_library
path_to_extension_lib = get_path_to_extension_library()
#! [add_extension_lib]
core = ov.Core()
# Load extensions library to ov.Core
core.add_extension(path_to_extension_lib)
#! [add_extension_lib]
#! [py_frontend_extension_MyRelu]
from openvino.frontend import OpExtension
core.add_extension(OpExtension("Relu", "MyRelu"))
#! [py_frontend_extension_MyRelu]
#! [py_frontend_extension_ThresholdedReLU]
def conversion(node):
input_node = node.get_input(0)
input_type = input_node.get_element_type()
greater = ops.greater(input_node, ops.constant([node.get_attribute("alpha")], input_type))
casted = ops.convert(greater, input_type.get_type_name())
return ops.multiply(input_node, casted).outputs()
core.add_extension(ConversionExtension("ThresholdedRelu", conversion))
#! [py_frontend_extension_ThresholdedReLU]
#! [py_frontend_extension_aten_hardtanh]
import torch
from openvino.frontend import ConversionExtension, NodeContext
from openvino.tools.mo import convert_model
class HardTanh(torch.nn.Module):
def __init__(self, min_val, max_val):
super(HardTanh, self).__init__()
self.min_val = min_val
self.max_val = max_val
def forward(self, inp):
return torch.nn.functional.hardtanh(inp, self.min_val, self.max_val)
def convert_hardtanh(node: NodeContext):
inp = node.get_input(0)
min_value = node.get_values_from_const_input(1)
max_value = node.get_values_from_const_input(2)
return ops.clamp(inp, min_value, max_value).outputs()
model = HardTanh(min_val=0.1, max_val=2.0)
hardtanh_ext = ConversionExtension("aten::hardtanh", convert_hardtanh)
ov_model = convert_model(input_model=model, extensions=[hardtanh_ext])
#! [py_frontend_extension_aten_hardtanh]