Caffe2 - Python API
A deep learning, cross platform ML framework
build_amd.py
1 #!/usr/bin/env python
2 
3 from __future__ import absolute_import, division, print_function
4 import os
5 import sys
6 import subprocess
7 import argparse
8 from functools import reduce
9 from itertools import chain
10 
11 from pyHIPIFY import hipify_python
12 
13 parser = argparse.ArgumentParser(description='Top-level script for HIPifying, filling in most common parameters')
14 parser.add_argument(
15  '--out-of-place-only',
16  action='store_true',
17  help="Whether to only run hipify out-of-place on source files")
18 
19 parser.add_argument(
20  '--project-directory',
21  type=str,
22  default='',
23  help="The root of the project.",
24  required=False)
25 
26 parser.add_argument(
27  '--output-directory',
28  type=str,
29  default='',
30  help="The Directory to Store the Hipified Project",
31  required=False)
32 
33 # Hipify using HIP-Clang launch.
34 parser.add_argument(
35  '--hip-clang-launch',
36  action='store_true',
37  help=argparse.SUPPRESS)
38 
39 args = parser.parse_args()
40 
41 amd_build_dir = os.path.dirname(os.path.realpath(__file__))
42 proj_dir = os.path.join(os.path.dirname(os.path.dirname(amd_build_dir)))
43 
44 if args.project_directory:
45  proj_dir = args.project_directory
46 
47 out_dir = proj_dir
48 if args.output_directory:
49  out_dir = args.output_directory
50 
51 includes = [
52  "caffe2/operators/*",
53  "caffe2/sgd/*",
54  "caffe2/image/*",
55  "caffe2/transforms/*",
56  "caffe2/video/*",
57  "caffe2/distributed/*",
58  "caffe2/queue/*",
59  "binaries/*",
60  "caffe2/**/*_test*",
61  "caffe2/core/*",
62  "caffe2/db/*",
63  "caffe2/utils/*",
64  "c10/cuda/*",
65  "c10/cuda/test/CMakeLists.txt",
66  "modules/*",
67  # PyTorch paths
68  # Keep this synchronized with is_pytorch_file in hipify_python.py
69  "aten/src/ATen/cuda/*",
70  "aten/src/ATen/native/cuda/*",
71  "aten/src/ATen/native/cudnn/*",
72  "aten/src/ATen/native/sparse/cuda/*",
73  "aten/src/THC/*",
74  "aten/src/THCUNN/*",
75  "aten/src/ATen/test/*",
76  # CMakeLists.txt isn't processed by default, but there are a few
77  # we do want to handle, so explicitly specify them
78  "aten/src/THC/CMakeLists.txt",
79  "aten/src/THCUNN/CMakeLists.txt",
80  "torch/*",
81  "tools/autograd/templates/python_variable_methods.cpp",
82 ]
83 
84 ignores = [
85  "caffe2/operators/depthwise_3x3_conv_op_cudnn.cu",
86  "caffe2/operators/pool_op_cudnn.cu",
87  '*/hip/*',
88  # These files are compatible with both cuda and hip
89  "aten/src/ATen/core/*",
90  "torch/csrc/autograd/engine.cpp",
91  # generated files we shouldn't frob
92  "torch/lib/tmp_install/*",
93  "torch/include/*",
94 ]
95 
96 json_settings = os.path.join(amd_build_dir, "disabled_features.json")
97 
98 if not args.out_of_place_only:
99  # Apply patch files in place (PyTorch only)
100  patch_folder = os.path.join(amd_build_dir, "patches")
101  for filename in os.listdir(os.path.join(amd_build_dir, "patches")):
102  subprocess.Popen(["git", "apply", os.path.join(patch_folder, filename)], cwd=proj_dir)
103 
104  # Make various replacements inside AMD_BUILD/torch directory
105  ignore_files = [
106  # These files use nvrtc, hip doesn't have equivalent
107  "csrc/autograd/profiler.h",
108  "csrc/autograd/profiler.cpp",
109  # These files are compatible with both cuda and hip
110  "csrc/autograd/engine.cpp"
111  ]
112  paths = ("torch", "tools")
113  for root, _directories, files in chain.from_iterable(os.walk(path) for path in paths):
114  for filename in files:
115  if filename.endswith(".cpp") or filename.endswith(".h"):
116  source = os.path.join(root, filename)
117  # Disabled files
118  if reduce(lambda result, exclude: source.endswith(exclude) or result, ignore_files, False):
119  continue
120  # Update contents.
121  with open(source, "r+") as f:
122  contents = f.read()
123  contents = contents.replace("USE_CUDA", "USE_ROCM")
124  contents = contents.replace("CUDA_VERSION", "0")
125  f.seek(0)
126  f.write(contents)
127  f.truncate()
128  f.flush()
129  os.fsync(f)
130 
131 hipify_python.hipify(
132  project_directory=proj_dir,
133  output_directory=out_dir,
134  includes=includes,
135  ignores=ignores,
136  out_of_place_only=args.out_of_place_only,
137  json_settings=json_settings,
138  hip_clang_launch=args.hip_clang_launch)