Skip to content
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

Set non_blocking=True When moving data from the CPU to the GPU #36384

Open
2 of 4 tasks
Hukongtao opened this issue Feb 25, 2025 · 3 comments · May be fixed by #36408
Open
2 of 4 tasks

Set non_blocking=True When moving data from the CPU to the GPU #36384

Hukongtao opened this issue Feb 25, 2025 · 3 comments · May be fixed by #36408
Labels

Comments

@Hukongtao
Copy link

System Info

No Need

Who can help?

No response

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

I used the transformer's Trainer to train the model, but used my own Dataloader.
Then I used pytorch profile to check my training performance and found that the CPU execution time accounted for a high proportion

Image

After a period of investigation, it was found that the non_blocking was not set when the data was transferred from the CPU to the GPU.
https://github.com/huggingface/transformers/blob/v4.49.0/src/transformers/trainer.py#L3625-L3631
The modified code is:

kwargs = {"device": self.args.device,“non_blocking”:  True}

Then I re-profiled my code and the results were as follows:

Image

You can see that the performance has been greatly improved.

I'm not sure if this is a bug in the code or a problem with the way I'm using it.

But there is no doubt that setting non_blocking=True has brought a great performance improvement to my training.

Looking forward to your reply

Expected behavior

No Need

@Hukongtao Hukongtao added the bug label Feb 25, 2025
@Hukongtao
Copy link
Author

If this is a bug, do you have plans to fix it?

@Rocketknight1
Copy link
Member

cc @muellerzr @SunMarc

@Hukongtao Hukongtao linked a pull request Feb 26, 2025 that will close this issue
5 tasks
@Hukongtao
Copy link
Author

I submitted a #36408, can you check it for me? @muellerzr @SunMarc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants