| 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 |
import time |
| 15 |
|
| 16 |
import torch |
| 17 |
from transformers import AutoModelForSequenceClassification |
| 18 |
|
| 19 |
from accelerate import PartialState, prepare_pippy |
| 20 |
from accelerate.test_utils import torch_device |
| 21 |
from accelerate.utils import set_seed |
| 22 |
|
| 23 |
|
| 24 |
synchronize_func = getattr(torch, torch_device, torch.cuda).synchronize |
| 25 |
|
| 26 |
# Set the random seed to have reproducable outputs |
| 27 |
set_seed(42) |
| 28 |
|
| 29 |
# Create an example model |
| 30 |
model = AutoModelForSequenceClassification.from_pretrained("gpt2") |
| 31 |
model.eval() |
| 32 |
|
| 33 |
# Input configs |
| 34 |
# Create example inputs for the model |
| 35 |
input = torch.randint( |
| 36 |
low=0, |
| 37 |
high=model.config.vocab_size, |
| 38 |
size=(1, 1024), # bs x seq_len |
| 39 |
device="cpu", |
| 40 |
dtype=torch.int64, |
| 41 |
requires_grad=False, |
| 42 |
) |
| 43 |
|
| 44 |
# Create a pipeline stage from the model |
| 45 |
# Using `auto` is equivalent to letting `device_map="auto"` figure |
| 46 |
# out device mapping and will also split the model according to the |
| 47 |
# number of total GPUs available if it fits on one GPU |
| 48 |
model = prepare_pippy(model, split_points="auto", example_args=(input,)) |
| 49 |
|
| 50 |
# You can pass `gather_output=True` to have the output from the model |
| 51 |
# available on all GPUs |
| 52 |
# model = prepare_pippy(model, split_points="auto", example_args=(input,), gather_output=True) |
| 53 |
|
| 54 |
# Create new inputs of the expected size (n_processes) |
| 55 |
input = torch.randint( |
| 56 |
low=0, |
| 57 |
high=model.config.vocab_size, |
| 58 |
size=(2, 1024), # bs x seq_len |
| 59 |
device="cpu", |
| 60 |
dtype=torch.int64, |
| 61 |
requires_grad=False, |
| 62 |
) |
| 63 |
|
| 64 |
# Move the inputs to the first device |
| 65 |
input = input.to(torch_device) |
| 66 |
|
| 67 |
# Take an average of 5 times |
| 68 |
# Measure first batch |
| 69 |
synchronize_func() |
| 70 |
start_time = time.time() |
| 71 |
with torch.no_grad(): |
| 72 |
output = model(input) |
| 73 |
synchronize_func() |
| 74 |
end_time = time.time() |
| 75 |
first_batch = end_time - start_time |
| 76 |
|
| 77 |
# Now that device/backend is init, measure after |
| 78 |
synchronize_func() |
| 79 |
start_time = time.time() |
| 80 |
for i in range(5): |
| 81 |
with torch.no_grad(): |
| 82 |
output = model(input) |
| 83 |
synchronize_func() |
| 84 |
end_time = time.time() |
| 85 |
|
| 86 |
# The outputs are only on the final process by default |
| 87 |
if PartialState().is_last_process: |
| 88 |
output = torch.stack(tuple(output[0])) |
| 89 |
print(f"Time of first pass: {first_batch}") |
| 90 |
print(f"Average time per batch: {(end_time - start_time) / 5}") |
| 91 |
PartialState().destroy_process_group() |