forked from huggingface/accelerate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_memory_utils.py
More file actions
184 lines (155 loc) · 5.27 KB
/
test_memory_utils.py
File metadata and controls
184 lines (155 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from torch import nn
from accelerate.test_utils import (
memory_allocated_func,
require_non_cpu,
require_non_torch_xla,
torch_device,
)
from accelerate.utils.memory import find_executable_batch_size, release_memory
def raise_fake_out_of_memory():
raise RuntimeError(f"{torch_device.upper()} out of memory.")
class ModelForTest(nn.Module):
def __init__(self):
super().__init__()
self.linear1 = nn.Linear(3, 4)
self.batchnorm = nn.BatchNorm1d(4)
self.linear2 = nn.Linear(4, 5)
def forward(self, x):
return self.linear2(self.batchnorm(self.linear1(x)))
class BigModelForTest(ModelForTest):
def __init__(self):
super().__init__()
self.linear3 = nn.Linear(5, 1000)
def forward(self, x):
return self.linear3(super().forward(x))
class MemoryTest(unittest.TestCase):
def test_memory_implicit(self):
batch_sizes = []
@find_executable_batch_size(starting_batch_size=128)
def mock_training_loop_function(batch_size):
nonlocal batch_sizes
batch_sizes.append(batch_size)
if batch_size != 8:
raise_fake_out_of_memory()
mock_training_loop_function()
assert batch_sizes == [
128,
115,
103,
92,
82,
73,
65,
58,
52,
46,
41,
36,
32,
28,
25,
22,
19,
17,
15,
13,
11,
9,
8,
]
def test_memory_explicit(self):
batch_sizes = []
@find_executable_batch_size(starting_batch_size=128)
def mock_training_loop_function(batch_size, arg1):
nonlocal batch_sizes
batch_sizes.append(batch_size)
if batch_size != 8:
raise_fake_out_of_memory()
return batch_size, arg1
bs, arg1 = mock_training_loop_function("hello")
assert batch_sizes == [
128,
115,
103,
92,
82,
73,
65,
58,
52,
46,
41,
36,
32,
28,
25,
22,
19,
17,
15,
13,
11,
9,
8,
]
assert [bs, arg1] == [8, "hello"]
def test_start_zero(self):
@find_executable_batch_size(starting_batch_size=0)
def mock_training_loop_function(batch_size):
pass
with self.assertRaises(RuntimeError) as cm:
mock_training_loop_function()
assert "No executable batch size found, reached zero." in cm.exception.args[0]
def test_approach_zero(self):
@find_executable_batch_size(starting_batch_size=16)
def mock_training_loop_function(batch_size):
if batch_size > 0:
raise_fake_out_of_memory()
pass
with self.assertRaises(RuntimeError) as cm:
mock_training_loop_function()
assert "No executable batch size found, reached zero." in cm.exception.args[0]
def test_verbose_guard(self):
@find_executable_batch_size(starting_batch_size=128)
def mock_training_loop_function(batch_size, arg1, arg2):
if batch_size != 8:
raise raise_fake_out_of_memory()
with self.assertRaises(TypeError) as cm:
mock_training_loop_function(128, "hello", "world")
assert "Batch size was passed into `f`" in cm.exception.args[0]
assert "`f(arg1='hello', arg2='world')" in cm.exception.args[0]
def test_any_other_error(self):
@find_executable_batch_size(starting_batch_size=16)
def mock_training_loop_function(batch_size):
raise ValueError("Oops, we had an error!")
with self.assertRaises(ValueError) as cm:
mock_training_loop_function()
assert "Oops, we had an error!" in cm.exception.args[0]
@require_non_cpu
@require_non_torch_xla
def test_release_memory(self):
starting_memory = memory_allocated_func()
if torch_device.startswith("hpu"):
# hpu has a minimum memory allocation that cannot be released,
# we need to surpass it by using a bigger model (>5767296 bytes)
model = BigModelForTest()
else:
model = ModelForTest()
model.to(torch_device)
assert memory_allocated_func() > starting_memory
model = release_memory(model)
assert memory_allocated_func() == starting_memory