- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6.5k
Fix TorchAO related bugs; revert device_map changes #10371
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 21 commits
      Commits
    
    
            Show all changes
          
          
            23 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      03049aa
              
                Revert "Add support for sharded models when TorchAO quantization is e…
              
              
                a-r-r-o-w 1a29a99
              
                update tests
              
              
                a-r-r-o-w c6651f9
              
                udpate
              
              
                a-r-r-o-w 87bb2fe
              
                update
              
              
                a-r-r-o-w ba1269d
              
                update
              
              
                a-r-r-o-w bc47057
              
                Merge branch 'main' into fix-torchao-related-bugs
              
              
                a-r-r-o-w 1873bb7
              
                update device map tests
              
              
                a-r-r-o-w d0b718a
              
                apply review suggestions
              
              
                a-r-r-o-w a10f19c
              
                update
              
              
                a-r-r-o-w fb8b44e
              
                make style
              
              
                a-r-r-o-w 2bd9302
              
                fix
              
              
                a-r-r-o-w 651666d
              
                update docs
              
              
                a-r-r-o-w d1b6405
              
                update tests
              
              
                a-r-r-o-w 1dcd24e
              
                update workflow
              
              
                a-r-r-o-w e5dcdec
              
                update
              
              
                a-r-r-o-w 1ae9fec
              
                improve tests
              
              
                a-r-r-o-w 2663e94
              
                allclose tolerance
              
              
                a-r-r-o-w 9f98833
              
                Update src/diffusers/models/modeling_utils.py
              
              
                a-r-r-o-w b7de749
              
                Update tests/quantization/torchao/test_torchao.py
              
              
                a-r-r-o-w 77a3456
              
                Merge branch 'main' into fix-torchao-related-bugs
              
              
                a-r-r-o-w 3e72979
              
                improve tests
              
              
                a-r-r-o-w a98a184
              
                fix
              
              
                a-r-r-o-w fa33949
              
                update correct slices
              
              
                a-r-r-o-w 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 | 
|---|---|---|
|  | @@ -25,6 +25,7 @@ Quantize a model by passing [`TorchAoConfig`] to [`~ModelMixin.from_pretrained`] | |
| The example below only quantizes the weights to int8. | ||
|  | ||
| ```python | ||
| import torch | ||
| from diffusers import FluxPipeline, FluxTransformer2DModel, TorchAoConfig | ||
|  | ||
| model_id = "black-forest-labs/FLUX.1-dev" | ||
|  | @@ -44,6 +45,10 @@ pipe = FluxPipeline.from_pretrained( | |
| ) | ||
| pipe.to("cuda") | ||
|  | ||
| # Without quantization: ~31.447 GB | ||
| # With quantization: ~20.40 GB | ||
| print(f"Pipeline memory usage: {torch.cuda.max_memory_reserved() / 1024**3:.3f} GB") | ||
|  | ||
| prompt = "A cat holding a sign that says hello world" | ||
| image = pipe( | ||
| prompt, num_inference_steps=50, guidance_scale=4.5, max_sequence_length=512 | ||
|  | @@ -88,6 +93,63 @@ Some quantization methods are aliases (for example, `int8wo` is the commonly use | |
|  | ||
| Refer to the official torchao documentation for a better understanding of the available quantization methods and the exhaustive list of configuration options available. | ||
|  | ||
| ## Serializing and Deserializing quantized models | ||
|  | ||
| To serialize a quantized model in a given dtype, first load the model with the desired quantization dtype and then save it using the [`~ModelMixin.save_pretrained`] method. | ||
|  | ||
| ```python | ||
| import torch | ||
| from diffusers import FluxTransformer2DModel, TorchAoConfig | ||
|  | ||
| quantization_config = TorchAoConfig("int8wo") | ||
| transformer = FluxTransformer2DModel.from_pretrained( | ||
| "black-forest-labs/Flux.1-Dev", | ||
| subfolder="transformer", | ||
| quantization_config=quantization_config, | ||
| torch_dtype=torch.bfloat16, | ||
| ) | ||
| transformer.save_pretrained("/path/to/flux_int8wo", safe_serialization=False) | ||
| ``` | ||
|  | ||
| To load a serialized quantized model, use the [`~ModelMixin.from_pretrained`] method. | ||
|  | ||
| ```python | ||
| import torch | ||
| from diffusers import FluxPipeline, FluxTransformer2DModel | ||
|  | ||
| transformer = FluxTransformer2DModel.from_pretrained("/path/to/flux_int8wo", torch_dtype=torch.bfloat16, use_safetensors=False) | ||
| pipe = FluxPipeline.from_pretrained("black-forest-labs/Flux.1-Dev", transformer=transformer, torch_dtype=torch.bfloat16) | ||
| pipe.to("cuda") | ||
|  | ||
| prompt = "A cat holding a sign that says hello world" | ||
| image = pipe(prompt, num_inference_steps=30, guidance_scale=7.0).images[0] | ||
| image.save("output.png") | ||
| ``` | ||
|  | ||
| Some quantization methods, such as `uint4wo`, cannot be loaded directly and may result in an `UnpicklingError` when trying to load the models, but work as expected when saving them. In order to work around this, one can load the state dict manually into the model. Note, however, that this requires using `weights_only=False` in `torch.load`, so it should be run only if the weights were obtained from a trustable source. | ||
| 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. Cc: @jerryzh168. Is this known? | ||
|  | ||
| ```python | ||
| import torch | ||
| from accelerate import init_empty_weights | ||
| from diffusers import FluxPipeline, FluxTransformer2DModel, TorchAoConfig | ||
|  | ||
| # Serialize the model | ||
| transformer = FluxTransformer2DModel.from_pretrained( | ||
| "black-forest-labs/Flux.1-Dev", | ||
| subfolder="transformer", | ||
| quantization_config=TorchAoConfig("uint4wo"), | ||
| torch_dtype=torch.bfloat16, | ||
| ) | ||
| transformer.save_pretrained("/path/to/flux_uint4wo", safe_serialization=False, max_shard_size="50GB") | ||
| # ... | ||
|  | ||
| # Load the model | ||
| state_dict = torch.load("/path/to/flux_uint4wo/diffusion_pytorch_model.bin", weights_only=False, map_location="cpu") | ||
| with init_empty_weights(): | ||
|         
                  a-r-r-o-w marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| transformer = FluxTransformer2DModel.from_config("/path/to/flux_uint4wo/config.json") | ||
| transformer.load_state_dict(state_dict, strict=True, assign=True) | ||
| ``` | ||
|  | ||
| ## Resources | ||
|  | ||
| - [TorchAO Quantization API](https://github.com/pytorch/ao/blob/main/torchao/quantization/README.md) | ||
|  | ||
  
    
      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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.