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([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 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([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)), 16) b.write(util.numpy_to_c(np.array([5, 6, 7, 8], dtype=np.float32)), 16) c.write(util.numpy_to_c(np.array([9, 10, 11, 12], dtype=np.float32)), 16) result_arr = np.array([0, 0, 0, 0], dtype=np.float32) result.write(util.numpy_to_c(result_arr), 16) handle = backend.compile(function) handle.call([result], [a, b, c]) result.read(util.numpy_to_c(result_arr), 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(op_str, a, b): if op_str == '+': return a + b elif op_str == 'Add': return Add(a, b) elif op_str == '-': return a - b elif op_str == 'Sub': return Subtract(a, b) elif op_str == '*': return a * b elif op_str == 'Mul': return Multiply(a, b) elif op_str == '/': return a / b elif op_str == 'Div': return Divide(a, b) elif op_str == 'Dot': return Dot(a, b) elif op_str == 'Equal': return Equal(a, b) elif op_str == 'Greater': return Greater(a, b) elif op_str == 'GreaterEq': return GreaterEq(a, b) elif op_str == 'Less': return Less(a, b) elif op_str == 'LessEq': return LessEq(a, b) elif op_str == 'Maximum': return Maximum(a, b) elif op_str == 'Minimum': return Minimum(a, b) elif op_str == 'NotEqual': return NotEqual(a, b) elif op_str == 'Power': return Power(a, b)
def multiply(left_node, right_node, name=None): # type: (NodeInput, NodeInput, str) -> Node """Return node which applies f(x) = A*B to the input nodes elementwise.""" return Multiply(left_node, right_node)