| 1 |
# Copyright 2024 The HuggingFace Inc. team. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import torch |
| 16 |
from transformers import AutoModelForCausalLM, AutoTokenizer |
| 17 |
|
| 18 |
from accelerate import PartialState |
| 19 |
from accelerate.utils import gather_object |
| 20 |
|
| 21 |
|
| 22 |
# Start up the distributed environment without needing the Accelerator. |
| 23 |
distributed_state = PartialState() |
| 24 |
|
| 25 |
# You can change the model to any LLM such as mistralai/Mistral-7B-v0.1 or meta-llama/Llama-2-7b-chat-hf |
| 26 |
model_name = "microsoft/phi-2" |
| 27 |
model = AutoModelForCausalLM.from_pretrained( |
| 28 |
model_name, device_map=distributed_state.device, torch_dtype=torch.float16 |
| 29 |
) |
| 30 |
|
| 31 |
tokenizer = AutoTokenizer.from_pretrained(model_name) |
| 32 |
# Need to set the padding token to the eos token for generation |
| 33 |
tokenizer.pad_token = tokenizer.eos_token |
| 34 |
|
| 35 |
prompts = [ |
| 36 |
"I would like to", |
| 37 |
"hello how are you", |
| 38 |
"what is going on", |
| 39 |
"roses are red and", |
| 40 |
"welcome to the hotel", |
| 41 |
] |
| 42 |
|
| 43 |
# You can change the batch size depending on your GPU RAM |
| 44 |
batch_size = 2 |
| 45 |
# We set it to 8 since it is better for some hardware. More information here https://github.com/huggingface/tokenizers/issues/991 |
| 46 |
pad_to_multiple_of = 8 |
| 47 |
|
| 48 |
# Split into batches |
| 49 |
# We will get the following results: |
| 50 |
# [ ["I would like to", "hello how are you"], [ "what is going on", "roses are red and"], [ "welcome to the hotel"] ] |
| 51 |
formatted_prompts = [prompts[i : i + batch_size] for i in range(0, len(prompts), batch_size)] |
| 52 |
|
| 53 |
# Apply padding on the left since we are doing generation |
| 54 |
padding_side_default = tokenizer.padding_side |
| 55 |
tokenizer.padding_side = "left" |
| 56 |
# Tokenize each batch |
| 57 |
tokenized_prompts = [ |
| 58 |
tokenizer(formatted_prompt, padding=True, pad_to_multiple_of=pad_to_multiple_of, return_tensors="pt") |
| 59 |
for formatted_prompt in formatted_prompts |
| 60 |
] |
| 61 |
# Put back the original padding behavior |
| 62 |
tokenizer.padding_side = padding_side_default |
| 63 |
|
| 64 |
completions_per_process = [] |
| 65 |
# We automatically split the batched data we passed to it across all the processes. We also set apply_padding=True |
| 66 |
# so that the GPUs will have the same number of prompts, and you can then gather the results. |
| 67 |
# For example, if we have 2 gpus, the distribution will be: |
| 68 |
# GPU 0: ["I would like to", "hello how are you"], "what is going on", "roses are red and"] |
| 69 |
# GPU 1: ["welcome to the hotel"], ["welcome to the hotel"] -> this prompt is duplicated to ensure that all gpus have the same number of prompts |
| 70 |
with distributed_state.split_between_processes(tokenized_prompts, apply_padding=True) as batched_prompts: |
| 71 |
for batch in batched_prompts: |
| 72 |
# Move the batch to the device |
| 73 |
batch = batch.to(distributed_state.device) |
| 74 |
# We generate the text, decode it and add it to the list completions_per_process |
| 75 |
outputs = model.generate(**batch, max_new_tokens=20) |
| 76 |
generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True) |
| 77 |
completions_per_process.extend(generated_text) |
| 78 |
|
| 79 |
# We are gathering string, so we need to use gather_object. |
| 80 |
# If you need to gather tensors, you can use gather from accelerate.utils |
| 81 |
completions_gather = gather_object(completions_per_process) |
| 82 |
|
| 83 |
# Drop duplicates produced by apply_padding in split_between_processes |
| 84 |
completions = completions_gather[: len(prompts)] |
| 85 |
|
| 86 |
distributed_state.print(completions) |