forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_amd.py
More file actions
executable file
·316 lines (274 loc) · 12.3 KB
/
build_amd.py
File metadata and controls
executable file
·316 lines (274 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python3
import argparse
import os
import sys
from pathlib import Path
# NOTE: `tools/amd_build/build_amd.py` could be a symlink.
# The behavior of `symlink / '..'` is different from `symlink.parent`.
# Use `pardir` three times rather than using `path.parents[2]`.
REPO_ROOT = (
Path(__file__).absolute() / os.path.pardir / os.path.pardir / os.path.pardir
).resolve()
sys.path.append(str(REPO_ROOT / "torch" / "utils"))
from hipify import hipify_python # type: ignore[import]
parser = argparse.ArgumentParser(
description="Top-level script for HIPifying, filling in most common parameters"
)
parser.add_argument(
"--out-of-place-only",
action="store_true",
help="Whether to only run hipify out-of-place on source files",
)
parser.add_argument(
"--project-directory",
type=str,
default="",
help="The root of the project.",
required=False,
)
parser.add_argument(
"--output-directory",
type=str,
default="",
help="The directory to store the hipified project",
required=False,
)
parser.add_argument(
"--extra-include-dir",
type=str,
default=[],
nargs="+",
help="The list of extra directories in caffe2 to hipify",
required=False,
)
args = parser.parse_args()
# NOTE: `tools/amd_build/build_amd.py` could be a symlink.
amd_build_dir = os.path.dirname(os.path.realpath(__file__))
proj_dir = os.path.dirname(os.path.dirname(amd_build_dir))
if args.project_directory:
proj_dir = args.project_directory
out_dir = proj_dir
if args.output_directory:
out_dir = args.output_directory
includes = [
"caffe2/operators/*",
"caffe2/sgd/*",
"caffe2/image/*",
"caffe2/transforms/*",
"caffe2/video/*",
"caffe2/distributed/*",
"caffe2/queue/*",
"caffe2/contrib/aten/*",
"binaries/*",
"caffe2/**/*_test*",
"caffe2/core/*",
"caffe2/db/*",
"caffe2/utils/*",
"caffe2/contrib/gloo/*",
"caffe2/contrib/nccl/*",
"c10/cuda/*",
"c10/cuda/test/CMakeLists.txt",
"modules/*",
"third_party/nvfuser/*",
# PyTorch paths
# Keep this synchronized with is_pytorch_file in hipify_python.py
"aten/src/ATen/cuda/*",
"aten/src/ATen/native/cuda/*",
"aten/src/ATen/native/cudnn/*",
"aten/src/ATen/native/quantized/cudnn/*",
"aten/src/ATen/native/nested/cuda/*",
"aten/src/ATen/native/sparse/cuda/*",
"aten/src/ATen/native/quantized/cuda/*",
"aten/src/ATen/native/transformers/cuda/attention_backward.cu",
"aten/src/ATen/native/transformers/cuda/attention.cu",
"aten/src/ATen/native/transformers/cuda/sdp_utils.cpp",
"aten/src/ATen/native/transformers/cuda/sdp_utils.h",
"aten/src/ATen/native/transformers/cuda/mem_eff_attention/debug_utils.h",
"aten/src/ATen/native/transformers/cuda/mem_eff_attention/gemm_kernel_utils.h",
"aten/src/ATen/native/transformers/cuda/mem_eff_attention/pytorch_utils.h",
"aten/src/THC/*",
"aten/src/ATen/test/*",
# CMakeLists.txt isn't processed by default, but there are a few
# we do want to handle, so explicitly specify them
"aten/src/THC/CMakeLists.txt",
"torch/*",
"tools/autograd/templates/python_variable_methods.cpp",
"torch/csrc/stable/*",
]
includes = [os.path.join(proj_dir, include) for include in includes]
for new_dir in args.extra_include_dir:
abs_new_dir = os.path.join(proj_dir, new_dir)
if os.path.exists(abs_new_dir):
abs_new_dir = os.path.join(abs_new_dir, "**/*")
includes.append(abs_new_dir)
ignores = [
"caffe2/operators/depthwise_3x3_conv_op_cudnn.cu",
"caffe2/operators/pool_op_cudnn.cu",
"*/hip/*",
# These files are compatible with both cuda and hip
"aten/src/ATen/core/*",
# Correct path to generate HIPConfig.h:
# CUDAConfig.h.in -> (amd_build) HIPConfig.h.in -> (cmake) HIPConfig.h
"aten/src/ATen/cuda/CUDAConfig.h",
"third_party/nvfuser/csrc/codegen.cpp",
"third_party/nvfuser/runtime/block_reduction.cu",
"third_party/nvfuser/runtime/block_sync_atomic.cu",
"third_party/nvfuser/runtime/block_sync_default_rocm.cu",
"third_party/nvfuser/runtime/broadcast.cu",
"third_party/nvfuser/runtime/grid_reduction.cu",
"third_party/nvfuser/runtime/helpers.cu",
"torch/csrc/jit/codegen/fuser/cuda/resource_strings.h",
"torch/csrc/jit/tensorexpr/ir_printer.cpp",
"torch/csrc/jit/ir/ir.h",
# generated files we shouldn't frob
"torch/lib/tmp_install/*",
"torch/include/*",
]
ignores = [os.path.join(proj_dir, ignore) for ignore in ignores]
# Check if the compiler is hip-clang.
#
# This used to be a useful function but now we can safely always assume hip-clang.
# Leaving the function here avoids bc-linter errors.
def is_hip_clang() -> bool:
return True
# TODO Remove once the following submodules are updated
hip_platform_files = [
"third_party/fbgemm/fbgemm_gpu/CMakeLists.txt",
"third_party/fbgemm/fbgemm_gpu/cmake/Hip.cmake",
"third_party/fbgemm/fbgemm_gpu/codegen/embedding_backward_dense_host.cpp",
"third_party/fbgemm/fbgemm_gpu/codegen/embedding_backward_split_host_template.cpp",
"third_party/fbgemm/fbgemm_gpu/codegen/embedding_backward_split_template.cu",
"third_party/fbgemm/fbgemm_gpu/codegen/embedding_forward_quantized_split_lookup.cu",
"third_party/fbgemm/fbgemm_gpu/include/fbgemm_gpu/utils/cuda_prelude.cuh",
"third_party/fbgemm/fbgemm_gpu/include/fbgemm_gpu/utils/stochastic_rounding.cuh",
"third_party/fbgemm/fbgemm_gpu/include/fbgemm_gpu/utils/vec4.cuh",
"third_party/fbgemm/fbgemm_gpu/include/fbgemm_gpu/utils/weight_row.cuh",
"third_party/fbgemm/fbgemm_gpu/include/fbgemm_gpu/sparse_ops.cuh",
"third_party/fbgemm/fbgemm_gpu/src/jagged_tensor_ops.cu",
"third_party/fbgemm/fbgemm_gpu/src/quantize_ops.cu",
"third_party/fbgemm/fbgemm_gpu/src/sparse_ops.cu",
"third_party/fbgemm/fbgemm_gpu/src/split_embeddings_cache_cuda.cu",
"third_party/fbgemm/fbgemm_gpu/src/topology_utils.cpp",
"third_party/fbgemm/src/EmbeddingSpMDM.cc",
"third_party/gloo/cmake/Dependencies.cmake",
"third_party/gloo/gloo/cuda.cu",
"third_party/kineto/libkineto/CMakeLists.txt",
"third_party/nvfuser/CMakeLists.txt",
"third_party/tensorpipe/cmake/Hip.cmake",
]
def remove_hcc(line: str) -> str:
line = line.replace("HIP_PLATFORM_HCC", "HIP_PLATFORM_AMD")
line = line.replace("HIP_HCC_FLAGS", "HIP_CLANG_FLAGS")
return line
for hip_platform_file in hip_platform_files:
do_write = False
if os.path.exists(hip_platform_file):
with open(hip_platform_file) as sources:
lines = sources.readlines()
newlines = [remove_hcc(line) for line in lines]
if lines == newlines:
print(f"{hip_platform_file} skipped")
else:
with open(hip_platform_file, "w") as sources:
for line in newlines:
sources.write(line)
print(f"{hip_platform_file} updated")
# NOTE: MSLK sources needing hipify
# MSLK is its own project with its own build system. pytorch uses mslk as
# a submodule to acquire some gpu source files but compiles only those sources
# instead of using mslk's own build system. One of the source files refers
# to a header file that is the result of running hipify, but mslk uses
# slightly different hipify settings than pytorch. mslk normally hipifies
# and renames tuning_cache.cuh to tuning_cache_hip.cuh, but pytorch's settings
# for hipify puts it into its own 'hip' directory. After hipify runs below with
# the added mslk file, we move it to its expected location.
# NOTE: Internal meta builds (using buck) don't need this step, so conditionally disable it
buck_build = os.environ.get("FBCODE_BUILD_TOOL", "") == "buck"
extra_files = [
"torch/_inductor/codegen/cuda/device_op_overrides.py",
"torch/_inductor/codegen/cpp_wrapper_cpu.py",
"torch/_inductor/codegen/cpp_wrapper_gpu.py",
"torch/_inductor/codegen/wrapper.py",
]
mslk_dir = REPO_ROOT / "third_party/mslk/include/mslk/utils/"
if not buck_build:
mslk_original = mslk_dir / "tuning_cache.cuh"
extra_files.append(mslk_original.as_posix())
# TODO Remove once the following submodules are updated to use hipify v2
hipify_v1_to_v2_files = [
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/gemm/ck_extensions.hip",
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/ck_extensions/bf16_grouped/bf16_grouped_gemm.hip",
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/ck_extensions/bf16_grouped/kernels/bf16_grouped_common.h",
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/ck_extensions/ck_utility.hip",
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/ck_extensions/fp8_blockwise_gemm.hip",
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/ck_extensions/fp8_rowwise_batched/kernels/fp8_rowwise_batched_common.h",
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/ck_extensions/fp8_rowwise_grouped/fp8_rowwise_grouped_gemm.hip",
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/ck_extensions/fp8_rowwise_grouped/kernels/fp8_rowwise_grouped_common.h",
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/ck_extensions/fp8_rowwise/kernels/fp8_rowwise_common.h",
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/ck_extensions/fp8_rowwise_preshuffle/kernels/fp8_rowwise_preshuffle_common.h",
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/ck_extensions/fp8_tensorwise_gemm.hip",
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/ck_extensions/fused_moe/fused_moe_kernel.hip",
"third_party/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/common/include/fbgemm_gpu/quantize/tuning_cache.hpp",
"third_party/mslk/csrc/moe/ck_extensions/fused_moe_kernel.hip",
"third_party/mslk/csrc/gemm/ck/bf16_grouped/bf16_grouped_gemm.hip",
"third_party/mslk/csrc/gemm/ck/bf16_grouped/kernels/bf16_grouped_common.h",
"third_party/mslk/csrc/gemm/ck/fp8_rowwise/kernels/fp8_rowwise_common.h",
"third_party/mslk/csrc/gemm/ck/fp8_rowwise_grouped/kernels/fp8_rowwise_grouped_common.h",
"third_party/mslk/csrc/gemm/ck/fp8_rowwise_grouped/fp8_rowwise_grouped_gemm.hip",
"third_party/mslk/csrc/gemm/ck/fp8_rowwise_batched/kernels/fp8_rowwise_batched_common.h",
"third_party/mslk/csrc/gemm/ck/fp8_rowwise_preshuffle/kernels/fp8_rowwise_preshuffle_common.h",
"third_party/mslk/csrc/gemm/ck/fp8_tensorwise_gemm.hip",
"third_party/mslk/csrc/gemm/ck/fp8_blockwise_gemm.hip",
]
def hipify_v1_to_v2(line: str) -> str:
line = line.replace("hip::HIPStreamMasqueradingAsCUDA", "cuda::CUDAStream")
line = line.replace(
"hip::HIPStreamGuardMasqueradingAsCUDA", "cuda::CUDAStreamGuard"
)
line = line.replace(
"hip::getStreamFromPoolMasqueradingAsCUDA", "cuda::getStreamFromPool"
)
line = line.replace("getCurrentHIPStream", "getCurrentCUDAStream")
return line
for hipify_v1_to_v2_file in hipify_v1_to_v2_files:
do_write = False
if os.path.exists(hipify_v1_to_v2_file):
with open(hipify_v1_to_v2_file) as sources:
lines = sources.readlines()
newlines = [hipify_v1_to_v2(line) for line in lines]
if lines == newlines:
print(f"{hipify_v1_to_v2_file} skipped")
else:
with open(hipify_v1_to_v2_file, "w") as sources:
for line in newlines:
sources.write(line)
print(f"{hipify_v1_to_v2_file} updated")
hipify_python.hipify(
project_directory=proj_dir,
output_directory=out_dir,
includes=includes,
ignores=ignores,
extra_files=extra_files,
out_of_place_only=args.out_of_place_only,
hip_clang_launch=is_hip_clang(),
)
if not buck_build:
mslk_move_src = mslk_dir / "hip/tuning_cache.cuh"
mslk_move_dst = mslk_dir / "tuning_cache_hip.cuh"
# only update the file if it changes or doesn't exist
do_write = True
src_lines = None
with open(mslk_move_src) as src:
src_lines = src.readlines()
if os.path.exists(mslk_move_dst):
dst_lines = None
with open(mslk_move_dst) as dst:
dst_lines = dst.readlines()
if src_lines == dst_lines:
print(f"{mslk_move_dst} skipped")
do_write = False
if do_write:
with open(mslk_move_dst, "w") as dst:
for line in src_lines:
dst.write(line)
print(f"{mslk_move_dst} updated")