| 1 |
# Copyright 2025 The HuggingFace 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 torch.utils.benchmark import Compare, Timer |
| 17 |
from transformers import AutoConfig, AutoModelForCausalLM |
| 18 |
|
| 19 |
from accelerate.test_utils.testing import get_backend |
| 20 |
from accelerate.utils import compile_regions |
| 21 |
|
| 22 |
|
| 23 |
torch.set_float32_matmul_precision("high") |
| 24 |
|
| 25 |
COMPILE_ITERS = 2 |
| 26 |
INFERENCE_ITERS = 100 |
| 27 |
|
| 28 |
BASELINE = "Baseline" |
| 29 |
COMPILE_TIME = "Compile time" |
| 30 |
INFRENCE_TIME = "Inference time" |
| 31 |
FULL_COMPILATION = "Full compilation" |
| 32 |
REGIONAL_COMPILATION = "Regional compilation" |
| 33 |
|
| 34 |
INFRENCE_STMT = "model(input_ids, use_cache=False)" |
| 35 |
COMPILE_STMT = f"torch._dynamo.reset(); torch._inductor.utils.clear_inductor_caches(); {INFRENCE_STMT}" |
| 36 |
|
| 37 |
torch_device_type, _, _ = get_backend() |
| 38 |
|
| 39 |
results = [] |
| 40 |
for model_id in [ |
| 41 |
# non-gated llama models |
| 42 |
"NousResearch/Llama-3.2-1B", |
| 43 |
"NousResearch/Hermes-3-Llama-3.2-3B", |
| 44 |
"NousResearch/Hermes-3-Llama-3.1-8B", |
| 45 |
"NousResearch/Nous-Hermes-Llama2-13b", |
| 46 |
]: |
| 47 |
with torch.device(torch_device_type): |
| 48 |
config = AutoConfig.from_pretrained(model_id) |
| 49 |
model = AutoModelForCausalLM.from_config(config).to(dtype=torch.float16).eval() |
| 50 |
|
| 51 |
full_compilation_model = torch.compile(model) |
| 52 |
regional_compilation_model = compile_regions(model) |
| 53 |
|
| 54 |
for model, sub_label, description, stmt, iters in [ |
| 55 |
(model, BASELINE, INFRENCE_TIME, INFRENCE_STMT, INFERENCE_ITERS), |
| 56 |
(full_compilation_model, FULL_COMPILATION, COMPILE_TIME, COMPILE_STMT, COMPILE_ITERS), |
| 57 |
(full_compilation_model, FULL_COMPILATION, INFRENCE_TIME, INFRENCE_STMT, INFERENCE_ITERS), |
| 58 |
(regional_compilation_model, REGIONAL_COMPILATION, COMPILE_TIME, COMPILE_STMT, COMPILE_ITERS), |
| 59 |
(regional_compilation_model, REGIONAL_COMPILATION, INFRENCE_TIME, INFRENCE_STMT, INFERENCE_ITERS), |
| 60 |
]: |
| 61 |
for batch_size, sequence_length in [(1, 128), (4, 128)]: |
| 62 |
input_ids = torch.randint( |
| 63 |
0, 1000, size=(batch_size, sequence_length), dtype=torch.int64, device=torch_device_type |
| 64 |
) |
| 65 |
results.append( |
| 66 |
Timer( |
| 67 |
label=model_id, |
| 68 |
sub_label=sub_label, |
| 69 |
description=f"{description} ({batch_size}x{sequence_length})", |
| 70 |
globals={"model": model, "input_ids": input_ids}, |
| 71 |
stmt=stmt, |
| 72 |
).timeit(number=iters) |
| 73 |
) |
| 74 |
|
| 75 |
compare = Compare(results) |
| 76 |
compare.colorize() |
| 77 |
compare.print() |