conda_build.py 9.6 KB
Newer Older
1
#!/bin/python
2 3

# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
4
#
5 6 7
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11 12 13 14 15 16
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

17 18 19
import argparse
import os

20 21 22
#
import platform

23 24 25

def parse_args():
    parser = argparse.ArgumentParser("conda build for paddlepaddle version")
26 27 28 29 30 31
    parser.add_argument(
        "--paddle_version",
        type=str,
        required=True,
        help="paddle version for conda build.",
    )
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    args = parser.parse_args()

    return args


class ConstantVar:
    def __init__(self):
        self.build = r"""
build:
  number: '0'
  string: """

        self.requirement_build = r"""
requirements:
  build:
47
    - numpy>=1.13
48 49 50 51 52 53
    - cython
    - setuptools
"""

        self.requirement_run = r"""
  run:
54 55 56 57 58
    - requests>=2.20.0
    - numpy>=1.13
    - protobuf>=3.1.0
    - gast==0.3.3
    - Pillow
59
    - decorator
60
    - astor
61 62 63 64
"""

        self.requirement_run_windows = r"""
  run:
65 66 67 68 69
    - requests>=2.20.0
    - numpy>=1.13
    - protobuf>=3.1.0
    - gast==0.3.3
    - Pillow
70
    - decorator
71
    - astor
72
"""
73
        self.test = r"""
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
test:
  import:
    paddle
"""

        self.about = r"""
about:
  home: http://www.paddlepaddle.org/
  license: APACHE 2.0
  license_family: APACHE
  summary: an easy-to-use, efficient, flexible and scalable deep learning platform
"""

        self.build_const = r"""
"""

90
        self.blt_const = r"""
91 92 93
"""

        self.python37 = r"    - python>=3.7, <3.8"
94
        self.python38 = r"    - python>=3.8, <3.9"
95
        self.python39 = r"    - python>=3.9, <3.10"
96
        self.python310 = r"   - python>=3.10, <3.11"
97 98

        self.python_version = [
99 100 101
            self.python37,
            self.python38,
            self.python39,
102
            self.python310,
103 104
        ]

105 106 107 108 109 110 111 112
        self.cuda101 = r"""
    - cudatoolkit>=10.1, <10.2
    - cudnn>=7.6, <7.7
    """
        self.cuda102 = r"""
    - cudatoolkit>=10.2, <10.3
    - cudnn>=7.6, <7.7
    """
113 114 115 116 117
        self.cuda112 = r"""
    - cudatoolkit>=11.2, <11.3
    - cudnn>=8.1, <8.2
    """

118 119 120 121 122
        self.cuda_info = [
            (self.cuda101, "cuda10.1", ".post101"),
            (self.cuda102, "cuda10.2", ""),
            (self.cuda112, "cuda11.2", ".post112"),
        ]
123
        self.py_str = ["py37", "py38", "py39", "py310"]
124 125
        self.pip_end = ".whl --no-deps"
        self.pip_prefix_linux = "pip install /package/paddlepaddle"
126
        self.pip_prefix_windows = r"pip install C:\package\paddlepaddle"
127 128 129
        self.pip_gpu = "_gpu-"
        self.pip_cpu = "-"
        self.mac_pip = [
130 131 132
            "-cp37-cp37m-macosx_10_6_intel",
            "-cp38-cp38-macosx_10_14_x86_64",
            "-cp39-cp39-macosx_10_14_x86_64",
133
            "-cp310-cp310-macosx_10_14_x86_64",
134 135
        ]
        self.linux_pip = [
136 137 138
            "-cp37-cp37m-linux_x86_64",
            "-cp38-cp38-linux_x86_64",
            "-cp39-cp39-linux_x86_64",
139
            "-cp310-cp310-linux_x86_64",
140 141
        ]
        self.windows_pip = [
142 143 144
            "-cp37-cp37m-win_amd64",
            "-cp38-cp38-win_amd64",
            "-cp39-cp39-win_amd64",
145
            "-cp310-cp310-win_amd64",
146 147 148 149
        ]


def meta_build_mac(var, python_str, paddle_version, build_var, build_name_str):
150 151
    package_str = (
        """
152 153
package:
  name: paddlepaddle
154 155 156 157 158 159
  version: """
        + paddle_version
    )
    requirement = (
        var.requirement_build + python_str + var.requirement_run + python_str
    )
160 161 162 163 164 165 166 167 168 169 170 171
    meta_build = var.build + build_name_str
    meta_str = package_str + meta_build + requirement + var.test + var.about
    build_str = var.build_const + build_var

    meta_filename = "meta.yaml"
    build_filename = "build.sh"
    with open(meta_filename, 'w') as f:
        f.write(meta_str)
    with open(build_filename, 'w') as f:
        f.write(build_str)


172 173 174
def meta_build_linux(
    var, python_str, paddle_version, build_var, build_name_str, cuda_str=None
):
175
    if cuda_str is None:
176 177
        package_str = (
            """
178 179
package:
  name: paddlepaddle
180 181 182
  version: """
            + paddle_version
        )
183
    else:
184 185
        package_str = (
            """
186 187
package:
  name: paddlepaddle-gpu
188 189 190 191 192 193
  version: """
            + paddle_version
        )
    requirement = (
        var.requirement_build + python_str + var.requirement_run + python_str
    )
194 195
    meta_build = var.build + build_name_str
    meta_str = package_str + meta_build + requirement
196
    if not (cuda_str is None):
197 198 199 200 201 202 203 204 205 206 207 208 209
        meta_str = meta_str + cuda_str
    meta_str = meta_str + var.test + var.about

    build_str = var.build_const + build_var

    meta_filename = "meta.yaml"
    build_filename = "build.sh"
    with open(meta_filename, 'w') as f:
        f.write(meta_str)
    with open(build_filename, 'w') as f:
        f.write(build_str)


210 211 212
def meta_build_windows(
    var, python_str, paddle_version, blt_var, build_name_str, cuda_str=None
):
213
    if cuda_str is None:
214 215
        package_str = (
            """
216 217
package:
  name: paddlepaddle
218 219 220
  version: """
            + paddle_version
        )
221
    else:
222 223
        package_str = (
            """
224 225
package:
  name: paddlepaddle-gpu
226 227 228 229 230 231 232 233 234 235
  version: """
            + paddle_version
        )

    requirement = (
        var.requirement_build
        + python_str
        + var.requirement_run_windows
        + python_str
    )
236 237
    meta_build = var.build + build_name_str
    meta_str = package_str + meta_build + requirement
238

239
    if not (cuda_str is None):
240
        meta_str = meta_str + cuda_str
241

242
    blt_str = var.blt_const + blt_var
243

244
    meta_str = meta_str + var.test + var.about
245 246 247 248 249 250 251 252 253 254
    meta_filename = "meta.yaml"
    build_filename = "bld.bat"
    with open(meta_filename, 'w') as f:
        f.write(meta_str)
    with open(build_filename, 'w') as f:
        f.write(blt_str)


def conda_build(paddle_version, var):
    sysstr = platform.system()
255
    if sysstr == "Windows":
256 257 258
        os.system("mkdir paddle")
        os.chdir(r"./paddle")
        for i in range(len(var.python_version)):
259 260 261 262 263 264 265
            blt_var = (
                var.pip_prefix_windows
                + var.pip_cpu
                + paddle_version
                + var.windows_pip[i]
                + var.pip_end
            )
266 267 268 269 270 271 272 273
            name = var.py_str[i] + "_cpu_windows"
            python_str = var.python_version[i]
            meta_build_windows(var, python_str, paddle_version, blt_var, name)
            os.system("conda build .")

        for i in range(len(var.python_version)):
            for cuda_str in var.cuda_info:
                post = cuda_str[2]
274 275 276 277 278 279 280 281
                blt_var = (
                    var.pip_prefix_windows
                    + var.pip_gpu
                    + paddle_version
                    + post
                    + var.windows_pip[i]
                    + var.pip_end
                )
282 283 284
                name = var.py_str[i] + "_gpu_" + cuda_str[1] + "_windows"
                cuda_cudnn_str = cuda_str[0]
                python_str = var.python_version[i]
285 286 287 288 289 290 291 292
                meta_build_windows(
                    var,
                    python_str,
                    paddle_version,
                    blt_var,
                    name,
                    cuda_cudnn_str,
                )
293 294
                os.system("conda build .")

295
    elif sysstr == "Linux":
296 297 298
        os.system("mkdir paddle")
        os.chdir(r"./paddle")
        for i in range(len(var.python_version)):
299 300 301 302 303 304 305
            build_var = (
                var.pip_prefix_linux
                + var.pip_cpu
                + paddle_version
                + var.linux_pip[i]
                + var.pip_end
            )
306 307 308 309 310 311 312 313
            name = var.py_str[i] + "_cpu_many_linux"
            python_str = var.python_version[i]
            meta_build_linux(var, python_str, paddle_version, build_var, name)
            os.system("conda build .")

        for i in range(len(var.python_version)):
            for cuda_str in var.cuda_info:
                post = cuda_str[2]
314 315 316 317 318 319 320 321
                build_var = (
                    var.pip_prefix_linux
                    + var.pip_gpu
                    + paddle_version
                    + post
                    + var.linux_pip[i]
                    + var.pip_end
                )
322 323 324
                name = var.py_str[i] + "_gpu_" + cuda_str[1] + "_many_linux"
                cuda_cudnn_str = cuda_str[0]
                python_str = var.python_version[i]
325 326 327 328 329 330 331 332
                meta_build_linux(
                    var,
                    python_str,
                    paddle_version,
                    build_var,
                    name,
                    cuda_cudnn_str,
                )
333 334 335 336
                os.system("conda build .")

        os.system("cd ..")

337
    elif sysstr == "Darwin":
338 339 340
        os.system("mkdir paddle")
        os.chdir(r"./paddle")
        for i in range(len(var.python_version)):
341 342 343 344 345 346 347
            build_var = (
                var.pip_prefix_linux
                + var.pip_cpu
                + paddle_version
                + var.mac_pip[i]
                + var.pip_end
            )
348 349 350 351 352 353 354 355 356 357 358 359 360
            name = var.py_str[i] + "_mac"
            python_str = var.python_version[i]
            meta_build_mac(var, python_str, paddle_version, build_var, name)
            os.system("conda build .")

        os.system("cd ..")


if __name__ == "__main__":
    args = parse_args()
    paddle_version = args.paddle_version
    var = ConstantVar()
    conda_build(paddle_version, var)