# Implementing Quantized Linear Layer in Numpy

**URL:** <https://discuss.pytorch.org/t/implementing-quantized-linear-layer-in-numpy/197716>\
**Category:** quantization\
**Created:** [February 26, 2024, 3:51pm UTC](https://discuss.pytorch.org/t/implementing-quantized-linear-layer-in-numpy/197716 "2024-02-26T15:51:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![costelljoe](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/costelljoe/32/30552_2.png) [@costelljoe](https://discuss.pytorch.org/u/costelljoe)\
**Post date:** [February 26, 2024, 3:51pm UTC](https://discuss.pytorch.org/t/implementing-quantized-linear-layer-in-numpy/197716/1 "2024-02-26T15:51:03Z")

</div>

I’m trying to implement a Torch quantized linear layer in Numpy so that I can verify the implementation before translating to my own embedded C code. I have a basic implementation below which correctly quantizes the inputs but the output of the linear layer doesn’t match the torch outputs.

My current code is based on [this post](https://discuss.pytorch.org/t/is-bias-quantized-while-doing-pytorch-static-quantization/146416/6), and I’m using the qnnpack backend.

Any tips are appreciated!

```auto
# I have float input 'x', linear layer 'fc', and quant stub 'quantx'

# quantize x
x_q = np.round((x / quantx_scale) + quantx_zero_point)
x_q = np.clip(x_q, 0, 255).astype(np.uint8)

# linear layer
matmul_out = np.matmul(x_q, fc_weight.T)
bias_q = np.round(fc_bias / (quantx_scale * fc_weight_scale)).astype(np.int32)
scale_factor = quantx_scale * fc_weight_scale / fc_scale
outq = np.round((matmul_out + bias_q) * scale_factor + fc_zero_point)
outq = np.clip(outq, 0, 255).astype(np.uint8)

```

I’m extracting the weights like:

```auto
fc_weight = fc.weight().int_repr().numpy()
fc_weight_scale = fc.weight().q_scale()
fc_bias = fc.bias().detach().numpy()
fc_scale = fc.scale
fc_zero_point = fc.zero_point
quantx_scale = quantx.scale.numpy()[0]
quantx_zero_point = quantx.zero_point.numpy()[0]

```

---

<div class="post-metadata">

**Author:** ![chenster\_liu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chenster_liu/32/67305_2.png) [@chenster\_liu](https://discuss.pytorch.org/u/chenster_liu)\
**Post date:** [March 21, 2024, 2:33pm UTC](https://discuss.pytorch.org/t/implementing-quantized-linear-layer-in-numpy/197716/2 "2024-03-21T14:33:41Z")

</div>

Your process looks fine. I refer to [this](https://discuss.pytorch.org/t/understanding-quantized-linear-layer/154000/3) for back engineering the QuantizedLinear OP.  
Only one point you may take care is that overflow can happen in

> matmul\_out = np.matmul(x\_q, fc\_weight.T)

Better to try the below again:

> matmul\_out = np.matmul(x\_q.astype(np.int32), fc\_weight.T.astype(np.int32))

---

<div class="post-metadata">

**Author:** ![Francisco\_Romaldo\_M](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/francisco_romaldo_m/32/69966_2.png) [@Francisco\_Romaldo\_M](https://discuss.pytorch.org/u/Francisco_Romaldo_M)\
**Post date:** [June 8, 2024, 1:24am UTC](https://discuss.pytorch.org/t/implementing-quantized-linear-layer-in-numpy/197716/3 "2024-06-08T01:24:42Z")

</div>

Check out this blog post, it explains it in detail

> **[A Manual Implementation of Quantization in PyTorch - Single Layer](https://franciscormendes.github.io/2024/05/16/quantization-1/)**
>
> IntroductionThe packaging of extremely complex techniques inside convenient wrappers in PyTorch often makes quick implementations fairly easy, it also removes the need to understand the inner workings
