Skip to content

Commit

Permalink
Fix LLM weight get (#991)
Browse files Browse the repository at this point in the history
Signed-off-by: Mengni Wang <mengni.wang@intel.com>
  • Loading branch information
mengniwang95 committed Jun 14, 2023
1 parent 6a3c643 commit 85c6a0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
7 changes: 5 additions & 2 deletions neural_compressor/adaptor/ox_utils/calibration.py
Expand Up @@ -25,7 +25,7 @@
import copy
import logging
import sys

import os
import numpy as np
import onnx
import onnxruntime
Expand Down Expand Up @@ -703,5 +703,8 @@ def calib_smooth(self, percentile, op_types, q_config):
max_vals_per_channel[key] = max_val_per_channel
shape_infos[key] = output_dicts[key][0].shape
for item in val:
shape_infos[item[1][1]] = numpy_helper.to_array(self.model_wrapper.get_initializer(item[1][1])).shape
shape_infos[item[1][1]] = numpy_helper.to_array(
self.model_wrapper.get_initializer(item[1][1]),
base_dir=os.path.dirname(self.model_wrapper.model_path) if \
self.model_wrapper.model_path is not None else "").shape
return max_vals_per_channel, shape_infos, tensors_to_node
20 changes: 16 additions & 4 deletions neural_compressor/adaptor/ox_utils/smooth_quant.py
Expand Up @@ -182,7 +182,10 @@ def recover(self):
if key not in self.tensor_scales_info:
continue
input = node_info[1][1]
weight = numpy_helper.to_array(self.model.get_initializer(input))
weight = numpy_helper.to_array(
self.model.get_initializer(input),
base_dir=os.path.dirname(self.model.model_path) if \
self.model.model_path is not None else "")
scale = self.tensor_scales_info[key]
new_weight = weight * scale
self.model.set_initializer(input, new_weight)
Expand Down Expand Up @@ -467,7 +470,10 @@ def _get_smooth_scales(self, alpha, target_list=[]):
node = self.model.input_name_to_nodes[node_info[1][1]][0]
if len(target_list) > 0 and node_info[0] not in target_list:
continue
weight = numpy_helper.to_array(self.model.get_initializer(node_info[1][1]))
weight = numpy_helper.to_array(
self.model.get_initializer(node_info[1][1]),
base_dir=os.path.dirname(self.model.model_path) if \
self.model.model_path is not None else "")
if (len(weight.shape) == 4 and weight.shape[1] != 1) or \
(node.op_type == 'Gemm' and is_B_transposed(node)):
weight = np.moveaxis(weight, 0, 1)
Expand All @@ -479,7 +485,10 @@ def _get_smooth_scales(self, alpha, target_list=[]):
weights_in_channel_max = []
for node_info in nodes:
node = self.model.input_name_to_nodes[node_info[1][1]][0]
weight = numpy_helper.to_array(self.model.get_initializer(node_info[1][1]))
weight = numpy_helper.to_array(
self.model.get_initializer(node_info[1][1]),
base_dir=os.path.dirname(self.model.model_path) if \
self.model.model_path is not None else "")
if (len(weight.shape) == 4 and weight.shape[1] != 1) or \
(node.op_type == 'Gemm' and is_B_transposed(node)):
weight = np.moveaxis(weight, 0, 1)
Expand Down Expand Up @@ -565,7 +574,10 @@ def _adjust_weights(self, scales):
continue
input = node_info[1][1]
node = self.model.input_name_to_nodes[input][0]
weight = numpy_helper.to_array(self.model.get_initializer(input))
weight = numpy_helper.to_array(
self.model.get_initializer(input),
base_dir=os.path.dirname(self.model.model_path) if \
self.model.model_path is not None else "")
if len(weight.shape) == 2:
scale = np.expand_dims(scales[key], axis=0) if \
node.op_type == 'Gemm' and is_B_transposed(node) else\
Expand Down

0 comments on commit 85c6a0f

Please sign in to comment.