Skip to content

Commit b362877

Browse files
authored
Merge branch 'allow-non-list-component' into comp-to-quant
2 parents 94ee321 + 15619e7 commit b362877

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

docs/source/en/quantization/overview.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ Initialize [`~quantizers.PipelineQuantizationConfig`] with the following paramet
3434
> [!TIP]
3535
> These `quant_kwargs` arguments are different for each backend. Refer to the [Quantization API](../api/quantization) docs to view the arguments for each backend.
3636
37-
- `components_to_quantize` specifies which components of the pipeline to quantize. Typically, you should quantize the most compute intensive components like the transformer. The text encoder is another component to consider quantizing if a pipeline has more than one such as [`FluxPipeline`]. The example below quantizes the T5 text encoder in [`FluxPipeline`] while keeping the CLIP model intact.
37+
- `components_to_quantize` specifies which component(s) of the pipeline to quantize. Typically, you should quantize the most compute intensive components like the transformer. The text encoder is another component to consider quantizing if a pipeline has more than one such as [`FluxPipeline`]. The example below quantizes the T5 text encoder in [`FluxPipeline`] while keeping the CLIP model intact.
38+
39+
`components_to_quantize` accepts either a list for multiple models or a string for a single model.
3840

3941
The example below loads the bitsandbytes backend with the following arguments from [`~quantizers.quantization_config.BitsAndBytesConfig`], `load_in_4bit`, `bnb_4bit_quant_type`, and `bnb_4bit_compute_dtype`.
4042

@@ -62,6 +64,7 @@ pipe = DiffusionPipeline.from_pretrained(
6264
image = pipe("photo of a cute dog").images[0]
6365
```
6466

67+
6568
### Advanced quantization
6669

6770
The `quant_mapping` argument provides more options for how to quantize each individual component in a pipeline, like combining different quantization backends.

src/diffusers/quantizers/pipe_quant_config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ def __init__(
4848
self,
4949
quant_backend: str = None,
5050
quant_kwargs: Dict[str, Union[str, float, int, dict]] = None,
51-
components_to_quantize: Optional[List[str]] = None,
51+
components_to_quantize: Optional[Union[List[str], str]] = None,
5252
quant_mapping: Dict[str, Union[DiffQuantConfigMixin, "TransformersQuantConfigMixin"]] = None,
5353
):
5454
self.quant_backend = quant_backend
5555
# Initialize kwargs to be {} to set to the defaults.
5656
self.quant_kwargs = quant_kwargs or {}
57+
if components_to_quantize:
58+
if isinstance(components_to_quantize, str):
59+
components_to_quantize = [components_to_quantize]
5760
self.components_to_quantize = components_to_quantize
5861
self.quant_mapping = quant_mapping
5962
self.config_mapping = {} # book-keeping Example: `{module_name: quant_config}`

tests/quantization/test_pipeline_level_quantization.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,19 @@ def _parse_config_string(self, config_string: str) -> tuple[str, dict]:
299299
data = json.loads(json_part)
300300

301301
return data
302+
303+
def test_single_component_to_quantize(self):
304+
component_to_quantize = "transformer"
305+
quant_config = PipelineQuantizationConfig(
306+
quant_backend="bitsandbytes_8bit",
307+
quant_kwargs={"load_in_8bit": True},
308+
components_to_quantize=component_to_quantize,
309+
)
310+
pipe = DiffusionPipeline.from_pretrained(
311+
self.model_name,
312+
quantization_config=quant_config,
313+
torch_dtype=torch.bfloat16,
314+
)
315+
for name, component in pipe.components.items():
316+
if name == component_to_quantize:
317+
self.assertTrue(hasattr(component.config, "quantization_config"))

0 commit comments

Comments
 (0)