From eeb01b19e233c5d9dc08bcd2cf24b15c3bbbc312 Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Wed, 13 Nov 2024 18:37:00 +0000 Subject: [PATCH] Fix uninitialized variable in quantized compressors Both compressors have a can_quantize() check, which if ever doesn't succeed would trigger: > UnboundLocalError: cannot access local variable 'quantized_weight' where it is not associated with a value --- .../compressors/quantized_compressors/naive_quantized.py | 6 ++++-- .../compressors/quantized_compressors/pack_quantized.py | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/compressed_tensors/compressors/quantized_compressors/naive_quantized.py b/src/compressed_tensors/compressors/quantized_compressors/naive_quantized.py index 0267aca4..85eebe00 100644 --- a/src/compressed_tensors/compressors/quantized_compressors/naive_quantized.py +++ b/src/compressed_tensors/compressors/quantized_compressors/naive_quantized.py @@ -93,9 +93,11 @@ def compress_weight( args=quantization_args, dtype=quantization_args.pytorch_dtype(), ) + else: + quantized_weight = weight - if device is not None: - quantized_weight = quantized_weight.to(device) + if device is not None: + quantized_weight = quantized_weight.to(device) return {"weight": quantized_weight} diff --git a/src/compressed_tensors/compressors/quantized_compressors/pack_quantized.py b/src/compressed_tensors/compressors/quantized_compressors/pack_quantized.py index ce9f0a57..c236f8c9 100644 --- a/src/compressed_tensors/compressors/quantized_compressors/pack_quantized.py +++ b/src/compressed_tensors/compressors/quantized_compressors/pack_quantized.py @@ -94,6 +94,8 @@ def compress_weight( args=quantization_args, dtype=torch.int8, ) + else: + quantized_weight = weight packed_weight = pack_to_int32(quantized_weight, quantization_args.num_bits) weight_shape = torch.tensor(weight.shape)