# Weight Quantization

**URL:** <https://discuss.pytorch.org/t/weight-quantization/50261>\
**Category:** vision\
**Created:** [July 10, 2019, 2:55pm UTC](https://discuss.pytorch.org/t/weight-quantization/50261 "2019-07-10T14:55:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin\_sabet](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/amin_sabet/32/12866_2.png) [@amin\_sabet](https://discuss.pytorch.org/u/amin_sabet)\
**Post date:** [July 10, 2019, 2:55pm UTC](https://discuss.pytorch.org/t/weight-quantization/50261/1 "2019-07-10T14:55:01Z")

</div>

I’m trying to implement the fixed point version of VGG 16. I want to start with the pre-trained VGG 16 with the floating point weight precision, then I wand to add a quantization layer before each convolutional layer which quantized the floating point weights into fixed point format (e.g., 8bits ) before multiplied by feature map in conolutional layers. My quantization function is :

wq = clip(round(w/stp), a,b)

where w,wq,stp, a and b are floating point weight, quantized weight, step size , min value and max value, respectively.

Then I want to fine tune my model with the quantized wieght.  
So far, I have defined a new layer as quantization layer which accept the floating point weight as the input and returns the quantized value of weight. here is my questions:

1. Is the the best way to impeliment fixed point network?
2. Do I need to define a new backward method to be used during the traning(fine tuning) process ?
3. How can I feed the weight of each convolution layer to the quantization layer ? or How can I use this layer in my model architecture?

```auto
class Linear_Quantization(nn.Module):
  def __init__ (self):
    super(). __init__ ()
    self.bit_width=8
    self.step_size = Parameter(torch.Tensor(1))
  
  def forward(self, x):
    x = torch.round(x/self.step_size) 
    x = torch.clamp(x, min = -2**(self.bit_width-1) , max = (2**(self.bit_width-1)-1))
    x = x*self.step_size
    return x

```

---

<div class="post-metadata">

**Author:** ![Sairam954](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sairam954/32/48574_2.png) [@Sairam954](https://discuss.pytorch.org/u/Sairam954)\
**Post date:** [February 23, 2020, 3:54am UTC](https://discuss.pytorch.org/t/weight-quantization/50261/2 "2020-02-23T03:54:35Z")

</div>

Hey Amin,  
Were you able find answers for these, I am trying to solve similar issue it would be helpful if you can share the information

---

<div class="post-metadata">

**Author:** ![amin\_sabet](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/amin_sabet/32/12866_2.png) [@amin\_sabet](https://discuss.pytorch.org/u/amin_sabet)\
**Post date:** [February 23, 2020, 1:41pm UTC](https://discuss.pytorch.org/t/weight-quantization/50261/3 "2020-02-23T13:41:53Z")

</div>

Hey Samira,

Recently, Pytorch has provided a wide range of quantization approaches. You can find more details [here](https://pytorch.org/docs/stable/quantization.html).

Regards  
Amin

---

<div class="post-metadata">

**Author:** ![Sairam954](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sairam954/32/48574_2.png) [@Sairam954](https://discuss.pytorch.org/u/Sairam954)\
**Post date:** [March 1, 2020, 10:06pm UTC](https://discuss.pytorch.org/t/weight-quantization/50261/4 "2020-03-01T22:06:33Z")

</div>

Thank you, Amin  
I was looking to quantize a model to 4 bit, currently pytorch has it implemented for 8 bit. I was trying to create my own Qconfig and provide the FakeQuantization with minlevel and maxlevel. Do you think this is the right way of achieving it?

---

<div class="post-metadata">

**Author:** ![mg1371](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/d6d6ee/32.png) [@mg1371](https://discuss.pytorch.org/u/mg1371)\
**Post date:** [April 6, 2020, 7:41pm UTC](https://discuss.pytorch.org/t/weight-quantization/50261/5 "2020-04-06T19:41:48Z")

</div>

I am trying to do the same thing Sairam954. Has it worked for you that way? Technically, that makes sense.
