Skip to content

Commit dbb23fb

Browse files
authored
Use symmetric quantization in the quantize subcommand (#2120)
Packing of asymmetric quantization is broken, all (q)zeros values of `0` get reset to `1`, resulting in a loss of accuracy. So instead use symmetric quantization. To be able to distinguish models with symmetric and asymmetric quantization, a new config tensor `gptq_sym` is added. If this tensor is not present, we assume `sym=False`.
1 parent c46eaf7 commit dbb23fb

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

server/text_generation_server/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ def quantize(
353353
upload_to_model_id=upload_to_model_id,
354354
percdamp=percdamp,
355355
act_order=act_order,
356+
sym=True,
356357
)
357358

358359

server/text_generation_server/layers/gptq/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,15 @@ def get_weights_row(self, weights: Weights, prefix: str):
393393
)
394394

395395
def _get_gptq_params(self, weights: Weights):
396-
try:
396+
if weights._has_tensor("gptq_bits") and weights._has_tensor("gptq_groupsize"):
397397
self.bits = weights.get_tensor("gptq_bits").item()
398398
self.groupsize = weights.get_tensor("gptq_groupsize").item()
399399
self.desc_act = False
400-
self.sym = False
400+
# `server quantize` used asymmetric quantization unconditionally
401+
# before the `gptq_sym` setting tensor was added.
402+
self.sym = (
403+
weights.get_tensor("gptq_sym").item()
404+
if weights._has_tensor("gptq_sym")
405+
else False
406+
)
401407
self.quant_method = "gptq"
402-
except (SafetensorError, RuntimeError) as e:
403-
pass

server/text_generation_server/layers/gptq/quantize.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ def quantize(
871871
upload_to_model_id: Optional[str],
872872
percdamp: float,
873873
act_order: bool,
874+
sym: bool,
874875
):
875876
print("loading model")
876877
config = AutoConfig.from_pretrained(
@@ -946,6 +947,7 @@ def _unload():
946947
percdamp=percdamp,
947948
act_order=act_order,
948949
hooks=hooks,
950+
sym=sym,
949951
)
950952
print(time.time() - tick)
951953

@@ -957,6 +959,7 @@ def _unload():
957959
state_dict = {k: v.cpu().contiguous() for k, v in state_dict.items()}
958960
state_dict["gptq_bits"] = torch.LongTensor([bits])
959961
state_dict["gptq_groupsize"] = torch.LongTensor([groupsize])
962+
state_dict["gptq_sym"] = torch.BoolTensor([sym])
960963

961964
max_shard_size = "10GB"
962965
shards, index = shard_checkpoint(

server/text_generation_server/utils/weights.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ def _get_slice(self, tensor_name: str):
146146
slice_ = f.get_slice(tensor_name)
147147
return slice_
148148

149+
def _has_tensor(self, tensor_name: str):
150+
try:
151+
self.get_filename(tensor_name)
152+
except Exception:
153+
return False
154+
return True
155+
149156
def get_shape(self, tensor_name: str):
150157
return self._get_slice(tensor_name).get_shape()
151158

0 commit comments

Comments
 (0)