예제 #1
0
    def test_eltwise_infer_none_val(self):
        graph = build_graph(
            nodes_attributes, [('node_1', 'eltw_1'), ('node_2', 'eltw_1'),
                               ('eltw_1', 'node_3'), ('node_3', 'op_output')],
            {
                'node_3': {
                    'shape': None
                },
                'node_1': {
                    'shape': np.array([1, 3, 256, 256]),
                    'value': None
                },
                'node_2': {
                    'shape': np.array([1, 3, 256, 256])
                }
            })
        graph.graph['layout'] = 'NCHW'
        eltwise_node = Node(graph, 'eltw_1')

        eltwise_infer(eltwise_node, lambda a, b: a * b)
        exp_shape = np.array([1, 3, 256, 256])
        res_shape = graph.node['node_3']['shape']
        res_value = eltwise_node.out_node().value
        for i in range(0, len(exp_shape)):
            self.assertEqual(exp_shape[i], res_shape[i])

        self.assertIsNone(res_value)
예제 #2
0
def nodes_dict(original,
               transformed=None,
               levels=255,
               data=None,
               il=[-127],
               ih=[127],
               ol=[-127],
               oh=[127]):
    shape = [1, 2, 3, 4] if data is None else np.array(data).shape
    data = np.ones(shape, dtype=original) if data is None else np.array(
        data, dtype=original)
    int_data = data.astype(dtype=np.int8)
    transformed = transformed if transformed is not None else original

    return {
        **valued_const_with_data('weights', data),
        **valued_const_with_data('int_weights', int_data),
        **regular_op_with_shaped_data(
            'weights_cast', shape, {
                'type': 'Convert',
                'op': 'Cast',
                'infer': Cast.infer,
                'dst_type': np.float32
            }),
        **regular_op_with_shaped_data(
            'cast', shape, {
                'type': 'Convert',
                'op': 'Cast',
                'infer': Cast.infer,
                'dst_type': transformed
            }),
        **valued_const_with_data('il', np.array(il)),
        **valued_const_with_data('ih', np.array(ih)),
        **valued_const_with_data('ol', np.array(ol)),
        **valued_const_with_data('oh', np.array(oh)),
        **regular_op_with_shaped_data(
            'FQ', shape, {
                'type': 'FakeQuantize',
                'infer': FakeQuantize.infer,
                'stop_value_propagation': True,
                'levels': levels,
                'op': 'FakeQuantize'
            }),
        **valued_const_with_data('zp', np.array([0])),
        **valued_const_with_data('scale', np.array([1])),
        **regular_op_with_shaped_data(
            'sub', shape, {
                'type': 'Subtract',
                'op': 'Sub',
                'infer': lambda node: eltwise_infer(node, Sub.operation)
            }),
        **regular_op_with_shaped_data(
            'mul', shape, {
                'type': 'Multiply',
                'op': 'Mul',
                'infer': lambda node: eltwise_infer(node, Mul.operation)
            }),
        **result()
    }
예제 #3
0
    def infer(node: Node):
        name = node.soft_get('name', node.id)
        connected_inputs = {idx: port for idx, port in node.in_ports().items() if not port.disconnected()}
        assert len(connected_inputs) == 1 and 0 in connected_inputs, \
            "AttributedPower should have 1 connected input port, but it doesn't for node: `{}`. Ports: {}" \
            "".format(name, connected_inputs)

        assert node.has_valid('scale'), \
            'AttributedPower operation should have `scale` parameter set, but it doesn`t for node {}'.format(name)
        assert node.has_valid('shift'), \
            'AttributedPower operation should have `shift` parameter set, but it doesn`t for node {}'.format(name)
        assert node.has_valid('power'), \
            'AttributedPower operation should have `power` parameter set, but it doesn`t for node {}'.format(name)

        eltwise_infer(node, lambda a: np.power(a * node.scale + node.shift, node.power))
예제 #4
0
    def __init__(self, graph: Graph, attrs: dict):
        operations = {
            'sum': ('Add', lambda a, b: a + b),
            'mul': ('Mul', lambda a, b: a * b),
            'max': ('Max', lambda a, b: np.ma.maximum(a, b)),
            'pow': ('Pow', lambda a, b: np.ma.power(a, b)),
            'less': ('Less', lambda a, b: np.ma.less(a, b)),
            'less_equal': ('LessEqual', lambda a, b: np.ma.less_equal(a, b)),
            'greater': ('Greater', lambda a, b: np.ma.greater(a, b)),
            'greater_equal':
            ('GreaterEqual', lambda a, b: np.ma.greater_equal(a, b)),
            'equal': ('Equal', lambda a, b: np.ma.equal(a, b)),
            'floor_mod': ('FloorMod', lambda a, b: np.ma.fmod(a, b)),
            'not_equal': ('NotEqual', lambda a, b: np.ma.not_equal(a, b)),
            'logical_or': ('LogicalOr', lambda a, b: bool(a) or bool(b)),
            'logical_and': ('LogicalAnd', lambda a, b: bool(a) and bool(b)),
            'logical_xor': ('LogicalXor', lambda a, b: bool(a) ^ bool(b)),
            'log': ('Log', lambda x: np.ma.log(x)),
        }

        super().__init__(
            graph, {
                'type':
                self.op,
                'op':
                operations[attrs['operation']][0],
                'infer':
                lambda node: eltwise_infer(node, operations[node.operation][1]
                                           ),
                'in_ports_count':
                2,
                'out_ports_count':
                1,
            }, attrs)
예제 #5
0
    def test_eltwise_infer_none_min_max(self):
        graph = build_graph(nodes_attributes,
                            [('node_1', 'eltw_1'),
                             ('node_2', 'eltw_1'),
                             ('eltw_1', 'node_3'),
                             ('node_3', 'op_output')
                             ],
                            {'node_3': {'shape': None},
                             'node_1': {'shape': np.array([1, 3, 257, 256])},
                             'node_2': {'shape': np.array([1, 3, 256, 257])}
                             })
        graph.graph['layout'] = 'NCHW'
        eltwise_node = Node(graph, 'eltw_1')

        with self.assertRaisesRegex(Error, 'Input shapes mismatch*'):
            eltwise_infer(eltwise_node)
예제 #6
0
    def test_eltwise_infer(self, value1, shape1, value2, shape2, shape_infer,
                           exp_value, exp_shape):
        graph = build_graph(
            nodes_attributes, [('node_1', 'eltw_1'), ('node_2', 'eltw_1'),
                               ('eltw_1', 'node_3'), ('node_3', 'op_output')],
            {
                'node_3': {
                    'shape': None
                },
                'node_1': {
                    'shape':
                    shape_array(value1).shape
                    if value1 is not None else shape_array(shape1),
                    'value':
                    value1
                },
                'node_2': {
                    'shape':
                    shape_array(value2).shape
                    if value2 is not None else shape_array(shape2),
                    'value':
                    value2
                }
            })

        graph.graph['layout'] = 'NCHW'

        eltwise_node = Node(graph, 'eltw_1')

        eltwise_infer(eltwise_node, shape_infer)
        res_shape = graph.node['node_3']['shape']
        res_value = eltwise_node.out_node().value
        if exp_value is not None:
            self.assertTrue(
                strict_compare_tensors(res_value, shape_array(exp_value)))
        self.assertTrue(
            strict_compare_tensors(res_shape, shape_array(exp_shape)))
예제 #7
0
 def __init__(self, graph: Graph, attrs: dict):
     super().__init__(graph, {
         'op': self.op,
         'type': self.op_type,
         'version': self.version,
         'infer': lambda node: eltwise_infer(node, self.operation),
         'type_infer': self.type_infer,
         'can_be_bias': True,
         'can_be_fused': True,
         'in_ports_count': 2,
         'out_ports_count': 1,
         'is_eltwise': True,
         'stop_value_propagation': False,
         'auto_broadcast': 'numpy'
     }, attrs)
    def test_zero_point_optimization(self, weights, zero_point, adj_weights,
                                     adj_zero_point):
        nodes = lambda w, zp: {
            **valued_const_with_data('weights', np.array(w, dtype=np.int8)),
            **regular_op_with_shaped_data(
                'cast', [len(w)], {
                    'type': 'Convert',
                    'op': 'Cast',
                    'infer': Cast.infer,
                    'dst_type': np.float32
                }),
            **valued_const_with_data('zp', np.array(zp, dtype=np.float32)),
            **regular_op_with_shaped_data(
                'sub', [len(w)], {
                    'type': 'Subtract',
                    'op': 'Sub',
                    'infer': lambda node: eltwise_infer(node, Sub.operation)
                }),
            **result()
        }
        edges = [
            *connect("weights:0", "0:cast"),
            *connect("cast:0", "0:sub"),
            *connect("zp:0", "1:sub"),
            *connect("sub:0", "0:output"),
        ]
        graph = build_graph(nodes(weights, zero_point),
                            edges,
                            nodes_with_edges_only=True)
        ZeroPointOptimizer().find_and_replace_pattern(graph)
        graph.clean_up()

        graph_ref = build_graph(nodes(adj_weights, adj_zero_point), [
            *connect("weights:0", "0:cast"),
            *connect("cast:0", "0:output"),
        ],
                                nodes_with_edges_only=True)
        graph_ref.clean_up()

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
예제 #9
0
 def infer(cls, node: Node):
     return eltwise_infer(node, node.operation)
예제 #10
0
 def infer(node: Node):
     return eltwise_infer(node, lambda x, negative_slope: LeakyReLU.leaky_relu(x, negative_slope),
                          negative_slope=node.negative_slope)
예제 #11
0
 def infer(cls, node: Node):
     return eltwise_infer(node, lambda x, alpha: ThresholdedRelu.thresholded_relu(x, alpha), alpha=node.alpha)
예제 #12
0
 def infer(cls, node: Node):
     return eltwise_infer(node, lambda x, alpha: Elu.elu(x, alpha), alpha=node.alpha)
const_value = np.random.rand(1, 3, 30, 30)
nodes_attributes = {'input': {'shape': int64_array([1, 3, 30, 30]), 'type': 'Parameter', 'kind': 'op',
                              'op': 'Parameter'},
                    'input_data': {'value': None, 'shape': int64_array([1, 3, 30, 30]), 'kind': 'data'},
                    'const': {'type': 'Const', 'kind': 'op', 'op': 'Const', 'value': const_value},
                    'const_data': {'kind': 'data', 'value': const_value},
                    'shapeof_input': {'kind': 'op', 'op': 'ShapeOf', 'value': int64_array([1, 3, 30, 30])},
                    'shapeof_input_data': {'value': None, 'shape': None, 'kind': 'data',
                                           'value': int64_array([1, 3, 30, 30])},

                    'shapeof_const': {'kind': 'op', 'op': 'ShapeOf', 'value': int64_array([1, 3, 30, 30])},
                    'shapeof_const_data': {'value': None, 'shape': None, 'kind': 'data',
                                           'value': int64_array([1, 3, 30, 30])},

                    'mul': {'kind': 'op', 'op': 'Mul', 'infer': lambda node: eltwise_infer(node, lambda a, b: a * b)},
                    'mul_data': {'kind': 'data', 'value': np.array([1, 9, 900, 900])},
                    'last': {'kind': 'op', 'op': 'Result'},

                    # new nodes
                    'new_const_shapeof': {'type': 'Const', 'kind': 'op', 'op': 'Const',
                                          'value': int64_array([1, 3, 30, 30])}
                    }

const_value2 = np.random.rand(30, 30)
nodes_attributes2 = {'input': {'shape': int64_array([1, 3, 30, 30]), 'type': 'Parameter', 'kind': 'op',
                               'op': 'Parameter'},
                     'input_data': {'value': None, 'shape': int64_array([1, 3, 30, 30]), 'kind': 'data'},

                     'const': {'type': 'Const', 'kind': 'op', 'op': 'Const', 'value': const_value2},
                     'const_data': {'kind': 'data', 'value': const_value2},
예제 #14
0
     'kind': 'data',
     'shape': None
 },
 'mul_val': {
     'kind': 'op',
     'op': 'Const'
 },
 'mul_val_d': {
     'kind': 'data',
     'shape': np.array([1]),
     'value': np.array([5])
 },
 'mul': {
     'kind': 'op',
     'op': 'Mul',
     'infer': lambda node: eltwise_infer(node, lambda a, b: a * b)
 },
 'mul_d': {
     'kind': 'data',
     'shape': np.array([1, 3, 224, 224]),
     'value': None
 },
 'mul_1': {
     'kind': 'op',
     'op': 'Mul',
     'infer': lambda node: eltwise_infer(node, lambda a, b: a * b)
 },
 'mul_1_d': {
     'kind': 'data',
     'shape': np.array([1, 3, 224, 224]),
     'value': None