rogermt commited on
Commit
6853363
Β·
verified Β·
1 Parent(s): 23347ef

V67 fix: tasks 176/292 remove fixed_shapes (variable widths, verified 25/25+28/28), task 315 Gather replaces Resize, task 339 remove fixed_shapes

Browse files
own-solver/neurogolf_solver/solvers/wave8.py CHANGED
@@ -1 +1,461 @@
1
- /app/wave8_upload.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Wave 8: More reverse-engineered solvers from submission-5743.
3
+
4
+ Tasks targeted (ALL genuinely unsolved in V65):
5
+ - s_fill_mask_delta: Task 176 β€” fill specific spatial positions with delta color (3x25)
6
+ - s_recolor_col_mask: Task 292 β€” recolor channel 4 at columnar mask positions (3x20)
7
+ - s_palette_lookup_markers: Task 262 β€” look up palette from column position of markers (3x3)
8
+ - s_count_dominant_bar: Task 339 β€” count non-bg pixels, output bar of dominant color (3x3β†’1x9)
9
+ - s_maxpool_3x3_downsample: Task 130 β€” MaxPool 3x3 stride 3 downsample (9x9β†’3x3)
10
+ - s_tile_mask_color2: Task 315 β€” tile 3x3β†’9x9, mask by color 2 presence
11
+ - s_matmul_projection: Task 296 β€” MatMul projection from 5x7β†’3x3
12
+ - s_count_colors_pattern_bank: Task 61 β€” count colors 4-9 present β†’ modular pattern (18x18)
13
+
14
+ Architecture source: submission-5743.zip (LB leader)
15
+ All architectures validated: 50/50 exact match on random inputs.
16
+ NOTE: Minimal detection β€” build unconditionally, let validate() filter.
17
+ """
18
+
19
+ import numpy as np
20
+ from onnx import helper as oh, numpy_helper as onh, TensorProto
21
+ from ..data_loader import get_exs, fixed_shapes
22
+
23
+
24
+ def s_fill_mask_delta(td):
25
+ """Task 176: At hardcoded spatial mask positions in a 3x25 grid,
26
+ convert bg (color 0) to color 4.
27
+
28
+ Architecture: Slice(3x25) β†’ Conv1x1(sum all channels) β†’ Mul(mask) β†’ Mul(delta_ch) β†’ Add β†’ Pad
29
+ """
30
+ exs = get_exs(td)
31
+ if len(exs) < 2:
32
+ return None
33
+ # Task has variable widths (10,15,18,25,...) β€” do NOT use fixed_shapes().
34
+ # Model operates on 30x30 canvas with periodic mask that works for all widths.
35
+ for inp, out in exs[:3]:
36
+ if inp.shape[0] != 3 or inp.shape != out.shape:
37
+ return None
38
+
39
+ MASK = np.array([
40
+ [0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0],
41
+ [1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1],
42
+ [1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1]
43
+ ], dtype=np.float32).reshape(1, 1, 3, 25)
44
+
45
+ delta_ch = np.zeros((1, 10, 1, 1), dtype=np.float32)
46
+ delta_ch[0, 0, 0, 0] = -1.0
47
+ delta_ch[0, 4, 0, 0] = 1.0
48
+
49
+ Wall = np.ones((1, 10, 1, 1), dtype=np.float32)
50
+
51
+ inits = [
52
+ onh.from_array(Wall, 'Wall'),
53
+ onh.from_array(MASK, 'MASK'),
54
+ onh.from_array(delta_ch, 'delta_ch'),
55
+ onh.from_array(np.array([0, 0, 0, 0], dtype=np.int64), 'crop_starts'),
56
+ onh.from_array(np.array([1, 10, 3, 25], dtype=np.int64), 'crop_ends'),
57
+ onh.from_array(np.array([0, 1, 2, 3], dtype=np.int64), 'crop_axes'),
58
+ onh.from_array(np.array([1, 1, 1, 1], dtype=np.int64), 'crop_steps'),
59
+ onh.from_array(np.array([0, 0, 0, 0, 0, 0, 27, 5], dtype=np.int64), 'pad_to_external'),
60
+ onh.from_array(np.float32(0.0), 'pad_zero'),
61
+ ]
62
+
63
+ nodes = [
64
+ oh.make_node('Slice', ['input', 'crop_starts', 'crop_ends', 'crop_axes', 'crop_steps'], ['input_inner']),
65
+ oh.make_node('Conv', ['input_inner', 'Wall'], ['inside'], kernel_shape=[1, 1], pads=[0, 0, 0, 0]),
66
+ oh.make_node('Mul', ['MASK', 'inside'], ['efmask']),
67
+ oh.make_node('Mul', ['efmask', 'delta_ch'], ['delta']),
68
+ oh.make_node('Add', ['input_inner', 'delta'], ['output_inner']),
69
+ oh.make_node('Pad', ['output_inner', 'pad_to_external', 'pad_zero'], ['output']),
70
+ ]
71
+
72
+ x = oh.make_tensor_value_info('input', TensorProto.FLOAT, [1, 10, 30, 30])
73
+ y = oh.make_tensor_value_info('output', TensorProto.FLOAT, [1, 10, 30, 30])
74
+ g = oh.make_graph(nodes, 'g', [x], [y], initializer=inits)
75
+ return oh.make_model(g, ir_version=8, opset_imports=[oh.make_opsetid('', 11)])
76
+
77
+
78
+ def s_recolor_col_mask(td):
79
+ """Task 292: At every-3rd-column positions in a 3x20 grid,
80
+ recolor color 4 β†’ color 6.
81
+
82
+ Architecture: Slice(3x20) β†’ Conv1x1(select ch4) β†’ Mul(col_mask) β†’ Conv1x1(recolor 4β†’6) β†’ Add β†’ Pad
83
+ """
84
+ exs = get_exs(td)
85
+ if len(exs) < 2:
86
+ return None
87
+ # Task has variable widths (10,11,...,20) β€” do NOT use fixed_shapes().
88
+ # Model operates on 30x30 canvas with periodic col_mask that works for all widths.
89
+ for inp, out in exs[:3]:
90
+ if inp.shape[0] != 3 or inp.shape != out.shape:
91
+ return None
92
+
93
+ w_ch4 = np.zeros((1, 10, 1, 1), dtype=np.float32)
94
+ w_ch4[0, 4, 0, 0] = 1.0
95
+
96
+ col_mask = np.zeros((1, 1, 3, 20), dtype=np.float32)
97
+ for c in range(0, 20, 3):
98
+ col_mask[0, 0, :, c] = 1.0
99
+
100
+ w_paint = np.zeros((10, 1, 1, 1), dtype=np.float32)
101
+ w_paint[4, 0, 0, 0] = -1.0
102
+ w_paint[6, 0, 0, 0] = 1.0
103
+
104
+ inits = [
105
+ onh.from_array(w_ch4, 'w_ch4'),
106
+ onh.from_array(col_mask, 'col_mask'),
107
+ onh.from_array(w_paint, 'w_paint'),
108
+ onh.from_array(np.array([0, 0, 0, 0], dtype=np.int64), 'crop_starts'),
109
+ onh.from_array(np.array([1, 10, 3, 20], dtype=np.int64), 'crop_ends'),
110
+ onh.from_array(np.array([0, 1, 2, 3], dtype=np.int64), 'crop_axes'),
111
+ onh.from_array(np.array([1, 1, 1, 1], dtype=np.int64), 'crop_steps'),
112
+ onh.from_array(np.array([0, 0, 0, 0, 0, 0, 27, 10], dtype=np.int64), 'pad_to_external'),
113
+ onh.from_array(np.float32(0.0), 'pad_zero'),
114
+ ]
115
+
116
+ nodes = [
117
+ oh.make_node('Slice', ['input', 'crop_starts', 'crop_ends', 'crop_axes', 'crop_steps'], ['input_inner']),
118
+ oh.make_node('Conv', ['input_inner', 'w_ch4'], ['mask4'], kernel_shape=[1, 1], pads=[0, 0, 0, 0]),
119
+ oh.make_node('Mul', ['mask4', 'col_mask'], ['convert']),
120
+ oh.make_node('Conv', ['convert', 'w_paint'], ['paint'], kernel_shape=[1, 1], pads=[0, 0, 0, 0]),
121
+ oh.make_node('Add', ['input_inner', 'paint'], ['output_inner']),
122
+ oh.make_node('Pad', ['output_inner', 'pad_to_external', 'pad_zero'], ['output']),
123
+ ]
124
+
125
+ x = oh.make_tensor_value_info('input', TensorProto.FLOAT, [1, 10, 30, 30])
126
+ y = oh.make_tensor_value_info('output', TensorProto.FLOAT, [1, 10, 30, 30])
127
+ g = oh.make_graph(nodes, 'g', [x], [y], initializer=inits)
128
+ return oh.make_model(g, ir_version=8, opset_imports=[oh.make_opsetid('', 11)])
129
+
130
+
131
+ def s_palette_lookup_markers(td):
132
+ """Task 262: Look up palette from column positions of color-5 markers.
133
+
134
+ Input: 3x3 grid with color-5 markers (one per row).
135
+ For each row, the column where the marker is indexes into palette [2,4,3].
136
+ Output: 3x3 grid where each row is filled with the looked-up color.
137
+
138
+ Architecture: Slice(ch5,3x3) β†’ ArgMax(axis=3) β†’ Squeeze β†’ Gather(palette) β†’
139
+ Unsqueeze β†’ Expand(3x3) β†’ Pad β†’ OneHot
140
+ """
141
+ exs = get_exs(td)
142
+ if len(exs) < 2:
143
+ return None
144
+ sp = fixed_shapes(td)
145
+ if sp is None:
146
+ return None
147
+ (IH, IW), (OH, OW) = sp
148
+ if IH != 3 or IW != 3 or OH != 3 or OW != 3:
149
+ return None
150
+
151
+ inits = [
152
+ onh.from_array(np.array([5, 0, 0], dtype=np.int64), 'starts'),
153
+ onh.from_array(np.array([6, 3, 3], dtype=np.int64), 'ends'),
154
+ onh.from_array(np.array([1, 2, 3], dtype=np.int64), 'axes'),
155
+ onh.from_array(np.array([1, 1, 1], dtype=np.int64), 'steps'),
156
+ onh.from_array(np.array([1], dtype=np.int64), 'squeeze_axes'),
157
+ onh.from_array(np.array([2, 4, 3], dtype=np.int64), 'palette'),
158
+ onh.from_array(np.array([2], dtype=np.int64), 'unsqueeze_axes'),
159
+ onh.from_array(np.array([1, 3, 3], dtype=np.int64), 'tile_shape'),
160
+ onh.from_array(np.array([0, 0, 0, 0, 27, 27], dtype=np.int64), 'pads'),
161
+ onh.from_array(np.array(10, dtype=np.int64), 'pad_value'),
162
+ onh.from_array(np.array(10, dtype=np.int64), 'depth'),
163
+ onh.from_array(np.array([0.0, 1.0], dtype=np.float32), 'onehot_values'),
164
+ ]
165
+
166
+ nodes = [
167
+ oh.make_node('Slice', ['input', 'starts', 'ends', 'axes', 'steps'], ['marker']),
168
+ oh.make_node('ArgMax', ['marker'], ['col_with_5'], axis=3, keepdims=0),
169
+ oh.make_node('Squeeze', ['col_with_5', 'squeeze_axes'], ['row_col']),
170
+ oh.make_node('Gather', ['palette', 'row_col'], ['row_labels'], axis=0),
171
+ oh.make_node('Unsqueeze', ['row_labels', 'unsqueeze_axes'], ['row_labels_col']),
172
+ oh.make_node('Expand', ['row_labels_col', 'tile_shape'], ['labels_3x3']),
173
+ oh.make_node('Pad', ['labels_3x3', 'pads', 'pad_value'], ['labels_30x30']),
174
+ oh.make_node('OneHot', ['labels_30x30', 'depth', 'onehot_values'], ['output'], axis=1),
175
+ ]
176
+
177
+ x = oh.make_tensor_value_info('input', TensorProto.FLOAT, [1, 10, 30, 30])
178
+ y = oh.make_tensor_value_info('output', TensorProto.FLOAT, [1, 10, 30, 30])
179
+ g = oh.make_graph(nodes, 'g', [x], [y], initializer=inits)
180
+ return oh.make_model(g, ir_version=8, opset_imports=[oh.make_opsetid('', 16)])
181
+
182
+
183
+ def s_count_dominant_bar(td):
184
+ """Task 339: Count non-bg pixels in 3x3 grid, find dominant color,
185
+ output 1x9 bar filled with that color for 'count' cells.
186
+
187
+ Architecture: Slice(ch1-9,3x3) β†’ ReduceSum(all) β†’ cast β†’ sub 1 β†’
188
+ ReduceSum(spatial) β†’ ArgMax(ch) β†’ Add 1 β†’
189
+ OneHot β†’ Unsqueeze β†’ Gather(mask_bank) β†’ Mul β†’ Pad
190
+ """
191
+ exs = get_exs(td)
192
+ if len(exs) < 2:
193
+ return None
194
+ # Arc-gen has variable sizes β€” do NOT use fixed_shapes().
195
+ for inp, out in exs[:3]:
196
+ if inp.shape != (3, 3) or out.shape != (1, 9):
197
+ return None
198
+ mask_bank = np.zeros((9, 1, 1, 9), dtype=np.float32)
199
+ for i in range(9):
200
+ mask_bank[i, 0, 0, :i+1] = 1.0
201
+
202
+ inits = [
203
+ onh.from_array(np.array([1, 0, 0], dtype=np.int64), 'starts'),
204
+ onh.from_array(np.array([10, 3, 3], dtype=np.int64), 'ends'),
205
+ onh.from_array(np.array([1, 2, 3], dtype=np.int64), 'axes'),
206
+ onh.from_array(np.array([1, 1, 1], dtype=np.int64), 'steps'),
207
+ onh.from_array(np.array([1], dtype=np.int64), 'one'),
208
+ onh.from_array(np.array([2, 3], dtype=np.int64), 'spatial_axes'),
209
+ onh.from_array(np.array(10, dtype=np.int64), 'depth'),
210
+ onh.from_array(np.array([0.0, 1.0], dtype=np.float32), 'hot_values'),
211
+ onh.from_array(mask_bank, 'mask_bank'),
212
+ onh.from_array(np.array([0, 0, 0, 0, 0, 0, 29, 21], dtype=np.int64), 'pads'),
213
+ onh.from_array(np.float32(0.0), 'pad_value'),
214
+ ]
215
+
216
+ nodes = [
217
+ oh.make_node('Slice', ['input', 'starts', 'ends', 'axes', 'steps'], ['colored']),
218
+ oh.make_node('ReduceSum', ['colored', 'axes'], ['count_float'], keepdims=0),
219
+ oh.make_node('Cast', ['count_float'], ['count_i64'], to=TensorProto.INT64),
220
+ oh.make_node('Sub', ['count_i64', 'one'], ['count_index']),
221
+ oh.make_node('ReduceSum', ['colored', 'spatial_axes'], ['color_counts'], keepdims=0),
222
+ oh.make_node('ArgMax', ['color_counts'], ['color_index0'], axis=1, keepdims=0),
223
+ oh.make_node('Add', ['color_index0', 'one'], ['color_index']),
224
+ oh.make_node('OneHot', ['color_index', 'depth', 'hot_values'], ['color_hot_2d'], axis=1),
225
+ oh.make_node('Unsqueeze', ['color_hot_2d', 'spatial_axes'], ['color_hot']),
226
+ oh.make_node('Gather', ['mask_bank', 'count_index'], ['count_mask'], axis=0),
227
+ oh.make_node('Mul', ['color_hot', 'count_mask'], ['small_output']),
228
+ oh.make_node('Pad', ['small_output', 'pads', 'pad_value'], ['output']),
229
+ ]
230
+
231
+ x = oh.make_tensor_value_info('input', TensorProto.FLOAT, [1, 10, 30, 30])
232
+ y = oh.make_tensor_value_info('output', TensorProto.FLOAT, [1, 10, 30, 30])
233
+ g = oh.make_graph(nodes, 'g', [x], [y], initializer=inits)
234
+ return oh.make_model(g, ir_version=8, opset_imports=[oh.make_opsetid('', 16)])
235
+
236
+
237
+ def s_maxpool_3x3_downsample(td):
238
+ """Task 130: Crop 9x9, MaxPool 3x3 stride 3 β†’ 3x3 output.
239
+ Skip channels 0 and 5 for the pool, reconstruct bg from absence.
240
+
241
+ Architecture: Gather(rows 0-8) β†’ Gather(cols 0-8) β†’ MaxPool(3x3,stride=3) β†’
242
+ Gather(channels 1-4,6-9) β†’ ReduceMax(ch) β†’ Sub(1,max) β†’
243
+ Conv1x1(identity remap) β†’ Mul(ch0 mask) β†’ Add β†’ Pad
244
+ """
245
+ exs = get_exs(td)
246
+ if len(exs) < 2:
247
+ return None
248
+ sp = fixed_shapes(td)
249
+ if sp is None:
250
+ return None
251
+ (IH, IW), (OH, OW) = sp
252
+ if IH != 9 or IW != 9 or OH != 3 or OW != 3:
253
+ return None
254
+
255
+ v4 = np.zeros((10, 8, 1, 1), dtype=np.float32)
256
+ orig_colors = [1, 2, 3, 4, 6, 7, 8, 9]
257
+ for in_ch, out_color in enumerate(orig_colors):
258
+ v4[out_color, in_ch, 0, 0] = 1.0
259
+
260
+ v2 = np.zeros((1, 10, 1, 1), dtype=np.float32)
261
+ v2[0, 0, 0, 0] = 1.0
262
+
263
+ inits = [
264
+ onh.from_array(np.arange(9, dtype=np.int64), 'v0'),
265
+ onh.from_array(np.array([1, 2, 3, 4, 6, 7, 8, 9], dtype=np.int64), 'v1'),
266
+ onh.from_array(v2, 'v2'),
267
+ onh.from_array(np.array([1.0], dtype=np.float32), 'v3'),
268
+ onh.from_array(v4, 'v4'),
269
+ ]
270
+
271
+ nodes = [
272
+ oh.make_node('Gather', ['input', 'v0'], ['v5'], axis=2),
273
+ oh.make_node('Gather', ['v5', 'v0'], ['v6'], axis=3),
274
+ oh.make_node('MaxPool', ['v6'], ['v7'], kernel_shape=[3, 3], strides=[3, 3]),
275
+ oh.make_node('Gather', ['v7', 'v1'], ['v8'], axis=1),
276
+ oh.make_node('ReduceMax', ['v8'], ['v9'], axes=[1], keepdims=1),
277
+ oh.make_node('Sub', ['v3', 'v9'], ['v10']),
278
+ oh.make_node('Conv', ['v8', 'v4'], ['v11'], kernel_shape=[1, 1], pads=[0, 0, 0, 0]),
279
+ oh.make_node('Mul', ['v10', 'v2'], ['v12']),
280
+ oh.make_node('Add', ['v11', 'v12'], ['v13']),
281
+ oh.make_node('Pad', ['v13'], ['output'], pads=[0, 0, 0, 0, 0, 0, 27, 27], value=0.0),
282
+ ]
283
+
284
+ x = oh.make_tensor_value_info('input', TensorProto.FLOAT, [1, 10, 30, 30])
285
+ y = oh.make_tensor_value_info('output', TensorProto.FLOAT, [1, 10, 30, 30])
286
+ g = oh.make_graph(nodes, 'g', [x], [y], initializer=inits)
287
+ return oh.make_model(g, ir_version=8, opset_imports=[oh.make_opsetid('', 10)])
288
+
289
+
290
+ def s_tile_mask_color2(td):
291
+ """Task 315: Tile 3x3 grid β†’ 9x9, masked by where color 2 appears.
292
+
293
+ Architecture: Slice(3x3) β†’ Tile(3x3 spatially) β†’ extract ch2 β†’ Gather(3x upscale) β†’
294
+ Slice(ch1-9) β†’ Mul(mask) β†’ ReduceMax β†’ Sub(1,max) β†’ Concat(bg,colored) β†’ Pad
295
+ """
296
+ exs = get_exs(td)
297
+ if len(exs) < 2:
298
+ return None
299
+ sp = fixed_shapes(td)
300
+ if sp is None:
301
+ return None
302
+ (IH, IW), (OH, OW) = sp
303
+ if IH != 3 or IW != 3 or OH != 9 or OW != 9:
304
+ return None
305
+
306
+ inits = [
307
+ onh.from_array(np.ones((1, 1, 9, 9), dtype=np.float32), 'ones_9'),
308
+ onh.from_array(np.array([1], dtype=np.int64), 'ax1'),
309
+ onh.from_array(np.array([2, 3], dtype=np.int64), 'ax23'),
310
+ onh.from_array(np.array([0, 0], dtype=np.int64), 's00'),
311
+ onh.from_array(np.array([3, 3], dtype=np.int64), 's33'),
312
+ onh.from_array(np.array([2], dtype=np.int64), 's2'),
313
+ onh.from_array(np.array([3], dtype=np.int64), 's3'),
314
+ onh.from_array(np.array([10], dtype=np.int64), 's10'),
315
+ onh.from_array(np.array([1, 1, 3, 3], dtype=np.int64), 'tile33'),
316
+ onh.from_array(np.array([0, 0, 0, 1, 1, 1, 2, 2, 2], dtype=np.int64), 'up3'),
317
+ ]
318
+
319
+ nodes = [
320
+ oh.make_node('Slice', ['input', 's00', 's33', 'ax23'], ['inp3']),
321
+ oh.make_node('Tile', ['inp3', 'tile33'], ['tiled9']),
322
+ oh.make_node('Slice', ['inp3', 's2', 's3', 'ax1'], ['c2_3']),
323
+ oh.make_node('Gather', ['c2_3', 'up3'], ['c2_up_h'], axis=2),
324
+ oh.make_node('Gather', ['c2_up_h', 'up3'], ['mask9'], axis=3),
325
+ oh.make_node('Slice', ['tiled9', 'ax1', 's10', 'ax1'], ['tiled_c1_9']),
326
+ oh.make_node('Mul', ['tiled_c1_9', 'mask9'], ['colored_c1_9']),
327
+ oh.make_node('ReduceMax', ['colored_c1_9'], ['col_pres'], axes=[1], keepdims=1),
328
+ oh.make_node('Sub', ['ones_9', 'col_pres'], ['c0_9']),
329
+ oh.make_node('Concat', ['c0_9', 'colored_c1_9'], ['out_9'], axis=1),
330
+ oh.make_node('Pad', ['out_9'], ['output'], pads=[0, 0, 0, 0, 0, 0, 21, 21], value=0.0),
331
+ ]
332
+
333
+ x = oh.make_tensor_value_info('input', TensorProto.FLOAT, [1, 10, 30, 30])
334
+ y = oh.make_tensor_value_info('output', TensorProto.FLOAT, [1, 10, 30, 30])
335
+ g = oh.make_graph(nodes, 'g', [x], [y], initializer=inits)
336
+ return oh.make_model(g, ir_version=8, opset_imports=[oh.make_opsetid('', 10)])
337
+
338
+
339
+ def s_matmul_projection(td):
340
+ """Task 296: Project flattened 5x7 non-bg channels through a fixed matrix to get 3x3 output.
341
+
342
+ Architecture: Slice(ch1-9,5x7) β†’ Reshape(9x35) β†’ MatMul(35x9) β†’ Greater(0) β†’
343
+ Reshape(9x3x3) β†’ Cast β†’ ReduceSum(ch) β†’ Sub(1,sum) β†’ Concat(bg,colored) β†’ Pad
344
+ """
345
+ exs = get_exs(td)
346
+ if len(exs) < 2:
347
+ return None
348
+ sp = fixed_shapes(td)
349
+ if sp is None:
350
+ return None
351
+ (IH, IW), (OH, OW) = sp
352
+ if IH != 5 or IW != 7 or OH != 3 or OW != 3:
353
+ return None
354
+
355
+ projection = np.array([
356
+ [1,0,0,0,0,0,0,0,0], [0,1,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0],
357
+ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,1,0,0,0,0,0,0,0],
358
+ [0,0,1,0,0,0,0,0,0], [0,0,0,1,0,0,0,0,0], [0,0,0,0,1,0,0,0,0],
359
+ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0],
360
+ [0,0,0,0,1,0,0,0,0], [0,0,0,0,0,1,0,0,0], [0,0,0,0,0,0,0,0,0],
361
+ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0],
362
+ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0],
363
+ [0,0,0,1,0,0,0,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,0,0,0,0,0,0],
364
+ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,1,0,0,0,0],
365
+ [0,0,0,0,0,1,0,0,0], [0,0,0,0,0,0,1,0,0], [0,0,0,0,0,0,0,1,0],
366
+ [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0],
367
+ [0,0,0,0,0,0,0,1,0], [0,0,0,0,0,0,0,0,1],
368
+ ], dtype=np.float32)
369
+
370
+ inits = [
371
+ onh.from_array(np.array([1, 0, 0], dtype=np.int64), 'starts'),
372
+ onh.from_array(np.array([10, 5, 7], dtype=np.int64), 'ends'),
373
+ onh.from_array(np.array([1, 2, 3], dtype=np.int64), 'axes'),
374
+ onh.from_array(np.array([1, 1, 1], dtype=np.int64), 'steps'),
375
+ onh.from_array(np.array([1, 9, 35], dtype=np.int64), 'flat_shape'),
376
+ onh.from_array(projection, 'projection'),
377
+ onh.from_array(np.float32(0.0), 'zero'),
378
+ onh.from_array(np.array([1, 9, 3, 3], dtype=np.int64), 'color_shape'),
379
+ onh.from_array(np.array([1], dtype=np.int64), 'channel_axis'),
380
+ onh.from_array(np.ones((1, 1, 3, 3), dtype=np.float32), 'ones3'),
381
+ onh.from_array(np.array([0, 0, 0, 0, 0, 0, 27, 27], dtype=np.int64), 'pads'),
382
+ onh.from_array(np.float32(0.0), 'pad_value'),
383
+ ]
384
+
385
+ nodes = [
386
+ oh.make_node('Slice', ['input', 'starts', 'ends', 'axes', 'steps'], ['patch']),
387
+ oh.make_node('Reshape', ['patch', 'flat_shape'], ['flat']),
388
+ oh.make_node('MatMul', ['flat', 'projection'], ['folded_counts']),
389
+ oh.make_node('Greater', ['folded_counts', 'zero'], ['folded_bool']),
390
+ oh.make_node('Reshape', ['folded_bool', 'color_shape'], ['color_output_pre_bool']),
391
+ oh.make_node('Cast', ['color_output_pre_bool'], ['color_output'], to=TensorProto.FLOAT),
392
+ oh.make_node('ReduceSum', ['color_output', 'channel_axis'], ['occupancy'], keepdims=1),
393
+ oh.make_node('Sub', ['ones3', 'occupancy'], ['zero_channel']),
394
+ oh.make_node('Concat', ['zero_channel', 'color_output'], ['small_output'], axis=1),
395
+ oh.make_node('Pad', ['small_output', 'pads', 'pad_value'], ['output']),
396
+ ]
397
+
398
+ x = oh.make_tensor_value_info('input', TensorProto.FLOAT, [1, 10, 30, 30])
399
+ y = oh.make_tensor_value_info('output', TensorProto.FLOAT, [1, 10, 30, 30])
400
+ g = oh.make_graph(nodes, 'g', [x], [y], initializer=inits)
401
+ return oh.make_model(g, ir_version=8, opset_imports=[oh.make_opsetid('', 16)])
402
+
403
+
404
+ def s_count_colors_pattern_bank(td):
405
+ """Task 61: Count how many of colors 4-9 are present β†’ lookup pattern from bank.
406
+ Input: 18x18 grid. Count how many distinct colors from channels 4-9 are present (1-6).
407
+ Output: 18x18 modular multiplication table pattern indexed by (count-1).
408
+
409
+ Architecture: Slice(ch4-9,18x18) β†’ ReduceSum(spatial) β†’ Greater(0) β†’ Cast β†’
410
+ ReduceSum(ch) β†’ Sub(1) β†’ Gather(label_bank) β†’ OneHot β†’ Pad
411
+ """
412
+ exs = get_exs(td)
413
+ if len(exs) < 2:
414
+ return None
415
+ sp = fixed_shapes(td)
416
+ if sp is None:
417
+ return None
418
+ (IH, IW), (OH, OW) = sp
419
+ if IH != 18 or IW != 18 or OH != 18 or OW != 18:
420
+ return None
421
+
422
+ # bank[n]: pattern with period (n+4), formula: (i*j) % period + 1
423
+ label_bank = np.zeros((6, 18, 18), dtype=np.int64)
424
+ for n in range(6):
425
+ period = n + 4
426
+ for i in range(18):
427
+ for j in range(18):
428
+ label_bank[n, i, j] = (i * j) % period + 1
429
+
430
+ inits = [
431
+ onh.from_array(np.array([4, 0, 0], dtype=np.int64), 'starts'),
432
+ onh.from_array(np.array([10, 18, 18], dtype=np.int64), 'ends'),
433
+ onh.from_array(np.array([1, 2, 3], dtype=np.int64), 'axes'),
434
+ onh.from_array(np.array([1, 1, 1], dtype=np.int64), 'steps'),
435
+ onh.from_array(np.array([2, 3], dtype=np.int64), 'spatial_axes'),
436
+ onh.from_array(np.float32(0.0), 'zero'),
437
+ onh.from_array(np.array([1], dtype=np.int64), 'color_axis'),
438
+ onh.from_array(label_bank, 'label_bank'),
439
+ onh.from_array(np.array(10, dtype=np.int64), 'depth'),
440
+ onh.from_array(np.array([0.0, 1.0], dtype=np.float32), 'onehot_values'),
441
+ onh.from_array(np.array([0, 0, 0, 0, 0, 0, 12, 12], dtype=np.int64), 'pads'),
442
+ onh.from_array(np.float32(0.0), 'pad_value'),
443
+ ]
444
+
445
+ nodes = [
446
+ oh.make_node('Slice', ['input', 'starts', 'ends', 'axes', 'steps'], ['colored']),
447
+ oh.make_node('ReduceSum', ['colored', 'spatial_axes'], ['color_counts'], keepdims=0),
448
+ oh.make_node('Greater', ['color_counts', 'zero'], ['present_bool']),
449
+ oh.make_node('Cast', ['present_bool'], ['present_f'], to=TensorProto.FLOAT),
450
+ oh.make_node('ReduceSum', ['present_f', 'color_axis'], ['n_colors_f'], keepdims=0),
451
+ oh.make_node('Cast', ['n_colors_f'], ['n_colors_i'], to=TensorProto.INT64),
452
+ oh.make_node('Sub', ['n_colors_i', 'color_axis'], ['bank_idx']),
453
+ oh.make_node('Gather', ['label_bank', 'bank_idx'], ['labels_2d'], axis=0),
454
+ oh.make_node('OneHot', ['labels_2d', 'depth', 'onehot_values'], ['small_output'], axis=1),
455
+ oh.make_node('Pad', ['small_output', 'pads', 'pad_value'], ['output']),
456
+ ]
457
+
458
+ x = oh.make_tensor_value_info('input', TensorProto.FLOAT, [1, 10, 30, 30])
459
+ y = oh.make_tensor_value_info('output', TensorProto.FLOAT, [1, 10, 30, 30])
460
+ g = oh.make_graph(nodes, 'g', [x], [y], initializer=inits)
461
+ return oh.make_model(g, ir_version=8, opset_imports=[oh.make_opsetid('', 16)])