-
Notifications
You must be signed in to change notification settings - Fork 11
/
unpack.py
37 lines (32 loc) · 1.51 KB
/
unpack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import argparse
import torch
from bitpack.pytorch_interface import load_quantized_state_dict
parser = argparse.ArgumentParser(description='To unpack models that are packed by BitPack')
parser.add_argument('--device',
type=int,
default=-1,
help='index of target device, -1 means cpu')
parser.add_argument('--input-packed-file',
type=str,
default=None,
help='path to packed file')
parser.add_argument('--original-int-file',
type=str,
default=None,
help='original quantized file in integer format, this is for correctness check')
args = parser.parse_args()
if args.device == -1:
target_device = torch.device('cpu')
else:
target_device = torch.device('cuda:' + str(args.device))
unpacked_state_dict = load_quantized_state_dict(args.input_packed_file, target_device)
if args.original_int_file:
original_state_dict = torch.load(args.original_int_file)['weight_integer']
for k in original_state_dict.keys():
if not torch.all(unpacked_state_dict[k].type_as(original_state_dict[k])==original_state_dict[k]):
print("Error Detected between Unpacked Tensor and Original Tensor with Key Value: ", k)
print("Unpacked Tensor: ", unpacked_state_dict[k])
print("Original Tensor: ", original_state_dict[k])
break
else:
print("Correctly Match: ", k)