def test_model_set_element_type(): model = init_model() place = model.get_place_by_tensor_name(tensorName="") model.set_element_type(place=place, type=get_element_type(np.int32)) stat = get_mdl_stat(model) assert stat.set_element_type == 1 assert stat.lastArgPlace == place assert stat.lastArgElementType == get_element_type(np.int32)
def test_reduce(): from functools import reduce np.random.seed(133391) # default reduce all axes init_val = np.float32(0.) input_data = np.random.randn(3, 4, 5).astype(np.float32) expected = np.sum(input_data) reduction_function_args = [init_val, ng.impl.op.Add] result = run_op_node([input_data], ng.reduce, *reduction_function_args) assert np.allclose(result, expected) reduction_axes = (0, 2) init_val = np.float32(0.) input_data = np.random.randn(3, 4, 5).astype(np.float32) expected = np.sum(input_data, axis=reduction_axes) reduction_function_args = [init_val, ng.impl.op.Add, list(reduction_axes)] result = run_op_node([input_data], ng.reduce, *reduction_function_args) assert np.allclose(result, expected) reduction_axes = (0, ) input_data = np.random.randn(100).astype(np.float32) expected = reduce(lambda x, y: x - y, input_data, np.float32(0.)) reduction_function_args = [ init_val, ng.impl.op.Subtract, list(reduction_axes) ] result = run_op_node([input_data], ng.reduce, *reduction_function_args) assert np.allclose(result, expected) reduction_axes = (0, ) input_data = np.random.randn(100).astype(np.float32) expected = reduce(lambda x, y: x + y * y, input_data, np.float32(0.)) reduction_function_args = [ init_val, lambda x, y: x + y * y, list(reduction_axes) ] result = run_op_node([input_data], ng.reduce, *reduction_function_args) assert np.allclose(result, expected) def custom_reduction_function(a, b): return a + b * b param1 = ng.impl.op.Parameter(get_element_type(np.float32), Shape([])) param2 = ng.impl.op.Parameter(get_element_type(np.float32), Shape([])) reduction_operation = Function( NodeVector([custom_reduction_function(param1, param2)]), [param1, param2], 'reduction_op') reduction_function_args = [ init_val, reduction_operation, list(reduction_axes) ] result = run_op_node([input_data], ng.reduce, *reduction_function_args) assert np.allclose(result, expected)
def test_element_type(self, mock_argparse): main(argparse.ArgumentParser(), fem, 'mock_mo_ngraph_frontend') stat = get_model_statistic() # verify that 'set_element_type' was called assert stat.set_element_type == 1 assert stat.lastArgElementType == get_element_type(np.int8)
def parameter(shape, dtype=np.float32, name=None): # type: (TensorShape, NumericType, str) -> Parameter """Return an ngraph Parameter object.""" assert_list_of_ints(shape, 'Parameter shape must be a list of integer values.') element_type = get_element_type(dtype) return Parameter(element_type, Shape(shape))
def test_eye_batch_shape(num_rows, num_columns, diagonal_index, batch_shape, out_type): num_rows_array = np.array([num_rows], np.int32) num_columns_array = np.array([num_columns], np.int32) diagonal_index_array = np.array([diagonal_index], np.int32) batch_shape_array = np.array(batch_shape, np.int32) num_rows_tensor = ng.constant(num_rows_array) num_columns_tensor = ng.constant(num_columns_array) diagonal_index_tensor = ng.constant(diagonal_index_array) batch_shape_tensor = ng.constant(batch_shape_array) # Create with param names eye_node = ng.eye(num_rows=num_rows_tensor, num_columns=num_columns_tensor, diagonal_index=diagonal_index_tensor, batch_shape=batch_shape_tensor, output_type=get_element_type_str(out_type)) # Create with default orded eye_node = ng.eye(num_rows_tensor, num_columns_tensor, diagonal_index_tensor, get_element_type_str(out_type), batch_shape_tensor) output_shape = [*batch_shape, 1, 1] one_matrix = np.eye(num_rows, M=num_columns, k=diagonal_index, dtype=np.float32) expected_results = np.tile(one_matrix, output_shape) assert eye_node.get_type_name() == "Eye" assert eye_node.get_output_size() == 1 assert eye_node.get_output_element_type(0) == get_element_type(out_type) assert tuple(eye_node.get_output_shape(0)) == expected_results.shape
def test_reduce(): from functools import reduce np.random.seed(133391) # default reduce all axes init_val = np.float32(0.) input_data = np.random.randn(3, 4, 5).astype(np.float32) expected = np.sum(input_data) reduction_function_args = [init_val, ng.impl.op.Add] result = run_op_node([input_data], ng.reduce, *reduction_function_args) assert np.allclose(result, expected) reduction_axes = (0, 2) init_val = np.float32(0.) input_data = np.random.randn(3, 4, 5).astype(np.float32) expected = np.sum(input_data, axis=reduction_axes) reduction_function_args = [init_val, ng.impl.op.Add, list(reduction_axes)] result = run_op_node([input_data], ng.reduce, *reduction_function_args) assert np.allclose(result, expected) reduction_axes = (0, ) input_data = np.random.randn(100).astype(np.float32) expected = reduce(lambda x, y: x - y, input_data, np.float32(0.)) reduction_function_args = [init_val, ng.impl.op.Subtract, list(reduction_axes)] result = run_op_node([input_data], ng.reduce, *reduction_function_args) assert np.allclose(result, expected) reduction_axes = (0, ) input_data = np.random.randn(100).astype(np.float32) expected = reduce(lambda x, y: x + y * y, input_data, np.float32(0.)) reduction_function_args = [init_val, lambda x, y: x + y * y, list(reduction_axes)] result = run_op_node([input_data], ng.reduce, *reduction_function_args) assert np.allclose(result, expected) def custom_reduction_function(a, b): return a + b * b param1 = ng.impl.op.Parameter(get_element_type(np.float32), Shape([])) param2 = ng.impl.op.Parameter(get_element_type(np.float32), Shape([])) reduction_operation = Function(NodeVector([custom_reduction_function(param1, param2)]), [param1, param2], 'reduction_op') reduction_function_args = [init_val, reduction_operation, list(reduction_axes)] result = run_op_node([input_data], ng.reduce, *reduction_function_args) assert np.allclose(result, expected)
def argmin(data, # type: Node axis=0, # type: int ): # type: (...) -> Node """Return a node which performs ArgMin index reduction operation. :param data: Input data. :param axis: Reduction Axis. :return: The new node which performs ArgMin """ return ArgMin(data, axis, get_element_type(np.int32))
def einsum_op_exec(input_shapes: list, equation: str, data_type: np.dtype, with_value=False, seed=202104): """Test Einsum operation for given input shapes, equation, and data type. It generates input data of given shapes and type, receives reference results using numpy, and tests IE implementation by matching with reference numpy results. :param input_shapes: a list of tuples with shapes :param equation: Einsum equation :param data_type: a type of input data :param with_value: if True - tests output data shape and type along with its value, otherwise, tests only the output shape and type :param seed: a seed for random generation of input data """ np.random.seed(seed) num_inputs = len(input_shapes) runtime = get_runtime() # set absolute tolerance based on the data type atol = 0.0 if np.issubdtype(data_type, np.integer) else 1e-04 # generate input tensors ng_inputs = [] np_inputs = [] for i in range(num_inputs): input_i = np.random.random_integers( 10, size=input_shapes[i]).astype(data_type) np_inputs.append(input_i) ng_inputs.append(ng.parameter(input_i.shape, dtype=data_type)) expected_result = np.einsum(equation, *np_inputs) einsum_model = ng.einsum(ng_inputs, equation) # check the output shape and type assert einsum_model.get_type_name() == "Einsum" assert einsum_model.get_output_size() == 1 assert list(einsum_model.get_output_shape(0)) == list( expected_result.shape) assert einsum_model.get_output_element_type(0) == get_element_type( data_type) # check inference result if with_value: computation = runtime.computation(einsum_model, *ng_inputs) actual_result = computation(*np_inputs) np.allclose(actual_result, expected_result, atol=atol)
def topk( data, # type: Node k, # type: int kaxis=-1, # type: int cmax=True, # type: bool ): # type: (...) -> Node """Return a node which performs TopK. :param data: Input data. :param kaxis: TopK Axis. :param k: K. :param cmax: Compute TopK largest (True) or smallest (False) :return: The new node which performs TopK (both indices and values) """ return TopK(data, len(data.get_shape()) - 1 if kaxis == -1 else kaxis, get_element_type(np.int32), k, cmax)
def convert(node, new_type, name=None): # type: (Node, NumericType, str) -> Node """Return node which casts input node values to specified type.""" new_element_type = get_element_type(new_type) return Convert(node, new_element_type)
def moc_pipeline(argv: argparse.Namespace, moc_front_end: FrontEnd): """ Load input model and convert it to nGraph function :param: argv: parsed command line arguments :param: moc_front_end: Loaded Frontend for converting input model :return: converted nGraph function ready for serialization """ input_model = moc_front_end.load(argv.input_model) user_shapes, outputs, freeze_placeholder = fe_user_data_repack( input_model, argv.placeholder_shapes, argv.placeholder_data_types, argv.output, argv.freeze_placeholder_with_value) def check_places_are_same(places_original: List[Place], places_new: List[Place]): """ Check if set of new places is same as original or not. :param places_original: List[Place] Original model places :param places_new: List[Place] New list of places :return: True if new list of places is same as original """ return len(places_original) == len(places_new) and len([ item for item in places_original if any([item.is_equal(item2['node']) for item2 in places_new]) ]) == len(places_original) inputs_equal = True if user_shapes: inputs_equal = check_places_are_same(input_model.get_inputs(), user_shapes) outputs_equal = True if outputs: outputs_equal = check_places_are_same(input_model.get_outputs(), outputs) log.debug('Inputs are same: {}, outputs are same: {}'.format( inputs_equal, outputs_equal)) if not inputs_equal and not outputs_equal: # Use ExtractSubgraph new_input_places = [x['node'] for x in user_shapes] new_output_places = [x['node'] for x in outputs] log.debug('Using extract subgraph') input_model.extract_subgraph(new_input_places, new_output_places) elif not inputs_equal: new_input_places = [x['node'] for x in user_shapes] log.debug('Using override_all_inputs') input_model.override_all_inputs(new_input_places) elif not outputs_equal: new_output_places = [x['node'] for x in outputs] log.debug('Using override_all_outputs') input_model.override_all_outputs(new_output_places) if user_shapes: for user_shape in user_shapes: if user_shape.get('shape') is not None: input_model.set_partial_shape( user_shape['node'], PartialShape(user_shape['shape'])) if user_shape.get('data_type') is not None: data_type = get_element_type(user_shape['data_type']) log.debug('Set data type: {}'.format(data_type)) input_model.set_element_type(user_shape['node'], data_type) def shape_to_array(shape: PartialShape): return [shape.get_dimension(i) for i in range(shape.rank.get_length())] # Set batch size if argv.batch is not None and argv.batch > 0: log.debug('Setting batch size to {}'.format(argv.batch)) for place in input_model.get_inputs(): old_partial_shape = input_model.get_partial_shape(place) old_shape_array = shape_to_array( old_partial_shape) if old_partial_shape.rank.is_static else [] joined_name = ' '.join(place.get_names()) validate_batch_in_shape(old_shape_array, joined_name) # Assume batch size is always 1-st dimension in shape # Keep other dimensions unchanged new_shape = [ old_partial_shape.get_dimension(i) for i in range(old_partial_shape.rank.get_length()) ] new_shape[0] = Dimension(argv.batch) new_partial_shape = PartialShape(new_shape) log.debug('Input: {}, Old shape: {}, New shape: {}'.format( joined_name, old_shape_array, new_shape)) input_model.set_partial_shape(place, new_partial_shape) ngraph_function = moc_front_end.convert(input_model) return ngraph_function