def test_reshape(): element_type = Type.f32 shape = Shape([2, 3]) A = Parameter(element_type, shape) parameter_list = [A] function = Function( NodeVector([Reshape(A, AxisVector([0, 1]), Shape([3, 2]))]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) a = backend.make_primary_tensor_view(element_type, shape) result = backend.make_primary_tensor_view(element_type, Shape([3, 2])) a.write( util.numpy_to_c(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)), 0, 24) result_arr = np.array([[0, 0], [0, 0], [0, 0]], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 24) cf.call([result], [a]) result.read(util.numpy_to_c(result_arr), 0, 24) a_arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) result_arr_ref = np.reshape(a_arr, (3, 2)) assert np.allclose(result_arr, result_arr_ref)
def test_broadcast(): element_type = Type.f32 A = Parameter(element_type, Shape([3])) parameter_list = [A] function = Function( NodeVector([Broadcast(A, Shape([3, 3]), AxisSet({0}))]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) a = backend.make_primary_tensor_view(element_type, Shape([3])) result = backend.make_primary_tensor_view(element_type, Shape([3, 3])) a.write(util.numpy_to_c(np.array([1, 2, 3], dtype=np.float32)), 0, 12) result_arr = np.zeros((3, 3), dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 36) cf.call([result], [a]) result.read(util.numpy_to_c(result_arr), 0, 36) a_arr = np.array([[0], [0], [0]], dtype=np.float32) b_arr = np.array([[1, 2, 3]], dtype=np.float32) result_arr_ref = np.add(a_arr, b_arr) assert np.allclose(result_arr, result_arr_ref)
def binary_op_exec(op_str): element_type = Type.f32 shape = Shape([2, 2]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) parameter_list = [A, B] function = Function(NodeVector([binary_op(op_str, A, B)]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) a = backend.make_primary_tensor_view(element_type, shape) b = backend.make_primary_tensor_view(element_type, shape) result = backend.make_primary_tensor_view(element_type, shape) a.write(util.numpy_to_c(np.array([[1, 6], [7, 4]], dtype=np.float32)), 0, 16) b.write(util.numpy_to_c(np.array([[5, 2], [3, 8]], dtype=np.float32)), 0, 16) result_arr = np.array([[0, 0], [0, 0]], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 16) cf.call([result], [a, b]) result.read(util.numpy_to_c(result_arr), 0, 16) a_arr = np.array([[1, 6], [7, 4]], dtype=np.float32) b_arr = np.array([[5, 2], [3, 8]], dtype=np.float32) result_arr_ref = binary_op_ref(op_str, a_arr, b_arr) assert np.allclose(result_arr, result_arr_ref)
def unary_op_exec(op_str, input_list): """ input_list needs to have deep length of 4 """ element_type = Type.f32 shape = Shape(np.array(input_list).shape) shape_np = np.array(input_list).shape A = Parameter(element_type, shape) parameter_list = [A] function = Function(NodeVector([unary_op(op_str, A)]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) a = backend.make_primary_tensor_view(element_type, shape) result = backend.make_primary_tensor_view(element_type, shape) a.write(util.numpy_to_c(np.array(input_list, dtype=np.float32)), 0, 16) result_arr = np.zeros(shape_np, dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 16) cf.call([result], [a]) result.read(util.numpy_to_c(result_arr), 0, 16) a_arr = np.array(input_list, dtype=np.float32) result_arr_ref = unary_op_ref(op_str, a_arr) assert np.allclose(result_arr, result_arr_ref)
def test_concat(): element_type = Type.f32 A = Parameter(element_type, Shape([1, 2])) B = Parameter(element_type, Shape([1, 2])) C = Parameter(element_type, Shape([1, 2])) parameter_list = [A, B, C] axis = 0 function = Function(NodeVector([Concat(NodeVector([A, B, C]), axis)]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) a = backend.make_primary_tensor_view(element_type, Shape([1, 2])) b = backend.make_primary_tensor_view(element_type, Shape([1, 2])) c = backend.make_primary_tensor_view(element_type, Shape([1, 2])) result = backend.make_primary_tensor_view(element_type, Shape([3, 2])) a.write(util.numpy_to_c(np.array([1, 2], dtype=np.float32)), 0, 8) b.write(util.numpy_to_c(np.array([5, 6], dtype=np.float32)), 0, 8) c.write(util.numpy_to_c(np.array([7, 8], dtype=np.float32)), 0, 8) result_arr = np.zeros(6, dtype=np.float32).reshape(3, 2) result.write(util.numpy_to_c(result_arr), 0, 24) cf.call([a, b, c], [result]) result.read(util.numpy_to_c(result_arr), 0, 24) a_arr = np.array([[1, 2]], dtype=np.float32) b_arr = np.array([[5, 6]], dtype=np.float32) c_arr = np.array([[7, 8]], dtype=np.float32) result_arr_ref = np.concatenate((a_arr, b_arr, c_arr), axis) assert np.allclose(result_arr, result_arr_ref)
def test_add_with_mul(): element_type = Type.f32 shape = Shape([2, 2]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) C = Parameter(element_type, shape) parameter_list = [A, B, C] function = Function(NodeVector([Multiply(Add(A, B), C)]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) a = backend.make_primary_tensor_view(element_type, shape) b = backend.make_primary_tensor_view(element_type, shape) c = backend.make_primary_tensor_view(element_type, shape) result = backend.make_primary_tensor_view(element_type, shape) a.write(util.numpy_to_c(np.array([1, 2, 3, 4], dtype=np.float32)), 0, 16) b.write(util.numpy_to_c(np.array([5, 6, 7, 8], dtype=np.float32)), 0, 16) c.write(util.numpy_to_c(np.array([9, 10, 11, 12], dtype=np.float32)), 0, 16) result_arr = np.array([0, 0, 0, 0], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 16) cf.call([a, b, c], [result]) result.read(util.numpy_to_c(result_arr), 0, 16) a_arr = np.array([1, 2, 3, 4], dtype=np.float32) b_arr = np.array([5, 6, 7, 8], dtype=np.float32) c_arr = np.array([9, 10, 11, 12], dtype=np.float32) result_arr_ref = (a_arr + b_arr) * c_arr assert np.allclose(result_arr, result_arr_ref)
def binary_op_comparison(op_str): element_type = Type.f32 shape = Shape([2, 2]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) parameter_list = [A, B] function = Function(NodeVector([binary_op(op_str, A, B)]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) a = backend.make_primary_tensor_view(element_type, shape) b = backend.make_primary_tensor_view(element_type, shape) result = backend.make_primary_tensor_view(Type.boolean, shape) a.write(util.numpy_to_c(np.array([[1, 5], [3, 2]], dtype=np.float32)), 0, 16) b.write(util.numpy_to_c(np.array([[2, 4], [3, 1]], dtype=np.float32)), 0, 16) result_arr = np.array([[False, False], [False, False]], dtype=np.bool) result.write(util.numpy_to_c(result_arr), 0, 4) cf.call([a, b], [result]) result.read(util.numpy_to_c(result_arr), 0, 4) a_arr = np.array([[1, 5], [3, 2]], dtype=np.float32) b_arr = np.array([[2, 4], [3, 1]], dtype=np.float32) result_arr_ref = binary_op_ref(op_str, a_arr, b_arr) assert np.allclose(result_arr, result_arr_ref)
def test_select(): element_type = Type.f32 A = Parameter(Type.boolean, Shape([1, 2])) B = Parameter(element_type, Shape([1, 2])) C = Parameter(element_type, Shape([1, 2])) parameter_list = [A, B, C] function = Function(NodeVector([Select(A, B, C)]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) a = backend.make_primary_tensor_view(Type.boolean, Shape([1, 2])) b = backend.make_primary_tensor_view(element_type, Shape([1, 2])) c = backend.make_primary_tensor_view(element_type, Shape([1, 2])) result = backend.make_primary_tensor_view(element_type, Shape([1, 2])) a.write(util.numpy_to_c(np.array([[True, False]], dtype=np.bool)), 0, 2) b.write(util.numpy_to_c(np.array([[5, 6]], dtype=np.float32)), 0, 8) c.write(util.numpy_to_c(np.array([[7, 8]], dtype=np.float32)), 0, 8) result_arr = np.array([[0, 0]], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 8) cf.call([result], [a, b, c]) result.read(util.numpy_to_c(result_arr), 0, 8) result_arr_ref = np.array([[5, 8]]) assert np.allclose(result_arr, result_arr_ref)
def test_convolution_with_filter_dilation(): element_type = Type.f32 image_shape = Shape([1, 1, 10, 10]) filter_shape = Shape([1, 1, 3, 3]) A = Parameter(element_type, image_shape) B = Parameter(element_type, filter_shape) parameter_list = [A, B] image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) strides = [1, 1] dilation = [2, 2] function = Function( NodeVector([Convolution(A, B, Strides(strides), Strides(dilation))]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, image_shape) b = backend.create_tensor(element_type, filter_shape) a.write(util.numpy_to_c(image_arr), 0, 10 * 10 * 4) b.write(util.numpy_to_c(filter_arr), 0, 3 * 3 * 4) result_arr = np.zeros(36, dtype=np.float32).reshape(1, 1, 6, 6) result = backend.create_tensor(element_type, Shape([1, 1, 6, 6])) result.write(util.numpy_to_c(result_arr), 0, 6 * 6 * 4) handle = backend.compile(function) handle.call([result], [a, b]) result.read(util.numpy_to_c(result_arr), 0, 6 * 6 * 4) result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilation).reshape(1, 1, 6, 6) assert np.allclose(result_arr, result_arr_ref)
def reduce( node, # type: Node initial_value, # type: ScalarData reduction_function, # type: Union[Callable, Function] reduction_axes=None, # type: List[int] name=None, # type: str ): # type: (...) -> Node """Perform general tensor reduction operation. :param node: The node providing data for reduction operation. :param initial_value: The initial value for reduction operation. :param reduction_function: The function performing binary reduction operation or a nGraph Function object. The operation must accept two nodes providing scalar operands and return a node which produces a scalar result. :param reduction_axes: The list of axes indices to be reduced. Default to reduce all axes. :param name: The new name for output node. :return: The node performing reduction operation with provided reduction node. """ if reduction_axes is None: reduction_axes = list(range(len(node.shape))) init_val_node = constant(initial_value) if not isinstance(reduction_function, Function): # wrap reduction function into Function object param1 = Parameter(node.get_element_type(), Shape([])) param2 = Parameter(node.get_element_type(), Shape([])) reduction_operation = Function( NodeVector([reduction_function(param1, param2)]), [param1, param2], 'reduction_operation') else: reduction_operation = reduction_function return Reduce(node, init_val_node, reduction_operation, AxisSet(set(reduction_axes)))
def test_add_with_mul(): element_type = Type.f32 shape = Shape([2, 2]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) C = Parameter(element_type, shape) parameter_list = [A, B, C] function = Function(NodeVector([Multiply(Add(A, B), C)]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, shape) b = backend.create_tensor(element_type, shape) c = backend.create_tensor(element_type, shape) result = backend.create_tensor(element_type, shape) a.write(util.numpy_to_c(np.array([1, 2, 3, 4], dtype=np.float32)), 0, 16) b.write(util.numpy_to_c(np.array([5, 6, 7, 8], dtype=np.float32)), 0, 16) c.write(util.numpy_to_c(np.array([9, 10, 11, 12], dtype=np.float32)), 0, 16) result_arr = np.array([0, 0, 0, 0], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 16) handle = backend.compile(function) handle.call([result], [a, b, c]) result.read(util.numpy_to_c(result_arr), 0, 16) a_arr = np.array([1, 2, 3, 4], dtype=np.float32) b_arr = np.array([5, 6, 7, 8], dtype=np.float32) c_arr = np.array([9, 10, 11, 12], dtype=np.float32) result_arr_ref = (a_arr + b_arr) * c_arr assert np.allclose(result_arr, result_arr_ref)
def binary_op_exec(op_str): element_type = Type.f32 shape = Shape([2, 2]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) parameter_list = [A, B] function = Function([binary_op(op_str, A, B)], parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, shape) b = backend.create_tensor(element_type, shape) result = backend.create_tensor(element_type, shape) a.write(util.numpy_to_c(np.array([[1, 6], [7, 4]], dtype=np.float32)), 0, 16) b.write(util.numpy_to_c(np.array([[5, 2], [3, 8]], dtype=np.float32)), 0, 16) result_arr = np.array([[0, 0], [0, 0]], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 16) handle = backend.compile(function) handle.call([result], [a, b]) result.read(util.numpy_to_c(result_arr), 0, 16) a_arr = np.array([[1, 6], [7, 4]], dtype=np.float32) b_arr = np.array([[5, 2], [3, 8]], dtype=np.float32) result_arr_ref = binary_op_ref(op_str, a_arr, b_arr) assert np.allclose(result_arr, result_arr_ref)
def binary_op_comparison(op_str): element_type = Type.f32 shape = Shape([2, 2]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) parameter_list = [A, B] function = Function(NodeVector([binary_op(op_str, A, B)]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, shape) b = backend.create_tensor(element_type, shape) result = backend.create_tensor(Type.boolean, shape) a.write(util.numpy_to_c(np.array([[1, 5], [3, 2]], dtype=np.float32)), 0, 16) b.write(util.numpy_to_c(np.array([[2, 4], [3, 1]], dtype=np.float32)), 0, 16) result_arr = np.array([[False, False], [False, False]], dtype=np.bool) result.write(util.numpy_to_c(result_arr), 0, 4) handle = backend.compile(function) handle.call([result], [a, b]) result.read(util.numpy_to_c(result_arr), 0, 4) a_arr = np.array([[1, 5], [3, 2]], dtype=np.float32) b_arr = np.array([[2, 4], [3, 1]], dtype=np.float32) result_arr_ref = binary_op_ref(op_str, a_arr, b_arr) assert np.allclose(result_arr, result_arr_ref)
def test_convolution(): element_type = Type.f32 image_shape = Shape([1, 1, 16, 16]) filter_shape = Shape([1, 1, 3, 3]) A = Parameter(element_type, image_shape) B = Parameter(element_type, filter_shape) parameter_list = [A, B] image_arr = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16) filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) filter_arr[0][0][0][0] = -1 filter_arr[0][0][1][1] = -1 filter_arr[0][0][2][2] = -1 filter_arr[0][0][0][2] = -1 filter_arr[0][0][2][0] = -1 result_arr = np.zeros(196, dtype=np.float32).reshape(1, 1, 14, 14) function = Function(NodeVector([Convolution(A, B)]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, image_shape) b = backend.create_tensor(element_type, filter_shape) a.write(util.numpy_to_c(image_arr), 0, 16*16*4) b.write(util.numpy_to_c(filter_arr), 0, 3*3*4) result = backend.create_tensor(element_type, Shape([1, 1, 14, 14])) result.write(util.numpy_to_c(result_arr), 0, 14*14*4) handle = backend.compile(function) handle.call([result], [a, b]) result.read(util.numpy_to_c(result_arr), 0, 14*14*4) result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0]).reshape(1, 1, 14, 14) assert np.allclose(result_arr, result_arr_ref)
def binary_op_exec(op_str): element_type = Type.f32 shape = Shape([2, 2]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) parameter_list = [A, B] function = Function(NodeVector([binary_op(op_str, A, B)]), parameter_list, 'test') backend = Backend.create(pytest.config.getoption('backend')) a = backend.create_tensor(element_type, shape) b = backend.create_tensor(element_type, shape) result = backend.create_tensor(element_type, shape) a.write(util.numpy_to_c(np.array([[1, 6], [7, 4]], dtype=np.float32)), 0, 16) b.write(util.numpy_to_c(np.array([[5, 2], [3, 8]], dtype=np.float32)), 0, 16) result_arr = np.array([[0, 0], [0, 0]], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 16) backend.call(function, [result], [a, b]) result.read(util.numpy_to_c(result_arr), 0, 16) a_arr = np.array([[1, 6], [7, 4]], dtype=np.float32) b_arr = np.array([[5, 2], [3, 8]], dtype=np.float32) result_arr_ref = binary_op_ref(op_str, a_arr, b_arr) assert np.allclose(result_arr, result_arr_ref)
def test_convolution_with_strides(): element_type = Type.f32 image_shape = Shape([1, 1, 10, 10]) filter_shape = Shape([1, 1, 3, 3]) A = Parameter(element_type, image_shape) B = Parameter(element_type, filter_shape) parameter_list = [A, B] image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = np.zeros(9, dtype=np.float32).reshape(1, 1, 3, 3) filter_arr[0][0][1][1] = 1 strides = [2, 2] function = Function(NodeVector([Convolution(A, B, Strides(strides))]), parameter_list, 'test') backend = Backend.create(pytest.config.getoption('backend')) a = backend.create_tensor(element_type, image_shape) b = backend.create_tensor(element_type, filter_shape) a.write(util.numpy_to_c(image_arr), 0, 10 * 10 * 4) b.write(util.numpy_to_c(filter_arr), 0, 3 * 3 * 4) result_arr = np.zeros(16, dtype=np.float32).reshape(1, 1, 4, 4) result = backend.create_tensor(element_type, Shape([1, 1, 4, 4])) result.write(util.numpy_to_c(result_arr), 0, 4 * 4 * 4) backend.call(function, [result], [a, b]) result.read(util.numpy_to_c(result_arr), 0, 4 * 4 * 4) result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides).reshape(1, 1, 4, 4) assert np.allclose(result_arr, result_arr_ref)
def unary_op_exec(op_str, input_list): """ input_list needs to have deep length of 4 """ element_type = Type.f32 shape = Shape(np.array(input_list).shape) shape_np = np.array(input_list).shape A = Parameter(element_type, shape) parameter_list = [A] function = Function(NodeVector([unary_op(op_str, A)]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, shape) result = backend.create_tensor(element_type, shape) a.write(util.numpy_to_c(np.array(input_list, dtype=np.float32)), 0, 16) result_arr = np.zeros(shape_np, dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 16) handle = backend.compile(function) handle.call([result], [a]) result.read(util.numpy_to_c(result_arr), 0, 16) a_arr = np.array(input_list, dtype=np.float32) result_arr_ref = unary_op_ref(op_str, a_arr) assert np.allclose(result_arr, result_arr_ref)
def test_sum(): element_type = Type.f32 shape = Shape([1, 4]) A = Parameter(element_type, shape) parameter_list = [A] function = Function(NodeVector([Sum(A, AxisSet({1}))]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, shape) result = backend.create_tensor(element_type, Shape([1])) a.write(util.numpy_to_c(np.array([1, 2, 3, 4], dtype=np.float32)), 0, 16) result_arr = np.array([0], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 4) handle = backend.compile(function) handle.get_performance_data() handle.call([result], [a]) result.read(util.numpy_to_c(result_arr), 0, 4) a_arr = np.array([1, 2, 3, 4], dtype=np.float32) result_arr_ref = np.sum(a_arr) assert np.allclose(result_arr[0], result_arr_ref)
def test_reshape(): element_type = Type.f32 shape = Shape([2, 3]) A = Parameter(element_type, shape) parameter_list = [A] function = Function( NodeVector([Reshape(A, AxisVector([0, 1]), Shape([3, 2]))]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, shape) result = backend.create_tensor(element_type, Shape([3, 2])) a.write( util.numpy_to_c(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)), 0, 24) result_arr = np.array([[0, 0], [0, 0], [0, 0]], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 24) handle = backend.compile(function) handle.call([result], [a]) result.read(util.numpy_to_c(result_arr), 0, 24) a_arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) result_arr_ref = np.reshape(a_arr, (3, 2)) assert np.allclose(result_arr, result_arr_ref)
def test_select(): element_type = Type.f32 A = Parameter(Type.boolean, Shape([1, 2])) B = Parameter(element_type, Shape([1, 2])) C = Parameter(element_type, Shape([1, 2])) parameter_list = [A, B, C] function = Function(NodeVector([Select(A, B, C)]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(Type.boolean, Shape([1, 2])) b = backend.create_tensor(element_type, Shape([1, 2])) c = backend.create_tensor(element_type, Shape([1, 2])) result = backend.create_tensor(element_type, Shape([1, 2])) a.write(util.numpy_to_c(np.array([[True, False]], dtype=np.bool)), 0, 2) b.write(util.numpy_to_c(np.array([[5, 6]], dtype=np.float32)), 0, 8) c.write(util.numpy_to_c(np.array([[7, 8]], dtype=np.float32)), 0, 8) result_arr = np.array([[0, 0]], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 8) handle = backend.compile(function) handle.call([result], [a, b, c]) result.read(util.numpy_to_c(result_arr), 0, 8) result_arr_ref = np.array([[5, 8]]) assert np.allclose(result_arr, result_arr_ref)
def test_convolution_simple(): element_type = Type.f32 image_shape = Shape([1, 1, 16, 16]) filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) parameter_list = [data, filters] image_arr = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16) filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) filter_arr[0][0][0][0] = -1 filter_arr[0][0][1][1] = -1 filter_arr[0][0][2][2] = -1 filter_arr[0][0][0][2] = -1 filter_arr[0][0][2][0] = -1 strides = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] dilations = [1, 1] model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations) function = Function([model], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(image_arr, filter_arr)[0] expected = convolution2d(image_arr[0][0], filter_arr[0][0]).reshape(1, 1, 14, 14) assert np.allclose(result, expected)
def test_concat(): element_type = Type.f32 A = Parameter(element_type, Shape([1, 2])) B = Parameter(element_type, Shape([1, 2])) C = Parameter(element_type, Shape([1, 2])) parameter_list = [A, B, C] axis = 0 function = Function(NodeVector([Concat(NodeVector([A, B, C]), axis)]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, Shape([1, 2])) b = backend.create_tensor(element_type, Shape([1, 2])) c = backend.create_tensor(element_type, Shape([1, 2])) result = backend.create_tensor(element_type, Shape([3, 2])) a.write(util.numpy_to_c(np.array([1, 2], dtype=np.float32)), 0, 8) b.write(util.numpy_to_c(np.array([5, 6], dtype=np.float32)), 0, 8) c.write(util.numpy_to_c(np.array([7, 8], dtype=np.float32)), 0, 8) result_arr = np.zeros(6, dtype=np.float32).reshape(3, 2) result.write(util.numpy_to_c(result_arr), 0, 24) handle = backend.compile(function) handle.call([result], [a, b, c]) result.read(util.numpy_to_c(result_arr), 0, 24) a_arr = np.array([[1, 2]], dtype=np.float32) b_arr = np.array([[5, 6]], dtype=np.float32) c_arr = np.array([[7, 8]], dtype=np.float32) result_arr_ref = np.concatenate((a_arr, b_arr, c_arr), axis) assert np.allclose(result_arr, result_arr_ref)
def test_broadcast(): element_type = Type.f32 A = Parameter(element_type, Shape([3])) parameter_list = [A] function = Function( NodeVector([Broadcast(A, Shape([3, 3]), AxisSet({0}))]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, Shape([3])) result = backend.create_tensor(element_type, Shape([3, 3])) a.write(util.numpy_to_c(np.array([1, 2, 3], dtype=np.float32)), 0, 12) result_arr = np.zeros((3, 3), dtype=np.float32) result.write(util.numpy_to_c(result_arr), 0, 36) handle = backend.compile(function) handle.call([result], [a]) result.read(util.numpy_to_c(result_arr), 0, 36) a_arr = np.array([[0], [0], [0]], dtype=np.float32) b_arr = np.array([[1, 2, 3]], dtype=np.float32) result_arr_ref = np.add(a_arr, b_arr) assert np.allclose(result_arr, result_arr_ref)
def test_convolution_with_non_zero_padding(): element_type = Type.f32 image_shape = Shape([1, 1, 10, 10]) filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) parameter_list = [data, filters] image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = (np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)) * -1 filter_arr[0][0][1][1] = 1 strides = [1, 1] dilations = [2, 2] pads_begin = [2, 1] pads_end = [1, 2] model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations) function = Function([model], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(image_arr, filter_arr)[0] expected = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilations, pads_begin, pads_end).reshape([1, 1, 9, 9]) assert np.allclose(result, expected)
def test_slice(): element_type = Type.f32 shape = Shape([6, 6]) A = Parameter(element_type, shape) parameter_list = [A] input_arr = np.arange(36, dtype=np.float32).reshape(6, 6) lower_bounds = [1, 1] upper_bounds = [5, 5] function = Function(NodeVector([Slice(A, Coordinate(lower_bounds), Coordinate(upper_bounds))]), parameter_list, 'test') backend = Backend.create(pytest.config.getoption('backend')) a = backend.create_tensor(element_type, shape) result = backend.create_tensor(element_type, Shape([4, 4])) a.write(util.numpy_to_c(input_arr), 0, 36*4) result_arr = np.zeros(16, dtype=np.float32).reshape(4, 4) result.write(util.numpy_to_c(result_arr), 0, 16*4) backend.call(backend.compile(function), [result], [a]) result.read(util.numpy_to_c(result_arr), 0, 64) result_arr_ref = input_arr[lower_bounds[0]:upper_bounds[0], lower_bounds[1]:upper_bounds[1]] assert np.allclose(result_arr, result_arr_ref) #test with strides strides = [1, 2] function = Function(NodeVector([Slice(A, Coordinate(lower_bounds), Coordinate(upper_bounds), Strides(strides))]), parameter_list, 'test') backend = Backend.create(pytest.config.getoption('backend')) result = backend.create_tensor(element_type, Shape([4, 2])) result_arr = np.zeros(8, dtype=np.float32).reshape(4, 2) result.write(util.numpy_to_c(result_arr), 0, 8*4) backend.call(backend.compile(function), [result], [a]) result.read(util.numpy_to_c(result_arr), 0, 32) result_arr_ref = result_arr_ref[::strides[0], ::strides[1]] assert np.allclose(result_arr, result_arr_ref)
def test_convolutionBackpropData(): element_type = Type.f32 image_shape = Shape([1, 1, 10, 10]) filter_shape = Shape([1, 1, 3, 3]) output_shape = Shape([1, 1, 17, 17]) image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) window_strides = [1, 1] window_dilation = [1, 1] padding_below = [0, 0] padding_above = [0, 0] data_dilation = [2, 2] output_arr = convolution2d(image_arr[0][0], filter_arr[0][0], window_strides, window_dilation, padding_below, padding_above, data_dilation).reshape(1, 1, 17, 17) A = Parameter(element_type, filter_shape) B = Parameter(element_type, output_shape) parameter_list = [A, B] function = Function( NodeVector([ ConvolutionBackpropData(image_shape, A, B, Strides(window_strides), Strides(window_dilation), CoordinateDiff(padding_below), CoordinateDiff(padding_above), Strides(data_dilation)) ]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) a = backend.make_primary_tensor_view(element_type, filter_shape) b = backend.make_primary_tensor_view(element_type, output_shape) a.write(util.numpy_to_c(filter_arr), 0, 3 * 3 * 4) b.write(util.numpy_to_c(output_arr), 0, 17 * 17 * 4) result_arr = np.zeros(10 * 10, dtype=np.float32).reshape(1, 1, 10, 10) result = backend.make_primary_tensor_view(element_type, Shape([1, 1, 10, 10])) result.write(util.numpy_to_c(result_arr), 0, 10 * 10 * 4) cf.call([result], [a, b]) result.read(util.numpy_to_c(result_arr), 0, 10 * 10 * 4) result_arr_ref = np.array( [[[[22, 60, 70, 80, 90, 100, 110, 120, 130, 54.], [105, 275, 300, 325, 350, 375, 400, 425, 450, 185.], [205, 525, 550, 575, 600, 625, 650, 675, 700, 285.], [305, 775, 800, 825, 850, 875, 900, 925, 950, 385.], [405, 1025, 1050, 1075, 1100, 1125, 1150, 1175, 1200, 485.], [505, 1275, 1300, 1325, 1350, 1375, 1400, 1425, 1450, 585.], [605, 1525, 1550, 1575, 1600, 1625, 1650, 1675, 1700, 685.], [705, 1775, 1800, 1825, 1850, 1875, 1900, 1925, 1950, 785.], [805, 2025, 2050, 2075, 2100, 2125, 2150, 2175, 2200, 885.], [342, 860, 870, 880, 890, 900, 910, 920, 930, 374.]]]]) assert np.allclose(result_arr, result_arr_ref)
def test_get_IENetwork_from_nGraph(): func = create_relu([1, 3, 22, 22]) caps = Function.to_capsule(func) cnnNetwork = IENetwork(caps) assert cnnNetwork != None assert ng.function_from_cnn(cnnNetwork) != None func2 = ng.function_from_cnn(cnnNetwork) assert func2 != None
def test_convert(): element_type = Type.f32 shape = Shape([1, 3]) A = Parameter(element_type, shape) parameter_list = [A] # f32 to boolean function = Function(NodeVector([Convert(A, Type.boolean)]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) a = backend.create_tensor(element_type, shape) result = backend.create_tensor(Type.boolean, shape) a.write(util.numpy_to_c(np.array([1, 5, 3], dtype=np.float32)), 0, 12) result_arr = np.array([False, False, False], dtype=np.bool) result.write(util.numpy_to_c(result_arr), 0, 3) handle = backend.compile(function) handle.call([result], [a]) result.read(util.numpy_to_c(result_arr), 0, 3) a_arr = np.array([1, 5, 3], dtype=np.float32) result_arr_ref = a_arr.astype(bool) assert np.allclose(result_arr, result_arr_ref) # f32 to i32 function = Function(NodeVector([Convert(A, Type.i32)]), parameter_list, 'test') backend = Backend.create(test.BACKEND_NAME) result = backend.create_tensor(Type.i32, shape) a.write(util.numpy_to_c(np.array([1.4, 5.5, 3.9], dtype=np.float32)), 0, 12) result_arr = np.array([0, 0, 0], dtype=np.int32) result.write(util.numpy_to_c(result_arr), 0, 12) handle = backend.compile(function) handle.call([result], [a]) result.read(util.numpy_to_c(result_arr), 0, 12) a_arr = np.array([1.4, 5.4, 3.9], dtype=np.float32) result_arr_ref = a_arr.astype(int) assert np.allclose(result_arr, result_arr_ref)
def test_create_IENetwork_from_nGraph(): func = create_ngraph_function([1, 3, 22, 22]) caps = Function.to_capsule(func) cnnNetwork = IENetwork(caps) assert cnnNetwork != None func2 = ng.function_from_cnn(cnnNetwork) assert func2 != None assert len(func2.get_ops()) == 3
def test_convert(): element_type = Type.f32 shape = Shape([1, 3]) A = Parameter(element_type, shape) parameter_list = [A] # f32 to boolean function = Function(NodeVector([Convert(A, Type.boolean)]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) a = backend.make_primary_tensor_view(element_type, shape) result = backend.make_primary_tensor_view(Type.boolean, shape) a.write(util.numpy_to_c(np.array([1, 5, 3], dtype=np.float32)), 0, 12) result_arr = np.array([False, False, False], dtype=np.bool) result.write(util.numpy_to_c(result_arr), 0, 3) cf.call([result], [a]) result.read(util.numpy_to_c(result_arr), 0, 3) a_arr = np.array([1, 5, 3], dtype=np.float32) result_arr_ref = a_arr.astype(bool) assert np.allclose(result_arr, result_arr_ref) # f32 to i32 function = Function(NodeVector([Convert(A, Type.i32)]), parameter_list, 'test') backend, cf = make_backend_call_frame(function) result = backend.make_primary_tensor_view(Type.i32, shape) a.write(util.numpy_to_c(np.array([1.4, 5.5, 3.9], dtype=np.float32)), 0, 12) result_arr = np.array([0, 0, 0], dtype=np.int32) result.write(util.numpy_to_c(result_arr), 0, 12) cf.call([result], [a]) result.read(util.numpy_to_c(result_arr), 0, 12) a_arr = np.array([1.4, 5.4, 3.9], dtype=np.float32) result_arr_ref = a_arr.astype(int) assert np.allclose(result_arr, result_arr_ref)