Repository: huggingface/transformers
Pull request: [Quantization] CI green by end of year

# What does this PR do?

Fixing some tests related to `compute_model_sizes` in bnb, and other tests for `compressed-tensors`

Changed files:
filename: src/transformers/integrations/compressed_tensors.py
patch: @@ -0,0 +1,48 @@
+# coding=utf-8
+# Copyright 2025 The HuggingFace Inc. team. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from typing import Optional
+
+import torch
+
+from transformers.utils import logging
+from transformers.utils.import_utils import is_torch_available
+
+
+if is_torch_available():
+    from ..core_model_loading import ConversionOps
+from ..quantizers.quantizers_utils import get_module_from_name
+
+
+logger = logging.get_logger(__name__)
+
+
+class CompressedTensorsMarkInitialize(ConversionOps):
+    def __init__(self, hf_quantizer):
+        self.hf_quantizer = hf_quantizer
+
+    def convert(
+        self,
+        input_dict: dict[str, torch.Tensor],
+        model: Optional[torch.nn.Module] = None,
+        full_layer_name: str | None = None,
+        missing_keys=None,
+        **kwargs,
+    ) -> dict[str, torch.Tensor]:
+        _, value = tuple(input_dict.items())[0]
+        module, tensor_name = get_module_from_name(model, full_layer_name)
+        module._is_hf_initialized = True
+
+        return {full_layer_name: value}

filename: src/transformers/quantizers/quantizer_compressed_tensors.py
patch: @@ -109,3 +109,17 @@ def is_qat_trainable(self) -> bool:
     def is_serializable(self) -> bool:
         """Models quantized using compressed tensors can be saved to disk"""
         return True
+
+    def get_weight_conversions(self):
+        from ..core_model_loading import WeightConverter
+        from ..integrations.compressed_tensors import CompressedTensorsMarkInitialize
+
+        return [
+            WeightConverter(
+                source_patterns=[
+                    "compressed",
+                ],
+                target_patterns="compressed",
+                operations=[CompressedTensorsMarkInitialize(self)],
+            ),
+        ]

filename: tests/quantization/bnb/test_4bit.py
patch: @@ -177,7 +177,7 @@ def test_compute_module_sizes(self):
 
             # testing prequantized = False should be enough, the shape should be the same whether it is pre-quantized or not
             hf_quantizer = AutoHfQuantizer.from_config(BitsAndBytesConfig(load_in_4bit=True), pre_quantized=False)
-            hf_quantizer.preprocess_model(model=model, config=model.config)
+            hf_quantizer.preprocess_model(model=model, config=model.config, device_map=expanded_device_map)
             quantized_model_size, _ = compute_module_sizes(model, hf_quantizer, only_modules=False)
 
             expected_keys = [name for name, _ in model.named_parameters()] + [

filename: tests/quantization/bnb/test_mixed_int8.py
patch: @@ -446,8 +446,10 @@ def test_compute_module_sizes(self):
 
             # testing prequantized = False should be enough, the shape should be the same whether it is pre-quantized or not
             hf_quantizer = AutoHfQuantizer.from_config(BitsAndBytesConfig(load_in_8bit=True), pre_quantized=False)
-            hf_quantizer.preprocess_model(model=model, config=model.config)
-            quantized_model_size, _ = compute_module_sizes(model, hf_quantizer, only_modules=False)
+            hf_quantizer.preprocess_model(model=model, config=model.config, device_map=expanded_device_map)
+            quantized_model_size, _ = compute_module_sizes(
+                model, hf_quantizer, only_modules=False, device_map=expanded_device_map
+            )
 
             expected_keys = [name for name, _ in model.named_parameters()] + [
                 name for name, _ in model.named_buffers()