run_func_test.py 9.01 KB
Newer Older
Elton Zheng's avatar
Elton Zheng committed
1
2
3
4
5
6
7
8
9
10
11
12
# coding=utf-8
# Copyright (c) 2019, The Microsoft DeepSpeed Team. All rights reserved.
#
# Note: please copy webtext data to "Megatron-LM" folder, before running this script.

import unittest
import subprocess
import os
import time
import re
from .test_common import BaseTestCase

13
14
15
16
17
LAYERS = 2
HIDDEN_SIZE = 128
ATTN_HEADS = 8
SEQ_LEN = 64

Elton Zheng's avatar
Elton Zheng committed
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

def grep_loss_from_file(file_name):
    loss = 0.0

    with open(file_name, 'r') as f:
        lines = f.readlines()
        line_filter = "validation loss at the end of training for test data | LM loss:"
        match_number = re.compile('LM loss: ([-+]?[0-9]+\.?[0-9]*(?:[Ee][-+]?[0-9]+)?)')

        for line in lines:
            if line_filter in line:
                loss = re.findall(match_number, line)
                loss = float(loss[0])

    if loss == 0.0:
        print("no loss found in file ", file_name)

    return loss


class GPT2FuncTestCase(BaseTestCase):
    def __init__(self, methodName="DeepSpeed function test on GPT2 model"):
        super(GPT2FuncTestCase, self).__init__(methodName)

    def setUp(self):
        self.save_dir = os.getcwd()
        new_dir = os.path.dirname(__file__)
        if new_dir:
            os.chdir(new_dir)

    def tearDown(self):
        os.chdir(self.save_dir)

Jeff Rasley's avatar
Jeff Rasley committed
51
    def test_mp1_gpu1_node1_zero1(self):
Elton Zheng's avatar
Elton Zheng committed
52
53
54
55
56
57
        test_config = {
            "mp": 1,
            "gpus": 1,
            "nodes": 1,
            "bs": 4,
            "steps": 1000,
58
59
60
61
            "layers": LAYERS,
            "hidden_size": HIDDEN_SIZE,
            "seq_length": SEQ_LEN,
            "heads": ATTN_HEADS,
Elton Zheng's avatar
Elton Zheng committed
62
            "deepspeed": False,
Jeff Rasley's avatar
Jeff Rasley committed
63
            "json": "ds_config_func_bs4_zero1.json",
Elton Zheng's avatar
Elton Zheng committed
64
65
66
67
68
        }

        succ = self.run_test(test_config, 0.01)
        self.assertTrue(succ)

Jeff Rasley's avatar
Jeff Rasley committed
69
    def test_mp1_gpu2_node1_zero1(self):
Elton Zheng's avatar
Elton Zheng committed
70
71
72
73
74
75
        test_config = {
            "mp": 1,
            "gpus": 2,
            "nodes": 1,
            "bs": 8,
            "steps": 1000,
76
77
78
79
            "layers": LAYERS,
            "hidden_size": HIDDEN_SIZE,
            "seq_length": SEQ_LEN,
            "heads": ATTN_HEADS,
Elton Zheng's avatar
Elton Zheng committed
80
            "deepspeed": False,
Jeff Rasley's avatar
Jeff Rasley committed
81
            "json": "ds_config_func_bs8_zero1.json",
Elton Zheng's avatar
Elton Zheng committed
82
83
84
85
86
        }

        succ = self.run_test(test_config, 0.01)
        self.assertTrue(succ)

Jeff Rasley's avatar
Jeff Rasley committed
87
    def test_mp2_gpu4_node1_zero1(self):
Elton Zheng's avatar
Elton Zheng committed
88
89
90
91
92
93
        test_config = {
            "mp": 2,
            "gpus": 4,
            "nodes": 1,
            "bs": 8,
            "steps": 1000,
94
95
96
97
            "layers": LAYERS,
            "hidden_size": HIDDEN_SIZE,
            "seq_length": SEQ_LEN,
            "heads": ATTN_HEADS,
Elton Zheng's avatar
Elton Zheng committed
98
            "deepspeed": False,
Jeff Rasley's avatar
Jeff Rasley committed
99
100
101
102
103
104
105
106
107
108
109
110
111
            "json": "ds_config_func_bs8_zero1.json",
        }

        succ = self.run_test(test_config, 0.01)
        self.assertTrue(succ)

    def test_mp4_gpu4_node1_zero1(self):
        test_config = {
            "mp": 4,
            "gpus": 4,
            "nodes": 1,
            "bs": 8,
            "steps": 1000,
112
113
114
115
            "layers": LAYERS,
            "hidden_size": HIDDEN_SIZE,
            "seq_length": SEQ_LEN,
            "heads": ATTN_HEADS,
Jeff Rasley's avatar
Jeff Rasley committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
            "deepspeed": False,
            "json": "ds_config_func_bs8_zero1.json",
        }

        succ = self.run_test(test_config, 0.01)
        self.assertTrue(succ)

    def test_mp1_gpu1_node1_zero2(self):
        test_config = {
            "mp": 1,
            "gpus": 1,
            "nodes": 1,
            "bs": 4,
            "steps": 1000,
130
131
132
133
            "layers": LAYERS,
            "hidden_size": HIDDEN_SIZE,
            "seq_length": SEQ_LEN,
            "heads": ATTN_HEADS,
Jeff Rasley's avatar
Jeff Rasley committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
            "deepspeed": False,
            "json": "ds_config_func_bs4_zero2.json",
        }

        succ = self.run_test(test_config, 0.01)
        self.assertTrue(succ)

    def test_mp1_gpu2_node1_zero2(self):
        test_config = {
            "mp": 1,
            "gpus": 2,
            "nodes": 1,
            "bs": 8,
            "steps": 1000,
148
149
150
151
            "layers": LAYERS,
            "hidden_size": HIDDEN_SIZE,
            "seq_length": SEQ_LEN,
            "heads": ATTN_HEADS,
Jeff Rasley's avatar
Jeff Rasley committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
            "deepspeed": False,
            "json": "ds_config_func_bs8_zero2.json",
        }

        succ = self.run_test(test_config, 0.01)
        self.assertTrue(succ)

    def test_mp2_gpu4_node1_zero2(self):
        test_config = {
            "mp": 2,
            "gpus": 4,
            "nodes": 1,
            "bs": 8,
            "steps": 1000,
166
167
168
169
            "layers": LAYERS,
            "hidden_size": HIDDEN_SIZE,
            "seq_length": SEQ_LEN,
            "heads": ATTN_HEADS,
Jeff Rasley's avatar
Jeff Rasley committed
170
171
            "deepspeed": False,
            "json": "ds_config_func_bs8_zero2.json",
Elton Zheng's avatar
Elton Zheng committed
172
173
174
175
176
177
178
179
        }

        succ = self.run_test(test_config, 0.01)
        self.assertTrue(succ)

        succ = self.run_partition_activations_test(test_config, 0.01)
        self.assertTrue(succ)

Jeff Rasley's avatar
Jeff Rasley committed
180
    def test_mp4_gpu4_node1_zero2(self):
Elton Zheng's avatar
Elton Zheng committed
181
182
183
184
185
186
        test_config = {
            "mp": 4,
            "gpus": 4,
            "nodes": 1,
            "bs": 8,
            "steps": 1000,
187
188
189
190
            "layers": LAYERS,
            "hidden_size": HIDDEN_SIZE,
            "seq_length": SEQ_LEN,
            "heads": ATTN_HEADS,
Elton Zheng's avatar
Elton Zheng committed
191
            "deepspeed": False,
Jeff Rasley's avatar
Jeff Rasley committed
192
            "json": "ds_config_func_bs8_zero2.json",
Elton Zheng's avatar
Elton Zheng committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
        }

        succ = self.run_test(test_config, 0.01)
        self.assertTrue(succ)

        succ = self.run_partition_activations_test(test_config, 0.01)
        self.assertTrue(succ)

    def test_optimizer_scheduler(self):
        test_config = {
            "mp": 1,
            "gpus": 1,
            "nodes": 1,
            "bs": 4,
            "steps": 20,
208
209
210
211
            "layers": LAYERS,
            "hidden_size": HIDDEN_SIZE,
            "seq_length": SEQ_LEN,
            "heads": ATTN_HEADS,
Elton Zheng's avatar
Elton Zheng committed
212
213
214
215
216
217
218
219
220
221
222
223
            "deepspeed": False,
            "json": "ds_config_func_scheduler.json",
        }

        succ = self.run_test(test_config, 0.01)
        # assure no crash.
        self.assertTrue(True)

    def run_partition_activations_test(self, test_config, r_tol):
        print("\n")
        print("{0}: starting......".format(self.id()))

Jeff Rasley's avatar
Jeff Rasley committed
224
        baseline_prefix = "gpt2_func_"
Elton Zheng's avatar
Elton Zheng committed
225
226
227
228
        prefix = "gpt2_partition_activation_"

        # baseline run...
        test_config["deepspeed"] = False
Jeff Rasley's avatar
Jeff Rasley committed
229
        base_file = self.gen_output_name(test_config, baseline_prefix)
Elton Zheng's avatar
Elton Zheng committed
230
231
232
233
234
235
236
237
238
239

        # skip baseline run if it exists.
        if not self.has_loss_data(base_file):
            print("{0}: baseline run.".format(self.id()))
            self.run_gpt2_test(test_config, base_file)
        else:
            print("{0}: baseline exists.".format(self.id()))

        # DeepSpeed run...
        test_config["deepspeed"] = True
Jeff Rasley's avatar
Jeff Rasley committed
240
        test_config["other_args"] = "--deepspeed-activation-checkpointing"
Elton Zheng's avatar
Elton Zheng committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
        print("{0}: DeepSpeed run.".format(self.id()))
        test_file = self.gen_output_name(test_config, prefix)
        self.run_gpt2_test(test_config, test_file)

        return self.check_parity(base_file, test_file, r_tol)

    def run_test(self, test_config, r_tol):
        print("\n")
        print("{0}: starting......".format(self.id()))

        prefix = "gpt2_func"

        # baseline run...
        test_config["deepspeed"] = False
        base_file = self.gen_output_name(test_config, prefix)

        # skip baseline run if it exists.
        if not self.has_loss_data(base_file):
            print("{0}: baseline run.".format(self.id()))
            self.run_gpt2_test(test_config, base_file)
        else:
            print("{0}: baseline exists.".format(self.id()))

        # DeepSpeed run...
        test_config["deepspeed"] = True
        print("{0}: DeepSpeed run.".format(self.id()))
        test_file = self.gen_output_name(test_config, prefix)
        self.run_gpt2_test(test_config, test_file)

        return self.check_parity(base_file, test_file, r_tol)

    def has_loss_data(self, file_name):
        has_loss = False
        if os.path.exists(file_name):
            loss = grep_loss_from_file(file_name)
            if loss != 0.0:
                has_loss = True

        return has_loss

    def check_parity(self, base_file, test_file, r_tol):
        base_loss = grep_loss_from_file(base_file)
        test_loss = grep_loss_from_file(test_file)

        print("baseline loss: {0}, test loss: {1}".format(base_loss, test_loss))

        if base_loss == 0.0 or test_loss == 0.0:
            return False

        if abs((base_loss - test_loss) / base_loss) > r_tol:
            return False

        return True


def suite():
    suite = unittest.TestSuite()
Jeff Rasley's avatar
Jeff Rasley committed
298
299
300
301
302
303
304
305
306
307
    suite.addTest(GPT2FuncTestCase('test_mp1_gpu1_node1_zero1'))
    suite.addTest(GPT2FuncTestCase('test_mp1_gpu2_node1_zero1'))
    suite.addTest(GPT2FuncTestCase('test_mp2_gpu4_node1_zero1'))
    suite.addTest(GPT2FuncTestCase('test_mp4_gpu4_node1_zero1'))

    suite.addTest(GPT2FuncTestCase('test_mp1_gpu1_node1_zero2'))
    suite.addTest(GPT2FuncTestCase('test_mp1_gpu2_node1_zero2'))
    suite.addTest(GPT2FuncTestCase('test_mp2_gpu4_node1_zero2'))
    suite.addTest(GPT2FuncTestCase('test_mp4_gpu4_node1_zero2'))

Elton Zheng's avatar
Elton Zheng committed
308
309
310
311
312
313
314
    suite.addTest(GPT2FuncTestCase('test_optimizer_scheduler'))
    return suite


if __name__ == '__main__':
    runner = unittest.TextTestRunner(failfast=True)
    runner.run(suite())