MCPcopy Create free account
hub / github.com/RightNow-AI/autokernel / _generate_cpp_wrapper

Function _generate_cpp_wrapper

kernels/cuda/_compile.py:103–146  ·  view source on GitHub ↗

Generate a pybind11-compatible C++ wrapper that forwards torch::Tensor arguments to the CUDA kernel launcher. arg_specs: list of (name, type_str) pairs. Supported types: - "tensor" -> torch::Tensor - "int" -> int64_t - "float" -> double - "bool" -> bool

(func_name: str, arg_specs: list)

Source from the content-addressed store, hash-verified

101# ---------------------------------------------------------------------------
102
103def _generate_cpp_wrapper(func_name: str, arg_specs: list) -> str:
104 """
105 Generate a pybind11-compatible C++ wrapper that forwards torch::Tensor
106 arguments to the CUDA kernel launcher.
107
108 arg_specs: list of (name, type_str) pairs. Supported types:
109 - "tensor" -> torch::Tensor
110 - "int" -> int64_t
111 - "float" -> double
112 - "bool" -> bool
113 """
114 # Build function signature
115 cpp_args = []
116 for name, type_str in arg_specs:
117 if type_str == "tensor":
118 cpp_args.append(f"torch::Tensor {name}")
119 elif type_str == "int":
120 cpp_args.append(f"int64_t {name}")
121 elif type_str == "float":
122 cpp_args.append(f"double {name}")
123 elif type_str == "bool":
124 cpp_args.append(f"bool {name}")
125 else:
126 cpp_args.append(f"torch::Tensor {name}")
127
128 args_str = ", ".join(cpp_args)
129 forward_args = ", ".join(name for name, _ in arg_specs)
130
131 wrapper = f"""
132#include <torch/extension.h>
133
134// Forward declaration of CUDA launcher (defined in .cu source)
135torch::Tensor {func_name}_cuda({args_str});
136
137// Python-facing wrapper
138torch::Tensor {func_name}({args_str}) {{
139 return {func_name}_cuda({forward_args});
140}}
141
142PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {{
143 m.def("{func_name}", &{func_name}, "{func_name}");
144}}
145"""
146 return wrapper
147
148
149# ---------------------------------------------------------------------------

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected