|
10 | 10 | using LoopVectorization: LoopVectorization |
11 | 11 | using Bumper: Bumper |
12 | 12 |
|
13 | | - # bring the generator into scope |
| 13 | + # Bring the generator into scope |
14 | 14 | include("supposition_utils.jl") |
15 | 15 |
|
16 | | - n_features = 5 |
17 | | - max_layers = 20 |
18 | | - T = Float64 |
19 | | - operators = OperatorEnum(((abs, cos, exp), (+, -, *, /), (fma, clamp, +, max))) |
| 16 | + # Test configuration constants |
| 17 | + const N_FEATURES = 5 |
| 18 | + const MAX_LAYERS = 20 |
| 19 | + const NUMERIC_TYPE = Float64 |
| 20 | + const OPERATORS = OperatorEnum(((abs, cos, exp), (+, -, *, /), (fma, clamp, +, max))) |
| 21 | + const VARIABLE_NAMES = ["x$i" for i in 1:N_FEATURES] |
20 | 22 |
|
| 23 | + # Create expression generator |
21 | 24 | expr_gen = make_expression_generator( |
22 | | - T; num_features=n_features, max_layers=max_layers, operators=operators |
| 25 | + NUMERIC_TYPE; num_features=N_FEATURES, max_layers=MAX_LAYERS, operators=OPERATORS |
23 | 26 | ) |
24 | 27 |
|
| 28 | + # Test 1: Round-trip string parsing consistency |
25 | 29 | result = @check function roundtrip_string(ex=expr_gen) |
26 | 30 | tree_str = string_tree(ex) |
27 | 31 | ex_parsed = parse_expression( |
28 | 32 | Meta.parse(tree_str); |
29 | 33 | operators=get_operators(ex), |
30 | | - variable_names=["x$i" for i in 1:n_features], |
| 34 | + variable_names=VARIABLE_NAMES, |
31 | 35 | node_type=Node{Float64,3}, |
32 | 36 | ) |
33 | 37 | return ex == ex_parsed |
34 | 38 | end |
35 | 39 | @test something(result.result) isa Supposition.Pass |
36 | 40 |
|
37 | | - input_gen = make_input_matrix_generator(T; n_features) |
38 | | - args_gen = map( |
39 | | - (ex, X, turbo, bumper) -> (; ex, X, turbo, bumper), |
40 | | - expr_gen, |
41 | | - input_gen, |
42 | | - Data.Booleans(), |
43 | | - Data.Booleans(), |
44 | | - ) |
45 | | - # We only consider expressions that don't have NaN/Inf/etc. |
46 | | - clean_args_gen = filter(args -> eval_tree_array(args.ex, args.X)[2], args_gen) |
47 | | - result2 = @check max_examples = 1000 function eval_against_string(args=clean_args_gen) |
48 | | - (; ex, X, turbo, bumper) = args |
49 | | - expression_result, ok = eval_tree_array(ex, X; turbo, bumper) |
| 41 | + # Test 2: Evaluation consistency against string representation |
| 42 | + input_gen = make_input_matrix_generator(NUMERIC_TYPE; n_features=N_FEATURES) |
| 43 | + |
| 44 | + # Helper function to create clean argument generators |
| 45 | + function clean_args_gen_maker(default_turbo) |
| 46 | + args_gen = map( |
| 47 | + (ex, X, turbo, bumper) -> let |
| 48 | + result, ok = eval_tree_array(ex, X; turbo, bumper) |
| 49 | + (; ex, X, turbo, bumper, result, ok) |
| 50 | + end, |
| 51 | + expr_gen, |
| 52 | + input_gen, |
| 53 | + map(_ -> default_turbo, Data.Booleans()), |
| 54 | + Data.Booleans(), |
| 55 | + ) |
| 56 | + # We only consider expressions that don't have NaN/Inf/etc. |
| 57 | + return filter(args -> args.ok, args_gen) |
| 58 | + end |
| 59 | + |
| 60 | + # Helper function to create turbo evaluation function |
| 61 | + function create_turbo_function(tree_str) |
| 62 | + turbo_expr = "(x1, x2, x3, x4, x5) -> let y = deepcopy(x1); @turbo(@.(y = ($tree_str))); y; end" |
| 63 | + return eval(Meta.parse(turbo_expr)) |
| 64 | + end |
| 65 | + |
| 66 | + # Helper function to create regular evaluation function |
| 67 | + function create_regular_function(tree_str) |
| 68 | + regular_expr = "(x1, x2, x3, x4, x5) -> ($tree_str)" |
| 69 | + return eval(Meta.parse(regular_expr)) |
| 70 | + end |
| 71 | + |
| 72 | + # Helper function to evaluate with turbo |
| 73 | + function evaluate_with_turbo(f, X) |
| 74 | + return Base.invokelatest(f, X[1, :], X[2, :], X[3, :], X[4, :], X[5, :]) |
| 75 | + end |
| 76 | + |
| 77 | + # Helper function to evaluate without turbo |
| 78 | + function evaluate_without_turbo(f, X) |
| 79 | + return Float64[Base.invokelatest(f, x...) for x in eachcol(X)] |
| 80 | + end |
| 81 | + |
| 82 | + # Helper function to evaluate expression against its string representation |
| 83 | + function _eval_against_string((; ex, X, turbo, bumper, result)) |
50 | 84 | tree_str = string_tree(ex) |
51 | | - f = eval(Meta.parse("(x1, x2, x3, x4, x5) -> ($tree_str)")) |
52 | | - true_result = Float64[Base.invokelatest(f, x...) for x in eachcol(X)] |
53 | | - return ok && expression_result ≈ true_result |
| 85 | + true_result = if turbo |
| 86 | + # Turbo changes the operators, so we need to use a different function |
| 87 | + f = create_turbo_function(tree_str) |
| 88 | + evaluate_with_turbo(f, X) |
| 89 | + else |
| 90 | + f = create_regular_function(tree_str) |
| 91 | + evaluate_without_turbo(f, X) |
| 92 | + end |
| 93 | + |
| 94 | + return result ≈ true_result |
54 | 95 | end |
55 | | - @test something(result2.result) isa Supposition.Pass |
| 96 | + |
| 97 | + # Test evaluation consistency without turbo |
| 98 | + no_turbo_args_gen = clean_args_gen_maker(false) |
| 99 | + result2_noturbo = @check max_examples = 2000 function eval_against_string( |
| 100 | + args=no_turbo_args_gen |
| 101 | + ) |
| 102 | + return _eval_against_string(args) |
| 103 | + end |
| 104 | + @test something(result2_noturbo.result) isa Supposition.Pass |
| 105 | + |
| 106 | + # TODO: We need to run this test manually, as there are too many |
| 107 | + # examples where turbo evaluation is slightly different. |
| 108 | + # # Test evaluation consistency with turbo (fewer examples due to performance) |
| 109 | + # turbo_args_gen = clean_args_gen_maker(true) |
| 110 | + # counter = Ref(0) |
| 111 | + # result2_turbo = @check max_examples = 50 function eval_against_string( |
| 112 | + # args=turbo_args_gen |
| 113 | + # ) |
| 114 | + # c = (counter[] += 1) |
| 115 | + # if c > 50 |
| 116 | + # # Supposition seems to not listen to max_examples sometimes |
| 117 | + # return true |
| 118 | + # else |
| 119 | + # return _eval_against_string(args) |
| 120 | + # end |
| 121 | + # end |
| 122 | + # @test something(result2_turbo.result) isa Supposition.Pass |
56 | 123 | end |
0 commit comments