Best Python code snippet using grail_python
broadcast_test.py
Source:broadcast_test.py
1# Copyright (C) 2018-2022 Intel Corporation2# SPDX-License-Identifier: Apache-2.03import unittest4import numpy as np5from generator import generator, generate6from openvino.tools.mo.front.common.partial_infer.utils import int64_array, undefined_shape_of_rank7from openvino.tools.mo.graph.graph import Node8from openvino.tools.mo.ops.broadcast import Broadcast9from unit_tests.utils.graph import build_graph, valued_const_with_data, regular_op_with_empty_data, \10 shaped_data11@generator12class BroadcastTest(unittest.TestCase):13 @generate(*[14 ([1], [3, 3], None, 'numpy', [[1, 1, 1], [1, 1, 1], [1, 1, 1]]),15 ([1], [3, 3], None, 'numpy'),16 # shape broadcasting17 ([1], [1, 2], [0], 'explicit'),18 ([1], [1, 2], [-2], 'explicit'),19 ([1, 7], [5, 1, 7, 3], [1, 2], 'explicit'),20 ([2, 1, 3], [2, 1, 3, 3], [0, 1, 2], 'explicit'),21 ([2, 1, 3], [5, 2, 1, 3], [1, 2, 3], 'explicit'),22 # value broadcasting23 ([1], [1, 2], [0], 'explicit', [[1, 1]]),24 ([[3, 1]], [2, 1, 2], [1, 2], 'explicit', [[[3, 1]], [[3, 1]]]), # ref_shape (2, 1, 2)25 ([[3, 1]], [2, 1, 2], [-2, -1], 'explicit', [[[3, 1]], [[3, 1]]]), # ref_shape (2, 1, 2)26 ([[[9, 5, 7]], [[9, 5, 7]]], [2, 2, 1, 3], [1, 2, 3], 'explicit', # in_shape (2, 1, 3)27 [[[[9, 5, 7]], [[9, 5, 7]]], [[[9, 5, 7]], [[9, 5, 7]]]]), # ref_out_shape (2, 2, 1, 3)28 ([[[9, 5, 7]], [[3, 4, 8]]], [2, 1, 3, 3], [0, 1, 2], 'explicit', # in_shape (2, 1, 3)29 [[[[9, 9, 9], [5, 5, 5], [7, 7, 7]]], [[[3, 3, 3], [4, 4, 4], [8, 8, 8]]]]), # ref_out_shape (2, 1, 3, 3)30 # negative tests31 ([1], [2, 2], [0], 'explicit', None, True),32 ([1, 7], [5, 2, 7, 3], [1, 2], 'explicit', None, True),33 ([1, 7], [5, 2, 7, 3], [2, 1], 'explicit', None, True),34 ([1, 7], [5, 2, 7, 3], [-3, -2], 'explicit', None, True),35 ])36 def test_broadcast(self, data, target_shape, axes_mapping=None, mode='numpy', ref_out=None, test_raising=False):37 if ref_out is not None:38 input = valued_const_with_data('data', int64_array(data))39 else:40 input = shaped_data('data', int64_array(data))41 nodes = {42 **input,43 **valued_const_with_data('target_shape', int64_array(target_shape)),44 **regular_op_with_empty_data('broadcast', {'op': 'Broadcast', 'mode': mode}),45 }46 edges = [('data', 'broadcast'),47 ('target_shape', 'broadcast'),48 ('broadcast', 'broadcast_d')]49 if axes_mapping is not None:50 nodes.update(**valued_const_with_data('axes_mapping', int64_array(axes_mapping)))51 edges.append(('axes_mapping', 'broadcast'))52 graph = build_graph(nodes, edges)53 broadcast_node = Node(graph, 'broadcast')54 if test_raising:55 self.assertRaises(AssertionError, Broadcast.infer, broadcast_node)56 return57 Broadcast.infer(broadcast_node)58 if ref_out is not None:59 self.assertTrue(np.array_equal(broadcast_node.out_node().value, np.array(ref_out)))60 else:61 self.assertTrue(np.array_equal(broadcast_node.out_node().shape, np.array(target_shape)))62 @generate(*[63 ([1], [3], 'numpy', undefined_shape_of_rank(3)),64 ([1], [3], 'explicit', undefined_shape_of_rank(3)),65 ([1, 2], [3], 'numpy', None, True),66 ])67 def test_broadcast_dynamic(self, data, target_shape_shape, mode='numpy', ref_out_shape=None, test_raising=False):68 nodes = {69 **shaped_data('data', int64_array(data)),70 **shaped_data('target_shape', int64_array(target_shape_shape)),71 **regular_op_with_empty_data('broadcast', {'op': 'Broadcast', 'mode': mode}),72 }73 edges = [('data', 'broadcast'),74 ('target_shape', 'broadcast'),75 ('broadcast', 'broadcast_d')]76 graph = build_graph(nodes, edges)77 broadcast_node = Node(graph, 'broadcast')78 if test_raising:79 self.assertRaises(AssertionError, Broadcast.infer, broadcast_node)80 return81 Broadcast.infer(broadcast_node)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!