diff options
author | Jan Buethe <jbuethe@amazon.de> | 2023-08-01 09:28:25 +0300 |
---|---|---|
committer | Jan Buethe <jbuethe@amazon.de> | 2023-08-01 09:28:25 +0300 |
commit | 1fbc5fdd4ee06c48e95afb2046b5645df61545be (patch) | |
tree | 83b0a4e1c0f5e94312d3020b24a8649b4d04ef43 | |
parent | aca390df18785abf70823879d8a071ba8997fee8 (diff) |
added auto-scaling to wexchange
-rw-r--r-- | dnn/torch/weight-exchange/wexchange/c_export/common.py | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/dnn/torch/weight-exchange/wexchange/c_export/common.py b/dnn/torch/weight-exchange/wexchange/c_export/common.py index b03ee7a0..e5263bf2 100644 --- a/dnn/torch/weight-exchange/wexchange/c_export/common.py +++ b/dnn/torch/weight-exchange/wexchange/c_export/common.py @@ -170,6 +170,21 @@ def print_sparse_weight(writer, A, name, scale=1/128, have_diag=True, quantize=F 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 @@ def print_linear_layer(writer : CWriter, nb_inputs, nb_outputs = weight.shape if scale is None: - raise ValueError("None scale case not implemented yet.") - + scale = compute_scaling(weight) if sparse: |