Skip to content

Fix AttributeError in gradient_checkpointing_enable(offload=True) - #48590

Merged
tarekziade merged 1 commit into
mainfrom
tarek/fix-gc-offload-no-accelerator
Sep 8, 2026
Merged

tarekziade merged 1 commit into
mainfrom
tarek/fix-gc-offload-no-accelerator

Conversation

@tarekziade

@tarekziade tarekziade commented Sep 7, 2026 •

Copy link
Copy Markdown
Collaborator

CPU CI GPU run-slow

What does this PR do?

torch.accelerator.current_accelerator() returns None when no accelerator is available, so .type raised AttributeError: 'NoneType' object has no attribute 'type' on CPU-only machines. Fall back to torch.device("cpu"), matching the idiom already used in quantizers/quantizer_mxfp4.py.

…hout an accelerator

`torch.accelerator.current_accelerator()` returns `None` when no accelerator is
available, so `.type` raised `AttributeError: 'NoneType' object has no attribute
'type'` on CPU-only machines. Fall back to `torch.device("cpu")`, matching the
idiom already used in `quantizers/quantizer_mxfp4.py`.
@tarekziade tarekziade self-assigned this Sep 7, 2026
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 34103855351
Result: success | Grafana metrics are not available yet.

@tarekziade

Copy link
Copy Markdown
Collaborator Author

@askserge review this fix

@sergereview sergereview Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤗 Serge says:

Minimal, correct fix.

  • Bug: On CPU-only hosts, torch.accelerator.current_accelerator() returns None, so .type raised AttributeError.
  • Fix: Fall back to torch.device("cpu").
  • Rationale: Activations already live on the host when there is no accelerator, matching the existing idiom in quantizers/quantizer_mxfp4.py.

serge v0.1.0 · model: moonshotai/Kimi-K2.7-Code · 4 LLM turns · 3 tool calls · 29.0s · 22220 in / 995 out tokens

@tarekziade
tarekziade added this pull request to the merge queue Sep 8, 2026
@tarekziade

Copy link
Copy Markdown
Collaborator Author

@qgallouedec we've merged to fix main because that broke make typing feel free to ping me back if the fix is not what you'd expect

Merged via the queue into main with commit 2dedac3 Sep 8, 2026
114 checks passed
@tarekziade
tarekziade deleted the tarek/fix-gc-offload-no-accelerator branch September 8, 2026 10:26
@qgallouedec

qgallouedec commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

Thks for the fix, no need to revert, but for the record I think it's the wrong shape.

No one trains on cpu on purpose, and if someone did and passed offload=True on purpose, what would they expect to offload to?
The fallback isn't free either: torch.cpu.is_available() is True, so save_on_cpu(pin_memory=True, device_type="cpu") allocates a pinned copy of every saved activation and does two host-to-host copies. So on cpu it now silently doubles activation memory for nothing instead of failing.

the test is @require_torch_accelerator, only make typing ever hit that line. if we want the checker quiet:

if (accelerator := torch.accelerator.current_accelerator()) is None:
    raise ValueError("`offload=True` requires an accelerator")
device_type = accelerator.type

Linting should be a bug catcher. Here it flagged a None nobody can reach meaningfully and we answered with a fallback we never want anyone to take.

@tarekziade

Copy link
Copy Markdown
Collaborator Author

Thks for the fix, no need to revert, but for the record I think it's the wrong shape.

No one trains on cpu on purpose, and if someone did and passed offload=True on purpose, what would they expect to offload to? The fallback isn't free either: torch.cpu.is_available() is True, so save_on_cpu(pin_memory=True, device_type="cpu") allocates a pinned copy of every saved activation and does two host-to-host copies. So on cpu it now silently doubles activation memory for nothing instead of failing.

the test is @require_torch_accelerator, only make typing ever hit that line. if we want the checker quiet:

if (accelerator := torch.accelerator.current_accelerator()) is None:
    raise ValueError("`offload=True` requires an accelerator")
device_type = accelerator.type

Linting should be a bug catcher. Here it flagged a None nobody can reach meaningfully and we answered with a fallback we never want anyone to take.

makes total sense. I don't think the ty AST can detect this. I guess an even simpler fix would be to revert and tell the linter to ignore that line.

sbucaille pushed a commit to sbucaille/transformers that referenced this pull request Sep 16, 2026
…ggingface#48590)

Fix AttributeError in gradient_checkpointing_enable(offload=True) without an accelerator

`torch.accelerator.current_accelerator()` returns `None` when no accelerator is
available, so `.type` raised `AttributeError: 'NoneType' object has no attribute
'type'` on CPU-only machines. Fall back to `torch.device("cpu")`, matching the
idiom already used in `quantizers/quantizer_mxfp4.py`.
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.

4 participants