From d7aef44454ca8c46e32da1fb29d2541bf0202c29 Mon Sep 17 00:00:00 2001 From: "lorenzo.lamberti" Date: Tue, 4 May 2021 23:03:08 +0000 Subject: [PATCH 1/5] fixed sporadic doubled eps_in with float value --- nemo/graph.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nemo/graph.py b/nemo/graph.py index d5d9a5b..c29dcef 100644 --- a/nemo/graph.py +++ b/nemo/graph.py @@ -262,7 +262,8 @@ def get_eps_at(self, key, eps_in, use_non_unique_name=True, verbose=False): break if type(eps) is float: eps_list.append(torch.tensor(eps)) - eps_list.append(eps) + else: + eps_list.append(eps) if len(eps_list) == 1: return eps_list[0] else: From d69523ca9f87615f5d6ab0345ec390a9d85c14d4 Mon Sep 17 00:00:00 2001 From: "lorenzo.lamberti" Date: Mon, 15 Nov 2021 17:29:21 +0000 Subject: [PATCH 2/5] use .clone().detach() instead of torch.as_tensor() --- nemo/quant/pact.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/nemo/quant/pact.py b/nemo/quant/pact.py index 0597271..3c3554d 100644 --- a/nemo/quant/pact.py +++ b/nemo/quant/pact.py @@ -50,12 +50,14 @@ def pact_quantized_requantize(t, eps_in, eps_out, D=1, exclude_requant_rounding= # re-quantize from a lower precision (larger eps_in) to a higher precision (lower eps_out) def pact_integer_requantize(t, eps_in, eps_out, D=1): + D = D.clone().detach().to(eps_in.device) eps_ratio = (D*eps_in/eps_out).round() device = t.device - return torch.as_tensor((torch.as_tensor(t, dtype=torch.int64) * torch.as_tensor(eps_ratio, dtype=torch.int64) // D), dtype=torch.float32, device=device) + return torch.as_tensor((t.clone().detach().type(torch.int64) * eps_ratio.clone().detach().type(torch.int64) // D), dtype=torch.float32, device=device) # re-quantize from a lower precision (larger eps_in) to a higher precision (lower eps_out) def pact_integer_requantize_add(*t, eps_in_list, eps_out, D=1): + D = D.clone().detach().to(eps_out.device) # unrolling the first iteration of the loop instead of using torch.zeros to init y is necessary for correct ONNX export eps_in = eps_in_list[0] eps_ratio = (D*eps_in/eps_out).round() @@ -477,7 +479,7 @@ def get_output_eps(self, eps_in_list): if type(eps_in_list) is list: self.eps_in_list = eps_in_list # for now, select the biggest eps_out as the target one - self.eps_out = max(self.eps_in_list) + self.eps_out = max(self.eps_in_list)*len(self.eps_in_list) # the second term avoid overflow self.alpha_out = 2.0**(self.precision.get_bits())-1 # D is selected as a power-of-two self.D = 2**torch.as_tensor(torch.ceil(torch.log2(self.requantization_factor * self.eps_out / min(self.eps_in_list))), dtype=torch.int64) From 0999d958218a280485c29ba0c127c3b7fe830b96 Mon Sep 17 00:00:00 2001 From: "lorenzo.lamberti" Date: Mon, 15 Nov 2021 17:35:13 +0000 Subject: [PATCH 3/5] use .clone().detach() instead of torch.as_tensor() --- nemo/transf/deploy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nemo/transf/deploy.py b/nemo/transf/deploy.py index 51eaa38..9f7257f 100644 --- a/nemo/transf/deploy.py +++ b/nemo/transf/deploy.py @@ -106,9 +106,9 @@ def _set_eps_in_pact(self, eps_in): if eps_in_new is None: continue if (m.__class__.__name__ == "PACT_Act"): - m.eps_in = torch.tensor(eps_in_new[0], requires_grad=False) + m.eps_in = eps_in_new[0].clone().detach().requires_grad_(False) if (m.__class__.__name__ == "PACT_QuantizedBatchNormNd"): - m.eps_in = torch.tensor(eps_in_new[0], requires_grad=False) + m.eps_in = eps_in_new[0].clone().detach().requires_grad_(False) if (m.__class__.__name__ == "PACT_IntegerAdd"): eps_in_list = [] for eps in eps_in_new: From fe10a0e3414c3faf70f7208877eefb88177075cb Mon Sep 17 00:00:00 2001 From: "lorenzo.lamberti" Date: Wed, 9 Feb 2022 11:37:07 +0000 Subject: [PATCH 4/5] fixed sporadic error: int object has no attribute clone --- nemo/quant/pact.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/nemo/quant/pact.py b/nemo/quant/pact.py index 3c3554d..1fa4768 100644 --- a/nemo/quant/pact.py +++ b/nemo/quant/pact.py @@ -51,6 +51,7 @@ def pact_quantized_requantize(t, eps_in, eps_out, D=1, exclude_requant_rounding= # re-quantize from a lower precision (larger eps_in) to a higher precision (lower eps_out) def pact_integer_requantize(t, eps_in, eps_out, D=1): D = D.clone().detach().to(eps_in.device) + # D = torch.tensor(D, device=eps_in.device) eps_ratio = (D*eps_in/eps_out).round() device = t.device return torch.as_tensor((t.clone().detach().type(torch.int64) * eps_ratio.clone().detach().type(torch.int64) // D), dtype=torch.float32, device=device) @@ -372,7 +373,7 @@ def set_static_precision(self, limit_at_32_bits=True, **kwargs): if not limit_at_32_bits: self.D = D else: - self.D = min(D, 2.0**(32-1-(self.precision.get_bits()))) + self.D = min(D, torch.tensor(2.0**(32-1-(self.precision.get_bits())), device = D.device)) def get_output_eps(self, eps_in): r"""Get the output quantum (:math:`\varepsilon`) given the input one. @@ -610,8 +611,9 @@ def set_output_eps(self, limit_at_32_bits=True, **kwargs): if not limit_at_32_bits: self.D = D else: - self.D = min(D, 2**(32-(self.precision.get_bits()))) - + # self.D = min(D, 2**(32-(self.precision.get_bits()))) + self.D = min(D, torch.tensor([2.0**(32-1-(self.precision.get_bits()))], device = D.device, dtype=torch.int64)) + def get_output_eps(self, eps_in): r"""Get the output quantum (:math:`\varepsilon`) given the input one. From d4cf2fd85a441f5c5a9053a43a662dec853854fc Mon Sep 17 00:00:00 2001 From: "lorenzo.lamberti" Date: Wed, 9 Feb 2022 15:17:50 +0000 Subject: [PATCH 5/5] fix merge artifact --- nemo/quant/pact.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/nemo/quant/pact.py b/nemo/quant/pact.py index 9807980..1fa4768 100644 --- a/nemo/quant/pact.py +++ b/nemo/quant/pact.py @@ -51,10 +51,7 @@ def pact_quantized_requantize(t, eps_in, eps_out, D=1, exclude_requant_rounding= # re-quantize from a lower precision (larger eps_in) to a higher precision (lower eps_out) def pact_integer_requantize(t, eps_in, eps_out, D=1): D = D.clone().detach().to(eps_in.device) -<<<<<<< HEAD # D = torch.tensor(D, device=eps_in.device) -======= ->>>>>>> 57d5f99b4c4b01e11fb60be2d97c33a5560cd442 eps_ratio = (D*eps_in/eps_out).round() device = t.device return torch.as_tensor((t.clone().detach().type(torch.int64) * eps_ratio.clone().detach().type(torch.int64) // D), dtype=torch.float32, device=device)