Fix KeyError when GPU is missing from accelerate max_memory#1457
Merged
wenhuach21 merged 12 commits intomainfrom Feb 25, 2026
Merged
Fix KeyError when GPU is missing from accelerate max_memory#1457wenhuach21 merged 12 commits intomainfrom
wenhuach21 merged 12 commits intomainfrom
Conversation
Signed-off-by: lvliang-intel <liang1.lv@intel.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a crash during GPU→CPU fallback when accelerate.get_max_memory() omits GPU entries (e.g., under near-OOM conditions), ensuring caching/dispatch logic doesn’t assume GPU keys exist.
Changes:
- Normalize bare accelerator strings in
parse_available_devices()(e.g.,"cuda"→"cuda:0"). - Skip devices missing from
get_max_memory()to avoidKeyError, and trigger the existing CPU fallback when no non-CPU device remains.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| auto_round/utils/device.py | Normalizes bare device-type strings to indexed device strings. |
| auto_round/compressors/base.py | Guards max_memory[device] access and forces CPU fallback when GPUs are missing from get_max_memory(). |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: lvliang-intel <liang1.lv@intel.com>
Signed-off-by: lvliang-intel <liang1.lv@intel.com>
wenhuach21
reviewed
Feb 25, 2026
wenhuach21
reviewed
Feb 25, 2026
wenhuach21
approved these changes
Feb 25, 2026
Signed-off-by: lvliang-intel <liang1.lv@intel.com>
wenhuach21
reviewed
Feb 25, 2026
Signed-off-by: lvliang-intel <liang1.lv@intel.com>
for more information, see https://pre-commit.ci
lvliang-intel
added a commit
that referenced
this pull request
Feb 27, 2026
Signed-off-by: lvliang-intel <liang1.lv@intel.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Issue1451 #1451
Root Cause:
When GPU VRAM is nearly exhausted, accelerate's get_max_memory() silently drops the GPU from its return dict (e.g., returns {'cpu': ...} instead of {0: ..., 'cpu': ...}). The code then tries to access max_memory[0], causing a KeyError crash.
Solution:
Skip unavailable GPU devices with a continue guard before accessing max_memory[device]. When no GPU survives, raise torch.OutOfMemoryError to trigger the existing CPU fallback handler, avoiding unnecessary get_balanced_memory/dispatch_model overhead. Also normalize bare device strings (e.g., "cuda" → "cuda:0") in parse_available_devices to prevent hardcoding device = 0.
Type of Change
Related Issues
#1451
Checklist Before Submitting