"Fossies" - the Fresh Open Source Software Archive

Member "pytorch-1.8.2/tools/amd_build/build_amd.py" (23 Jul 2021, 5041 Bytes) of package /linux/misc/pytorch-1.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "build_amd.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.12.1_vs_1.13.0.

    1 #!/usr/bin/env python
    2 
    3 
    4 import os
    5 import argparse
    6 import sys
    7 sys.path.append(os.path.realpath(os.path.join(
    8     __file__,
    9     os.path.pardir,
   10     os.path.pardir,
   11     os.path.pardir,
   12     'torch',
   13     'utils')))
   14 
   15 from hipify import hipify_python
   16 
   17 parser = argparse.ArgumentParser(description='Top-level script for HIPifying, filling in most common parameters')
   18 parser.add_argument(
   19     '--out-of-place-only',
   20     action='store_true',
   21     help="Whether to only run hipify out-of-place on source files")
   22 
   23 parser.add_argument(
   24     '--project-directory',
   25     type=str,
   26     default='',
   27     help="The root of the project.",
   28     required=False)
   29 
   30 parser.add_argument(
   31     '--output-directory',
   32     type=str,
   33     default='',
   34     help="The directory to store the hipified project",
   35     required=False)
   36 
   37 parser.add_argument(
   38     '--extra-include-dir',
   39     type=str,
   40     default=[],
   41     nargs='+',
   42     help="The list of extra directories in caffe2 to hipify",
   43     required=False)
   44 
   45 args = parser.parse_args()
   46 
   47 amd_build_dir = os.path.dirname(os.path.realpath(__file__))
   48 proj_dir = os.path.join(os.path.dirname(os.path.dirname(amd_build_dir)))
   49 
   50 if args.project_directory:
   51     proj_dir = args.project_directory
   52 
   53 out_dir = proj_dir
   54 if args.output_directory:
   55     out_dir = args.output_directory
   56 
   57 includes = [
   58     "caffe2/operators/*",
   59     "caffe2/sgd/*",
   60     "caffe2/image/*",
   61     "caffe2/transforms/*",
   62     "caffe2/video/*",
   63     "caffe2/distributed/*",
   64     "caffe2/queue/*",
   65     "caffe2/contrib/aten/*",
   66     "binaries/*",
   67     "caffe2/**/*_test*",
   68     "caffe2/core/*",
   69     "caffe2/db/*",
   70     "caffe2/utils/*",
   71     "caffe2/contrib/gloo/*",
   72     "caffe2/contrib/nccl/*",
   73     "c10/cuda/*",
   74     "c10/cuda/test/CMakeLists.txt",
   75     "modules/*",
   76     # PyTorch paths
   77     # Keep this synchronized with is_pytorch_file in hipify_python.py
   78     "aten/src/ATen/cuda/*",
   79     "aten/src/ATen/native/cuda/*",
   80     "aten/src/ATen/native/cudnn/*",
   81     "aten/src/ATen/native/sparse/cuda/*",
   82     "aten/src/ATen/native/quantized/cuda/*",
   83     "aten/src/THC/*",
   84     "aten/src/THCUNN/*",
   85     "aten/src/ATen/test/*",
   86     # CMakeLists.txt isn't processed by default, but there are a few
   87     # we do want to handle, so explicitly specify them
   88     "aten/src/THC/CMakeLists.txt",
   89     "aten/src/THCUNN/CMakeLists.txt",
   90     "torch/*",
   91     "tools/autograd/templates/python_variable_methods.cpp",
   92 ]
   93 
   94 for new_dir in args.extra_include_dir:
   95     abs_new_dir = os.path.join(proj_dir, new_dir)
   96     if os.path.exists(abs_new_dir):
   97         new_dir = os.path.join(new_dir, '**/*')
   98         includes.append(new_dir)
   99 
  100 ignores = [
  101     "caffe2/operators/depthwise_3x3_conv_op_cudnn.cu",
  102     "caffe2/operators/pool_op_cudnn.cu",
  103     '*/hip/*',
  104     # These files are compatible with both cuda and hip
  105     "aten/src/ATen/core/*",
  106     # generated files we shouldn't frob
  107     "torch/lib/tmp_install/*",
  108     "torch/include/*",
  109 ]
  110 
  111 # Check if the compiler is hip-clang.
  112 def is_hip_clang():
  113     try:
  114         hip_path = os.getenv('HIP_PATH', '/opt/rocm/hip')
  115         return 'HIP_COMPILER=clang' in open(hip_path + '/lib/.hipInfo').read()
  116     except IOError:
  117         return False
  118 
  119 # TODO Remove once gloo submodule is recent enough to contain upstream fix.
  120 if is_hip_clang():
  121     gloo_cmake_file = "third_party/gloo/cmake/Hip.cmake"
  122     do_write = False
  123     with open(gloo_cmake_file, "r") as sources:
  124         lines = sources.readlines()
  125     newlines = [line.replace(' hip_hcc ', ' amdhip64 ') for line in lines]
  126     if lines == newlines:
  127         print("%s skipped" % gloo_cmake_file)
  128     else:
  129         with open(gloo_cmake_file, "w") as sources:
  130             for line in newlines:
  131                 sources.write(line)
  132         print("%s updated" % gloo_cmake_file)
  133 
  134 gloo_cmake_file = "third_party/gloo/cmake/Modules/Findrccl.cmake"
  135 if os.path.exists(gloo_cmake_file):
  136     do_write = False
  137     with open(gloo_cmake_file, "r") as sources:
  138         lines = sources.readlines()
  139     newlines = [line.replace('RCCL_LIBRARY', 'RCCL_LIBRARY_PATH') for line in lines]
  140     if lines == newlines:
  141         print("%s skipped" % gloo_cmake_file)
  142     else:
  143         with open(gloo_cmake_file, "w") as sources:
  144             for line in newlines:
  145                 sources.write(line)
  146         print("%s updated" % gloo_cmake_file)
  147 
  148 # TODO Remove once gloo submodule is recent enough to contain upstream fix.
  149 if is_hip_clang():
  150     gloo_cmake_file = "third_party/gloo/cmake/Dependencies.cmake"
  151     do_write = False
  152     with open(gloo_cmake_file, "r") as sources:
  153         lines = sources.readlines()
  154     newlines = [line.replace('HIP_HCC_FLAGS', 'HIP_CLANG_FLAGS') for line in lines]
  155     if lines == newlines:
  156         print("%s skipped" % gloo_cmake_file)
  157     else:
  158         with open(gloo_cmake_file, "w") as sources:
  159             for line in newlines:
  160                 sources.write(line)
  161         print("%s updated" % gloo_cmake_file)
  162 
  163 hipify_python.hipify(
  164     project_directory=proj_dir,
  165     output_directory=out_dir,
  166     includes=includes,
  167     ignores=ignores,
  168     out_of_place_only=args.out_of_place_only,
  169     hip_clang_launch=is_hip_clang())