Skip to content

Fix KeyError when GPU is missing from accelerate max_memory#1457

Merged
wenhuach21 merged 12 commits intomainfrom
lvl/fix_gpu_memory_keyerror
Feb 25, 2026
Merged

Fix KeyError when GPU is missing from accelerate max_memory#1457
wenhuach21 merged 12 commits intomainfrom
lvl/fix_gpu_memory_keyerror

Conversation

@lvliang-intel
Copy link
Copy Markdown
Contributor

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

  • Bug fix
  • New feature
  • Documentation update
  • Performance improvement
  • Code refactoring
  • Other (please specify):

Related Issues

#1451

Checklist Before Submitting

  • My code has been tested locally.
  • Documentation has been updated as needed.
  • New or updated tests are included where applicable.

Signed-off-by: lvliang-intel <liang1.lv@intel.com>
Signed-off-by: lvliang-intel <liang1.lv@intel.com>
Copilot AI review requested due to automatic review settings February 21, 2026 15:46
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 avoid KeyError, 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().

lvliang-intel and others added 4 commits February 22, 2026 10:13
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 wenhuach21 requested a review from n1ck-guo February 25, 2026 02:02
@wenhuach21 wenhuach21 merged commit b697e1d into main Feb 25, 2026
29 checks passed
@wenhuach21 wenhuach21 deleted the lvl/fix_gpu_memory_keyerror branch February 25, 2026 11:29
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants