rogermt commited on
Commit
bdf0499
·
verified ·
1 Parent(s): af6bda6

Add task084 build script, task209/366 solvers, and updated submission

Browse files
Files changed (1) hide show
  1. medal-solvers/build_task084_onnx.py +108 -0
medal-solvers/build_task084_onnx.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Build optimized ONNX model for Task 084.
2
+
3
+ Algorithm (175/175 verified):
4
+ 1. Input has a colored column at col 0 (N rows tall = grid is NxN)
5
+ 2. Draw anti-diagonal (color 2) from (0, N-1) to (N-2, 1)
6
+ 3. Fill bottom row (N-1, cols 1..N-1) with color 4
7
+ 4. Keep original column 0
8
+
9
+ ONNX approach:
10
+ - Detect N from sum of non-bg channels
11
+ - Use row/col grids to compute masks for anti-diagonal and bottom row
12
+ - Combine into output using channel selection
13
+
14
+ Score: 175/175 pass, file size 10KB (~12 pts estimated)
15
+
16
+ Usage:
17
+ python build_task084_onnx.py --task-data-dir ../task-data
18
+ """
19
+ from onnx import TensorProto
20
+ import numpy as np
21
+ from onnx_builder import OnnxBuilder, build_and_validate
22
+
23
+
24
+ def build_task084():
25
+ b = OnnxBuilder()
26
+ const, nd = b.const, b.nd
27
+
28
+ const('c_one', [1.0])
29
+ const('c_zero', [0.0])
30
+ const('c_half', [0.5])
31
+ const('axes123', [1, 2, 3], 'i')
32
+ const('axes23', [2, 3], 'i')
33
+ const('axes1', [1], 'i')
34
+ const('shape_1_1_30_30', [1, 1, 30, 30], 'i')
35
+ const('shape_1_1_1_1', [1, 1, 1, 1], 'i')
36
+ const('shape_1_10_1_1', [1, 10, 1, 1], 'i')
37
+
38
+ row_vals = np.arange(30, dtype=np.float32).reshape(1, 1, 30, 1) * np.ones((1, 1, 1, 30), dtype=np.float32)
39
+ col_vals = np.arange(30, dtype=np.float32).reshape(1, 1, 1, 30) * np.ones((1, 1, 30, 1), dtype=np.float32)
40
+ const('row_grid', row_vals)
41
+ const('col_grid', col_vals)
42
+
43
+ const('idx_0', [0], 'i')
44
+ const('idx_1', [1], 'i')
45
+ const('idx_2', [2], 'i')
46
+ const('idx_10', [10], 'i')
47
+ const('depth_10', [10.0])
48
+ const('oh_vals', [0.0, 1.0])
49
+ const('ch2_idx', [2], 'i')
50
+ const('ch4_idx', [4], 'i')
51
+
52
+ nonbg = nd('Slice', ['input', 'idx_1', 'idx_10', 'idx_1'], [[1, 9, 30, 30]])
53
+ n_total = nd('ReduceSum', [nonbg, 'axes123'], [[1, 1, 1, 1]], keepdims=1)
54
+
55
+ row_plus_col = nd('Add', ['row_grid', 'col_grid'], [[1, 1, 30, 30]])
56
+ n_minus_1 = nd('Sub', [n_total, 'c_one'], [[1, 1, 1, 1]])
57
+
58
+ on_antidiag = nd('Equal', [row_plus_col, n_minus_1], [([1, 1, 30, 30], TensorProto.BOOL)])
59
+ col_gt_0 = nd('Greater', ['col_grid', 'c_zero'], [([1, 1, 30, 30], TensorProto.BOOL)])
60
+ row_lt_nm1 = nd('Less', ['row_grid', n_minus_1], [([1, 1, 30, 30], TensorProto.BOOL)])
61
+ ad_and_col = nd('And', [on_antidiag, col_gt_0], [([1, 1, 30, 30], TensorProto.BOOL)])
62
+ antidiag_mask = nd('And', [ad_and_col, row_lt_nm1], [([1, 1, 30, 30], TensorProto.BOOL)])
63
+
64
+ on_bottom = nd('Equal', ['row_grid', n_minus_1], [([1, 1, 30, 30], TensorProto.BOOL)])
65
+ bottom_and_col = nd('And', [on_bottom, col_gt_0], [([1, 1, 30, 30], TensorProto.BOOL)])
66
+ col_lt_n_bool = nd('Less', ['col_grid', n_total], [([1, 1, 30, 30], TensorProto.BOOL)])
67
+ bottom_mask = nd('And', [bottom_and_col, col_lt_n_bool], [([1, 1, 30, 30], TensorProto.BOOL)])
68
+
69
+ antidiag_f = nd('Cast', [antidiag_mask], [[1, 1, 30, 30]], to=1)
70
+ bottom_f = nd('Cast', [bottom_mask], [[1, 1, 30, 30]], to=1)
71
+
72
+ col_eq_0 = nd('Equal', ['col_grid', 'c_zero'], [([1, 1, 30, 30], TensorProto.BOOL)])
73
+ col_eq_0_f = nd('Cast', [col_eq_0], [[1, 1, 30, 30]], to=1)
74
+ row_lt_n = nd('Less', ['row_grid', n_total], [([1, 1, 30, 30], TensorProto.BOOL)])
75
+ row_lt_n_f = nd('Cast', [row_lt_n], [[1, 1, 30, 30]], to=1)
76
+ col_lt_n = nd('Less', ['col_grid', n_total], [([1, 1, 30, 30], TensorProto.BOOL)])
77
+ col_lt_n_f = nd('Cast', [col_lt_n], [[1, 1, 30, 30]], to=1)
78
+ inside_grid_f = nd('Mul', [row_lt_n_f, col_lt_n_f], [[1, 1, 30, 30]])
79
+ col0_mask = nd('Mul', [col_eq_0_f, inside_grid_f], [[1, 1, 30, 30]])
80
+
81
+ col0_values = nd('Mul', ['input', col0_mask], [[1, 10, 30, 30]])
82
+
83
+ ch2_onehot = nd('OneHot', ['ch2_idx', 'depth_10', 'oh_vals'], [[1, 10]], axis=1)
84
+ ch2_onehot_4d = nd('Reshape', [ch2_onehot, 'shape_1_10_1_1'], [[1, 10, 1, 1]])
85
+ antidiag_ch2 = nd('Mul', [ch2_onehot_4d, antidiag_f], [[1, 10, 30, 30]])
86
+
87
+ ch4_onehot = nd('OneHot', ['ch4_idx', 'depth_10', 'oh_vals'], [[1, 10]], axis=1)
88
+ ch4_onehot_4d = nd('Reshape', [ch4_onehot, 'shape_1_10_1_1'], [[1, 10, 1, 1]])
89
+ bottom_ch4 = nd('Mul', [ch4_onehot_4d, bottom_f], [[1, 10, 30, 30]])
90
+
91
+ occupied_1 = nd('Add', [col0_mask, antidiag_f], [[1, 1, 30, 30]])
92
+ occupied = nd('Add', [occupied_1, bottom_f], [[1, 1, 30, 30]])
93
+ ch0_mask_raw = nd('Sub', [inside_grid_f, occupied], [[1, 1, 30, 30]])
94
+
95
+ const('ch0_idx', [0], 'i')
96
+ ch0_onehot = nd('OneHot', ['ch0_idx', 'depth_10', 'oh_vals'], [[1, 10]], axis=1)
97
+ ch0_onehot_4d = nd('Reshape', [ch0_onehot, 'shape_1_10_1_1'], [[1, 10, 1, 1]])
98
+ bg_ch0 = nd('Mul', [ch0_onehot_4d, ch0_mask_raw], [[1, 10, 30, 30]])
99
+
100
+ out_1 = nd('Add', [col0_values, antidiag_ch2], [[1, 10, 30, 30]])
101
+ out_2 = nd('Add', [out_1, bottom_ch4], [[1, 10, 30, 30]])
102
+ final = nd('Add', [out_2, bg_ch0], [[1, 10, 30, 30]])
103
+
104
+ return b.finish('task084', last_tensor=final)
105
+
106
+
107
+ if __name__ == '__main__':
108
+ build_and_validate(build_task084, task_num=84)