def test_Convolution2d_layer_optional_provided(): net = ann.INetwork() layer = net.AddConvolution2dLayer(convolution2dDescriptor=ann.Convolution2dDescriptor(), weights=ann.ConstTensor(), biases=ann.ConstTensor()) assert layer
def test_fullyconnected_layer_optional_provided(): net = ann.INetwork() layer = net.AddFullyConnectedLayer(fullyConnectedDescriptor=ann.FullyConnectedDescriptor(), weights=ann.ConstTensor(), biases=ann.ConstTensor()) assert layer
def test_Convolution2d_layer_all_args(): net = ann.INetwork() layer = net.AddConvolution2dLayer(convolution2dDescriptor=ann.Convolution2dDescriptor(), weights=ann.ConstTensor(), biases=ann.ConstTensor(), name='NAME1') assert layer assert 'NAME1' == layer.GetName()
def test_fullyconnected_layer_all_args(): net = ann.INetwork() layer = net.AddFullyConnectedLayer(fullyConnectedDescriptor=ann.FullyConnectedDescriptor(), weights=ann.ConstTensor(), biases=ann.ConstTensor(), name='NAME1') assert layer assert 'NAME1' == layer.GetName()
def test_copy_const_tensor(self, dt, data): tensor_info = _get_tensor_info(dt) tensor = ann.ConstTensor(tensor_info, data) 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_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_DepthwiseConvolution2d_layer_optional_none(): net = ann.INetwork() layer = net.AddDepthwiseConvolution2dLayer( convolution2dDescriptor=ann.DepthwiseConvolution2dDescriptor(), weights=ann.ConstTensor()) assert layer
def test_const_tensor_incorrect_input_datatype(dt, data): tensor_info = _get_tensor_info(dt) with pytest.raises(TypeError) as err: ann.ConstTensor(tensor_info, data) assert 'Data must be provided as a numpy array.' in str(err.value)
def test_const_tensor_unsupported_datatype(dt, data): tensor_info = _get_tensor_info(dt) with pytest.raises(ValueError) as err: ann.ConstTensor(tensor_info, data) assert 'The data type provided for this Tensor is not supported: -1' in str( err.value)
def test_const_tensor_multi_dimensional_input(dt, data): tensor = ann.ConstTensor(ann.TensorInfo(ann.TensorShape((2, 2, 3, 3)), dt), data) assert data.size == tensor.GetNumElements() assert data.nbytes == tensor.GetNumBytes() assert dt == tensor.GetDataType() assert tensor.get_memory_area().data
def test_const_tensor_too_little_elements(dt, data): tensor_info = _get_tensor_info(dt) num_bytes = tensor_info.GetNumBytes() with pytest.raises(ValueError) as err: ann.ConstTensor(tensor_info, data) assert 'ConstTensor requires {} bytes, {} provided.'.format( num_bytes, data.nbytes) in str(err.value)
def test_immutable_memory(self, dt, data): tensor_info = _get_tensor_info(dt) tensor = ann.ConstTensor(tensor_info, data) with pytest.raises(ValueError) as err: tensor.get_memory_area()[0] = 0 assert 'is read-only' in str(err.value)
def test_const_tensor__str__(self, dt, data): tensor_info = _get_tensor_info(dt) d_type = tensor_info.GetDataType() num_dimensions = tensor_info.GetNumDimensions() num_bytes = tensor_info.GetNumBytes() num_elements = tensor_info.GetNumElements() tensor = ann.ConstTensor(tensor_info, data) assert str(tensor) == "ConstTensor{{DataType: {}, NumBytes: {}, NumDimensions: " \ "{}, NumElements: {}}}".format(d_type, num_bytes, num_dimensions, num_elements)
def test_const_tensor_with_info(self, dt, data): tensor_info = _get_tensor_info(dt) elements = tensor_info.GetNumElements() num_bytes = tensor_info.GetNumBytes() d_type = dt tensor = ann.ConstTensor(tensor_info, data) assert tensor_info != tensor.GetInfo(), "Different objects" assert elements == tensor.GetNumElements() assert num_bytes == tensor.GetNumBytes() assert d_type == tensor.GetDataType()
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_numpy_dtype_matches_ann_dtype(self, dt, data): np_data_type_mapping = { ann.DataType_QAsymmU8: np.uint8, ann.DataType_QAsymmS8: np.int8, ann.DataType_QSymmS8: np.int8, ann.DataType_Float32: np.float32, ann.DataType_QSymmS16: np.int16, ann.DataType_Signed32: np.int32, ann.DataType_Float16: np.float16 } tensor_info = _get_tensor_info(dt) tensor = ann.ConstTensor(tensor_info, data) assert np_data_type_mapping[tensor.GetDataType()] == data.dtype
def test_numpy_dtype_mismatch_ann_dtype(dt, data): np_data_type_mapping = { ann.DataType_QAsymmU8: np.uint8, ann.DataType_QAsymmS8: np.int8, ann.DataType_QSymmS8: np.int8, ann.DataType_Float32: np.float32, ann.DataType_QSymmS16: np.int16, ann.DataType_Signed32: np.int32, ann.DataType_Float16: np.float16 } tensor_info = _get_tensor_info(dt) with pytest.raises(TypeError) as err: ann.ConstTensor(tensor_info, data) assert str( err.value ) == "Expected data to have type {} for type {} but instead got numpy.{}".format( np_data_type_mapping[dt], dt, data.dtype)
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_const_tensor_incorrect_args(): with pytest.raises(ValueError) as err: ann.ConstTensor('something', 'something') expected_error_message = "Incorrect number of arguments or type of arguments provided to create Const Tensor." assert expected_error_message in str(err.value)