Skip to content

Add Swin Transformer Example #1346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 23, 2025
Merged

Add Swin Transformer Example #1346

merged 7 commits into from
Aug 23, 2025

Conversation

sumantro93
Copy link
Contributor

Solves part of #1131

@msaroufim

Copy link

netlify bot commented May 16, 2025

Deploy Preview for pytorch-examples-preview canceled.

Name Link
🔨 Latest commit e6b3313
🔍 Latest deploy log https://app.netlify.com/projects/pytorch-examples-preview/deploys/682d33fe7f760d000856e958

Comment on lines 175 to 176
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to use the accelerator API

Suggested change
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
use_accel = torch.accelerator.is_available()
device = torch.accelerator.current_accelerator() if use_accel else torch.device("cpu")
print(f"Using device: {device}")

Install dependencies:

```bash
pip install torch torchvision
Copy link
Contributor

Choose a reason for hiding this comment

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

Use requirements.txt to be consistent with other examples

Testing is done automatically after each epoch. To only test, run with:

```bash
python swin_transformer.py --epochs 0
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this should be 1 epoch

@sumantro93
Copy link
Contributor Author

@jafraustro , Thanks for the review. I've updated my PR. Please have a look and lemme know if something else is needed. :))

Copy link
Contributor

@jafraustro jafraustro left a comment

Choose a reason for hiding this comment

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

LGTM

@sumantro93
Copy link
Contributor Author

@jafraustro can this be merged ? or does it require something else from my end?

@jafraustro
Copy link
Contributor

Hi @msaroufim, could you give a look to this PR?

@@ -0,0 +1,2 @@
torch
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
torch
torch>=2.6

Due to usage of torch.accelerator.

@@ -192,6 +196,7 @@ function stop() {
word_language_model/model.pt \
gcn/cora/ \
gat/cora/ || error "couldn't clean up some files"
swin_transformer/swin_cifar10.pt || error "couldn't clean up some files"
Copy link
Contributor

Choose a reason for hiding this comment

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

It will be failing with "command swin_transformer/swin_cifar10.pt" not found. You need to add to the list. I can't suggest the change since it touches non-modified cmdline, but code should be:

gat/cora/ \
swin_transformer/swin_cifar10.pt || error "couldn't clean up some files"

I.e. line break on gat/cora/ line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, i ran the examples and yes I had to also change the swin to swin_transformer in Line 228

@@ -0,0 +1,207 @@
from __future__ import print_function
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems script does not actually use print_function. Can this be dropped?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in 5478049

torch.save(model.state_dict(), "swin_cifar10.pt")

if __name__ == '__main__':
main()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
main()
main()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in 5478049

@sumantro93
Copy link
Contributor Author

@dvrogozh Thanks for your reviwes, I've made the changes :)

sumantro93 and others added 2 commits May 21, 2025 07:31
Co-authored-by: Dmitry Rogozhkin <[email protected]>
Co-authored-by: Dmitry Rogozhkin <[email protected]>
@sumantro93
Copy link
Contributor Author

@dvrogozh I have fixed as suggested. Can you PTAL?

@sumantro93
Copy link
Contributor Author

@dvrogozh can this be merged ? I can start working on another example that way

Copy link
Contributor

@dvrogozh dvrogozh left a comment

Choose a reason for hiding this comment

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

LGTM

@msaroufim : can you, please, take a look?

@dvrogozh
Copy link
Contributor

@dvrogozh can this be merged ?

@sumantro93 : PR looks good to me now. But we need someone with merge rights to do the actual merge. I hope @msaroufim will have time to review it.

@sumantro93
Copy link
Contributor Author

Hey folks!! Any updates on this ? :)

@msaroufim msaroufim merged commit 892eca0 into pytorch:main Aug 23, 2025
6 checks passed
msaroufim added a commit that referenced this pull request Aug 23, 2025
msaroufim added a commit that referenced this pull request Aug 23, 2025
Revert "Add Swin Transformer Example  (#1346)"

This reverts commit 892eca0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants