Extract the Triton kernel code from a Python file. Returns everything from the first import statement onward, skipping the module docstring and KERNEL_TYPE/BACKEND declarations.
(source: str)
| 235 | # --------------------------------------------------------------------------- |
| 236 | |
| 237 | def extract_triton_code(source: str) -> str: |
| 238 | """ |
| 239 | Extract the Triton kernel code from a Python file. |
| 240 | |
| 241 | Returns everything from the first import statement onward, skipping |
| 242 | the module docstring and KERNEL_TYPE/BACKEND declarations. |
| 243 | """ |
| 244 | lines = source.split("\n") |
| 245 | |
| 246 | # Find the first import line |
| 247 | import_idx = None |
| 248 | for i, line in enumerate(lines): |
| 249 | stripped = line.strip() |
| 250 | if stripped.startswith("import ") or stripped.startswith("from "): |
| 251 | import_idx = i |
| 252 | break |
| 253 | |
| 254 | if import_idx is not None: |
| 255 | return "\n".join(lines[import_idx:]) |
| 256 | |
| 257 | # Fallback: return everything after KERNEL_TYPE line |
| 258 | for i, line in enumerate(lines): |
| 259 | if line.strip().startswith("KERNEL_TYPE"): |
| 260 | return "\n".join(lines[i + 1 :]) |
| 261 | |
| 262 | return source |
| 263 | |
| 264 | |
| 265 | # --------------------------------------------------------------------------- |
no outgoing calls
no test coverage detected