Beispiel #1
0
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)
Beispiel #2
0
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)
Beispiel #3
0
def test_dot_tensor_scalar():
    np.random.seed(133391)
    left_input = 10.0
    right_input = -100.0 + np.random.rand(2, 3, 4) * 200.0
    expected = left_input * right_input

    result = run_op_node([left_input, right_input], ng.dot)
    assert np.allclose(result, expected)

    result = run_op_node([right_input, left_input], ng.dot)
    assert np.allclose(result, expected)
Beispiel #4
0
def test_shape_of():
    input_tensor = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                            dtype=np.float32)

    result = run_op_node([input_tensor], ng.shape_of)

    assert np.allclose(result, [3, 3])
Beispiel #5
0
def test_broadcast_1():
    input_data = np.array([1, 2, 3])

    new_shape = [3, 3]
    expected = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
    result = run_op_node([input_data], ng.broadcast, new_shape)
    assert np.allclose(result, expected)
Beispiel #6
0
def test_convert_to_uint(destination_type, expected_type):
    np.random.seed(133391)
    input_data = np.ceil(np.random.rand(2, 3, 4) * 16)
    expected = np.array(input_data, dtype=expected_type)
    result = run_op_node([input_data], ng.convert, destination_type)
    assert np.allclose(result, expected)
    assert np.array(result).dtype == expected_type
Beispiel #7
0
def test_strided_slice():
    input_tensor = np.arange(2 * 3 * 4, dtype=np.float32).reshape((2, 3, 4))
    begin = np.array([1, 0], dtype=np.int64)
    end = np.array([0, 0], dtype=np.int64)
    strides = np.array([1, 1], dtype=np.int64)
    begin_mask = np.array([0, 0, 0], dtype=np.int64)
    end_mask = np.array([0, 0, 0], dtype=np.int64)
    new_axis_mask = np.array([0, 1, 0], dtype=np.int64)
    shrink_axis_mask = np.array([1, 0, 0], dtype=np.int64)
    ellipsis_mask = np.array([0, 0, 0], dtype=np.int64)

    result = run_op_node(
        [input_tensor, begin, end, strides],
        ng.strided_slice,
        begin_mask,
        end_mask,
        new_axis_mask,
        shrink_axis_mask,
        ellipsis_mask,
    )

    expected = np.array([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
                        dtype=np.float32).reshape((1, 3, 4))

    assert np.allclose(result, expected)
Beispiel #8
0
def test_range():
    start = 5
    stop = 35
    step = 5

    result = run_op_node([start, stop, step], ng.ops.range)
    assert np.allclose(result, [5, 10, 15, 20, 25, 30])
Beispiel #9
0
def test_convert_to_float(destination_type, rand_range, in_dtype, expected_type):
    np.random.seed(133391)
    input_data = np.random.randint(*rand_range, size=(2, 2), dtype=in_dtype)
    expected = np.array(input_data, dtype=expected_type)
    result = run_op_node([input_data], ng.convert, destination_type)
    assert np.allclose(result, expected)
    assert np.array(result).dtype == expected_type
Beispiel #10
0
def test_logical_not(input_data):
    expected = np.logical_not(input_data)

    result = run_op_node([input_data], ng.logical_not)[0]

    assert np.array_equal(result, expected)
    result = run_op_numeric_data(input_data, ng.logical_not)[0]
    assert np.array_equal(result, expected)
def test_select():
    cond = [[False, False], [True, False], [True, True]]
    then_node = [[-1, 0], [1, 2], [3, 4]]
    else_node = [[11, 10], [9, 8], [7, 6]]
    excepted = [[11, 10], [1, 8], [3, 4]]

    result = run_op_node([cond, then_node, else_node], ng.select)
    assert np.allclose(result, excepted)
Beispiel #12
0
def test_dot(left_shape, right_shape, reduction_axes_count, numpy_axes):
    np.random.seed(133391)
    left_input = -100.0 + np.random.rand(*left_shape) * 200.0
    right_input = -100.0 + np.random.rand(*right_shape) * 200.0

    expected = np.tensordot(left_input, right_input, numpy_axes)
    result = run_op_node([left_input, right_input], ng.dot, reduction_axes_count)
    assert np.allclose(result, expected)
Beispiel #13
0
def test_broadcast_3():
    input_data = np.array([1, 2, 3])
    new_shape = [3, 3]
    axis_mapping = [0]
    expected = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

    result = run_op_node([input_data], ng.broadcast, new_shape, axis_mapping, "EXPLICIT")
    assert np.allclose(result, expected)
Beispiel #14
0
def test_reduction_ops(ng_api_helper, numpy_function, reduction_axes):
    shape = [2, 4, 3, 2]
    np.random.seed(133391)
    input_data = np.random.randn(*shape).astype(np.float32)

    expected = numpy_function(input_data, axis=reduction_axes)
    result = run_op_node([input_data], ng_api_helper, reduction_axes)
    assert np.allclose(result, expected)
Beispiel #15
0
def test_unary_op_scalar(ng_api_fn, numpy_fn, input_data):
    expected = numpy_fn(input_data)

    result = run_op_node([input_data], ng_api_fn)
    assert np.allclose(result, expected)

    result = run_op_numeric_data(input_data, ng_api_fn)
    assert np.allclose(result, expected)
Beispiel #16
0
def test_power_v1():
    A = np.arange(48, dtype=np.float32).reshape((8, 1, 6, 1))
    B = np.arange(35, dtype=np.float32).reshape((7, 1, 5))

    expected = np.power(A, B)
    result = run_op_node([A, B], ng.power)

    assert np.allclose(result, expected)
Beispiel #17
0
def test_reduction_ops(ng_api_helper, numpy_function, reduction_axes):
    shape = [2, 4, 3, 2]
    np.random.seed(133391)
    input_data = np.random.randn(*shape).astype(np.float32)

    expected = numpy_function(input_data, axis=reduction_axes)
    result = run_op_node([input_data], ng_api_helper, reduction_axes)
    assert np.allclose(result, expected)
Beispiel #18
0
def test_unary_op_scalar(ng_api_fn, numpy_fn, input_data):
    expected = numpy_fn(input_data)

    result = run_op_node([input_data], ng_api_fn)
    assert np.allclose(result, expected)

    result = run_op_numeric_data(input_data, ng_api_fn)
    assert np.allclose(result, expected)
Beispiel #19
0
def test_logical_not(input_data):
    expected = np.logical_not(input_data)

    result = run_op_node([input_data], ng.logical_not)

    assert np.allclose(result, expected)
    result = run_op_numeric_data(input_data, ng.logical_not)
    assert np.allclose(result, expected)
Beispiel #20
0
def test_multiply():
    A = np.arange(48).reshape((8, 1, 6, 1))
    B = np.arange(35).reshape((7, 1, 5))

    expected = np.multiply(A, B)
    result = run_op_node([A, B], ng.multiply)

    assert np.allclose(result, expected)
Beispiel #21
0
def test_convert_to_float(val_type, range_start, range_end, in_dtype):
    np.random.seed(133391)
    input_data = np.random.randint(range_start,
                                   range_end,
                                   size=(2, 2),
                                   dtype=in_dtype)
    expected = np.array(input_data, dtype=val_type)
    result = run_op_node([input_data], ng.convert, val_type)
    assert np.allclose(result, expected)
Beispiel #22
0
def test_tile():
    input_tensor = np.arange(6).reshape((2, 1, 3))
    repeats = np.array([2, 1])

    result = run_op_node([input_tensor, repeats], ng.tile)

    expected = np.array([0, 1, 2, 0, 1, 2, 3, 4, 5, 3, 4, 5]).reshape((2, 2, 3))

    assert np.allclose(result, expected)
Beispiel #23
0
def test_transpose():
    input_tensor = np.arange(3 * 3 * 224 * 224).reshape((3, 3, 224, 224))
    input_order = np.array([0, 2, 3, 1])

    result = run_op_node([input_tensor, input_order], ng.transpose)

    expected = np.transpose(input_tensor, input_order)

    assert np.allclose(result, expected)
Beispiel #24
0
def test_erf():
    input_tensor = np.array([-1.0, 0.0, 1.0, 2.5, 3.14, 4.0], dtype=np.float32)
    expected = [-0.842701, 0.0, 0.842701, 0.999593, 0.999991, 1.0]

    result = run_op_node([input_tensor], ng.erf)
    assert np.allclose(result, expected)

    result = run_op_numeric_data(input_tensor, ng.erf)
    assert np.allclose(result, expected)
Beispiel #25
0
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)
Beispiel #26
0
def test_one_hot():
    data = np.array([0, 1, 2], dtype=np.int32)
    depth = 2
    on_value = 5
    off_value = 10
    axis = -1
    excepted = [[5, 10], [10, 5], [10, 10]]

    result = run_op_node([data, depth, on_value, off_value], ng.one_hot, axis)
    assert np.allclose(result, excepted)
Beispiel #27
0
def test_softmax():
    axis = 0
    input_tensor = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)

    result = run_op_node([input_tensor], ng.ops.softmax, axis)

    expected = [[0.00426978, 0.01160646, 0.03154963],
                [0.08576079, 0.23312202, 0.6336913]]

    assert np.allclose(result, expected)
Beispiel #28
0
def test_unary_op_array(ng_api_fn, numpy_fn, range_start, range_end):
    np.random.seed(133391)
    input_data = range_start + np.random.rand(2, 3, 4) * (range_end - range_start)
    expected = numpy_fn(input_data)

    result = run_op_node([input_data], ng_api_fn)
    assert np.allclose(result, expected)

    result = run_op_numeric_data(input_data, ng_api_fn)
    assert np.allclose(result, expected)
Beispiel #29
0
def test_reshape_v1():
    A = np.arange(1200, dtype=np.float32).reshape((2, 5, 5, 24))
    shape = np.array([0, -1, 4])
    special_zero = True

    expected_shape = np.array([2, 150, 4])
    expected = np.reshape(A, expected_shape)
    result = run_op_node([A, shape], ng.reshape, special_zero)

    assert np.allclose(result, expected)
Beispiel #30
0
def test_unary_op_array(ng_api_fn, numpy_fn, range_start, range_end):
    np.random.seed(133391)
    input_data = range_start + np.random.rand(2, 3, 4) * (range_end - range_start)
    expected = numpy_fn(input_data)

    result = run_op_node([input_data], ng_api_fn)[0]
    np.testing.assert_allclose(result, expected, rtol=0.001)

    result = run_op_numeric_data(input_data, ng_api_fn)[0]
    np.testing.assert_allclose(result, expected, rtol=0.001)
Beispiel #31
0
def test_serialization():
    dtype = np.float32
    backend_name = test.BACKEND_NAME

    shape = [2, 2]
    parameter_a = ng.parameter(shape, dtype=dtype, name='A')
    parameter_b = ng.parameter(shape, dtype=dtype, name='B')
    parameter_c = ng.parameter(shape, dtype=dtype, name='C')
    model = (parameter_a + parameter_b) * parameter_c
    runtime = ng.runtime(backend_name=backend_name)
    computation = runtime.computation(model, parameter_a, parameter_b, parameter_c)
    try:
        serialized = computation.serialize(2)
        serial_json = json.loads(serialized)

        assert serial_json[0]['name'] != ''
        assert 10 == len(serial_json[0]['ops'])
    except Exception:
        pass

    input_data = np.array([1, 2, 3])

    new_shape = [3, 3]
    expected = [[1, 2, 3],
                [1, 2, 3],
                [1, 2, 3]]
    result = run_op_node([input_data], ng.broadcast_to, new_shape)
    assert np.allclose(result, expected)

    axis = 0
    expected = [[1, 1, 1],
                [2, 2, 2],
                [3, 3, 3]]

    result = run_op_node([input_data], ng.broadcast_to, new_shape, axis)
    assert np.allclose(result, expected)

    input_data = np.arange(4)
    new_shape = [3, 4, 2, 4]
    expected = np.broadcast_to(input_data, new_shape)
    result = run_op_node([input_data], ng.broadcast_to, new_shape)
    assert np.allclose(result, expected)
Beispiel #32
0
def test_broadcast_v1():
    input_tensor = np.array([1.0, 2.0, 3.0], np.float32)
    input_shape = np.array([3, 3], np.int64)
    input_axes = np.array([1], np.int64)
    result = run_op_node([input_tensor, input_shape, input_axes], ng.broadcast)

    a_arr = np.array([[0], [0], [0]], dtype=np.float32)
    b_arr = np.array([[1, 2, 3]], dtype=np.float32)
    expected = np.add(a_arr, b_arr)

    assert np.allclose(result, expected)
Beispiel #33
0
def test_sigmoid():
    input_data = np.array([-3.14, -1.0, 0.0, 2.71001, 1000.0],
                          dtype=np.float32)
    result = run_op_node([input_data], ng.sigmoid)

    def sigmoid(x):
        return 1.0 / (1.0 + np.exp(-x))

    expected = np.array(list(map(sigmoid, input_data)))

    assert np.allclose(result, expected)
Beispiel #34
0
def test_gather():
    input_data = np.array([1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2], np.float32).reshape((3, 3))
    input_indices = np.array([0, 2], np.int64).reshape(1, 2)
    input_axes = np.array([1], np.int64)

    expected = np.array([1.0, 1.2, 2.0, 2.2, 3.0, 3.2], dtype=np.float32).reshape((3, 1, 2))

    result = run_op_node([input_data, input_indices, input_axes], ng.gather)
    assert np.allclose(result, expected)

    result = run_op_numeric_data(input_data, ng.gather, input_indices, input_axes)
    assert np.allclose(result, expected)
Beispiel #35
0
def test_serialization():
    dtype = np.float32
    backend_name = pytest.config.getoption('backend', default='CPU')

    shape = [2, 2]
    parameter_a = ng.parameter(shape, dtype=dtype, name='A')
    parameter_b = ng.parameter(shape, dtype=dtype, name='B')
    parameter_c = ng.parameter(shape, dtype=dtype, name='C')
    model = (parameter_a + parameter_b) * parameter_c
    runtime = ng.runtime(backend_name=backend_name)
    computation = runtime.computation(model, parameter_a, parameter_b, parameter_c)
    serialized = computation.serialize(2)
    serial_json = json.loads(serialized)

    assert serial_json[0]['name'] != ''
    assert 10 == len(serial_json[0]['ops'])

    input_data = np.array([1, 2, 3])

    new_shape = [3, 3]
    expected = [[1, 2, 3],
                [1, 2, 3],
                [1, 2, 3]]
    result = run_op_node([input_data], ng.broadcast, new_shape)
    assert np.allclose(result, expected)

    axis = 0
    expected = [[1, 1, 1],
                [2, 2, 2],
                [3, 3, 3]]

    result = run_op_node([input_data], ng.broadcast, new_shape, axis)
    assert np.allclose(result, expected)

    input_data = np.arange(4)
    new_shape = [3, 4, 2, 4]
    expected = np.broadcast_to(input_data, new_shape)
    result = run_op_node([input_data], ng.broadcast, new_shape)
    assert np.allclose(result, expected)
Beispiel #36
0
def test_convert_to_bool(val_type, input_data):
    expected = np.array(input_data, dtype=val_type)
    result = run_op_node([input_data], ng.convert, val_type)
    assert np.allclose(result, expected)
Beispiel #37
0
def test_convert_to_float(val_type, range_start, range_end, in_dtype):
    np.random.seed(133391)
    input_data = np.random.randint(range_start, range_end, size=(2, 2), dtype=in_dtype)
    expected = np.array(input_data, dtype=val_type)
    result = run_op_node([input_data], ng.convert, val_type)
    assert np.allclose(result, expected)
Beispiel #38
0
def test_convert_to_uint(val_type):
    np.random.seed(133391)
    input_data = np.ceil(np.random.rand(2, 3, 4) * 16)
    expected = np.array(input_data, dtype=val_type)
    result = run_op_node([input_data], ng.convert, val_type)
    assert np.allclose(result, expected)