Export a Triton kernel to HF Kernels format. Triton kernels are already Python, so the export is simpler: package the Triton code as a Python module. No CUDA compilation needed -- the Triton JIT compiler handles everything at runtime.
(
source: str,
name: str,
output_dir: str,
repo_id: str,
)
| 585 | # --------------------------------------------------------------------------- |
| 586 | |
| 587 | def _export_triton_kernel( |
| 588 | source: str, |
| 589 | name: str, |
| 590 | output_dir: str, |
| 591 | repo_id: str, |
| 592 | ) -> None: |
| 593 | """ |
| 594 | Export a Triton kernel to HF Kernels format. |
| 595 | |
| 596 | Triton kernels are already Python, so the export is simpler: package |
| 597 | the Triton code as a Python module. No CUDA compilation needed -- the |
| 598 | Triton JIT compiler handles everything at runtime. |
| 599 | """ |
| 600 | # Create directory structure |
| 601 | project_dir = os.path.join(output_dir, name) |
| 602 | module_dir = os.path.join(project_dir, name) |
| 603 | |
| 604 | os.makedirs(module_dir, exist_ok=True) |
| 605 | |
| 606 | # 1. Write the Triton kernel as kernel.py inside the module |
| 607 | triton_code = extract_triton_code(source) |
| 608 | kernel_py_path = os.path.join(module_dir, "kernel.py") |
| 609 | with open(kernel_py_path, "w", encoding="utf-8") as f: |
| 610 | f.write(triton_code.strip()) |
| 611 | f.write("\n") |
| 612 | print(f" Created {os.path.relpath(kernel_py_path, output_dir)}") |
| 613 | |
| 614 | # 2. Write __init__.py |
| 615 | init_py_path = os.path.join(module_dir, "__init__.py") |
| 616 | # For Triton, functions are the Python entry points (kernel_fn) |
| 617 | functions = [{"name": "kernel_fn"}] |
| 618 | init_py_content = generate_init_py(name, functions, repo_id, backend="triton") |
| 619 | with open(init_py_path, "w", encoding="utf-8") as f: |
| 620 | f.write(init_py_content) |
| 621 | print(f" Created {os.path.relpath(init_py_path, output_dir)}") |
| 622 | |
| 623 | # 3. Write a minimal pyproject.toml for the Triton package |
| 624 | pyproject_path = os.path.join(project_dir, "pyproject.toml") |
| 625 | pyproject_content = textwrap.dedent(f"""\ |
| 626 | [project] |
| 627 | name = "{name}" |
| 628 | version = "0.1.0" |
| 629 | description = "Optimized Triton GPU kernel exported from AutoKernel" |
| 630 | requires-python = ">=3.10" |
| 631 | dependencies = [ |
| 632 | "torch>=2.4.0", |
| 633 | "triton>=3.3.0", |
| 634 | ] |
| 635 | """) |
| 636 | with open(pyproject_path, "w", encoding="utf-8") as f: |
| 637 | f.write(pyproject_content) |
| 638 | print(f" Created {os.path.relpath(pyproject_path, output_dir)}") |
| 639 | |
| 640 | # 4. Write a README for the Hub repo |
| 641 | readme_path = os.path.join(project_dir, "README.md") |
| 642 | readme_content = textwrap.dedent(f"""\ |
| 643 | # {name} |
| 644 |
no test coverage detected