co-authored by
Aniket Shivam
parent
ca23ff7924
commit
b72cbf957d
@@ -0,0 +1,36 @@
|
||||
|
||||
# Copyright (c) 2017 - 2022 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.
|
||||
|
||||
|
||||
|
||||
cutlass_example_add_executable(
|
||||
37_gemm_layernorm_gemm_fusion
|
||||
gemm_layernorm.cu
|
||||
)
|
||||
|
||||
@@ -0,0 +1,937 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright (c) 2017 - 2022 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 CUTLASS Layernorm Example.
|
||||
|
||||
This workload provides a layer normalization example using a one-pass, square-sum-based
|
||||
variance calculation. Specifically, we fuse the reduction operation to find
|
||||
local mean and local square sum mean in the epilogue of 1st GEMM. After a light
|
||||
full reduction kernel, the mean / variance values are readily calculated for element-wise
|
||||
operations which are fused into the 2nd GEMM.
|
||||
|
||||
As stated in https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Computing_shifted_data,
|
||||
the square-sum based one-pass implementation may raise concerns on numerical stability issues.
|
||||
That being said, though this fully fused layernorm example almost perfectly hides all the memory cost to
|
||||
access the intermediate matrix for layernorm computation, the numerical issue might hinder a persuasive
|
||||
usage in real-world scenarios. If that is the case, a user may turn to the stand-alone CUTLASS layernorm
|
||||
example in tools/util/include/cutlass/util/device_layernorm.h
|
||||
|
||||
Examples:
|
||||
|
||||
# Run a CUTLASS layernorm example with default setup ,
|
||||
# using the language of the transformer model as an example,
|
||||
(Column Major output matrix, hidden dimension = 768, valid word number = 4096, intermediate_scale = 4)
|
||||
$ ./examples/37_gemm_layernorm_gemm_fusion/37_gemm_layernorm_gemm_fusion
|
||||
|
||||
# Run an attention example with hidden dimension = 512
|
||||
$ ./examples/37_gemm_layernorm_gemm_fusion/37_gemm_layernorm_gemm_fusion --hidden_dim=512
|
||||
|
||||
*/
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
|
||||
#include "cutlass/cutlass.h"
|
||||
#include "cutlass/arch/memory.h"
|
||||
#include "cutlass/arch/memory_sm75.h"
|
||||
#include "cutlass/gemm/device/gemm_complex.h"
|
||||
#include "cutlass/epilogue/thread/scale_type.h"
|
||||
|
||||
#include "cutlass/util/command_line.h"
|
||||
#include "cutlass/util/host_tensor.h"
|
||||
#include "cutlass/util/reference/device/gemm.h"
|
||||
#include "cutlass/util/reference/host/gemm_complex.h"
|
||||
#include "cutlass/util/reference/host/tensor_reduce.h"
|
||||
#include "cutlass/util/reference/host/tensor_compare.h"
|
||||
#include "cutlass/util/reference/host/tensor_norm.h"
|
||||
#include "cutlass/util/reference/host/tensor_copy.h"
|
||||
#include "cutlass/util/reference/host/tensor_fill.h"
|
||||
#include "cutlass/util/reference/host/error_metrics.h"
|
||||
#include "cutlass/util/tensor_view_io.h"
|
||||
#include "cutlass/fast_math.h"
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "gemm_with_layernorm.h"
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
enum class Disposition {
|
||||
kPassed,
|
||||
kIncorrect,
|
||||
kNotVerified
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Command line options parsing
|
||||
template<typename LayoutOutput_>
|
||||
struct Options {
|
||||
|
||||
using LayoutOutput = LayoutOutput_;
|
||||
|
||||
static bool const kIsColumnMajorOutput = cutlass::platform::is_same<LayoutOutput, cutlass::layout::ColumnMajor>::value;
|
||||
|
||||
bool help;
|
||||
cutlass::gemm::GemmCoord problem_size0;
|
||||
cutlass::gemm::GemmCoord problem_size1;
|
||||
int hidden_dim;
|
||||
int valid_word_num;
|
||||
int intermediate_scale;
|
||||
int iterations;
|
||||
unsigned seed;
|
||||
float alpha;
|
||||
float beta;
|
||||
bool verification_enabled;
|
||||
double tolerance;
|
||||
|
||||
Options():
|
||||
help(false),
|
||||
iterations(20),
|
||||
seed(2022),
|
||||
hidden_dim(768),
|
||||
valid_word_num(4096),
|
||||
intermediate_scale(4),
|
||||
alpha(1),
|
||||
beta(0),
|
||||
verification_enabled(true),
|
||||
tolerance(0.01),
|
||||
problem_size1(problem_size0.m() * 4, problem_size0.n(), problem_size0.m())
|
||||
{ }
|
||||
|
||||
bool valid() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parses the command line
|
||||
void parse(int argc, char const **args) {
|
||||
cutlass::CommandLine cmd(argc, args);
|
||||
|
||||
if (cmd.check_cmd_line_flag("help")) {
|
||||
help = true;
|
||||
}
|
||||
|
||||
cmd.get_cmd_line_argument("hidden_dim", hidden_dim, 768);
|
||||
cmd.get_cmd_line_argument("valid_word_num", valid_word_num, 4096);
|
||||
cmd.get_cmd_line_argument("iterations", iterations);
|
||||
cmd.get_cmd_line_argument("verify", verification_enabled);
|
||||
cmd.get_cmd_line_argument("seed", seed);
|
||||
cmd.get_cmd_line_argument("tolerance", tolerance);
|
||||
|
||||
if (kIsColumnMajorOutput) {
|
||||
// column major output setup
|
||||
problem_size0.m() = hidden_dim;
|
||||
problem_size0.n() = valid_word_num;
|
||||
problem_size0.k() = hidden_dim;
|
||||
|
||||
problem_size1.m() = hidden_dim * intermediate_scale;
|
||||
problem_size1.n() = valid_word_num;
|
||||
problem_size1.k() = hidden_dim;
|
||||
}else{
|
||||
// row major output setup
|
||||
problem_size0.m() = valid_word_num;
|
||||
problem_size0.n() = hidden_dim;
|
||||
problem_size0.k() = hidden_dim;
|
||||
|
||||
problem_size1.m() = valid_word_num;
|
||||
problem_size1.n() = hidden_dim * intermediate_scale;
|
||||
problem_size1.k() = hidden_dim;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Prints the usage statement.
|
||||
std::ostream & print_usage(std::ostream &out) const {
|
||||
|
||||
out << "37_gemm_layernorm_gemm_fusion example\n\n"
|
||||
<< " This example uses the CUTLASS Library to compute GEMM + Layernorm for arbitrary problem sizes.\n\n"
|
||||
<< "Options:\n\n"
|
||||
<< " --help If specified, displays this usage statement.\n\n"
|
||||
<< " --hidden_dim=<int> Hidden dimension\n"
|
||||
<< " --valid_word_num=<int> Valid word number\n"
|
||||
<< " --seed=<int> Random number seed (1*)\n\n"
|
||||
<< " --iterations=<int> Number of profiling iterations to perform (0 to disable profiling).\n\n"
|
||||
<< " --verify=<bool> If true, performs reference calculation.\n\n"
|
||||
<< " --tolerance <float> Error tolerance\n"
|
||||
;
|
||||
|
||||
out << "\n\nExamples:\n\n"
|
||||
<< "$ ./examples/37_gemm_layernorm_gemm_fusion/37_gemm_layernorm_gemm_fusion \\\n"
|
||||
<< " --hidden_dim=768 --valid_word_num=1024 \n\n";
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Returns true if the environment and Toolkit support this
|
||||
bool supported(bool verbose = true) const {
|
||||
|
||||
// Ampere Tensor Core operations exposed with mma.sync and ldmatrix are first available
|
||||
// in CUDA 11.0.
|
||||
//
|
||||
// CUTLASS must be compiled with CUDA 11.0 Toolkit to run these examples.
|
||||
if (!(__CUDACC_VER_MAJOR__ >= 11)) {
|
||||
if (verbose) {
|
||||
std::cerr << "Ampere Tensor Core operations must be compiled with CUDA 11.0 Toolkit or later." << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
cudaDeviceProp props;
|
||||
|
||||
cudaError_t error = cudaGetDeviceProperties(&props, 0);
|
||||
if (error != cudaSuccess) {
|
||||
if (verbose) {
|
||||
std::cerr << "cudaGetDeviceProperties() returned an error: " << cudaGetErrorString(error) << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!((props.major * 10 + props.minor) >= 80)) {
|
||||
if (verbose) {
|
||||
std::cerr << "Ampere Tensor Core operations must be run on a machine with compute capability at least 80."
|
||||
<< std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// CUTLASS attempts to load 128b vectors of cutlass::half_t (F16) elements. Consequently,
|
||||
// all pointers, strides, and tensor extents must be divisible by 8 elements.
|
||||
//
|
||||
int const kAlignment = 8;
|
||||
|
||||
if ((problem_size0.m() % kAlignment) ||
|
||||
(problem_size0.n() % kAlignment) ||
|
||||
(problem_size0.k() % kAlignment)) {
|
||||
if (verbose) {
|
||||
std::cerr << "Misaligned input in 1st GEMM." << std::endl;
|
||||
}
|
||||
// misaligned tensors for Gemm1
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((problem_size1.m() % kAlignment) ||
|
||||
(problem_size1.n() % kAlignment) ||
|
||||
(problem_size1.k() % kAlignment)) {
|
||||
if (verbose) {
|
||||
std::cerr << "Misaligned input in 2nd GEMM." << std::endl;
|
||||
}
|
||||
// misaligned tensors for Gemm2
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<
|
||||
typename LayoutOutput_>
|
||||
struct Testbed {
|
||||
|
||||
//
|
||||
// Type definitions
|
||||
//
|
||||
|
||||
// User-defined data types
|
||||
using ElementInputA0 = cutlass::half_t;
|
||||
using ElementInputB0 = cutlass::half_t;
|
||||
using ElementOutput = cutlass::half_t;
|
||||
using ElementCompute = cutlass::half_t;
|
||||
|
||||
using LayoutInputA0 = cutlass::layout::RowMajor;
|
||||
using LayoutInputB0 = cutlass::layout::ColumnMajor;
|
||||
using LayoutOutput = LayoutOutput_;
|
||||
|
||||
static bool const kIsColumnMajorOutput = cutlass::platform::is_same<LayoutOutput, cutlass::layout::ColumnMajor>::value;
|
||||
// turn of shifted K by default
|
||||
static bool const kIsShiftedVariance = false;
|
||||
|
||||
/// Linear scaling operator
|
||||
using EpilogueFunctorOp = cutlass::epilogue::thread::LinearCombination<
|
||||
ElementOutput,
|
||||
128 / cutlass::sizeof_bits<ElementOutput>::value,
|
||||
ElementCompute,
|
||||
ElementCompute
|
||||
>;
|
||||
|
||||
using ThreadblockShape = cutlass::gemm::GemmShape<128, 128, 32>;
|
||||
using WarpShape = cutlass::gemm::GemmShape<64, 64, 32>;
|
||||
using InstructionShape = cutlass::gemm::GemmShape<16, 8, 16>;
|
||||
|
||||
static int const kStages0 = 3;
|
||||
static int const kStages1 = 4;
|
||||
|
||||
using GemmLayernorm = cutlass::GemmLayernorm<
|
||||
ElementInputA0,
|
||||
LayoutInputA0,
|
||||
ElementInputB0,
|
||||
LayoutInputB0,
|
||||
ElementOutput,
|
||||
LayoutOutput,
|
||||
ElementCompute,
|
||||
EpilogueFunctorOp,
|
||||
ThreadblockShape,
|
||||
WarpShape,
|
||||
InstructionShape,
|
||||
kStages0,
|
||||
kStages1,
|
||||
kIsShiftedVariance
|
||||
>;
|
||||
|
||||
using ElementInputA1 = typename GemmLayernorm::ElementInputA1;
|
||||
using ElementOutputC1 = typename GemmLayernorm::ElementOutputC1;
|
||||
using ElementInputScaleBias = typename GemmLayernorm::ElementInputScaleBias;
|
||||
using ElementLayernormCompute = typename GemmLayernorm::ElementLayernormCompute;
|
||||
|
||||
using LayoutInputA1 = typename GemmLayernorm::LayoutInputA1;
|
||||
using LayoutOutputC0 = typename GemmLayernorm::LayoutOutputC0;
|
||||
using LayoutOutputC1 = typename GemmLayernorm::LayoutOutputC1;
|
||||
using LayoutInputScaleBias = typename GemmLayernorm::LayoutInputScaleBias;
|
||||
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
Options<LayoutOutput> const &options;
|
||||
|
||||
cutlass::HostTensor<ElementInputA0, LayoutInputA0> tensor_A0;
|
||||
cutlass::HostTensor<ElementInputB0, LayoutInputB0> tensor_B0;
|
||||
cutlass::HostTensor<ElementOutput, LayoutOutputC0> tensor_C0;
|
||||
cutlass::HostTensor<ElementInputA1, LayoutInputA1> tensor_A1;
|
||||
cutlass::HostTensor<ElementOutputC1, LayoutOutputC1> tensor_C1;
|
||||
|
||||
cutlass::HostTensor<ElementOutput, LayoutOutputC0> reference_C0;
|
||||
cutlass::HostTensor<ElementOutputC1, LayoutOutputC1> reference_C1;
|
||||
|
||||
cutlass::HostTensor<ElementInputScaleBias, LayoutInputScaleBias> tensor_Variance;
|
||||
cutlass::HostTensor<ElementInputScaleBias, LayoutInputScaleBias> tensor_Mean;
|
||||
cutlass::HostTensor<ElementInputScaleBias, LayoutInputScaleBias> tensor_Beta;
|
||||
cutlass::HostTensor<ElementInputScaleBias, LayoutInputScaleBias> tensor_Gamma;
|
||||
|
||||
cutlass::HostTensor<ElementInputScaleBias, LayoutInputScaleBias> reference_Mean;
|
||||
cutlass::HostTensor<ElementInputScaleBias, LayoutInputScaleBias> reference_Variance;
|
||||
|
||||
// shifted K tensor to better ensure the numerical stability
|
||||
// According to https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
|
||||
// the closer shifted K to the actual mean, the better numerical stability we'll observe
|
||||
cutlass::HostTensor<ElementOutput, LayoutOutputC0> tensor_Shifted_K;
|
||||
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
Testbed(
|
||||
Options<LayoutOutput> const &options_
|
||||
):
|
||||
options(options_)
|
||||
{
|
||||
|
||||
tensor_A0.reset({options.problem_size0.m(), options.problem_size0.k()});
|
||||
tensor_B0.reset({options.problem_size0.k(), options.problem_size0.n()});
|
||||
|
||||
tensor_C0.reset({options.problem_size0.m(), options.problem_size0.n()});
|
||||
|
||||
tensor_A1.reset({options.problem_size1.m(), options.problem_size1.k()});
|
||||
tensor_C1.reset({options.problem_size1.m(), options.problem_size1.n()});
|
||||
|
||||
reference_C0.reset({options.problem_size0.m(), options.problem_size0.n()});
|
||||
reference_C1.reset({options.problem_size1.m(), options.problem_size1.n()});
|
||||
|
||||
int leading_dim_0 = kIsColumnMajorOutput ? options.problem_size0.n() : options.problem_size0.m();
|
||||
int leading_dim_1 = kIsColumnMajorOutput ? options.problem_size0.m() : options.problem_size0.n();
|
||||
|
||||
int block_num = (leading_dim_1 + GemmLayernorm::ThreadblockShape::kM - 1) / GemmLayernorm::ThreadblockShape::kM;
|
||||
|
||||
tensor_Variance.reset({block_num, leading_dim_0});
|
||||
tensor_Mean.reset({block_num, leading_dim_0});
|
||||
tensor_Shifted_K.reset({1, leading_dim_0});
|
||||
|
||||
tensor_Beta.reset({1, leading_dim_1});
|
||||
tensor_Gamma.reset({1, leading_dim_1});
|
||||
|
||||
reference_Mean.reset({1, leading_dim_0}, false);
|
||||
reference_Variance.reset({1, leading_dim_0}, false);
|
||||
|
||||
}
|
||||
|
||||
/// Run
|
||||
Disposition run() {
|
||||
|
||||
Disposition disposition = Disposition::kNotVerified;
|
||||
|
||||
//
|
||||
// Initialize the workspace
|
||||
//
|
||||
|
||||
initialize();
|
||||
|
||||
//
|
||||
// Launch device kernel
|
||||
//
|
||||
cutlass::Status status = cutlass::Status::kSuccess;
|
||||
|
||||
status = execute_device_kernel();
|
||||
|
||||
if (status != cutlass::Status::kSuccess) {
|
||||
std::cerr << "Device execution failed." << std::endl;
|
||||
return disposition;
|
||||
}
|
||||
|
||||
cudaError_t result = cudaDeviceSynchronize();
|
||||
if (result != cudaSuccess) {
|
||||
std::cerr << "Device synchronize failed with error "
|
||||
<< cudaGetErrorString(result) << std::endl;
|
||||
return disposition;
|
||||
}
|
||||
|
||||
//
|
||||
// Compute the reference
|
||||
//
|
||||
compute_reference();
|
||||
|
||||
//
|
||||
// Verify
|
||||
//
|
||||
|
||||
if (options.verification_enabled) {
|
||||
|
||||
bool passed = verify();
|
||||
|
||||
if (passed) {
|
||||
disposition = Disposition::kPassed;
|
||||
}
|
||||
else {
|
||||
disposition = Disposition::kIncorrect;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Profiling
|
||||
//
|
||||
if (options.iterations) {
|
||||
profile();
|
||||
}
|
||||
|
||||
return disposition;
|
||||
}
|
||||
|
||||
/// Random initialization
|
||||
void initialize() {
|
||||
|
||||
cutlass::reference::host::TensorFillRandomUniform(
|
||||
tensor_A0.host_view(),
|
||||
options.seed,
|
||||
ElementInputA0(5),
|
||||
ElementInputA0(-5),
|
||||
0
|
||||
);
|
||||
|
||||
cutlass::reference::host::TensorFillRandomUniform(
|
||||
tensor_B0.host_view(),
|
||||
options.seed + 1,
|
||||
ElementInputB0(5),
|
||||
ElementInputB0(-5),
|
||||
0
|
||||
);
|
||||
|
||||
cutlass::reference::host::TensorFillRandomUniform(
|
||||
tensor_A1.host_view(),
|
||||
options.seed + 2,
|
||||
ElementInputA1(5),
|
||||
ElementInputA1(-5),
|
||||
0
|
||||
);
|
||||
|
||||
cutlass::reference::host::TensorFillRandomUniform(
|
||||
tensor_Beta.host_view(),
|
||||
options.seed + 3,
|
||||
ElementInputScaleBias(5),
|
||||
ElementInputScaleBias(-5),
|
||||
0
|
||||
);
|
||||
|
||||
cutlass::reference::host::TensorFillRandomUniform(
|
||||
tensor_Gamma.host_view(),
|
||||
options.seed + 4,
|
||||
ElementInputScaleBias(5),
|
||||
ElementInputScaleBias(-5),
|
||||
0
|
||||
);
|
||||
|
||||
cutlass::reference::host::TensorFillRandomUniform(
|
||||
tensor_Shifted_K.host_view(),
|
||||
options.seed + 5,
|
||||
ElementOutput(5),
|
||||
ElementOutput(-6),
|
||||
0
|
||||
);
|
||||
|
||||
tensor_A0.sync_device();
|
||||
tensor_B0.sync_device();
|
||||
tensor_A1.sync_device();
|
||||
tensor_Beta.sync_device();
|
||||
tensor_Gamma.sync_device();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
cutlass::Status execute_device_kernel() {
|
||||
|
||||
cutlass::Status status = cutlass::Status::kSuccess;
|
||||
|
||||
//
|
||||
// Setup arguments
|
||||
//
|
||||
|
||||
typename GemmLayernorm::Arguments args(
|
||||
options.problem_size0,
|
||||
options.problem_size1,
|
||||
tensor_A0.device_ref().data(),
|
||||
tensor_B0.device_ref().data(),
|
||||
tensor_C0.device_ref().data(),
|
||||
tensor_C0.device_ref().data(),
|
||||
tensor_A1.device_ref().data(),
|
||||
tensor_C1.device_ref().data(),
|
||||
tensor_A0.device_ref().stride(0),
|
||||
tensor_B0.device_ref().stride(0),
|
||||
tensor_C0.device_ref().stride(0),
|
||||
tensor_C0.device_ref().stride(0),
|
||||
tensor_A1.device_ref().stride(0),
|
||||
tensor_C1.device_ref().stride(0),
|
||||
{
|
||||
ElementCompute(options.alpha),
|
||||
ElementCompute(options.beta)
|
||||
},
|
||||
tensor_Variance.device_ref(),
|
||||
tensor_Mean.device_ref(),
|
||||
tensor_Gamma.device_ref(),
|
||||
tensor_Beta.device_ref(),
|
||||
tensor_Shifted_K.device_ref().data()
|
||||
);
|
||||
|
||||
//
|
||||
// Launch
|
||||
//
|
||||
|
||||
GemmLayernorm gemm_layernorm;
|
||||
|
||||
// Initialize
|
||||
status = gemm_layernorm.initialize(args);
|
||||
if (status != cutlass::Status::kSuccess) {
|
||||
return status;
|
||||
}
|
||||
|
||||
// Run
|
||||
status = gemm_layernorm();
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/// Reference calculation
|
||||
void compute_reference() {
|
||||
|
||||
cutlass::reference::device::Gemm<
|
||||
ElementInputA0,
|
||||
LayoutInputA0,
|
||||
ElementInputB0,
|
||||
LayoutInputB0,
|
||||
ElementOutput,
|
||||
LayoutOutputC0,
|
||||
ElementCompute,
|
||||
ElementCompute
|
||||
> gemm_device0;
|
||||
|
||||
cutlass::reference::device::Gemm<
|
||||
ElementInputA1,
|
||||
LayoutInputA1,
|
||||
ElementOutput,
|
||||
LayoutOutputC0,
|
||||
ElementOutputC1,
|
||||
LayoutOutputC1,
|
||||
ElementCompute,
|
||||
ElementCompute
|
||||
> gemm_device1;
|
||||
|
||||
// Compute 1st GEMM
|
||||
gemm_device0(
|
||||
options.problem_size0,
|
||||
ElementCompute(options.alpha),
|
||||
tensor_A0.device_ref(),
|
||||
tensor_B0.device_ref(),
|
||||
ElementCompute(options.beta),
|
||||
tensor_C0.device_ref(),
|
||||
reference_C0.device_ref()
|
||||
);
|
||||
|
||||
reference_C0.sync_host();
|
||||
|
||||
tensor_Mean.sync_host();
|
||||
tensor_Variance.sync_host();
|
||||
tensor_Gamma.sync_host();
|
||||
tensor_Beta.sync_host();
|
||||
tensor_Shifted_K.sync_host();
|
||||
|
||||
// Compute the sum and square sum for verification purpose
|
||||
if (kIsColumnMajorOutput) {
|
||||
for (int n = 0; n < options.problem_size0.n(); ++n) {
|
||||
|
||||
ElementLayernormCompute sum = ElementLayernormCompute(0);
|
||||
ElementLayernormCompute square_sum = ElementLayernormCompute(0);
|
||||
for (int m = 0; m < options.problem_size0.m(); ++m) {
|
||||
sum += ElementLayernormCompute(reference_C0.at({m, n}));
|
||||
square_sum += ElementLayernormCompute(reference_C0.at({m, n})) * ElementLayernormCompute(reference_C0.at({m, n}));
|
||||
}
|
||||
|
||||
ElementLayernormCompute mean = sum / ElementLayernormCompute(options.problem_size0.m());
|
||||
ElementLayernormCompute square_mean = square_sum / ElementLayernormCompute(options.problem_size0.m());
|
||||
ElementLayernormCompute variance = cutlass::constants::one<ElementLayernormCompute>() / cutlass::fast_sqrt(square_mean - mean * mean + ElementLayernormCompute(1e-6) ) ;
|
||||
|
||||
mean = -mean * variance;
|
||||
|
||||
reference_Mean.at({0, n}) = ElementInputScaleBias(mean);
|
||||
reference_Variance.at({0, n}) = ElementInputScaleBias(variance);
|
||||
}
|
||||
}else{
|
||||
for (int m = 0; m < options.problem_size0.m(); ++m) {
|
||||
|
||||
ElementLayernormCompute sum = ElementLayernormCompute(0);
|
||||
ElementLayernormCompute square_sum = ElementLayernormCompute(0);
|
||||
for (int n = 0; n < options.problem_size0.n(); ++n) {
|
||||
sum += ElementLayernormCompute(reference_C0.at({m, n})) ;
|
||||
square_sum += ElementLayernormCompute(reference_C0.at({m, n})) * ElementLayernormCompute(reference_C0.at({m, n})) ;
|
||||
}
|
||||
|
||||
ElementLayernormCompute mean = sum / ElementLayernormCompute(options.problem_size0.n());
|
||||
ElementLayernormCompute square_mean = square_sum / ElementLayernormCompute(options.problem_size0.n());
|
||||
ElementLayernormCompute variance = cutlass::constants::one<ElementLayernormCompute>() / cutlass::fast_sqrt(square_mean - mean * mean + ElementLayernormCompute(1e-6)) ;
|
||||
|
||||
mean = -mean * variance;
|
||||
|
||||
reference_Mean.at({0, m}) = ElementInputScaleBias(mean);
|
||||
reference_Variance.at({0, m}) = ElementInputScaleBias(variance);
|
||||
}
|
||||
}
|
||||
|
||||
// Element-wise transform for OutputC0 using 1-pass layernorm algo
|
||||
if (kIsColumnMajorOutput) {
|
||||
for (int n = 0; n < options.problem_size0.n(); ++n) {
|
||||
|
||||
ElementLayernormCompute sum = ElementLayernormCompute(0);
|
||||
for (int m = 0; m < options.problem_size0.m(); ++m) {
|
||||
sum += ElementLayernormCompute(reference_C0.at({m, n})) ;
|
||||
}
|
||||
|
||||
ElementInputScaleBias mean = ElementInputScaleBias(sum / ElementLayernormCompute(options.problem_size0.m()));
|
||||
sum = ElementLayernormCompute(0);
|
||||
for (int m = 0; m < options.problem_size0.m(); ++m) {
|
||||
sum += ElementLayernormCompute(reference_C0.at({m, n}) - ElementLayernormCompute(mean)) * ElementLayernormCompute(reference_C0.at({m, n}) - ElementLayernormCompute(mean)) ;
|
||||
}
|
||||
|
||||
ElementLayernormCompute square_mean = sum / ElementLayernormCompute(options.problem_size0.m());
|
||||
ElementInputScaleBias variance = ElementInputScaleBias(cutlass::constants::one<ElementLayernormCompute>()
|
||||
/ cutlass::fast_sqrt(square_mean + ElementLayernormCompute(1e-6))) ;
|
||||
|
||||
for (int m = 0; m < options.problem_size0.m(); ++m) {
|
||||
reference_C0.at({m, n}) =
|
||||
ElementOutput( ( (ElementInputScaleBias(reference_C0.at({m, n})) - mean) * variance )
|
||||
* tensor_Gamma.at({0, m}) + tensor_Beta.at({0, m}));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}else{
|
||||
|
||||
for (int m = 0; m < options.problem_size0.m(); ++m) {
|
||||
|
||||
float sum = float(0);
|
||||
for (int n = 0; n < options.problem_size0.n(); ++n) {
|
||||
sum += float(reference_C0.at({m, n})) ;
|
||||
}
|
||||
|
||||
float mean = sum / float(options.problem_size0.n());
|
||||
sum = float(0);
|
||||
for (int n = 0; n < options.problem_size0.n(); ++n) {
|
||||
sum += float(reference_C0.at({m, n}) - mean) * float(reference_C0.at({m, n}) - mean) ;
|
||||
}
|
||||
|
||||
float square_mean = sum / float(options.problem_size0.n());
|
||||
float variance = cutlass::constants::one<float>() / cutlass::fast_sqrt(square_mean + ElementLayernormCompute(1e-6)) ;
|
||||
|
||||
for (int n = 0; n < options.problem_size0.n(); ++n) {
|
||||
reference_C0.at({m, n}) =
|
||||
ElementOutput( ( (float(reference_C0.at({m, n})) - mean) * variance )
|
||||
* float(tensor_Gamma.at({0, n})) + float(tensor_Beta.at({0, n})));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Sync host data with device after element-wise transform
|
||||
reference_C0.sync_device();
|
||||
|
||||
// Compute 2nd GEMM
|
||||
gemm_device1(
|
||||
options.problem_size1,
|
||||
ElementCompute(options.alpha),
|
||||
kIsColumnMajorOutput ? tensor_A1.device_ref() : reference_C0.device_ref(),
|
||||
kIsColumnMajorOutput ? reference_C0.device_ref() :tensor_A1.device_ref(),
|
||||
ElementCompute(options.beta),
|
||||
reference_C1.device_ref(),
|
||||
reference_C1.device_ref()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/// Emits all tensor values
|
||||
void emit_results() {
|
||||
std::cout << "tensor_C1 = \n" << tensor_C1.host_view() << "\n\n";
|
||||
std::cout << "Reference C1 = \n" << reference_C1.host_view() << "\n\n";
|
||||
std::cout << "Mean = \n" << tensor_Mean.host_view() << "\n\n";
|
||||
std::cout << "rsqrt(Variance) = \n" << tensor_Variance.host_view() << "\n\n";
|
||||
std::cout << "Reference Mean = \n" << reference_Mean.host_view() << "\n\n";
|
||||
std::cout << "Reference rsqrt(Variance) = \n" << reference_Variance.host_view() << "\n\n";
|
||||
}
|
||||
|
||||
template<typename Element, typename Layout>
|
||||
bool verify_tensor(cutlass::HostTensor<Element, Layout> tensor, \
|
||||
cutlass::HostTensor<Element, Layout> reference,
|
||||
int leading_dim0, int leading_dim1, bool is_print = false) {
|
||||
float const kThreshold = float(options.tolerance);
|
||||
float const kAbsThreshold = 0.5f;
|
||||
float const kRelativeThreshold = 0.1f;
|
||||
// Adds a constant bias to avoid being divided by '0'
|
||||
float const kBias = 1e-5f;
|
||||
int counter = 0;
|
||||
for (int m = 0; m < leading_dim0; m++) {
|
||||
for (int n = 0; n < leading_dim1; ++n) {
|
||||
float diff = (float)(tensor.at({m, n}) - reference.at({m, n}));
|
||||
float rel_diff = fabs(diff) / fabs(reference.at({m, n}) + kBias);
|
||||
if (fabs(diff) > kAbsThreshold && rel_diff > kRelativeThreshold) {
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float err_rate = float(counter) / (float(leading_dim0) * float(leading_dim1));
|
||||
return (err_rate < kThreshold);
|
||||
}
|
||||
|
||||
/// Verifies the reference matches
|
||||
bool verify() {
|
||||
|
||||
tensor_Variance.sync_host();
|
||||
tensor_Mean.sync_host();
|
||||
tensor_C1.sync_host();
|
||||
reference_C1.sync_host();
|
||||
|
||||
// Verification checks - set any of these to 'true' to override the verification checks.
|
||||
bool verified_C1 = false;
|
||||
bool verified_Mean = false;
|
||||
bool verified_Variance = false;
|
||||
|
||||
// Verify layernorm output
|
||||
if (!verified_C1) {
|
||||
verified_C1 = verify_tensor<ElementOutputC1, LayoutOutputC1>(tensor_C1, reference_C1, options.problem_size1.m(), options.problem_size1.n());
|
||||
}
|
||||
|
||||
if (!verified_Variance) {
|
||||
verified_Variance = verify_tensor<ElementInputScaleBias, LayoutInputScaleBias>(tensor_Variance, reference_Variance, 1, options.problem_size0.n());
|
||||
}
|
||||
|
||||
if (!verified_Mean) {
|
||||
verified_Mean = verify_tensor<ElementInputScaleBias, LayoutInputScaleBias>(tensor_Mean, reference_Mean, 1, options.problem_size0.n());
|
||||
}
|
||||
|
||||
if (!verified_C1 || !verified_Mean || !verified_Variance) {
|
||||
|
||||
// emit_results();
|
||||
|
||||
std::cerr << "Verification check failed for tensor Layernorm" << std::endl;
|
||||
|
||||
// Summarize which checks failed
|
||||
if (!verified_C1) {
|
||||
std::cerr << "Verification of O tensor failed\n";
|
||||
}
|
||||
|
||||
if (!verified_Mean) {
|
||||
std::cerr << "Verification of Mean tensor failed\n";
|
||||
}
|
||||
|
||||
if (!verified_Variance) {
|
||||
std::cerr << "Verification of Variance tensor failed\n";
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Profiles
|
||||
bool profile() {
|
||||
|
||||
//
|
||||
// Profile
|
||||
//
|
||||
|
||||
cutlass::Status status = cutlass::Status::kSuccess;
|
||||
cudaError_t result;
|
||||
cudaEvent_t events[2];
|
||||
int const kIterations = options.iterations;
|
||||
|
||||
for (cudaEvent_t &evt : events) {
|
||||
result = cudaEventCreate(&evt);
|
||||
if (result != cudaSuccess) {
|
||||
std::cerr << "cudaEventCreate failed with error " << cudaGetErrorString(result) << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
result = cudaEventRecord(events[0]);
|
||||
|
||||
if (result != cudaSuccess) {
|
||||
std::cerr << "cudaEventRecord() failed with error " << cudaGetErrorString(result) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int iter = 0; iter < kIterations; ++iter) {
|
||||
|
||||
status = execute_device_kernel();
|
||||
|
||||
if (status != cutlass::Status::kSuccess) {
|
||||
std::cerr << "Device execution failed." << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
result = cudaEventRecord(events[1]);
|
||||
|
||||
if (result != cudaSuccess) {
|
||||
std::cerr << "cudaEventRecord() failed with error " << cudaGetErrorString(result) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = cudaDeviceSynchronize();
|
||||
|
||||
if (result != cudaSuccess) {
|
||||
std::cerr << "cudaDeviceSynchronize() failed with error " << cudaGetErrorString(result) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
float elapsed_ms = 0;
|
||||
result = cudaEventElapsedTime(&elapsed_ms, events[0], events[1]);
|
||||
|
||||
float elapsed_ms_per_iter = elapsed_ms / float(kIterations);
|
||||
|
||||
if (result != cudaSuccess) {
|
||||
std::cerr << "cudaEventElapsedTime() failed with error " << cudaGetErrorString(result) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (cudaEvent_t &evt : events) {
|
||||
result = cudaEventDestroy(evt);
|
||||
if (result != cudaSuccess) {
|
||||
std::cerr << "cudaEventDestroy() failed with error " << cudaGetErrorString(result) << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int64_t flops = int64_t(options.problem_size0.m()) * options.problem_size0.n() * options.problem_size0.k() * 2 \
|
||||
+ int64_t(options.problem_size1.m()) * options.problem_size1.n() * options.problem_size1.k() * 2;
|
||||
|
||||
double gflops_per_second = double(flops) * kIterations / double(elapsed_ms / 1000.0f) / double(1.0e9);
|
||||
|
||||
std::cout << " 1st GEMM: "
|
||||
<< options.problem_size0.m() << "-by-" << options.problem_size0.n() << "-by-" << options.problem_size0.k() << "\n"
|
||||
<< " 2nd GEMM: "
|
||||
<< options.problem_size1.m() << "-by-" << options.problem_size1.n() << "-by-" << options.problem_size1.k()
|
||||
<< std::endl;
|
||||
|
||||
std::cout << " Runtime / iteration: " << elapsed_ms_per_iter << " ms\n" << std::endl;
|
||||
std::cout << " GFLOPs: " << gflops_per_second << " GFLOPs" << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
|
||||
// Define final layout
|
||||
using LayoutOutput = cutlass::layout::ColumnMajor;
|
||||
|
||||
// Options parsing
|
||||
Options<LayoutOutput> options;
|
||||
options.parse(argc, argv);
|
||||
|
||||
if (options.help) {
|
||||
options.print_usage(std::cout) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!options.supported()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Run
|
||||
Testbed<LayoutOutput> testbed(options);
|
||||
|
||||
Disposition disposition = testbed.run();
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
switch (disposition) {
|
||||
case Disposition::kPassed:
|
||||
std::cout << "Passed" << std::endl;
|
||||
break;
|
||||
case Disposition::kIncorrect:
|
||||
std::cout << "Incorrect" << std::endl;
|
||||
break;
|
||||
case Disposition::kNotVerified:
|
||||
std::cout << "Not verified" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
return (disposition == Disposition::kPassed ? 0 : -1);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -0,0 +1,450 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright (c) 2017 - 2022 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 GEMM kernel to support the epilogue visitor model
|
||||
for customized layernorm partial reduction epilogue fusion.
|
||||
|
||||
This source file will likely be moved to `include/cutlass/gemm/kernel/` in the future once
|
||||
its usage has been stabilized. For now, it is included in this example to demonstrate
|
||||
some basic output fusion options.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cutlass/cutlass.h"
|
||||
#include "cutlass/fast_math.h"
|
||||
#include "cutlass/gemm/gemm.h"
|
||||
#include "cutlass/matrix_coord.h"
|
||||
#include "cutlass/complex.h"
|
||||
#include "cutlass/semaphore.h"
|
||||
|
||||
#include "cutlass/trace.h"
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace cutlass {
|
||||
namespace gemm {
|
||||
namespace kernel {
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <
|
||||
typename Mma_, ///! Threadblock-scoped matrix multiply-accumulate
|
||||
typename Epilogue_, ///! Epilogue
|
||||
typename ThreadblockSwizzle_ ///! Threadblock swizzling function
|
||||
>
|
||||
struct GemmWithEpilogueVisitor {
|
||||
public:
|
||||
|
||||
using Mma = Mma_;
|
||||
using Epilogue = Epilogue_;
|
||||
using EpilogueVisitor = typename Epilogue::Visitor;
|
||||
using ThreadblockSwizzle = ThreadblockSwizzle_;
|
||||
|
||||
using ElementA = typename Mma::IteratorA::Element;
|
||||
using LayoutA = typename Mma::IteratorA::Layout;
|
||||
using TensorRefA = TensorRef<ElementA, LayoutA>;
|
||||
|
||||
using ElementB = typename Mma::IteratorB::Element;
|
||||
using LayoutB = typename Mma::IteratorB::Layout;
|
||||
using TensorRefB = TensorRef<ElementB, LayoutB>;
|
||||
|
||||
using ElementC = typename EpilogueVisitor::ElementOutput;
|
||||
using LayoutC = typename Epilogue::Layout;
|
||||
|
||||
static ComplexTransform const kTransformA = Mma::kTransformA;
|
||||
static ComplexTransform const kTransformB = Mma::kTransformB;
|
||||
using Operator = typename Mma::Operator;
|
||||
|
||||
using OperatorClass = typename Mma::Operator::OperatorClass;
|
||||
using ThreadblockShape = typename Mma::Shape;
|
||||
using WarpShape = typename Mma::Operator::Shape;
|
||||
using InstructionShape = typename Mma::Policy::Operator::InstructionShape;
|
||||
using ArchTag = typename Mma::ArchTag;
|
||||
|
||||
static int const kStages = Mma::kStages;
|
||||
static int const kAlignmentA = Mma::IteratorA::AccessType::kElements;
|
||||
static int const kAlignmentB = Mma::IteratorB::AccessType::kElements;
|
||||
static int const kAlignmentC = EpilogueVisitor::kElementsPerAccess;
|
||||
|
||||
/// Warp count (concept: GemmShape)
|
||||
using WarpCount = typename Mma::WarpCount;
|
||||
static int const kThreadCount = 32 * WarpCount::kCount;
|
||||
|
||||
/// Split-K preserves splits that are 128b aligned
|
||||
static int const kSplitKAlignment = const_max(
|
||||
128 / sizeof_bits<ElementA>::value,
|
||||
128 / sizeof_bits<ElementB>::value
|
||||
);
|
||||
|
||||
//
|
||||
// Structures
|
||||
//
|
||||
|
||||
/// Argument structure
|
||||
struct Arguments {
|
||||
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
GemmUniversalMode mode;
|
||||
GemmCoord problem_size;
|
||||
|
||||
TensorRefA ref_A;
|
||||
TensorRefB ref_B;
|
||||
|
||||
typename EpilogueVisitor::Arguments epilogue_visitor;
|
||||
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
Arguments():
|
||||
mode(GemmUniversalMode::kGemm)
|
||||
{ }
|
||||
|
||||
|
||||
/// constructs an arguments structure
|
||||
Arguments(
|
||||
GemmUniversalMode mode_,
|
||||
GemmCoord problem_size_,
|
||||
TensorRefA ref_A_,
|
||||
TensorRefB ref_B_,
|
||||
typename EpilogueVisitor::Arguments epilogue_visitor_
|
||||
):
|
||||
mode(mode_),
|
||||
problem_size(problem_size_),
|
||||
ref_A(ref_A_),
|
||||
ref_B(ref_B_),
|
||||
epilogue_visitor(epilogue_visitor_)
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Structure for precomputing values in host memory and passing to kernels
|
||||
//
|
||||
|
||||
/// Parameters structure
|
||||
struct Params {
|
||||
|
||||
cutlass::gemm::GemmCoord problem_size;
|
||||
cutlass::gemm::GemmCoord grid_tiled_shape;
|
||||
int swizzle_log_tile;
|
||||
|
||||
typename Mma::IteratorA::Params params_A;
|
||||
typename Mma::IteratorB::Params params_B;
|
||||
|
||||
GemmUniversalMode mode;
|
||||
int gemm_k_size;
|
||||
|
||||
void * ptr_A;
|
||||
void * ptr_B;
|
||||
|
||||
typename EpilogueVisitor::Params epilogue_visitor;
|
||||
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
CUTLASS_HOST_DEVICE
|
||||
Params():
|
||||
swizzle_log_tile(0),
|
||||
params_A(0),
|
||||
params_B(0),
|
||||
gemm_k_size(0),
|
||||
mode(cutlass::gemm::GemmUniversalMode::kGemm),
|
||||
ptr_A(nullptr),
|
||||
ptr_B(nullptr)
|
||||
{ }
|
||||
|
||||
|
||||
Params(
|
||||
Arguments const &args
|
||||
):
|
||||
problem_size(args.problem_size),
|
||||
swizzle_log_tile(0),
|
||||
params_A(args.ref_A.layout()),
|
||||
params_B(args.ref_B.layout()),
|
||||
mode(args.mode),
|
||||
gemm_k_size(args.problem_size.k()),
|
||||
ptr_A(args.ref_A.data()),
|
||||
ptr_B(args.ref_B.data()),
|
||||
epilogue_visitor(args.epilogue_visitor)
|
||||
{
|
||||
|
||||
ThreadblockSwizzle threadblock_swizzle;
|
||||
|
||||
grid_tiled_shape = threadblock_swizzle.get_tiled_shape(
|
||||
args.problem_size,
|
||||
{ThreadblockShape::kM, ThreadblockShape::kN, ThreadblockShape::kK}, 1);
|
||||
|
||||
if (args.mode == GemmUniversalMode::kGemm || args.mode == GemmUniversalMode::kGemmSplitKParallel) {
|
||||
|
||||
int const kAlignK = const_max(const_max(128 / sizeof_bits<ElementA>::value, 128 / sizeof_bits<ElementB>::value), 1);
|
||||
|
||||
gemm_k_size = round_up(args.problem_size.k(), kAlignK);
|
||||
|
||||
if (gemm_k_size) {
|
||||
grid_tiled_shape.k() = ceil_div(args.problem_size.k(), gemm_k_size);
|
||||
}
|
||||
}
|
||||
|
||||
swizzle_log_tile = threadblock_swizzle.get_log_tile(grid_tiled_shape);
|
||||
}
|
||||
};
|
||||
|
||||
/// Shared memory storage structure
|
||||
union SharedStorage {
|
||||
|
||||
typename Mma::SharedStorage main_loop;
|
||||
|
||||
struct {
|
||||
typename Epilogue::SharedStorage epilogue;
|
||||
typename EpilogueVisitor::SharedStorage visitor;
|
||||
} epilogue;
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
CUTLASS_DEVICE
|
||||
GemmWithEpilogueVisitor() { }
|
||||
|
||||
/// Determines whether kernel satisfies alignment
|
||||
static Status can_implement(
|
||||
cutlass::gemm::GemmCoord const & problem_size) {
|
||||
|
||||
CUTLASS_TRACE_HOST("GemmWithEpilogueVisitor::can_implement()");
|
||||
|
||||
static int const kAlignmentA = Mma::IteratorA::AccessType::kElements;
|
||||
static int const kAlignmentB = Mma::IteratorB::AccessType::kElements;
|
||||
static int const kAlignmentC = Epilogue::OutputTileIterator::kElementsPerAccess;
|
||||
|
||||
bool isAMisaligned = false;
|
||||
bool isBMisaligned = false;
|
||||
bool isCMisaligned = false;
|
||||
|
||||
if (platform::is_same<LayoutA, layout::RowMajor>::value) {
|
||||
isAMisaligned = problem_size.k() % kAlignmentA;
|
||||
} else if (platform::is_same<LayoutA, layout::ColumnMajor>::value) {
|
||||
isAMisaligned = problem_size.m() % kAlignmentA;
|
||||
} else if (platform::is_same<LayoutA, layout::ColumnMajorInterleaved<32>>::value
|
||||
|| platform::is_same<LayoutA, layout::ColumnMajorInterleaved<64>>::value) {
|
||||
isAMisaligned = problem_size.k() % kAlignmentA;
|
||||
}
|
||||
|
||||
if (platform::is_same<LayoutB, layout::RowMajor>::value) {
|
||||
isBMisaligned = problem_size.n() % kAlignmentB;
|
||||
} else if (platform::is_same<LayoutB, layout::ColumnMajor>::value) {
|
||||
isBMisaligned = problem_size.k() % kAlignmentB;
|
||||
} else if (platform::is_same<LayoutB, layout::RowMajorInterleaved<32>>::value
|
||||
|| platform::is_same<LayoutB, layout::RowMajorInterleaved<64>>::value) {
|
||||
isBMisaligned = problem_size.k() % kAlignmentB;
|
||||
}
|
||||
|
||||
if (platform::is_same<LayoutC, layout::RowMajor>::value) {
|
||||
isCMisaligned = problem_size.n() % kAlignmentC;
|
||||
} else if (platform::is_same<LayoutC, layout::ColumnMajor>::value) {
|
||||
isCMisaligned = problem_size.m() % kAlignmentC;
|
||||
} else if (platform::is_same<LayoutC, layout::ColumnMajorInterleaved<32>>::value
|
||||
|| platform::is_same<LayoutC, layout::ColumnMajorInterleaved<64>>::value) {
|
||||
isCMisaligned = problem_size.n() % kAlignmentC;
|
||||
}
|
||||
|
||||
if (isAMisaligned) {
|
||||
CUTLASS_TRACE_HOST(" returning kErrorMisalignedOperand for A operand");
|
||||
return Status::kErrorMisalignedOperand;
|
||||
}
|
||||
|
||||
if (isBMisaligned) {
|
||||
CUTLASS_TRACE_HOST(" returning kErrorMisalignedOperand for B operand");
|
||||
return Status::kErrorMisalignedOperand;
|
||||
}
|
||||
|
||||
if (isCMisaligned) {
|
||||
CUTLASS_TRACE_HOST(" returning kErrorMisalignedOperand for C operand");
|
||||
return Status::kErrorMisalignedOperand;
|
||||
}
|
||||
|
||||
CUTLASS_TRACE_HOST(" returning kSuccess");
|
||||
|
||||
return Status::kSuccess;
|
||||
}
|
||||
|
||||
static Status can_implement(Arguments const &args) {
|
||||
return can_implement(args.problem_size);
|
||||
}
|
||||
|
||||
static size_t get_extra_workspace_size(Arguments const &args,
|
||||
cutlass::gemm::GemmCoord const &grid_tiled_shape) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// Executes one GEMM
|
||||
CUTLASS_DEVICE
|
||||
void operator()(Params const ¶ms, SharedStorage &shared_storage) {
|
||||
|
||||
// Compute threadblock location
|
||||
ThreadblockSwizzle threadblock_swizzle;
|
||||
|
||||
cutlass::gemm::GemmCoord threadblock_tile_offset = threadblock_swizzle.get_tile_offset(params.swizzle_log_tile);
|
||||
|
||||
// Early exit if CTA is out of range
|
||||
if (params.grid_tiled_shape.m() <= threadblock_tile_offset.m() ||
|
||||
params.grid_tiled_shape.n() <= threadblock_tile_offset.n()) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int offset_k = 0;
|
||||
int problem_size_k = params.problem_size.k();
|
||||
|
||||
ElementA *ptr_A = static_cast<ElementA *>(params.ptr_A);
|
||||
ElementB *ptr_B = static_cast<ElementB *>(params.ptr_B);
|
||||
|
||||
// Compute initial location in logical coordinates
|
||||
cutlass::MatrixCoord tb_offset_A{
|
||||
threadblock_tile_offset.m() * Mma::Shape::kM,
|
||||
offset_k,
|
||||
};
|
||||
|
||||
cutlass::MatrixCoord tb_offset_B{
|
||||
offset_k,
|
||||
threadblock_tile_offset.n() * Mma::Shape::kN
|
||||
};
|
||||
|
||||
// Compute position within threadblock
|
||||
int thread_idx = threadIdx.x;
|
||||
|
||||
// Construct iterators to A and B operands
|
||||
typename Mma::IteratorA iterator_A(
|
||||
params.params_A,
|
||||
ptr_A,
|
||||
{params.problem_size.m(), problem_size_k},
|
||||
thread_idx,
|
||||
tb_offset_A);
|
||||
|
||||
typename Mma::IteratorB iterator_B(
|
||||
params.params_B,
|
||||
ptr_B,
|
||||
{problem_size_k, params.problem_size.n()},
|
||||
thread_idx,
|
||||
tb_offset_B);
|
||||
|
||||
// Broadcast the warp_id computed by lane 0 to ensure dependent code
|
||||
// is compiled as warp-uniform.
|
||||
int warp_idx = __shfl_sync(0xffffffff, threadIdx.x / 32, 0);
|
||||
|
||||
int lane_idx = threadIdx.x % 32;
|
||||
|
||||
//
|
||||
// Main loop
|
||||
//
|
||||
|
||||
// Construct thread-scoped matrix multiply
|
||||
Mma mma(shared_storage.main_loop, thread_idx, warp_idx, lane_idx);
|
||||
|
||||
typename Mma::FragmentC accumulators;
|
||||
|
||||
accumulators.clear();
|
||||
|
||||
// Compute threadblock-scoped matrix multiply-add
|
||||
int gemm_k_iterations = (problem_size_k - offset_k + Mma::Shape::kK - 1) / Mma::Shape::kK;
|
||||
|
||||
// Compute threadblock-scoped matrix multiply-add
|
||||
mma(
|
||||
gemm_k_iterations,
|
||||
accumulators,
|
||||
iterator_A,
|
||||
iterator_B,
|
||||
accumulators);
|
||||
|
||||
//
|
||||
// Masked tile iterators constructed from members
|
||||
//
|
||||
|
||||
threadblock_tile_offset = threadblock_swizzle.get_tile_offset(params.swizzle_log_tile);
|
||||
|
||||
//assume identity swizzle
|
||||
MatrixCoord threadblock_offset(
|
||||
threadblock_tile_offset.m() * Mma::Shape::kM,
|
||||
threadblock_tile_offset.n() * Mma::Shape::kN
|
||||
);
|
||||
|
||||
int block_idx = threadblock_tile_offset.m() + threadblock_tile_offset.n() * params.grid_tiled_shape.m();
|
||||
|
||||
//
|
||||
// Construct the epilogue visitor
|
||||
//
|
||||
|
||||
EpilogueVisitor epilogue_visitor(
|
||||
params.epilogue_visitor,
|
||||
shared_storage.epilogue.visitor,
|
||||
params.problem_size.mn(),
|
||||
thread_idx,
|
||||
warp_idx,
|
||||
lane_idx,
|
||||
threadblock_offset);
|
||||
|
||||
if (params.mode == GemmUniversalMode::kGemm) {
|
||||
// Indicate which position in a serial reduction the output operator is currently updating
|
||||
epilogue_visitor.set_k_partition(threadblock_tile_offset.k(), params.grid_tiled_shape.k());
|
||||
}
|
||||
else if (params.mode == GemmUniversalMode::kBatched || params.mode == GemmUniversalMode::kArray) {
|
||||
epilogue_visitor.set_batch_index(threadblock_tile_offset.k());
|
||||
}
|
||||
|
||||
// Construct the epilogue
|
||||
Epilogue epilogue(
|
||||
shared_storage.epilogue.epilogue,
|
||||
thread_idx,
|
||||
warp_idx,
|
||||
lane_idx);
|
||||
|
||||
// Execute the epilogue operator to update the destination tensor.
|
||||
epilogue(epilogue_visitor, accumulators);
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
} // namespace kernel
|
||||
} // namespace gemm
|
||||
} // namespace cutlass
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user