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

[Idefics3] Move image features to same device as input embeds #35100

Merged

Conversation

shyshin
Copy link
Contributor

@shyshin shyshin commented Dec 5, 2024

What does this PR do?

Fixes #35031
Summary:
Got a device mismatch issue on running trainer.train().

File ~/.local/lib/python3.11/site-packages/transformers/models/idefics3/modeling_idefics3.py:904, in Idefics3Model.inputs_merger(self, input_ids, inputs_embeds, image_hidden_states)
    902 # cast to the dtype of the input_embeds to support quantized models
    903 reshaped_image_hidden_states = reshaped_image_hidden_states.to(inputs_embeds.dtype)
--> 904 new_inputs_embeds[special_image_token_mask] = reshaped_image_hidden_states
    905 return new_inputs_embeds

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1

Moved image_features to same device as input_embeds.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@zucchini-nlp
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

Copy link
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

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

LGTM, let's just move the device placement in the same line as
reshaped_image_hidden_states = reshaped_image_hidden_states.to(inputs_embeds.dtype). So that we assign the correct dtype/device in one line for more readability on what's going on

@shyshin
Copy link
Contributor Author

shyshin commented Dec 6, 2024

You want me to have a change like this?

reshaped_image_hidden_states = reshaped_image_hidden_states.to(inputs_embeds.dtype).to(self.device)
new_inputs_embeds[special_image_token_mask] = reshaped_image_hidden_states

I will try to run this on my multi device setup.

@zucchini-nlp
Copy link
Member

@shyshin you can do in one go as reshaped_image_hidden_states = reshaped_image_hidden_states.to(device=self.device, dtype=inputs_embeds.dtype) :)

@shyshin shyshin force-pushed the 35031-idefics3-device-mismatch-error branch 2 times, most recently from 485df3a to 31a5e10 Compare December 9, 2024 20:02
@shyshin
Copy link
Contributor Author

shyshin commented Dec 10, 2024

@zucchini-nlp I have updated the code with your suggestions

@HuggingFaceDocBuilderDev

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.

@zucchini-nlp
Copy link
Member

Seems like a tiny fix without need for core maintainer's approval, so maybe another review from @qubvel and let's merge

Copy link
Member

@qubvel qubvel left a comment

Choose a reason for hiding this comment

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

Hey @shyshin, thanks for the fix! What would be a self.device in case the model is split across multiple devices? Would it be safer to use new_inputs_embeds.device instead?

@zucchini-nlp
Copy link
Member

Hey @shyshin ! Would you be able to incorporate the suggested changes ? If you are busy, I can push the changes and merge the PR :)

@shyshin
Copy link
Contributor Author

shyshin commented Jan 7, 2025

@qubvel @zucchini-nlp I am sorry I am really swamped with work atm. It would be really nice if you could push the changes and merge the PR :)

@zucchini-nlp zucchini-nlp merged commit 633da1b into huggingface:main Jan 9, 2025
10 checks passed
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.

[Idefics 3] Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1
4 participants