4242from torch .nn import Parameter
4343
4444
45- def _is_convertible_to_relu (node ):
46- bounds = ClampConverter ._get_clamp_bounds (node )
47- bounds = tuple (v if v is not None and math .isfinite (v ) else None for v in bounds )
48-
49- # Some specific bounds can be replaced with single op ReLU.
50- if bounds not in ClampConverter .RELU_COMPATIBLE_BOUNDS .values ():
51- return False
52-
53- return True
54-
55-
5645class ClampConverter (NodeConverter ):
5746 RELU_COMPATIBLE_BOUNDS = {
5847 "ReluN1To1" : (- 1 , 1 ),
@@ -70,12 +59,25 @@ class ClampConverter(NodeConverter):
7059
7160 # noinspection PyShadowingBuiltins
7261 @staticmethod
73- def _get_clamp_bounds ( clamp_node : Node ) -> tuple [float | None , float | None ]:
62+ def _get_bounds ( node : Node ) -> tuple [float | None , float | None ]:
7463 """Extract min and max bounds from `aten.clamp.default` node."""
75- min = try_get_arg (clamp_node , 1 )
76- max = try_get_arg (clamp_node , 2 )
64+ min = try_get_arg (node , 1 )
65+ max = try_get_arg (node , 2 )
7766 return min , max
7867
68+ @classmethod
69+ def _is_convertible_to_relu (cls , node ):
70+ bounds = cls ._get_bounds (node )
71+ bounds = tuple (
72+ v if v is not None and math .isfinite (v ) else None for v in bounds
73+ )
74+
75+ # Some specific bounds can be replaced with single op ReLU.
76+ if bounds not in cls .RELU_COMPATIBLE_BOUNDS .values ():
77+ return False
78+
79+ return True
80+
7981 @staticmethod
8082 def _is_supported_in_IR (
8183 node : Node ,
@@ -100,20 +102,21 @@ def _io_quant_is_same(node: Node):
100102 dq_params = dequant .args [1 :]
101103 return all (q == dq for q , dq in zip (q_params , dq_params ))
102104
103- @staticmethod
105+ @classmethod
104106 def _is_supported_on_target (
107+ cls ,
105108 node : Node ,
106109 neutron_target_spec : NeutronTargetSpec ,
107110 parameters_mapping : dict [str , Parameter ],
108111 custom_delegation_options : CustomDelegationOptions ,
109112 ) -> bool :
110- relu_compatible = _is_convertible_to_relu (node )
111- bounds = ClampConverter . _get_clamp_bounds (node )
113+ relu_compatible = cls . _is_convertible_to_relu (node )
114+ bounds = cls . _get_bounds (node )
112115
113116 if all (b is None or math .isinf (b ) for b in bounds ):
114117 return False
115118
116- io_quant_consistent = ClampConverter ._io_quant_is_same (node )
119+ io_quant_consistent = cls ._io_quant_is_same (node )
117120 quant_supported = NodeConverter .uses_quantization_type_for_io (
118121 node ,
119122 supported_types = [torch .int8 , torch .uint8 ],
@@ -138,19 +141,20 @@ def supports_partitioning_result(
138141 neutron_target_spec : NeutronTargetSpec ,
139142 parameters_mapping : dict [str , Parameter ],
140143 ) -> bool :
141- bounds = cls ._get_clamp_bounds (node )
144+ bounds = cls ._get_bounds (node )
142145
143146 # Neutron cannot delegate a partition where ReLU or ReLU6 is the only operator
144147 # and at the same time the node does not satisfy delegation requirements.
145- # In contrast, ReLUN1To1 and ReLU0To1 are supported and delegated successfuly .
148+ # In contrast, ReLUN1To1 and ReLU0To1 are supported and delegated successfully .
146149 if bounds in cls .RELU_COMPATIBLE_BOUNDS .values ():
147150 is_alone_in_partition = cls .is_node_alone_in_partition (
148151 node , partition_list , filter_fn = is_not_qdq_node
149152 )
150153 if is_alone_in_partition :
154+ # noinspection PyTypeChecker
151155 return is_clamp_preserved_under_quantization (
152156 node ,
153- min_val = bounds [0 ],
157+ min_val = bounds [0 ] if bounds [ 0 ] is not None else 0 ,
154158 max_val = bounds [1 ],
155159 )
156160
@@ -167,9 +171,9 @@ def convert(self, node: Node):
167171 ) -> Tensor
168172 """
169173 self .assert_convertible (node )
170- to_relu = _is_convertible_to_relu (node )
174+ to_relu = self . _is_convertible_to_relu (node )
171175
172- bounds = self ._get_clamp_bounds (node )
176+ bounds = self ._get_bounds (node )
173177 bounds = tuple (
174178 v if v is not None and math .isfinite (v ) else None for v in bounds
175179 )
0 commit comments