From b059a32fff91217da27aeb49e0d3bcb18d09f028 Mon Sep 17 00:00:00 2001 From: Ruonan Wang Date: Wed, 17 Jan 2024 14:24:35 +0800 Subject: [PATCH] LLM: add benchmark api for bigdl-llm fp16 on GPU (#9919) * add bmk for bigdl fp16 * fix --- .../llm/dev/benchmark/all-in-one/config.yaml | 1 + python/llm/dev/benchmark/all-in-one/run.py | 77 +++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/python/llm/dev/benchmark/all-in-one/config.yaml b/python/llm/dev/benchmark/all-in-one/config.yaml index a26b8ba7..580f1d42 100644 --- a/python/llm/dev/benchmark/all-in-one/config.yaml +++ b/python/llm/dev/benchmark/all-in-one/config.yaml @@ -18,6 +18,7 @@ test_api: - "pytorch_autocast_bf16" # - "transformer_autocast_bf16" # - "ipex_fp16_gpu" # on Intel GPU + # - "bigdl_fp16_gpu" # on Intel GPU # - "transformer_int4_gpu" # on Intel GPU # - "optimize_model_gpu" # on Intel GPU # - "deepspeed_transformer_int4_cpu" # on Intel SPR Server diff --git a/python/llm/dev/benchmark/all-in-one/run.py b/python/llm/dev/benchmark/all-in-one/run.py index 35591f75..16b825dc 100644 --- a/python/llm/dev/benchmark/all-in-one/run.py +++ b/python/llm/dev/benchmark/all-in-one/run.py @@ -80,6 +80,8 @@ def run_model(repo_id, test_api, in_out_pairs, local_model_hub=None, warm_up=1, result = run_pytorch_autocast_bf16(repo_id, local_model_hub, in_out_pairs, warm_up, num_trials, num_beams) elif test_api == 'ipex_fp16_gpu': result = run_ipex_fp16_gpu(repo_id, local_model_hub, in_out_pairs, warm_up, num_trials, num_beams) + elif test_api == "bigdl_fp16_gpu": + result = result = run_bigdl_fp16_gpu(repo_id, local_model_hub, in_out_pairs, warm_up, num_trials, num_beams) elif test_api == 'deepspeed_transformer_int4_cpu': result = run_deepspeed_transformer_int4_cpu(repo_id, local_model_hub, in_out_pairs, warm_up, num_trials, num_beams, low_bit) elif test_api == 'transformer_int4_gpu_win': @@ -579,6 +581,81 @@ def run_ipex_fp16_gpu(repo_id, torch.xpu.empty_cache() return result + +def run_bigdl_fp16_gpu(repo_id, + local_model_hub, + in_out_pairs, + warm_up, + num_trials, + num_beams): + from bigdl.llm.transformers import AutoModel, AutoModelForCausalLM + from transformers import AutoTokenizer, GPTJForCausalLM, LlamaTokenizer + import intel_extension_for_pytorch as 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, trust_remote_code=True, use_cache=True, + load_in_low_bit="fp16", torch_dtype=torch.float16) + 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, trust_remote_code=True, + use_cache=True, + load_in_low_bit="fp16", + torch_dtype=torch.float16) + tokenizer = LlamaTokenizer.from_pretrained(model_path, trust_remote_code=True) + model = model.to('xpu') + else: + model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, + use_cache=True, + load_in_low_bit="fp16", + torch_dtype=torch.float16) + tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) + model = model.to('xpu') + end = time.perf_counter() + print(">> loading of model costs {}s".format(end - st)) + + model = BenchmarkWrapper(model) + + 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_ids = tokenizer.encode(true_str, return_tensors="pt").to('xpu') + 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: + result[in_out].append([model.first_cost, model.rest_cost_mean, model.encoder_time, + actual_in_len, actual_out_len]) + del model + torch.xpu.empty_cache() + return result + def run_deepspeed_transformer_int4_cpu(repo_id, local_model_hub, in_out_pairs,