def template_dot_one_placeholder_and_scalar(row, col, scalar, flex_exceptions, iters): arg_array = np.array([i for i in range(col * row)]).reshape(row, col) ng_placeholder = template_create_placeholder(row, col) ng_var = ng.placeholder(()) ng_fun = ng.dot(ng_var, ng_placeholder) flex_exceptions_index = 0 print("Initial scalar: ", scalar) print("Matrix:\n", arg_array) with executor(ng_fun, ng_var, ng_placeholder) as m_executor: for i in range(row): print("Iteration " + str(i + 1)) ng_op_out = m_executor(scalar, arg_array) np_op_out = np.dot(scalar, arg_array) # After each iteration matrix values are updated. arg_array = ng_op_out print("Flex dot product result: \n", ng_op_out) print("Numpy dot product result: \n", np_op_out) try: assert_allclose(ng_op_out, np_op_out) except AssertionError: print( "Flex dot product result doesn't match to numpy.\n" "Try to check if flex result is inside flex exceptions list" ) print("Flex dot product result: \n", ng_op_out) print("Current array inside flex exceptions list: \n", flex_exceptions[flex_exceptions_index]) assert_allclose(ng_op_out, flex_exceptions[flex_exceptions_index]) # Iterate to the next element of flex exceptions list flex_exceptions_index += 1
def test_gemm(transformer_factory): """ TODO: make this more interesting """ n, c = 32, 32 N = ng.make_axis(length=n, name='N') C = ng.make_axis(length=c) X = ng.placeholder(axes=[C, N]) Y = ng.placeholder(axes=[N]) W = ng.variable(axes=[C - 1], initial_value=0.1) Y_hat = ng.dot(W, X) with executor(Y_hat, X) as ex: mm_executor = ex w = np.ones(c) * 0.1 xs = np.ones(n * c).reshape(c, n) for ii in range(3): y_hat_val = mm_executor(xs) # 8.8 fixed point test # assert np.allclose(np.dot(xs, w) - y_hat_val, 0.075*np.ones(n)) # autoflex test assert_allclose(np.dot(xs, w), y_hat_val)
def template_dot_two_placeholders(rows_1, col_1, col_2): ng_placeholder2, ng_placeholder1 = \ template_create_placeholders_for_multiplication(col_2, col_1, rows_1) ng_fun = ng.dot(ng_placeholder1, ng_placeholder2) arg_array1 = np.array([i for i in range(col_1 * rows_1) ]).reshape(rows_1, col_1) arg_array2 = np.array([i for i in range(col_1 * col_2) ]).reshape(col_1, col_2) print("Matrix 1:\n", arg_array1) print("Matrix 2:\n", arg_array2) with executor(ng_fun, ng_placeholder1, ng_placeholder2) as mm_executor: np_op_out = np.dot(arg_array1, arg_array2) ng_op_out = mm_executor(arg_array1, arg_array2) print("Flex dot product result: \n", ng_op_out) print("Numpy dot product result: \n", np_op_out) assert_allclose(ng_op_out, np_op_out)
def test_plusconst(transformer_factory): """ x + 1.5 """ x = ng.placeholder(()) x_plus_const = x + 1.5 with executor(x_plus_const, x) as ex: plusconst_executor = ex for i in range(5): # 8.8 fixed point test # assert plusconst_executor(i) == i + 1.5 # autoflex test if i == 1: # expect overflow assert_allclose(plusconst_executor(i), 1.9999) else: assert plusconst_executor(i) == i + 1.5
def execute_calculation(operands, first_operand, const_executor): iterations = len(operands) != 1 for i in operands: _operands, expected_result, description = unpack_list(*i) if description: print("Description: ", description) print("Operands: ", _operands) print("Expected result: ", expected_result) flex_result = const_executor(*_operands) try: print("flex_result: {0:.30}".format(float(flex_result))) except TypeError: # exception for arrays np.set_printoptions(precision=30) print("flex_result: {}".format(flex_result)) print("difference: ", flex_result - expected_result) if iterations: assert_allclose(flex_result, expected_result) elif not isinstance(first_operand, np.ndarray): assert flex_result == expected_result else: assert np.array_equal(flex_result, expected_result)
def test_sum(transformer_factory, num_units, sequence_length, batch_size): """ This tests for a non-deterministic error that arose in ng.sum following a dot product using the gpu transformer. """ shape = (num_units, sequence_length, batch_size) np_inp = np.random.uniform(-1, 1, shape) # Use an identity weight matrix on top of it np_w = np.eye(shape[0]) # Create ngraph versions inp = ng.constant(np_inp) reduction_axes = inp.axes[:-2] other_axes = inp.axes[-2:] new_axis = ng.make_axis(length=shape[0]) w_axes = ng.make_axes(new_axis) | reduction_axes w = ng.constant(np_w, axes=w_axes) # Reshape to do similar dot in numpy inp_reshape = np.reshape( np_inp, (np.prod(reduction_axes.lengths), np.prod(other_axes.lengths))) w_reshape = np.reshape(np_w, (new_axis.length, inp_reshape.shape[0])) # Reduce dimensions with identity weight matrix np_x = np.dot(w_reshape, inp_reshape) x = ng.dot(w, inp) # Sum over all but the first axis output_axes = ng.make_axes(x.axes[0]) y = ng.sum(x, out_axes=output_axes) np_y = np.sum(np_x, axis=1) with executor([y, x]) as f: y_val, x_val = f() assert_allclose(x_val.ravel(), np_x.ravel(), atol=1e-5) assert_allclose(y_val, np_y, atol=1e-5)
def template_dot_one_placeholder(row, col, const_val, flex_exceptions, iters): ng_placeholder, ng_variable = template_create_placeholder_and_variable( col, row, const_val) ng_fun = ng.dot(ng_variable, ng_placeholder) arg_array = np.array([i for i in range(row * col)]).reshape(row, col) arg_array2 = np.copy(arg_array) flex_exceptions_index = 0 vector = np.ones(row) * const_val print("Vector: \n", vector) with executor(ng_fun, ng_placeholder) as mm_executor: for i in range(iters): print("Iteration " + str(i + 1)) # After each iteration, matrix values are changed arg_array = arg_array2 * (i + 1) print("Matrix: \n", arg_array) ng_op_out = mm_executor(arg_array) np_op_out = np.dot(vector, arg_array) print("Flex dot product result: \n ", ng_op_out) print("Numpy dot product result: \n", np_op_out) try: assert_allclose(ng_op_out, np_op_out) except AssertionError: print( "Flex dot product result doesn't match to numpy.\n" "Try to check if flex result is inside flex exceptions list" ) print("Flex dot product result: \n", ng_op_out) print("Current array inside flex exceptions list: \n", flex_exceptions[flex_exceptions_index]) assert list( ng_op_out) == flex_exceptions[flex_exceptions_index] # Iterate to the next element of flex exceptions list flex_exceptions_index += 1