def test_ngraph_function_api(): shape = [2, 2] parameter_a = ng.parameter(shape, dtype=np.float32, name="A") parameter_b = ng.parameter(shape, dtype=np.float32, name="B") parameter_c = ng.parameter(shape, dtype=np.float32, name="C") model = (parameter_a + parameter_b) * parameter_c function = Function(model, [parameter_a, parameter_b, parameter_c], "TestFunction") function.get_parameters()[1].set_partial_shape(PartialShape([3, 4, 5])) ordered_ops = function.get_ordered_ops() op_types = [op.get_type_name() for op in ordered_ops] assert op_types == [ "Parameter", "Parameter", "Parameter", "Add", "Multiply", "Result" ] assert len(function.get_ops()) == 6 assert function.get_output_size() == 1 assert function.get_output_op(0).get_type_name() == "Result" assert function.get_output_element_type( 0) == parameter_a.get_element_type() assert list(function.get_output_shape(0)) == [2, 2] assert (function.get_parameters()[1].get_partial_shape()) == PartialShape( [3, 4, 5]) assert len(function.get_parameters()) == 3 assert len(function.get_results()) == 1 assert function.get_friendly_name() == "TestFunction"
def test_partial_shape_equals(): ps1 = PartialShape.dynamic() ps2 = PartialShape.dynamic() assert ps1 == ps2 ps1 = PartialShape([1, 2, 3]) ps2 = PartialShape([1, 2, 3]) assert ps1 == ps2 shape = Shape([1, 2, 3]) ps = PartialShape([1, 2, 3]) assert shape == ps
def test_node_input(): shape = [2, 2] parameter_a = ng.parameter(shape, dtype=np.float32, name="A") parameter_b = ng.parameter(shape, dtype=np.float32, name="B") model = parameter_a + parameter_b model_inputs = model.inputs() assert len(model_inputs) == 2 assert [input_node.get_index() for input_node in model_inputs] == [0, 1] assert np.equal( [input_node.get_element_type() for input_node in model_inputs], model.get_element_type(), ).all() assert np.equal([input_node.get_shape() for input_node in model_inputs], Shape(shape)).all() assert np.equal( [input_node.get_partial_shape() for input_node in model_inputs], PartialShape(shape), ).all() input0 = model.input(0) input1 = model.input(1) assert [input0.get_index(), input1.get_index()] == [0, 1]
def test_node_output(): input_array = np.array([0, 1, 2, 3, 4, 5]) splits = 3 expected_shape = len(input_array) // splits input_tensor = ng.constant(input_array, dtype=np.int32) axis = ng.constant(0, dtype=np.int64) split_node = ng.split(input_tensor, axis, splits) split_node_outputs = split_node.outputs() assert len(split_node_outputs) == splits assert [output_node.get_index() for output_node in split_node_outputs] == [0, 1, 2] assert np.equal( [output_node.get_element_type() for output_node in split_node_outputs], input_tensor.get_element_type(), ).all() assert np.equal( [output_node.get_shape() for output_node in split_node_outputs], Shape([expected_shape]), ).all() assert np.equal( [output_node.get_partial_shape() for output_node in split_node_outputs], PartialShape([expected_shape]), ).all() output0 = split_node.output(0) output1 = split_node.output(1) output2 = split_node.output(2) assert [output0.get_index(), output1.get_index(), output2.get_index()] == [0, 1, 2]
def __call__(self, *input_values: NumericData) -> List[NumericData]: """Run computation on input values and return result.""" input_values = [np.array(input_value) for input_value in input_values] input_shapes = [get_shape(input_value) for input_value in input_values] param_names = [param.friendly_name for param in self.parameters] if self.network_cache.get(str(input_shapes)) is None: capsule = Function.to_capsule(self.function) cnn_network = IENetwork(capsule) if self.function.is_dynamic(): cnn_network.reshape(dict(zip(param_names, input_shapes))) # Convert unsupported inputs of the network _convert_inputs(cnn_network) self.network_cache[str(input_shapes)] = cnn_network else: cnn_network = self.network_cache[str(input_shapes)] executable_network = self.runtime.backend.load_network( cnn_network, self.runtime.backend_name) # Input validation if len(input_values) != len(self.parameters): raise UserInputError("Expected %s parameters, received %s.", len(self.parameters), len(input_values)) for parameter, input in zip(self.parameters, input_values): parameter_shape = parameter.get_output_partial_shape(0) input_shape = PartialShape(input.shape) if len(input.shape) > 0 and not parameter_shape.compatible( input_shape): raise UserInputError( "Provided tensor's shape: %s does not match the expected: %s.", input_shape, parameter_shape, ) request = executable_network.requests[0] request.infer(dict(zip(param_names, input_values))) # Set order of output blobs compatible with nG Function result_buffers = [ self.__get_ie_output_blob_buffer(request.output_blobs, result) for result in self.results ] # Since OV overwrite result data type we have to convert results to the original one. original_dtypes = [ get_dtype(result.get_output_element_type(0)) for result in self.results ] converted_buffers = [ buffer.astype(original_dtype) for buffer, original_dtype in zip(result_buffers, original_dtypes) ] return converted_buffers
def test_runtime_info(): test_shape = PartialShape([1, 1, 1, 1]) test_type = Type.f32 test_param = Parameter(test_type, test_shape) relu_node = ng.relu(test_param) runtime_info = relu_node.get_rt_info() runtime_info["affinity"] = "test_affinity" relu_node.set_friendly_name("testReLU") runtime_info_after = relu_node.get_rt_info() assert runtime_info_after["affinity"] == "test_affinity"
def test_repr_dynamic_shape(): shape = PartialShape([-1, 2]) parameter_a = ng.parameter(shape, dtype=np.float32, name="A") parameter_b = ng.parameter(shape, dtype=np.float32, name="B") model = parameter_a + parameter_b function = Function(model, [parameter_a, parameter_b], "simple_dyn_shapes_graph") assert repr(function) == "<Function: 'simple_dyn_shapes_graph' ({?,2})>" ops = function.get_ordered_ops() for op in ops: assert "{?,2}" in repr(op)
def test_sink_function_ctor(): input_data = ng.parameter([2, 2], name="input_data", dtype=np.float32) rv = ng.read_value(input_data, "var_id_667") add = ng.add(rv, input_data, name="MemoryAdd") node = ng.assign(add, "var_id_667") res = ng.result(add, "res") function = Function(results=[res], sinks=[node], parameters=[input_data], name="TestFunction") ordered_ops = function.get_ordered_ops() op_types = [op.get_type_name() for op in ordered_ops] assert op_types == ["Parameter", "ReadValue", "Add", "Assign", "Result"] assert len(function.get_ops()) == 5 assert function.get_output_size() == 1 assert function.get_output_op(0).get_type_name() == "Result" assert function.get_output_element_type(0) == input_data.get_element_type() assert list(function.get_output_shape(0)) == [2, 2] assert (function.get_parameters()[0].get_partial_shape()) == PartialShape([2, 2]) assert len(function.get_parameters()) == 1 assert len(function.get_results()) == 1 assert function.get_friendly_name() == "TestFunction"
def __call__(self, *input_values: NumericData) -> List[NumericData]: """Run computation on input values and return result.""" input_values = [np.array(input_value) for input_value in input_values] input_shapes = [get_shape(input_value) for input_value in input_values] if self.network_cache.get(str(input_shapes)) is None: capsule = Function.to_capsule(self.function) cnn_network = IENetwork(capsule) if self.function.is_dynamic(): param_names = [ param.friendly_name for param in self.parameters ] cnn_network.reshape(dict(zip(param_names, input_shapes))) self.network_cache[str(input_shapes)] = cnn_network else: cnn_network = self.network_cache[str(input_shapes)] executable_network = self.runtime.backend.load_network( cnn_network, self.runtime.backend_name) # Input validation if len(input_values) != len(self.parameters): raise UserInputError("Expected %s parameters, received %s.", len(self.parameters), len(input_values)) for parameter, input in zip(self.parameters, input_values): parameter_shape = parameter.get_output_partial_shape(0) input_shape = PartialShape(input.shape) if len(input.shape) > 0 and not parameter_shape.compatible( input_shape): raise UserInputError( "Provided tensor's shape: %s does not match the expected: %s.", input_shape, parameter_shape, ) request = executable_network.requests[0] request.infer(dict(zip(request._inputs_list, input_values))) return [blob.buffer for blob in request.output_blobs.values()]
def test_runtime_info(): test_shape = PartialShape([1, 3, 22, 22]) test_type = Type.f32 test_param = Parameter(test_type, test_shape) relu_node = ng.relu(test_param) runtime_info = relu_node.get_rt_info() runtime_info["affinity"] = "test_affinity" relu_node.set_friendly_name("testReLU") runtime_info_after = relu_node.get_rt_info() assert runtime_info == runtime_info_after params = [test_param] results = [relu_node] ng_function = Function(results, params, "testFunc") capsule = Function.to_capsule(ng_function) cnn_network = IENetwork(capsule) cnn_layer = cnn_network.layers["testReLU"] assert cnn_layer is not None assert cnn_layer.affinity == "test_affinity"
def test_partial_shape(): ps = PartialShape([1, 2, 3, 4]) assert ps.is_static assert not ps.is_dynamic assert ps.rank == 4 assert repr(ps) == "<PartialShape: {1,2,3,4}>" assert ps.get_dimension(0) == Dimension(1) assert ps.get_dimension(1) == Dimension(2) assert ps.get_dimension(2) == Dimension(3) assert ps.get_dimension(3) == Dimension(4) shape = Shape([1, 2, 3]) ps = PartialShape(shape) assert ps.is_static assert not ps.is_dynamic assert ps.all_non_negative assert ps.rank == 3 assert list(ps.get_shape()) == [1, 2, 3] assert list(ps.get_max_shape()) == [1, 2, 3] assert list(ps.get_min_shape()) == [1, 2, 3] assert list(ps.to_shape()) == [1, 2, 3] assert repr(shape) == "<Shape: {1, 2, 3}>" assert repr(ps) == "<PartialShape: {1,2,3}>" ps = PartialShape( [Dimension(1), Dimension(2), Dimension(3), Dimension.dynamic()]) assert not ps.is_static assert ps.is_dynamic assert ps.all_non_negative assert ps.rank == 4 assert list(ps.get_min_shape()) == [1, 2, 3, 0] assert list(ps.get_max_shape())[3] > 1000000000 assert repr(ps) == "<PartialShape: {1,2,3,?}>" assert ps.get_dimension(0) == Dimension(1) assert ps.get_dimension(1) == Dimension(2) assert ps.get_dimension(2) == Dimension(3) assert ps.get_dimension(3) == Dimension.dynamic() ps = PartialShape([1, 2, 3, -1]) assert not ps.is_static assert ps.is_dynamic assert ps.all_non_negative assert ps.rank == 4 assert list(ps.get_min_shape()) == [1, 2, 3, 0] assert list(ps.get_max_shape())[3] > 1000000000 assert repr(ps) == "<PartialShape: {1,2,3,?}>" ps = PartialShape.dynamic() assert not ps.is_static assert ps.is_dynamic assert ps.rank == Dimension.dynamic() assert list(ps.get_min_shape()) == [] assert list(ps.get_max_shape()) == [] assert repr(ps) == "<PartialShape: ?>" ps = PartialShape.dynamic(r=Dimension(2)) assert not ps.is_static assert ps.is_dynamic assert ps.rank == 2 assert 2 == ps.rank assert list(ps.get_min_shape()) == [0, 0] assert list(ps.get_max_shape())[0] > 1000000000 assert repr(ps) == "<PartialShape: {?,?}>"
def test_partial_shape_refinement(): ps1 = PartialShape.dynamic() ps2 = PartialShape.dynamic() assert ps1.refines(ps2) assert ps1.relaxes(ps2) assert ps2.refines(ps1) assert ps2.relaxes(ps1) ps1 = PartialShape.dynamic() ps2 = PartialShape([3, -1, 7, 9]) assert not ps1.refines(ps2) assert ps1.relaxes(ps2) assert ps2.refines(ps1) assert not ps2.relaxes(ps1) ps1 = PartialShape.dynamic() ps2 = PartialShape([3, 5, 7, 9]) assert not ps1.refines(ps2) assert ps1.relaxes(ps2) assert ps2.refines(ps1) assert not ps2.relaxes(ps1)
def test_partial_shape_same_scheme(): ps1 = PartialShape([1, 2, -1]) ps2 = PartialShape([1, 3, -1]) assert not ps1.same_scheme(ps2) ps1 = PartialShape([1, 2, -1]) ps2 = PartialShape([1, 2, -1]) assert ps1.same_scheme(ps2) ps1 = PartialShape([1, 2, 3]) ps2 = PartialShape([1, 2, 3]) assert ps1.same_scheme(ps2) ps1 = PartialShape([-1, 2, 3]) ps2 = PartialShape([1, -1, 3]) assert not ps1.same_scheme(ps2) ps1 = PartialShape.dynamic() ps2 = PartialShape.dynamic() assert ps1.same_scheme(ps2)
def test_partial_shape_compatible(): ps1 = PartialShape.dynamic() ps2 = PartialShape.dynamic() assert ps1.compatible(ps2) ps1 = PartialShape([3]) ps2 = PartialShape.dynamic() assert ps1.compatible(ps2) ps1 = PartialShape.dynamic() ps2 = PartialShape([4]) assert ps1.compatible(ps2) ps1 = PartialShape([2, -1, 3, -1, 5]) ps2 = PartialShape([2, -1, -1, 4, 5]) assert ps1.compatible(ps2) ps1 = PartialShape([2, -1, 3, -1, 5]) ps2 = PartialShape([1, -1, -1, 4, 5]) assert not ps1.compatible(ps2)
def partial_shape_from_data(data: Union[DataPtr, CDataPtr]) -> PartialShape: """Get nGraph PartialShape from Inference Engine Data.""" capsule = data._get_partial_shape_capsule() return PartialShape.from_capsule(capsule)