-
Notifications
You must be signed in to change notification settings - Fork 13
refactor: Builder System #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ubospica
wants to merge
2
commits into
flashinfer-ai:main
Choose a base branch
from
Ubospica:main-dev/2025-11-30-builders
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,27 @@ | ||
| """Compiler subsystem package. | ||
| Exports common builder types for convenience. | ||
| This package provides the infrastructure for building solutions into executable runnables. | ||
| It includes: | ||
| - Builder: Abstract base class for different language/build system implementations | ||
| - BuilderRegistry: Central registry for managing and dispatching builders | ||
| - Runnable: Executable wrapper around compiled solutions | ||
| - RunnableMetadata: Metadata about build process and source | ||
| The typical workflow is: | ||
| 1. Get the singleton registry: registry = BuilderRegistry.get_instance() | ||
| 2. Build a solution: runnable = registry.build(definition, solution) | ||
| 3. Execute: result = runnable(**inputs) | ||
| """ | ||
|
|
||
| from .builder import Builder, BuildError | ||
| from .registry import BuilderRegistry, get_builder_registry | ||
| from .runnable import Runnable | ||
| from .runnable import Runnable, RunnableMetadata | ||
|
|
||
| __all__ = ["Builder", "BuildError", "BuilderRegistry", "Runnable", "get_builder_registry"] | ||
| __all__ = [ | ||
| "Builder", | ||
| "BuildError", | ||
| "BuilderRegistry", | ||
| "Runnable", | ||
| "RunnableMetadata", | ||
| "get_builder_registry", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,95 +1,93 @@ | ||
| """Abstract base class for solution builders.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import hashlib | ||
| import os | ||
| import re | ||
| import tempfile | ||
| from abc import ABC, abstractmethod | ||
| from typing import Callable, Dict, Optional | ||
|
|
||
| from flashinfer_bench.compile.runnable import Runnable | ||
| from flashinfer_bench.data import Definition, Solution, SourceFile | ||
|
|
||
|
|
||
| def write_sources_to_dir(dir: str, sources: list[SourceFile]) -> None: | ||
| os.makedirs(dir, exist_ok=True) | ||
| for src in sources: | ||
| abspath = os.path.join(dir, src.path) | ||
| os.makedirs(os.path.dirname(abspath), exist_ok=True) | ||
| with open(abspath, "w", encoding="utf-8") as f: | ||
| f.write(src.content) | ||
|
|
||
|
|
||
| def write_sources_to_temp(base: str, sources: list[SourceFile], pkg: Optional[str] = None) -> str: | ||
| os.makedirs(base, exist_ok=True) | ||
| tmpdir = tempfile.mkdtemp(dir=base) | ||
| if pkg: | ||
| tmpdir = os.path.join(tmpdir, pkg) | ||
| os.makedirs(tmpdir, exist_ok=True) | ||
| write_sources_to_dir(tmpdir, sources) | ||
| return tmpdir | ||
|
|
||
|
|
||
| def create_pkg_name(sol: Solution, prefix: str = "") -> str: | ||
| # Normalize the solution name | ||
| s = re.sub(r"[^0-9a-zA-Z_]", "_", sol.name) | ||
| if not s or s[0].isdigit(): | ||
| s = "_" + s | ||
| from pathlib import Path | ||
| from typing import Dict, Tuple | ||
|
|
||
| # Hash the sources | ||
| h = hashlib.sha1() | ||
| for src in sol.sources: | ||
| h.update(src.path.encode()) | ||
| h.update(src.content.encode()) | ||
| from flashinfer_bench.data import Definition, Solution | ||
| from flashinfer_bench.env import get_fib_cache_path | ||
|
|
||
| return prefix + s + "_" + h.hexdigest()[:6] | ||
| from .runnable import Runnable | ||
| from .utils import create_package_name | ||
|
|
||
|
|
||
| class BuildError(RuntimeError): | ||
| """Raised when a builder fails to construct a runnable implementation.""" | ||
|
|
||
|
|
||
| class Builder(ABC): | ||
| """Builder abstraction: (Definition, Solution) -> Runnable with hidden cache.""" | ||
| """Abstract base class for building solutions into runnable implementations. | ||
| def __init__(self) -> None: | ||
| self._cache: Dict[str, Runnable] = {} | ||
| A Builder transforms a (Definition, Solution) pair into a Runnable object, which is an | ||
| executable implementation of the solution. Different builders handle different programming | ||
| languages (e.g., Python, CUDA, Triton) and build systems. | ||
| @abstractmethod | ||
| def can_build(self, solution: Solution) -> bool: | ||
| """Build guard to check if this builder can handle the given solution.""" | ||
| ... | ||
| Subclasses must implement all its abstract methods. Expectedly, the concrete builder should | ||
| operate in the folder `FIB_CACHE_PATH / builder_specific_subfolder / key`, where `key` is | ||
| a unique identifier for the solution. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def _build(self, definition: Definition, solution: Solution) -> Runnable: | ||
| """Perform a real build and return a Runnable; raise BuildError on failure.""" | ||
| ... | ||
| def __init__(self, key_prefix: str, build_dir_name: str) -> None: | ||
| """Initialize the builder.""" | ||
| self._key_prefix = key_prefix | ||
| self._build_dir_name = build_dir_name | ||
|
|
||
| @abstractmethod | ||
| def _make_closer(self, *args, **kwargs) -> Callable[[], None]: | ||
| """Factory for a resource closer used by the concrete builder.""" | ||
| def can_build(self, solution: Solution) -> bool: | ||
| """Check if this builder can handle the given solution. | ||
| Parameters | ||
| ---------- | ||
| solution : Solution | ||
| The solution to check. | ||
| Returns | ||
| ------- | ||
| bool | ||
| True if this builder can build the solution, False otherwise. | ||
| """ | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def _make_key(self, solution: Solution) -> str: | ||
| """Cache key for a solution.""" | ||
| def build(self, definition: Definition, solution: Solution) -> Runnable: | ||
| """Build a solution into a runnable implementation. | ||
| This method compiles/loads the solution's source code and returns a Runnable | ||
| object that can be executed with the interface specified by the definition. | ||
| Parameters | ||
| ---------- | ||
| definition : Definition | ||
| The problem definition that specifies the expected interface. | ||
| solution : Solution | ||
| The solution implementation to build. | ||
| Returns | ||
| ------- | ||
| Runnable | ||
| An executable wrapper around the built implementation. | ||
| Raises | ||
| ------ | ||
| BuildError | ||
| If the build fails for any reason (compilation errors, missing dependencies, etc.). | ||
| """ | ||
| ... | ||
|
|
||
| def build(self, definition: Definition, solution: Solution) -> Runnable: | ||
| """Public entry with per-solution cache keyed by solution.name.""" | ||
| key = self._make_key(solution) | ||
| if key in self._cache: | ||
| return self._cache[key] | ||
| runnable = self._build(definition, solution) | ||
| self._cache[key] = runnable | ||
| return runnable | ||
|
|
||
| def clear_cache(self) -> None: | ||
| """Close all cached runnables and clear the cache.""" | ||
| for r in list(self._cache.values()): | ||
| try: | ||
| r.close() | ||
| except Exception: | ||
| # Best-effort cleanup; keep going | ||
| pass | ||
| self._cache.clear() | ||
| def get_package_name_and_build_path(self, solution: Solution) -> Tuple[str, Path]: | ||
| """Get the package name and build path for the solution. | ||
| Parameters | ||
| ---------- | ||
| solution : Solution | ||
| The solution to get the package name and build path for. | ||
| Returns | ||
| ------- | ||
| Tuple[str, Path]: The package name and build path for the solution. | ||
| """ | ||
| package_name = create_package_name(solution, self._key_prefix) | ||
| build_path = get_fib_cache_path() / self._build_dir_name / package_name | ||
| return package_name, build_path | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| from .cuda_builder import CUDABuilder | ||
| """Concrete builder implementations for different languages and build systems.""" | ||
|
|
||
| from .python_builder import PythonBuilder | ||
| from .torch_builder import TorchBuilder | ||
| from .triton_builder import TritonBuilder | ||
| from .tvm_ffi_builder import TVMFFIBuilder | ||
|
|
||
| __all__ = ["CUDABuilder", "PythonBuilder", "TritonBuilder", "TVMFFIBuilder"] | ||
| __all__ = ["TorchBuilder", "PythonBuilder", "TritonBuilder", "TVMFFIBuilder"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
buildmethod is now abstract, but some test implementations likeDummyBuilderintests/compile/test_builder.pystill implement_build, which will cause an error. Please ensure all concrete builder implementations, including those in tests, are updated to implement the publicbuildmethod.