- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6.5k
fix "Expected all tensors to be on the same device, but found at least two devices" error #11690
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
                    Changes from 23 commits
      Commits
    
    
            Show all changes
          
          
            31 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      6001899
              
                xx
              
              
                yao-matrix 8a1d6e5
              
                fix
              
              
                yao-matrix 603257b
              
                Update model_loading_utils.py
              
              
                yao-matrix 8cdfdd8
              
                Update test_models_unet_2d_condition.py
              
              
                yao-matrix 45e29bd
              
                Update test_models_unet_2d_condition.py
              
              
                yao-matrix 2a7c17d
              
                Merge branch 'main' into xpu
              
              
                yao-matrix fae7c70
              
                fix style
              
              
                yao-matrix 80fdbfc
              
                Merge branch 'main' into xpu
              
              
                yao-matrix 97a37a1
              
                Merge branch 'main' into xpu
              
              
                yao-matrix 5f0c794
              
                Merge branch 'main' into xpu
              
              
                yao-matrix 8cd06b3
              
                Merge branch 'main' into xpu
              
              
                yao-matrix 02a6a35
              
                Merge branch 'main' into xpu
              
              
                yao-matrix ed1a788
              
                Merge branch 'main' into xpu
              
              
                yao-matrix 220ce94
              
                Merge branch 'main' into xpu
              
              
                yao-matrix e59cb0c
              
                Merge branch 'main' into xpu
              
              
                yao-matrix fd618b5
              
                Merge branch 'main' into xpu
              
              
                yao-matrix c340f9e
              
                Merge branch 'main' into xpu
              
              
                yao-matrix e674ce7
              
                Merge branch 'main' into xpu
              
              
                yao-matrix d389758
              
                Merge branch 'main' into xpu
              
              
                yao-matrix 7e8ae22
              
                Merge branch 'main' into xpu
              
              
                yao-matrix c43bb19
              
                Merge branch 'main' into xpu
              
              
                yao-matrix 49ac5d4
              
                Merge branch 'main' into xpu
              
              
                yao-matrix b7148d6
              
                Merge branch 'main' into xpu
              
              
                yao-matrix bda0afd
              
                Merge branch 'main' into xpu
              
              
                yao-matrix 1ba8a88
              
                fix comments
              
              
                yao-matrix fd9fa99
              
                Update unet_2d_blocks.py
              
              
                yao-matrix 692f0bd
              
                Merge branch 'main' into xpu
              
              
                sayakpaul ab5f55c
              
                Merge branch 'main' into xpu
              
              
                sayakpaul 9b41f3a
              
                Merge branch 'main' into xpu
              
              
                yao-matrix 9948c9c
              
                update
              
              
                yao-matrix 38ff983
              
                Merge branch 'main' into xpu
              
              
                yao-matrix File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -74,7 +74,6 @@ | |
| require_torch_2, | ||
| require_torch_accelerator, | ||
| require_torch_accelerator_with_training, | ||
| require_torch_gpu, | ||
| require_torch_multi_accelerator, | ||
| require_torch_version_greater, | ||
| run_test_in_subprocess, | ||
|  | @@ -1902,7 +1901,7 @@ def test_push_to_hub_library_name(self): | |
| delete_repo(self.repo_id, token=TOKEN) | ||
|  | ||
|  | ||
| @require_torch_gpu | ||
| @require_torch_accelerator | ||
| @require_torch_2 | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is unrelated to this PR. Going forward prefer not including unrelated changes in a particular PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, will follow the rule going forward. | ||
| @is_torch_compile | ||
| @slow | ||
|  | @@ -1970,7 +1969,7 @@ def test_compile_with_group_offloading(self): | |
| model.eval() | ||
| # TODO: Can test for other group offloading kwargs later if needed. | ||
| group_offload_kwargs = { | ||
| "onload_device": "cuda", | ||
| "onload_device": torch_device, | ||
| "offload_device": "cpu", | ||
| "offload_type": "block_level", | ||
| "num_blocks_per_group": 1, | ||
|  | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we shouldn't need that since both hidden_states and res_hidden_states should be on the same device no ? The pre-forward hook added by accelerate should be move all the inputs to the same device.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SunMarc , i suppose this is a corner case?
torch.catis a weight-less function, so seems cannot covered by the pre-forward hook set byaccelerate...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean since
hidden_statesandres_hidden_states_tupleare in the forward definition, they should be moved to the same device by the pre-forward hook added by accelerateUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SunMarc We run into a corner case here. Since we have 8 cards here, so the determined
device_map(by https://github.com/huggingface/diffusers/blob/1bc6f3dc0f21779480db70a4928d14282c0198ed/src/diffusers/models/model_loading_utils.py#L64C5-L64C26) isWe can see
UpBlockis not the atomic module, its submodules are assigned to different devices(up_blocks.0.resnets.0, up_blocks.0.resnets.1), so pre-hook forUpBlockwill not help in this case. And sincetorch.catis not pre-hooked(and cannot since it's a function rather than a module?), so the issue happens.If there is no a
torch.catbtw the sub-blocks inUpBlock, things will be all fine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SunMarc, need your inputs in how to proceed for this corner case, thx.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SunMarc We can see a similar case in
transformersutpytest -rA tests/models/chameleon/test_modeling_chameleon.py::ChameleonVision2SeqModelTest::test_model_parallel_beam_searchw/ 2 cards, the error log is "RuntimeError: Expected all tensors to be on the same device, but found at least two devices,src/transformers/models/chameleon/modeling_chameleon.py", the reason is evenresidualis in the same device ashidden_statesat the beginning, but after they went through some operators as both input and output, they finally placed to different device, but when they come to+which is not a nn.Module(so accelerate cannot pre-hook it), error happens. Do you have some insights on such issues?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SunMarc, could you share your insights on the issue i mentioned above? thx very much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the long wait @yao-matrix , if you add
UpBlock2Din_no_split_modulesofUNet2DConditionModel, the test should pass !_no_split_modules = ["BasicTransformerBlock", "ResnetBlock2D", "CrossAttnUpBlock2D", "UpBlock2D"]I've tested on my end and it works.
As for ChameleonVision2SeqModelTest, we probably need to also update _no_split_modules.