nickfraser
commited on
Commit
•
dca9b6e
1
Parent(s):
8e3c05a
Updated QOp model to fuse SmoothQuant scales with input quantization
Browse files- math_model.py +4 -4
math_model.py
CHANGED
@@ -47,9 +47,9 @@ class QuantLinear(nn.Module):
|
|
47 |
# - multiply this sum with every weight zero-point (e.g., `torch.sum(quant_input, dim=-1) * self.weight_zp`
|
48 |
# - Subtract from previous output (e.g., `quant_output -= torch.sum(quant_input, dim=-1) * self.weight_zp`)
|
49 |
# - All other code is just to make sure the broadcasting semantics work correctly
|
50 |
-
scaled_x = x * self.mul_factor
|
51 |
quant_weight = quantize(self.linear.weight, self.weight_scale, self.weight_zp, is_asym=True).to(torch.uint8)
|
52 |
-
|
|
|
53 |
quant_output = torch.nn.functional.linear(quant_input.to(torch.float32), quant_weight.to(torch.float32), None).to(torch.int32) # Convert inputs to FP32 to avoid F.linear quantizing the output to int8
|
54 |
correction = torch.sum(quant_input, dim=-1, keepdim=True).to(torch.int32) * (-self.weight_zp).to(torch.uint8).view([1]*(quant_input.ndim-1) + [self.weight_zp.nelement()]) # Correct for weight zero-point
|
55 |
quant_output = quant_output + correction
|
@@ -103,13 +103,13 @@ class QuantConv2d(nn.Module):
|
|
103 |
# - multiply this sum with every weight zero-point (e.g., `sum * self.weight_zp`
|
104 |
# - Subtract from previous output (e.g., `quant_output -= sum * self.weight_zp`)
|
105 |
# - All other code is just to make sure the broadcasting semantics work correctly
|
106 |
-
scaled_x = x * self.mul_factor
|
107 |
quant_weight = quantize(self.conv2d.weight, self.weight_scale, self.weight_zp, is_asym=True).to(torch.uint8)
|
108 |
b_shape = list(quant_weight.shape) # Used for weight zero-point correction
|
109 |
b_shape[0] = 1 # Used for weight zero-point correction
|
110 |
weight_cat = torch.ones((1,1,1,1)).broadcast_to(b_shape).to(torch.uint8) # Used for weight zero-point correction
|
111 |
quant_weight = torch.cat((quant_weight,weight_cat),dim=0).to(torch.uint8) # Create extra output channel, used for weight zero-point correction
|
112 |
-
|
|
|
113 |
quant_output = torch.nn.functional.conv2d(quant_input.to(torch.float32), quant_weight.to(torch.float32), None).to(torch.int32) # Convert inputs to FP32 to avoid F.conv2d quantizing the output to int8
|
114 |
correction = quant_output[:,-1,:,:] * (-self.weight_zp).to(torch.uint8).view([1, self.weight_zp.nelement()] + [1]*(quant_output.ndim-2)) # Correct zero-point for weight
|
115 |
quant_output = quant_output[:,:-1,:,:] + correction
|
|
|
47 |
# - multiply this sum with every weight zero-point (e.g., `torch.sum(quant_input, dim=-1) * self.weight_zp`
|
48 |
# - Subtract from previous output (e.g., `quant_output -= torch.sum(quant_input, dim=-1) * self.weight_zp`)
|
49 |
# - All other code is just to make sure the broadcasting semantics work correctly
|
|
|
50 |
quant_weight = quantize(self.linear.weight, self.weight_scale, self.weight_zp, is_asym=True).to(torch.uint8)
|
51 |
+
fused_input_scale = self.input_scale / self.mul_factor # Fuse SmoothQuant and input scales, can be computed offline
|
52 |
+
quant_input = quantize(x, fused_input_scale, self.input_zp, is_asym=False).to(torch.int8)
|
53 |
quant_output = torch.nn.functional.linear(quant_input.to(torch.float32), quant_weight.to(torch.float32), None).to(torch.int32) # Convert inputs to FP32 to avoid F.linear quantizing the output to int8
|
54 |
correction = torch.sum(quant_input, dim=-1, keepdim=True).to(torch.int32) * (-self.weight_zp).to(torch.uint8).view([1]*(quant_input.ndim-1) + [self.weight_zp.nelement()]) # Correct for weight zero-point
|
55 |
quant_output = quant_output + correction
|
|
|
103 |
# - multiply this sum with every weight zero-point (e.g., `sum * self.weight_zp`
|
104 |
# - Subtract from previous output (e.g., `quant_output -= sum * self.weight_zp`)
|
105 |
# - All other code is just to make sure the broadcasting semantics work correctly
|
|
|
106 |
quant_weight = quantize(self.conv2d.weight, self.weight_scale, self.weight_zp, is_asym=True).to(torch.uint8)
|
107 |
b_shape = list(quant_weight.shape) # Used for weight zero-point correction
|
108 |
b_shape[0] = 1 # Used for weight zero-point correction
|
109 |
weight_cat = torch.ones((1,1,1,1)).broadcast_to(b_shape).to(torch.uint8) # Used for weight zero-point correction
|
110 |
quant_weight = torch.cat((quant_weight,weight_cat),dim=0).to(torch.uint8) # Create extra output channel, used for weight zero-point correction
|
111 |
+
fused_input_scale = self.input_scale / self.mul_factor # Fuse SmoothQuant and input scales, can be computed offline
|
112 |
+
quant_input = quantize(x, fused_input_scale, self.input_zp, is_asym=False).to(torch.int8)
|
113 |
quant_output = torch.nn.functional.conv2d(quant_input.to(torch.float32), quant_weight.to(torch.float32), None).to(torch.int32) # Convert inputs to FP32 to avoid F.conv2d quantizing the output to int8
|
114 |
correction = quant_output[:,-1,:,:] * (-self.weight_zp).to(torch.uint8).view([1, self.weight_zp.nelement()] + [1]*(quant_output.ndim-2)) # Correct zero-point for weight
|
115 |
quant_output = quant_output[:,:-1,:,:] + correction
|