Generate torch_binding.cpp with TORCH_LIBRARY_EXPAND registration. Uses the HuggingFace Kernels convention: torch/library.h + registration.h + REGISTER_EXTENSION macro for compatibility with the kernel-builder Nix build pipeline.
(
name: str,
functions: List[Dict[str, str]],
)
| 365 | |
| 366 | |
| 367 | def generate_torch_binding_cpp( |
| 368 | name: str, |
| 369 | functions: List[Dict[str, str]], |
| 370 | ) -> str: |
| 371 | """ |
| 372 | Generate torch_binding.cpp with TORCH_LIBRARY_EXPAND registration. |
| 373 | |
| 374 | Uses the HuggingFace Kernels convention: torch/library.h + registration.h |
| 375 | + REGISTER_EXTENSION macro for compatibility with the kernel-builder |
| 376 | Nix build pipeline. |
| 377 | """ |
| 378 | # Build ops.def() and ops.impl() lines |
| 379 | ops_lines = [] |
| 380 | for func in functions: |
| 381 | schema = _build_ops_schema(func) |
| 382 | ops_lines.append(f' ops.def("{schema}");') |
| 383 | ops_lines.append(f' ops.impl("{func["name"]}", torch::kCUDA, &{func["name"]});') |
| 384 | |
| 385 | ops_str = "\n".join(ops_lines) |
| 386 | |
| 387 | return ( |
| 388 | "#include <torch/library.h>\n" |
| 389 | "\n" |
| 390 | '#include "registration.h"\n' |
| 391 | '#include "torch_binding.h"\n' |
| 392 | "\n" |
| 393 | "TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {\n" |
| 394 | f"{ops_str}\n" |
| 395 | "}\n" |
| 396 | "\n" |
| 397 | "REGISTER_EXTENSION(TORCH_EXTENSION_NAME)\n" |
| 398 | ) |
| 399 | |
| 400 | |
| 401 | def generate_torch_binding_h( |
no test coverage detected