| 16 | |
| 17 | |
| 18 | class GemmTuner: |
| 19 | def __init__(self, config_path, model_path, threads=16): |
| 20 | self.config_path = Path(config_path) |
| 21 | self.model_path = model_path |
| 22 | self.threads = threads |
| 23 | self.backup_path = self.config_path.parent / f"gemm-config.h.backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}" |
| 24 | self.build_dir = Path("../build") |
| 25 | self.results = [] |
| 26 | |
| 27 | def backup_config(self): |
| 28 | """Backup current configuration file""" |
| 29 | print(f"📦 Backing up current config to {self.backup_path}") |
| 30 | shutil.copy2(self.config_path, self.backup_path) |
| 31 | |
| 32 | def restore_config(self): |
| 33 | """Restore original configuration file""" |
| 34 | print(f"♻️ Restoring original config from {self.backup_path}") |
| 35 | shutil.copy2(self.backup_path, self.config_path) |
| 36 | |
| 37 | def generate_config(self, act_parallel, row_block_size, col_block_size, parallel_size): |
| 38 | """Generate new configuration file with simplified format""" |
| 39 | content = "" |
| 40 | |
| 41 | # Simplified configuration format |
| 42 | if act_parallel: |
| 43 | content += "#define ACT_PARALLEL\n" |
| 44 | |
| 45 | content += f"#define ROW_BLOCK_SIZE {row_block_size}\n" |
| 46 | content += f"#define COL_BLOCK_SIZE {col_block_size}\n" |
| 47 | content += f"#define PARALLEL_SIZE {parallel_size}\n" |
| 48 | |
| 49 | with open(self.config_path, 'w') as f: |
| 50 | f.write(content) |
| 51 | |
| 52 | def rebuild_project(self): |
| 53 | """Rebuild project""" |
| 54 | print("🔨 Rebuilding project...") |
| 55 | result = subprocess.run( |
| 56 | ["cmake", "--build", str(self.build_dir), "--target", "llama-bench"], |
| 57 | capture_output=True, |
| 58 | text=True, |
| 59 | cwd=os.getcwd() |
| 60 | ) |
| 61 | if result.returncode != 0: |
| 62 | print(f"⚠️ Build warning/error: {result.stderr}") |
| 63 | return False |
| 64 | return True |
| 65 | |
| 66 | def run_benchmark(self): |
| 67 | """Run benchmark test""" |
| 68 | cmd = [ |
| 69 | f"{self.build_dir}/bin/llama-bench", |
| 70 | "-m", self.model_path, |
| 71 | "-p", "128", |
| 72 | "-n", "0", |
| 73 | "-t", str(self.threads), |
| 74 | "-ngl", "0" |
| 75 | ] |