Skip to content

Commit

Permalink
Avoid calling patchelf (microsoft#17365)
Browse files Browse the repository at this point in the history
### Description
Resolve microsoft#9754
  • Loading branch information
snnn authored and kleiti committed Mar 22, 2024
1 parent d051aba commit 73e0aa7
Showing 1 changed file with 33 additions and 101 deletions.
134 changes: 33 additions & 101 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datetime
import logging
import platform
import shlex
import subprocess
import sys
from glob import glob, iglob
Expand Down Expand Up @@ -183,108 +184,37 @@ def run(self):
dest = "onnxruntime/capi/onnxruntime_pybind11_state_manylinux1.so"
logger.info("copying %s -> %s", source, dest)
copyfile(source, dest)
result = subprocess.run(
["patchelf", "--print-needed", dest], check=True, stdout=subprocess.PIPE, text=True
)
dependencies = [
"librccl.so",
"libamdhip64.so",
"librocblas.so",
"libMIOpen.so",
"libhsa-runtime64.so",
"libhsakmt.so",
]

to_preload = []
to_preload_cuda = []
to_preload_tensorrt = []
to_preload_cann = []
cuda_dependencies = []
args = ["patchelf", "--debug"]
for line in result.stdout.split("\n"):
for dependency in dependencies:
if dependency in line:
to_preload.append(line)
args.extend(["--remove-needed", line])
args.append(dest)
if len(args) > 3:
subprocess.run(args, check=True, stdout=subprocess.PIPE)

dest = "onnxruntime/capi/libonnxruntime_providers_" + ("rocm.so" if is_rocm else "cuda.so")
if path.isfile(dest):
result = subprocess.run(
["patchelf", "--print-needed", dest],
check=True,
stdout=subprocess.PIPE,
text=True,
)
cuda_dependencies = [
"libcublas.so",
"libcublasLt.so",
"libcudnn.so",
"libcudart.so",
"libcurand.so",
"libcufft.so",
"libnvToolsExt.so",
"libcupti.so",
]
rocm_dependencies = [
"librccl.so",
"libamdhip64.so",
"librocblas.so",
"libMIOpen.so",
"libhsa-runtime64.so",
"libhsakmt.so",
]
args = ["patchelf", "--debug"]
for line in result.stdout.split("\n"):
for dependency in cuda_dependencies + rocm_dependencies:
if dependency in line:
if dependency not in to_preload:
to_preload_cuda.append(line)
args.extend(["--remove-needed", line])
args.append(dest)
if len(args) > 3:
subprocess.run(args, check=True, stdout=subprocess.PIPE)

dest = "onnxruntime/capi/libonnxruntime_providers_" + ("migraphx.so" if is_rocm else "tensorrt.so")
if path.isfile(dest):
result = subprocess.run(
["patchelf", "--print-needed", dest],
check=True,
stdout=subprocess.PIPE,
text=True,
)
tensorrt_dependencies = ["libnvinfer.so", "libnvinfer_plugin.so", "libnvonnxparser.so"]
args = ["patchelf", "--debug"]
for line in result.stdout.split("\n"):
for dependency in cuda_dependencies + tensorrt_dependencies:
if dependency in line:
if dependency not in (to_preload + to_preload_cuda):
to_preload_tensorrt.append(line)
args.extend(["--remove-needed", line])
args.append(dest)
if len(args) > 3:
subprocess.run(args, check=True, stdout=subprocess.PIPE)

dest = "onnxruntime/capi/libonnxruntime_providers_cann.so"
if path.isfile(dest):
result = subprocess.run(
["patchelf", "--print-needed", dest],
check=True,
stdout=subprocess.PIPE,
text=True,
)
cann_dependencies = ["libascendcl.so", "libacl_op_compiler.so", "libfmk_onnx_parser.so"]
args = ["patchelf", "--debug"]
for line in result.stdout.split("\n"):
for dependency in cann_dependencies:
if dependency in line:
if dependency not in to_preload:
to_preload_cann.append(line)
args.extend(["--remove-needed", line])
args.append(dest)
if len(args) > 3:
subprocess.run(args, check=True, stdout=subprocess.PIPE)

cuda_dependencies = [
"libcublas.so.11",
"libcublasLt.so.11",
"libcudnn.so.8",
"libcudart.so.11.0",
"libcurand.so.10",
"libcufft.so.10",
]
rocm_dependencies = [
"librccl.so.1",
"libnuma.so.1",
"libamd_comgr.so.2",
"libdrm.so.2",
"librocblas.so.0",
"libdrm_amdgpu.so.1",
"libamdhip64.so.5",
"libroctracer64.so.4",
"libMIOpen.so.1",
"libtinfo.so.6",
"libelf.so.1",
"librocm_smi64.so.5",
"libhsa-runtime64.so.1",
]

tensorrt_dependencies = ["libnvinfer.so.8.6", "libnvinfer_plugin.so.8.6", "libnvonnxparser.so.8.6"]

dest = "onnxruntime/capi/libonnxruntime_providers_openvino.so"
if path.isfile(dest):
Expand All @@ -308,10 +238,12 @@ def run(self):
assert self.dist_dir is not None
file = glob(path.join(self.dist_dir, "*linux*.whl"))[0]
logger.info("repairing %s for manylinux1", file)
auditwheel_cmd = ["auditwheel", "-v", "repair", "-w", self.dist_dir, file]
for i in cuda_dependencies + rocm_dependencies + tensorrt_dependencies:
auditwheel_cmd += ["--exclude", i]
logger.info("Running {}".format(" ".join([shlex.quote(arg) for arg in auditwheel_cmd])))
try:
subprocess.run(
["auditwheel", "repair", "-w", self.dist_dir, file], check=True, stdout=subprocess.PIPE
)
subprocess.run(auditwheel_cmd, check=True, stdout=subprocess.PIPE)
finally:
logger.info("removing %s", file)
remove(file)
Expand Down

0 comments on commit 73e0aa7

Please sign in to comment.