Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build_tools/jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def setup_jax_extension(
sources = all_files_in_dir(extensions_dir, name_extension="cpp")

# Header files
include_dirs = get_cuda_include_dirs()
include_dirs = list(get_cuda_include_dirs())
if (discovered_nccl_include_path := nccl_include_path()) is not None:
include_dirs.append(discovered_nccl_include_path)
include_dirs.append(cudnn_frontend_include_path())
Expand Down
22 changes: 21 additions & 1 deletion build_tools/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""PyTorch related extensions."""

import os
import sysconfig
from pathlib import Path
from importlib import metadata

Expand All @@ -16,6 +17,7 @@
get_cuda_include_dirs,
debug_build_enabled,
get_bolt_build_flags,
get_warning_flags,
nccl_ep_enabled,
setup_mpi_flags,
)
Expand Down Expand Up @@ -58,7 +60,7 @@ def setup_pytorch_extension(
sources = all_files_in_dir(Path(csrc_source_files), name_extension="cpp")

# Header files
include_dirs = get_cuda_include_dirs()
include_dirs = list(get_cuda_include_dirs())
include_dirs.extend(
[
common_header_files,
Expand All @@ -79,6 +81,18 @@ def setup_pytorch_extension(
bolt_cxx_flags, linker_flags = get_bolt_build_flags()
cxx_flags.extend(bolt_cxx_flags)

# Compiler warnings. Third-party headers (PyTorch, CUDA, Python, and MPI or
# NVSHMEM when enabled) are included as system headers, so warnings are only
# reported for TE's own sources.
from torch.utils.cpp_extension import include_paths as torch_include_paths

cxx_flags.extend(get_warning_flags())
system_include_dirs = [
*torch_include_paths(),
*get_cuda_include_dirs(),
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
sysconfig.get_path("include"),
]

# Version-dependent CUDA options
try:
version = cuda_version()
Expand All @@ -89,6 +103,8 @@ def setup_pytorch_extension(
raise RuntimeError("Transformer Engine requires CUDA 12.0 or newer")

setup_mpi_flags(include_dirs, cxx_flags)
if bool(int(os.getenv("NVTE_UB_WITH_MPI", "0"))):
system_include_dirs.append(Path(os.getenv("MPI_HOME")) / "include")

# ProcessGroupNCCL class declarations are gated on USE_C10D_NCCL. get_nccl_comm_ptr
# borrows the raw ncclComm_t from that backend and is used by both NCCL EP and the
Expand All @@ -113,13 +129,17 @@ def setup_pytorch_extension(
), "NVSHMEM_HOME must be set when compiling with NVTE_ENABLE_NVSHMEM=1"
nvshmem_home = Path(os.getenv("NVSHMEM_HOME"))
include_dirs.append(nvshmem_home / "include")
system_include_dirs.append(nvshmem_home / "include")
library_dirs.append(nvshmem_home / "lib")
libraries.append("nvshmem_host")
cxx_flags.append("-DNVTE_ENABLE_NVSHMEM")

if bool(int(os.getenv("NVTE_WITH_CUBLASMP", 0))):
cxx_flags.append("-DNVTE_WITH_CUBLASMP")

for system_include_dir in system_include_dirs:
cxx_flags.extend(["-isystem", str(system_include_dir)])

# Construct PyTorch CUDA extension
sources = [str(path) for path in sources]
include_dirs = [str(path) for path in include_dirs]
Expand Down
13 changes: 13 additions & 0 deletions build_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ def get_bolt_build_flags() -> Tuple[List[str], List[str]]:
return compiler_flags, linker_flags


def warnings_as_errors_enabled() -> bool:
"""Whether compiler warnings in TE sources are treated as errors"""
return bool(int(os.getenv("NVTE_WARNINGS_AS_ERRORS", "1")))


def get_warning_flags() -> List[str]:
"""Host compiler warning flags for TE's own sources"""
flags = ["-Wall", "-Wextra"]
if warnings_as_errors_enabled():
flags.append("-Werror")
return flags


@functools.lru_cache(maxsize=None)
def get_max_jobs_for_parallel_build() -> int:
"""Number of parallel jobs for Nina build"""
Expand Down
6 changes: 6 additions & 0 deletions docs/envvars.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ Build Configuration
:Default: ``0``
:Description: Enable debug build mode. When set to ``1``, the build includes debug symbols (``-g``) and disables optimizations.

.. envvar:: NVTE_WARNINGS_AS_ERRORS

:Type: ``int`` (0 or 1)
:Default: ``1``
:Description: Treat compiler warnings in Transformer Engine's own C++ and CUDA sources as errors. Set to ``0`` to report warnings without failing the build, e.g. when building with a newer compiler that emits new warnings. Third-party headers are excluded.

.. envvar:: NVTE_BUILD_MAX_JOBS

:Type: ``int``
Expand Down
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
nccl_ep_enabled,
get_max_jobs_for_parallel_build,
nvcc_path,
warnings_as_errors_enabled,
)

frameworks = get_frameworks()
Expand Down Expand Up @@ -86,6 +87,11 @@ def setup_common_extension() -> CMakeExtension:
), "NVSHMEM_HOME must be set when compiling with NVTE_ENABLE_NVSHMEM=1"
cmake_flags.append("-DNVTE_ENABLE_NVSHMEM=ON")

if warnings_as_errors_enabled():
cmake_flags.append("-DNVTE_WARNINGS_AS_ERRORS=ON")
else:
cmake_flags.append("-DNVTE_WARNINGS_AS_ERRORS=OFF")

if bool(int(os.getenv("NVTE_BUILD_ACTIVATION_WITH_FAST_MATH", "0"))):
cmake_flags.append("-DNVTE_BUILD_ACTIVATION_WITH_FAST_MATH=ON")

Expand Down
55 changes: 47 additions & 8 deletions transformer_engine/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ function(nvte_enable_bolt_compatible_compile_options TARGET_NAME)
endif()
endfunction()

# Compiler warnings. These apply only to Transformer Engine's own targets, are
# PRIVATE so they never propagate to consumers, and third-party headers are
# included as SYSTEM so their warnings are not reported.
option(NVTE_WARNINGS_AS_ERRORS "Treat compiler warnings as errors" ON)
function(nvte_enable_compiler_warnings TARGET_NAME)
target_compile_options(
${TARGET_NAME}
PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:-Wall>
$<$<COMPILE_LANGUAGE:CXX>:-Wextra>
$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=-Wall,-Wextra>
)
if(NVTE_WARNINGS_AS_ERRORS)
target_compile_options(
${TARGET_NAME}
PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:-Werror>
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
$<$<COMPILE_LANGUAGE:CUDA>:-Werror=all-warnings>
$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=-Werror>
)
endif()
endfunction()

# CUDA Toolkit
find_package(CUDAToolkit REQUIRED)
if (CUDAToolkit_VERSION VERSION_LESS 12.1)
Expand Down Expand Up @@ -434,6 +457,7 @@ endforeach()

add_library(transformer_engine SHARED ${transformer_engine_SOURCES})
nvte_enable_bolt_compatible_compile_options(transformer_engine)
nvte_enable_compiler_warnings(transformer_engine)

# This is TE-specific and should not apply to all targets
target_link_options(
Expand Down Expand Up @@ -470,6 +494,21 @@ set_property(
PROPERTY
COMPILE_OPTIONS "--generate-code=arch=compute_90a,code=sm_90a")

# The CUTLASS SM90 grouped GEMM kernels use TMA multicast. When they are also
# compiled for family targets such as sm_120f, ptxas emits an advisory warning
# about third-party CUTLASS code, so silence that advisory for this source.
execute_process(COMMAND "${CUDAToolkit_BIN_DIR}/ptxas" --help
OUTPUT_VARIABLE _nvte_ptxas_help
ERROR_QUIET)
if(_nvte_ptxas_help MATCHES "suppress-async-bulk-multicast-advisory-warning")
set_property(
SOURCE gemm/cutlass_grouped_gemm.cu
APPEND
PROPERTY
COMPILE_OPTIONS "-Xptxas=--suppress-async-bulk-multicast-advisory-warning")
endif()
unset(_nvte_ptxas_help)

# CUTLASS kernels could cause hang in debug build
set(CUTLASS_KERNEL_SOURCES
gemm/cutlass_grouped_gemm.cu
Expand All @@ -490,7 +529,7 @@ target_link_libraries(transformer_engine PUBLIC
target_link_libraries(transformer_engine PRIVATE ${CMAKE_DL_LIBS})

if(NVTE_WITH_CUTEDSL)
target_include_directories(transformer_engine PRIVATE ${TVM_FFI_INCLUDE_DIR})
target_include_directories(transformer_engine SYSTEM PRIVATE ${TVM_FFI_INCLUDE_DIR})
target_compile_definitions(
transformer_engine PRIVATE
NVTE_WITH_CUTEDSL
Expand All @@ -500,11 +539,11 @@ endif()

target_include_directories(transformer_engine PRIVATE
${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
target_include_directories(transformer_engine PRIVATE ${NCCL_INCLUDE_DIR})
target_include_directories(transformer_engine SYSTEM PRIVATE ${NCCL_INCLUDE_DIR})
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
target_include_directories(transformer_engine SYSTEM PRIVATE "${CUDNN_FRONTEND_INCLUDE_DIR}")
target_include_directories(transformer_engine SYSTEM PRIVATE
${CUTLASS_INCLUDE_DIR}
${CUTLASS_TOOLS_INCLUDE_DIR})

Expand All @@ -513,7 +552,7 @@ 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_include_directories(transformer_engine SYSTEM PRIVATE ${MPI_CXX_INCLUDES})
target_compile_definitions(transformer_engine PUBLIC NVTE_UB_WITH_MPI)
endif()

Expand All @@ -529,7 +568,7 @@ option(NVTE_WITH_CUBLASMP "Use cuBLASMp for tensor parallel GEMMs" OFF)
if (NVTE_WITH_CUBLASMP)

target_compile_definitions(transformer_engine PRIVATE NVTE_WITH_CUBLASMP)
target_include_directories(transformer_engine PRIVATE ${CUBLASMP_DIR}/include)
target_include_directories(transformer_engine SYSTEM PRIVATE ${CUBLASMP_DIR}/include)
find_cublasmp_version(CUBLASMP_VERSION CUBLASMP_INCLUDE_DIR ${CUBLASMP_DIR})
find_library(CUBLASMP_LIB
NAMES cublasmp libcublasmp.so libcublasmp.so.0
Expand Down Expand Up @@ -562,7 +601,7 @@ endif()
option(NVTE_WITH_CUSOLVERMP "Use cuSolverMp for distributed Newton-Schulz" OFF)
if (NVTE_WITH_CUSOLVERMP)
target_compile_definitions(transformer_engine PRIVATE NVTE_WITH_CUSOLVERMP)
target_include_directories(transformer_engine PRIVATE ${CUSOLVERMP_DIR}/include)
target_include_directories(transformer_engine SYSTEM PRIVATE ${CUSOLVERMP_DIR}/include)
find_library(CUSOLVERMP_LIB
NAMES cusolverMp libcusolverMp
PATHS ${CUSOLVERMP_DIR}
Expand Down Expand Up @@ -616,7 +655,7 @@ if(NOT NCCL_LIB)
REQUIRED)
endif()

target_include_directories(transformer_engine PRIVATE
target_include_directories(transformer_engine SYSTEM PRIVATE
${NCCL_EP_INCLUDE_DIR})

# libtransformer_engine.so uses only the baseline NCCL API directly. NCCL EP's
Expand Down
19 changes: 8 additions & 11 deletions transformer_engine/common/activation/scaled_activation.cu
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,10 @@ __device__ __forceinline__ void gated_backward_values(const float act_in, const

template <int nvec, typename InputT, typename ScaleT, typename OutputT, typename ParamOP,
float (*ActOP)(float, const ParamOP &)>
__global__ void __launch_bounds__(kThreads, 4)
scaled_gated_forward_kernel(const InputT *__restrict__ input,
const ScaleT *__restrict__ act_scales, OutputT *__restrict__ output,
const size_t rows, const size_t hidden, const size_t segment_size,
const size_t num_segments, const size_t num_vectors_per_segment,
const ParamOP param) {
__global__ void NVTE_SCALED_ACTIVATION_LAUNCH_BOUNDS scaled_gated_forward_kernel(
const InputT *__restrict__ input, const ScaleT *__restrict__ act_scales,
OutputT *__restrict__ output, const size_t rows, const size_t hidden, const size_t segment_size,
const size_t num_segments, const size_t num_vectors_per_segment, const ParamOP param) {
const size_t total_vectors = rows * num_segments * num_vectors_per_segment;
for (size_t tid = blockIdx.x * blockDim.x + threadIdx.x; tid < total_vectors;
tid += gridDim.x * blockDim.x) {
Expand Down Expand Up @@ -225,11 +223,10 @@ __global__ void __launch_bounds__(kReductionThreads, 4)

template <int nvec, typename InputT, typename ScaleT, typename OutputT, typename ParamOP,
float (*ActOP)(float, const ParamOP &)>
__global__ void __launch_bounds__(kThreads, 4)
scaled_unary_forward_kernel(const InputT *__restrict__ input,
const ScaleT *__restrict__ act_scales, OutputT *__restrict__ output,
const size_t rows, const size_t hidden,
const size_t num_vectors_per_row, const ParamOP param) {
__global__ void NVTE_SCALED_ACTIVATION_LAUNCH_BOUNDS
scaled_unary_forward_kernel(const InputT *__restrict__ input, const ScaleT *__restrict__ act_scales,
OutputT *__restrict__ output, const size_t rows, const size_t hidden,
const size_t num_vectors_per_row, const ParamOP param) {
const size_t total_vectors = rows * num_vectors_per_row;
for (size_t tid = blockIdx.x * blockDim.x + threadIdx.x; tid < total_vectors;
tid += gridDim.x * blockDim.x) {
Expand Down
15 changes: 14 additions & 1 deletion transformer_engine/common/activation/scaled_activation.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ namespace detail {
namespace scaled_activation {

constexpr int kThreads = unary_kernel_threads;

// Launch bounds for the kThreads-wide kernels. A minimum of 4 resident 512-thread blocks needs
// 2048 threads per SM, which SM 7.5 (1024) and SM 8.6/8.7/8.9/12.x (1536) cannot hold, so ptxas
// ignores the minimum there with a warning. Leave it out on those targets; the generated code is
// the same.
#if defined(__CUDA_ARCH__) && \
(__CUDA_ARCH__ == 750 || __CUDA_ARCH__ == 860 || __CUDA_ARCH__ == 870 || \
__CUDA_ARCH__ == 890 || __CUDA_ARCH__ == 1200 || __CUDA_ARCH__ == 1210)
#define NVTE_SCALED_ACTIVATION_LAUNCH_BOUNDS __launch_bounds__(kThreads)
#else
#define NVTE_SCALED_ACTIVATION_LAUNCH_BOUNDS __launch_bounds__(kThreads, 4)
#endif

constexpr int kReductionThreads = 256;
constexpr int kReductionWarps = kReductionThreads / THREADS_PER_WARP;

Expand All @@ -39,7 +52,7 @@ inline int choose_reduction_threads(const size_t row_vectors) {
}
const int needed = static_cast<int>(row_vectors);
int rounded = (needed + THREADS_PER_WARP - 1) / THREADS_PER_WARP * THREADS_PER_WARP;
if (rounded < THREADS_PER_WARP) {
if (rounded < static_cast<int>(THREADS_PER_WARP)) {
rounded = THREADS_PER_WARP;
}
return rounded;
Expand Down
2 changes: 1 addition & 1 deletion transformer_engine/common/cast/cast.cu
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void nvte_multi_tensor_quantize(const NVTETensor *inputs, NVTETensor *outputs,
cudaStreamWaitEvent(detail::get_compute_stream(s), detail::get_compute_stream_event(0)));
}

for (int i = 0; i < num_tensors; i++) {
for (size_t i = 0; i < num_tensors; i++) {
dispatch::quantize_fwd_helper<IS_ACT, Empty, nullptr>(
inputs[i], outputs[i], quant_configs, detail::get_compute_stream(i % num_streams));
}
Expand Down
42 changes: 21 additions & 21 deletions transformer_engine/common/cast/dispatch/quantize.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,6 @@ void group_quantize_fwd_host_aware_helper(const NVTETensor input, NVTETensor *ou
// output list here is allowed to have empty tensor

// Choose kernel
const auto [rows, cols] = input_tensor->flat_2d_dims();
auto dtype = input_tensor->dtype();

const bool nvfp4_use_4over6 = quant_config_cpp.nvfp4_4over6_mode != kNVTENVFP44Over6Disabled;
if (!nvfp4_use_4over6) {
for (const auto *output_tensor : output_tensors) {
Expand Down Expand Up @@ -582,25 +579,28 @@ void group_quantize_fwd_helper(const NVTEGroupedTensor input, NVTEGroupedTensor
}
case NVTE_NVFP4_1D_SCALING: {
NVTE_CHECK(!IS_ACT, "IS_ACT is not implemented for grouped NVTE_NVFP4_1D_SCALING.");
NVTE_CHECK(output_tensor->row_scaled_nvfp4,
"Grouped NVFP4 quantization is only implemented for row-scaled NVFP4.");
NVTE_CHECK(!quant_config_cpp.stochastic_rounding,
"Grouped row-scaled NVFP4 quantization does not support stochastic rounding.");
NVTE_CHECK(!quant_config_cpp.nvfp4_2d_quantization,
"Grouped row-scaled NVFP4 quantization does not support 2D quantization.");
NVTE_CHECK(quant_config_cpp.nvfp4_4over6_mode == kNVTENVFP44Over6Disabled,
"Grouped row-scaled NVFP4 quantization does not support 4over6.");
if (input_tensor->num_tensors == 0) {
break;
// Discard the rest for IS_ACT, where the check above always throws.
if constexpr (!IS_ACT) {
NVTE_CHECK(output_tensor->row_scaled_nvfp4,
"Grouped NVFP4 quantization is only implemented for row-scaled NVFP4.");
NVTE_CHECK(!quant_config_cpp.stochastic_rounding,
"Grouped row-scaled NVFP4 quantization does not support stochastic rounding.");
NVTE_CHECK(!quant_config_cpp.nvfp4_2d_quantization,
"Grouped row-scaled NVFP4 quantization does not support 2D quantization.");
NVTE_CHECK(quant_config_cpp.nvfp4_4over6_mode == kNVTENVFP44Over6Disabled,
"Grouped row-scaled NVFP4 quantization does not support 4over6.");
if (input_tensor->num_tensors == 0) {
break;
}
nvfp4::group_row_amax_graph_safe(input_tensor, noop_tensor, output_tensor, stream);
const DType scale_dtype = output_tensor->scale_inv.has_data()
? output_tensor->scale_inv.dtype
: output_tensor->columnwise_scale_inv.dtype;
TRANSFORMER_ENGINE_NVFP4_SCALE_TYPE_SWITCH(
scale_dtype, ScaleType,
nvfp4::group_row_scaled_cast_graph_safe<ScaleType>(
input_tensor, noop_tensor, output_tensor, &quant_config_cpp, stream););
}
nvfp4::group_row_amax_graph_safe(input_tensor, noop_tensor, output_tensor, stream);
const DType scale_dtype = output_tensor->scale_inv.has_data()
? output_tensor->scale_inv.dtype
: output_tensor->columnwise_scale_inv.dtype;
TRANSFORMER_ENGINE_NVFP4_SCALE_TYPE_SWITCH(
scale_dtype, ScaleType,
nvfp4::group_row_scaled_cast_graph_safe<ScaleType>(
input_tensor, noop_tensor, output_tensor, &quant_config_cpp, stream););
break;
}
default:
Expand Down
Loading