Skip to content

fix(windows): make environment set from Rust reach MLX's getenv - #2805

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-2803-windows-native-env
Nov 1, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-2803-windows-native-env

Conversation

@inureyes

Copy link
Copy Markdown
Member

On Windows std::env::set_var does not update the C runtime's getenv, which MLX reads, so every default mlxcel writes for MLX was inert there. mlxcel_core::native_env also calls _wputenv_s, in the one UCRT that Rust, the bridge, MLX and the nvcc objects share (all MD_DynamicRelease, measured). Production writes go through it, and scripts/ci/check_native_env_writes.py fails CI on a direct one. Other platforms still only make the std::env call.

Audit

Variable Writer Native reader Effective on Windows before
MLX_USE_CUDA_GRAPHS loading/mod.rs device.cpp No, capture stayed on for Gemma 4
MLX_CUDA_GRAPH_CACHE_SIZE hardware.rs lru_cache.h No, 400
MLX_CUDA_SDPA_CACHE_SIZE hardware.rs lru_cache.h No, 256
MLX_MAX_OPS_PER_BUFFER cuda_graph_budget.rs, hardware.rs (Metal) get_graph_limits No, 20 for Laguna on 12.1
MLX_MAX_MB_PER_BUFFER cuda_graph_budget.rs get_graph_limits No, 25 for Laguna on 12.1
MLX_PTX_CACHE_DIR ensure_persistent_ptx_cache ptx_cache_dir_win.h (wide) Yes, since #2799
MLXCEL_QMM_TILE_M cuda_kernel_knobs.rs qmm_sm80.cu No
MLXCEL_QMV_MULTIROW_MAX_ROWS cuda_kernel_knobs.rs qmv.cu No

MLXCEL_KV_BOUNDARY_V_LAYERS and MLXCEL_PREFILL_CHUNK are read by Rust only; they use the helper so the guard has one rule. MLX_ENABLE_TF32 and the mlx_server.rs writes are test-only.

Behavior change on Windows

Gemma 4 runs with graph capture off, Laguna gets the 100 ops and 1000 "MB" budget on compute capability 12.1, and both cache capacities are 2000. The N1X serving A/B is run separately by the owner; no serving benchmark ran here.

Validation

On an RTX Spark N1X: 5 of 8 native_env tests fail without the _wputenv_s call and 8 of 8 pass with it, reading through the bridge and mlx::core::env::get_var. cuda_ptx_cache_trust_windows passes 22 of 22. RUST_LOG=mlxcel_core::native_env=debug showed the expected values in mlxcel and mlxcel-server for config-only llama, gemma4 and laguna directories, and an operator value still wins. Not run: POSIX locally (CI covers it), weights on these paths, and a cache grown past 400 entries.

Closes #2803

On Windows `std::env::set_var` only calls `SetEnvironmentVariableW`, and the C runtime's `getenv`, which MLX reads, keeps its own copies. Every default mlxcel writes for MLX was inert there: the graph and SDPA cache capacities, the per-family graph budget, the Gemma 4 graph capture switch and the tuned kernel knobs.

`mlxcel_core::native_env` also calls `_wputenv_s`, in the one UCRT that Rust, the bridge, MLX and the nvcc objects share (all `/MD`). Production writes go through it, and `check_native_env_writes.py` fails CI on a direct one. Other platforms still only make the `std::env` call.

Tests read back through the bridge and `mlx::core::env::get_var`; 5 of 8 fail without the `_wputenv_s` call. A debug line reports each audited variable as native code reads it.

Closes #2803
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:windows Windows (native) specific labels Oct 31, 2026
@inureyes
inureyes merged commit f1839fd into main Nov 1, 2026
32 checks passed
@inureyes
inureyes deleted the fix/issue-2803-windows-native-env branch November 1, 2026 12:42
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:windows Windows (native) specific priority:high High priority status:review Under review type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(windows): environment set from Rust does not reach MLX's getenv

1 participant