|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# encoding: UTF-8 |
| 3 | + |
| 4 | +import os |
| 5 | +import shutil |
| 6 | +from subprocess import check_call, check_output |
| 7 | +from typing import List |
| 8 | + |
| 9 | +from pygit2 import Repository |
| 10 | + |
| 11 | + |
| 12 | +def list_dir(path: str) -> List[str]: |
| 13 | + """' |
| 14 | + Helper for getting paths for Python |
| 15 | + """ |
| 16 | + return check_output(["ls", "-1", path]).decode().split("\n") |
| 17 | + |
| 18 | + |
| 19 | +def build_ArmComputeLibrary() -> None: |
| 20 | + """ |
| 21 | + Using ArmComputeLibrary for aarch64 PyTorch |
| 22 | + """ |
| 23 | + print("Building Arm Compute Library") |
| 24 | + acl_build_flags = [ |
| 25 | + "debug=0", |
| 26 | + "neon=1", |
| 27 | + "opencl=0", |
| 28 | + "os=linux", |
| 29 | + "openmp=1", |
| 30 | + "cppthreads=0", |
| 31 | + "arch=armv8a", |
| 32 | + "multi_isa=1", |
| 33 | + "fixed_format_kernels=1", |
| 34 | + "build=native", |
| 35 | + ] |
| 36 | + acl_install_dir = "/acl" |
| 37 | + acl_checkout_dir = "ComputeLibrary" |
| 38 | + os.makedirs(acl_install_dir) |
| 39 | + check_call( |
| 40 | + [ |
| 41 | + "git", |
| 42 | + "clone", |
| 43 | + "https://github.com/ARM-software/ComputeLibrary.git", |
| 44 | + "-b", |
| 45 | + "v24.09", |
| 46 | + "--depth", |
| 47 | + "1", |
| 48 | + "--shallow-submodules", |
| 49 | + ] |
| 50 | + ) |
| 51 | + |
| 52 | + check_call( |
| 53 | + ["scons", "Werror=1", "-j8", f"build_dir=/{acl_install_dir}/build"] |
| 54 | + + acl_build_flags, |
| 55 | + cwd=acl_checkout_dir, |
| 56 | + ) |
| 57 | + for d in ["arm_compute", "include", "utils", "support", "src"]: |
| 58 | + shutil.copytree(f"{acl_checkout_dir}/{d}", f"{acl_install_dir}/{d}") |
| 59 | + |
| 60 | + |
| 61 | +def update_wheel(wheel_path) -> None: |
| 62 | + """ |
| 63 | + Update the cuda wheel libraries |
| 64 | + """ |
| 65 | + folder = os.path.dirname(wheel_path) |
| 66 | + wheelname = os.path.basename(wheel_path) |
| 67 | + os.mkdir(f"{folder}/tmp") |
| 68 | + os.system(f"unzip {wheel_path} -d {folder}/tmp") |
| 69 | + libs_to_copy = [ |
| 70 | + "/usr/local/cuda/extras/CUPTI/lib64/libcupti.so.12", |
| 71 | + "/usr/local/cuda/lib64/libcudnn.so.9", |
| 72 | + "/usr/local/cuda/lib64/libcublas.so.12", |
| 73 | + "/usr/local/cuda/lib64/libcublasLt.so.12", |
| 74 | + "/usr/local/cuda/lib64/libcudart.so.12", |
| 75 | + "/usr/local/cuda/lib64/libcufft.so.11", |
| 76 | + "/usr/local/cuda/lib64/libcusparse.so.12", |
| 77 | + "/usr/local/cuda/lib64/libcusparseLt.so.0", |
| 78 | + "/usr/local/cuda/lib64/libcusolver.so.11", |
| 79 | + "/usr/local/cuda/lib64/libcurand.so.10", |
| 80 | + "/usr/local/cuda/lib64/libnvToolsExt.so.1", |
| 81 | + "/usr/local/cuda/lib64/libnvJitLink.so.12", |
| 82 | + "/usr/local/cuda/lib64/libnvrtc.so.12", |
| 83 | + "/usr/local/cuda/lib64/libnvrtc-builtins.so.12.4", |
| 84 | + "/usr/local/cuda/lib64/libcudnn_adv.so.9", |
| 85 | + "/usr/local/cuda/lib64/libcudnn_cnn.so.9", |
| 86 | + "/usr/local/cuda/lib64/libcudnn_graph.so.9", |
| 87 | + "/usr/local/cuda/lib64/libcudnn_ops.so.9", |
| 88 | + "/usr/local/cuda/lib64/libcudnn_engines_runtime_compiled.so.9", |
| 89 | + "/usr/local/cuda/lib64/libcudnn_engines_precompiled.so.9", |
| 90 | + "/usr/local/cuda/lib64/libcudnn_heuristic.so.9", |
| 91 | + "/opt/conda/envs/aarch64_env/lib/libgomp.so.1", |
| 92 | + "/usr/lib64/libgfortran.so.5", |
| 93 | + "/acl/build/libarm_compute.so", |
| 94 | + "/acl/build/libarm_compute_graph.so", |
| 95 | + ] |
| 96 | + if enable_cuda: |
| 97 | + libs_to_copy += [ |
| 98 | + "/usr/local/lib/libnvpl_lapack_lp64_gomp.so.0", |
| 99 | + "/usr/local/lib/libnvpl_blas_lp64_gomp.so.0", |
| 100 | + "/usr/local/lib/libnvpl_lapack_core.so.0", |
| 101 | + "/usr/local/lib/libnvpl_blas_core.so.0", |
| 102 | + ] |
| 103 | + else: |
| 104 | + libs_to_copy += [ |
| 105 | + "/opt/OpenBLAS/lib/libopenblas.so.0", |
| 106 | + ] |
| 107 | + # Copy libraries to unzipped_folder/a/lib |
| 108 | + for lib_path in libs_to_copy: |
| 109 | + lib_name = os.path.basename(lib_path) |
| 110 | + shutil.copy2(lib_path, f"{folder}/tmp/torch/lib/{lib_name}") |
| 111 | + os.system( |
| 112 | + f"cd {folder}/tmp/torch/lib/; " |
| 113 | + f"patchelf --set-rpath '$ORIGIN' --force-rpath {folder}/tmp/torch/lib/{lib_name}" |
| 114 | + ) |
| 115 | + os.mkdir(f"{folder}/cuda_wheel") |
| 116 | + os.system(f"cd {folder}/tmp/; zip -r {folder}/cuda_wheel/{wheelname} *") |
| 117 | + shutil.move( |
| 118 | + f"{folder}/cuda_wheel/{wheelname}", |
| 119 | + f"{folder}/{wheelname}", |
| 120 | + copy_function=shutil.copy2, |
| 121 | + ) |
| 122 | + os.system(f"rm -rf {folder}/tmp/ {folder}/cuda_wheel/") |
| 123 | + |
| 124 | + |
| 125 | +def complete_wheel(folder: str) -> str: |
| 126 | + """ |
| 127 | + Complete wheel build and put in artifact location |
| 128 | + """ |
| 129 | + wheel_name = list_dir(f"/{folder}/dist")[0] |
| 130 | + |
| 131 | + if "pytorch" in folder and not enable_cuda: |
| 132 | + print("Repairing Wheel with AuditWheel") |
| 133 | + check_call(["auditwheel", "repair", f"dist/{wheel_name}"], cwd=folder) |
| 134 | + repaired_wheel_name = list_dir(f"/{folder}/wheelhouse")[0] |
| 135 | + |
| 136 | + print(f"Moving {repaired_wheel_name} wheel to /{folder}/dist") |
| 137 | + os.rename( |
| 138 | + f"/{folder}/wheelhouse/{repaired_wheel_name}", |
| 139 | + f"/{folder}/dist/{repaired_wheel_name}", |
| 140 | + ) |
| 141 | + else: |
| 142 | + repaired_wheel_name = wheel_name |
| 143 | + |
| 144 | + print(f"Copying {repaired_wheel_name} to artifacts") |
| 145 | + shutil.copy2( |
| 146 | + f"/{folder}/dist/{repaired_wheel_name}", f"/artifacts/{repaired_wheel_name}" |
| 147 | + ) |
| 148 | + |
| 149 | + return repaired_wheel_name |
| 150 | + |
| 151 | + |
| 152 | +def parse_arguments(): |
| 153 | + """ |
| 154 | + Parse inline arguments |
| 155 | + """ |
| 156 | + from argparse import ArgumentParser |
| 157 | + |
| 158 | + parser = ArgumentParser("AARCH64 wheels python CD") |
| 159 | + parser.add_argument("--debug", action="store_true") |
| 160 | + parser.add_argument("--build-only", action="store_true") |
| 161 | + parser.add_argument("--test-only", type=str) |
| 162 | + parser.add_argument("--enable-mkldnn", action="store_true") |
| 163 | + parser.add_argument("--enable-cuda", action="store_true") |
| 164 | + return parser.parse_args() |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == "__main__": |
| 168 | + """ |
| 169 | + Entry Point |
| 170 | + """ |
| 171 | + args = parse_arguments() |
| 172 | + enable_mkldnn = args.enable_mkldnn |
| 173 | + enable_cuda = args.enable_cuda |
| 174 | + repo = Repository("/pytorch") |
| 175 | + branch = repo.head.name |
| 176 | + if branch == "HEAD": |
| 177 | + branch = "master" |
| 178 | + |
| 179 | + print("Building PyTorch wheel") |
| 180 | + build_vars = "MAX_JOBS=5 CMAKE_SHARED_LINKER_FLAGS=-Wl,-z,max-page-size=0x10000 " |
| 181 | + os.system("cd /pytorch; python setup.py clean") |
| 182 | + |
| 183 | + override_package_version = os.getenv("OVERRIDE_PACKAGE_VERSION") |
| 184 | + if override_package_version is not None: |
| 185 | + version = override_package_version |
| 186 | + build_vars += ( |
| 187 | + f"BUILD_TEST=0 PYTORCH_BUILD_VERSION={version} PYTORCH_BUILD_NUMBER=1 " |
| 188 | + ) |
| 189 | + elif branch in ["nightly", "master"]: |
| 190 | + build_date = ( |
| 191 | + check_output(["git", "log", "--pretty=format:%cs", "-1"], cwd="/pytorch") |
| 192 | + .decode() |
| 193 | + .replace("-", "") |
| 194 | + ) |
| 195 | + version = ( |
| 196 | + check_output(["cat", "version.txt"], cwd="/pytorch").decode().strip()[:-2] |
| 197 | + ) |
| 198 | + if enable_cuda: |
| 199 | + desired_cuda = os.getenv("DESIRED_CUDA") |
| 200 | + build_vars += f"BUILD_TEST=0 PYTORCH_BUILD_VERSION={version}.dev{build_date}+{desired_cuda} PYTORCH_BUILD_NUMBER=1 " |
| 201 | + else: |
| 202 | + build_vars += f"BUILD_TEST=0 PYTORCH_BUILD_VERSION={version}.dev{build_date} PYTORCH_BUILD_NUMBER=1 " |
| 203 | + elif branch.startswith(("v1.", "v2.")): |
| 204 | + build_vars += f"BUILD_TEST=0 PYTORCH_BUILD_VERSION={branch[1:branch.find('-')]} PYTORCH_BUILD_NUMBER=1 " |
| 205 | + |
| 206 | + if enable_mkldnn: |
| 207 | + build_ArmComputeLibrary() |
| 208 | + print("build pytorch with mkldnn+acl backend") |
| 209 | + build_vars += ( |
| 210 | + "USE_MKLDNN=ON USE_MKLDNN_ACL=ON " |
| 211 | + "ACL_ROOT_DIR=/acl " |
| 212 | + "LD_LIBRARY_PATH=/pytorch/build/lib:/acl/build:$LD_LIBRARY_PATH " |
| 213 | + "ACL_INCLUDE_DIR=/acl/build " |
| 214 | + "ACL_LIBRARY=/acl/build " |
| 215 | + ) |
| 216 | + if enable_cuda: |
| 217 | + build_vars += "BLAS=NVPL " |
| 218 | + else: |
| 219 | + build_vars += "BLAS=OpenBLAS OpenBLAS_HOME=/OpenBLAS " |
| 220 | + else: |
| 221 | + print("build pytorch without mkldnn backend") |
| 222 | + |
| 223 | + os.system(f"cd /pytorch; {build_vars} python3 setup.py bdist_wheel") |
| 224 | + if enable_cuda: |
| 225 | + print("Updating Cuda Dependency") |
| 226 | + filename = os.listdir("/pytorch/dist/") |
| 227 | + wheel_path = f"/pytorch/dist/{filename[0]}" |
| 228 | + update_wheel(wheel_path) |
| 229 | + pytorch_wheel_name = complete_wheel("/pytorch/") |
| 230 | + print(f"Build Complete. Created {pytorch_wheel_name}..") |
0 commit comments