-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable the Combination of Multiple Algorithms within a Single Model (#…
…1616) Signed-off-by: yiliu30 <yi4.liu@intel.com>
- Loading branch information
Showing
4 changed files
with
154 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
test/3x/torch/quantization/weight_only/test_mixed_algos.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import copy | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
import torch | ||
import transformers | ||
|
||
from neural_compressor.common.utils import logger | ||
from neural_compressor.torch.quantization import GPTQConfig, RTNConfig, quantize | ||
|
||
|
||
def run_fn(model): | ||
# GPTQ uses ValueError to reduce computation when collecting input data of the first block | ||
# It's special for UTs, no need to add this wrapper in examples. | ||
with pytest.raises(ValueError): | ||
model(torch.tensor([[10, 20, 30]], dtype=torch.long)) | ||
model(torch.tensor([[40, 50, 60]], dtype=torch.long)) | ||
|
||
|
||
class TestMixedTwoAlgo: | ||
def test_mixed_gptq_and_rtn(self): | ||
with patch.object(logger, "info") as mock_info: | ||
rtn_config = RTNConfig(white_list=["lm_head"]) | ||
gptq_config = GPTQConfig(double_quant_bits=4, white_list=["transformer.*"]) | ||
combined_config = rtn_config + gptq_config | ||
logger.info(combined_config) | ||
|
||
self.tiny_gptj = transformers.AutoModelForCausalLM.from_pretrained( | ||
"hf-internal-testing/tiny-random-GPTJForCausalLM", | ||
) | ||
self.example_inputs = torch.tensor([[10, 20, 30, 40, 50, 60]], dtype=torch.long) | ||
# record label for comparison | ||
out_original_model = self.tiny_gptj(self.example_inputs)[0] | ||
model = copy.deepcopy(self.tiny_gptj) | ||
q_model = quantize(model, combined_config, run_fn=run_fn) | ||
out_q_model = q_model(self.example_inputs)[0] | ||
rtn_log = "Start to apply rtn on the model." | ||
gptq_log = "Start to apply gptq on the model." | ||
assert rtn_log in [_call[0][0] for _call in mock_info.call_args_list] | ||
assert gptq_log in [_call[0][0] for _call in mock_info.call_args_list] |