| 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 |
import unittest |
| 15 |
from unittest import skip |
| 16 |
|
| 17 |
import torch |
| 18 |
from torch.utils.benchmark import Timer |
| 19 |
|
| 20 |
from accelerate.test_utils import require_huggingface_suite, require_non_cpu, require_non_hpu, slow, torch_device |
| 21 |
from accelerate.utils import compile_regions, extract_model_from_parallel, release_memory |
| 22 |
|
| 23 |
|
| 24 |
MODEL_ID = "gpt2" |
| 25 |
|
| 26 |
COMPILE_ITERS = 2 |
| 27 |
INFERENCE_ITERS = 100 |
| 28 |
|
| 29 |
INFRENCE_STMT = "model(input_ids, use_cache=False)" |
| 30 |
COMPILE_STMT = f"torch._dynamo.reset(); torch._inductor.utils.clear_inductor_caches(); {INFRENCE_STMT}" |
| 31 |
|
| 32 |
if torch_device == "hpu": |
| 33 |
backend = "hpu_backend" |
| 34 |
else: |
| 35 |
backend = "inductor" |
| 36 |
|
| 37 |
|
| 38 |
@require_huggingface_suite |
| 39 |
@skip("Don't work with torch 2.8") |
| 40 |
class RegionalCompilationTester(unittest.TestCase): |
| 41 |
def _get_model_and_inputs(self): |
| 42 |
from transformers import AutoConfig, AutoModelForCausalLM |
| 43 |
|
| 44 |
with torch.device(torch_device): |
| 45 |
config = AutoConfig.from_pretrained(MODEL_ID) |
| 46 |
model = AutoModelForCausalLM.from_config(config) |
| 47 |
input_ids = torch.randint(0, 1000, (4, 128), dtype=torch.int64) |
| 48 |
|
| 49 |
return model, input_ids |
| 50 |
|
| 51 |
def test_regions_are_compiled(self): |
| 52 |
model, _ = self._get_model_and_inputs() |
| 53 |
compiled_model = compile_regions(model, mode="reduce-overhead", backend=backend) |
| 54 |
|
| 55 |
# Check that the compiled model keeps a reference to the original model |
| 56 |
assert hasattr(compiled_model, "_orig_mod") |
| 57 |
assert compiled_model._orig_mod is model |
| 58 |
|
| 59 |
# Check that the compiled_model.transformer.h[i] and compiled_model.lm_head are compiled separately |
| 60 |
assert isinstance(compiled_model.transformer.h[0], torch._dynamo.eval_frame.OptimizedModule) |
| 61 |
assert isinstance(compiled_model.lm_head, torch._dynamo.eval_frame.OptimizedModule) |
| 62 |
assert compiled_model.transformer.h[0]._orig_mod is model.transformer.h[0] |
| 63 |
assert compiled_model.lm_head._orig_mod is model.lm_head |
| 64 |
|
| 65 |
def test_extract_model_keep_torch_compile(self): |
| 66 |
model, _ = self._get_model_and_inputs() |
| 67 |
compiled_model = compile_regions(model, mode="reduce-overhead", backend=backend) |
| 68 |
|
| 69 |
distributed_model = torch.nn.parallel.DataParallel(model) |
| 70 |
distributed_compiled_model = compile_regions(distributed_model, mode="reduce-overhead", backend=backend) |
| 71 |
compiled_model_unwrapped = extract_model_from_parallel(distributed_compiled_model, keep_torch_compile=True) |
| 72 |
|
| 73 |
assert compiled_model._orig_mod is compiled_model_unwrapped._orig_mod |
| 74 |
|
| 75 |
def test_extract_model_remove_torch_compile(self): |
| 76 |
model, _ = self._get_model_and_inputs() |
| 77 |
compiled_model = compile_regions(model, mode="reduce-overhead", backend=backend) |
| 78 |
|
| 79 |
distributed_model = torch.nn.parallel.DataParallel(model) |
| 80 |
distributed_compiled_model = compile_regions(distributed_model, mode="reduce-overhead", backend=backend) |
| 81 |
compiled_model_unwrapped = extract_model_from_parallel(distributed_compiled_model, keep_torch_compile=False) |
| 82 |
|
| 83 |
assert compiled_model._orig_mod is compiled_model_unwrapped |
| 84 |
|
| 85 |
@require_non_cpu |
| 86 |
@require_huggingface_suite |
| 87 |
def test_regional_compilation_cold_start(self): |
| 88 |
model, input_ids = self._get_model_and_inputs() |
| 89 |
|
| 90 |
regional_compilation_model = compile_regions(model, backend=backend) |
| 91 |
regional_compilation_cold_start = ( |
| 92 |
Timer(stmt=COMPILE_STMT, globals={"model": regional_compilation_model, "input_ids": input_ids}) |
| 93 |
.timeit(COMPILE_ITERS) |
| 94 |
.median |
| 95 |
) |
| 96 |
|
| 97 |
full_compilation_model = torch.compile(model, backend=backend) |
| 98 |
full_compilation_cold_start = ( |
| 99 |
Timer(stmt=COMPILE_STMT, globals={"model": full_compilation_model, "input_ids": input_ids}) |
| 100 |
.timeit(COMPILE_ITERS) |
| 101 |
.median |
| 102 |
) |
| 103 |
|
| 104 |
self.assertLess( |
| 105 |
regional_compilation_cold_start, |
| 106 |
full_compilation_cold_start, |
| 107 |
"Regional compilation should have a faster cold start than full compilation", |
| 108 |
) |
| 109 |
|
| 110 |
release_memory(model, full_compilation_model, regional_compilation_model) |
| 111 |
|
| 112 |
@slow |
| 113 |
@require_non_hpu |
| 114 |
@require_non_cpu |
| 115 |
@require_huggingface_suite |
| 116 |
def test_regional_compilation_inference_speedup(self): |
| 117 |
model, input_ids = self._get_model_and_inputs() |
| 118 |
|
| 119 |
baseline_inference_latency = ( |
| 120 |
Timer(stmt=INFRENCE_STMT, globals={"model": model, "input_ids": input_ids}).timeit(INFERENCE_ITERS).median |
| 121 |
) |
| 122 |
|
| 123 |
regional_compilation_model = compile_regions(model, backend=backend) |
| 124 |
regional_compilation_inference_latency = ( |
| 125 |
Timer(stmt=INFRENCE_STMT, globals={"model": regional_compilation_model, "input_ids": input_ids}) |
| 126 |
.timeit(INFERENCE_ITERS) |
| 127 |
.median |
| 128 |
) |
| 129 |
|
| 130 |
full_compilation_model = torch.compile(model, backend=backend) |
| 131 |
full_compilation_inference_latency = ( |
| 132 |
Timer(stmt=INFRENCE_STMT, globals={"model": full_compilation_model, "input_ids": input_ids}) |
| 133 |
.timeit(INFERENCE_ITERS) |
| 134 |
.median |
| 135 |
) |
| 136 |
|
| 137 |
full_compilation_inference_speedup = baseline_inference_latency / full_compilation_inference_latency |
| 138 |
regional_compilation_inference_speedup = baseline_inference_latency / regional_compilation_inference_latency |
| 139 |
|
| 140 |
self.assertAlmostEqual( |
| 141 |
regional_compilation_inference_speedup, |
| 142 |
full_compilation_inference_speedup, |
| 143 |
delta=0.1, |
| 144 |
msg="Regional compilation should have a similar speedup to full compilation", |
| 145 |
) |
| 146 |
|
| 147 |
release_memory(model, full_compilation_model, regional_compilation_model) |