def test_set_1d_negative_slice(): x = np.array([1,2,3,4,5,6]) idx = slice(4,2,-1) y = [10, 20] x2 = x.copy() x[idx] = y testing_helpers.expect(setidx, (x2, idx, y), x)
def test_set_1d_simple_slice(): x = np.array([1,2,3,4,5,6]) idx = slice(2,4) y = [10, 20] x2 = x.copy() x[idx] = y testing_helpers.expect(setidx, [x2, idx, y], x)
def test_simple_regression(): N = 10 x = np.random.randn(N).astype('float64') slope = 903.29 offset = 102.1 y = slope * x + offset expect(fit_simple_regression, [x, y], (slope, offset))
def test_set_1d_step_slice_to_const(): x = np.array([1,2,3,4,5,6]) idx = slice(2,4,2) y = 0 x2 = x.copy() x[idx] = y testing_helpers.expect(setidx, (x2, idx, y), x)
def test_set_idx_1d(): idx = 10 for vec in vecs: vec1, vec2 = vec.copy(), vec.copy() val = -vec[idx] vec2[idx] = val expect(set_idx_1d, [vec1, idx, val], vec2)
def test_constants_across_control_flow(): expect(const_across_control_flow, [True], 1) typed_fn = parakeet.typed_repr(const_across_control_flow, [True]) assert len(typed_fn.body) == 1, "Fn body too long: " + str(typed_fn.body) stmt = typed_fn.body[0] assert isinstance(stmt, syntax.Return) assert isinstance(stmt.value, syntax.Const)
def test_always_false(): expect(always_false_branch, [], 1) typed_fn = parakeet.typed_repr(always_false_branch, []) assert len(typed_fn.body) == 1, "Fn body too long: " + str(typed_fn.body) stmt = typed_fn.body[0] assert isinstance(stmt, syntax.Return) assert isinstance(stmt.value, syntax.Const)
def run_vm(xtype, ytype): x = get_vector(xtype) y = get_vector(ytype) y = y.reshape(mat_shape) x = x[:y.shape[0]] expect(np.dot, [x, y], np.dot(x, y))
def test_set_idx_2d(): i = 2 j = 2 for mat in matrices: mat1, mat2 = mat.copy(), mat.copy() val = -mat[i,j] mat2[i,j] = val expect(set_idx_2d, [mat1, i, j, val], mat2)
def test_assign_slices(): for m in matrices: m_expect = m.copy() m_input = m.copy() (i,j,k,l) = (0,2,0,4) (a,b,c,d) = (1,3,5,9) m_expect[i:j, k:l] = m_expect[a:b, c:d] expect(assign_slices, [m_input, (i,j,k,l), (a,b,c,d)], m_expect) expect(assign_slices, [m_input, (i,j,k,l), (a,b,c,d)], m_expect)
def test_set_idx_3d(): i = 2 j = 3 k = 1 for x in tensors: x1, x2 = x.copy(), x.copy() val = -x[i, j, k] x2[i, j, k] = val expect(set_idx_3d, [x1, i, j, k, val], x2)
def test_copy_elimination(): x = np.array([[1, 2, 3], [4, 5, 6]]) expect(nested_add1, [x], x + 1.0) typed_fn = parakeet.typed_repr(nested_add1, [x]) lowered = lowering.apply(typed_fn) n_loops = count_loops(lowered) n_expected = 3 if config.opt_loop_unrolling else 2 assert n_loops <= n_expected, \ "Too many loops generated! Expected at most 2, got %d" % n_loops
def unary(fn): for x in mats: try: expected = fn(x) except: expected = fn(x.astype('int')) if expected.dtype == 'float16': expected = fn(x.astype('int')) expect(fn, [x], expected, fn.__name__ + "-" + str(x.dtype) + str(len(x.shape)))
def all_tuples(f, unpack_args=True): """ Given a function which should act as the identity, test it on multiple tuples """ for t in [ints, mixed, nested2, nested2]: if unpack_args: expect(f, t, t) else: expect(f, [t], t)
def test_nested_2d(): expect(create_const, [np.array([1, 2])], np.array([[1, 2], [1, 2], [1, 2], [1, 2]])) expect(create_const, [np.array([1.0, 2.0])], np.array([[1.0, 2.0], [1.0, 2.0], [1.0, 2.0], [1.0, 2.0]])) expect( create_const, [np.array([True, False])], np.array([[True, False], [True, False], [True, False], [True, False]]))
def test_histogram_intersection_kernel(): n, d = 3, 4 X = np.arange(n * d).reshape((n, d)) K = kernel(X) testing_helpers.expect(kernel, [X], K)
def test_call_add1(): expect(call_add1, [1], 2)
def test_erode_identity_shape(): testing_helpers.expect(erode, [x, (1,1)], x)
def test_erode(): shape = (3,3) expected = np.array([[1, 1, 2], [1,1,2]]) testing_helpers.expect(erode, [x, shape], expected)
def test_if_true_const(): expect(if_true_const, [], 1)
def test_find_border(): is_background = np.empty((20, 20), dtype=np.bool) expect(find_border, [is_background], find_border(is_background))
def test_simple_merge(): expect(simple_merge, [2], 2) expect(simple_merge, [0], 1)
def test_one_sided_merge(): expect(one_sided_merge, [100,True], 1) expect(one_sided_merge, [100,False], 100)
def test_simple_branch(): expect(simple_branch, [9], 1) expect(simple_branch, [10], 2)
def test_always_1(): expect(always_1, [], 1)
def test_if_exp(): expect(use_if_exp, [9], 1) expect(use_if_exp, [10], 2)
def test_use_global(): expect(use_global, [3], 8)
def test_create_const(): expect(create_const, [1], np.array([1, 1, 1, 1])) expect(create_const, [1.0], np.array([1.0, 1.0, 1.0, 1.0])) expect(create_const, [True], np.array([True, True, True, True]))
def test_count_thresh(): v = np.array([1.2, 1.4, 5.0, 2, 3]) parakeet_result = count_thresh(v, 2.0) python_result = count_thresh_orig(v, 2.0) testing_helpers.expect(count_thresh, [v, 2.0], count_thresh_orig(v, 2.0))
def test_nested_ident(): expect(call_nested_ident, [1], 1)