LLM: Add speculative benchmark on CPU/XPU (#10464)
Add speculative benchmark on CPU/XPU.
This commit is contained in:
parent
28c315a5b9
commit
5a5fd5af5b
2 changed files with 161 additions and 0 deletions
|
|
@ -30,5 +30,7 @@ test_api:
|
|||
# - "transformer_int4_fp16_gpu_win" # on Intel GPU for Windows, use fp16 for non-linear layer
|
||||
# - "transformer_int4_loadlowbit_gpu_win" # on Intel GPU for Windows using load_low_bit API. Please make sure you have used the save.py to save the converted low bit model
|
||||
# - "deepspeed_optimize_model_gpu" # deepspeed autotp on Intel GPU
|
||||
# - "speculative_cpu"
|
||||
# - "speculative_gpu"
|
||||
cpu_embedding: False # whether put embedding to CPU (only avaiable now for gpu win related test_api)
|
||||
streaming: False # whether output in streaming way (only avaiable now for gpu win related test_api)
|
||||
|
|
|
|||
|
|
@ -102,6 +102,10 @@ def run_model(repo_id, test_api, in_out_pairs, local_model_hub=None, warm_up=1,
|
|||
result = run_bigdl_ipex_int8(repo_id, local_model_hub, in_out_pairs, warm_up, num_trials, num_beams, batch_size)
|
||||
elif test_api == 'deepspeed_optimize_model_gpu':
|
||||
result = run_deepspeed_optimize_model_gpu(repo_id, local_model_hub, in_out_pairs, warm_up, num_trials, num_beams, low_bit, batch_size)
|
||||
elif test_api == 'speculative_cpu':
|
||||
result = run_speculative_cpu(repo_id, local_model_hub, in_out_pairs, warm_up, num_trials, num_beams, batch_size)
|
||||
elif test_api == 'speculative_gpu':
|
||||
result = run_speculative_gpu(repo_id, local_model_hub, in_out_pairs, warm_up, num_trials, num_beams, batch_size)
|
||||
|
||||
for in_out_pair in in_out_pairs:
|
||||
if result and result[in_out_pair]:
|
||||
|
|
@ -1523,6 +1527,161 @@ def run_deepspeed_optimize_model_gpu(repo_id,
|
|||
return result
|
||||
|
||||
|
||||
def run_speculative_cpu(repo_id,
|
||||
local_model_hub,
|
||||
in_out_pairs,
|
||||
warm_up,
|
||||
num_trials,
|
||||
num_beams,
|
||||
batch_size):
|
||||
from bigdl.llm.transformers import AutoModel, AutoModelForCausalLM
|
||||
from transformers import AutoTokenizer, LlamaTokenizer
|
||||
from bigdl.llm.transformers.convert import get_enable_ipex
|
||||
|
||||
_enable_ipex = get_enable_ipex()
|
||||
|
||||
model_path = get_model_path(repo_id, local_model_hub)
|
||||
|
||||
st = time.perf_counter()
|
||||
if repo_id in CHATGLM_IDS:
|
||||
model = AutoModel.from_pretrained(model_path, load_in_low_bit='bf16', trust_remote_code=True, torch_dtype=torch.bfloat16,
|
||||
use_cache=True, torchscript=True, speculative=True)
|
||||
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
||||
elif repo_id in LLAMA_IDS:
|
||||
model = AutoModelForCausalLM.from_pretrained(model_path, load_in_low_bit='bf16', trust_remote_code=True, torch_dtype=torch.bfloat16,
|
||||
use_cache=True, torchscript=True, speculative=True)
|
||||
tokenizer = LlamaTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
||||
else:
|
||||
model = AutoModelForCausalLM.from_pretrained(model_path, load_in_low_bit='bf16', trust_remote_code=True, torch_dtype=torch.bfloat16,
|
||||
use_cache=True, torchscript=True, speculative=True)
|
||||
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
||||
if tokenizer.pad_token is None:
|
||||
tokenizer.pad_token = tokenizer.eos_token
|
||||
|
||||
end = time.perf_counter()
|
||||
load_time = end - st
|
||||
print(">> loading of model costs {}s".format(load_time))
|
||||
|
||||
result = {}
|
||||
with torch.inference_mode():
|
||||
for in_out in in_out_pairs:
|
||||
in_out_len = in_out.split("-")
|
||||
in_len = int(in_out_len[0])
|
||||
out_len = int(in_out_len[1])
|
||||
# As different tokenizer has different encodings,
|
||||
# in_len.txt maybe shorter than we need,
|
||||
# use much longer context to make sure input length
|
||||
test_length = min(in_len*2, 8192)
|
||||
while test_length not in [32, 256, 1024, 2048, 8192]:
|
||||
test_length = test_length * 2
|
||||
input_str = open(f"prompt/{test_length}.txt", 'r').read()
|
||||
# As different tokenizer has different encodings,
|
||||
# slice the input_ids to ensure the prompt length is required length.
|
||||
input_ids = tokenizer.encode(input_str, return_tensors="pt")
|
||||
input_ids = input_ids[:, :in_len]
|
||||
true_str = tokenizer.batch_decode(input_ids)[0]
|
||||
input_list = [true_str] * batch_size
|
||||
inputs = tokenizer(input_list, return_tensors="pt")
|
||||
input_ids = inputs.input_ids
|
||||
attention_mask = inputs.attention_mask
|
||||
actual_in_len = input_ids.shape[1]
|
||||
result[in_out] = []
|
||||
for i in range(num_trials + warm_up):
|
||||
st = time.perf_counter()
|
||||
if _enable_ipex:
|
||||
output_ids = model.generate(input_ids, do_sample=False, max_new_tokens=out_len,
|
||||
num_beams=num_beams, attention_mask=attention_mask)
|
||||
else:
|
||||
output_ids = model.generate(input_ids, do_sample=False, max_new_tokens=out_len,
|
||||
num_beams=num_beams)
|
||||
end = time.perf_counter()
|
||||
print("model generate cost: " + str(end - st))
|
||||
output = tokenizer.batch_decode(output_ids)
|
||||
print(output[0])
|
||||
actual_out_len = output_ids.shape[1] - actual_in_len
|
||||
if i >= warm_up:
|
||||
e2e_time = end - st
|
||||
rest_cost_mean = (e2e_time - model.first_token_time)/(model.n_token_generated - 1)
|
||||
result[in_out].append([model.first_token_time, rest_cost_mean, 0,
|
||||
actual_in_len, actual_out_len, load_time])
|
||||
return result
|
||||
|
||||
|
||||
def run_speculative_gpu(repo_id,
|
||||
local_model_hub,
|
||||
in_out_pairs,
|
||||
warm_up,
|
||||
num_trials,
|
||||
num_beams,
|
||||
batch_size):
|
||||
from bigdl.llm.transformers import AutoModel, AutoModelForCausalLM
|
||||
from transformers import AutoTokenizer, LlamaTokenizer
|
||||
|
||||
model_path = get_model_path(repo_id, local_model_hub)
|
||||
|
||||
st = time.perf_counter()
|
||||
if repo_id in CHATGLM_IDS:
|
||||
model = AutoModel.from_pretrained(model_path, load_in_low_bit='fp16', trust_remote_code=True, torch_dtype=torch.float16,
|
||||
use_cache=True, speculative=True)
|
||||
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
||||
model = model.to('xpu')
|
||||
elif repo_id in LLAMA_IDS:
|
||||
model = AutoModelForCausalLM.from_pretrained(model_path, load_in_low_bit='fp16', trust_remote_code=True, torch_dtype=torch.float16,
|
||||
use_cache=True, speculative=True)
|
||||
tokenizer = LlamaTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
||||
model = model.to('xpu')
|
||||
else:
|
||||
model = AutoModelForCausalLM.from_pretrained(model_path, load_in_low_bit='fp16', trust_remote_code=True, torch_dtype=torch.float16,
|
||||
use_cache=True, speculative=True)
|
||||
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
||||
model = model.to('xpu')
|
||||
end = time.perf_counter()
|
||||
load_time = end - st
|
||||
print(">> loading of model costs {}s".format(load_time))
|
||||
|
||||
result = {}
|
||||
with torch.inference_mode():
|
||||
for in_out in in_out_pairs:
|
||||
in_out_len = in_out.split("-")
|
||||
in_len = int(in_out_len[0])
|
||||
out_len = int(in_out_len[1])
|
||||
# As different tokenizer has different encodings,
|
||||
# in_len.txt maybe shorter than we need,
|
||||
# use much longer context to make sure input length
|
||||
test_length = min(in_len*2, 8192)
|
||||
while test_length not in [32, 256, 1024, 2048, 8192]:
|
||||
test_length = test_length * 2
|
||||
input_str = open(f"prompt/{test_length}.txt", 'r').read()
|
||||
# As different tokenizer has different encodings,
|
||||
# slice the input_ids to ensure the prompt length is required length.
|
||||
input_ids = tokenizer.encode(input_str, return_tensors="pt")
|
||||
input_ids = input_ids[:, :in_len]
|
||||
true_str = tokenizer.batch_decode(input_ids)[0]
|
||||
input_list = [true_str] * batch_size
|
||||
input_ids = tokenizer(input_list, return_tensors="pt").input_ids.to(model.device)
|
||||
actual_in_len = input_ids.shape[1]
|
||||
result[in_out] = []
|
||||
for i in range(num_trials + warm_up):
|
||||
st = time.perf_counter()
|
||||
output_ids = model.generate(input_ids, do_sample=False, max_new_tokens=out_len,
|
||||
num_beams=num_beams)
|
||||
torch.xpu.synchronize()
|
||||
end = time.perf_counter()
|
||||
output_ids = output_ids.cpu()
|
||||
print("model generate cost: " + str(end - st))
|
||||
output = tokenizer.batch_decode(output_ids)
|
||||
actual_out_len = output_ids.shape[1] - actual_in_len
|
||||
print(output[0])
|
||||
if i >= warm_up:
|
||||
e2e_time = end - st
|
||||
rest_cost_mean = (e2e_time - model.first_token_time)/(model.n_token_generated - 1)
|
||||
result[in_out].append([model.first_token_time, rest_cost_mean, 0,
|
||||
actual_in_len, actual_out_len, load_time])
|
||||
del model
|
||||
torch.xpu.empty_cache()
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from omegaconf import OmegaConf
|
||||
conf = OmegaConf.load(f'{current_dir}/config.yaml')
|
||||
|
|
|
|||
Loading…
Reference in a new issue