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

Function _export_cuda_kernel

export_hf.py:492–580  ·  view source on GitHub ↗

Export a CUDA C++ kernel to HF Kernels format.

(
    source: str,
    name: str,
    output_dir: str,
    repo_id: str,
)

Source from the content-addressed store, hash-verified

490# ---------------------------------------------------------------------------
491
492def _export_cuda_kernel(
493 source: str,
494 name: str,
495 output_dir: str,
496 repo_id: str,
497) -> None:
498 """Export a CUDA C++ kernel to HF Kernels format."""
499
500 # Extract the CUDA source string
501 cuda_src = extract_cuda_source(source)
502 if cuda_src is None:
503 print("ERROR: Could not extract CUDA_SRC from kernel file.")
504 print(" Expected a CUDA_SRC = r\"\"\"...\"\"\" string assignment.")
505 sys.exit(1)
506
507 # Parse function signatures from the CUDA source
508 functions = extract_function_signatures(cuda_src)
509 if not functions:
510 # Try to detect from compile_cuda call
511 func_name = extract_function_name_from_compile(source)
512 if func_name:
513 print(
514 f"WARNING: Could not parse function signatures from CUDA source. "
515 f"Using function name from compile_cuda call: {func_name}"
516 )
517 # Create a placeholder signature -- the user may need to adjust
518 functions = [
519 {
520 "return_type": "torch::Tensor",
521 "name": func_name,
522 "params": "torch::Tensor input",
523 "full_signature": f"torch::Tensor {func_name}(torch::Tensor input)",
524 }
525 ]
526 else:
527 print("ERROR: Could not find any torch::Tensor-returning functions in CUDA source.")
528 print(" The CUDA source should contain launcher functions like:")
529 print(" torch::Tensor my_kernel_cuda(torch::Tensor A, torch::Tensor B) { ... }")
530 sys.exit(1)
531
532 # Create directory structure (matches kernels-community convention)
533 project_dir = os.path.join(output_dir, name)
534 kernel_cuda_dir = os.path.join(project_dir, f"{name}_cuda")
535 torch_ext_dir = os.path.join(project_dir, "torch-ext")
536
537 os.makedirs(kernel_cuda_dir, exist_ok=True)
538 os.makedirs(torch_ext_dir, exist_ok=True)
539
540 # 1. Write kernel.cu
541 kernel_cu_path = os.path.join(kernel_cuda_dir, "kernel.cu")
542 cuda_src_clean = cuda_src.strip()
543 with open(kernel_cu_path, "w", encoding="utf-8") as f:
544 f.write(cuda_src_clean)
545 f.write("\n")
546 print(f" Created {os.path.relpath(kernel_cu_path, output_dir)}")
547
548 # 2. Write build.toml
549 build_toml_path = os.path.join(project_dir, "build.toml")

Callers 1

export_kernelFunction · 0.85

Calls 7

extract_cuda_sourceFunction · 0.85
generate_build_tomlFunction · 0.85
generate_torch_binding_hFunction · 0.85
generate_flake_nixFunction · 0.85

Tested by

no test coverage detected