forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathov_extensions.py
73 lines (54 loc) · 2.16 KB
/
ov_extensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright (C) 2018-2024 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 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, extension=[hardtanh_ext])
#! [py_frontend_extension_aten_hardtanh]