tpt_gpu_kernel_optimizer/
kernel_category.rs1use crate::{ParamDim, ParamSpace};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8pub enum KernelCategory {
9 Gemm,
10 Attention,
11 Conv2d,
12 VectorAdd,
13 Normalization,
14}
15
16impl KernelCategory {
17 pub fn from_name(name: &str) -> Option<Self> {
19 match name {
20 "matmul" => Some(KernelCategory::Gemm),
21 "flash_attention" => Some(KernelCategory::Attention),
22 "conv_bn_relu" | "conv3d" => Some(KernelCategory::Conv2d),
23 "vector_add" => Some(KernelCategory::VectorAdd),
24 "softmax" | "layer_norm" | "batch_norm" | "group_norm" => {
25 Some(KernelCategory::Normalization)
26 }
27 _ => None,
28 }
29 }
30
31 pub fn all_kernel_names() -> &'static [&'static str] {
33 &[
34 "matmul",
35 "flash_attention",
36 "conv_bn_relu",
37 "conv3d",
38 "vector_add",
39 "softmax",
40 "layer_norm",
41 "batch_norm",
42 "group_norm",
43 ]
44 }
45
46 pub fn description(&self) -> &'static str {
48 match self {
49 KernelCategory::Gemm => "GEMM / matrix multiplication",
50 KernelCategory::Attention => "Flash Attention",
51 KernelCategory::Conv2d => "2D Convolution",
52 KernelCategory::VectorAdd => "Element-wise vector operations",
53 KernelCategory::Normalization => "Normalization (softmax, layer/batch/group norm)",
54 }
55 }
56}
57
58impl ParamSpace {
59 pub fn attention() -> Self {
61 Self::new(vec![
62 ParamDim::new("block_q", vec![16, 32, 64, 128]),
63 ParamDim::new("block_kv", vec![16, 32, 64, 128]),
64 ParamDim::powers_of_two("num_heads", 1, 16),
65 ParamDim::new("head_dim", vec![32, 64, 128]),
66 ParamDim::new("unroll", vec![1, 2, 4, 8]),
67 ])
68 }
69
70 pub fn conv2d() -> Self {
72 Self::new(vec![
73 ParamDim::powers_of_two("tile_oc", 16, 128),
74 ParamDim::powers_of_two("tile_ic", 8, 64),
75 ParamDim::powers_of_two("tile_oh", 4, 32),
76 ParamDim::powers_of_two("tile_ow", 4, 32),
77 ParamDim::new("kernel_w", vec![1, 3, 5, 7]),
78 ParamDim::new("kernel_h", vec![1, 3, 5, 7]),
79 ParamDim::new("stride", vec![1, 2, 4]),
80 ParamDim::new("unroll", vec![1, 2, 4, 8]),
81 ])
82 }
83
84 pub fn vector_add() -> Self {
86 Self::new(vec![
87 ParamDim::new("block_size", vec![64, 128, 256, 512, 1024]),
88 ParamDim::powers_of_two("vec_width", 1, 8),
89 ParamDim::powers_of_two("grid_size", 1, 128),
90 ])
91 }
92
93 pub fn normalization() -> Self {
95 Self::new(vec![
96 ParamDim::new("block_size", vec![64, 128, 256, 512]),
97 ParamDim::powers_of_two("vec_width", 1, 8),
98 ParamDim::new("unroll", vec![1, 2, 4, 8]),
99 ParamDim::new("warp_reduce", vec![0, 1]),
100 ])
101 }
102
103 pub fn for_category(category: KernelCategory) -> Self {
105 match category {
106 KernelCategory::Gemm => Self::gemm_default(),
107 KernelCategory::Attention => Self::attention(),
108 KernelCategory::Conv2d => Self::conv2d(),
109 KernelCategory::VectorAdd => Self::vector_add(),
110 KernelCategory::Normalization => Self::normalization(),
111 }
112 }
113
114 pub fn for_kernel(kernel_name: &str) -> Result<Self, String> {
116 match KernelCategory::from_name(kernel_name) {
117 Some(cat) => Ok(Self::for_category(cat)),
118 None => Err(format!(
119 "Unknown kernel '{}'. Supported: {}",
120 kernel_name,
121 KernelCategory::all_kernel_names().join(", ")
122 )),
123 }
124 }
125}