-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquantization.py
32 lines (24 loc) · 935 Bytes
/
quantization.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
import torch
def quant_signed_15(original, bit=5):
bit = bit - 2
original = original.clamp(max=1.875,min=-1.875)
#print("Activation result quantization")
torch.set_printoptions(precision=bit)
return ((original * (2**bit)).int()) / (2**bit)
def quant_signed_05(original, bit=5):
bit = bit - 1
original = original.clamp(max=0.9375,min=-0.9375)
torch.set_printoptions(precision=bit)
return ((original * (2**bit)).int()) / (2**bit)
# Quant with 1 integer bit
def quant_signed_1(original, bit=6):
bit = bit - 2
original = original.clamp(max=1.9375,min=-1.9375)
torch.set_printoptions(precision=bit)
return ((original * (2**bit)).int()) / (2**bit)
# Quant with no integer bit
def quant_signed_0(original, bit=6):
bit = bit - 1
original = original.clamp(max=0.96875,min=-0.96875)
torch.set_printoptions(precision=bit)
return ((original * (2**bit)).int()) / (2**bit)