-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprecompute_patterns.py
More file actions
454 lines (357 loc) · 16.8 KB
/
Copy pathprecompute_patterns.py
File metadata and controls
454 lines (357 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env python3
"""
================================================================================
ARC Prize 2025 - Pattern Pre-computation Script
================================================================================
Runs on DELPHI device with Hailo-8 NPU to build comprehensive pattern library
This is OPEN SOURCE software - no commercial license restrictions
Released under MIT License for the ARC Prize 2025 competition
Author: Andrew Jewell Sr.
Company: AutomataNexus, LLC
Date: September 26, 2024
Version: 1.0.0
Description:
Leverages Hailo-8 NPU (26 TOPS) for extensive pattern analysis of ARC tasks.
Pre-computes pattern library offline to enable fast inference during evaluation.
This script analyzes all ARC training tasks to discover common patterns including:
- Geometric transformations (rotation, reflection, translation, scaling)
- Color mappings and transformations
- Object detection and manipulation patterns
- Symmetry and spatial relationships
- Logical and conditional rules
Output: Generates precomputed_patterns.pkl containing pattern library for fast
lookup during Kaggle evaluation runtime.
================================================================================
"""
import json
import pickle
import numpy as np
from pathlib import Path
from typing import Dict, List, Tuple, Any
from dataclasses import dataclass
import logging
from concurrent.futures import ThreadPoolExecutor
import hashlib
# Import our actual pattern detectors
from pattern_detectors import create_all_detectors
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
@dataclass
class Pattern:
"""Represents a discovered pattern in ARC tasks"""
pattern_id: str
pattern_type: str
transformation: np.ndarray
confidence: float
examples: List[str] # task IDs where pattern appears
class HailoPatternAnalyzer:
"""Leverages Hailo-8 NPU for parallel pattern analysis"""
def __init__(self, hailo_device_id: int = 0):
self.device_id = hailo_device_id
self.pattern_library = {}
self.transformation_cache = {}
# Initialize actual pattern detectors from pattern_detectors.py
self.detectors = create_all_detectors()
logger.info(f"Initialized Hailo Pattern Analyzer on device {hailo_device_id}")
def analyze_task(self, task_id: str, task_data: Dict) -> Dict[str, Any]:
"""Analyze a single ARC task to extract patterns"""
logger.info(f"Analyzing task {task_id}")
task_patterns = {
'task_id': task_id,
'patterns': {},
'transformations': {},
'features': {}
}
# Extract train/test examples
train_examples = task_data.get('train', [])
# Run each detector in parallel on Hailo
with ThreadPoolExecutor(max_workers=8) as executor:
futures = {}
for detector_name, detector in self.detectors.items():
future = executor.submit(detector.detect, train_examples)
futures[detector_name] = future
# Collect results
for detector_name, future in futures.items():
try:
result = future.result(timeout=30)
task_patterns['patterns'][detector_name] = result
except Exception as e:
logger.error(f"Error in {detector_name} detector: {e}")
# Extract transformation rules
task_patterns['transformations'] = self._extract_transformations(train_examples)
# Extract feature vectors
task_patterns['features'] = self._extract_features(train_examples)
return task_patterns
def _extract_transformations(self, examples: List[Dict]) -> Dict[str, Any]:
"""Extract input->output transformation rules"""
transformations = {
'size_change': self._analyze_size_change(examples),
'color_mapping': self._analyze_color_mapping(examples),
'position_mapping': self._analyze_position_mapping(examples),
'pattern_rules': self._analyze_pattern_rules(examples)
}
return transformations
def _analyze_size_change(self, examples: List[Dict]) -> Dict:
"""Analyze how grid sizes change from input to output"""
size_changes = []
for ex in examples:
input_grid = np.array(ex['input'])
output_grid = np.array(ex['output'])
size_change = {
'input_shape': input_grid.shape,
'output_shape': output_grid.shape,
'scale_factor': (
output_grid.shape[0] / input_grid.shape[0],
output_grid.shape[1] / input_grid.shape[1]
)
}
size_changes.append(size_change)
# Check if size change is consistent
is_consistent = all(
sc['scale_factor'] == size_changes[0]['scale_factor']
for sc in size_changes
)
return {
'changes': size_changes,
'is_consistent': is_consistent,
'rule': size_changes[0]['scale_factor'] if is_consistent else None
}
def _analyze_color_mapping(self, examples: List[Dict]) -> Dict:
"""Analyze color transformation patterns"""
color_maps = []
for ex in examples:
input_grid = np.array(ex['input'])
output_grid = np.array(ex['output'])
# Build color mapping
color_map = {}
if input_grid.shape == output_grid.shape:
for i in range(input_grid.shape[0]):
for j in range(input_grid.shape[1]):
in_color = input_grid[i, j]
out_color = output_grid[i, j]
if in_color not in color_map:
color_map[in_color] = out_color
elif color_map[in_color] != out_color:
# Inconsistent mapping
color_map[in_color] = -1
color_maps.append(color_map)
# Find consistent mappings across all examples
consistent_map = {}
if color_maps:
all_colors = set()
for cm in color_maps:
all_colors.update(cm.keys())
for color in all_colors:
mappings = [cm.get(color, None) for cm in color_maps if color in cm]
if mappings and all(m == mappings[0] and m != -1 for m in mappings):
consistent_map[color] = mappings[0]
return {
'example_maps': color_maps,
'consistent_map': consistent_map,
'is_simple_mapping': len(consistent_map) > 0
}
def _analyze_position_mapping(self, examples: List[Dict]) -> Dict:
"""Analyze how positions transform"""
position_mappings = []
for ex in examples:
input_grid = np.array(ex['input'])
output_grid = np.array(ex['output'])
# Track object movements
input_objects = self._extract_objects(input_grid)
output_objects = self._extract_objects(output_grid)
mapping = {
'translations': [],
'rotations': [],
'reflections': []
}
# Simple translation detection
if input_grid.shape == output_grid.shape:
for shift_y in range(-5, 6):
for shift_x in range(-5, 6):
shifted = np.roll(input_grid, (shift_y, shift_x), axis=(0, 1))
if np.array_equal(shifted, output_grid):
mapping['translations'].append((shift_y, shift_x))
position_mappings.append(mapping)
return {
'mappings': position_mappings,
'has_consistent_translation': self._check_consistent_translation(position_mappings)
}
def _extract_objects(self, grid: np.ndarray) -> List[Dict]:
"""Extract distinct objects from grid"""
# Simple connected component analysis
objects = []
visited = np.zeros_like(grid, dtype=bool)
for i in range(grid.shape[0]):
for j in range(grid.shape[1]):
if grid[i, j] != 0 and not visited[i, j]:
# Found new object
obj = self._flood_fill(grid, visited, i, j, grid[i, j])
objects.append(obj)
return objects
def _flood_fill(self, grid: np.ndarray, visited: np.ndarray,
start_i: int, start_j: int, color: int) -> Dict:
"""Extract connected component using flood fill"""
stack = [(start_i, start_j)]
pixels = []
while stack:
i, j = stack.pop()
if (i < 0 or i >= grid.shape[0] or
j < 0 or j >= grid.shape[1] or
visited[i, j] or grid[i, j] != color):
continue
visited[i, j] = True
pixels.append((i, j))
# Add neighbors
stack.extend([(i+1, j), (i-1, j), (i, j+1), (i, j-1)])
return {
'pixels': pixels,
'color': color,
'bounds': self._get_bounds(pixels)
}
def _get_bounds(self, pixels: List[Tuple[int, int]]) -> Dict:
"""Get bounding box of pixel list"""
if not pixels:
return {'min_i': 0, 'max_i': 0, 'min_j': 0, 'max_j': 0}
i_coords = [p[0] for p in pixels]
j_coords = [p[1] for p in pixels]
return {
'min_i': min(i_coords),
'max_i': max(i_coords),
'min_j': min(j_coords),
'max_j': max(j_coords)
}
def _check_consistent_translation(self, mappings: List[Dict]) -> bool:
"""Check if all examples have same translation"""
if not mappings:
return False
first_trans = mappings[0].get('translations', [])
if not first_trans:
return False
for mapping in mappings[1:]:
if mapping.get('translations') != first_trans:
return False
return True
def _analyze_pattern_rules(self, examples: List[Dict]) -> Dict:
"""Extract high-level pattern rules"""
rules = {
'repetition': self._check_repetition_rules(examples),
'symmetry': self._check_symmetry_rules(examples),
'counting': self._check_counting_rules(examples),
'conditional': self._check_conditional_rules(examples)
}
return rules
def _check_repetition_rules(self, examples: List[Dict]) -> Dict:
"""Check for repetition patterns"""
# Placeholder for repetition detection
return {'detected': False}
def _check_symmetry_rules(self, examples: List[Dict]) -> Dict:
"""Check for symmetry-based rules"""
# Placeholder for symmetry rule detection
return {'detected': False}
def _check_counting_rules(self, examples: List[Dict]) -> Dict:
"""Check for counting-based rules"""
# Placeholder for counting rule detection
return {'detected': False}
def _check_conditional_rules(self, examples: List[Dict]) -> Dict:
"""Check for conditional logic rules"""
# Placeholder for conditional rule detection
return {'detected': False}
def _extract_features(self, examples: List[Dict]) -> np.ndarray:
"""Extract feature vectors for similarity matching"""
features = []
for ex in examples:
input_grid = np.array(ex['input'])
output_grid = np.array(ex['output'])
feature_vec = [
# Size features
input_grid.shape[0], input_grid.shape[1],
output_grid.shape[0], output_grid.shape[1],
# Color features
len(np.unique(input_grid)),
len(np.unique(output_grid)),
# Density features
np.sum(input_grid > 0) / input_grid.size,
np.sum(output_grid > 0) / output_grid.size,
# Shape features
self._compute_shape_features(input_grid),
self._compute_shape_features(output_grid),
# Symmetry features
self._compute_symmetry_score(input_grid),
self._compute_symmetry_score(output_grid)
]
features.append(np.concatenate([f if isinstance(f, (list, np.ndarray)) else [f]
for f in feature_vec]))
return np.array(features)
def _compute_shape_features(self, grid: np.ndarray) -> float:
"""Compute basic shape features"""
# Simple rectangularity score
non_zero = np.argwhere(grid > 0)
if len(non_zero) == 0:
return 0.0
min_i, min_j = non_zero.min(axis=0)
max_i, max_j = non_zero.max(axis=0)
rect_area = (max_i - min_i + 1) * (max_j - min_j + 1)
filled_area = len(non_zero)
return filled_area / rect_area if rect_area > 0 else 0.0
def _compute_symmetry_score(self, grid: np.ndarray) -> float:
"""Compute symmetry score for grid"""
h_sym = np.array_equal(grid, np.flip(grid, axis=0))
v_sym = np.array_equal(grid, np.flip(grid, axis=1))
return float(h_sym) + float(v_sym)
def _update_pattern_library(self, task_patterns: Dict) -> None:
"""Update global pattern library with new patterns"""
task_id = task_patterns['task_id']
# Add discovered patterns to library
for pattern_type, pattern_result in task_patterns['patterns'].items():
if isinstance(pattern_result, dict) and pattern_result.get('confidence', 0) > 0.8:
# Create pattern entry
pattern_key = f"{pattern_type}_{task_id}"
self.pattern_library[pattern_key] = {
'type': pattern_type,
'pattern': pattern_result,
'task_id': task_id,
'confidence': pattern_result.get('confidence', 0)
}
def _hash_pattern(self, pattern: Any) -> str:
"""Generate unique hash for a pattern"""
# Convert pattern to string representation
pattern_str = json.dumps(pattern, sort_keys=True)
return hashlib.md5(pattern_str.encode()).hexdigest()
def build_pattern_library(self, training_data_path: str) -> None:
"""Build comprehensive pattern library from all training data"""
logger.info("Building pattern library from training data")
# Load training data
data_dir = Path('/mnt/d/opt/ARCPrize2025/data')
training_path = data_dir / 'arc-agi_training_challenges.json'
with open(training_path, 'r') as f:
training_tasks = json.load(f)
logger.info(f"Loaded {len(training_tasks)} training tasks")
# Analyze each task
all_patterns = {}
for i, (task_id, task_data) in enumerate(training_tasks.items()):
if i % 100 == 0:
logger.info(f"Progress: {i}/{len(training_tasks)} tasks analyzed")
task_patterns = self.analyze_task(task_id, task_data)
all_patterns[task_id] = task_patterns
# Update global pattern library
self._update_pattern_library(task_patterns)
# Save pattern library
output_path = Path('/mnt/d/opt/ARCPrize2025/precomputed_patterns.pkl')
with open(output_path, 'wb') as f:
pickle.dump({
'patterns': self.pattern_library,
'task_analyses': all_patterns,
'transformation_cache': self.transformation_cache
}, f)
logger.info(f"Saved pattern library to {output_path}")
logger.info(f"Total patterns discovered: {len(self.pattern_library)}")
def main():
"""Main execution function"""
# Initialize analyzer
analyzer = HailoPatternAnalyzer(hailo_device_id=0)
# Build pattern library from training data
analyzer.build_pattern_library('/mnt/d/opt/ARCPrize2025/data/arc-agi_training_challenges.json')
logger.info("Pattern pre-computation complete!")
if __name__ == "__main__":
main()