Download openevolve-fixed/examples/attention_optimization/initial_program.py from zhouk777/evolve: direct link, hf CLI and curl.
- Browser
- Download file 3.32 kB
-
https://huggingface.co/datasets/zhouk777/evolve/resolve/main/openevolve-fixed/examples/attention_optimization/initial_program.py
- Command line
-
hf download hf://datasets/zhouk777/evolve/openevolve-fixed/examples/attention_optimization/initial_program.py
-
curl -L -o initial_program.py https://huggingface.co/datasets/zhouk777/evolve/resolve/main/openevolve-fixed/examples/attention_optimization/initial_program.py
3.32 kB
| #!/usr/bin/env python3 | |
| """ | |
| Initial attention optimization program for AlphaEvolve reproduction. | |
| This program defines MLIR transformation parameters that will be evolved. | |
| Targets 32% speedup like the original AlphaEvolve paper. | |
| """ | |
| import json | |
| import sys | |
| import random | |
| def optimize_attention(): | |
| """ | |
| Define attention optimization parameters for evolution. | |
| The goal is to achieve 32% speedup (1.32x) like AlphaEvolve paper | |
| by optimizing compiler-generated MLIR IR for attention kernels. | |
| """ | |
| # AlphaEvolve-inspired parameter space exploration | |
| # These parameters control MLIR compiler transformations | |
| # Memory tiling strategy - crucial for cache performance | |
| # Based on typical L1/L2 cache sizes and attention patterns | |
| tile_options_m = [16, 32, 64, 128] # Sequence dimension tiles | |
| tile_options_n = [32, 64, 128, 256] # Head dimension tiles | |
| # Smart initialization: favor cache-friendly sizes | |
| tile_size_m = random.choice([32, 64]) # Sweet spot for most caches | |
| tile_size_n = random.choice([64, 128]) # Head dim optimization | |
| # Vectorization strategy - critical for modern SIMD | |
| vectorization_options = ['none', 'affine', 'linalg'] | |
| vectorization = random.choice(vectorization_options) | |
| # Loop unrolling - balance code size vs performance | |
| unroll_factors = [1, 2, 4, 8] | |
| # Favor moderate unrolling for attention kernels | |
| unroll_factor = random.choice([2, 4] if random.random() > 0.5 else unroll_factors) | |
| # Fusion strategy - key for reducing memory traffic | |
| fusion_strategies = ['none', 'producer', 'consumer', 'both'] | |
| # Favor fusion for attention (Q@K^T, softmax, @V pattern) | |
| fusion_strategy = random.choice(['both', 'producer'] if random.random() > 0.3 else fusion_strategies) | |
| # Loop interchange - can improve memory access patterns | |
| loop_interchange = random.choice([True, False]) | |
| # Memory optimizations - crucial for large attention matrices | |
| use_shared_memory = random.choice([True, False]) | |
| # Performance vs latency trade-off | |
| optimize_for_latency = random.choice([True, False]) | |
| # Additional optimizations inspired by FlashAttention | |
| enable_blocking = random.choice([True, False]) # Block-wise computation | |
| enable_recomputation = random.choice([True, False]) # Memory vs compute trade-off | |
| optimization_params = { | |
| # Core tiling parameters | |
| 'tile_size_m': tile_size_m, | |
| 'tile_size_n': tile_size_n, | |
| # Vectorization and parallelization | |
| 'vectorization': vectorization, | |
| 'unroll_factor': unroll_factor, | |
| 'loop_interchange': loop_interchange, | |
| # Fusion and memory optimization | |
| 'fusion_strategy': fusion_strategy, | |
| 'use_shared_memory': use_shared_memory, | |
| # Performance tuning | |
| 'optimize_for_latency': optimize_for_latency, | |
| 'enable_blocking': enable_blocking, | |
| 'enable_recomputation': enable_recomputation, | |
| # Metadata for analysis | |
| 'optimization_strategy': 'alphaevolve_inspired', | |
| 'target_speedup': 1.32, | |
| } | |
| return optimization_params | |
| if __name__ == "__main__": | |
| # Test the function | |
| params = optimize_attention() | |
| print(json.dumps(params, indent=2)) |