Skip to content

Commit

Permalink
Port torch GPTQ to 3.x (#1408)
Browse files Browse the repository at this point in the history
Signed-off-by: yiliu30 <[email protected]>
  • Loading branch information
yiliu30 authored Nov 27, 2023
1 parent bb60e33 commit 9150181
Show file tree
Hide file tree
Showing 15 changed files with 1,595 additions and 121 deletions.
1 change: 1 addition & 0 deletions .azure-pipelines/scripts/ut/run_itrex.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ echo "run itrex ut..."

# prepare itrex
git clone https://github.com/intel/intel-extension-for-transformers.git /intel-extension-for-transformers
cd /intel-extension-for-transformers && git rev-parse --short HEAD
bash /intel-extension-for-transformers/.github/workflows/script/prepare_env.sh
bash /intel-extension-for-transformers/.github/workflows/script/install_binary.sh

Expand Down
34 changes: 22 additions & 12 deletions neural_compressor/common/base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,16 @@ def from_dict(cls, config_dict, str2operator=None):
Returns:
The constructed config.
"""
config = cls(**config_dict.get(GLOBAL, {}))
operator_config = config_dict.get(LOCAL, {})
if operator_config:
for op_name, op_config in operator_config.items():
config.set_local(op_name, cls(**op_config))
return config
if GLOBAL not in config_dict and LOCAL not in config_dict:
config = cls(**config_dict)
return config
else:
config = cls(**config_dict.get(GLOBAL, {}))
operator_config = config_dict.get(LOCAL, {})
if operator_config:
for op_name, op_config in operator_config.items():
config.set_local(op_name, cls(**op_config))
return config

@classmethod
def to_diff_dict(cls, instance) -> Dict[str, Any]:
Expand Down Expand Up @@ -201,11 +205,11 @@ def to_config_mapping(
global_config = config.global_config
op_type_config_dict, op_name_config_dict = config._get_op_name_op_type_config()
for op_name, op_type in model_info:
config_mapping.setdefault(op_type, OrderedDict())[op_name] = global_config
config_mapping[(op_type, op_name)] = global_config
if op_type in op_type_config_dict:
config_mapping[op_type][op_name] = op_name_config_dict[op_type]
config_mapping[(op_type, op_name)] = op_name_config_dict[op_type]
if op_name in op_name_config_dict:
config_mapping[op_type][op_name] = op_name_config_dict[op_name]
config_mapping[(op_type, op_name)] = op_name_config_dict[op_name]
return config_mapping

@staticmethod
Expand Down Expand Up @@ -234,9 +238,15 @@ def to_dict(self, params_list=[], operator2str=None):
return result

@classmethod
def from_dict(cls, config_dict, str2operator=None):
# TODO(Yi)
pass
def from_dict(cls, config_dict: OrderedDict[str, Dict], config_registry: Dict[str, BaseConfig]):
assert len(config_dict) >= 1, "The config dict must include at least one configuration."
num_configs = len(config_dict)
name, value = next(iter(config_dict.items()))
config = config_registry[name].from_dict(value)
for _ in range(num_configs - 1):
name, value = next(iter(config_dict.items()))
config += config_registry[name].from_dict(value)
return config

def to_json_string(self, use_diff: bool = False) -> str:
return json.dumps(self.to_dict(), indent=2) + "\n"
Expand Down
1 change: 1 addition & 0 deletions neural_compressor/common/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
BASE_CONFIG = "base_config"
COMPOSABLE_CONFIG = "composable_config"
RTN_WEIGHT_ONLY_QUANT = "rtn_weight_only_quant"
GPTQ = "gptq"
DUMMY_CONFIG = "dummy_config"
4 changes: 3 additions & 1 deletion neural_compressor/torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
# limitations under the License.

from neural_compressor.torch.utils import register_algo
from neural_compressor.torch.algorithms import rtn_quantize_entry
from neural_compressor.torch.algorithms import rtn_quantize_entry, gptq_quantize_entry

from neural_compressor.torch.quantization import (
quantize,
RTNWeightQuantConfig,
get_default_rtn_config,
DummyConfig,
get_default_dummy_config,
GPTQConfig,
get_default_gptq_config,
)
3 changes: 2 additions & 1 deletion neural_compressor/torch/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
# limitations under the License.


from neural_compressor.torch.algorithms.rtn_quantize import rtn_quantize_entry
from neural_compressor.torch.algorithms.weight_only_algos import rtn_quantize_entry
from neural_compressor.torch.algorithms.weight_only_algos import gptq_quantize_entry
Loading

0 comments on commit 9150181

Please sign in to comment.