Skip to content

Commit a81bfe2

Browse files
Michael Gschwindfacebook-github-bot
authored andcommitted
Remove padding from Linear,supported by quantization
Summary: Remove padding from Linear,supported by quantization Reviewed By: kimishpatel Differential Revision: D54350128 fbshipit-source-id: 62ac2989df9990a57c173a8fadc6ddfd25673c0e
1 parent ac833fa commit a81bfe2

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

examples/models/llama2/quantize.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222

2323

2424
try:
25-
# pyre-ignore
25+
# pyre-ignore[21]: Undefined import.
2626
from fairseq2.nn.embedding import (
2727
Embedding as fsEmbedding,
2828
StandardEmbedding as fsStandardEmbedding,
2929
)
3030

31-
# pyre-ignore
31+
# pyre-ignore[21]: Undefined import.
3232
from fairseq2.nn.projection import Linear as fsLinear
3333
except:
3434
print("Could not import fairseq2 modules.")
@@ -645,14 +645,6 @@ def create_quantized_state_dict(self) -> Dict:
645645

646646
# print(f"initial weight shape {mod.weight.shape}")
647647
input_weight = mod.weight.float()
648-
input_weight_shape_1 = input_weight.shape[1]
649-
if (self.group_size is not None) and (
650-
input_weight_shape_1 % self.group_size != 0
651-
):
652-
padding = self.group_size - (
653-
input_weight_shape_1 % self.group_size
654-
)
655-
input_weight = F.pad(input_weight, (0, padding))
656648

657649
# print(f"expanded weight shape {input_weight.shape}")
658650
weight, scales, _ = dynamically_quantize_per_channel(
@@ -663,9 +655,8 @@ def create_quantized_state_dict(self) -> Dict:
663655
self.group_size,
664656
scales_dtype=mod.weight.dtype,
665657
)
666-
unpadded_weight = weight[:, :input_weight_shape_1]
667658

668-
cur_state_dict[f"{fqn}.weight"] = unpadded_weight
659+
cur_state_dict[f"{fqn}.weight"] = weight
669660
# squeeze makes groupsize=rowsize unidimensional
670661
cur_state_dict[f"{fqn}.scales"] = scales.squeeze(dim=-1)
671662

0 commit comments

Comments
 (0)