File size: 6,273 Bytes
e202b16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
#
# This source code is licensed under the BSD license found in the
# LICENSE file in the root directory of this source tree.
import argparse
import os
import shutil
import subprocess
from dataclasses import dataclass
from pathlib import Path
from typing import List

THIS_PATH = Path(__file__).resolve()
SOURCE_ROOT_DIR = THIS_PATH.parents[1]


@dataclass
class Build:
    """
    Represents one configuration of a build, i.e.
    a set of versions of dependent libraries.

    Members:
        conda_always_copy: avoids hard linking which can behave weirdly.
        conda_debug: get added information about package search
        conda_dirty: see intermediate files after build
        build_inside_tree: output in build/ not ../build
        is_release: whether this is an official versioned release
    """

    python_version: str
    pytorch_version: str
    pytorch_channel: str
    cuda_version: str
    cuda_dep_runtime: str

    conda_always_copy: bool = True
    conda_debug: bool = False
    conda_dirty: bool = False
    build_inside_tree: bool = False

    def _set_env_for_build(self) -> None:
        """
        NOTE: Variables set here won't be visible in `setup.py`
        UNLESS they are also specified in meta.yaml
        """
        assert (
            "BUILD_VERSION" in os.environ
        ), "BUILD_VERSION must be set as env variable"
        tag = subprocess.check_output(
            ["git", "rev-parse", "--short", "HEAD"], text=True
        ).strip()
        os.environ["GIT_TAG"] = tag
        os.environ["PYTORCH_VERSION"] = self.pytorch_version
        os.environ["CU_VERSION"] = self.cuda_version
        os.environ["SOURCE_ROOT_DIR"] = str(SOURCE_ROOT_DIR)

        # At build time, the same major/minor (otherwise we might get a CPU pytorch ...)
        cuda_constraint_build = "=" + ".".join(self.cuda_version.split(".")[:2])
        pytorch_version_tuple = tuple(
            int(v) for v in self.pytorch_version.split(".")[:2]
        )
        if pytorch_version_tuple < (1, 13):
            os.environ[
                "CONDA_CUDA_CONSTRAINT_BUILD"
            ] = f"cudatoolkit{cuda_constraint_build}"
            os.environ[
                "CONDA_CUDA_CONSTRAINT_RUN"
            ] = f"cudatoolkit{self.cuda_dep_runtime}"
        else:
            os.environ[
                "CONDA_CUDA_CONSTRAINT_BUILD"
            ] = f"pytorch-cuda{cuda_constraint_build}"
            os.environ[
                "CONDA_CUDA_CONSTRAINT_RUN"
            ] = f"pytorch-cuda{self.cuda_dep_runtime}"

        if self.conda_always_copy:
            os.environ["CONDA_ALWAYS_COPY"] = "true"

    def _get_build_args(self) -> List[str]:
        args = [
            "conda",
            "build",
            "-c",
            self.pytorch_channel,
            "-c",
            "nvidia",
            "--python",
            self.python_version,
            "--no-anaconda-upload",
        ]
        if self.conda_debug:
            args += ["--debug"]
        if self.conda_dirty:
            args += ["--dirty"]
        if not self.build_inside_tree:
            args += ["--croot", "../build"]
        return args + ["packaging/xformers"]

    def do_build(self) -> None:
        self._set_env_for_build()
        args = self._get_build_args()
        print(args)
        subprocess.check_call(args)

    def move_artifacts_to_store(self, store_pytorch_package: bool) -> None:
        """
        Run after a build to move the built package, and, if using nightly, the
        used PyTorch package, to a location where they will be recognized
        as build artifacts.
        """
        print("moving artifacts")
        assert not self.build_inside_tree
        artifacts = Path("packages")
        artifacts.mkdir(exist_ok=True)
        for filename in Path("../build/linux-64").resolve().glob("*.tar.bz2"):
            print("moving", filename, "to", artifacts)
            shutil.move(filename, artifacts)
        if store_pytorch_package:
            for filename in Path("/opt/conda/pkgs").glob("pytorch-[12].*.tar.bz2"):
                print("moving", filename, "to", artifacts)
                shutil.move(filename, artifacts)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Build the conda package.")
    parser.add_argument(
        "--python", metavar="3.X", required=True, help="python version e.g. 3.10"
    )
    parser.add_argument(
        "--cuda-dep-runtime", metavar="1X.Y", required=True, help="eg '>=11.7,<11.9"
    )
    parser.add_argument(
        "--cuda", metavar="1X.Y", required=True, help="cuda version e.g. 11.3"
    )
    parser.add_argument(
        "--pytorch", metavar="1.Y.Z", required=True, help="PyTorch version e.g. 1.11.0"
    )
    parser.add_argument(
        "--build-inside-tree",
        action="store_true",
        help="Build in build/ instead of ../build/",
    )
    parser.add_argument(
        "--store",
        action="store_true",
        help="position artifact to store",
    )
    parser.add_argument(
        "--store-pytorch-package",
        action="store_true",
        help="position artifact to store",
    )
    parser.add_argument(
        "--pytorch-channel", default="pytorch", help="Use 'pytorch-nightly' for nightly"
    )
    args = parser.parse_args()

    pkg = Build(
        pytorch_channel=args.pytorch_channel,
        python_version=args.python,
        pytorch_version=args.pytorch,
        cuda_version=args.cuda,
        build_inside_tree=args.build_inside_tree,
        cuda_dep_runtime=args.cuda_dep_runtime,
    )

    pkg.do_build()
    pkg.move_artifacts_to_store(store_pytorch_package=args.store_pytorch_package)


# python packaging/conda/build_conda.py  --cuda 11.6 --python 3.10 --pytorch 1.12.1
# python packaging/conda/build_conda.py  --cuda 11.3 --python 3.9 --pytorch 1.12.1  # <= the dino one
# python packaging/conda/build_conda.py  --cuda 11.6 --python 3.10 --pytorch 1.11.0

# Note this does the build outside the root of the tree.

# TODO:
# - Make a local conda package cache available inside docker
# - do we need builds for both _GLIBCXX_USE_CXX11_ABI values?
# - how to prevent some cpu only builds of pytorch from being discovered?