def test_create_tensor_from_tensor(dt): tensor_info = __get_tensor_info(dt) tensor = ann.Tensor(tensor_info) copied_tensor = ann.Tensor(tensor) assert copied_tensor != tensor, "Different objects" assert copied_tensor.GetInfo() != tensor.GetInfo(), "Different objects" assert copied_tensor.get_memory_area( ).ctypes.data == tensor.get_memory_area().ctypes.data, "Same memory area" assert copied_tensor.GetNumElements() == tensor.GetNumElements() assert copied_tensor.GetNumBytes() == tensor.GetNumBytes() assert copied_tensor.GetDataType() == tensor.GetDataType()
def test_const_tensor_from_tensor_has_memory_area_access_after_deletion_of_original_tensor( ): tensor_info = ann.TensorInfo(ann.TensorShape((2, 3)), ann.DataType_Float32) tensor = ann.Tensor(tensor_info) tensor.get_memory_area()[0] = 100 copied_mem = tensor.get_memory_area().copy() assert 100 == copied_mem[0], "Memory was copied correctly" copied_tensor = ann.ConstTensor(tensor) tensor.get_memory_area()[0] = 200 assert 200 == tensor.get_memory_area( )[0], "Tensor and copied Tensor point to the same memory" assert 200 == copied_tensor.get_memory_area( )[0], "Tensor and copied Tensor point to the same memory" assert 100 == copied_mem[0], "Copied test memory not affected" copied_mem[0] = 200 # modify test memory to equal copied Tensor del tensor np.testing.assert_array_equal(copied_tensor.get_memory_area(), copied_mem), "After initial tensor was deleted, " \ "copied Tensor still has " \ "its memory as expected"
def test_tensor_memory_output(dt): tensor_info = __get_tensor_info(dt) tensor = ann.Tensor(tensor_info) # empty memory area because inference has not yet been run. assert tensor.get_memory_area().tolist() # has random stuff assert 4 == tensor.get_memory_area().itemsize, "it is float32"
def test_tensor_memory_output_fp16(dt): # Check Tensor with float16 tensor_info = __get_tensor_info(dt) tensor = ann.Tensor(tensor_info) assert tensor.GetNumElements() == 6 assert tensor.GetNumBytes() == 12 assert tensor.GetDataType() == ann.DataType_Float16
def test_copied_tensor_has_memory_area_access_after_deletion_of_original_tensor( dt): tensor = ann.Tensor(__get_tensor_info(dt)) tensor.get_memory_area()[0] = 100 initial_mem_copy = np.array(tensor.get_memory_area()) assert 100 == initial_mem_copy[0] copied_tensor = ann.Tensor(tensor) del tensor np.testing.assert_array_equal(copied_tensor.get_memory_area(), initial_mem_copy) assert 100 == copied_tensor.get_memory_area()[0]
def test_create_tensor_undefined_datatype(): tensor_info = ann.TensorInfo() tensor_info.SetDataType(99) with pytest.raises(ValueError) as err: ann.Tensor(tensor_info) assert 'The data type provided for this Tensor is not supported.' in str( err.value)
def test_tflite_parser_end_to_end(shared_data_folder): parser = ann.ITfLiteParser() network = parser.CreateNetworkFromBinaryFile( os.path.join(shared_data_folder, "mock_model.tflite")) graphs_count = parser.GetSubgraphCount() graph_id = graphs_count - 1 input_names = parser.GetSubgraphInputTensorNames(graph_id) input_binding_info = parser.GetNetworkInputBindingInfo( graph_id, input_names[0]) output_names = parser.GetSubgraphOutputTensorNames(graph_id) preferred_backends = [ann.BackendId('CpuAcc'), ann.BackendId('CpuRef')] options = ann.CreationOptions() runtime = ann.IRuntime(options) opt_network, messages = ann.Optimize(network, preferred_backends, runtime.GetDeviceSpec(), ann.OptimizerOptions()) assert 0 == len(messages) net_id, messages = runtime.LoadNetwork(opt_network) assert "" == messages # Load test image data stored in input_lite.npy input_tensor_data = np.load( os.path.join(shared_data_folder, 'tflite_parser/input_lite.npy')) input_tensors = ann.make_input_tensors([input_binding_info], [input_tensor_data]) output_tensors = [] for index, output_name in enumerate(output_names): out_bind_info = parser.GetNetworkOutputBindingInfo( graph_id, output_name) out_tensor_info = out_bind_info[1] out_tensor_id = out_bind_info[0] output_tensors.append((out_tensor_id, ann.Tensor(out_tensor_info))) runtime.EnqueueWorkload(net_id, input_tensors, output_tensors) output_vectors = [] for index, out_tensor in enumerate(output_tensors): output_vectors.append(out_tensor[1].get_memory_area()) # Load golden output file for result comparison. expected_outputs = np.load( os.path.join(shared_data_folder, 'tflite_parser/golden_output_lite.npy')) # Check that output matches golden output assert (expected_outputs == output_vectors[0]).all()
def test_tensor__str__(dt): tensor_info = __get_tensor_info(dt) elements = tensor_info.GetNumElements() num_bytes = tensor_info.GetNumBytes() d_type = dt dimensions = tensor_info.GetNumDimensions() tensor = ann.Tensor(tensor_info) assert str(tensor) == "Tensor{{DataType: {}, NumBytes: {}, NumDimensions: " \ "{}, NumElements: {}}}".format(d_type, num_bytes, dimensions, elements)
def test_create_const_tensor_from_tensor(): tensor_info = ann.TensorInfo(ann.TensorShape((2, 3)), ann.DataType_Float32) tensor = ann.Tensor(tensor_info) copied_tensor = ann.ConstTensor(tensor) assert copied_tensor != tensor, "Different objects" assert copied_tensor.GetInfo() != tensor.GetInfo(), "Different objects" assert copied_tensor.get_memory_area( ).ctypes.data == tensor.get_memory_area().ctypes.data, "Same memory area" assert copied_tensor.GetNumElements() == tensor.GetNumElements() assert copied_tensor.GetNumBytes() == tensor.GetNumBytes() assert copied_tensor.GetDataType() == tensor.GetDataType()
def test_create_tensor_with_info(dt): tensor_info = __get_tensor_info(dt) elements = tensor_info.GetNumElements() num_bytes = tensor_info.GetNumBytes() d_type = dt tensor = ann.Tensor(tensor_info) assert tensor_info != tensor.GetInfo(), "Different objects" assert elements == tensor.GetNumElements() assert num_bytes == tensor.GetNumBytes() assert d_type == tensor.GetDataType()
def test_deserializer_end_to_end(shared_data_folder): parser = ann.IDeserializer() network = parser.CreateNetworkFromBinary( os.path.join(shared_data_folder, "mock_model.armnn")) # use 0 as a dummy value for layer_id, which is unused in the actual implementation layer_id = 0 input_name = 'input_1' output_name = 'dense/Softmax' input_binding_info = parser.GetNetworkInputBindingInfo( layer_id, input_name) preferred_backends = [ann.BackendId('CpuAcc'), ann.BackendId('CpuRef')] options = ann.CreationOptions() runtime = ann.IRuntime(options) opt_network, messages = ann.Optimize(network, preferred_backends, runtime.GetDeviceSpec(), ann.OptimizerOptions()) assert 0 == len(messages) net_id, messages = runtime.LoadNetwork(opt_network) assert "" == messages # Load test image data stored in input_lite.npy input_tensor_data = np.load( os.path.join(shared_data_folder, 'deserializer/input_lite.npy')) input_tensors = ann.make_input_tensors([input_binding_info], [input_tensor_data]) output_tensors = [] out_bind_info = parser.GetNetworkOutputBindingInfo(layer_id, output_name) out_tensor_info = out_bind_info[1] out_tensor_id = out_bind_info[0] output_tensors.append((out_tensor_id, ann.Tensor(out_tensor_info))) runtime.EnqueueWorkload(net_id, input_tensors, output_tensors) output_vectors = [] for index, out_tensor in enumerate(output_tensors): output_vectors.append(out_tensor[1].get_memory_area()) # Load golden output file for result comparison. expected_outputs = np.load( os.path.join(shared_data_folder, 'deserializer/golden_output_lite.npy')) # Check that output matches golden output assert (expected_outputs == output_vectors[0]).all()
def random_runtime(shared_data_folder): parser = ann.ITfLiteParser() network = parser.CreateNetworkFromBinaryFile( os.path.join(shared_data_folder, 'mock_model.tflite')) preferred_backends = [ann.BackendId('CpuRef')] options = ann.CreationOptions() runtime = ann.IRuntime(options) graphs_count = parser.GetSubgraphCount() graph_id = graphs_count - 1 input_names = parser.GetSubgraphInputTensorNames(graph_id) input_binding_info = parser.GetNetworkInputBindingInfo( graph_id, input_names[0]) input_tensor_id = input_binding_info[0] input_tensor_info = input_binding_info[1] output_names = parser.GetSubgraphOutputTensorNames(graph_id) input_data = np.random.randint(255, size=input_tensor_info.GetNumElements(), dtype=np.uint8) const_tensor_pair = (input_tensor_id, ann.ConstTensor(input_tensor_info, input_data)) input_tensors = [const_tensor_pair] output_tensors = [] for index, output_name in enumerate(output_names): out_bind_info = parser.GetNetworkOutputBindingInfo( graph_id, output_name) out_tensor_info = out_bind_info[1] out_tensor_id = out_bind_info[0] output_tensors.append((out_tensor_id, ann.Tensor(out_tensor_info))) yield preferred_backends, network, runtime, input_tensors, output_tensors
def test_add_constant_layer_to_fully_connected(): inputWidth = 1 inputHeight = 1 inputChannels = 5 inputNum = 2 outputChannels = 3 outputNum = 2 inputShape = (inputNum, inputChannels, inputHeight, inputWidth) outputShape = (outputNum, outputChannels) weightsShape = (inputChannels, outputChannels) biasShape = (outputChannels, ) input = np.array([[1.0, 2.0, 3.0, 4.0, 5.0], [5.0, 4.0, 3.0, 2.0, 1.0]], dtype=np.float32) weights = np.array( [[.5, 2., .5], [.5, 2., 1.], [.5, 2., 2.], [.5, 2., 3.], [.5, 2., 4.]], dtype=np.float32) biasValues = np.array([10, 20, 30], dtype=np.float32) expectedOutput = np.array([[ 0.5 + 1.0 + 1.5 + 2.0 + 2.5 + biasValues[0], 2.0 + 4.0 + 6.0 + 8.0 + 10. + biasValues[1], 0.5 + 2.0 + 6.0 + 12. + 20. + biasValues[2] ], [ 2.5 + 2.0 + 1.5 + 1.0 + 0.5 + biasValues[0], 10.0 + 8.0 + 6.0 + 4.0 + 2. + biasValues[1], 2.5 + 4.0 + 6.0 + 6. + 4. + biasValues[2] ]], dtype=np.float32) network = ann.INetwork() input_info = ann.TensorInfo(ann.TensorShape(inputShape), ann.DataType_Float32, 0, 0, True) input_tensor = ann.ConstTensor(input_info, input) input_layer = network.AddInputLayer(0, "input") w_info = ann.TensorInfo(ann.TensorShape(weightsShape), ann.DataType_Float32, 0, 0, True) w_tensor = ann.ConstTensor(w_info, weights) w_layer = network.AddConstantLayer(w_tensor, "weights") b_info = ann.TensorInfo(ann.TensorShape(biasShape), ann.DataType_Float32, 0, 0, True) b_tensor = ann.ConstTensor(b_info, biasValues) b_layer = network.AddConstantLayer(b_tensor, "bias") fc_descriptor = ann.FullyConnectedDescriptor() fc_descriptor.m_BiasEnabled = True fc_descriptor.m_ConstantWeights = True fully_connected = network.AddFullyConnectedLayer(fc_descriptor, "fc") output_info = ann.TensorInfo(ann.TensorShape(outputShape), ann.DataType_Float32) output_tensor = ann.Tensor(output_info, np.zeros([1, 1], dtype=np.float32)) output = network.AddOutputLayer(0, "output") input_layer.GetOutputSlot(0).Connect(fully_connected.GetInputSlot(0)) w_layer.GetOutputSlot(0).Connect(fully_connected.GetInputSlot(1)) b_layer.GetOutputSlot(0).Connect(fully_connected.GetInputSlot(2)) fully_connected.GetOutputSlot(0).Connect(output.GetInputSlot(0)) input_layer.GetOutputSlot(0).SetTensorInfo(input_info) w_layer.GetOutputSlot(0).SetTensorInfo(w_info) b_layer.GetOutputSlot(0).SetTensorInfo(b_info) fully_connected.GetOutputSlot(0).SetTensorInfo(output_info) preferred_backends = [ann.BackendId('CpuRef')] options = ann.CreationOptions() runtime = ann.IRuntime(options) opt_network, messages = ann.Optimize(network, preferred_backends, runtime.GetDeviceSpec(), ann.OptimizerOptions()) net_id, messages = runtime.LoadNetwork(opt_network) input_tensors = [(0, input_tensor)] output_tensors = [(0, output_tensor)] runtime.EnqueueWorkload(net_id, input_tensors, output_tensors) output_vectors = ann.workload_tensors_to_ndarray(output_tensors) assert (output_vectors == expectedOutput).all()
def test_create_empty_tensor(): tensor = ann.Tensor() assert 0 == tensor.GetNumElements() assert 0 == tensor.GetNumBytes() assert tensor.get_memory_area() is None
def test_create_const_tensor_incorrect_args(): with pytest.raises(ValueError) as err: ann.Tensor('something', 'something') expected_error_message = "Incorrect number of arguments or type of arguments provided to create Tensor." assert expected_error_message in str(err.value)