| 1 |
# Copyright 2022 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 unittest |
| 16 |
|
| 17 |
from torch import nn |
| 18 |
|
| 19 |
from accelerate.test_utils import ( |
| 20 |
memory_allocated_func, |
| 21 |
require_non_cpu, |
| 22 |
require_non_torch_xla, |
| 23 |
torch_device, |
| 24 |
) |
| 25 |
from accelerate.utils.memory import find_executable_batch_size, release_memory |
| 26 |
|
| 27 |
|
| 28 |
def raise_fake_out_of_memory(): |
| 29 |
raise RuntimeError(f"{torch_device.upper()} out of memory.") |
| 30 |
|
| 31 |
|
| 32 |
class ModelForTest(nn.Module): |
| 33 |
def __init__(self): |
| 34 |
super().__init__() |
| 35 |
self.linear1 = nn.Linear(3, 4) |
| 36 |
self.batchnorm = nn.BatchNorm1d(4) |
| 37 |
self.linear2 = nn.Linear(4, 5) |
| 38 |
|
| 39 |
def forward(self, x): |
| 40 |
return self.linear2(self.batchnorm(self.linear1(x))) |
| 41 |
|
| 42 |
|
| 43 |
class BigModelForTest(ModelForTest): |
| 44 |
def __init__(self): |
| 45 |
super().__init__() |
| 46 |
self.linear3 = nn.Linear(5, 1000) |
| 47 |
|
| 48 |
def forward(self, x): |
| 49 |
return self.linear3(super().forward(x)) |
| 50 |
|
| 51 |
|
| 52 |
class MemoryTest(unittest.TestCase): |
| 53 |
def test_memory_implicit(self): |
| 54 |
batch_sizes = [] |
| 55 |
|
| 56 |
@find_executable_batch_size(starting_batch_size=128) |
| 57 |
def mock_training_loop_function(batch_size): |
| 58 |
nonlocal batch_sizes |
| 59 |
batch_sizes.append(batch_size) |
| 60 |
if batch_size != 8: |
| 61 |
raise_fake_out_of_memory() |
| 62 |
|
| 63 |
mock_training_loop_function() |
| 64 |
assert batch_sizes == [ |
| 65 |
128, |
| 66 |
115, |
| 67 |
103, |
| 68 |
92, |
| 69 |
82, |
| 70 |
73, |
| 71 |
65, |
| 72 |
58, |
| 73 |
52, |
| 74 |
46, |
| 75 |
41, |
| 76 |
36, |
| 77 |
32, |
| 78 |
28, |
| 79 |
25, |
| 80 |
22, |
| 81 |
19, |
| 82 |
17, |
| 83 |
15, |
| 84 |
13, |
| 85 |
11, |
| 86 |
9, |
| 87 |
8, |
| 88 |
] |
| 89 |
|
| 90 |
def test_memory_explicit(self): |
| 91 |
batch_sizes = [] |
| 92 |
|
| 93 |
@find_executable_batch_size(starting_batch_size=128) |
| 94 |
def mock_training_loop_function(batch_size, arg1): |
| 95 |
nonlocal batch_sizes |
| 96 |
batch_sizes.append(batch_size) |
| 97 |
if batch_size != 8: |
| 98 |
raise_fake_out_of_memory() |
| 99 |
return batch_size, arg1 |
| 100 |
|
| 101 |
bs, arg1 = mock_training_loop_function("hello") |
| 102 |
assert batch_sizes == [ |
| 103 |
128, |
| 104 |
115, |
| 105 |
103, |
| 106 |
92, |
| 107 |
82, |
| 108 |
73, |
| 109 |
65, |
| 110 |
58, |
| 111 |
52, |
| 112 |
46, |
| 113 |
41, |
| 114 |
36, |
| 115 |
32, |
| 116 |
28, |
| 117 |
25, |
| 118 |
22, |
| 119 |
19, |
| 120 |
17, |
| 121 |
15, |
| 122 |
13, |
| 123 |
11, |
| 124 |
9, |
| 125 |
8, |
| 126 |
] |
| 127 |
assert [bs, arg1] == [8, "hello"] |
| 128 |
|
| 129 |
def test_start_zero(self): |
| 130 |
@find_executable_batch_size(starting_batch_size=0) |
| 131 |
def mock_training_loop_function(batch_size): |
| 132 |
pass |
| 133 |
|
| 134 |
with self.assertRaises(RuntimeError) as cm: |
| 135 |
mock_training_loop_function() |
| 136 |
assert "No executable batch size found, reached zero." in cm.exception.args[0] |
| 137 |
|
| 138 |
def test_approach_zero(self): |
| 139 |
@find_executable_batch_size(starting_batch_size=16) |
| 140 |
def mock_training_loop_function(batch_size): |
| 141 |
if batch_size > 0: |
| 142 |
raise_fake_out_of_memory() |
| 143 |
pass |
| 144 |
|
| 145 |
with self.assertRaises(RuntimeError) as cm: |
| 146 |
mock_training_loop_function() |
| 147 |
assert "No executable batch size found, reached zero." in cm.exception.args[0] |
| 148 |
|
| 149 |
def test_verbose_guard(self): |
| 150 |
@find_executable_batch_size(starting_batch_size=128) |
| 151 |
def mock_training_loop_function(batch_size, arg1, arg2): |
| 152 |
if batch_size != 8: |
| 153 |
raise raise_fake_out_of_memory() |
| 154 |
|
| 155 |
with self.assertRaises(TypeError) as cm: |
| 156 |
mock_training_loop_function(128, "hello", "world") |
| 157 |
assert "Batch size was passed into `f`" in cm.exception.args[0] |
| 158 |
assert "`f(arg1='hello', arg2='world')" in cm.exception.args[0] |
| 159 |
|
| 160 |
def test_any_other_error(self): |
| 161 |
@find_executable_batch_size(starting_batch_size=16) |
| 162 |
def mock_training_loop_function(batch_size): |
| 163 |
raise ValueError("Oops, we had an error!") |
| 164 |
|
| 165 |
with self.assertRaises(ValueError) as cm: |
| 166 |
mock_training_loop_function() |
| 167 |
assert "Oops, we had an error!" in cm.exception.args[0] |
| 168 |
|
| 169 |
@require_non_cpu |
| 170 |
@require_non_torch_xla |
| 171 |
def test_release_memory(self): |
| 172 |
starting_memory = memory_allocated_func() |
| 173 |
|
| 174 |
if torch_device.startswith("hpu"): |
| 175 |
# hpu has a minimum memory allocation that cannot be released, |
| 176 |
# we need to surpass it by using a bigger model (>5767296 bytes) |
| 177 |
model = BigModelForTest() |
| 178 |
else: |
| 179 |
model = ModelForTest() |
| 180 |
|
| 181 |
model.to(torch_device) |
| 182 |
assert memory_allocated_func() > starting_memory |
| 183 |
model = release_memory(model) |
| 184 |
assert memory_allocated_func() == starting_memory |