Example #1
0
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"
Example #2
0
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
Example #3
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)
Example #4
0
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()
Example #5
0
def test_tensor_info_ctor_shape():
    tensor_shape = ann.TensorShape((1, 1, 2))

    tensor_info = ann.TensorInfo(tensor_shape, ann.DataType_QAsymmU8, 0.5, 1)

    assert 2 == tensor_info.GetNumElements()
    assert 3 == tensor_info.GetNumDimensions()
    assert ann.DataType_QAsymmU8 == tensor_info.GetDataType()
    assert 0.5 == tensor_info.GetQuantizationScale()
    assert 1 == tensor_info.GetQuantizationOffset()

    shape = tensor_info.GetShape()

    assert 2 == shape.GetNumElements()
    assert 3 == shape.GetNumDimensions()
Example #6
0
    def test_output_slot(self, network):

        # Create input, addition & output layer
        input1 = network.AddInputLayer(0, "input1")
        input2 = network.AddInputLayer(1, "input2")
        add = network.AddAdditionLayer("addition")
        output = network.AddOutputLayer(0, "output")

        # Connect the input/output slots for each layer
        input1.GetOutputSlot(0).Connect(add.GetInputSlot(0))
        input2.GetOutputSlot(0).Connect(add.GetInputSlot(1))
        add.GetOutputSlot(0).Connect(output.GetInputSlot(0))

        # Check IInputSlot GetConnection()
        add_get_input_connection = add.GetInputSlot(0).GetConnection()
        output_get_input_connection = output.GetInputSlot(0).GetConnection()

        # Check IOutputSlot GetConnection()
        add_get_output_connect = add.GetOutputSlot(0).GetConnection(0)
        assert isinstance(add_get_output_connect.GetConnection(), ann.IOutputSlot)

        # Test IOutputSlot GetNumConnections() & CalculateIndexOnOwner()
        assert add_get_input_connection.GetNumConnections() == 1
        assert len(add_get_input_connection) == 1
        assert add_get_input_connection[0]
        assert add_get_input_connection.CalculateIndexOnOwner() == 0

        # Check GetOwningLayerGuid(). Check that it is different for add and output layer
        assert add_get_input_connection.GetOwningLayerGuid() != output_get_input_connection.GetOwningLayerGuid()

        # Set TensorInfo
        test_tensor_info = ann.TensorInfo(ann.TensorShape((2, 3)), ann.DataType_Float32)

        # Check IsTensorInfoSet()
        assert not add_get_input_connection.IsTensorInfoSet()
        add_get_input_connection.SetTensorInfo(test_tensor_info)
        assert add_get_input_connection.IsTensorInfoSet()

        # Check GetTensorInfo()
        output_tensor_info = add_get_input_connection.GetTensorInfo()
        assert 2 == output_tensor_info.GetNumDimensions()
        assert 6 == output_tensor_info.GetNumElements()

        # Check Disconnect()
        assert output_get_input_connection.GetNumConnections() == 1  # 1 connection to Outputslot0 from input1
        add.GetOutputSlot(0).Disconnect(output.GetInputSlot(0))  # disconnect add.OutputSlot0 from Output.InputSlot0
        assert output_get_input_connection.GetNumConnections() == 0
Example #7
0
def _get_tensor_info(dt):
    tensor_info = ann.TensorInfo(ann.TensorShape((2, 3)), dt)

    return tensor_info
Example #8
0
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()
Example #9
0
def test_tensor_info__str__():
    tensor_info = ann.TensorInfo(ann.TensorShape((2, 3)),
                                 ann.DataType_QAsymmU8, 0.5, 1)

    assert tensor_info.__str__() == "TensorInfo{DataType: 2, IsQuantized: 1, QuantizationScale: 0.500000, " \
                                    "QuantizationOffset: 1, NumDimensions: 2, NumElements: 6}"