Open alex96295 opened 4 years ago
@raghuramank100 can you have a look?
RuntimeError: Error(s) in loading state_dict for BMNet: Unexpected key(s) in state_dict: "conv1a.0.scale", "conv1a.0.zero_point", "conv1aa.0.scale", "conv1aa.0.zero_point", "conv1b.0.scale", "conv1b.0.zero_point", "conv2a.0.scale", "conv2a.0.zero_point", "conv2aa.0.scale", "conv2aa.0.zero_point", "conv2b.0.scale", "conv2b.0.zero_point", "conv3a.0.scale", "conv3a.0.zero_point", "conv3aa.0.scale", "conv3aa.0.zero_point", "conv3b.0.scale", "conv3b.0.zero_point", "conv4a.0.scale", "conv4a.0.zero_point", "conv4aa.0.scale", "conv4aa.0.zero_point", "conv4b.0.scale", "conv4b
Hi @alex96295 One Alternative Way to load the Quantized Model in PyTorch is
model_fp32 = #Load and store the state dict of the Original Model (Unquantized)
model_quant_state_dict = torch.load(quant_model_path) # Load Quant Model which you have converted
quant_model = torch.quantization.convert(model_fp32) # Convert to Quant
quant_model.load_state_dict(model_quant_state_dict)
Thats it, Now yo can run the Quant Model without any errors
I have some same error to load quantizated model in torch
self._create_model()
if quantized:
self.model.qconfig = torch.ao.quantization.get_default_qconfig('x86')
model_fp32_prepared = torch.ao.quantization.prepare_qat(self.model)
model_int8 = torch.ao.quantization.convert(model_fp32_prepared)
#TODO load dynamic name
from pprint import pprint
# pprint(torch.load('/home/wangft/workspace/weigh/train_model/output/run_289/TCN4_50_QAT_fp32_opset_12_2024-08-28-17-49-47_qat.pth', map_location=self.device).keys())
# exit()
pprint(model_int8)
# exit()
model_int8.load_state_dict(torch.load('/home/wangft/workspace/weigh/train_model/output/run_289/TCN4_50_QAT_fp32_opset_12_2024-08-28-17-49-47_qat.pth', map_location=self.device))
exit()
Traceback (most recent call last):
File "/home/wangft/workspace/weigh/train_model/inference_torch.py", line 5, in <module>
task.run(False, quantized=True)
File "/home/wangft/workspace/weigh/train_model/tasks/task.py", line 550, in run
model_int8.load_state_dict(torch.load('/home/wangft/workspace/weigh/train_model/output/run_289/TCN4_50_QAT_fp32_opset_12_2024-08-28-17-49-47_qat.pth', map_location=self.device))
File "/home/wangft/anaconda3/envs/py310/lib/python3.10/site-packages/torch/nn/modules/module.py", line 2215, in load_state_dict
raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for TCN4_50_QAT:
Missing key(s) in state_dict: "net.network.0.TCM_net.0.1.weight", "net.network.0.TCM_net.0.1.bias", "net.network.0.TCM_net.0.1.running_mean", "net.network.0.TCM_net.0.1.running_var", "net.network.0.TCM_net.1.net.2.1.weight", "net.network.0.TCM_net.1.net.2.1.bias", "net.network.0.TCM_net.1.net.2.1.running_mean", "net.network.0.TCM_net.1.net.2.1.running_var", "net.network.1.TCM_net.0.1.weight", "net.network.1.TCM_net.0.1.bias", "net.network.1.TCM_net.0.1.running_mean", "net.network.1.TCM_net.0.1.running_var", "net.network.1.TCM_net.1.net.2.1.weight", "net.network.1.TCM_net.1.net.2.1.bias", "net.network.1.TCM_net.1.net.2.1.running_mean", "net.network.1.TCM_net.1.net.2.1.running_var", "net.network.2.TCM_net.0.1.weight", "net.network.2.TCM_net.0.1.bias", "net.network.2.TCM_net.0.1.running_mean", "net.network.2.TCM_net.0.1.running_var", "net.network.2.TCM_net.1.net.2.1.weight", "net.network.2.TCM_net.1.net.2.1.bias", "net.network.2.TCM_net.1.net.2.1.running_mean", "net.network.2.TCM_net.1.net.2.1.running_var", "net.network.3.TCM_net.0.1.weight", "net.network.3.TCM_net.0.1.bias", "net.network.3.TCM_net.0.1.running_mean", "net.network.3.TCM_net.0.1.running_var", "net.network.3.TCM_net.1.net.2.1.weight", "net.network.3.TCM_net.1.net.2.1.bias", "net.network.3.TCM_net.1.net.2.1.running_mean", "net.network.3.TCM_net.1.net.2.1.running_var", "net.network.4.TCM_net.0.1.weight", "net.network.4.TCM_net.0.1.bias", "net.network.4.TCM_net.0.1.running_mean", "net.network.4.TCM_net.0.1.running_var", "net.network.4.TCM_net.1.net.2.1.weight", "net.network.4.TCM_net.1.net.2.1.bias", "net.network.4.TCM_net.1.net.2.1.running_mean", "net.network.4.TCM_net.1.net.2.1.running_var", "net.network.5.TCM_net.0.1.weight", "net.network.5.TCM_net.0.1.bias", "net.network.5.TCM_net.0.1.running_mean", "net.network.5.TCM_net.0.1.running_var", "net.network.5.TCM_net.1.net.2.1.weight", "net.network.5.TCM_net.1.net.2.1.bias", "net.network.5.TCM_net.1.net.2.1.running_mean", "net.network.5.TCM_net.1.net.2.1.running_var", "net.network.6.TCM_net.0.1.weight", "net.network.6.TCM_net.0.1.bias", "net.network.6.TCM_net.0.1.running_mean", "net.network.6.TCM_net.0.1.running_var", "net.network.6.TCM_net.1.net.2.1.weight", "net.network.6.TCM_net.1.net.2.1.bias", "net.network.6.TCM_net.1.net.2.1.running_mean", "net.network.6.TCM_net.1.net.2.1.running_var".
Unexpected key(s) in state_dict: "quant.scale", "quant.zero_point".
Hi @alex96295 One Alternative Way to load the Quantized Model in PyTorch is
model_fp32 = #Load and store the state dict of the Original Model (Unquantized)
model_quant_state_dict = torch.load(quant_model_path) # Load Quant Model which you have converted
quant_model = torch.quantization.convert(model_fp32) # Convert to Quant
quant_model.load_state_dict(model_quant_state_dict)
Thats it, Now yo can run the Quant Model without any errors
I tried doin the same, but still i am getting the error. For reference
pretrain_model_url = { 'restoration': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth', }
device = 'cuda' if torch.cuda.is_available() else 'cpu' net = ARCH_REGISTRY.get('CodeFormer')(dim_embd=512, codebook_size=1024, n_head=8, n_layers=9, connect_list=['32', '64', '128', '256']).to(device)
ckpt_path = 'weights/CodeFormer/codeformer.pth' checkpoint = torch.load(ckpt_path)['params_ema'] net.load_state_dict(checkpoint) net.eval()
Create the data, using the dummy data here as an example
traced_bs = 1
x = torch.randn(traced_bs, 3, 512, 512).contiguous(memory_format=torch.channels_last)
example_inputs = (x,)
model_path = "/content/drive/MyDrive/CodeFormer_Quantization/weights/CodeFormer/codeformer_quantized_model.pth"
Load the state dictionary into a new instance of the model architecture
loaded_model = capture_pre_autograd_graph(net, example_inputs) # Recreate the model architecture
Load the state dict (weights) into the model
quantizer = X86InductorQuantizer()
quantizer.set_global(xiq.get_default_x86_inductor_quantization_config())
prepared_model = prepare_pt2e(loaded_model, quantizer)
converted_model = convert_pt2e(prepared_model)
prepared_model.load_state_dict(torch.load(model_path), strict=False)
converted_model = convert_pt2e(prepared_model)
quant_net = torch.ao.quantization.convert(net, inplace=False).to(device) quant_net.load_state_dict(torch.load(model_path))
This gives me the following error:
RuntimeError: Error(s) in loading state_dict for CodeFormer: Missing key(s) in state_dict: "encoder.blocks.0.weight", "encoder.blocks.0.bias", "encoder.blocks.1.norm1.weight", "encoder.blocks.1.norm1.bias", "encoder.blocks.1.conv1.weight", "encoder.blocks.1.conv1.bias"....`
🐛 Bug
Hello, I am trying to quantize a model. I have done post training static quantization following the tutorial. During the conversion, I:
mymodel = model(cfg)
mymodel = load_state_dict(torch.load('weights.pt'))
Prepare the model, calibrate it and convert it to its quantized version
save the quantized version using
torch.save(mymodel_q.state_dict(), 'weights_q.pt')
When I load it I use the same code as before, i.e. I define the model and then load it using load_state_dict
But having the quantized model information about the scale and zero_point it seems that they are missing from the original model definition. It was guessable since nobody has ever changed 'model(cfg)' after quantization. But how to include info about scale and zero_point?
Am I saving the quantized model in the wrong way?
Thank you!
conda
,pip
, source): pipAdditional context