forked from pytorch/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regenerate.py
executable file
·342 lines (256 loc) · 9.42 KB
/
regenerate.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python3
"""
This script should use a very simple, functional programming style.
Avoid Jinja macros in favor of native Python functions.
Don't go overboard on code generation; use Python only to generate
content that can't be easily declared statically using CircleCI's YAML API;
i.e. make use of CircleCI mechanisms for parameterization and conditional execution.
Data declarations (e.g. the nested loops for defining the configuration matrix)
should be at the top of the file for easy updating.
See this comment for design rationale:
https://github.com/pytorch/vision/pull/1321#issuecomment-531033978
"""
import jinja2
import yaml
import os.path
from packaging.version import Version, parse
import argparse
ALL_PYTHON_VERSIONS = [
"2.7",
"3.5",
"3.6",
"3.7",
]
ALL_CUDA_VERSIONS = [
"cu92",
"cu100",
"cu101",
]
BLACKLISTED_TEST_DIRS = {
# FIXME
# Works but too many unit tests (takes too long)
"pyro",
}
def get_applicable_os_list(btype):
os_list = ["macos"]
if btype != "wheel":
os_list.extend([
"linux", # TODO Get linux working with CUDA for wheels
"win",
])
return os_list
PARMNAME_IS_PYTHON_3 = "is-python3"
PARMNAME_IS_MACOS = "is-macos"
PARMNAME_RUN_EXTERNAL_PROJECTS = "run-external-projects"
STEP_PREFIX_EXAMPLE_TEST = "Example"
STEP_PREFIX_EXTERNAL_PROJECT = "Project"
TEST_NAME_PREFIX_SEPARATOR = ": "
def get_unicode_variants(btype, python_version):
return [False, True] if btype == "wheel" and python_version == "2.7" else [False]
def generate_workflows(category, prefix='', indentation=6):
w = []
for btype in ["wheel", "conda"]:
for os_type in get_applicable_os_list(btype):
# PyTorch for Python 2.7 builds are not supported on Windows.
python_versions = [p for p in ALL_PYTHON_VERSIONS if not (os_type == "win" and p == "2.7")]
for python_version in python_versions:
# TODO allow Windows to run CUDA
cuda_list = ALL_CUDA_VERSIONS if os_type == "linux" else []
for cu_version in (["cpu"] + cuda_list):
for unicode in get_unicode_variants(btype, python_version):
should_run_external_projects = python_version == ALL_PYTHON_VERSIONS[-1] and \
cu_version in ("cpu", ALL_CUDA_VERSIONS[-1])
w += workflow_item(
should_run_external_projects,
category,
btype,
os_type,
python_version,
cu_version,
unicode,
prefix,
)
return indent(indentation, w)
def workflow_item(
should_run_external_projects,
category,
btype,
os_type,
python_version,
cu_version,
unicode,
prefix=''):
unicode_suffix = "u" if unicode else ""
python_descriptor = f"py{python_version}{unicode_suffix}"
name_components = [prefix] if prefix else [] + [
category,
os_type,
btype,
python_descriptor,
cu_version,
]
base_workflow_name = "_".join(name_components)
w = generate_base_workflow(
should_run_external_projects,
base_workflow_name,
python_version,
cu_version,
unicode,
os_type,
btype,
)
return [w]
def generate_base_workflow(
should_run_external_projects,
base_workflow_name,
python_version,
cu_version,
unicode,
os_type,
btype):
d = {
"name": base_workflow_name,
"python_version": python_version,
"cu_version": cu_version,
}
if parse(python_version) >= Version("3"):
d[PARMNAME_IS_PYTHON_3] = True
if os_type == "macos":
d[PARMNAME_IS_MACOS] = True
if should_run_external_projects:
d[PARMNAME_RUN_EXTERNAL_PROJECTS] = True
if unicode:
d["unicode_abi"] = '1'
if cu_version == "cu92":
d["wheel_docker_image"] = "soumith/manylinux-cuda92"
job_name_pieces = [
"binary",
os_type,
btype,
]
if cu_version != "cpu" and btype == "conda":
job_name_pieces.append("cuda")
job_name = "_".join(job_name_pieces)
return {job_name: d}
def generate_subdirectory_paths(parent_directory, whitelisted_tests=None):
"""
Generate the tests, one for each repo.
The whitelist is for local test only (using command-line args).
The whitelist takes precendence over a blacklist.
"""
current_directory = os.path.abspath(os.getcwd())
print("current_directory:", current_directory)
def predicate_whitelists_and_blacklists(test_name):
if whitelisted_tests:
return test_name in whitelisted_tests
else:
return test_name not in BLACKLISTED_TEST_DIRS
return sorted([
os.path.normpath(os.path.join(parent_directory, o))
for o in os.listdir(parent_directory)
if os.path.isdir(os.path.join(parent_directory, o))
and os.path.exists(os.path.join(parent_directory, o, "run.sh"))
and predicate_whitelists_and_blacklists(o)
])
def wrap_conditional_steps(parameter_name, original_step_dicts, invert_boolean=False):
keyword = "unless" if invert_boolean else "when"
return {
keyword: {
"condition": "<< parameters.%s >>" % parameter_name,
"steps": original_step_dicts,
}
}
def render_step(test_name_prefix, testdir):
runner_cmd = os.path.join(testdir, "run.sh")
wrapper_args = [
"<< parameters.script-wrapper >>",
runner_cmd,
]
testname = os.path.basename(testdir)
raw_step = {
"run": {
"name": test_name_prefix + TEST_NAME_PREFIX_SEPARATOR + testname,
"command": " ".join(wrapper_args),
"no_output_timeout": 600,
# forces every test to run, even if the previous fails:
"when": "always",
}
}
conditional_parm = None
conditional_inversion = False
# Don't run these tests with Python 2.7
if testname in ["mnist_hogwild"]:
conditional_parm = PARMNAME_IS_PYTHON_3
# Runs out of disk space on MacOS when downloading many-GB file
if testname in ["fast_neural_style"]:
conditional_parm = PARMNAME_IS_MACOS
conditional_inversion = True
conditional_step = wrap_conditional_steps(conditional_parm, [raw_step], conditional_inversion)
wrapped_step = conditional_step if conditional_parm else raw_step
return wrapped_step
def gen_command_steps_for_subdir(whitelisted_tests):
"""
Whitelist may be empty.
"""
example_subdirs = generate_subdirectory_paths("test_community_repos/examples", whitelisted_tests)
external_project_subdirs = generate_subdirectory_paths("test_community_repos/external_projects", whitelisted_tests)
external_projects_steps = []
for testdir in external_project_subdirs:
external_projects_steps.append(render_step(STEP_PREFIX_EXTERNAL_PROJECT, testdir))
steps_list = []
for testdir in example_subdirs:
steps_list.append(render_step(STEP_PREFIX_EXAMPLE_TEST, testdir))
steps_list.append(wrap_conditional_steps(PARMNAME_RUN_EXTERNAL_PROJECTS, external_projects_steps))
return {
"description": "PyTorch examples",
"steps": steps_list,
"parameters": {
"script-wrapper": {
"type": "string",
"default": "",
"description": "A command to which the script will be passed as an argument",
},
PARMNAME_IS_PYTHON_3: {
"type": "boolean",
"default": False,
"description": "Whether this is Python 3",
},
PARMNAME_IS_MACOS: {
"type": "boolean",
"default": False,
"description": "Whether this is operating system is MacOS",
},
PARMNAME_RUN_EXTERNAL_PROJECTS: {
"type": "boolean",
"default": False,
"description": "Should external projects be run?",
},
},
}
def gen_commands(whitelisted_tests):
commands_dict = {
"run_integration_tests": gen_command_steps_for_subdir(whitelisted_tests),
}
return indent(2, commands_dict)
def indent(indentation, data_list):
return ("\n" + " " * indentation).join(yaml.dump(data_list).splitlines())
def get_cli_args():
parser = argparse.ArgumentParser(description='Regenerate CircleCI config')
parser.add_argument('whitelisted_tests', metavar='TESTS', type=str, nargs='*',
help='Whitelisted tests for local invocation')
return parser.parse_args()
if __name__ == "__main__":
d = os.path.dirname(__file__)
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(d),
lstrip_blocks=True,
autoescape=False,
)
options = get_cli_args()
generated_commands = gen_commands(options.whitelisted_tests)
with open(os.path.join(d, 'config.yml'), 'w') as f:
f.write(env.get_template('config.in.yml').render(
workflows_standard=generate_workflows("pytorch_test"),
workflows_nightly=generate_workflows("pytorch_test", prefix="nightly"),
generated_commands=generated_commands
))