-
Notifications
You must be signed in to change notification settings - Fork 989
/
test_model_generation.py
189 lines (141 loc) · 7.16 KB
/
test_model_generation.py
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
185
186
187
188
189
"""
instantiate models, save checkpoints, load checkpoints, compare loaded parameters to saved parameters and compare forward pass outputs
This tests contain a relatively large number of functions. They are not split into separate tests because a lot of boilerplate (e.g. instantiate model) needs
to run in order to perform follow up tests. Joining in one test reduces runtime at the expense of decreased transparency of test results in case of failures.
"""
import json
import os
from pathlib import Path
from ..common import TEST_CHECKPOINT_DIR, TEST_LOG_DIR, TEST_TENSORBOARD_DIR
from ..common import distributed_test, get_root_directory, get_test_configs_with_path, clear_test_dirs
import torch
@distributed_test(world_size=1)
def test_model_generation_unconditional_small_0():
yaml_list = get_test_configs_with_path(["test_local_setup.yml", "test_small_0.yml"])
run_generate_uncondional_test(yaml_list)
# @distributed_test(world_size=1)
# def test_model_generation_unconditional_small_1():
# yaml_list = get_test_configs_with_path(["test_local_setup.yml", "test_small_1.yml"])
# run_generate_uncondional_test(yaml_list)
# # for some reason this testcase is running way to long
# # potentially the optimizer problem?
# # @distributed_test(world_size=2)
# # def test_model_generation_unconditional_small_2():
# # yaml_list = get_test_configs_with_path(["test_local_setup.yml", "test_small_2.yml"])
# # run_generate_uncondional_test(yaml_list)
# @distributed_test(world_size=1)
# def test_model_generation_unconditional_small_3():
# yaml_list = get_test_configs_with_path(["test_local_setup.yml", "test_small_3.yml"])
# run_generate_uncondional_test(yaml_list)
# @distributed_test(world_size=2)
# def test_model_generation_unconditional_small_4():
# yaml_list = get_test_configs_with_path(["test_local_setup.yml", "test_small_4.yml"])
# run_generate_uncondional_test(yaml_list)
# @distributed_test(world_size=1)
# def test_model_generation_input_file_small_0():
# yaml_list = get_test_configs_with_path(["test_local_setup.yml", "test_small_0.yml"])
# run_generate_input_file_test(yaml_list)
# @distributed_test(world_size=1)
# def test_model_generation_input_file_small_1():
# yaml_list = get_test_configs_with_path(["test_local_setup.yml", "test_small_1.yml"])
# run_generate_input_file_test(yaml_list)
# # for some reason this testcase is running way to long
# # potentially the optimizer problem?
# # @distributed_test(world_size=2)
# # def test_model_generation_input_file_small_2():
# # yaml_list = get_test_configs_with_path(["test_local_setup.yml", "test_small_2.yml"])
# # run_generate_input_file_test(yaml_list)
# @distributed_test(world_size=1)
# def test_model_generation_input_file_small_3():
# yaml_list = get_test_configs_with_path(["test_local_setup.yml", "test_small_3.yml"])
# run_generate_input_file_test(yaml_list)
# @distributed_test(world_size=2)
# def test_model_generation_input_file_small_4():
# yaml_list = get_test_configs_with_path(["test_local_setup.yml", "test_small_4.yml"])
# run_generate_input_file_test(yaml_list)
def run_generate_uncondional_test(yaml_list):
from megatron.neox_arguments import NeoXArgs
from megatron import initialize_megatron
from megatron.training import setup_model_and_optimizer
from megatron.mpu import destroy_model_parallel
from megatron.text_generation_utils import generate_and_write_samples_unconditional
genfile = "test_generation_file.txt"
num_samples = 3
destroy_model_parallel() # mpu model parallel contains remaining global vars
if torch.distributed.get_world_size() == 1 or torch.distributed.get_rank() == 0:
clear_test_dirs()
# intitially load config from files as would be the case in deepy.py
args_loaded = NeoXArgs.from_ymls(yaml_list, overwrite_values={
"user_script": str(get_root_directory() / "pretrain_gpt2.py"),
"save": TEST_CHECKPOINT_DIR,
"load": TEST_CHECKPOINT_DIR,
"log_dir": TEST_LOG_DIR,
"tensorboard_dir": TEST_TENSORBOARD_DIR,
"checkpoint_activations": False,
"partition_activations": False,
"no_load_optim": True,
"text_gen_type": "unconditional",
"genfile": genfile,
"num_samples": num_samples,
})
args_loaded.build_tokenizer()
initialize_megatron(neox_args=args_loaded)
model, _, _ = setup_model_and_optimizer(neox_args=args_loaded, inference=True, get_key_value=True)
model.eval()
generate_and_write_samples_unconditional(neox_args=args_loaded, model=model)
assert Path(genfile).is_file(), "unconditional samples generated"
sample_count = 0
with open(genfile, "r") as f:
for sample_src in f:
if sample_src == "": continue
sample_count += 1
loaded = json.loads(sample_src)
assert sample_count == num_samples, "generated the right number of unconditional samples"
Path(genfile).unlink()
def run_generate_input_file_test(yaml_list):
from megatron.neox_arguments import NeoXArgs
from megatron import initialize_megatron
from megatron.training import setup_model_and_optimizer
from megatron.mpu import destroy_model_parallel
from megatron.text_generation_utils import generate_samples_input_from_file
sample_input_file = "test_generation_input.txt"
sample_output_file = "test_generation_output.txt"
num_samples = 3
with open(sample_input_file, "w") as f:
f.write("This is the first prompt")
destroy_model_parallel() # mpu model parallel contains remaining global vars
if torch.distributed.get_world_size() == 1 or torch.distributed.get_rank() == 0:
clear_test_dirs()
# intitially load config from files as would be the case in deepy.py
args_loaded = NeoXArgs.from_ymls(yaml_list, overwrite_values={
"user_script": str(get_root_directory() / "pretrain_gpt2.py"),
"save": TEST_CHECKPOINT_DIR,
"load": TEST_CHECKPOINT_DIR,
"log_dir": TEST_LOG_DIR,
"tensorboard_dir": TEST_TENSORBOARD_DIR,
"checkpoint_activations": False,
"partition_activations": False,
"no_load_optim": True,
"text_gen_type": "input-file",
"sample_input_file": sample_input_file,
"sample_output_file": sample_output_file,
"num_samples": num_samples,
})
args_loaded.build_tokenizer()
initialize_megatron(neox_args=args_loaded)
model, _, _ = setup_model_and_optimizer(neox_args=args_loaded, inference=True, get_key_value=True)
model.eval()
generate_samples_input_from_file(neox_args=args_loaded, model=model)
assert Path(genfile).is_file(), "unconditional samples generated"
sample_count = 0
with open(sample_output_file, "r") as f:
for sample_src in f:
if sample_src == "": continue
sample_count += 1
loaded = json.loads(sample_src)
assert sample_count == 2 * num_samples, "generated the right number of unconditional samples"
#Path(sample_input_file).unlink()
#Path(sample_output_file).unlink()
if __name__ == "__main__":
yaml_list = get_test_configs_with_path(["test_local_setup.yml", "test_small_0.yml"])
run_generate_input_file_test(yaml_list)