CUTLASS 2.3 initial commit (#134)

CUTLASS 2.3 adds GEMMs targeting Sparse Tensor Cores on the NVIDIA Ampere Architecture, fast SGEMM, and small matrix classes, bug fixes, and performance enhancements.
This commit is contained in:
Andrew Kerr
2020-09-23 14:00:58 -07:00
committed by GitHub
parent 4dac7490e6
commit c53f3339bb
209 changed files with 46922 additions and 1677 deletions
+2
View File
@@ -27,6 +27,8 @@ cutlass_test_unit_add_executable(
bfloat16.cu
tfloat32.cu
complex.cu
quaternion.cu
matrix.cu
predicate_vector.cu
tensor_ref.cu
tensor_view.cu
+3
View File
@@ -142,6 +142,9 @@ TEST(bfloat16_t, host_conversion) {
EXPECT_TRUE(static_cast<float>(y) == f);
}
// Try out default-ctor (zero initialization of primitive proxy type)
EXPECT_TRUE(cutlass::bfloat16_t() == 0.0_bf16);
// Try out user-defined literals
EXPECT_TRUE(cutlass::bfloat16_t(7) == 7_bf16);
EXPECT_TRUE(7 == static_cast<int>(7_bf16));
+70 -3
View File
@@ -23,15 +23,16 @@
*
**************************************************************************************************/
/*! \file
\brief Statically sized array of elements that accommodates all CUTLASS-supported numeric types
and is safe to use in a union.
\brief CUTLASS host-device template for complex numbers supporting all CUTLASS numeric types.
*/
// Standard Library's std::complex<T> used for reference checking
#include <complex>
#include "../common/cutlass_unit_test.h"
#include "cutlass/complex.h"
#include "cutlass/numeric_conversion.h"
#include "cutlass/util/device_memory.h"
/////////////////////////////////////////////////////////////////////////////////////////////////
@@ -82,4 +83,70 @@ TEST(complex, f16_to_f32_conversion) {
dest.real() == 1.5f && dest.imag() == -1.25f);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace test {
/// Thorough testing for basic complex math operators. Uses std::complex as a reference.
template <typename T, int N, int M>
struct ComplexOperators {
ComplexOperators() {
for (int ar = -N; ar <= N; ++ar) {
for (int ai = -N; ai <= N; ++ai) {
for (int br = -N; br <= N; ++br) {
for (int bi = -N; bi <= N; ++bi) {
cutlass::complex<T> Ae(T(ar) / T(M), T(ai) / T(M));
cutlass::complex<T> Be(T(br) / T(M), T(bi) / T(M));
std::complex<T> Ar(T(ar) / T(M), T(ai) / T(M));
std::complex<T> Br(T(br) / T(M), T(bi) / T(M));
cutlass::complex<T> add_e = Ae + Be;
cutlass::complex<T> sub_e = Ae - Be;
cutlass::complex<T> mul_e = Ae * Be;
std::complex<T> add_r = (Ar + Br);
std::complex<T> sub_r = (Ar - Br);
std::complex<T> mul_r = (Ar * Br);
EXPECT_EQ(real(add_e), real(add_r));
EXPECT_EQ(imag(add_e), imag(add_r));
EXPECT_EQ(real(sub_e), real(sub_r));
EXPECT_EQ(imag(sub_e), imag(sub_r));
EXPECT_EQ(real(mul_e), real(mul_r));
EXPECT_EQ(imag(mul_e), imag(mul_r));
if (!(br == 0 && bi == 0)) {
cutlass::complex<T> div_e = Ae / Be;
std::complex<T> div_r = Ar / Br;
T const kRange = T(0.001);
EXPECT_NEAR(real(div_e), real(div_r), kRange);
EXPECT_NEAR(imag(div_e), imag(div_r), kRange);
}
}
}
}
}
}
};
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(complex, host_float) {
test::ComplexOperators<float, 32, 8> test;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(complex, host_double) {
test::ComplexOperators<double, 32, 8> test;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
+3
View File
@@ -50,6 +50,9 @@ TEST(half_t, host_conversion) {
EXPECT_TRUE(static_cast<float>(y) == f);
}
// Try out default-ctor (zero initialization of primitive proxy type)
EXPECT_TRUE(cutlass::half_t() == 0.0_hf);
// Try out user-defined literals
EXPECT_TRUE(cutlass::half_t(7) == 7_hf);
EXPECT_TRUE(7 == static_cast<int>(7_hf));
+198
View File
@@ -0,0 +1,198 @@
/***************************************************************************************************
* Copyright (c) 2017-2020, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* * 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.
* * Neither the name of the NVIDIA CORPORATION 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 NVIDIA CORPORATION 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 TOR (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 Unit tests for the small matrix class.
*/
#include <iostream>
#include "../common/cutlass_unit_test.h"
#include "cutlass/matrix.h"
/////////////////////////////////////////////////////////////////////////////////////////////////
TEST(Matrix, elementwise_add) {
using Matrix4x4 = cutlass::Matrix4x4<float>;
Matrix4x4 A = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
Matrix4x4 B = A.transpose();
Matrix4x4 C = A.add(B * 2.125f);
bool passed = true;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
float got = C.at(i, j);
float expected = A.at(i, j) + A.at(j, i) * 2.125f;
if (got != expected) {
passed = false;
}
}
}
EXPECT_TRUE(passed);
if (!passed) {
std::cout << "A:\n" << A << "\n\nB:\n" << B << "\n\nC:\n" << C << std::endl;
}
}
TEST(Matrix, elementwise_multiply) {
using Matrix4x4 = cutlass::Matrix4x4<float>;
Matrix4x4 A = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
Matrix4x4 B = A.transpose();
Matrix4x4 C = A.multiply(B);
bool passed = true;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
float got = C.at(i, j);
float expected = A.at(i, j) * A.at(j, i);
if (got != expected) {
passed = false;
}
}
}
EXPECT_TRUE(passed);
if (!passed) {
std::cout << "A:\n" << A << "\n\nB:\n" << B << "\n\nC:\n" << C << std::endl;
}
}
TEST(Matrix, product_4x4_overloads) {
using Matrix4x4 = cutlass::Matrix4x4<float>;
Matrix4x4 A = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
Matrix4x4 B = {
-1, -2, 0, 4,
1, 2, 1, 1,
3, 2, 1, 1,
1, 0, 8, 2
};
Matrix4x4 C = Matrix4x4::identity();
Matrix4x4 D = A * B + C;
bool passed = true;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
float got = D.at(i, j);
float expected = (i == j ? 1.0f : 0);
for (int k = 0; k < 4; ++k) {
expected += A.at(i, k) * B.at(k, j);
}
if (got != expected) {
passed = false;
}
}
}
EXPECT_TRUE(passed);
if (!passed) {
std::cout << "A:\n" << A << "\n\nB:\n" << B << "\n\nC:\n" << C << "\n\nD:\n" << D << std::endl;
}
}
TEST(Matrix, product_4x4) {
using Matrix4x4 = cutlass::Matrix4x4<float>;
Matrix4x4 A = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
Matrix4x4 B = {
-1, -2, 0, 4,
1, 2, 1, 1,
3, 2, 1, 1,
1, 0, 8, 2
};
Matrix4x4 C = Matrix4x4::identity();
// Compute product with optional source accumulator
Matrix4x4 D = A.product(B, C);
bool passed = true;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
float got = D.at(i, j);
float expected = (i == j ? 1.0f : 0.0f);
for (int k = 0; k < 4; ++k) {
expected += A.at(i, k) * B.at(k, j);
}
if (got != expected) {
passed = false;
}
}
}
EXPECT_TRUE(passed);
if (!passed) {
std::cout << "A:\n" << A << "\n\nB:\n" << B << "\n\nC:\n" << C << "\n\nD:\n" << D << std::endl;
}
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
float c = (i == j ? 1.0f : 0.0f);
EXPECT_TRUE(A.row(i).dot(B.column(j)) + c == D.at(i, j));
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
+162
View File
@@ -0,0 +1,162 @@
/***************************************************************************************************
* Copyright (c) 2017-2020, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* * 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.
* * Neither the name of the NVIDIA CORPORATION 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 NVIDIA CORPORATION 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 TOR (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 Unit tests for the CUTLASS Quaternion template class.
*/
#include "../common/cutlass_unit_test.h"
#include "cutlass/cutlass.h"
#include "cutlass/core_io.h"
#include "cutlass/quaternion.h"
#include "cutlass/numeric_conversion.h"
#include "cutlass/constants.h"
/////////////////////////////////////////////////////////////////////////////////////////////////
static float const half_pi = cutlass::constants::half_pi<float>();
/////////////////////////////////////////////////////////////////////////////////////////////////
TEST(Quaternion, add_f32) {
cutlass::Quaternion<float> q0(1, 1, 1, 1);
cutlass::Quaternion<float> q1(0, 0, 0, 2);
cutlass::Quaternion<float> q2 = q0 + q1;
EXPECT_TRUE(
q2.x() == 1 &&
q2.y() == 1 &&
q2.z() == 1 &&
q2.w() == 3
);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
TEST(Quaternion, rotation) {
cutlass::Matrix3x1<float> x(1.0f, 0.0f, 0.0f);
cutlass::Quaternion<float> q = cutlass::Quaternion<float>::rotation(0, 0, 1, half_pi) * 2.0f;
cutlass::Matrix3x1<float> v = q.rotate(x);
float epsilon = 0.001f;
EXPECT_TRUE(
std::abs(v.at(0)) < epsilon &&
std::abs(v.at(1)) > (1 - epsilon) &&
std::abs(v.at(2)) < epsilon
);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
TEST(Quaternion, rotation_inv) {
cutlass::Matrix3x1<float> x(1.0f, 0.0f, 0.0f);
cutlass::Quaternion<float> q = cutlass::Quaternion<float>::rotation(0, 0, 1, half_pi) * 2.0f;
cutlass::Matrix3x1<float> v = q.rotate(x);
float epsilon = 0.001f;
EXPECT_TRUE(
std::abs(v.at(0)) < epsilon &&
std::abs(-v.at(1)) > (1 - epsilon) &&
std::abs(v.at(2)) < epsilon
);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
TEST(Quaternion, spinor_rotation) {
cutlass::Matrix3x1<float> x(1.0f, 0.0f, 0.0f);
cutlass::Quaternion<float> q = cutlass::Quaternion<float>::rotation(0, 0, 1, half_pi);
cutlass::Matrix3x1<float> v = cutlass::spinor_rotation(q, x);
float epsilon = 0.001f;
EXPECT_TRUE(
std::abs(v.at(0)) < epsilon &&
std::abs(v.at(1)) > (1 - epsilon) &&
std::abs(v.at(2)) < epsilon
);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
TEST(Quaternion, spinor_rotation_inv) {
cutlass::Matrix3x1<float> x(1.0f, 0.0f, 0.0f);
cutlass::Quaternion<float> q = cutlass::Quaternion<float>::rotation(0, 0, 1, half_pi);
cutlass::Matrix3x1<float> v = cutlass::spinor_rotation_inv(q, x);
float epsilon = 0.001f;
EXPECT_TRUE(
std::abs(v.at(0)) < epsilon &&
std::abs(-v.at(1)) > (1 - epsilon) &&
std::abs(v.at(2)) < epsilon
);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
TEST(Quaternion, as_rotation_matrix3x3) {
cutlass::Matrix3x1<float> x(1.0f, 0.0f, 0.0f);
cutlass::Quaternion<float> q = cutlass::Quaternion<float>::rotation(0, 0, 1, half_pi);
cutlass::Matrix3x1<float> v = q.as_rotation_matrix_3x3().product(x);
float epsilon = 0.001f;
EXPECT_TRUE(
std::abs(v.at(0)) < epsilon &&
std::abs(v.at(1)) > (1 - epsilon) &&
std::abs(v.at(2)) < epsilon
);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
TEST(Quaternion, as_rotation_matrix4x4) {
cutlass::Matrix4x1<float> x(1.0f, 0.0f, 0.0f, 1.0f);
cutlass::Quaternion<float> q = cutlass::Quaternion<float>::rotation(0, 0, 1, half_pi);
cutlass::Matrix4x1<float> v = q.as_rotation_matrix_4x4().product(x);
float epsilon = 0.001f;
EXPECT_TRUE(
std::abs(v.at(0)) < epsilon &&
std::abs(v.at(1)) > (1 - epsilon) &&
std::abs(v.at(2)) < epsilon &&
std::abs(v.at(3)) > (1 - epsilon)
);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
+6 -6
View File
@@ -114,9 +114,9 @@ TEST(TensorView, contiguous) {
int32_t,
cutlass::layout::ContiguousLayout> ContiguousTensorView;
cutlass::MatrixLayout layouts[] = {
cutlass::MatrixLayout::kColumnMajor,
cutlass::MatrixLayout::kRowMajor
cutlass::layout::Matrix layouts[] = {
cutlass::layout::Matrix::kColumnMajor,
cutlass::layout::Matrix::kRowMajor
};
cutlass::Coord<2> bounds = cutlass::make_Coord(M, N);
@@ -129,7 +129,7 @@ TEST(TensorView, contiguous) {
int row_stride;
int col_stride;
if (layouts[i] == cutlass::MatrixLayout::kColumnMajor) {
if (layouts[i] == cutlass::layout::Matrix::kColumnMajor) {
row_stride = 1;
col_stride = M;
ldm = col_stride;
@@ -156,7 +156,7 @@ TEST(TensorView, contiguous) {
}
std::cout << "---------\n";
std::cout << (layouts[i] == cutlass::MatrixLayout::kColumnMajor ?
std::cout << (layouts[i] == cutlass::layout::Matrix::kColumnMajor ?
"Column-major:" : "Row-major:") << "\n\n";
std::cout << "Logical view:\n";
@@ -165,7 +165,7 @@ TEST(TensorView, contiguous) {
std::cout << "Linear memory:";
for (int idx = 0; idx < view.capacity(); ++idx) {
if (!(idx % (layouts[i] == cutlass::MatrixLayout::kColumnMajor ? M : N))) {
if (!(idx % (layouts[i] == cutlass::layout::Matrix::kColumnMajor ? M : N))) {
std::cout << std::endl;
}
std::cout << std::setw(4) << view.at(idx) << " ";
+3
View File
@@ -51,6 +51,9 @@ TEST(tfloat32_t, host_conversion) {
EXPECT_TRUE(static_cast<float>(y) == f);
}
// Try out default-ctor (zero initialization of primitive proxy type)
EXPECT_TRUE(cutlass::tfloat32_t() == 0.0_tf32);
// Try out user-defined literals
EXPECT_TRUE(cutlass::tfloat32_t(7) == 7_tf32);
EXPECT_TRUE(7 == static_cast<int>(7_tf32));