From 0451103a437fbdf8f79f9f2b3b88034a57601a45 Mon Sep 17 00:00:00 2001 From: Jin Qiao <89779290+JinBridger@users.noreply.github.com> Date: Tue, 19 Mar 2024 11:11:25 +0800 Subject: [PATCH] LLM: add int4+fp16 benchmark script for windows benchmarking (#10449) * LLM: add fp16 for benchmark script * remove transformer_int4_fp16_loadlowbit_gpu_win --- python/llm/dev/benchmark/all-in-one/README.md | 1 + .../llm/dev/benchmark/all-in-one/config.yaml | 1 + python/llm/dev/benchmark/all-in-one/run.py | 108 ++++++++++++++++++ 3 files changed, 110 insertions(+) diff --git a/python/llm/dev/benchmark/all-in-one/README.md b/python/llm/dev/benchmark/all-in-one/README.md index 187001e4..780ee4cc 100644 --- a/python/llm/dev/benchmark/all-in-one/README.md +++ b/python/llm/dev/benchmark/all-in-one/README.md @@ -48,6 +48,7 @@ test_api: # - "optimize_model_gpu" # on Intel GPU # - "deepspeed_transformer_int4_cpu" # on Intel SPR Server # - "transformer_int4_gpu_win" # on Intel GPU for Windows + # - "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 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) diff --git a/python/llm/dev/benchmark/all-in-one/config.yaml b/python/llm/dev/benchmark/all-in-one/config.yaml index 53d9c90f..d788427e 100644 --- a/python/llm/dev/benchmark/all-in-one/config.yaml +++ b/python/llm/dev/benchmark/all-in-one/config.yaml @@ -26,6 +26,7 @@ test_api: # - "optimize_model_gpu" # on Intel GPU # - "deepspeed_transformer_int4_cpu" # on Intel SPR Server # - "transformer_int4_gpu_win" # on Intel GPU for Windows + # - "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 cpu_embedding: False # whether put embedding to CPU (only avaiable now for gpu win related test_api) diff --git a/python/llm/dev/benchmark/all-in-one/run.py b/python/llm/dev/benchmark/all-in-one/run.py index 2acbc251..a01d18f0 100644 --- a/python/llm/dev/benchmark/all-in-one/run.py +++ b/python/llm/dev/benchmark/all-in-one/run.py @@ -86,6 +86,8 @@ def run_model(repo_id, test_api, in_out_pairs, local_model_hub=None, warm_up=1, result = run_deepspeed_transformer_int4_cpu(repo_id, local_model_hub, in_out_pairs, warm_up, num_trials, num_beams, low_bit, batch_size) elif test_api == 'transformer_int4_gpu_win': result = run_transformer_int4_gpu_win(repo_id, local_model_hub, in_out_pairs, warm_up, num_trials, num_beams, low_bit, cpu_embedding, batch_size, streaming) + elif test_api == 'transformer_int4_fp16_gpu_win': + result = run_transformer_int4_fp16_gpu_win(repo_id, local_model_hub, in_out_pairs, warm_up, num_trials, num_beams, low_bit, cpu_embedding, batch_size, streaming) elif test_api == 'transformer_int4_loadlowbit_gpu_win': # drop the results of the first time for better performance run_transformer_int4_loadlowbit_gpu_win(repo_id, local_model_hub, in_out_pairs, warm_up, num_trials, num_beams, low_bit, cpu_embedding, batch_size, streaming) @@ -910,6 +912,112 @@ def run_transformer_int4_gpu_win(repo_id, return result +def run_transformer_int4_fp16_gpu_win(repo_id, + local_model_hub, + in_out_pairs, + warm_up, + num_trials, + num_beams, + low_bit, + cpu_embedding, + batch_size, + streaming): + from bigdl.llm.transformers import AutoModel, AutoModelForCausalLM + from transformers import AutoTokenizer, GPTJForCausalLM, LlamaTokenizer, TextStreamer + import intel_extension_for_pytorch as ipex + model_path = get_model_path(repo_id, local_model_hub) + # Load model in 4 bit, + # which convert the relevant layers in the model into INT4 format + st = time.perf_counter() + if repo_id in CHATGLM_IDS: + model = AutoModel.from_pretrained(model_path, load_in_low_bit=low_bit, optimize_model=True, torch_dtype=torch.float16, + trust_remote_code=True, use_cache=True, cpu_embedding=cpu_embedding).eval() + 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=low_bit, optimize_model=True, torch_dtype=torch.float16, + trust_remote_code=True, use_cache=True, cpu_embedding=cpu_embedding).eval() + tokenizer = LlamaTokenizer.from_pretrained(model_path, trust_remote_code=True) + model = model.to('xpu') + elif repo_id in LLAVA_IDS: + llava_repo_dir = os.environ.get('LLAVA_REPO_DIR') + sys.path.append(rf"{llava_repo_dir}") + from llava.model.language_model.llava_llama import LlavaLlamaForCausalLM + model = AutoModelForCausalLM.from_pretrained(model_path, load_in_low_bit=low_bit, optimize_model=True, torch_dtype=torch.float16, + trust_remote_code=True, use_cache=True, cpu_embedding=cpu_embedding).eval() + tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) + model = model.to('xpu') + else: + model = AutoModelForCausalLM.from_pretrained(model_path, optimize_model=True, load_in_low_bit=low_bit, torch_dtype=torch.float16, + trust_remote_code=True, use_cache=True, cpu_embedding=cpu_embedding).eval() + tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) + model = model.to('xpu') + if isinstance(model, GPTJForCausalLM): + # For gpt-j model family, this optimization can provide a better performance. + model = ipex.optimize(model.eval(), inplace=True) + end = time.perf_counter() + load_time = end - st + print(">> loading of model costs {}s and {}GB".format(load_time, torch.xpu.memory.memory_reserved()/(1024**3))) + + model = BenchmarkWrapper(model) + streamer = TextStreamer(tokenizer, skip_prompt=True) + + result = {} + with torch.inference_mode(): + for in_out in in_out_pairs: + try: + 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('xpu') + actual_in_len = input_ids.shape[1] + result[in_out] = [] + for i in range(num_trials + warm_up): + st = time.perf_counter() + if streaming: + output_ids = model.generate(input_ids, do_sample=False, max_new_tokens=out_len, + num_beams=num_beams, streamer=streamer) + else: + 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) + if not streaming: + print(output[0]) + actual_out_len = output_ids.shape[1] - actual_in_len + if i >= warm_up: + result[in_out].append([model.first_cost, model.rest_cost_mean, model.encoder_time, + actual_in_len, actual_out_len, load_time, model.peak_memory]) + # torch.xpu.empty_cache() # this may make first token slower + except RuntimeError: + traceback.print_exc() + pass + torch.xpu.synchronize() + torch.xpu.empty_cache() + model.to('cpu') + torch.xpu.synchronize() + torch.xpu.empty_cache() + del model + gc.collect() + return result + + def run_transformer_int4_loadlowbit_gpu_win(repo_id, local_model_hub, in_out_pairs,