Skip to content

Update torch AMP autocast syntax for CUDA compatibility#6267

Open
KanTakahiro wants to merge 3 commits into
espnet:masterfrom
KanTakahiro:kan_PR_250724
Open

Update torch AMP autocast syntax for CUDA compatibility#6267
KanTakahiro wants to merge 3 commits into
espnet:masterfrom
KanTakahiro:kan_PR_250724

Conversation

@KanTakahiro
Copy link
Copy Markdown
Contributor

What did you change?

I modified espnet2/enh/encoder/stft_encoder.py and espnet2/enh/layers/uses2_swin.py:

-    @torch.cuda.amp.autocast(enabled=False)
+    @torch.amp.autocast('cuda', enabled=False)

Why did you make this change?

There is a FutureWarning when I use espnet2:

~/.pyenv/myenv12/lib/python3.12/site-packages/espnet2/enh/encoder/stft_encoder.py:79: FutureWarning: torch.cuda.amp.autocast(args...) is deprecated. Please use torch.amp.autocast('cuda', args...) instead.
@torch.cuda.amp.autocast(enabled=False)
~/.pyenv/myenv12/lib/python3.12/site-packages/espnet2/enh/layers/uses2_swin.py:329: FutureWarning: torch.cuda.amp.autocast(args...) is deprecated. Please use torch.amp.autocast('cuda', args...) instead.


Is your PR small enough?

Yes


Additional Context

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. Enhancement Enhancement labels Oct 20, 2025
@mergify mergify Bot added the ESPnet2 label Oct 20, 2025
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly updates the deprecated torch.cuda.amp.autocast syntax to torch.amp.autocast('cuda', ...) in espnet2/enh/encoder/stft_encoder.py and espnet2/enh/layers/uses2_swin.py. The changes address a FutureWarning and are consistent with PyTorch's current API.

While the changes made are correct, a similar instance of the deprecated syntax was missed in espnet2/enh/layers/uses2_swin.py on line 147. To ensure all deprecation warnings of this type are resolved, I recommend updating that line as well:

# espnet2/enh/layers/uses2_swin.py:147
- with torch.cuda.amp.autocast(enabled=False):
+ with torch.amp.autocast('cuda', enabled=False):

By including this change, the pull request will be more complete. No other issues were found.

@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 20, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.39%. Comparing base (0459b11) to head (002e14e).
⚠️ Report is 1153 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master    #6267       +/-   ##
===========================================
+ Coverage   56.24%   69.39%   +13.14%     
===========================================
  Files         897      759      -138     
  Lines       84917    69849    -15068     
===========================================
+ Hits        47765    48473      +708     
+ Misses      37152    21376    -15776     
Flag Coverage Δ
test_integration_espnet2 46.96% <100.00%> (+1.06%) ⬆️
test_integration_espnetez ?
test_python_espnet2 62.07% <100.00%> (+11.17%) ⬆️
test_python_espnet3 16.06% <100.00%> (?)
test_python_espnetez ?
test_utils 62.07% <100.00%> (+43.30%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sw005320
Copy link
Copy Markdown
Contributor

Thanks a lot.
Actually, torch.cuda.amp.autocast seems to be used in various places, and we may need to consistently update it.
https://github.com/search?q=repo%3Aespnet%2Fespnet%20torch.cuda.amp.autocast&type=code

I also found several other potential issues with using torch.cuda.amp
https://github.com/search?q=repo%3Aespnet%2Fespnet%20torch.cuda.amp&type=code
E.g., it is imported with the version check, but we do not support lower versions than 1.6.0, and they are obsolete.
So, we can remove this version condition and directly import torch.amp

@Emrys365, what do you think?

@Fhrozen Fhrozen added this to the v.202512 milestone Oct 23, 2025
@Emrys365
Copy link
Copy Markdown
Collaborator

Thanks a lot. Actually, torch.cuda.amp.autocast seems to be used in various places, and we may need to consistently update it. https://github.com/search?q=repo%3Aespnet%2Fespnet%20torch.cuda.amp.autocast&type=code

I also found several other potential issues with using torch.cuda.amp https://github.com/search?q=repo%3Aespnet%2Fespnet%20torch.cuda.amp&type=code E.g., it is imported with the version check, but we do not support lower versions than 1.6.0, and they are obsolete. So, we can remove this version condition and directly import torch.amp

@Emrys365, what do you think?

Sorry for missing the discussion. I agree that we should update the deprecated torch.cuda.amp.autocast with torch.amp.autocast('cuda', ...).

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.
It will be closed if no further activity occurs.
Thank you for your contributions.

@github-actions github-actions Bot added Stale For probot and removed Stale For probot labels Mar 20, 2026
@Fhrozen Fhrozen modified the milestones: v.202604, v.202607 Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement Enhancement ESPnet2 size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants