You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

create_cuda_build_libs.py 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. import subprocess
  5. import glob
  6. def handle_cuda_libs(path):
  7. subprocess.check_call('rm -rf tmp && rm -rf tmp_sub', shell=True)
  8. print('\nhandle cuda file from.{}'.format(path))
  9. cmd = 'dpkg-deb -xv {} tmp'.format(path)
  10. subprocess.check_call(cmd, shell=True)
  11. sub_debs = glob.glob('tmp/**/*.deb', recursive=True)
  12. assert(len(sub_debs) > 0)
  13. for sub_deb in sub_debs:
  14. subprocess.check_call('rm -rf tmp_sub', shell=True)
  15. print('handle sub_deb: {}'.format(sub_deb))
  16. cmd = 'dpkg-deb -xv {} tmp_sub'.format(sub_deb)
  17. subprocess.check_call(cmd, shell=True)
  18. sub_sub_debs = glob.glob('tmp_sub/**/*.deb', recursive=True)
  19. assert(len(sub_sub_debs) == 0)
  20. if (os.path.isdir('tmp_sub/usr/share/')):
  21. subprocess.check_call('cp -v tmp_sub/usr/share/* output/ -rf', shell=True)
  22. if (os.path.isdir('tmp_sub/usr/local/')):
  23. subprocess.check_call('cp -v tmp_sub/usr/local/* output/ -rf', shell=True)
  24. def main():
  25. parser = argparse.ArgumentParser()
  26. parser.add_argument(
  27. "-s",
  28. "--sbsa_mode",
  29. action="store_true",
  30. help="create cuda sbsa libs, which means use to x86 cross build for aarch64 cuda libs",
  31. )
  32. parser.add_argument(
  33. "-t",
  34. "--target_aarch",
  35. type=str,
  36. choices=['x86-64', 'aarch64'],
  37. help="create libs build for, now support x86-64 and aarch64",
  38. dest="target_aarch",
  39. required=True,
  40. )
  41. parser.add_argument(
  42. "-d",
  43. "--cudnn_deb",
  44. help="cudnn deb package, download from: download from: https://developer.nvidia.com/cudnn-download-survey",
  45. dest="cudnn_deb",
  46. type=str,
  47. required=True,
  48. )
  49. parser.add_argument(
  50. "-r",
  51. "--trt_deb",
  52. help="trt deb package, download from: https://developer.nvidia.com/nvidia-tensorrt-download",
  53. dest="trt_deb",
  54. type=str,
  55. required=True,
  56. )
  57. parser.add_argument(
  58. "-c",
  59. "--cuda_deb",
  60. help="cuda deb package, download from: https://developer.nvidia.com/cuda-downloads",
  61. dest="cuda_deb",
  62. type=str,
  63. required=True,
  64. )
  65. parser.add_argument(
  66. "-a",
  67. "--cuda_aarch64_deb",
  68. help="cuda aarch64 libs package: download from: https://developer.nvidia.com/cuda-downloads",
  69. type=str,
  70. dest="cuda_aarch64_deb",
  71. )
  72. args = parser.parse_args()
  73. if (args.target_aarch == 'x86-64' and args.sbsa_mode):
  74. print('ERROR: sbsa_mode only support target_aarch = \'aarch64\' now')
  75. exit(-1)
  76. if (args.sbsa_mode and not args.cuda_aarch64_deb):
  77. print('ERROR: sbsa_mode need -a/--cuda_aarch64_deb to provide cuda aarch64 libs package')
  78. exit(-1)
  79. if (not os.path.isfile(args.cuda_deb)):
  80. print('ERROR: can not find file:{}'.format(args.cuda_deb))
  81. exit(-1)
  82. if (args.sbsa_mode and not os.path.isfile(args.cuda_aarch64_deb)):
  83. print('ERROR: can not find file:{}'.format(args.cuda_aarch64_deb))
  84. exit(-1)
  85. if (not os.path.isfile(args.cudnn_deb)):
  86. print('ERROR: can not find file:{}'.format(args.cudnn_deb))
  87. exit(-1)
  88. if (not os.path.isfile(args.trt_deb)):
  89. print('ERROR: can not find file:{}'.format(args.trt_deb))
  90. exit(-1)
  91. print("CONFIG SUMMARY: create cuda cmake build libs for {}, is for sbsa_mode: {}".format(args.target_aarch, args.sbsa_mode))
  92. cmd = 'rm -rf output && mkdir output'
  93. subprocess.check_call(cmd, shell=True)
  94. #handle cuda
  95. handle_cuda_libs(args.cuda_deb)
  96. #handle sbsa_mode
  97. if (args.sbsa_mode):
  98. handle_cuda_libs(args.cuda_aarch64_deb)
  99. # check cuda/sbsa_mode valid and handle link
  100. nvcc = glob.glob('./output/*/bin/nvcc', recursive=True)
  101. cuda_version = nvcc[0][9:-9]
  102. print('cuda version: {}'.format(cuda_version))
  103. assert(len(nvcc) == 1)
  104. if (args.sbsa_mode):
  105. subprocess.check_call('file {} | grep {}'.format(nvcc[0], 'x86-64'), shell=True)
  106. remove_x86_64_libs = ['targets/x86_64-linux', 'include', 'lib64']
  107. for remove_lib in remove_x86_64_libs:
  108. subprocess.check_call('rm -rf ./output/{}/{}'.format(cuda_version, remove_lib), shell=True)
  109. #create link for sbsa
  110. cwd = os.getcwd()
  111. os.chdir('output/{}'.format(cuda_version))
  112. cmd = 'ln -s targets/sbsa-linux/include/ include && ln -s targets/sbsa-linux/lib/ lib64'
  113. subprocess.check_call(cmd, shell=True)
  114. #handle libnvrtc.so
  115. readelf_nvrtc = os.popen('readelf -d lib64/stubs/libnvrtc.so | grep SONAME').read().split('\n')[0]
  116. loc = readelf_nvrtc.find('[')
  117. libnvrtc_with_version = readelf_nvrtc[loc+1:-1]
  118. print('libnvrtc_with_version: {}'.format(libnvrtc_with_version))
  119. cmd = 'cp lib64/stubs/libnvrtc.so lib64/{}'.format(libnvrtc_with_version)
  120. subprocess.check_call(cmd, shell=True)
  121. os.chdir(cwd)
  122. else:
  123. subprocess.check_call('file {} | grep {}'.format(nvcc[0], args.target_aarch), shell=True)
  124. # handle cudnn
  125. subprocess.check_call('rm -rf tmp && rm -rf tmp_sub && mkdir tmp', shell=True)
  126. print('\nhandle cuda file from.{}'.format(args.cudnn_deb))
  127. # FIXME: later release cudnn may dir not with cuda, nvidia may fix later!!
  128. cmd = 'tar -xvf {} -C tmp && mv tmp/cuda output/cudnn'.format(args.cudnn_deb)
  129. subprocess.check_call(cmd, shell=True)
  130. cudnn_libs = glob.glob('output/cudnn/lib64/libcudnn.so*')
  131. cudnn_real_libs = []
  132. for lib in cudnn_libs:
  133. if (not os.path.islink(lib)):
  134. cudnn_real_libs.append(lib)
  135. assert(len(cudnn_real_libs) > 0)
  136. for lib in cudnn_real_libs:
  137. subprocess.check_call('file {} | grep {}'.format(lib, args.target_aarch), shell=True)
  138. # handle trt
  139. print('\nhandle cuda file from.{}'.format(args.trt_deb))
  140. cmd = 'tar -xvf {} -C output'.format(args.trt_deb)
  141. subprocess.check_call(cmd, shell=True)
  142. trt_libs = glob.glob('output/TensorRT-*/lib/libnvinfer.so.*')
  143. trt_real_libs = []
  144. for lib in trt_libs:
  145. if (not os.path.islink(lib)):
  146. trt_real_libs.append(lib)
  147. assert(len(trt_real_libs) > 0)
  148. for lib in trt_real_libs:
  149. subprocess.check_call('file {} | grep {}'.format(lib, args.target_aarch), shell=True)
  150. if __name__ == "__main__":
  151. main()