fix UT threshold (#10689)

This commit is contained in:
Yishuo Wang 2024-04-08 14:58:20 +08:00 committed by GitHub
parent c0cd238e40
commit 65127622aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 29 deletions

View file

@ -138,7 +138,7 @@ class Test_Optimize_Gpu_Model:
def Mistral_gpu_model(self, Name, Model, Tokenizer, model_path):
layer_before_RMSNorm = "model.layers.30"
RMSNorm_layer = "model.layers.31.input_layernorm"
lower_bound = 8e-6
lower_bound = 2e-5
self.run_optimize_gpu_model(Name, Model, Tokenizer, model_path, RMSNorm_layer, layer_before_RMSNorm, lower_bound)
def Baichuan_gpu_model(self, Name, Model, Tokenizer, model_path):

View file

@ -121,9 +121,9 @@ class Test_Optimize_Gpu_Model:
del model
del opt_model
gc.collect()
assert all(max_diff <= lower_bound for max_diff in max_diff_tensor)
@pytest.mark.parametrize('Name, Model, Tokenizer, model_path',TEST_MODEL_LIST)
def test_dynamic_functions(self, Name, Model, Tokenizer, model_path):
if Name == "MPT-7B":
@ -141,7 +141,7 @@ class Test_Optimize_Gpu_Model:
elif Name == "Qwen-7B-Chat":
self.Qwen_gpu_model(Name, Model, Tokenizer, model_path)
def MPT_7B_gpu_model(self, Name, Model, Tokenizer, model_path):
# currently only need to compare the output of one self-attention layer.
layer_norm = "transformer.blocks.31.norm_1"
@ -155,14 +155,14 @@ class Test_Optimize_Gpu_Model:
self_attn = "model.layers.31.self_attn"
lower_bound = 8e-3
self.run_optimize_gpu_model(Name, Model, Tokenizer, model_path, self_attn, layer_norm, lower_bound)
def Falcon_7B_gpu_model(self, Name, Model, Tokenizer, model_path):
# currently only compare the output of the last self-attention layer.
layer_norm = "transformer.h.31.input_layernorm"
self_attn = "transformer.h.31.self_attention"
lower_bound = 0
self.run_optimize_gpu_model(Name, Model, Tokenizer, model_path, self_attn, layer_norm, lower_bound)
def Chatglm2_gpu_model(self, Name, Model, Tokenizer, model_path):
# currently only need to compare the output of one self-attention layer.
layer_norm = "transformer.encoder.layers.27.input_layernorm"
@ -176,12 +176,12 @@ class Test_Optimize_Gpu_Model:
self_attn = "model.layers.31.self_attn"
lower_bound = 9e-3
self.run_optimize_gpu_model(Name, Model, Tokenizer, model_path, self_attn, layer_norm, lower_bound)
def Baichuan_gpu_model(self, Name, Model, Tokenizer, model_path):
# currently only need to compare the output of one self-attention layer.
layer_norm = "model.layers.31.input_layernorm"
self_attn = "model.layers.31.self_attn"
lower_bound = 2e-3
lower_bound = 8e-3
self.run_optimize_gpu_model(Name, Model, Tokenizer, model_path, self_attn, layer_norm, lower_bound)
def Qwen_gpu_model(self, Name, Model, Tokenizer, model_path):

View file

@ -13,41 +13,41 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import gc
import pytest
import torch
from ipex_llm.transformers import AutoModelForCausalLM, AutoModel
from transformers import LlamaTokenizer, AutoTokenizer
device = os.environ['DEVICE']
print(f'Running on {device}')
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"
TEST_MODEL_LIST = [
("Qwen-7B-Chat", AutoModelForCausalLM, AutoTokenizer, os.environ.get('QWEN_7B_ORIGIN_PATH')),
("Mistral-7B-Instruct-v0.1", AutoModelForCausalLM, AutoTokenizer, os.environ.get('MISTRAL_7B_INSTRUCT_V0_1_ORIGIN_PATH')),
("Llama2-7B", AutoModelForCausalLM, LlamaTokenizer, os.environ.get('LLAMA2_7B_ORIGIN_PATH'))
]
class Test_Optimize_Gpu_Model:
def setup_method(self):
self.layer_outputs = []
self.pre_layer_outputs = []
def run_optimize_gpu_model(self, Name, Model, Tokenizer, model_path, MLP_layer, layer_before_MLP, lower_bound):
with torch.inference_mode():
def pre_forward_hook(module, input, output, layer_name):
self.pre_layer_outputs.append(output)
def forward_hook(module, input, output, layer_name):
self.layer_outputs.append(output)
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,
@ -66,18 +66,18 @@ class Test_Optimize_Gpu_Model:
# the list `layer_output` has only one element.
layer_tensor = self.layer_outputs.pop()
model.to('cpu')
opt_model = Model.from_pretrained(model_path,
load_in_4bit=True,
optimize_model=True,
trust_remote_code=True)
opt_model = opt_model.to(device)
def replace_forward_hook(module, input, output, layer_name):
output = self.pre_layer_outputs[0]
return output
for layer_name, layer_module in opt_model.named_modules():
if layer_name == layer_before_MLP:
layer_module.register_forward_hook(
@ -91,7 +91,7 @@ class Test_Optimize_Gpu_Model:
# the list `layer_output` has only one element.
opt_layer_tensor = self.layer_outputs[0]
opt_model.to('cpu')
MLP_output_diff = []
for i, (t1, t2) in enumerate(zip(layer_tensor, opt_layer_tensor)):
if isinstance(t1, torch.Tensor) and isinstance(t2, torch.Tensor):
@ -99,7 +99,7 @@ class Test_Optimize_Gpu_Model:
else:
for i, (t3, t4) in enumerate(zip(t1, t2)):
MLP_output_diff.append(t3 - t4)
max_diff_tensor = [torch.max(item).item() for item in MLP_output_diff]
print(max_diff_tensor)
torch.xpu.empty_cache()
@ -107,7 +107,7 @@ class Test_Optimize_Gpu_Model:
del opt_model
gc.collect()
assert all(max_diff <= lower_bound for max_diff in max_diff_tensor)
@pytest.mark.parametrize('Name, Model, Tokenizer, model_path',TEST_MODEL_LIST)
def test_dynamic_functions(self, Name, Model, Tokenizer, model_path):
if Name == "Qwen-7B-Chat":
@ -117,25 +117,25 @@ class Test_Optimize_Gpu_Model:
elif Name == "Llama2-7B":
self.Llama2_7B_gpu_model(Name, Model, Tokenizer, model_path)
def Qwen_7B_gpu_model(self, Name, Model, Tokenizer, model_path):
# currently only compare the output of the last mlp layer.
layer_before_MLP = "transformer.h.31.ln_2"
MLP_layer = "transformer.h.31.mlp"
lower_bound = 0
self.run_optimize_gpu_model(Name, Model, Tokenizer, model_path, MLP_layer, layer_before_MLP, lower_bound)
def Mistral_7B_Instruct_gpu_model(self, Name, Model, Tokenizer, model_path):
# currently only compare the output of the last mlp layer.
layer_before_MLP = "model.layers.31.post_attention_layernorm"
MLP_layer = "model.layers.31.mlp"
lower_bound = 0
self.run_optimize_gpu_model(Name, Model, Tokenizer, model_path, MLP_layer, layer_before_MLP, lower_bound)
def Llama2_7B_gpu_model(self, Name, Model, Tokenizer, model_path):
# The tests are actually testing the mlp layer. We can't test the mlp layer directly
# The tests are actually testing the decode layer. We can't test the mlp layer directly
# since the original Llama2 code adds residual after the mlp layer, which differs from the implementation of bigdl
layer_before_Decoder = "model.layers.30"
Decoder_layer = "model.layers.31"
lower_bound = 5e-2
lower_bound = 1e-1
self.run_optimize_gpu_model(Name, Model, Tokenizer, model_path, Decoder_layer, layer_before_Decoder, lower_bound)