-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support pipeline parallel for glm-4v #11545
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
python/llm/example/GPU/Pipeline-Parallel-Inference/glm_4v_generate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# | ||
# 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 os | ||
import time | ||
import torch | ||
import argparse | ||
import requests | ||
|
||
from PIL import Image | ||
from ipex_llm.transformers import AutoModelForCausalLM, init_pipeline_parallel | ||
from transformers import AutoTokenizer | ||
|
||
init_pipeline_parallel() | ||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser(description='Predict Tokens using `generate()` API for THUDM/glm-4v-9b model') | ||
parser.add_argument('--repo-id-or-model-path', type=str, default="THUDM/glm-4v-9b", | ||
help='The huggingface repo id for the THUDM/glm-4v-9b model to be downloaded' | ||
', or the path to the huggingface checkpoint folder') | ||
parser.add_argument('--image-url-or-path', type=str, | ||
default='http://farm6.staticflickr.com/5268/5602445367_3504763978_z.jpg', | ||
help='The URL or path to the image to infer') | ||
parser.add_argument('--prompt', type=str, default="这是什么?", | ||
help='Prompt to infer') | ||
parser.add_argument('--n-predict', type=int, default=32, | ||
help='Max tokens to predict') | ||
parser.add_argument('--low-bit', type=str, default='sym_int4', help='The quantization type the model will convert to.') | ||
parser.add_argument('--gpu-num', type=int, default=2, help='GPU number to use') | ||
|
||
args = parser.parse_args() | ||
model_path = args.repo_id_or_model_path | ||
image_path = args.image_url_or_path | ||
|
||
model = AutoModelForCausalLM.from_pretrained(model_path, | ||
load_in_low_bit=args.low_bit, | ||
optimize_model=True, | ||
trust_remote_code=True, | ||
use_cache=True, | ||
pipeline_parallel_stages=args.gpu_num) | ||
model = model.half() | ||
|
||
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) | ||
local_rank = torch.distributed.get_rank() | ||
|
||
query = args.prompt | ||
if os.path.exists(image_path): | ||
image = Image.open(image_path) | ||
else: | ||
image = Image.open(requests.get(image_path, stream=True).raw) | ||
|
||
# here the prompt tuning refers to https://huggingface.co/THUDM/glm-4v-9b/blob/main/README.md | ||
inputs = tokenizer.apply_chat_template([{"role": "user", "image": image, "content": query}], | ||
add_generation_prompt=True, | ||
tokenize=True, | ||
return_tensors="pt", | ||
return_dict=True) # chat mode | ||
inputs = inputs.to(f'xpu:{local_rank}') | ||
all_input = [{'image': image_path}, {'text': query}] | ||
|
||
# Generate predicted tokens | ||
with torch.inference_mode(): | ||
gen_kwargs = {"max_new_tokens": args.n_predict, "do_sample": False,} | ||
st = time.time() | ||
outputs = model.generate(**inputs, **gen_kwargs) | ||
outputs = outputs[:, inputs['input_ids'].shape[1]:] | ||
end = time.time() | ||
if local_rank == args.gpu_num - 1: | ||
print(f'Inference time: {end-st} s') | ||
output_str = tokenizer.decode(outputs[0]) | ||
print('-'*20, 'Input', '-'*20) | ||
print(f'Message: {all_input}') | ||
print('-'*20, 'Output', '-'*20) | ||
print(output_str) |
31 changes: 31 additions & 0 deletions
31
python/llm/example/GPU/Pipeline-Parallel-Inference/run_glm_4v_arc_2_card.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
source /opt/intel/oneapi/setvars.sh | ||
export MASTER_ADDR=127.0.0.1 | ||
export MASTER_PORT=9090 | ||
export FI_PROVIDER=tcp | ||
export USE_XETLA=OFF | ||
export OMP_NUM_THREADS=6 | ||
if [[ $KERNEL_VERSION != *"6.5"* ]]; then | ||
export SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS=1 | ||
fi | ||
export TORCH_LLM_ALLREDUCE=0 | ||
|
||
NUM_GPUS=2 # number of used GPU | ||
# To run glm-4v-9b | ||
CCL_ZE_IPC_EXCHANGE=sockets torchrun --standalone --nnodes=1 --nproc-per-node $NUM_GPUS \ | ||
glm_4v_generate.py --repo-id-or-model-path 'THUDM/glm-4v-9b' --gpu-num $NUM_GPUS --low-bit 'sym_int4' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
input_ids
is needed forself.get_masks()
. Maybe add an empty tensor here (similar to chatglm2/4)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have updated :)