Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(backends): read LAYA_INDUCTOR_CACHE_DIR stripped, so a padded path is the path

aashish254b0bb484903fa
diff --git a/laya/backends/compile.py b/laya/backends/compile.py
index 7b45e336..ac48dab8 100644
--- a/laya/backends/compile.py
+++ b/laya/backends/compile.py
@@ -44,8 +44,15 @@ def configure_inductor_cache() -> str:
Returns the directory in use. Inductor reads `TORCHINDUCTOR_CACHE_DIR` when it opens the
cache, so this must run before the first compile; an explicit `TORCHINDUCTOR_CACHE_DIR` from
the environment is left alone.
+
+ `LAYA_INDUCTOR_CACHE_DIR` is stripped at the read site the way `warmup_enabled` below reads
+ `LAYA_COMPILE_WARMUP`, so a padded path means the path and a blank one means unset.
+ `expanduser` anchors `~` to the first character, and a value that is not absolute lands under
+ the current working directory, so an unstripped `" /tmp/laya-inductor-cache "` is neither: it
+ resolves to a directory of that name *under the cwd*, and the next process -- started from
+ anywhere else -- compiles from scratch while reporting success.
"""
- cache_dir = os.path.expanduser(os.environ.get(CACHE_DIR_ENV) or DEFAULT_CACHE_DIR)
+ cache_dir = os.path.expanduser(os.environ.get(CACHE_DIR_ENV, "").strip() or DEFAULT_CACHE_DIR)
os.environ.setdefault("TORCHINDUCTOR_CACHE_DIR", cache_dir)
os.environ.setdefault("TORCHINDUCTOR_FX_GRAPH_CACHE", "1")
os.environ.setdefault("TORCHINDUCTOR_AUTOGRAD_CACHE", "1")
diff --git a/tests/test_backends.py b/tests/test_backends.py
index 3bf3ad0f..41a69501 100644
--- a/tests/test_backends.py
+++ b/tests/test_backends.py
@@ -249,6 +249,55 @@ check("compile/warmup failure falls back", be.name, "eager")
check_true("compile/warmup failure restores stock", untouched(cuda_agent.model))
check("compile/warmup failure warns once", len(w), 1)
+# ------------------------------------------------------------------ inductor cache directory
+# `configure_inductor_cache` is the only reader of `LAYA_INDUCTOR_CACHE_DIR`, so what it resolves is
+# what an operator's environment has to say for the documented directory to be the one in use.
+# Nothing below needs a GPU, a checkpoint or a compiler: the function only reads the environment and
+# returns inductor's own normalisation of it.
+from laya.backends.compile import CACHE_DIR_ENV, DEFAULT_CACHE_DIR, configure_inductor_cache # noqa: E402
+
+EXPANDED_DEFAULT = os.path.expanduser(DEFAULT_CACHE_DIR)
+ABS_DIR = os.path.join(os.sep, "tmp", "laya-inductor-cache")
+
+
+def resolved_cache_dir(laya_value, torchinductor_value=None):
+ """The directory `configure_inductor_cache` resolves to for one pair of variables.
+
+ `patch.dict` restores every key the function writes with `setdefault`, not just the ones it was
+ handed. And by this point the suite has already installed a compile backend above, which put a
+ `TORCHINDUCTOR_CACHE_DIR` in the environment -- it has to go first, or `setdefault` keeps that
+ value for every case below and each case measures the same directory.
+ """
+ given = {} if laya_value is None else {CACHE_DIR_ENV: laya_value}
+ if torchinductor_value is not None:
+ given["TORCHINDUCTOR_CACHE_DIR"] = torchinductor_value
+ with patch.dict(os.environ, given, clear=False):
+ if torchinductor_value is None:
+ os.environ.pop("TORCHINDUCTOR_CACHE_DIR", None)
+ return configure_inductor_cache()
+
+
+check("inductor cache/unset uses the documented default", resolved_cache_dir(None), EXPANDED_DEFAULT)
+for blank in ("", " ", "\t", " \n "):
+ check("inductor cache/blank %r reads as unset" % blank, resolved_cache_dir(blank), EXPANDED_DEFAULT)
+check("inductor cache/absolute path kept", resolved_cache_dir(ABS_DIR), ABS_DIR)
+check("inductor cache/padded absolute path", resolved_cache_dir(" %s " % ABS_DIR), ABS_DIR)
+check("inductor cache/tilde expanded", resolved_cache_dir("~/laya-inductor-cache"),
+ os.path.expanduser("~/laya-inductor-cache"))
+check("inductor cache/padded tilde expanded", resolved_cache_dir(" ~/laya-inductor-cache "),
+ os.path.expanduser("~/laya-inductor-cache"))
+# A relative value is the one case the fix must not "help": it stays relative, padding and all.
+relative = os.path.join("laya", "inductor-relative")
+resolved_relative = resolved_cache_dir(" %s " % relative)
+check_true("inductor cache/padded relative path resolves under the cwd",
+ os.path.abspath(resolved_relative) == os.path.join(os.getcwd(), relative),
+ "resolved %r" % (resolved_relative,))
+# The documented precedence: a `TORCHINDUCTOR_*` export belongs to the operator, not to this knob.
+check("inductor cache/operator TORCHINDUCT_CACHE_DIR wins",
+ resolved_cache_dir(ABS_DIR, ABS_DIR + "-operator"), ABS_DIR + "-operator")
+check("inductor cache/operator wins over a blank LAYA value",
+ resolved_cache_dir("", ABS_DIR + "-operator"), ABS_DIR + "-operator")
+
# ------------------------------------------------------------------ forwarding and legacy Agent substitutes
from laya.router import Router # noqa: E402