diff --git a/include/cutlass/epilogue/thread/linear_combination.h b/include/cutlass/epilogue/thread/linear_combination.h index c7dbd147c8..c180987814 100644 --- a/include/cutlass/epilogue/thread/linear_combination.h +++ b/include/cutlass/epilogue/thread/linear_combination.h @@ -227,11 +227,8 @@ class LinearCombination { // Convert to destination numeric type NumericArrayConverter destination_converter; - FragmentCompute converted_source = source_converter(source); - FragmentCompute converted_accumulator = accumulator_converter(accumulator); - if (Scale == ScaleType::Nothing) - return destination_converter(converted_accumulator); + return destination_converter(accumulator_converter(accumulator)); // Perform binary operations FragmentCompute intermediate; @@ -239,6 +236,21 @@ class LinearCombination { multiplies mul_add_source; multiply_add mul_add_accumulator; + // Avoid propagating NaNs from the accumulator when its coefficient is zero. + if (alpha_ == ElementCompute(0)) { + intermediate.clear(); + + if (Scale == ScaleType::NoBetaScaling) + intermediate = source_converter(source); + else if (beta_ != ElementCompute(0)) + intermediate = mul_add_source(beta_, source_converter(source)); + + return destination_converter(intermediate); + } + + FragmentCompute converted_source = source_converter(source); + FragmentCompute converted_accumulator = accumulator_converter(accumulator); + if (Scale == ScaleType::NoBetaScaling) intermediate = converted_source; else @@ -260,16 +272,21 @@ class LinearCombination { // Convert to destination numeric type NumericArrayConverter destination_converter; - FragmentCompute converted_accumulator = accumulator_converter(accumulator); - if (Scale == ScaleType::Nothing) - return destination_converter(converted_accumulator); + return destination_converter(accumulator_converter(accumulator)); // Perform binary operations FragmentCompute intermediate; + + // Avoid propagating NaNs from the accumulator when its coefficient is zero. + if (alpha_ == ElementCompute(0)) { + intermediate.clear(); + return destination_converter(intermediate); + } + multiplies mul_accumulator; - intermediate = mul_accumulator(alpha_, converted_accumulator); // D = alpha * Accum + intermediate = mul_accumulator(alpha_, accumulator_converter(accumulator)); // D = alpha * Accum return destination_converter(intermediate); } @@ -286,9 +303,8 @@ class LinearCombination { // Convert to destination numeric type - ElementCompute converted_accumulator = accumulator_converter(accumulator); if constexpr (Scale == ScaleType::Nothing) { - return destination_converter(converted_accumulator); + return destination_converter(accumulator_converter(accumulator)); } // Perform binary operations @@ -296,6 +312,19 @@ class LinearCombination { multiplies multiply; multiply_add madd; + // Avoid propagating NaNs from the accumulator when its coefficient is zero. + if (alpha_ == ElementCompute(0)) { + if constexpr (Scale == ScaleType::NoBetaScaling) { + return destination_converter(source_converter(source)); + } + else { + return destination_converter( + beta_ == ElementCompute(0) ? ElementCompute(0) : multiply(beta_, source_converter(source))); + } + } + + ElementCompute converted_accumulator = accumulator_converter(accumulator); + if constexpr (Scale == ScaleType::NoBetaScaling) { intermediate = source_converter(source); } @@ -312,18 +341,21 @@ class LinearCombination { // Convert everything to Compute type, do compute, and then store to output type NumericConverter accumulator_converter; NumericConverter destination_converter; - ElementCompute converted_accumulator = accumulator_converter(accumulator); - // Convert to destination numeric type if constexpr (Scale == ScaleType::Nothing) { - return destination_converter(converted_accumulator); + return destination_converter(accumulator_converter(accumulator)); + } + + // Avoid propagating NaNs from the accumulator when its coefficient is zero. + if (alpha_ == ElementCompute(0)) { + return destination_converter(ElementCompute(0)); } // Perform binary operations ElementCompute intermediate; multiplies multiply; - intermediate = multiply(alpha_, accumulator); // D = alpha * Accum + intermediate = multiply(alpha_, accumulator_converter(accumulator)); // D = alpha * Accum return destination_converter(intermediate); } }; diff --git a/test/unit/epilogue/thread/linear_combination.cu b/test/unit/epilogue/thread/linear_combination.cu index 6c75e9ae31..0257b45421 100644 --- a/test/unit/epilogue/thread/linear_combination.cu +++ b/test/unit/epilogue/thread/linear_combination.cu @@ -38,6 +38,8 @@ #include "cutlass/epilogue/thread/linear_combination_gelu.h" #include "cutlass/epilogue/thread/activation.h" +#include + ///////////////////////////////////////////////////////////////////////////////////////////////// TEST(Epilogue_thread_linear_combination, device_side_f16_f32_value) { @@ -128,6 +130,81 @@ TEST(Epilogue_thread_linear_combination, device_side_f16_f32_ptr) { ///////////////////////////////////////////////////////////////////////////////////////////////// +TEST(Epilogue_thread_linear_combination, alpha_zero_does_not_propagate_nan) { + + using Element = float; + int const kCount = 4; + + using LinearCombination = cutlass::epilogue::thread::LinearCombination< + Element, + kCount, + Element, + Element>; + + Element const alpha = Element(0); + Element const beta = Element(1); + Element const quiet_nan = std::numeric_limits::quiet_NaN(); + + LinearCombination linear_combination_op(typename LinearCombination::Params(alpha, beta)); + + typename LinearCombination::FragmentAccumulator accum; + typename LinearCombination::FragmentSource source; + + for (int i = 0; i < kCount; ++i) { + accum[i] = quiet_nan; + source[i] = Element(i + 1); + } + + typename LinearCombination::FragmentOutput destination = linear_combination_op(accum, source); + + for (int i = 0; i < kCount; ++i) { + EXPECT_EQ(destination[i], source[i]); + } + + EXPECT_EQ(linear_combination_op(quiet_nan, Element(7)), Element(7)); +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + +TEST(Epilogue_thread_linear_combination, alpha_beta_zero_do_not_propagate_nan) { + + using Element = float; + int const kCount = 4; + + using LinearCombination = cutlass::epilogue::thread::LinearCombination< + Element, + kCount, + Element, + Element>; + + Element const alpha = Element(0); + Element const beta = Element(0); + Element const quiet_nan = std::numeric_limits::quiet_NaN(); + + LinearCombination linear_combination_op(typename LinearCombination::Params(alpha, beta)); + + typename LinearCombination::FragmentAccumulator accum; + typename LinearCombination::FragmentSource source; + + for (int i = 0; i < kCount; ++i) { + accum[i] = quiet_nan; + source[i] = quiet_nan; + } + + typename LinearCombination::FragmentOutput destination_with_source = linear_combination_op(accum, source); + typename LinearCombination::FragmentOutput destination_without_source = linear_combination_op(accum); + + for (int i = 0; i < kCount; ++i) { + EXPECT_EQ(destination_with_source[i], Element(0)); + EXPECT_EQ(destination_without_source[i], Element(0)); + } + + EXPECT_EQ(linear_combination_op(quiet_nan, quiet_nan), Element(0)); + EXPECT_EQ(linear_combination_op(quiet_nan), Element(0)); +} + +///////////////////////////////////////////////////////////////////////////////////////////////// + TEST(Epilogue_thread_linear_combination_gelu, device_side_f16_f16_ptr) { @@ -202,4 +279,3 @@ TEST(Epilogue_thread_linear_combination_gelu_taylor, device_side_f16_f16_ptr) { } ///////////////////////////////////////////////////////////////////////////////////////////////// - diff --git a/test/unit/gemm/device/CMakeLists.txt b/test/unit/gemm/device/CMakeLists.txt index 3c26a5116d..4a74c4f61e 100644 --- a/test/unit/gemm/device/CMakeLists.txt +++ b/test/unit/gemm/device/CMakeLists.txt @@ -129,6 +129,12 @@ cutlass_test_unit_gemm_device_add_executable( list(APPEND CUTLASS_TEST_UNIT_GEMM_DEVICE_LIST cutlass_test_unit_gemm_device_simt) +cutlass_test_unit_gemm_device_add_executable( + cutlass_test_unit_gemm_device_simt_alpha_zero + + simt_sgemm_alpha_zero_sm80.cu +) + cutlass_test_unit_gemm_device_add_executable( cutlass_test_unit_gemm_device_simt_3x diff --git a/test/unit/gemm/device/simt_sgemm_alpha_zero_sm80.cu b/test/unit/gemm/device/simt_sgemm_alpha_zero_sm80.cu new file mode 100644 index 0000000000..889e0a11bc --- /dev/null +++ b/test/unit/gemm/device/simt_sgemm_alpha_zero_sm80.cu @@ -0,0 +1,100 @@ +/*************************************************************************************************** + * Copyright (c) 2017 - 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + **************************************************************************************************/ +/*! \file + \brief Regression tests for a zero-alpha SIMT GEMM epilogue. +*/ + +#include + +#include "../../common/cutlass_unit_test.h" + +#include "cutlass/cutlass.h" +#include "cutlass/gemm/device/gemm.h" +#include "cutlass/util/host_tensor.h" +#include "cutlass/util/reference/host/tensor_fill.h" + +#if defined(CUTLASS_ARCH_MMA_SM80_SUPPORTED) + +TEST(SM80_Device_Gemm_f32_simt, alpha_zero_does_not_propagate_nan) { + using Element = float; + using Layout = cutlass::layout::RowMajor; + using Gemm = cutlass::gemm::device::Gemm< + Element, + Layout, + Element, + Layout, + Element, + Layout, + Element, + cutlass::arch::OpClassSimt, + cutlass::arch::Sm80>; + + cutlass::gemm::GemmCoord const problem_size(8, 8, 8); + Element const quiet_nan = std::numeric_limits::quiet_NaN(); + + cutlass::HostTensor tensor_A(problem_size.mk()); + cutlass::HostTensor tensor_B(problem_size.kn()); + cutlass::HostTensor tensor_C(problem_size.mn()); + cutlass::HostTensor tensor_D(problem_size.mn()); + + cutlass::reference::host::TensorFill(tensor_A.host_view(), quiet_nan); + cutlass::reference::host::TensorFill(tensor_B.host_view(), quiet_nan); + tensor_A.sync_device(); + tensor_B.sync_device(); + + for (Element beta : {Element(1), Element(0)}) { + Element const source_value = beta == Element(0) ? quiet_nan : Element(3); + cutlass::reference::host::TensorFill(tensor_C.host_view(), source_value); + tensor_C.sync_device(); + + typename Gemm::Arguments arguments{ + problem_size, + tensor_A.device_ref(), + tensor_B.device_ref(), + tensor_C.device_ref(), + tensor_D.device_ref(), + {Element(0), beta}}; + + Gemm gemm_op; + ASSERT_EQ(gemm_op.can_implement(arguments), cutlass::Status::kSuccess); + ASSERT_EQ(gemm_op(arguments), cutlass::Status::kSuccess); + ASSERT_EQ(cudaDeviceSynchronize(), cudaSuccess); + + tensor_D.sync_host(); + Element const expected = beta == Element(0) ? Element(0) : source_value; + + for (size_t i = 0; i < tensor_D.capacity(); ++i) { + EXPECT_EQ(tensor_D.host_data()[i], expected) << "beta = " << beta << ", index = " << i; + } + } +} + +#endif // CUTLASS_ARCH_MMA_SM80_SUPPORTED