Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: the AMD engine's environment: a HIP_PATH that points nowhere is dropped, an unwritable TEMP is replaced (#654)

Niko12210e773c8624d9
diff --git a/serve/server.py b/serve/server.py
index 58893a7cb..07e0588b6 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -2021,9 +2021,52 @@ def child_env(cfg: dict) -> dict:
if dirs:
var = "PATH" if os.name == "nt" else "LD_LIBRARY_PATH"
env[var] = os.pathsep.join(dirs + ([env[var]] if env.get(var) else []))
+ if cfg.get("backend") == "hip":
+ hip_env_guard(env, cfg)
return env
+def _dir_writable(p: str) -> bool:
+ try:
+ with tempfile.TemporaryFile(dir=p):
+ return True
+ except OSError:
+ return False
+
+
+def hip_env_guard(env: dict, cfg: dict) -> list[str]:
+ """#654: two things in an AMD user's environment that crash the HIP runtime before it prints a line. (1) HIP_PATH,
+ HIP_DEVICE_LIB_PATH or LLVM_PATH naming a folder that is gone (a ROCm build that was deleted): the bundled runtime
+ follows them. They are dropped, and only when the folder does not exist. (2) A TEMP / TMP / TMPDIR the engine cannot
+ create a file in: the runtime's comgr JIT builds its blit kernels through temporary files and the first call
+ crashes in amdhip64 (0xC0000005). A writable folder next to the engine's log takes its place. Returns what it
+ changed (said once, on stderr); a healthy environment is left exactly as it was."""
+ said: list[str] = []
+ for k in ("HIP_PATH", "HIP_DEVICE_LIB_PATH", "LLVM_PATH"):
+ v = env.get(k)
+ if v and not Path(v).exists():
+ del env[k]
+ said.append(f"{k}={v} does not exist: not passed to the engine")
+ bad = [k for k in ("TEMP", "TMP", "TMPDIR") if env.get(k) and not _dir_writable(env[k])]
+ if bad:
+ base = Path(cfg.get("log") or cfg.get("cwd") or ROOT).resolve()
+ for d in (base.parent / "tmp", ROOT / "tmp"):
+ try:
+ d.mkdir(parents=True, exist_ok=True)
+ except OSError:
+ continue
+ if _dir_writable(str(d)):
+ for k in bad:
+ said.append(f"{k}={env[k]} is not writable: using {d}")
+ env[k] = str(d)
+ break
+ else:
+ said.append(f"{', '.join(bad)} not writable (AMD's runtime needs a writable temporary folder)")
+ for line in said:
+ print(f"strata serve: AMD: {line}", file=sys.stderr, flush=True)
+ return said
+
+
def vision_env(cfg: dict, env: dict) -> dict:
"""The image encoder's environment: the engine's, unless the config's vision section names its own "cuda_device"
(numbered like nvidia-smi) - then the encoder runs on that card alone, so a spare GPU can hold it while the engine
diff --git a/serve/test_server.py b/serve/test_server.py
index 5f2d75c9d..427f0aa2d 100644
--- a/serve/test_server.py
+++ b/serve/test_server.py
@@ -1400,6 +1400,52 @@ class GpuChoice(unittest.TestCase):
self.assertEqual(plain.get("HIP_VISIBLE_DEVICES"), os.environ.get("HIP_VISIBLE_DEVICES"))
+class HipEnvGuard(unittest.TestCase):
+ """#654: a HIP_PATH that points nowhere and an unwritable TEMP crash the AMD runtime; both are repaired, a healthy
+ environment is not touched."""
+
+ def test_stale_hip_path_dropped(self):
+ import tempfile
+ from serve.server import hip_env_guard
+ with tempfile.TemporaryDirectory() as d:
+ gone = os.path.join(d, "TheRock", "build")
+ env = {"HIP_PATH": gone, "HIP_DEVICE_LIB_PATH": gone, "LLVM_PATH": d}
+ said = hip_env_guard(env, {"log": os.path.join(d, "x.log")})
+ self.assertNotIn("HIP_PATH", env)
+ self.assertNotIn("HIP_DEVICE_LIB_PATH", env)
+ self.assertEqual(env["LLVM_PATH"], d) # exists: left alone
+ self.assertEqual(len(said), 2)
+
+ def test_unwritable_temp_replaced(self):
+ import tempfile
+ from serve.server import hip_env_guard
+ with tempfile.TemporaryDirectory() as d:
+ nothing = os.path.join(d, "no", "such", "dir")
+ env = {"TEMP": nothing, "TMP": d}
+ hip_env_guard(env, {"log": os.path.join(d, "x.log")})
+ self.assertEqual(env["TMP"], d) # writable: untouched
+ self.assertTrue(os.path.samefile(env["TEMP"], os.path.join(d, "tmp")))
+ self.assertTrue(os.path.isdir(env["TEMP"]))
+
+ def test_healthy_untouched(self):
+ import tempfile
+ from serve.server import hip_env_guard
+ with tempfile.TemporaryDirectory() as d:
+ env = {"HIP_PATH": d, "TEMP": d}
+ before = dict(env)
+ self.assertEqual(hip_env_guard(env, {}), [])
+ self.assertEqual(env, before)
+
+ def test_only_for_hip(self):
+ from serve.server import child_env
+ os.environ["HIP_PATH"] = r"Z:\definitely\not\here"
+ try:
+ self.assertEqual(child_env({}).get("HIP_PATH"), os.environ["HIP_PATH"])
+ self.assertNotIn("HIP_PATH", child_env({"backend": "hip"}))
+ finally:
+ del os.environ["HIP_PATH"]
+
+
class RecordingPrompt(MockEngine):
def generate(self, ids, max_new, sampling, cancel, embeddings=None):
self.last_ids = list(ids)