fix: save quantization states with torch.save
This commit is contained in:
@@ -69,11 +69,14 @@ def streaming_quantize(model_path: str, output_path: str):
|
|||||||
|
|
||||||
print(f" Quantizing {len(weight_keys)} tensors...")
|
print(f" Quantizing {len(weight_keys)} tensors...")
|
||||||
|
|
||||||
|
quant_states = {}
|
||||||
for key in weight_keys:
|
for key in weight_keys:
|
||||||
try:
|
try:
|
||||||
weight = state_dict[key]
|
weight = state_dict[key]
|
||||||
qweight, qstate = quantize_weight_nf4(weight)
|
qweight, qstate = quantize_weight_nf4(weight)
|
||||||
state_dict[key] = qweight
|
state_dict[key] = qweight
|
||||||
|
if qstate is not None:
|
||||||
|
quant_states[f"{key}.quant_state"] = qstate
|
||||||
del weight, qweight, qstate
|
del weight, qweight, qstate
|
||||||
gc.collect()
|
gc.collect()
|
||||||
torch.cuda.empty_cache()
|
torch.cuda.empty_cache()
|
||||||
@@ -88,8 +91,8 @@ def streaming_quantize(model_path: str, output_path: str):
|
|||||||
for k, v in state_dict.items()
|
for k, v in state_dict.items()
|
||||||
}
|
}
|
||||||
|
|
||||||
save_file(state_dict, output_path / shard_name)
|
torch.save({**state_dict, **quant_states}, output_path / shard_name)
|
||||||
print(f" ✓ Saved {shard_name}")
|
print(f" ✓ Saved {shard_name} ({len(quant_states)} quant states)")
|
||||||
|
|
||||||
del state_dict
|
del state_dict
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|||||||
Reference in New Issue
Block a user