Skip to content

Commit 9a84cc9

Browse files
coderfeliclaude
andauthored
[FIX] Support AOT cross-compilation with COMPILE_ONLY cache save (#383)
* [FIX] Support AOT cross-compilation with COMPILE_ONLY cache save * [FIX] Simplify aot_example.py precompile path --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3346bc9 commit 9a84cc9

4 files changed

Lines changed: 37 additions & 12 deletions

File tree

lib/Bindings/Python/DLTensorAdaptor.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,6 @@ class DLTensorAdaptor {
242242

243243
LayoutAttr layoutAttr = LayoutAttr::get(ctx, shapeAttr, strideAttr);
244244

245-
if (getAddressSpace() != 1) {
246-
throw std::runtime_error("Only device address space is supported");
247-
}
248245
AddressSpaceAttr addrSpaceAttr = AddressSpaceAttr::get(ctx, AddressSpace::Global);
249246

250247
assert(alignment_ > 0 && "alignment must be positive");

python/flydsl/_version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "0.1.3.1"

python/flydsl/compiler/jit_function.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,6 @@ def __call__(self, *args, **kwargs):
829829

830830
compiled_module = MlirCompiler.compile(module, arch=backend.target.arch, func_name=self.func.__name__)
831831

832-
if env.compile.compile_only:
833-
print(f"[flydsl] COMPILE_ONLY=1, compilation succeeded (arch={backend.target.arch})")
834-
return None
835-
836832
compiled_func = CompiledArtifact(
837833
compiled_module,
838834
self.func.__name__,
@@ -851,6 +847,10 @@ def __call__(self, *args, **kwargs):
851847
str_key = self._cache_key_to_str(cache_key)
852848
self.cache_manager.set(str_key, compiled_func)
853849

850+
if env.compile.compile_only:
851+
print(f"[flydsl] COMPILE_ONLY=1, compilation succeeded (arch={backend.target.arch})")
852+
return None
853+
854854
result = compiled_func(*jit_args)
855855

856856
# Build CallState so subsequent calls skip DLPack. The in-process

tests/python/examples/aot_example.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,40 @@
2727
import os
2828
import sys
2929
import time
30-
3130
_REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
3231
if _REPO_ROOT not in sys.path:
3332
sys.path.insert(0, _REPO_ROOT)
3433

3534
from kernels.preshuffle_gemm import compile_preshuffle_gemm_a8
3635

3736

38-
def _run_kernel(
37+
def precompile_to_cache(launch_fn, M: int, N: int, K: int, in_dtype: str):
38+
"""Trigger JIT compilation with CPU dummy tensors and COMPILE_ONLY=1."""
39+
import torch
40+
41+
is_low_prec = in_dtype not in ("fp16", "bf16")
42+
a_dtype = torch.int8 if is_low_prec else (torch.float16 if in_dtype == "fp16" else torch.bfloat16)
43+
b_elems = (N * K) // 2 if in_dtype == "int4" else N * K
44+
45+
prev = os.environ.get("COMPILE_ONLY")
46+
os.environ["COMPILE_ONLY"] = "1"
47+
try:
48+
launch_fn(
49+
torch.zeros(M * N, dtype=torch.float16),
50+
torch.zeros(M * K, dtype=a_dtype),
51+
torch.zeros(b_elems, dtype=torch.int8 if is_low_prec else a_dtype),
52+
torch.zeros(M, dtype=torch.float32) if is_low_prec else torch.empty(0, dtype=torch.float32),
53+
torch.zeros(N, dtype=torch.float32) if is_low_prec else torch.empty(0, dtype=torch.float32),
54+
M, N, 0,
55+
)
56+
finally:
57+
if prev is None:
58+
os.environ.pop("COMPILE_ONLY", None)
59+
else:
60+
os.environ["COMPILE_ONLY"] = prev
61+
62+
63+
def run_and_verify(
3964
launch_fn,
4065
M: int,
4166
N: int,
@@ -211,12 +236,14 @@ def compile_one_config(
211236
in_dtype=in_dtype,
212237
lds_stage=lds_stage,
213238
)
239+
if run_kernel:
240+
run_and_verify(launch_fn, M, N, K, in_dtype)
241+
else:
242+
precompile_to_cache(launch_fn, M, N, K, in_dtype)
243+
214244
elapsed = time.time() - t0
215245
result["compile_time"] = elapsed
216246
print(f" [OK] compile {elapsed:6.1f}s {shape_str}")
217-
218-
if run_kernel and launch_fn is not None:
219-
_run_kernel(launch_fn, M, N, K, in_dtype)
220247
except Exception as e:
221248
print(f" [FAIL] compile {shape_str}: {e}")
222249

0 commit comments

Comments
 (0)