CMakeLists.txt 23.9 KB
Newer Older
1
# Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Przemek Tredak's avatar
Przemek Tredak committed
2
3
#
# See LICENSE for license information.
4

5
cmake_minimum_required(VERSION 3.21)
6

yuguo's avatar
yuguo committed
7
8
9
10
11
12
13
14
15
option(USE_ROCM "Use ROCm" OFF)
option(USE_HIPBLASLT "Use HIPBLASLT" ON)
# Temp unsupport  aottriton\ck backend and Use ROCBLAS
option(USE_ROCBLAS "Use ROCBLAS" OFF)

if(NOT USE_ROCM)
  if(((EXISTS "/opt/dtk/") OR (EXISTS $ENV{ROCM_PATH})) AND NOT (EXISTS "/bin/nvcc"))
    message("hcu detected.")
    set(USE_ROCM ON)
16
  endif()
17
18
endif()

yuguo's avatar
yuguo committed
19
20
21
22
23
24
25
26
27
if (USE_ROCM)
  add_compile_definitions(__HIP_CLANG_ONLY__=1)
  if (NOT USE_HIPBLASLT AND NOT USE_ROCBLAS)
    message(FATAL_ERROR "Need specify at least one GEMM library to use: HIPBLASLT or ROCBLAS")
  endif()
  unset(USE_CUDA)
else()
  set(USE_CUDA TRUE)
endif()
28

29

yuguo's avatar
yuguo committed
30
31
32
33
34
35
36
37
# Language options
if(USE_CUDA)
  set(CMAKE_CXX_STANDARD 17)
  set(CMAKE_CUDA_STANDARD 17)
  set(CMAKE_CUDA_STANDARD_REQUIRED ON)
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_CUDA_FLAGS_DEBUG "${CMAKE_CUDA_FLAGS_DEBUG} -g -G")
  endif()
wenjh's avatar
wenjh committed
38

yuguo's avatar
yuguo committed
39
40
41
  # Hide non-necessary symbols in shared object.
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/libtransformer_engine.version")
  set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/libtransformer_engine.version")
42

yuguo's avatar
yuguo committed
43
44
  # Transformer Engine library
  project(transformer_engine LANGUAGES CUDA CXX)
wenjh's avatar
wenjh committed
45

yuguo's avatar
yuguo committed
46
47
  # CUDA Toolkit
  find_package(CUDAToolkit REQUIRED)
wenjh's avatar
wenjh committed
48
49
  if (CUDAToolkit_VERSION VERSION_LESS 12.1)
    message(FATAL_ERROR "CUDA 12.1+ is required, but found CUDA ${CUDAToolkit_VERSION}")
50
51
  endif()

wenjh's avatar
wenjh committed
52
53
54
55
56
57
58
59
60
  # Process GPU architectures
  if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
    if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL 13.0)
      set(CMAKE_CUDA_ARCHITECTURES 75 80 89 90 100 120)
    elseif (CUDAToolkit_VERSION VERSION_GREATER_EQUAL 12.8)
      set(CMAKE_CUDA_ARCHITECTURES 70 80 89 90 100 120)
    else ()
      set(CMAKE_CUDA_ARCHITECTURES 70 80 89 90)
    endif()
61
62
  endif()

wenjh's avatar
wenjh committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  # Process CMAKE_CUDA_ARCHITECTURES to separate generic and specific architectures
  set(NVTE_GENERIC_ARCHS)
  set(NVTE_SPECIFIC_ARCHS)

  # Check for architecture 100
  list(FIND CMAKE_CUDA_ARCHITECTURES "100" arch_100_index)
  if(NOT arch_100_index EQUAL -1)
    list(REMOVE_ITEM CMAKE_CUDA_ARCHITECTURES "100")
    list(APPEND NVTE_GENERIC_ARCHS "100")
    list(APPEND NVTE_SPECIFIC_ARCHS "100a")
    if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 12.9)
      list(APPEND NVTE_SPECIFIC_ARCHS "103a")
    endif()
  endif()
77

wenjh's avatar
wenjh committed
78
79
80
81
82
83
84
  # Check for architecture 101 (if we see this we are in toolkit <= 12.9)
  list(FIND CMAKE_CUDA_ARCHITECTURES "101" arch_101_index)
  if(NOT arch_101_index EQUAL -1)
    list(REMOVE_ITEM CMAKE_CUDA_ARCHITECTURES "101")
    list(APPEND NVTE_GENERIC_ARCHS "101")
    list(APPEND NVTE_SPECIFIC_ARCHS "101a")
  endif()
85

wenjh's avatar
wenjh committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  # Check for architecture 110 (if we see this we are in toolkit >= 13.0)
  list(FIND CMAKE_CUDA_ARCHITECTURES "110" arch_110_index)
  if(NOT arch_110_index EQUAL -1)
    list(REMOVE_ITEM CMAKE_CUDA_ARCHITECTURES "110")
    list(APPEND NVTE_GENERIC_ARCHS "110")
    list(APPEND NVTE_SPECIFIC_ARCHS "110f")
  endif()

  # Check for architecture 120
  list(FIND CMAKE_CUDA_ARCHITECTURES "120" arch_120_index)
  if(NOT arch_120_index EQUAL -1)
    list(REMOVE_ITEM CMAKE_CUDA_ARCHITECTURES "120")
    list(APPEND NVTE_GENERIC_ARCHS "120")
    if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 12.9)
      list(APPEND NVTE_SPECIFIC_ARCHS "120f")
    else()
      list(APPEND NVTE_SPECIFIC_ARCHS "120a")
    endif()
yuguo's avatar
yuguo committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  endif()
  
  # cuDNN frontend API
  set(CUDNN_FRONTEND_INCLUDE_DIR
      "${CMAKE_CURRENT_SOURCE_DIR}/../../3rdparty/cudnn-frontend/include")
  if(NOT EXISTS "${CUDNN_FRONTEND_INCLUDE_DIR}")
      message(FATAL_ERROR
              "Could not find cuDNN frontend API at ${CUDNN_FRONTEND_INCLUDE_DIR}. "
              "Try running 'git submodule update --init --recursive' "
              "within the Transformer Engine source.")
  endif()
  include(${CMAKE_CURRENT_SOURCE_DIR}/../../3rdparty/cudnn-frontend/cmake/cuDNN.cmake)
else()
  set(CMAKE_CXX_STANDARD 17)
  project(transformer_engine LANGUAGES HIP CXX)
  
  # Disable Asserts In Code (Can't use asserts on HIP stack.)
  add_definitions(-DNDEBUG)
  add_definitions(-DUSE_ROCM)
123
  add_definitions(-DHIP_ENABLE_WARP_SYNC_BUILTINS)
yuguo's avatar
yuguo committed
124
125
126
127
  # Change clang++ to hipcc 
  SET(CMAKE_CXX_COMPILER "${ROCM_PATH}/bin/hipcc")  
  
  if(NOT DEFINED ENV{NVTE_ROCM_ARCH})
wenjh's avatar
wenjh committed
128
    SET(CMAKE_HIP_ARCHITECTURES gfx906;gfx926;gfx928;gfx936;gfx938)
yuguo's avatar
yuguo committed
129
130
131
132
133
134
135
136
137
138
139
  else()
    SET(CMAKE_HIP_ARCHITECTURES $ENV{NVTE_ROCM_ARCH})
  endif()
  
  # build error will be dup-ed parallel-jobs times
  # set(CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS} -parallel-jobs=4")
  if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS} -g")
  endif()
  
  list(APPEND CMAKE_MODULE_PATH "/opt/dtk")
140
endif()
141

yuguo's avatar
yuguo committed
142
143
144
145
146
147
set(message_line "-------------------------------------------------------------")
message("${message_line}")
message(STATUS "USE_ROCM ${USE_ROCM}")
if(USE_ROCM)
  message(STATUS "CMAKE_HIP_ARCHITECTURES: ${CMAKE_HIP_ARCHITECTURES}")
  message(STATUS "USE_HIPBLASLT ${USE_HIPBLASLT} USE_ROCBLAS ${USE_ROCBLAS}")
148
149
endif()

150
151
152
153
154
set(CUTLASS_INCLUDE_DIR
  "${CMAKE_CURRENT_SOURCE_DIR}/../../3rdparty/cutlass/include")
set(CUTLASS_TOOLS_INCLUDE_DIR
  "${CMAKE_CURRENT_SOURCE_DIR}/../../3rdparty/cutlass/tools/util/include")

155
# Python
156
157
find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)

158
# Configure Transformer Engine library
159
include_directories(${PROJECT_SOURCE_DIR}/..)
160
set(transformer_engine_SOURCES)
161
162
163
set(transformer_engine_cpp_sources)
set(transformer_engine_cuda_sources)
set(transformer_engine_cuda_arch_specific_sources)
yuguo's avatar
yuguo committed
164
165

if(USE_CUDA)
wenjh's avatar
wenjh committed
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
  list(APPEND transformer_engine_cpp_sources
      cudnn_utils.cpp
      transformer_engine.cpp
      fused_attn/fused_attn.cpp
      gemm/config.cpp
      normalization/common.cpp
      normalization/layernorm/ln_api.cpp
      normalization/rmsnorm/rmsnorm_api.cpp
      util/cuda_driver.cpp
      util/cuda_nvml.cpp
      util/cuda_runtime.cpp
      util/multi_stream.cpp
      util/rtc.cpp
      comm_gemm_overlap/userbuffers/ipcsocket.cc
      comm_gemm_overlap/userbuffers/userbuffers-host.cpp
      comm_gemm_overlap/comm_gemm_overlap.cpp)

  list(APPEND transformer_engine_cuda_sources
      common.cu
      multi_tensor/adam.cu
      multi_tensor/l2norm.cu
      multi_tensor/scale.cu
      multi_tensor/sgd.cu
      transpose/cast_transpose.cu
      transpose/transpose.cu
      transpose/cast_transpose_fusion.cu
      transpose/transpose_fusion.cu
      transpose/multi_cast_transpose.cu
      transpose/quantize_transpose_vector_blockwise.cu
      transpose/swap_first_dims.cu
      dropout/dropout.cu
      fused_attn/flash_attn.cu
      fused_attn/context_parallel.cu
      fused_attn/kv_cache.cu
      fused_attn/fused_attn_f16_max512_seqlen.cu
      fused_attn/fused_attn_f16_arbitrary_seqlen.cu
      fused_attn/fused_attn_fp8.cu
      fused_attn/utils.cu
      gemm/cublaslt_gemm.cu
wenjh's avatar
wenjh committed
205
      gemm/cublaslt_grouped_gemm.cu
wenjh's avatar
wenjh committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
      normalization/layernorm/ln_bwd_semi_cuda_kernel.cu
      normalization/layernorm/ln_fwd_cuda_kernel.cu
      normalization/rmsnorm/rmsnorm_bwd_semi_cuda_kernel.cu
      normalization/rmsnorm/rmsnorm_fwd_cuda_kernel.cu
      permutation/permutation.cu
      util/padding.cu
      swizzle/swizzle.cu
      swizzle/swizzle_block_scaling.cu
      fused_softmax/scaled_masked_softmax.cu
      fused_softmax/scaled_upper_triang_masked_softmax.cu
      fused_softmax/scaled_aligned_causal_masked_softmax.cu
      fused_rope/fused_rope.cu
      fused_router/fused_moe_aux_loss.cu
      fused_router/fused_score_for_moe_aux_loss.cu
      fused_router/fused_topk_with_score_function.cu
      recipe/current_scaling.cu
      recipe/delayed_scaling.cu
      recipe/fp8_block_scaling.cu
      recipe/nvfp4.cu
      comm_gemm_overlap/userbuffers/userbuffers.cu)

  list(APPEND transformer_engine_cuda_arch_specific_sources
      activation/gelu.cu
wenjh's avatar
wenjh committed
229
      activation/glu.cu
wenjh's avatar
wenjh committed
230
231
      activation/relu.cu
      activation/swiglu.cu
wenjh's avatar
wenjh committed
232
233
234
      cast/cast.cu
      gemm/cutlass_grouped_gemm.cu
      hadamard_transform/group_hadamard_transform.cu
wenjh's avatar
wenjh committed
235
      hadamard_transform/graph_safe_group_hadamard_transform.cu
wenjh's avatar
wenjh committed
236
      hadamard_transform/hadamard_transform.cu
wenjh's avatar
wenjh committed
237
238
239
      hadamard_transform/hadamard_transform_cast_fusion.cu
      hadamard_transform/group_hadamard_transform_cast_fusion.cu
      hadamard_transform/group_row_cast_col_hadamard_transform_cast_fusion.cu
wenjh's avatar
wenjh committed
240
      hadamard_transform/graph_safe_group_row_cast_col_hadamard_transform_cast_fusion.cu
wenjh's avatar
wenjh committed
241
242
243
244
      multi_tensor/compute_scale.cu
      recipe/mxfp8_scaling.cu
      transpose/quantize_transpose_square_blockwise.cu
      transpose/quantize_transpose_vector_blockwise_fp4.cu)
wenjh's avatar
wenjh committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

  # Compiling the files with the worst compilation time first to hopefully overlap
  # better with the faster-compiling cpp files
  list(APPEND transformer_engine_SOURCES ${transformer_engine_cuda_arch_specific_sources}
                                        ${transformer_engine_cuda_sources}
                                        ${transformer_engine_cpp_sources})

  # Set compile options for CUDA sources with generic architectures
  foreach(cuda_source IN LISTS transformer_engine_cuda_sources)
    set(arch_compile_options)
    foreach(arch IN LISTS NVTE_GENERIC_ARCHS)
      list(APPEND arch_compile_options "--generate-code=arch=compute_${arch},code=sm_${arch}")
    endforeach()

    if(arch_compile_options)
      set_property(
        SOURCE ${cuda_source}
        APPEND
        PROPERTY
        COMPILE_OPTIONS ${arch_compile_options}
      )
    endif()
267
268
  endforeach()

wenjh's avatar
wenjh committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
  # Set compile options for CUDA sources with specific architectures
  foreach(cuda_source IN LISTS transformer_engine_cuda_arch_specific_sources)
    set(arch_compile_options)
    foreach(arch IN LISTS NVTE_SPECIFIC_ARCHS)
      list(APPEND arch_compile_options "--generate-code=arch=compute_${arch},code=sm_${arch}")
    endforeach()

    if(arch_compile_options)
      set_property(
        SOURCE ${cuda_source}
        APPEND
        PROPERTY
        COMPILE_OPTIONS ${arch_compile_options}
      )
    endif()
284
285
  endforeach()

wenjh's avatar
wenjh committed
286
  if (NVTE_WITH_CUBLASMP)
wenjh's avatar
wenjh committed
287
  list(APPEND transformer_engine_SOURCES
wenjh's avatar
wenjh committed
288
289
      comm_gemm/comm_gemm.cpp)
  endif()
290

yuguo's avatar
yuguo committed
291
  add_library(transformer_engine SHARED ${transformer_engine_SOURCES})
wenjh's avatar
wenjh committed
292
293
  target_include_directories(transformer_engine PUBLIC
                            "${CMAKE_CURRENT_SOURCE_DIR}/include")
wenjh's avatar
wenjh committed
294

wenjh's avatar
wenjh committed
295
  # Grouped GEMM kernels require SM90a
wenjh's avatar
wenjh committed
296
297
298
299
  set_property(
    SOURCE gemm/cutlass_grouped_gemm.cu
    APPEND
    PROPERTY
wenjh's avatar
wenjh committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
    COMPILE_OPTIONS "--generate-code=arch=compute_90a,code=sm_90a")

  # CUTLASS kernels could cause hang in debug build
  set(CUTLASS_KERNEL_SOURCES
      gemm/cutlass_grouped_gemm.cu
      hadamard_transform/group_hadamard_transform_cast_fusion.cu
      hadamard_transform/group_row_cast_col_hadamard_transform_cast_fusion.cu
      hadamard_transform/hadamard_transform_cast_fusion.cu)
  set_property(
    SOURCE ${CUTLASS_KERNEL_SOURCES}
    APPEND
    PROPERTY
    COMPILE_OPTIONS "-g0;-dopt=on")

  # Configure dependencies
  target_link_libraries(transformer_engine PUBLIC
                        CUDA::cublas
                        CUDA::cudart
                        CUDNN::cudnn_all)

  target_include_directories(transformer_engine PRIVATE
                            ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
  target_include_directories(transformer_engine SYSTEM PRIVATE
                            ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}/cccl)
  target_include_directories(transformer_engine PRIVATE "${CUDNN_FRONTEND_INCLUDE_DIR}")
  target_include_directories(transformer_engine PRIVATE
                            ${CUTLASS_INCLUDE_DIR}
                            ${CUTLASS_TOOLS_INCLUDE_DIR})
yuguo's avatar
yuguo committed
328
else()
wenjh's avatar
wenjh committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
  list(APPEND transformer_engine_cpp_sources
      cudnn_utils.cpp
      transformer_engine.cpp
      gemm/config.cpp
      normalization/common.cpp
      normalization/layernorm/ln_api.cpp
      normalization/rmsnorm/rmsnorm_api.cpp
      util/cuda_driver.cpp
      util/cuda_nvml.cpp
      util/cuda_runtime.cpp
      util/multi_stream.cpp
      util/rtc.cpp
      comm_gemm_overlap/userbuffers/ipcsocket.cc
      comm_gemm_overlap/userbuffers/userbuffers-host.cpp
      comm_gemm_overlap/comm_gemm_overlap.cpp)

  list(APPEND transformer_engine_cuda_sources
      common.cu
      multi_tensor/adam.cu
      multi_tensor/l2norm.cu
      multi_tensor/scale.cu
      multi_tensor/sgd.cu
      transpose/cast_transpose.cu
      transpose/transpose.cu
      transpose/cast_transpose_fusion.cu
      transpose/transpose_fusion.cu
      transpose/multi_cast_transpose.cu
      transpose/quantize_transpose_vector_blockwise.cu
      transpose/swap_first_dims.cu
      dropout/dropout.cu
      fused_attn/flash_attn.cu
      fused_attn/context_parallel.cu
      fused_attn/kv_cache.cu
      fused_attn/utils.cu
      gemm/cublaslt_gemm.cu
wenjh's avatar
wenjh committed
364
      gemm/cublaslt_grouped_gemm.cu
wenjh's avatar
wenjh committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
      gemm/hipblas_gemm.cu
      normalization/layernorm/ln_bwd_semi_cuda_kernel.cu
      normalization/layernorm/ln_fwd_cuda_kernel.cu
      normalization/rmsnorm/rmsnorm_bwd_semi_cuda_kernel.cu
      normalization/rmsnorm/rmsnorm_fwd_cuda_kernel.cu
      permutation/permutation.cu
      util/padding.cu
      swizzle/swizzle.cu
      swizzle/swizzle_block_scaling.cu
      fused_softmax/scaled_masked_softmax.cu
      fused_softmax/scaled_upper_triang_masked_softmax.cu
      fused_softmax/scaled_aligned_causal_masked_softmax.cu
      fused_rope/fused_rope.cu
      fused_router/fused_moe_aux_loss.cu
      fused_router/fused_score_for_moe_aux_loss.cu
      fused_router/fused_topk_with_score_function.cu
      recipe/current_scaling.cu
      recipe/delayed_scaling.cu
      recipe/fp8_block_scaling.cu
      recipe/nvfp4.cu
      comm_gemm_overlap/userbuffers/userbuffers.cu)

  list(APPEND transformer_engine_cuda_arch_specific_sources
      activation/gelu.cu
wenjh's avatar
wenjh committed
389
      activation/glu.cu
wenjh's avatar
wenjh committed
390
391
      activation/relu.cu
      activation/swiglu.cu
wenjh's avatar
wenjh committed
392
393
394
      cast/cast.cu
      multi_tensor/compute_scale.cu
      recipe/mxfp8_scaling.cu
wenjh's avatar
wenjh committed
395
396
397
398
399
400
401
402
      transpose/quantize_transpose_square_blockwise.cu
      transpose/quantize_transpose_vector_blockwise_fp4.cu)

  # Compiling the files with the worst compilation time first to hopefully overlap
  # better with the faster-compiling cpp files
  list(APPEND transformer_engine_SOURCES ${transformer_engine_cuda_arch_specific_sources}
                                        ${transformer_engine_cuda_sources}
                                        ${transformer_engine_cpp_sources})
wenjh's avatar
wenjh committed
403
404
405
406
  if (NVTE_WITH_CUBLASMP)
    list(APPEND transformer_engine_SOURCES
      comm_gemm/comm_gemm.cpp)
  endif()
yuguo's avatar
yuguo committed
407
408
409
410
411
412
413
414
415
416
417
418
419
420
  # process source code files
  message("${message_line}")
  message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")
  message(STATUS "PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")

  set(TE ${CMAKE_CURRENT_SOURCE_DIR}/../..)
  set(THIRDPARTY ${TE}/3rdparty)
  list(APPEND CMAKE_MODULE_PATH "${THIRDPARTY}/hipify_torch/cmake")
  include(Hipify)
  message(STATUS "CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}")

  set(header_include_dir
      ${CMAKE_CURRENT_SOURCE_DIR}/comm_gemm_overlap/userbuffers
      ${CMAKE_CURRENT_SOURCE_DIR}/activation 
421
      ${CMAKE_CURRENT_SOURCE_DIR}/fused_attn
yuguo's avatar
yuguo committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
      ${CMAKE_CURRENT_SOURCE_DIR}/include 
      ${CMAKE_CURRENT_SOURCE_DIR}/transpose
      ${CMAKE_CURRENT_SOURCE_DIR}/util
      ${CMAKE_CURRENT_SOURCE_DIR}/normalization
      ${CMAKE_CURRENT_SOURCE_DIR}/normalization/rmsnorm
      ${CMAKE_CURRENT_SOURCE_DIR}/normalization/layernorm 
      ${CMAKE_CURRENT_SOURCE_DIR})
  message(STATUS "HIPIFY CUDA_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")
  message(STATUS "HIPIFY HEADER_INCLUDE_DIR: ${header_include_dir}")
  hipify(CUDA_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}
      HEADER_INCLUDE_DIR ${header_include_dir}
      IGNORES "*/amd_detail/*"
      CUSTOM_MAP_FILE "${TE}/hipify_custom_map.json"
  )
  get_hipified_list("${transformer_engine_SOURCES}" te_hip_sources)
  message("${message_line}")
  message(STATUS "nvte hipified sources: ${te_hip_sources}")

  add_library(transformer_engine SHARED ${te_hip_sources})
wenjh's avatar
wenjh committed
441
442
  target_include_directories(transformer_engine PUBLIC
                            "${CMAKE_CURRENT_SOURCE_DIR}/include")
wenjh's avatar
wenjh committed
443

yuguo's avatar
yuguo committed
444
  target_include_directories(transformer_engine PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
yuguo's avatar
yuguo committed
445
446
447
448
449
  # Aotriton is currently unsupported 
  set(AotritonAndCk_fused_attn "unsupported")

  find_package(hip)
  list(APPEND transformer_engine_LINKER_LIBS hip::host hip::device roctx64)
yuguo's avatar
yuguo committed
450
451
  find_package(rccl)
  list(APPEND transformer_engine_LINKER_LIBS rccl)
yuguo's avatar
yuguo committed
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  if(USE_HIPBLASLT)
    find_package(hipblaslt)
    find_package(hipblas REQUIRED PATHS ${ROCM_PATH})
    target_compile_definitions(transformer_engine PUBLIC USE_HIPBLASLT)
    list(APPEND transformer_engine_LINKER_LIBS roc::hipblaslt hipblas)
  endif()
  if(USE_ROCBLAS)
    find_package(rocblas)
    target_compile_definitions(transformer_engine PUBLIC USE_ROCBLAS)
    list(APPEND transformer_engine_LINKER_LIBS roc::rocblas)
  endif()
  target_link_libraries(transformer_engine PUBLIC ${transformer_engine_LINKER_LIBS})
endif()

466
467
468
469
470
471
472
473
474
# Compiling Userbuffers with native MPI bootstrapping requires linking against MPI
option(NVTE_UB_WITH_MPI "Bootstrap Userbuffers with MPI" OFF)
if (NVTE_UB_WITH_MPI)
    find_package(MPI REQUIRED)
    target_link_libraries(transformer_engine PUBLIC MPI::MPI_CXX)
    target_include_directories(transformer_engine PRIVATE ${MPI_CXX_INCLUDES})
    target_compile_definitions(transformer_engine PUBLIC NVTE_UB_WITH_MPI)
endif()

475
476
477
478
479
480
481
option(NVTE_ENABLE_NVSHMEM "Compile with NVSHMEM library" OFF)
if (NVTE_ENABLE_NVSHMEM)
    add_subdirectory(nvshmem_api)
    target_link_libraries(transformer_engine PUBLIC nvshmemapi)
    target_include_directories(transformer_engine PUBLIC ${NVSHMEMAPI_INCLUDE_DIR})
endif()

482
483
484
option(NVTE_WITH_CUBLASMP "Use cuBLASMp for tensor parallel GEMMs" OFF)
if (NVTE_WITH_CUBLASMP)
    target_compile_definitions(transformer_engine PRIVATE NVTE_WITH_CUBLASMP)
485
    target_include_directories(transformer_engine PRIVATE ${CUBLASMP_DIR}/include)
486
487
488
489
490
    find_library(CUBLASMP_LIB
                 NAMES cublasmp libcublasmp
                 PATHS ${CUBLASMP_DIR}
                 PATH_SUFFIXES lib
                 REQUIRED)
491
492
    find_library(NCCL_LIB
                 NAMES nccl libnccl
493
494
                 PATH_SUFFIXES lib
                 REQUIRED)
495
  target_link_libraries(transformer_engine PUBLIC ${NCCL_LIB} ${CUBLASMP_LIB})
496
  message(STATUS "Using cuBLASMp at: ${CUBLASMP_DIR}")
497
498
endif()

yuguo's avatar
yuguo committed
499
500
501
502
if (USE_CUDA)
  # Hack to enable dynamic loading in cuDNN frontend
  target_compile_definitions(transformer_engine PUBLIC NV_CUDNN_FRONTEND_USE_DYNAMIC_LOADING)
endif()
503
504

# Helper functions to make header files with C++ strings
Tim Moon's avatar
Tim Moon committed
505
506
507
508
509
510
511
512
513
514
515
function(make_string_header STRING STRING_NAME)
    configure_file(util/string_header.h.in
                   "string_headers/${STRING_NAME}.h"
                   @ONLY)
endfunction()
function(make_string_header_from_file file_ STRING_NAME)
    file(READ "${file_}" STRING)
    configure_file(util/string_header.h.in
                   "string_headers/${STRING_NAME}.h"
                   @ONLY)
endfunction()
516
517

# Header files with C++ strings
yuguo's avatar
yuguo committed
518
519
520
521
522
523
524
525
526
527
if(USE_CUDA)
  list(GET CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES 0 cuda_include_path)
  make_string_header("${cuda_include_path}"
                     string_path_cuda_include)
  make_string_header_from_file(transpose/rtc/cast_transpose_fusion.cu
                               string_code_transpose_rtc_cast_transpose_fusion_cu)
  make_string_header_from_file(transpose/rtc/cast_transpose.cu
                               string_code_transpose_rtc_cast_transpose_cu)
  make_string_header_from_file(transpose/rtc/transpose.cu
                               string_code_transpose_rtc_transpose_cu)
528
529
  make_string_header_from_file(transpose/rtc/swap_first_dims.cu
                               string_code_transpose_rtc_swap_first_dims_cu)                             
yuguo's avatar
yuguo committed
530
531
532
533
534
535
536
537
538
539
540
  make_string_header_from_file(utils.cuh
                               string_code_utils_cuh)
else()
  make_string_header_from_file(utils_hip.cuh
                               string_code_utils_cuh)
  make_string_header_from_file(transpose/rtc/cast_transpose_fusion.hip
                              string_code_transpose_rtc_cast_transpose_fusion_cu)
  make_string_header_from_file(transpose/rtc/cast_transpose.hip
                              string_code_transpose_rtc_cast_transpose_cu)
  make_string_header_from_file(transpose/rtc/transpose.hip
                              string_code_transpose_rtc_transpose_cu)
541
  make_string_header_from_file(transpose/rtc/swap_first_dims.hip
542
                              string_code_transpose_rtc_swap_first_dims_cu)
yuguo's avatar
yuguo committed
543
544
545
endif()


546
547
make_string_header_from_file(util/math.h
                             string_code_util_math_h)
Tim Moon's avatar
Tim Moon committed
548
549
550
target_include_directories(transformer_engine PRIVATE
                           "${CMAKE_CURRENT_BINARY_DIR}/string_headers")

551
# Compiler options
552
553
554
555
556
557
558
559
560
561
562
563
564
set(nvte_sources_with_fast_math)
list(APPEND nvte_sources_with_fast_math fused_softmax/scaled_masked_softmax.cu
                                        fused_softmax/scaled_upper_triang_masked_softmax.cu
                                        fused_softmax/scaled_aligned_causal_masked_softmax.cu
                                        multi_tensor/adam.cu
                                        multi_tensor/compute_scale.cu
                                        multi_tensor/l2norm.cu
                                        multi_tensor/scale.cu
                                        multi_tensor/sgd.cu
                                        fused_attn/flash_attn.cu
                                        fused_attn/context_parallel.cu
                                        fused_attn/kv_cache.cu)

565
566
option(NVTE_BUILD_ACTIVATION_WITH_FAST_MATH "Compile activation kernels with --use_fast_math option" OFF)
if (NVTE_BUILD_ACTIVATION_WITH_FAST_MATH)
567
  list(APPEND nvte_sources_with_fast_math activation/gelu.cu
Kim, Jin (Jay@SKT)'s avatar
Kim, Jin (Jay@SKT) committed
568
                                          activation/glu.cu
569
                                          activation/relu.cu
570
                                          activation/swiglu.cu)
571
endif()
yuguo's avatar
yuguo committed
572
if(USE_CUDA)
wenjh's avatar
wenjh committed
573
574
575
576
577
578
579
  foreach(cuda_source IN LISTS nvte_sources_with_fast_math)
    set_property(
      SOURCE ${cuda_source}
      APPEND
      PROPERTY
      COMPILE_OPTIONS "--use_fast_math")
  endforeach()
580

yuguo's avatar
yuguo committed
581
582
583
  set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-relaxed-constexpr")
  set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -O3")
else()
584
585
586
587
588
589
590
591
592
593
594
595
  option(NVTE_BUILD_SUPPRESS_UNUSED_WARNING "Suppress unused* wanings while build" ON)
  option(NVTE_BUILD_SUPPRESS_RETURN_TYPE_WARNING "Suppress return type waning while build" OFF)
  option(NVTE_BUILD_SUPPRESS_SIGN_COMPARE_WARNING "Suppress sign compare waning while build" OFF)
  if(NVTE_BUILD_SUPPRESS_UNUSED_WARNING)
    set(CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS} -Wno-unused-result -Wno-unused-function -Wno-unused-private-field -Wno-unused-variable")
  endif()
  if(NVTE_BUILD_SUPPRESS_RETURN_TYPE_WARNING)
    set(CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS} -Wno-return-type")
  endif()
  if(NVTE_BUILD_SUPPRESS_SIGN_COMPARE_WARNING)
    set(CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS} -Wno-sign-compare")
  endif()
yuguo's avatar
yuguo committed
596
597
598
599
  set(CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS} -O3")
  set(HIP_HCC_FLAGS "${CMAKE_HIP_FLAGS} -mavx2 -mf16c -mfma -std=c++17")
  # Ask hcc to generate device code during compilation so we can use
  # host linker to link.
yuguo's avatar
yuguo committed
600
  set(HIP_HCC_FLAGS "${HIP_HCC_FLAGS} -fno-gpu-rdc -w")
yuguo's avatar
yuguo committed
601
602
603
604
605
606
  foreach(rocm_arch ${CMAKE_HIP_ARCHITECTURES})
    # if CMAKE_CXX_FLAGS has --offload-arch set already, better to rm first
    set(HIP_HCC_FLAGS "${HIP_HCC_FLAGS} --offload-arch=${rocm_arch}")
  endforeach()
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${HIP_HCC_FLAGS}")
endif()
Tim Moon's avatar
Tim Moon committed
607

608
# Number of parallel build jobs
609
610
611
612
if($ENV{MAX_JOBS})
  set(BUILD_JOBS_STR $ENV{MAX_JOBS})
elseif($ENV{NVTE_BUILD_MAX_JOBS})
  set(BUILD_JOBS_STR $ENV{NVTE_BUILD_MAX_JOBS})
613
614
615
616
617
618
619
620
621
622
623
624
625
else()
  set(BUILD_JOBS_STR "max")
endif()
message(STATUS "Parallel build jobs: ${BUILD_JOBS_STR}")

# Number of threads per parallel build job
set(BUILD_THREADS_PER_JOB $ENV{NVTE_BUILD_THREADS_PER_JOB})
if (NOT BUILD_THREADS_PER_JOB)
  set(BUILD_THREADS_PER_JOB 1)
endif()
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --threads ${BUILD_THREADS_PER_JOB}")
message(STATUS "Threads per parallel build job: ${BUILD_THREADS_PER_JOB}")

Tim Moon's avatar
Tim Moon committed
626
627
# Install library
install(TARGETS transformer_engine DESTINATION .)