Changed quantized weights not reflecting in state_dict()

PyTorch: 1.7.0

I modified the quantized weights of a net post-quantization as follows:

# instantiate the quantized net (not shown here).

# get one of the conv layers
tmp = model_int8.state_dict()['features.0.weight']
scales = tmp.q_per_channel_scales()
zero_pts = tmp.q_per_channel_zero_points()
axis = tmp.q_per_channel_axis()

# get int repr
tmp_int8 = tmp.int_repr()

# change value (value change is dependent on the int8 value)
tmp_int8[0][0][0][0] = new_value

# how to convert tmp_int8 to torch.qint8 type?
new_tmp = torch._make_per_channel_quantized_tensor(tmp_int8, scales, zero_pts, axis)

# based on the above step:
model_int8.features[0].weight = new_tmp

However, model_int8.features[0].weight shows updated values, but model_int8.state_dict()['features.0.weight'] shows old vales.

I also tried saving the modified model and reloading, but the problem persists.

Question is which weight values are being used for inference? I do not see change in the classification results.

You need to use conv_obj._weight_bias() to get the weight, and conv_obj.set_weight_bias(w, b) to set the weight. Here is an example: gist:8ad0e472e8743e142a1e72cb0f9efb1d · GitHub

1 Like