Repository navigation
[Bugfix] Preserve user PYTORCH_CUDA_ALLOC_CONF keys in _scoped_allocator_max_split - #51096
Sahil170595 wants to merge 3 commits into
Conversation
…tor_max_split torch's AcceleratorAllocatorConfig::parseArgs resets max_split_size_mb, garbage_collection_threshold, and roundup_power2_divisions to defaults on every call, so passing only max_split_size_mb wiped any user-configured values for the other two keys during load_model and the restore path wiped them permanently. Snapshot the full allocator settings string via torch._C._accelerator_getAllocatorSettings() and restore it on exit. Signed-off-by: Sahil170595 <147995121+Sahil170595@users.noreply.github.com>
|
This pull request has merge conflicts that must be resolved before it can be |
|
Self-review summary before closing, since this is now superseded. What this fixed: Why it was a bug: torch's Status: #58430 landed the same snapshot / prepend / restore logic today while extending the scoped limit to the memory-profiling pass, and it covers cases this PR did not: ROCm, non-native allocator backends, the exception path (the override is applied inside the |
Purpose
_scoped_allocator_max_split(added in #41268) temporarily setsmax_split_size_mb:20aroundload_modelto reduce allocator fragmentation. PyTorch'sAcceleratorAllocatorConfig::parseArgs(c10/core/AllocatorConfig.cpp @ v2.13.0) resetsmax_split_size_mb,garbage_collection_threshold, androundup_power2_divisionsto defaults at the start of every call:So for a user running with, e.g.
PYTORCH_CUDA_ALLOC_CONF=garbage_collection_threshold:0.8,roundup_power2_divisions:4, the scoped override silently disabled both keys during weight loading, and the restore path — which passed back onlymax_split_size_mb— wiped them for the rest of the process lifetime. Reproduced against real torch:The same held for settings supplied via the newer
PYTORCH_ALLOC_CONFor set programmatically, which the env-var-based restore missed entirely (theirmax_split_size_mbwas also replaced by theSIZE_MAX // 2**20approximation instead of the user's value).The fix: snapshot the full settings string with
torch._C._accelerator_getAllocatorSettings()(it returns the storedlast_allocator_settings_string, so the round-trip is exact), append themax_split_size_mboverride for the scoped region (keys are parsed sequentially, so the appended override wins), and restore the snapshot on exit.parseArgs("")still performs the defaults reset, so the empty-snapshot case restores true defaults. Also drops the now-unusedregeximport.Found while auditing worker-startup state for #48518 (see my comment there); this PR is independent of that issue's L2 claim, which did not reproduce. Checked open PRs for
PYTORCH_CUDA_ALLOC_CONF/max_split_size_mb/ allocator-settings terms: existing PRs (#43923, #42405, #48243, #49151) touchexpandable_segmentsand CuMem pool behavior; none address this.Test Plan
New
test_scoped_allocator_max_split_preserves_alloc_confintests/v1/worker/test_gpu_worker.py: asserts user keys survive inside the scoped region alongside the override, the exact settings string is restored on exit, and the empty-settings case round-trips. Runs against the real torch allocator-settings bindings; no GPU allocation required.Test Result
(WSL2, Python 3.12.3, torch 2.13.0+cu130, editable
VLLM_USE_PRECOMPILEDinstall.) Negative control: withvllm/v1/worker/gpu_worker.pyreverted to main and the new test kept, the test fails; with the fix it passes.ruff check/ruff format --check(pre-commit pin v0.14.0) pass on both changed files.Model evaluation: not applicable — no change to model output, kernel selection, or numerics; only which allocator-config keys survive
load_model.This change was developed with AI assistance (Claude Fable 5). I have reviewed every changed line and run the tests above.