Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 18 additions & 117 deletions projects/hipblaslt/tensilelite/Tensile/TensileCreateLibrary/Run.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,18 @@ def memCompress(obj):
def memDecompress(byt):
return pickle.loads(zlib.decompress(byt))

_GFX1250 = (12, 5)


def _emitKernel(kernelWriterAssembly, splitGSU, kernel, compress=False) -> KernelCodeGenResult:
"""Run codegen on a single kernel after rocisa state has been set up."""
def processKernelSource(kernelWriterAssembly, data, outOptions, splitGSU, kernel, compress = False) -> KernelCodeGenResult:
"""
Generate source for a single kernel.
Returns (error, source, header, kernelName).
"""
kernelWriter = kernelWriterAssembly
kernelWriter.setRocIsa(data, outOptions)
asmFilename = getKernelFileBase(splitGSU, kernel)
err, src = kernelWriterAssembly.getSourceFileString(kernel)
err, src = kernelWriter.getSourceFileString(kernel)
if compress:
src = memCompress(src)
header = kernelWriterAssembly.getHeaderFileString(kernel)
header = kernelWriter.getHeaderFileString(kernel)
objFilename = kernel._state.get("codeObjectFile", None)
pgr = int(kernel["PrefetchGlobalRead"])
cuocc = kernel["CUOccupancy"]
Expand All @@ -260,38 +262,11 @@ def _emitKernel(kernelWriterAssembly, splitGSU, kernel, compress=False) -> Kerne
f"runtime will clamp to 1."
)
return KernelCodeGenResult(
err, src, header, asmFilename, objFilename, tuple(kernel["ISA"]),
kernel["WavefrontSize"], cuocc,
err, src, header, asmFilename, objFilename, tuple(kernel["ISA"]), \
kernel["WavefrontSize"], cuocc, \
pgr, kernel["MathClocksUnrolledLoop"]
)


def processKernelSource(kernelWriterAssembly, data, outOptions, splitGSU, kernel, compress = False) -> KernelCodeGenResult:
"""Generate source for a single kernel."""
kernelWriterAssembly.setRocIsa(data, outOptions)
return _emitKernel(kernelWriterAssembly, splitGSU, kernel, compress)


def processKernelSourceNativeInit(
kernelWriterAssembly, assemblerPath, outputNoComment, splitGSU, kernel, compress=False
) -> KernelCodeGenResult:
"""Generate source for a non-gfx1250 kernel using native rocisa.

Workers importing native rocisa (``ROCISA_BACKEND=rocisa``) call this
instead of ``processKernelSource`` so they initialise the rocisa
singleton from scratch rather than accepting pickled adapter data.
Each ISA is initialised at most once per worker process.
"""
import rocisa as _ri
ti = _ri.rocIsa.getInstance()
isa = tuple(kernel["ISA"])
if isa not in ti.getData():
ti.init(isa, assemblerPath, False)
outOpts = ti.getOutputOptions()
outOpts.outputNoComment = outputNoComment
kernelWriterAssembly.setRocIsa(ti.getData(), outOpts)
return _emitKernel(kernelWriterAssembly, splitGSU, kernel, compress)

def _checkInvalidSolutionsAndKernels(errorTolerant, result, kernel):
if result.err != 0:
if not errorTolerant:
Expand Down Expand Up @@ -701,87 +676,13 @@ def composed_function(kernel):
return processed_kernel
return composed_function

# --- Backend-aware kernel batching ---
# When the stinkytofu adapter is active (gfx1250 auto-detected), split
# kernels so gfx1250 uses the adapter and non-gfx1250 uses native rocisa
# in a fresh worker pool.
_stinkytofu_active = getattr(rocisa, "_BACKEND", "") == "stinkytofu"

if _stinkytofu_active:
gfx1250_kernels = [k for k in uniqueAsmKernels if tuple(k["ISA"])[:2] == _GFX1250]
native_kernels = [k for k in uniqueAsmKernels if tuple(k["ISA"])[:2] != _GFX1250]
else:
gfx1250_kernels = []
native_kernels = []

if not native_kernels:
# Common path: single-backend (all stinkytofu or all native).
results = ParallelMap2(
compose(unaryAssemble, unaryWriteAssembly, unaryProcessKernelSource),
uniqueAsmKernels,
"Generating assembly kernels",
multiArg=False,
return_as="list"
)
else:
results = []
# Batch 1: gfx1250 kernels with stinkytofu adapter workers.
if gfx1250_kernels:
results.extend(ParallelMap2(
compose(unaryAssemble, unaryWriteAssembly, unaryProcessKernelSource),
gfx1250_kernels,
"Generating assembly kernels (gfx1250, stinkytofu)",
multiArg=False,
return_as="list"
))

# Batch 2: non-gfx1250 kernels with fresh native-rocisa workers.
# Shut down the existing worker pool so new workers inherit the
# updated ROCISA_BACKEND env and import native rocisa.
try:
from joblib.externals.loky import get_reusable_executor
get_reusable_executor().shutdown(wait=True)
except Exception:
pass

_orig_backend = os.environ.get("ROCISA_BACKEND")
_orig_threads = globalParameters["CpuThreads"]
os.environ["ROCISA_BACKEND"] = "rocisa"
# Force at least 2 threads so joblib spawns a subprocess.
# Running in-process would reuse the already-imported stinkytofu
# adapter from sys.modules instead of loading native rocisa.
if _orig_threads >= 0 and _orig_threads < 2:
globalParameters["CpuThreads"] = 2
try:
assemblerPath = str(asmToolchain.assembler._component_path)
unaryProcessNative = functools.partial(
processKernelSourceNativeInit,
kernelWriterAssembly,
assemblerPath,
not disableAsmComments,
splitGSU,
compress=memcompress,
)
results.extend(ParallelMap2(
compose(unaryAssemble, unaryWriteAssembly, unaryProcessNative),
native_kernels,
"Generating assembly kernels (native rocisa)",
multiArg=False,
return_as="list"
))
finally:
# Shut down native workers before restoring env so joblib won't
# reuse them for subsequent stinkytofu codegen.
try:
from joblib.externals.loky import get_reusable_executor
get_reusable_executor().shutdown(wait=True)
except Exception:
pass
globalParameters["CpuThreads"] = _orig_threads
if _orig_backend is None:
os.environ.pop("ROCISA_BACKEND", None)
else:
os.environ["ROCISA_BACKEND"] = _orig_backend
results = ParallelMap2(
compose(unaryAssemble, unaryWriteAssembly, unaryProcessKernelSource),
uniqueAsmKernels,
"Generating assembly kernels",
multiArg=False,
return_as="list"
)

buildAssemblyCodeObjectFiles(
asmToolchain.linker,
Expand Down
54 changes: 15 additions & 39 deletions projects/hipblaslt/tensilelite/rocisa/rocisa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,7 @@ def _import_rocisa():
# force the native path on gfx1250.

def _detect_default_backend() -> str:
"""Return ``"stinkytofu"`` if gfx1250 hardware is detected AND the build
targets include gfx1250, else ``""``."""
# If --architecture is on the command line and none of the targets are
# gfx1250, there is no point loading the stinkytofu adapter regardless
# of the physical GPU. This covers every entry point (bin/*, __main__,
# -m invocations) without per-script patches.
for i, arg in enumerate(sys.argv):
arch_val = None
if arg == "--architecture" and i + 1 < len(sys.argv):
arch_val = sys.argv[i + 1]
elif arg.startswith("--architecture="):
arch_val = arg.split("=", 1)[1]
if arch_val is not None:
archs = arch_val.replace(";", "_").split("_")
if any("gfx1250" in a for a in archs):
return "stinkytofu"
return ""

"""Return ``"stinkytofu"`` if gfx1250 hardware is detected, else ``""``."""
import subprocess as _sp
rocmpath = os.environ.get(
"TENSILE_ROCM_PATH", os.environ.get("ROCM_PATH", "/opt/rocm")
Expand Down Expand Up @@ -192,15 +175,6 @@ def _load_stinkytofu_adapter() -> "tuple[bool, str]":
# ``rocisa_stinkytofu_adaptor.*`` submodule under ``rocisa.*`` in
# ``sys.modules``.
sys.modules["rocisa"] = _adapter
_adapter._BACKEND = "stinkytofu"

# Force-load lazy submodules so ``import rocisa.code`` etc. find the
# adapter's modules (not duplicates created via __path__ lookup).
# Safe here because _load_stinkytofu_adapter only runs when the
# stinkytofu backend is actually wanted; native workers never call it.
for _lazy in getattr(_adapter, "_LAZY_SUBMODULES", ()):
getattr(_adapter, _lazy, None)

_prefix = f"{_ADAPTER_PKG}."
for _name, _obj in vars(_adapter).items():
if isinstance(_obj, types.ModuleType) and _obj.__name__.startswith(_prefix):
Expand Down Expand Up @@ -300,26 +274,28 @@ def _resolve_backend(requested, available_fn, load_fn, warn=warnings.warn,
auto_detected=False) -> bool:
"""Decide whether to use the stinkytofu adapter (True) or native rocisa (False).

Warns only when the user *explicitly* set ``ROCISA_BACKEND=stinkytofu``
but the backend cannot be loaded. Auto-detected failures (gfx1250 machine
where stinkytofu is simply not built) fall back silently — the fallback to
native rocisa is correct and expected in that case.
Emits a warning *only* when the stinkytofu backend was explicitly requested
(or auto-detected) but we have to fall back to native — so an unnoticed
silent fallback becomes visible, with a cause-specific reason attached.
``available_fn`` and ``load_fn`` share the same ``(ok, reason)`` contract;
the reason is surfaced verbatim so each distinct failure mode produces its
own warning. Requesting anything else (or unset) selects native without
touching the probes and without warning.
"""
if requested != "stinkytofu":
return False
_origin = "auto-detected for gfx1250" if auto_detected else "ROCISA_BACKEND=stinkytofu"
available, avail_reason = available_fn()
if not available:
if not auto_detected:
warn(avail_reason, stacklevel=2)
warn(avail_reason, stacklevel=2)
return False
ok, reason = load_fn()
if not ok:
if not auto_detected:
warn(
f"stinkytofu backend (ROCISA_BACKEND=stinkytofu) requested "
f"but the adapter failed to load ({reason}){_FALLBACK}",
stacklevel=2,
)
warn(
f"stinkytofu backend ({_origin}) requested but the adapter failed "
f"to load ({reason}){_FALLBACK}",
stacklevel=2,
)
return False
return True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,31 +159,6 @@ def test_load_failure_falls_back_with_reason(warnings_sink):
assert "adapter failed to load" in msgs[0]


def test_auto_detected_unavailable_falls_back_silently(warnings_sink):
"""Auto-detected gfx1250 but stinkytofu not built -> silent fallback."""
msgs, warn = warnings_sink
load = _Probe((True, ""))

assert _resolve_backend(
"stinkytofu", _Probe((False, "not built")), load,
warn=warn, auto_detected=True,
) is False
assert load.calls == 0
assert msgs == []


def test_auto_detected_load_failure_falls_back_silently(warnings_sink):
"""Auto-detected gfx1250, stinkytofu available but adapter fails -> silent fallback."""
msgs, warn = warnings_sink
load = _Probe((False, "import failed: AttributeError('boom')"))

assert _resolve_backend(
"stinkytofu", _Probe((True, "")), load,
warn=warn, auto_detected=True,
) is False
assert msgs == []


def test_backend_value_is_normalized_strip_lower():
"""Contract mirror of the module-level ``_BACKEND`` normalization: the env
value is compared case-insensitively and whitespace-trimmed."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,28 @@
# Make submodules importable as attributes (``rocisa.code`` etc.). The
# rocisa dispatcher in ``tensilelite/rocisa/rocisa/__init__.py`` is what
# ultimately installs them under the ``rocisa.*`` name in ``sys.modules``.
#
# Heavy submodules (code, asmpass, functions, container, instruction, …) are
# loaded lazily via ``__getattr__`` so that *importing this package* does not
# pull in ``_stinkytofu.so``. This matters when a multiprocessing worker
# unpickles an adapter-typed object (e.g. ``base.IsaInfo``) while native
# ``_rocisa.so`` is already loaded: eager import would trigger a fatal
# nanobind duplicate-type abort.
from . import asmpass as asmpass
from . import base as base

_LAZY_SUBMODULES = frozenset({
"asmpass", "code", "container", "enum", "functions",
"instruction", "label", "macro", "register",
})

def __getattr__(name: str):
if name in _LAZY_SUBMODULES:
import importlib, sys as _sys
mod = importlib.import_module(f".{name}", __name__)
globals()[name] = mod
# Wire under ``rocisa.*`` so ``from rocisa.code import Module``
# finds the same object (the adapter is installed as ``rocisa``).
_sys.modules.setdefault(f"rocisa.{name}", mod)
return mod
if name in ("toStinkyTofuModule", "StinkyAsmModule", "CloneSpec"):
_ensure_stinky_types()
val = globals()[name]
return val
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
from . import code as code
from . import container as container
from . import enum as enum
from . import functions as functions
from . import instruction as instruction
from . import label as label
from . import macro as macro
from . import register as register
from .stinky_interop import toStinkyTofuModule

_P = "rocisa"

# Lazy-loaded on first access (avoids pulling in _stinkytofu.so at import time).
_stinky_types_loaded = False
try:
import stinkytofu as _stinkytofu # type: ignore[import-not-found]

def _ensure_stinky_types():
global _stinky_types_loaded
if _stinky_types_loaded:
return
_stinky_types_loaded = True
from .stinky_interop import toStinkyTofuModule as _tsm
globals()["toStinkyTofuModule"] = _tsm
try:
import stinkytofu as _st
globals()["StinkyAsmModule"] = _st.StinkyAsmModule
globals()["CloneSpec"] = _st.CloneSpec
except ImportError:
globals()["StinkyAsmModule"] = make_dummy_class(f"{_P}.StinkyAsmModule")
globals()["CloneSpec"] = make_dummy_class(f"{_P}.CloneSpec")
StinkyAsmModule = _stinkytofu.StinkyAsmModule
CloneSpec = _stinkytofu.CloneSpec
except ImportError:
StinkyAsmModule = make_dummy_class(f"{_P}.StinkyAsmModule")
CloneSpec = make_dummy_class(f"{_P}.CloneSpec")


# ---------------------------------------------------------------------------
Expand Down
Loading