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
60 changes: 46 additions & 14 deletions include/cutlass/epilogue/thread/linear_combination.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,30 @@ class LinearCombination {
// Convert to destination numeric type
NumericArrayConverter<ElementOutput, ElementCompute, kCount, Round> 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;

multiplies<FragmentCompute> mul_add_source;
multiply_add<FragmentCompute> 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
Expand All @@ -260,16 +272,21 @@ class LinearCombination {
// Convert to destination numeric type
NumericArrayConverter<ElementOutput, ElementCompute, kCount, Round> 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<FragmentCompute> 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);
}
Expand All @@ -286,16 +303,28 @@ 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
ElementCompute intermediate;
multiplies<ElementCompute> multiply;
multiply_add<ElementCompute> 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);
}
Expand All @@ -312,18 +341,21 @@ class LinearCombination {
// Convert everything to Compute type, do compute, and then store to output type
NumericConverter<ElementCompute, ElementAccumulator, Round> accumulator_converter;
NumericConverter<ElementD, ElementCompute, Round> 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<ElementCompute> multiply;

intermediate = multiply(alpha_, accumulator); // D = alpha * Accum
intermediate = multiply(alpha_, accumulator_converter(accumulator)); // D = alpha * Accum
return destination_converter(intermediate);
}
};
Expand Down
78 changes: 77 additions & 1 deletion test/unit/epilogue/thread/linear_combination.cu
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include "cutlass/epilogue/thread/linear_combination_gelu.h"
#include "cutlass/epilogue/thread/activation.h"

#include <limits>

/////////////////////////////////////////////////////////////////////////////////////////////////

TEST(Epilogue_thread_linear_combination, device_side_f16_f32_value) {
Expand Down Expand Up @@ -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<Element>::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<Element>::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) {

Expand Down Expand Up @@ -202,4 +279,3 @@ TEST(Epilogue_thread_linear_combination_gelu_taylor, device_side_f16_f16_ptr) {
}

/////////////////////////////////////////////////////////////////////////////////////////////////

6 changes: 6 additions & 0 deletions test/unit/gemm/device/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
100 changes: 100 additions & 0 deletions test/unit/gemm/device/simt_sgemm_alpha_zero_sm80.cu
Original file line number Diff line number Diff line change
@@ -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 <limits>

#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<Element>::quiet_NaN();

cutlass::HostTensor<Element, Layout> tensor_A(problem_size.mk());
cutlass::HostTensor<Element, Layout> tensor_B(problem_size.kn());
cutlass::HostTensor<Element, Layout> tensor_C(problem_size.mn());
cutlass::HostTensor<Element, Layout> 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