shithub: opus

Download patch

ref: 1fbc5fdd4ee06c48e95afb2046b5645df61545be
parent: aca390df18785abf70823879d8a071ba8997fee8
author: Jan Buethe <jbuethe@amazon.de>
date: Tue Aug 1 04:28:25 EDT 2023

added auto-scaling to wexchange

--- a/dnn/torch/weight-exchange/wexchange/c_export/common.py
+++ b/dnn/torch/weight-exchange/wexchange/c_export/common.py
@@ -170,6 +170,21 @@
     return Aq
 
 
+
+def compute_scaling(weight):
+    """ computes optimal scaling vector for weight of shape (features_in, features_out) """
+
+    n_in, _ = weight.shape
+    n_in2 = 2 * (n_in // 2)
+
+    weight_sums = np.abs(weight[: n_in2 : 2]) + np.abs(weight[1 : n_in : 2])
+    weight_max = weight_sums.max(axis=0)
+    if n_in % 2: weight_max = np.maximum(weight_max, np.abs(weight[-1]))
+
+    scale = weight_max / 127
+
+    return scale
+
 def qn(string):
     if string == "NULL": return string
     else: return '"' + string + '"'
@@ -212,8 +227,7 @@
     nb_inputs, nb_outputs = weight.shape
 
     if scale is None:
-        raise ValueError("None scale case not implemented yet.")
-
+        scale = compute_scaling(weight)
 
 
     if sparse:
--