File size: 3,705 Bytes
ea8c728
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# MLX Metal Kernel Optimization Example

This example uses OpenEvolve to automatically discover optimized Metal GPU kernels for Grouped Query Attention (GQA) in Qwen3-0.6B on Apple Silicon.

## Target Configuration

- **Model**: Qwen3-0.6B-bf16
- **Architecture**: 16 query heads : 8 KV heads (2:1 ratio), 2048 hidden size, 128 head dimension
- **Hardware**: Apple M-series GPUs with unified memory
- **Baseline**: `mx.fast.scaled_dot_product_attention` via `mlx_lm.generate`
- **Goal**: Evolve custom Metal kernel source code to outperform baseline

## Quick Start

### Prerequisites

```bash
pip install mlx mlx-lm openevolve

# Set API key (Gemini via OpenAI-compatible endpoint)
export OPENAI_API_KEY="your-gemini-key"
```

### Run Evolution

```bash
cd openevolve/examples/mlx_metal_kernel_opt

# Using the experiment runner script
./run_evolve_experiment.sh --run-name test_run --iterations 25

# Or directly
python -m openevolve.cli \
    --initial-program initial_program.py \
    --evaluator evaluator.py \
    --config config.yaml \
    --iterations 25 \
    --output ./openevolve_output
```

### Verify Evaluation Validity

```bash
# Run a single benchmark with verbose output
python -c "
from evaluator import Qwen3GQAEvaluator
e = Qwen3GQAEvaluator()
result = e.evaluate('initial_program.py')
print(result['summary'])
"
```

## Files

| File | Purpose |
| ---- | ------- |
| `initial_program.py` | Starting Metal kernel (to be evolved) |
| `evaluator.py` | Correctness + performance evaluation |
| `config.yaml` | Evolution configuration |
| `qwen3_benchmark_suite.py` | Benchmark definitions |
| `mlx_lm_generate_with_hook.py` | Subprocess hook wrapper |
| `run_evolve_experiment.sh` | Experiment runner script |

## Validity Fixes (This PR)

This PR corrects critical issues that invalidated prior evaluation results:

1. **Subprocess Kernel Hook**: Evolved kernels are now properly applied in benchmark subprocesses via `mlx_lm_generate_with_hook.py`

2. **bfloat16 Correctness Gate**: Correctness tests now use `mx.bfloat16` inputs to match actual inference dtype

3. **Architecture Alignment**: Fixed head ratio from 40:8 to correct 16:8 (2:1 GQA pattern)

4. **Evaluation Flow Optimizations**: Early exit on compilation errors, correctness-before-baseline ordering, GPU state cleanup between runs

## Current Status

After fixing validity issues, we ran 25 evolution iterations.

**Result: The best evolved kernel is 3.2% SLOWER than MLX's baseline implementation.**

The evolution improved from an initial -11.5% regression to -3.2%, but never exceeded baseline. This indicates fundamental limitations in the current evolution mechanism that require further investigation.

For detailed experiment results and analysis, see [EVOLUTION_ANALYSIS.md](./EVOLUTION_ANALYSIS.md).

### Demo Results (Committed)

For review and reproducibility, this example repo includes a committed snapshot of one post-fix evolution run:

- `best_program.py`: best evolved program (iteration 23)
- `best_program_info.json`: metrics + baseline comparisons (includes the -3.2% result)

The full run output directory is intentionally git-ignored (see `.gitignore`) to avoid committing large run artifacts.

### Known Limitations

1. MAP-Elites selection uses abstract `combined_score` instead of direct speedup ratios
2. LLM context underutilized (only 1 parent + 5 samples per iteration)
3. No GPU profiling data to guide optimization
4. 32% bf16 compilation failure rate

## References

- [OpenEvolve](https://github.com/codelion/openevolve)
- [MLX](https://github.com/ml-explore/mlx)
- [MLX-LM](https://github.com/ml-explore/mlx-examples)
- [KernelBench](https://github.com/ScalingIntelligence/KernelBench)