Add regression test for optimize_model on gpu (#9268)
* Add MPT model to transformer API test * Add regression test for optimize_model on gpu. --------- Co-authored-by: sgwhat <ge.song@intel.com>
This commit is contained in:
parent
733df28a2b
commit
6c9ae420a5
2 changed files with 60 additions and 1 deletions
58
python/llm/test/inference_gpu/test_optimize_model.py
Normal file
58
python/llm/test/inference_gpu/test_optimize_model.py
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
#
|
||||||
|
# Copyright 2016 The BigDL Authors.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
#
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import os
|
||||||
|
|
||||||
|
from bigdl.llm.transformers import AutoModelForCausalLM, AutoModel
|
||||||
|
from transformers import LlamaTokenizer, AutoTokenizer
|
||||||
|
|
||||||
|
device = os.environ['DEVICE']
|
||||||
|
print(f'Running on {device}')
|
||||||
|
if device == 'xpu':
|
||||||
|
import intel_extension_for_pytorch as ipex
|
||||||
|
|
||||||
|
prompt = "Once upon a time, there existed a little girl who liked to have adventures. She wanted to go to places and meet new people, and have fun"
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('Model, Tokenizer, model_path',[
|
||||||
|
(AutoModelForCausalLM, AutoTokenizer, os.environ.get('MPT_7B_ORIGIN_PATH')),
|
||||||
|
(AutoModelForCausalLM, AutoTokenizer, os.environ.get('FALCON_7B_ORIGIN_PATH')),
|
||||||
|
])
|
||||||
|
def test_optimize_model(Model, Tokenizer, model_path):
|
||||||
|
tokenizer = Tokenizer.from_pretrained(model_path, trust_remote_code=True)
|
||||||
|
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
|
||||||
|
|
||||||
|
model = Model.from_pretrained(model_path,
|
||||||
|
load_in_4bit=True,
|
||||||
|
optimize_model=False,
|
||||||
|
trust_remote_code=True)
|
||||||
|
model = model.to(device)
|
||||||
|
logits_base_model = (model(input_ids)).logits
|
||||||
|
|
||||||
|
model = Model.from_pretrained(model_path,
|
||||||
|
load_in_4bit=True,
|
||||||
|
optimize_model=True,
|
||||||
|
trust_remote_code=True)
|
||||||
|
model = model.to(device)
|
||||||
|
logits_optimized_model = (model(input_ids)).logits
|
||||||
|
|
||||||
|
diff = abs(logits_base_model - logits_optimized_model).flatten()
|
||||||
|
|
||||||
|
assert any(diff) is False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
pytest.main([__file__])
|
||||||
|
|
@ -27,12 +27,13 @@ if device == 'xpu':
|
||||||
import intel_extension_for_pytorch as ipex
|
import intel_extension_for_pytorch as ipex
|
||||||
|
|
||||||
@pytest.mark.parametrize('prompt, answer', [
|
@pytest.mark.parametrize('prompt, answer', [
|
||||||
('What is the capital of France?\n\n','Paris')
|
('What is the capital of France?\n\n', 'Paris')
|
||||||
])
|
])
|
||||||
@pytest.mark.parametrize('Model, Tokenizer, model_path',[
|
@pytest.mark.parametrize('Model, Tokenizer, model_path',[
|
||||||
(AutoModelForCausalLM, LlamaTokenizer, os.environ.get('LLAMA2_7B_ORIGIN_PATH')),
|
(AutoModelForCausalLM, LlamaTokenizer, os.environ.get('LLAMA2_7B_ORIGIN_PATH')),
|
||||||
(AutoModel, AutoTokenizer, os.environ.get('CHATGLM2_6B_ORIGIN_PATH')),
|
(AutoModel, AutoTokenizer, os.environ.get('CHATGLM2_6B_ORIGIN_PATH')),
|
||||||
(AutoModelForCausalLM, AutoTokenizer, os.environ.get('FALCON_7B_ORIGIN_PATH')),
|
(AutoModelForCausalLM, AutoTokenizer, os.environ.get('FALCON_7B_ORIGIN_PATH')),
|
||||||
|
(AutoModelForCausalLM, AutoTokenizer, os.environ.get('MPT_7B_ORIGIN_PATH')),
|
||||||
])
|
])
|
||||||
def test_completion(Model, Tokenizer, model_path, prompt, answer):
|
def test_completion(Model, Tokenizer, model_path, prompt, answer):
|
||||||
tokenizer = Tokenizer.from_pretrained(model_path, trust_remote_code=True)
|
tokenizer = Tokenizer.from_pretrained(model_path, trust_remote_code=True)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue