|
12 | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
| 15 | +import json |
15 | 16 | import tempfile |
16 | 17 | import unittest |
17 | 18 |
|
18 | 19 | import torch |
19 | 20 | from parameterized import parameterized |
20 | 21 |
|
21 | | -from diffusers import DiffusionPipeline, QuantoConfig |
| 22 | +from diffusers import BitsAndBytesConfig, DiffusionPipeline, QuantoConfig |
22 | 23 | from diffusers.quantizers import PipelineQuantizationConfig |
23 | 24 | from diffusers.utils import logging |
24 | 25 | from diffusers.utils.testing_utils import ( |
@@ -243,3 +244,57 @@ def test_no_quantization_for_all_invalid_components(self, method): |
243 | 244 | for name, component in pipe.components.items(): |
244 | 245 | if isinstance(component, torch.nn.Module): |
245 | 246 | self.assertTrue(not hasattr(component.config, "quantization_config")) |
| 247 | + |
| 248 | + @parameterized.expand(["quant_kwargs", "quant_mapping"]) |
| 249 | + def test_quant_config_repr(self, method): |
| 250 | + component_name = "transformer" |
| 251 | + if method == "quant_kwargs": |
| 252 | + components_to_quantize = [component_name] |
| 253 | + quant_config = PipelineQuantizationConfig( |
| 254 | + quant_backend="bitsandbytes_8bit", |
| 255 | + quant_kwargs={"load_in_8bit": True}, |
| 256 | + components_to_quantize=components_to_quantize, |
| 257 | + ) |
| 258 | + else: |
| 259 | + quant_config = PipelineQuantizationConfig( |
| 260 | + quant_mapping={component_name: BitsAndBytesConfig(load_in_8bit=True)} |
| 261 | + ) |
| 262 | + |
| 263 | + pipe = DiffusionPipeline.from_pretrained( |
| 264 | + self.model_name, |
| 265 | + quantization_config=quant_config, |
| 266 | + torch_dtype=torch.bfloat16, |
| 267 | + ) |
| 268 | + self.assertTrue(getattr(pipe, "quantization_config", None) is not None) |
| 269 | + retrieved_config = pipe.quantization_config |
| 270 | + expected_config = """ |
| 271 | +transformer BitsAndBytesConfig { |
| 272 | + "_load_in_4bit": false, |
| 273 | + "_load_in_8bit": true, |
| 274 | + "bnb_4bit_compute_dtype": "float32", |
| 275 | + "bnb_4bit_quant_storage": "uint8", |
| 276 | + "bnb_4bit_quant_type": "fp4", |
| 277 | + "bnb_4bit_use_double_quant": false, |
| 278 | + "llm_int8_enable_fp32_cpu_offload": false, |
| 279 | + "llm_int8_has_fp16_weight": false, |
| 280 | + "llm_int8_skip_modules": null, |
| 281 | + "llm_int8_threshold": 6.0, |
| 282 | + "load_in_4bit": false, |
| 283 | + "load_in_8bit": true, |
| 284 | + "quant_method": "bitsandbytes" |
| 285 | +} |
| 286 | +
|
| 287 | +""" |
| 288 | + expected_data = self._parse_config_string(expected_config) |
| 289 | + actual_data = self._parse_config_string(str(retrieved_config)) |
| 290 | + self.assertTrue(actual_data == expected_data) |
| 291 | + |
| 292 | + def _parse_config_string(self, config_string: str) -> tuple[str, dict]: |
| 293 | + first_brace = config_string.find("{") |
| 294 | + if first_brace == -1: |
| 295 | + raise ValueError("Could not find opening brace '{' in the string.") |
| 296 | + |
| 297 | + json_part = config_string[first_brace:] |
| 298 | + data = json.loads(json_part) |
| 299 | + |
| 300 | + return data |
0 commit comments