Debian Codegraph @

examples/inference/pippy/bert.py

raw
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 AutoModelForMaskedLM
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 = AutoModelForMaskedLM.from_pretrained("bert-base-uncased")
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, 512), # bs x seq_len
39 device="cpu",
40 dtype=torch.int64,
41 requires_grad=False,
42 )
43
44
45 # Create a pipeline stage from the model
46 # Using `auto` is equivalent to letting `device_map="auto"` figure
47 # out device mapping and will also split the model according to the
48 # number of total GPUs available if it fits on one GPU
49 model = prepare_pippy(model, split_points="auto", example_args=(input,))
50
51 # You can pass `gather_output=True` to have the output from the model
52 # available on all GPUs
53 # model = prepare_pippy(model, split_points="auto", example_args=(input,), gather_output=True)
54
55 # Create new inputs of the expected size (n_processes)
56 input = torch.randint(
57 low=0,
58 high=model.config.vocab_size,
59 size=(2, 512), # bs x seq_len
60 device="cpu",
61 dtype=torch.int64,
62 requires_grad=False,
63 )
64
65 # Move the inputs to the first device
66 input = input.to(torch_device)
67
68 # Take an average of 5 times
69 # Measure first batch
70 synchronize_func()
71 start_time = time.time()
72 with torch.no_grad():
73 output = model(input)
74 synchronize_func()
75 end_time = time.time()
76 first_batch = end_time - start_time
77
78 # Now that hpu is init, measure after
79 synchronize_func()
80 start_time = time.time()
81 for i in range(5):
82 with torch.no_grad():
83 output = model(input)
84 synchronize_func()
85 end_time = time.time()
86
87 # The outputs are only on the final process by default
88 if PartialState().is_last_process:
89 output = torch.stack(tuple(output[0]))
90 print(f"Time of first pass: {first_batch}")
91 print(f"Average time per batch: {(end_time - start_time) / 5}")
92 PartialState().destroy_process_group()