regenerate.py 4.75 KB
Newer Older
facebook-github-bot's avatar
facebook-github-bot committed
1
#!/usr/bin/env python3
2
# Copyright (c) Meta Platforms, Inc. and affiliates.
Patrick Labatut's avatar
Patrick Labatut committed
3
4
5
6
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
facebook-github-bot's avatar
facebook-github-bot committed
7
8
9
10
11
12

"""
This script is adapted from the torchvision one.
"""

import os.path
13

facebook-github-bot's avatar
facebook-github-bot committed
14
15
import jinja2
import yaml
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
16
from packaging import version
facebook-github-bot's avatar
facebook-github-bot committed
17
18


Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
19
20
# The CUDA versions which have pytorch conda packages available for linux for each
# version of pytorch.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
21
# Pytorch 1.4 also supports cuda 10.0 but we no longer build for cuda 10.0 at all.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
22
CONDA_CUDA_VERSIONS = {
23
    "1.10.0": ["cu102", "cu111", "cu113"],
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
24
    "1.10.1": ["cu102", "cu111", "cu113"],
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
25
26
    "1.10.2": ["cu102", "cu111", "cu113"],
    "1.11.0": ["cu102", "cu111", "cu113", "cu115"],
27
    "1.12.0": ["cu102", "cu113", "cu116"],
28
    "1.12.1": ["cu102", "cu113", "cu116"],
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
29
    "1.13.0": ["cu116", "cu117"],
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
30
    "1.13.1": ["cu116", "cu117"],
31
    "2.0.0": ["cu117", "cu118"],
32
    "2.0.1": ["cu117", "cu118"],
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
33
34
35
}


Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
36
def conda_docker_image_for_cuda(cuda_version):
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
37
38
    if cuda_version in ("cu101", "cu102", "cu111"):
        return None
39
40
41
    if len(cuda_version) != 5:
        raise ValueError("Unknown cuda version")
    return "pytorch/conda-builder:cuda" + cuda_version[2:]
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
42
43


44
def pytorch_versions_for_python(python_version):
45
    if python_version in ["3.8", "3.9"]:
46
        return list(CONDA_CUDA_VERSIONS)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
47
48
49
50
51
52
    if python_version == "3.10":
        return [
            i
            for i in CONDA_CUDA_VERSIONS
            if version.Version(i) >= version.Version("1.11.0")
        ]
53
54


facebook-github-bot's avatar
facebook-github-bot committed
55
56
57
def workflows(prefix="", filter_branch=None, upload=False, indentation=6):
    w = []
    for btype in ["conda"]:
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
58
        for python_version in ["3.8", "3.9", "3.10"]:
59
            for pytorch_version in pytorch_versions_for_python(python_version):
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
60
61
62
63
64
65
66
67
68
69
                for cu_version in CONDA_CUDA_VERSIONS[pytorch_version]:
                    w += workflow_pair(
                        btype=btype,
                        python_version=python_version,
                        pytorch_version=pytorch_version,
                        cu_version=cu_version,
                        prefix=prefix,
                        upload=upload,
                        filter_branch=filter_branch,
                    )
facebook-github-bot's avatar
facebook-github-bot committed
70
71
72
73
74

    return indent(indentation, w)


def workflow_pair(
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
75
76
77
78
79
80
81
82
    *,
    btype,
    python_version,
    pytorch_version,
    cu_version,
    prefix="",
    upload=False,
    filter_branch,
facebook-github-bot's avatar
facebook-github-bot committed
83
84
85
):

    w = []
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
86
87
88
    py = python_version.replace(".", "")
    pyt = pytorch_version.replace(".", "")
    base_workflow_name = f"{prefix}linux_{btype}_py{py}_{cu_version}_pyt{pyt}"
facebook-github-bot's avatar
facebook-github-bot committed
89
90
91
92
93

    w.append(
        generate_base_workflow(
            base_workflow_name=base_workflow_name,
            python_version=python_version,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
94
            pytorch_version=pytorch_version,
facebook-github-bot's avatar
facebook-github-bot committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
            cu_version=cu_version,
            btype=btype,
            filter_branch=filter_branch,
        )
    )

    if upload:
        w.append(
            generate_upload_workflow(
                base_workflow_name=base_workflow_name,
                btype=btype,
                cu_version=cu_version,
                filter_branch=filter_branch,
            )
        )

    return w


def generate_base_workflow(
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
115
116
117
118
119
120
121
    *,
    base_workflow_name,
    python_version,
    cu_version,
    pytorch_version,
    btype,
    filter_branch=None,
facebook-github-bot's avatar
facebook-github-bot committed
122
123
124
125
126
127
):

    d = {
        "name": base_workflow_name,
        "python_version": python_version,
        "cu_version": cu_version,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
128
        "pytorch_version": pytorch_version,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
129
        "context": "DOCKERHUB_TOKEN",
facebook-github-bot's avatar
facebook-github-bot committed
130
131
    }

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
132
133
134
135
    conda_docker_image = conda_docker_image_for_cuda(cu_version)
    if conda_docker_image is not None:
        d["conda_docker_image"] = conda_docker_image

facebook-github-bot's avatar
facebook-github-bot committed
136
137
138
139
140
141
    if filter_branch is not None:
        d["filters"] = {"branches": {"only": filter_branch}}

    return {f"binary_linux_{btype}": d}


142
def generate_upload_workflow(*, base_workflow_name, btype, cu_version, filter_branch):
facebook-github-bot's avatar
facebook-github-bot committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    d = {
        "name": f"{base_workflow_name}_upload",
        "context": "org-member",
        "requires": [base_workflow_name],
    }

    if btype == "wheel":
        d["subfolder"] = cu_version + "/"

    if filter_branch is not None:
        d["filters"] = {"branches": {"only": filter_branch}}

    return {f"binary_{btype}_upload": d}


def indent(indentation, data_list):
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
159
160
    if len(data_list) == 0:
        return ""
facebook-github-bot's avatar
facebook-github-bot committed
161
162
163
164
165
166
167
168
    return ("\n" + " " * indentation).join(
        yaml.dump(data_list, default_flow_style=False).splitlines()
    )


if __name__ == "__main__":
    d = os.path.dirname(__file__)
    env = jinja2.Environment(
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
169
170
171
172
        loader=jinja2.FileSystemLoader(d),
        lstrip_blocks=True,
        autoescape=False,
        keep_trailing_newline=True,
facebook-github-bot's avatar
facebook-github-bot committed
173
174
175
176
    )

    with open(os.path.join(d, "config.yml"), "w") as f:
        f.write(env.get_template("config.in.yml").render(workflows=workflows))