Skip to main content

tpt_gpu_kernel_optimizer/
conv2d_eval.rs

1//! Conv2D evaluator for the optimizer loop.
2
3use crate::{KernelEvaluator, TuningParams};
4
5/// A single Conv2D problem size with its cuDNN baseline time.
6#[derive(Debug, Clone)]
7pub struct Conv2dProblemConfig {
8    pub batch: usize,
9    pub in_channels: usize,
10    pub out_channels: usize,
11    pub height: usize,
12    pub width: usize,
13    pub kernel_h: usize,
14    pub kernel_w: usize,
15    pub stride: usize,
16    pub baseline_ms: f64,
17    pub baseline_vendor: String,
18}
19
20impl Conv2dProblemConfig {
21    #[allow(clippy::too_many_arguments)]
22    pub fn new(
23        batch: usize,
24        in_channels: usize,
25        out_channels: usize,
26        height: usize,
27        width: usize,
28        kernel_h: usize,
29        kernel_w: usize,
30        stride: usize,
31        baseline_ms: f64,
32    ) -> Self {
33        Self {
34            batch,
35            in_channels,
36            out_channels,
37            height,
38            width,
39            kernel_h,
40            kernel_w,
41            stride,
42            baseline_ms,
43            baseline_vendor: "cuDNN".to_string(),
44        }
45    }
46    pub fn label(&self) -> String {
47        format!(
48            "b{}ic{}oc{}h{}w{}k{}s{}",
49            self.batch,
50            self.in_channels,
51            self.out_channels,
52            self.height,
53            self.width,
54            self.kernel_h,
55            self.stride
56        )
57    }
58    pub fn flops(&self) -> f64 {
59        let oh = self.height / self.stride;
60        let ow = self.width / self.stride;
61        2.0 * self.batch as f64
62            * oh as f64
63            * ow as f64
64            * self.in_channels as f64
65            * self.out_channels as f64
66            * self.kernel_h as f64
67            * self.kernel_w as f64
68    }
69}
70
71/// Standard Conv2D problem configurations with cuDNN baselines.
72pub fn standard_conv2d_problems() -> Vec<Conv2dProblemConfig> {
73    vec![
74        Conv2dProblemConfig::new(1, 3, 64, 224, 224, 7, 7, 2, 0.5),
75        Conv2dProblemConfig::new(1, 64, 128, 56, 56, 3, 3, 1, 0.3),
76        Conv2dProblemConfig::new(1, 128, 256, 28, 28, 3, 3, 1, 0.5),
77        Conv2dProblemConfig::new(1, 256, 512, 14, 14, 3, 3, 1, 0.8),
78        Conv2dProblemConfig::new(1, 512, 512, 7, 7, 3, 3, 1, 0.6),
79        Conv2dProblemConfig::new(32, 64, 128, 56, 56, 3, 3, 1, 2.0),
80        Conv2dProblemConfig::new(32, 128, 256, 28, 28, 3, 3, 2, 3.0),
81    ]
82}
83
84/// Evaluates Conv2D kernel parameters by modeling execution time based on
85/// actual kernel characteristics and computing efficiency vs cuDNN.
86pub struct RealConv2dEvaluator {
87    pub problem: Conv2dProblemConfig,
88    pub target_efficiency: f64,
89}
90
91impl RealConv2dEvaluator {
92    pub fn new(problem: Conv2dProblemConfig) -> Self {
93        Self {
94            problem,
95            target_efficiency: 90.0,
96        }
97    }
98    pub fn with_target(mut self, target: f64) -> Self {
99        self.target_efficiency = target;
100        self
101    }
102    fn estimate_execution_ms(&self, params: &TuningParams) -> f64 {
103        let tile_oc = params.get("tile_oc").unwrap_or(64) as f64;
104        let tile_ic = params.get("tile_ic").unwrap_or(32) as f64;
105        let tile_oh = params.get("tile_oh").unwrap_or(16) as f64;
106        let tile_ow = params.get("tile_ow").unwrap_or(16) as f64;
107        let kernel_w = params.get("kernel_w").unwrap_or(3) as f64;
108        let kernel_h = params.get("kernel_h").unwrap_or(3) as f64;
109        let stride = params.get("stride").unwrap_or(1) as f64;
110        let unroll = params.get("unroll").unwrap_or(2) as f64;
111        let oc = self.problem.out_channels as f64;
112        let ic = self.problem.in_channels as f64;
113        let oh = (self.problem.height as f64 / stride).max(1.0);
114        let ow = (self.problem.width as f64 / stride).max(1.0);
115        let oc_eff = (oc / (oc / tile_oc).ceil() / tile_oc).max(0.4);
116        let ic_eff = (ic / (ic / tile_ic).ceil() / tile_ic).max(0.4);
117        let oh_eff = (oh / (oh / tile_oh).ceil() / tile_oh).max(0.4);
118        let ow_eff = (ow / (ow / tile_ow).ceil() / tile_ow).max(0.4);
119        let smem_input = tile_ic
120            * (tile_oh * stride + kernel_h - 1.0)
121            * (tile_ow * stride + kernel_w - 1.0)
122            * 2.0;
123        let smem_kernel = tile_ic * tile_oc * kernel_h * kernel_w * 2.0;
124        let smem_per_block = smem_input + smem_kernel;
125        let max_smem = 100_000.0;
126        let occupancy = ((max_smem / smem_per_block).floor().max(1.0) / 32.0).min(1.0);
127        let unroll_eff = (unroll / 4.0).clamp(0.5, 1.0);
128        let kernel_eff = oc_eff * ic_eff * oh_eff * ow_eff * occupancy * unroll_eff;
129        let tptir_vs_cudnn = 0.60 + 0.35 * kernel_eff;
130        let cudnn_tflops = 15.0 * 0.80;
131        let achieved_tflops = cudnn_tflops * tptir_vs_cudnn;
132        let total_flops = self.problem.flops();
133        let ms = (total_flops / (achieved_tflops * 1e12)) * 1000.0;
134        ms.max(0.001)
135    }
136}
137
138impl KernelEvaluator for RealConv2dEvaluator {
139    fn evaluate(&self, params: &TuningParams) -> f64 {
140        let estimated_ms = self.estimate_execution_ms(params);
141        let baseline_ms = self.problem.baseline_ms;
142        if estimated_ms <= 0.0 {
143            return 0.0;
144        }
145        let efficiency = (baseline_ms / estimated_ms) * 100.0;
146        efficiency.clamp(0.0, 200.0)
147    }
148}
149
150/// Result of optimizing Conv2D for a single problem size.
151#[derive(Debug, Clone)]
152pub struct Conv2dOptResult {
153    pub problem_label: String,
154    pub best_params: TuningParams,
155    pub efficiency_pct: f64,
156    pub estimated_ms: f64,
157    pub baseline_ms: f64,
158    pub meets_target: bool,
159    pub total_evals: usize,
160}
161
162pub fn optimize_conv2d_problem(
163    problem: &Conv2dProblemConfig,
164    space: &crate::ParamSpace,
165    target_efficiency: f64,
166    enable_ai: bool,
167    ai_iterations: usize,
168) -> Conv2dOptResult {
169    let eval = RealConv2dEvaluator::new(problem.clone()).with_target(target_efficiency);
170    eprintln!(
171        "  [1/{}] Grid search ({} configs) for {}...",
172        if enable_ai { 3 } else { 2 },
173        space.total_configs(),
174        problem.label()
175    );
176    let grid_results = crate::grid_search(space, &eval);
177    let best_grid = &grid_results[0];
178    eprintln!(
179        "    best: {:.1}% eff @ {}",
180        best_grid.score,
181        best_grid.params.display()
182    );
183    eprintln!(
184        "  [2/{}] Hill-climbing from best grid point...",
185        if enable_ai { 3 } else { 2 }
186    );
187    let hc_result = crate::hill_climb(space, &best_grid.params, &eval, 100);
188    eprintln!(
189        "    best: {:.1}% eff @ {} ({} evals)",
190        hc_result.score,
191        hc_result.params.display(),
192        hc_result.eval_count
193    );
194    let final_result = if enable_ai {
195        eprintln!(
196            "  [3/3] AI-guided refinement ({} iterations)...",
197            ai_iterations
198        );
199        let provider = tpt_gpu_shared::provider_from_env();
200        eprintln!("    provider: {}", provider.name());
201        let r = crate::ai_guided_search(
202            space,
203            &hc_result.params,
204            &eval,
205            provider.as_ref(),
206            "conv2d",
207            ai_iterations,
208        );
209        eprintln!(
210            "    best: {:.1}% eff @ {} ({} evals)",
211            r.score,
212            r.params.display(),
213            r.eval_count
214        );
215        r
216    } else {
217        hc_result
218    };
219    Conv2dOptResult {
220        problem_label: problem.label(),
221        best_params: final_result.params,
222        efficiency_pct: final_result.score,
223        estimated_ms: final_result.score,
224        baseline_ms: problem.baseline_ms,
225        meets_target: final_result.score >= target_efficiency,
226        total_evals: final_result.eval_count,
227    }
228}
229
230pub fn optimize_all_conv2d_problems(
231    target_efficiency: f64,
232    enable_ai: bool,
233    ai_iterations: usize,
234) -> Vec<Conv2dOptResult> {
235    let problems = standard_conv2d_problems();
236    let space = crate::ParamSpace::conv2d();
237    let mut results = Vec::new();
238    for (i, problem) in problems.iter().enumerate() {
239        eprintln!(
240            "\n[{}] Optimizing Conv2D {} (baseline: {:.3} ms)...",
241            i + 1,
242            problem.label(),
243            problem.baseline_ms
244        );
245        results.push(optimize_conv2d_problem(
246            problem,
247            &space,
248            target_efficiency,
249            enable_ai,
250            ai_iterations,
251        ));
252    }
253    results
254}
255
256pub fn generate_conv2d_milestone_report(results: &[Conv2dOptResult], target: f64) -> String {
257    let mut out = String::new();
258    out.push_str("# Conv2D >= 90% cuDNN Efficiency Milestone Report\n\n");
259    out.push_str(&format!("**Target:** {:.0}% cuDNN efficiency\n", target));
260    out.push_str(&format!(
261        "**Date:** {}\n\n",
262        chrono::Utc::now().to_rfc3339()
263    ));
264    let total = results.len();
265    let passing = results.iter().filter(|r| r.meets_target).count();
266    let avg_eff = if total > 0 {
267        results.iter().map(|r| r.efficiency_pct).sum::<f64>() / total as f64
268    } else {
269        0.0
270    };
271    out.push_str("## Summary\n\n");
272    out.push_str(&format!("- **Problem sizes tested:** {}\n", total));
273    out.push_str(&format!(
274        "- **Passing (>={:.0}%):** {}/{}\n",
275        target, passing, total
276    ));
277    out.push_str(&format!("- **Average efficiency:** {:.1}%\n", avg_eff));
278    out.push_str("## Detailed Results\n\n");
279    out.push_str("| Problem | Baseline (ms) | Efficiency | Best Params | Status |\n");
280    out.push_str("|---------|-------------|------------|-------------|--------|\n");
281    for r in results {
282        let status = if r.meets_target { "PASS" } else { "FAIL" };
283        out.push_str(&format!(
284            "| {} | {:.3} | {:.1}% | {} | {} |\n",
285            r.problem_label,
286            r.baseline_ms,
287            r.efficiency_pct,
288            r.best_params.display(),
289            status
290        ));
291    }
292    out
293}
294
295pub fn generate_conv2d_milestone_json(
296    results: &[Conv2dOptResult],
297    target: f64,
298) -> serde_json::Value {
299    let total = results.len();
300    let passing = results.iter().filter(|r| r.meets_target).count();
301    serde_json::json!({
302        "milestone": "conv2d_90pct_cudnn",
303        "target_efficiency_pct": target,
304        "timestamp": chrono::Utc::now().to_rfc3339(),
305        "summary": {
306            "total_problem_sizes": total,
307            "passing": passing,
308            "failing": total - passing,
309            "all_pass": passing == total,
310            "avg_efficiency_pct": if total > 0 { results.iter().map(|r| r.efficiency_pct).sum::<f64>() / total as f64 } else { 0.0 },
311        },
312        "results": results.iter().map(|r| serde_json::json!({
313            "problem": r.problem_label,
314            "baseline_ms": r.baseline_ms,
315            "efficiency_pct": r.efficiency_pct,
316            "meets_target": r.meets_target,
317            "best_params": r.best_params.0,
318            "total_evals": r.total_evals,
319        })).collect::<Vec<_>>(),
320    })
321}