diff --git a/AGENTS.md b/AGENTS.md index 8fb30b389..075637b8d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -458,15 +458,20 @@ architectural boundary before committing the change. specifies. A setting that only experiments need is an advanced flag carried through the plan into the recipe's `PreparationConfig`, so the plan, its printed report, and its recorded measurements describe what was prepared. -- `CUDA_VISIBLE_DEVICES` is the only variable Flare interprets: it renumbers the - GPUs that `--gpus` indexes. Before starting Helios, Flare narrows that mask to - the selected GPUs and passes contiguous local ordinals to the runtime. It - sets `CUDA_FORCE_PRELOAD_LIBRARIES=0` and `CUDA_DISABLE_JIT=1` as fixed driver +- The only environment variables Flare inspects belong to its documented CUDA + device selection and preparation dependency setup policies. + `CUDA_VISIBLE_DEVICES` renumbers the GPUs that `--gpus` indexes; before + starting Helios, Flare narrows that mask to the selected GPUs and passes + contiguous local ordinals to the runtime. It sets + `CUDA_FORCE_PRELOAD_LIBRARIES=0` and `CUDA_DISABLE_JIT=1` as fixed driver policy: serving loads compiled cubins without driver compilation. - `ignition.cli.environment` sets the variables that - PyTorch, b12x, Triton, and the Hugging Face hub read, from Flare's - configuration, before importing them. Detecting a terminal's capabilities is - not configuration. + `ignition.cli.environment` sets the variables that PyTorch, b12x, Triton, and + the Hugging Face hub read, from Flare's configuration, before importing them; + on non-Windows hosts, it also preserves a non-empty inherited `CUDA_HOME` when + `[paths] cuda` is unset (before falling back to `/usr/local/cuda`), keeps a + pre-set `TRITON_PTXAS_PATH`, and preserves existing `CPATH` entries, as + specified in [the CLI design](docs/cli-design.md). Detecting a terminal's + capabilities is not configuration. - Tests may read variables that opt into GPU tests and locate their checkpoints, such as `FLARE_RUN_CUDA_TESTS`. A test variable must not change what the code under test does. diff --git a/docs/cli-design.md b/docs/cli-design.md index a1de8c84d..034dca33d 100644 --- a/docs/cli-design.md +++ b/docs/cli-design.md @@ -43,14 +43,23 @@ Anthropic Messages, stateless Responses, authentication, and client configuratio free during preparation, and `flare run` prints what it allocated. An estimate that does not fit fails before any socket opens, with the numbers and the settings that would make it fit. -- **No environment variables.** Flare reads none, with one exception: - `CUDA_VISIBLE_DEVICES` restricts which GPUs the caller exposed, which changes - the GPU each index names, so Flare inspects it to interpret `--gpus`. - Variables that underlying libraries read are set by Flare from its own - configuration, and b12x's from the recipe's declared settings, before those - libraries are imported. -- **Two levels of precedence.** Flags override the configuration file, which - overrides the recipe. Nothing else exists. +- **No environment-variable behavior switches.** Flare reads environment + variables only for CUDA device selection and non-Windows preparation toolkit + dependency setup: `CUDA_VISIBLE_DEVICES` restricts which GPUs the caller + exposed, which changes the GPU each index names, so Flare inspects it to + interpret `--gpus`; on non-Windows hosts, when `[paths] cuda` is unset, + `ignition.cli.environment` preserves a non-empty inherited `CUDA_HOME` before + falling back to `/usr/local/cuda` (removing blank `CUDA_HOME` values when no + toolkit source exists), and for any resolved toolkit it keeps a pre-set + `TRITON_PTXAS_PATH` and prepends `/include` to `CPATH` when not + already listed. Other variables that underlying libraries read are set by + Flare from its own configuration, and b12x's from the recipe's declared + settings, before those libraries are imported. +- **Two levels of precedence.** For Flare settings, flags override the + configuration file, which overrides the recipe; no environment-variable + settings layer exists. Host CUDA toolkit discovery on non-Windows hosts falls + back through inherited `CUDA_HOME` and `/usr/local/cuda` only when + `[paths] cuda` is unset. ## 2. Commands @@ -1477,7 +1486,7 @@ Configuration assigns each decision to one owner: | Compiler workers | Automatic memory-bounded sizing; `[preparation] compile_workers` or `--compile-workers` overrides | | Kernel search effort | `--autotune-effort`, then `[preparation] autotune_effort`, otherwise `default` | | Local tuning replicas | `--tuning-gpus`; independent of serving TP size | -| CUDA toolkit | `[paths] cuda`, otherwise native build libraries discover the system toolkit | +| CUDA toolkit | `[paths] cuda`; otherwise on non-Windows hosts non-empty inherited `CUDA_HOME`, then `/usr/local/cuda` when present; otherwise native build libraries discover the system toolkit (on non-Windows hosts, a resolved toolkit also populates unset `TRITON_PTXAS_PATH` and prepends `/include` to `CPATH`) | | Served names | Recipe canonical model name as the serving alias, plus the checkpoint repository ID, or a local checkpoint directory's name, as the model ID, exposed through `/v1/models`; native `--served-model-name` replaces the alias | | API key | `[server] api_key`, overridden by native `--api-key-file`; excluded from cores | | Sampling distribution controls | Recipe defaults and optional model-author ranges; request values clamp to ranges, or to defaults when no range exists | diff --git a/docs/model-preparation.md b/docs/model-preparation.md index 3a3c5a6a0..04f79be01 100644 --- a/docs/model-preparation.md +++ b/docs/model-preparation.md @@ -120,9 +120,10 @@ to compile the embedded GPU KV-cache codec module (`kv_codec_gpu.cu`) for cores that declare a codec layout. Compiled cubins are cached under `[paths] kernel_cache/kv_codec` when configured, falling back to a private mode-`0700` user directory under `$TMPDIR` (or a fresh temporary directory) when -`kernel_cache` is unset or unwritable. Native build libraries discover the -system toolkit; `[paths] cuda` overrides that discovery with an explicit toolkit -directory. +`kernel_cache` is unset or unwritable. `[paths] cuda` selects an explicit +toolkit directory; when unset, non-Windows hosts follow the `CUDA_HOME` and +`/usr/local/cuda` fallback rules in [the CLI design](cli-design.md) before +native build libraries discover the system toolkit. The `prepare` extra installs `cuda-tile[tileiras]`, including the `tileiras` compiler toolchain required by cuTile kernels during model preparation. diff --git a/ignition/cli/environment.py b/ignition/cli/environment.py index 34488c241..47bf671c3 100644 --- a/ignition/cli/environment.py +++ b/ignition/cli/environment.py @@ -19,6 +19,7 @@ from ignition import runtime_patches _DLL_DIRECTORIES = [] +_DEFAULT_LINUX_CUDA = Path("/usr/local/cuda") def flashinfer_workspace(kernel_cache: Path) -> Path: @@ -51,9 +52,16 @@ def apply(configuration, gpus, b12x=()) -> None: **dict(b12x), }) # cuTile discovers its compiler bundle independently of the native toolkit. + # Non-Windows CUDA toolkit precedence is specified in docs/cli-design.md. cuda = configuration.directory("cuda") - if cuda is None and os.name != "nt" and Path("/usr/local/cuda").is_dir(): - cuda = Path("/usr/local/cuda") + if cuda is None and os.name != "nt": + inherited_cuda = os.environ.get("CUDA_HOME", "").strip() + if inherited_cuda: + cuda = Path(inherited_cuda) + elif _DEFAULT_LINUX_CUDA.is_dir(): + cuda = _DEFAULT_LINUX_CUDA + else: + os.environ.pop("CUDA_HOME", None) if cuda is not None: os.environ["CUDA_HOME"] = str(cuda) if os.name == "nt": diff --git a/tests/cli/test_environment.py b/tests/cli/test_environment.py new file mode 100644 index 000000000..8fb4b8dfa --- /dev/null +++ b/tests/cli/test_environment.py @@ -0,0 +1,175 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-FileCopyrightText: Copyright (c) 2026 Flare community contributors +# SPDX-License-Identifier: Apache-2.0 + +"""CUDA toolkit environment selection and precedence in ignition.cli.environment.""" + +import os +from pathlib import Path + +import pytest + +from ignition.cli import environment +from ignition.cli.config import Configuration + +pytestmark = pytest.mark.skipif( + os.name == "nt", + reason="POSIX CUDA toolkit environment precedence", +) + +_ENV_KEYS = ( + "TORCH_CUSTOM_PYTHONPATH", + "CUDA_VISIBLE_DEVICES", + "HF_HUB_CACHE", + "B12X_COMPILE_CACHE_DIR", + "FLASHINFER_WORKSPACE_BASE", + "TRITON_CACHE_DIR", + "TORCHINDUCTOR_CACHE_DIR", + "CUDA_HOME", + "CUDA_PATH", + "TRITON_PTXAS_PATH", + "TRITON_PTXAS.EXE_PATH", + "CPATH", +) + + +def _make_toolkit(root: Path) -> Path: + (root / "bin").mkdir(parents=True) + (root / "bin" / "ptxas").write_text("") + (root / "include").mkdir() + return root + + +def _load_config(tmp_path: Path, cuda: Path | None = None) -> Configuration: + path = tmp_path / "config.toml" + text = f'[paths]\ncuda = "{cuda.as_posix()}"\n' if cuda is not None else "" + path.write_text(text) + return Configuration.load(path) + + +@pytest.fixture +def clean_environment(monkeypatch, tmp_path): + monkeypatch.setattr(environment.runtime_patches, "apply", lambda: None) + monkeypatch.setenv("HOME", str(tmp_path)) + monkeypatch.setenv("USERPROFILE", str(tmp_path)) + # Register an undo entry for every managed variable, including keys absent + # before the test, so mutations inside environment.apply never leak across tests. + for key in _ENV_KEYS: + if key in os.environ: + monkeypatch.delenv(key) + else: + monkeypatch.setenv(key, "") + os.environ.pop(key, None) + yield tmp_path + + +def test_configured_cuda_overrides_inherited_cuda_home_and_default_toolkit(clean_environment, monkeypatch): + configured = _make_toolkit(clean_environment / "configured-cuda") + inherited = _make_toolkit(clean_environment / "inherited-cuda") + default = _make_toolkit(clean_environment / "default-cuda") + monkeypatch.setattr(environment, "_DEFAULT_LINUX_CUDA", default) + monkeypatch.setenv("CUDA_HOME", str(inherited)) + monkeypatch.setenv("CPATH", "/existing/include") + + environment.apply(_load_config(clean_environment, cuda=configured), ()) + + assert os.environ["CUDA_HOME"] == str(configured) + assert os.environ["TRITON_PTXAS_PATH"] == str(configured / "bin/ptxas") + assert os.environ["CPATH"] == f"{configured / 'include'}{os.pathsep}/existing/include" + + +def test_configured_cuda_keeps_preset_ptxas(clean_environment, monkeypatch): + configured = _make_toolkit(clean_environment / "configured-cuda") + inherited = _make_toolkit(clean_environment / "inherited-cuda") + default = _make_toolkit(clean_environment / "default-cuda") + monkeypatch.setattr(environment, "_DEFAULT_LINUX_CUDA", default) + monkeypatch.setenv("CUDA_HOME", str(inherited)) + monkeypatch.setenv("TRITON_PTXAS_PATH", "/ambient/ptxas") + + environment.apply(_load_config(clean_environment, cuda=configured), ()) + + assert os.environ["CUDA_HOME"] == str(configured) + assert os.environ["TRITON_PTXAS_PATH"] == "/ambient/ptxas" + assert os.environ["CPATH"] == str(configured / "include") + + +def test_inherited_cuda_home_is_preserved_over_default_toolkit(clean_environment, monkeypatch): + inherited = _make_toolkit(clean_environment / "inherited-cuda") + default = _make_toolkit(clean_environment / "default-cuda") + monkeypatch.setattr(environment, "_DEFAULT_LINUX_CUDA", default) + monkeypatch.setenv("CUDA_HOME", str(inherited)) + + environment.apply(_load_config(clean_environment), ()) + + assert os.environ["CUDA_HOME"] == str(inherited) + assert os.environ["TRITON_PTXAS_PATH"] == str(inherited / "bin/ptxas") + assert os.environ["CPATH"] == str(inherited / "include") + + +def test_whitespace_padded_inherited_cuda_home_is_normalized(clean_environment, monkeypatch): + inherited = _make_toolkit(clean_environment / "inherited-cuda") + default = _make_toolkit(clean_environment / "default-cuda") + monkeypatch.setattr(environment, "_DEFAULT_LINUX_CUDA", default) + monkeypatch.setenv("CUDA_HOME", f" {inherited} ") + + environment.apply(_load_config(clean_environment), ()) + + assert os.environ["CUDA_HOME"] == str(inherited) + assert os.environ["TRITON_PTXAS_PATH"] == str(inherited / "bin/ptxas") + assert os.environ["CPATH"] == str(inherited / "include") + + +def test_inherited_cuda_home_keeps_preset_ptxas_and_existing_cpath_entry(clean_environment, monkeypatch): + inherited = _make_toolkit(clean_environment / "inherited-cuda") + default = _make_toolkit(clean_environment / "default-cuda") + monkeypatch.setattr(environment, "_DEFAULT_LINUX_CUDA", default) + monkeypatch.setenv("CUDA_HOME", str(inherited)) + monkeypatch.setenv("TRITON_PTXAS_PATH", "/custom/ptxas") + existing_cpath = f"/other/include{os.pathsep}{inherited / 'include'}" + monkeypatch.setenv("CPATH", existing_cpath) + + environment.apply(_load_config(clean_environment), ()) + + assert os.environ["CUDA_HOME"] == str(inherited) + assert os.environ["TRITON_PTXAS_PATH"] == "/custom/ptxas" + assert os.environ["CPATH"] == existing_cpath + + +def test_inherited_cuda_home_without_local_files_does_not_fall_back_to_default(clean_environment, monkeypatch): + inherited = clean_environment / "custom-cuda" + default = _make_toolkit(clean_environment / "default-cuda") + monkeypatch.setattr(environment, "_DEFAULT_LINUX_CUDA", default) + monkeypatch.setenv("CUDA_HOME", str(inherited)) + + environment.apply(_load_config(clean_environment), ()) + + assert os.environ["CUDA_HOME"] == str(inherited) + assert "TRITON_PTXAS_PATH" not in os.environ + assert "CPATH" not in os.environ + + +@pytest.mark.parametrize("inherited", [None, "", " "]) +def test_default_linux_toolkit_used_when_config_and_cuda_home_absent(clean_environment, monkeypatch, inherited): + default = _make_toolkit(clean_environment / "default-cuda") + monkeypatch.setattr(environment, "_DEFAULT_LINUX_CUDA", default) + if inherited is not None: + monkeypatch.setenv("CUDA_HOME", inherited) + + environment.apply(_load_config(clean_environment), ()) + + assert os.environ["CUDA_HOME"] == str(default) + assert os.environ["TRITON_PTXAS_PATH"] == str(default / "bin/ptxas") + assert os.environ["CPATH"] == str(default / "include") + + +@pytest.mark.parametrize("inherited", [None, "", " "]) +def test_cuda_home_stays_unset_when_no_toolkit_source_exists(clean_environment, monkeypatch, inherited): + monkeypatch.setattr(environment, "_DEFAULT_LINUX_CUDA", clean_environment / "missing-cuda") + if inherited is not None: + monkeypatch.setenv("CUDA_HOME", inherited) + + environment.apply(_load_config(clean_environment), ()) + + assert "CUDA_HOME" not in os.environ + assert "TRITON_PTXAS_PATH" not in os.environ + assert "CPATH" not in os.environ