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 test_always_true(): expect(always_true_branch, [], 0) typed_fn = parakeet.typed_repr(always_true_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 expect_shape(python_fn, args_list, expected): print "[expect_shape]" print " -- fn: ", python_fn print " -- args: ", args_list typed_fn = parakeet.typed_repr(python_fn, args_list) print " -- types: ", typed_fn.input_types result_shape = call_shape_expr(typed_fn) assert result_shape == expected, \ "Expected shape %s, but got: %s" % (expected, result_shape)
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 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 test_combine_nested_maps(): x = np.array([[1,2], [3,4]]) typed_fn = parakeet.typed_repr(allpairs_dist, [x]) typed_fn = high_level_optimizations.apply(typed_fn) assert len(typed_fn.body) == 1 stmt = typed_fn.body[0] assert stmt.__class__ is Return, "Expected Return but got %s" % stmt v = stmt.value assert v.__class__ is OuterMap, "Expected OuterMap but got %s" % v assert len(v.args) == 2, "Expected OuterMap to have two args, but got %s" % (v.args,)
def test_combine_nested_maps(): x = np.array([[1,2], [3,4]]) typed_fn = parakeet.typed_repr(allpairs_dist, [x]) typed_fn = high_level_optimizations.apply(typed_fn) assert len(typed_fn.body) == 1 stmt = typed_fn.body[0] assert stmt.__class__ is Return v = stmt.value assert v.__class__ is OuterMap assert len(v.args) == 2
def test_combine_nested_maps(): x = np.array([[1, 2], [3, 4]]) typed_fn = parakeet.typed_repr(allpairs_dist, [x]) typed_fn = high_level_optimizations.apply(typed_fn) assert len(typed_fn.body) == 1 stmt = typed_fn.body[0] assert stmt.__class__ is Return, "Expected Return but got %s" % stmt v = stmt.value assert v.__class__ is OuterMap, "Expected OuterMap but got %s" % v assert len( v.args) == 2, "Expected OuterMap to have two args, but got %s" % ( v.args, )
def test_combine_nested_index_maps(): n = 3 typed_fn = parakeet.typed_repr(allpairs_add_imap, [n]) print "TYPED FN", typed_fn typed_fn = high_level_optimizations.apply(typed_fn) print "OPT FN", typed_fn assert len(typed_fn.body) in (1, 2) stmt = typed_fn.body[-1] print "STMT", stmt assert stmt.__class__ is Return, "Expected Return but got %s" % stmt v = stmt.value print "VALUE", v assert v.__class__ is IndexMap, "Expected IndexMap but got %s" % v nested_fn = get_fn(v.fn) assert len(nested_fn.body) == 1 nested_stmt = nested_fn.body[0] assert nested_stmt.__class__ is Return nested_value = nested_stmt.value assert nested_value.__class__ is PrimCall, "Expected PrimCall but got %s" % nested_value
def test_combine_nested_index_maps(): n = 3 typed_fn = parakeet.typed_repr(allpairs_add_imap, [n]) print "TYPED FN", typed_fn typed_fn = high_level_optimizations.apply(typed_fn) print "OPT FN", typed_fn assert len(typed_fn.body) in (1,2) stmt = typed_fn.body[-1] print "STMT", stmt assert stmt.__class__ is Return, "Expected Return but got %s" % stmt v = stmt.value print "VALUE", v assert v.__class__ is IndexMap, "Expected IndexMap but got %s" % v nested_fn = get_fn(v.fn) assert len(nested_fn.body) == 1 nested_stmt = nested_fn.body[0] assert nested_stmt.__class__ is Return nested_value = nested_stmt.value assert nested_value.__class__ is PrimCall, "Expected PrimCall but got %s" % nested_value
def test_simple_constant_folding(): expect(lots_of_arith, [1], 1) typed_fn = parakeet.typed_repr(lots_of_arith, [1], optimize=True) assert len(typed_fn.body) == 1, \ "Insufficiently simplified body: %s" % typed_fn
def test_simple_constant_folding(): expect(lots_of_arith, [1], 1) typed_fn = parakeet.typed_repr(lots_of_arith, [1], optimize = True) assert len(typed_fn.body) == 1, \ "Insufficiently simplified body: %s" % typed_fn
def test_inline(): typed_fn = parakeet.typed_repr(C, [1]) assert len(typed_fn.body) in [1,2], \ "Expected body to be 1 or 2 statements, got %s" % typed_fn assert simple_fn(typed_fn) expect(C, [1], 2)