@@ -105,8 +105,9 @@ def test_no_warnings_for_all_models(self):
105105@require_compressed_tensors
106106@require_torch
107107class DequantizeTest (unittest .TestCase ):
108- """Non-FP8 schemes have no kernels, so they run dequantized: the weights are decompressed while loading.
109- `dequantize=True` (or the deprecated `run_compressed=False`) also drops the activation quantization."""
108+ """Non-FP8 schemes have no kernels, so they run dequantized. `dequantize=True` (or the
109+ deprecated `run_compressed=False`) does it at load time; by default the weights are left
110+ compressed and compressed-tensors decompresses them on the first forward pass."""
110111
111112 tinyllama_w4a16 = "nm-testing/tinyllama-w4a16-compressed"
112113 tinyllama_w8a8 = "nm-testing/tinyllama-w8a8-compressed"
@@ -120,19 +121,26 @@ def tearDown(self):
120121 backend_empty_cache (torch_device )
121122 gc .collect ()
122123
123- def test_default_load_decompresses_while_loading (self ):
124+ def test_default_load_stays_compressed_until_first_forward (self ):
124125 from compressed_tensors import QuantizationStatus
125126
126127 for stub in self .stubs :
127128 model = AutoModelForCausalLM .from_pretrained (stub )
128129
130+ compressed_count = sum (
131+ 1 for m in model .modules () if getattr (m , "quantization_status" , None ) == QuantizationStatus .COMPRESSED
132+ )
133+ self .assertGreater (compressed_count , 0 , "weights must not be decompressed at load time" )
134+
135+ model (input_ids = torch .tensor ([[0 , 1 , 2 ]]))
136+
137+ # The hook compressed-tensors registered decompressed the model on that forward pass.
129138 compressed_count = sum (
130139 1 for m in model .modules () if getattr (m , "quantization_status" , None ) == QuantizationStatus .COMPRESSED
131140 )
132141 self .assertEqual (compressed_count , 0 , "no modules should be left in COMPRESSED state" )
133142 self .assertFalse (any ("weight_packed" in name for name , _ in model .named_parameters ()))
134143 self .assertTrue (model .model .layers [0 ].self_attn .q_proj .weight .is_floating_point ())
135- model (input_ids = torch .tensor ([[0 , 1 , 2 ]]))
136144
137145 def test_deprecated_run_compressed_maps_to_dequantize (self ):
138146 # `run_compressed=False` is deprecated and must behave as `dequantize=True`.
@@ -166,17 +174,10 @@ def test_dequantize_outputs_match_default(self):
166174 self .assertEqual (tokenizer .decode (output_default [0 ]), tokenizer .decode (output_dequantized [0 ]))
167175
168176 def test_dequantize_drops_activation_quantization (self ):
169- """`dequantize=True` loads a dense model: w8a8 also stops quantizing its activations, so its outputs differ
170- from the default (quantized activations) load."""
171- from transformers import AutoTokenizer
172-
173- tokenizer = AutoTokenizer .from_pretrained (self .tinyllama_w8a8 )
174- input_ids = tokenizer (self .prompt , return_tensors = "pt" ).input_ids
177+ """`dequantize=True` loads a plain dense model: no compressed-tensors scheme is left, so w8a8 also stops
178+ quantizing its activations."""
175179 model = AutoModelForCausalLM .from_pretrained (
176180 self .tinyllama_w8a8 , quantization_config = CompressedTensorsConfig (dequantize = True )
177181 )
178- output = model .generate (input_ids , max_new_tokens = 20 , do_sample = False )
179- self .assertEqual (
180- tokenizer .decode (output [0 ]),
181- "<s> Paris is the capital of which country?\n \n * a) France\n * b) Germany\n * c) Italy\n " ,
182- )
182+ self .assertFalse (any (hasattr (module , "quantization_scheme" ) for module in model .modules ()))
183+ self .assertTrue (model .model .layers [0 ].self_attn .q_proj .weight .is_floating_point ())
0 commit comments