Find all torch::Tensor-returning function declarations in the CUDA source. Returns a list of dicts with keys: - 'return_type': e.g. 'torch::Tensor' - 'name': e.g. 'matmul_cuda' - 'params': e.g. 'torch::Tensor A, torch::Tensor B' - 'full_signature': the complete decl
(cuda_src: str)
| 157 | # --------------------------------------------------------------------------- |
| 158 | |
| 159 | def extract_function_signatures(cuda_src: str) -> List[Dict[str, str]]: |
| 160 | """ |
| 161 | Find all torch::Tensor-returning function declarations in the CUDA source. |
| 162 | |
| 163 | Returns a list of dicts with keys: |
| 164 | - 'return_type': e.g. 'torch::Tensor' |
| 165 | - 'name': e.g. 'matmul_cuda' |
| 166 | - 'params': e.g. 'torch::Tensor A, torch::Tensor B' |
| 167 | - 'full_signature': the complete declaration |
| 168 | |
| 169 | Only extracts non-kernel functions (i.e., the C++ launcher functions that |
| 170 | PyTorch binds to, not __global__ CUDA kernels). |
| 171 | """ |
| 172 | # Match: torch::Tensor func_name(params) { |
| 173 | # Also match at::Tensor, std::vector<torch::Tensor>, void |
| 174 | pattern = ( |
| 175 | r"^((?:torch::Tensor|at::Tensor|std::vector<torch::Tensor>|void)\s+" |
| 176 | r"(\w+)\s*\(([^)]*)\))\s*\{" |
| 177 | ) |
| 178 | |
| 179 | results = [] |
| 180 | for match in re.finditer(pattern, cuda_src, re.MULTILINE): |
| 181 | full_sig = match.group(1).strip() |
| 182 | func_name = match.group(2) |
| 183 | params = match.group(3).strip() |
| 184 | return_type = full_sig.split(func_name)[0].strip() |
| 185 | |
| 186 | # Skip __global__ kernels (they are called from launchers, not from Python) |
| 187 | # Check if the line before has __global__ |
| 188 | start = match.start() |
| 189 | preceding = cuda_src[max(0, start - 200) : start] |
| 190 | if "__global__" in preceding.split("\n")[-1] if preceding else "": |
| 191 | continue |
| 192 | |
| 193 | # Also skip if the function name suggests it's a device helper |
| 194 | if func_name.startswith("__"): |
| 195 | continue |
| 196 | |
| 197 | results.append( |
| 198 | { |
| 199 | "return_type": return_type, |
| 200 | "name": func_name, |
| 201 | "params": params, |
| 202 | "full_signature": full_sig, |
| 203 | } |
| 204 | ) |
| 205 | |
| 206 | return results |
| 207 | |
| 208 | |
| 209 | def _parse_param_list(params_str: str) -> List[Tuple[str, str]]: |
no outgoing calls
no test coverage detected