CUTLASS 2.0 (#62)
CUTLASS 2.0 Substantially refactored for - Better performance, particularly for native Turing Tensor Cores - Robust and durable templates spanning the design space - Encapsulated functionality embodying modern C++11 programming techniques - Optimized containers and data types for efficient, generic, portable device code Updates to: - Quick start guide - Documentation - Utilities - CUTLASS Profiler Native Turing Tensor Cores - Efficient GEMM kernels targeting Turing Tensor Cores - Mixed-precision floating point, 8-bit integer, 4-bit integer, and binarized operands Coverage of existing CUTLASS functionality: - GEMM kernels targeting CUDA and Tensor Cores in NVIDIA GPUs - Volta Tensor Cores through native mma.sync and through WMMA API - Optimizations such as parallel reductions, threadblock rasterization, and intra-threadblock reductions - Batched GEMM operations - Complex-valued GEMMs Note: this commit and all that follow require a host compiler supporting C++11 or greater.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
# Copyright (c) 2017-2019, 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.
|
||||
|
||||
cutlass_test_unit_add_executable(
|
||||
cutlass_test_unit_core
|
||||
array.cu
|
||||
half.cu
|
||||
complex.cu
|
||||
predicate_vector.cu
|
||||
tensor_ref.cu
|
||||
tensor_view.cu
|
||||
matrix_coord.cu
|
||||
numeric_conversion.cu
|
||||
functional.cu
|
||||
)
|
||||
@@ -0,0 +1,247 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright (c) 2017-2019, 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 Statically sized array of elements that accommodates all CUTLASS-supported numeric types
|
||||
and is safe to use in a union.
|
||||
*/
|
||||
|
||||
#include "../common/cutlass_unit_test.h"
|
||||
|
||||
#include "cutlass/array.h"
|
||||
#include "cutlass/util/device_memory.h"
|
||||
#pragma warning( disable : 4800)
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace test {
|
||||
namespace core {
|
||||
|
||||
/// Each thread clears its array and writes to global memory. No PRMT instructions should
|
||||
/// be generated if Array<T, N> is a multiple of 32 bits.
|
||||
template <typename T, int N>
|
||||
__global__ void test_array_clear(cutlass::Array<T, N> *ptr) {
|
||||
|
||||
cutlass::Array<T, N> storage;
|
||||
|
||||
storage.clear();
|
||||
|
||||
ptr[threadIdx.x] = storage;
|
||||
}
|
||||
|
||||
/// Each thread writes its thread index into the elements of its array and then writes the result
|
||||
/// to global memory.
|
||||
template <typename T, int N>
|
||||
__global__ void test_array_threadid(cutlass::Array<T, N> *ptr) {
|
||||
|
||||
cutlass::Array<T, N> storage;
|
||||
|
||||
CUTLASS_PRAGMA_UNROLL
|
||||
for (int i = 0; i < N; ++i) {
|
||||
storage.at(i) = T(int(threadIdx.x));
|
||||
}
|
||||
|
||||
ptr[threadIdx.x] = storage;
|
||||
}
|
||||
|
||||
/// Each thread writes its thread index into the elements of its array and then writes the result
|
||||
/// to global memory.
|
||||
template <typename T, int N>
|
||||
__global__ void test_array_sequence(cutlass::Array<T, N> *ptr) {
|
||||
|
||||
cutlass::Array<T, N> storage;
|
||||
|
||||
CUTLASS_PRAGMA_UNROLL
|
||||
for (int i = 0; i < N; ++i) {
|
||||
storage.at(i) = T(i);
|
||||
}
|
||||
|
||||
ptr[threadIdx.x] = storage;
|
||||
}
|
||||
|
||||
} // namespace core
|
||||
} // namespace test
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <typename T, int N>
|
||||
class TestArray {
|
||||
public:
|
||||
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Number of threads
|
||||
int const kThreads = 32;
|
||||
|
||||
typedef cutlass::Array<T, N> ArrayTy;
|
||||
|
||||
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Ctor
|
||||
TestArray() {
|
||||
|
||||
}
|
||||
|
||||
/// Runs the test
|
||||
void run() {
|
||||
|
||||
/// Device memory containing output
|
||||
cutlass::device_memory::allocation< ArrayTy > output(kThreads);
|
||||
std::vector< ArrayTy > output_host(kThreads);
|
||||
|
||||
dim3 grid(1,1);
|
||||
dim3 block(kThreads, 1, 1);
|
||||
|
||||
test::core::test_array_clear<<< grid, block >>>(output.get());
|
||||
|
||||
cudaError_t result = cudaDeviceSynchronize();
|
||||
ASSERT_EQ(result, cudaSuccess) << "CUDA error: " << cudaGetErrorString(result);
|
||||
|
||||
//
|
||||
// Verify contains all zeros
|
||||
//
|
||||
|
||||
cutlass::device_memory::copy_to_host(output_host.data(), output.get(), kThreads);
|
||||
|
||||
result = cudaGetLastError();
|
||||
ASSERT_EQ(result, cudaSuccess) << "CUDA error: " << cudaGetErrorString(result);
|
||||
|
||||
char const *ptr_host = reinterpret_cast<char const *>(output_host.data());
|
||||
for (int i = 0; i < sizeof(ArrayTy) * kThreads; ++i) {
|
||||
EXPECT_FALSE(ptr_host[i]);
|
||||
}
|
||||
|
||||
//
|
||||
// Verify each element contains the low bits of the thread Id
|
||||
//
|
||||
|
||||
test::core::test_array_threadid<<< grid, block >>>(output.get());
|
||||
|
||||
result = cudaDeviceSynchronize();
|
||||
ASSERT_EQ(result, cudaSuccess) << "CUDA error: " << cudaGetErrorString(result);
|
||||
|
||||
cutlass::device_memory::copy_to_host(output_host.data(), output.get(), kThreads);
|
||||
|
||||
result = cudaGetLastError();
|
||||
ASSERT_EQ(result, cudaSuccess) << "CUDA error: " << cudaGetErrorString(result);
|
||||
|
||||
for (int i = 0; i < kThreads; ++i) {
|
||||
T tid = T(i);
|
||||
|
||||
ArrayTy thread = output_host.at(i);
|
||||
|
||||
// Element-wise access
|
||||
for (int j = 0; j < N; ++j) {
|
||||
EXPECT_TRUE(tid == thread[j]);
|
||||
}
|
||||
|
||||
// Iterator access
|
||||
for (auto it = thread.begin(); it != thread.end(); ++it) {
|
||||
EXPECT_TRUE(tid == *it);
|
||||
}
|
||||
|
||||
// Range-based for
|
||||
for (auto const & x : thread) {
|
||||
EXPECT_TRUE(tid == x);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Verify each element
|
||||
//
|
||||
|
||||
test::core::test_array_sequence<<< grid, block >>>(output.get());
|
||||
|
||||
result = cudaDeviceSynchronize();
|
||||
ASSERT_EQ(result, cudaSuccess) << "CUDA error: " << cudaGetErrorString(result);
|
||||
|
||||
cutlass::device_memory::copy_to_host(output_host.data(), output.get(), kThreads);
|
||||
|
||||
result = cudaGetLastError();
|
||||
ASSERT_EQ(result, cudaSuccess) << "CUDA error: " << cudaGetErrorString(result);
|
||||
|
||||
for (int i = 0; i < kThreads; ++i) {
|
||||
|
||||
ArrayTy thread = output_host.at(i);
|
||||
|
||||
// Element-wise access
|
||||
for (int j = 0; j < N; ++j) {
|
||||
T got = T(j);
|
||||
EXPECT_TRUE(got == thread[j]);
|
||||
}
|
||||
|
||||
// Iterator access
|
||||
int j = 0;
|
||||
for (auto it = thread.begin(); it != thread.end(); ++it, ++j) {
|
||||
T got = T(j);
|
||||
EXPECT_TRUE(got == *it);
|
||||
}
|
||||
|
||||
// Range-based for
|
||||
j = 0;
|
||||
for (auto const & x : thread) {
|
||||
T got = T(j);
|
||||
EXPECT_TRUE(got == x);
|
||||
++j;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
TEST(Array, Int8x16) {
|
||||
TestArray<int8_t, 16>().run();
|
||||
}
|
||||
|
||||
TEST(Array, Int32x4) {
|
||||
TestArray<int, 4>().run();
|
||||
}
|
||||
|
||||
#if __CUDA_ARCH__ >= 520
|
||||
TEST(Array, Float16x8) {
|
||||
TestArray<cutlass::half_t, 8>().run();
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(Array, Float32x4) {
|
||||
TestArray<float, 4>().run();
|
||||
}
|
||||
|
||||
TEST(Array, Int4x32) {
|
||||
TestArray<cutlass::int4b_t, 32>().run();
|
||||
}
|
||||
|
||||
TEST(Array, Uint4x32) {
|
||||
TestArray<cutlass::uint4b_t, 32>().run();
|
||||
}
|
||||
|
||||
TEST(Array, Bin1x128) {
|
||||
TestArray<cutlass::bin1_t, 128>().run();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -0,0 +1,85 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright (c) 2017-2019, 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 Statically sized array of elements that accommodates all CUTLASS-supported numeric types
|
||||
and is safe to use in a union.
|
||||
*/
|
||||
|
||||
#include "../common/cutlass_unit_test.h"
|
||||
|
||||
#include "cutlass/complex.h"
|
||||
#include "cutlass/numeric_conversion.h"
|
||||
#include "cutlass/util/device_memory.h"
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(complex, f64_to_f32_conversion) {
|
||||
|
||||
cutlass::complex<double> source = {1.5, -1.25};
|
||||
|
||||
cutlass::complex<float> dest = cutlass::complex<float>(source); // explicit conversion
|
||||
|
||||
EXPECT_TRUE(source.real() == 1.5 && source.imag() == -1.25 &&
|
||||
dest.real() == 1.5f && dest.imag() == -1.25f);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(complex, f32_to_f64_conversion) {
|
||||
|
||||
cutlass::complex<float> source = {-1.5f, 1.25f};
|
||||
|
||||
cutlass::complex<double> dest = source; // implicit conversion
|
||||
|
||||
EXPECT_TRUE(source.real() == -1.5f && source.imag() == 1.25f &&
|
||||
dest.real() == -1.5 && dest.imag() == 1.25);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(complex, s32_to_f64_conversion) {
|
||||
|
||||
cutlass::complex<int> source = {-2, 1};
|
||||
|
||||
cutlass::complex<double> dest = source; // implicit conversion
|
||||
|
||||
EXPECT_TRUE(source.real() == -2 && source.imag() == 1 &&
|
||||
dest.real() == -2 && dest.imag() == 1);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(complex, f16_to_f32_conversion) {
|
||||
|
||||
cutlass::complex<cutlass::half_t> source = {1.5_hf, -1.25_hf};
|
||||
|
||||
cutlass::complex<float> dest = cutlass::complex<float>(source); // explicit conversion
|
||||
|
||||
EXPECT_TRUE(source.real() == 1.5_hf && source.imag() == -1.25_hf &&
|
||||
dest.real() == 1.5f && dest.imag() == -1.25f);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -0,0 +1,410 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright (c) 2017-2019, 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 functional operators.
|
||||
*/
|
||||
|
||||
#include "../common/cutlass_unit_test.h"
|
||||
|
||||
#include "cutlass/functional.h"
|
||||
|
||||
#include "cutlass/layout/matrix.h"
|
||||
#include "cutlass/util/host_tensor.h"
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace test {
|
||||
namespace core {
|
||||
namespace kernel {
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Conversion template
|
||||
template <typename Element, typename Operator>
|
||||
__global__ void unary_operator(Element *d, Element const *a) {
|
||||
|
||||
Operator op;
|
||||
|
||||
*d = op(*a);
|
||||
}
|
||||
|
||||
/// Conversion template
|
||||
template <typename Element, typename Operator>
|
||||
__global__ void binary_operator(Element *d, Element const *a, Element const *b, int Iterations = 1) {
|
||||
|
||||
Operator op;
|
||||
|
||||
Element a_x = *a;
|
||||
Element b_x = *b;
|
||||
|
||||
CUTLASS_PRAGMA_NO_UNROLL
|
||||
for (int i = 0; i < Iterations; ++i) {
|
||||
b_x = op(a_x, b_x);
|
||||
}
|
||||
|
||||
*d = b_x;
|
||||
}
|
||||
|
||||
/// Conversion template
|
||||
template <typename Element, typename Operator>
|
||||
__global__ void trinary_operator(
|
||||
Element *d,
|
||||
Element const *a,
|
||||
Element const *b,
|
||||
Element const *c,
|
||||
int Iterations = 1) {
|
||||
|
||||
Operator op;
|
||||
|
||||
Element a_x = *a;
|
||||
Element b_x = *b;
|
||||
Element c_x = *c;
|
||||
|
||||
CUTLASS_PRAGMA_NO_UNROLL
|
||||
for (int i = 0; i < Iterations; ++i) {
|
||||
c_x = op(a_x, b_x, c_x);
|
||||
}
|
||||
|
||||
*d = c_x;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
} // namespace kernel
|
||||
} // namespace core
|
||||
} // namespace test
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <int kN>
|
||||
void Functional_plus_f16xN() {
|
||||
|
||||
using Element = cutlass::Array<cutlass::half_t, kN>;
|
||||
using Operator = cutlass::plus<Element>;
|
||||
|
||||
using Tensor = cutlass::HostTensor<cutlass::half_t, cutlass::layout::RowMajor>;
|
||||
|
||||
Tensor D({1, kN});
|
||||
Tensor A({1, kN});
|
||||
Tensor B({1, kN});
|
||||
Tensor C({1, kN});
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
A.host_data()[i] = cutlass::half_t((i * 2 + 1) % 5);
|
||||
B.host_data()[i] = cutlass::half_t((i * 4 + 8) % 7);
|
||||
D.host_data()[i] = cutlass::half_t(0);
|
||||
}
|
||||
|
||||
D.sync_device();
|
||||
A.sync_device();
|
||||
B.sync_device();
|
||||
|
||||
test::core::kernel::binary_operator<Element, Operator><<< dim3(1,1), dim3(1,1) >>>(
|
||||
reinterpret_cast<Element *>(D.device_data()),
|
||||
reinterpret_cast<Element const *>(A.device_data()),
|
||||
reinterpret_cast<Element const *>(B.device_data())
|
||||
);
|
||||
|
||||
D.sync_host();
|
||||
|
||||
bool some_d_nonzero = false;
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
float a = float(A.host_data()[i]);
|
||||
float b = float(B.host_data()[i]);
|
||||
float d = float(D.host_data()[i]);
|
||||
|
||||
EXPECT_TRUE(d == (a + b));
|
||||
|
||||
if (d != 0) {
|
||||
some_d_nonzero = true;
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(some_d_nonzero);
|
||||
}
|
||||
|
||||
TEST(Functional, plus_f16x16) {
|
||||
Functional_plus_f16xN<16>();
|
||||
}
|
||||
|
||||
TEST(Functional, plus_f16x17) {
|
||||
Functional_plus_f16xN<17>();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <int kN>
|
||||
void Functional_minus_f16xN() {
|
||||
|
||||
using Element = cutlass::Array<cutlass::half_t, kN>;
|
||||
using Operator = cutlass::minus<Element>;
|
||||
|
||||
using Tensor = cutlass::HostTensor<cutlass::half_t, cutlass::layout::RowMajor>;
|
||||
|
||||
Tensor D({1, kN});
|
||||
Tensor A({1, kN});
|
||||
Tensor B({1, kN});
|
||||
Tensor C({1, kN});
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
A.host_data()[i] = cutlass::half_t((i * 2 + 1) % 5);
|
||||
B.host_data()[i] = cutlass::half_t((i * 4 + 8) % 7);
|
||||
D.host_data()[i] = cutlass::half_t(0);
|
||||
}
|
||||
|
||||
D.sync_device();
|
||||
A.sync_device();
|
||||
B.sync_device();
|
||||
|
||||
test::core::kernel::binary_operator<Element, Operator><<< dim3(1,1), dim3(1,1) >>>(
|
||||
reinterpret_cast<Element *>(D.device_data()),
|
||||
reinterpret_cast<Element const *>(A.device_data()),
|
||||
reinterpret_cast<Element const *>(B.device_data())
|
||||
);
|
||||
|
||||
D.sync_host();
|
||||
|
||||
bool some_d_nonzero = false;
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
float a = float(A.host_data()[i]);
|
||||
float b = float(B.host_data()[i]);
|
||||
float d = float(D.host_data()[i]);
|
||||
|
||||
EXPECT_TRUE(d == (a - b));
|
||||
|
||||
if (d != 0) {
|
||||
some_d_nonzero = true;
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(some_d_nonzero);
|
||||
}
|
||||
|
||||
TEST(Functional, minus_f16x16) {
|
||||
Functional_minus_f16xN<16>();
|
||||
}
|
||||
|
||||
TEST(Functional, minus_f16x17) {
|
||||
Functional_minus_f16xN<17>();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <int kN>
|
||||
void Functional_multiplies_f16xN() {
|
||||
|
||||
using Element = cutlass::Array<cutlass::half_t, kN>;
|
||||
using Operator = cutlass::multiplies<Element>;
|
||||
|
||||
using Tensor = cutlass::HostTensor<cutlass::half_t, cutlass::layout::RowMajor>;
|
||||
|
||||
Tensor D({1, kN});
|
||||
Tensor A({1, kN});
|
||||
Tensor B({1, kN});
|
||||
Tensor C({1, kN});
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
A.host_data()[i] = cutlass::half_t((i * 2 + 1) % 5);
|
||||
B.host_data()[i] = cutlass::half_t((i * 4 + 8) % 7);
|
||||
D.host_data()[i] = cutlass::half_t(0);
|
||||
}
|
||||
|
||||
D.sync_device();
|
||||
A.sync_device();
|
||||
B.sync_device();
|
||||
|
||||
test::core::kernel::binary_operator<Element, Operator><<< dim3(1,1), dim3(1,1) >>>(
|
||||
reinterpret_cast<Element *>(D.device_data()),
|
||||
reinterpret_cast<Element const *>(A.device_data()),
|
||||
reinterpret_cast<Element const *>(B.device_data())
|
||||
);
|
||||
|
||||
D.sync_host();
|
||||
|
||||
bool some_d_nonzero = false;
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
float a = float(A.host_data()[i]);
|
||||
float b = float(B.host_data()[i]);
|
||||
float d = float(D.host_data()[i]);
|
||||
|
||||
EXPECT_TRUE(d == (a * b));
|
||||
|
||||
if (d != 0) {
|
||||
some_d_nonzero = true;
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(some_d_nonzero);
|
||||
}
|
||||
|
||||
TEST(Functional, multiplies_f16x16) {
|
||||
|
||||
Functional_multiplies_f16xN<16>();
|
||||
}
|
||||
|
||||
TEST(Functional, multiplies_f16x17) {
|
||||
|
||||
Functional_multiplies_f16xN<17>();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <int kN>
|
||||
void Functional_divides_f16xN() {
|
||||
|
||||
using Element = cutlass::Array<cutlass::half_t, kN>;
|
||||
using Operator = cutlass::divides<Element>;
|
||||
|
||||
using Tensor = cutlass::HostTensor<cutlass::half_t, cutlass::layout::RowMajor>;
|
||||
|
||||
Tensor D({1, kN});
|
||||
Tensor A({1, kN});
|
||||
Tensor B({1, kN});
|
||||
Tensor C({1, kN});
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
A.host_data()[i] = cutlass::half_t((i * 2 + 1) % 5);
|
||||
B.host_data()[i] = cutlass::half_t((i * 4 + 8) % 7);
|
||||
D.host_data()[i] = cutlass::half_t(0);
|
||||
}
|
||||
|
||||
D.sync_device();
|
||||
A.sync_device();
|
||||
B.sync_device();
|
||||
|
||||
test::core::kernel::binary_operator<Element, Operator><<< dim3(1,1), dim3(1,1) >>>(
|
||||
reinterpret_cast<Element *>(D.device_data()),
|
||||
reinterpret_cast<Element const *>(A.device_data()),
|
||||
reinterpret_cast<Element const *>(B.device_data())
|
||||
);
|
||||
|
||||
D.sync_host();
|
||||
|
||||
bool some_d_nonzero = false;
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
float a = float(A.host_data()[i]);
|
||||
float b = float(B.host_data()[i]);
|
||||
float d = float(D.host_data()[i]);
|
||||
|
||||
float expected = a / b;
|
||||
|
||||
float const kThreshold = 0.0005f;
|
||||
|
||||
if (std::isnan(expected)) {
|
||||
EXPECT_TRUE(std::isnan(d));
|
||||
}
|
||||
else if (std::isinf(expected)) {
|
||||
EXPECT_TRUE(std::isinf(d));
|
||||
}
|
||||
else {
|
||||
EXPECT_TRUE(std::abs(d - expected) < kThreshold)
|
||||
<< "Got: " << d << " = " << a << " / " << b << ", expected: " << (a / b);
|
||||
}
|
||||
|
||||
if (d != 0) {
|
||||
some_d_nonzero = true;
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(some_d_nonzero);
|
||||
}
|
||||
|
||||
TEST(Functional, divides_f16x16) {
|
||||
|
||||
Functional_divides_f16xN<16>();
|
||||
}
|
||||
|
||||
TEST(Functional, divides_f16x17) {
|
||||
|
||||
Functional_divides_f16xN<17>();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <int kN>
|
||||
void Functional_multiply_add_f16xN() {
|
||||
|
||||
using Element = cutlass::Array<cutlass::half_t, kN>;
|
||||
using Operator = cutlass::multiply_add<Element>;
|
||||
|
||||
using Tensor = cutlass::HostTensor<cutlass::half_t, cutlass::layout::RowMajor>;
|
||||
|
||||
Tensor D({1, kN});
|
||||
Tensor A({1, kN});
|
||||
Tensor B({1, kN});
|
||||
Tensor C({1, kN});
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
A.host_data()[i] = cutlass::half_t((i * 2 + 1) % 5);
|
||||
B.host_data()[i] = cutlass::half_t((i * 4 + 8) % 7);
|
||||
C.host_data()[i] = cutlass::half_t((i * 3 + 11) % 11);
|
||||
D.host_data()[i] = cutlass::half_t(0);
|
||||
}
|
||||
|
||||
D.sync_device();
|
||||
A.sync_device();
|
||||
B.sync_device();
|
||||
C.sync_device();
|
||||
|
||||
test::core::kernel::trinary_operator<Element, Operator><<< dim3(1,1), dim3(1,1) >>>(
|
||||
reinterpret_cast<Element *>(D.device_data()),
|
||||
reinterpret_cast<Element const *>(A.device_data()),
|
||||
reinterpret_cast<Element const *>(B.device_data()),
|
||||
reinterpret_cast<Element const *>(C.device_data())
|
||||
);
|
||||
|
||||
D.sync_host();
|
||||
|
||||
bool some_d_nonzero = false;
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
float a = float(A.host_data()[i]);
|
||||
float b = float(B.host_data()[i]);
|
||||
float c = float(C.host_data()[i]);
|
||||
float d = float(D.host_data()[i]);
|
||||
|
||||
EXPECT_TRUE(d == (a * b + c));
|
||||
|
||||
if (d != 0) {
|
||||
some_d_nonzero = true;
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_TRUE(some_d_nonzero);
|
||||
}
|
||||
|
||||
TEST(Functional, multiply_add_f16x16) {
|
||||
Functional_multiply_add_f16xN<16>();
|
||||
}
|
||||
|
||||
TEST(Functional, multiply_add_f16x17) {
|
||||
Functional_multiply_add_f16xN<17>();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -0,0 +1,81 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright (c) 2017-2019, 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 Statically sized array of elements that accommodates all CUTLASS-supported numeric types
|
||||
and is safe to use in a union.
|
||||
*/
|
||||
|
||||
#include "../common/cutlass_unit_test.h"
|
||||
|
||||
#include "cutlass/array.h"
|
||||
#include "cutlass/numeric_conversion.h"
|
||||
#include "cutlass/util/device_memory.h"
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Host
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(half_t, host_conversion) {
|
||||
for (int i = -1024; i < 1024; ++i) {
|
||||
float f = static_cast<float>(i);
|
||||
|
||||
cutlass::half_t x = static_cast<cutlass::half_t>(i);
|
||||
cutlass::half_t y = static_cast<cutlass::half_t>(f);
|
||||
|
||||
EXPECT_TRUE(static_cast<int>(x) == i);
|
||||
EXPECT_TRUE(static_cast<float>(y) == f);
|
||||
}
|
||||
|
||||
// Try out user-defined literals
|
||||
EXPECT_TRUE(cutlass::half_t(7) == 7_hf);
|
||||
EXPECT_TRUE(7 == static_cast<int>(7_hf));
|
||||
}
|
||||
|
||||
TEST(half_t, host_arithmetic) {
|
||||
|
||||
for (int i = -100; i < 100; ++i) {
|
||||
for (int j = -100; j < 100; ++j) {
|
||||
|
||||
cutlass::half_t x = static_cast<cutlass::half_t>(i);
|
||||
cutlass::half_t y = static_cast<cutlass::half_t>(j);
|
||||
|
||||
EXPECT_TRUE(static_cast<int>(x + y) == (i + j));
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = -6; i < 6; ++i) {
|
||||
for (int j = -6; j < 6; ++j) {
|
||||
|
||||
cutlass::half_t x = static_cast<cutlass::half_t>(i);
|
||||
cutlass::half_t y = static_cast<cutlass::half_t>(j);
|
||||
|
||||
EXPECT_TRUE(static_cast<int>(x * y) == (i * j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -0,0 +1,221 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright (c) 2017-2019, 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 matrix_coord
|
||||
*/
|
||||
|
||||
#include "../common/cutlass_unit_test.h"
|
||||
|
||||
#include "cutlass/matrix_coord.h"
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
namespace test {
|
||||
namespace core {
|
||||
|
||||
void test_matrix_coord(cutlass::MatrixCoord::Index row, cutlass::MatrixCoord::Index column) {
|
||||
cutlass::MatrixCoord matrix_coord(row, column);
|
||||
|
||||
EXPECT_EQ(matrix_coord.row(), row);
|
||||
EXPECT_EQ(matrix_coord.column(), column);
|
||||
}
|
||||
|
||||
void test_matrix_coord_operator_addition() {
|
||||
cutlass::MatrixCoord::Index row_a = 13;
|
||||
cutlass::MatrixCoord::Index column_a = 42;
|
||||
cutlass::MatrixCoord::Index row_b = 20;
|
||||
cutlass::MatrixCoord::Index column_b = 15;
|
||||
|
||||
cutlass::MatrixCoord matrix_coord_a(row_a, column_a);
|
||||
cutlass::MatrixCoord matrix_coord_b(row_b, column_b);
|
||||
|
||||
auto matrix_coord_c = matrix_coord_a + matrix_coord_b;
|
||||
|
||||
EXPECT_EQ(matrix_coord_c.row(), row_a + row_b);
|
||||
EXPECT_EQ(matrix_coord_c.column(), column_a + column_b);
|
||||
}
|
||||
|
||||
void test_matrix_coord_operator_subtraction() {
|
||||
cutlass::MatrixCoord::Index row_a = 13;
|
||||
cutlass::MatrixCoord::Index column_a = 42;
|
||||
cutlass::MatrixCoord::Index row_b = 20;
|
||||
cutlass::MatrixCoord::Index column_b = 15;
|
||||
|
||||
cutlass::MatrixCoord matrix_coord_a(row_a, column_a);
|
||||
cutlass::MatrixCoord matrix_coord_b(row_b, column_b);
|
||||
|
||||
auto matrix_coord_c = matrix_coord_a - matrix_coord_b;
|
||||
|
||||
EXPECT_EQ(matrix_coord_c.row(), row_a - row_b);
|
||||
EXPECT_EQ(matrix_coord_c.column(), column_a - column_b);
|
||||
}
|
||||
|
||||
void test_matrix_coord_operator_multiply() {
|
||||
cutlass::MatrixCoord::Index row_a = 13;
|
||||
cutlass::MatrixCoord::Index column_a = 42;
|
||||
cutlass::MatrixCoord::Index row_b = 20;
|
||||
cutlass::MatrixCoord::Index column_b = 15;
|
||||
|
||||
cutlass::MatrixCoord matrix_coord_a(row_a, column_a);
|
||||
cutlass::MatrixCoord matrix_coord_b(row_b, column_b);
|
||||
|
||||
auto matrix_coord_c = matrix_coord_a * matrix_coord_b;
|
||||
|
||||
EXPECT_EQ(matrix_coord_c.row(), row_a * row_b);
|
||||
EXPECT_EQ(matrix_coord_c.column(), column_a * column_b);
|
||||
}
|
||||
|
||||
void test_matrix_coord_operator_division() {
|
||||
cutlass::MatrixCoord::Index row_a = 13;
|
||||
cutlass::MatrixCoord::Index column_a = 42;
|
||||
cutlass::MatrixCoord::Index row_b = 20;
|
||||
cutlass::MatrixCoord::Index column_b = 15;
|
||||
|
||||
cutlass::MatrixCoord matrix_coord_a(row_a, column_a);
|
||||
cutlass::MatrixCoord matrix_coord_b(row_b, column_b);
|
||||
|
||||
auto matrix_coord_c = matrix_coord_a / matrix_coord_b;
|
||||
|
||||
EXPECT_EQ(matrix_coord_c.row(), row_a / row_b);
|
||||
EXPECT_EQ(matrix_coord_c.column(), column_a / column_b);
|
||||
}
|
||||
|
||||
void test_matrix_coord_operator_addition_assignment() {
|
||||
cutlass::MatrixCoord::Index row_a = 13;
|
||||
cutlass::MatrixCoord::Index column_a = 42;
|
||||
cutlass::MatrixCoord::Index row_b = 20;
|
||||
cutlass::MatrixCoord::Index column_b = 15;
|
||||
|
||||
cutlass::MatrixCoord matrix_coord_a(row_a, column_a);
|
||||
cutlass::MatrixCoord matrix_coord_b(row_b, column_b);
|
||||
|
||||
matrix_coord_a += matrix_coord_b;
|
||||
|
||||
EXPECT_EQ(matrix_coord_a.row(), row_a + row_b);
|
||||
EXPECT_EQ(matrix_coord_a.column(), column_a + column_b);
|
||||
}
|
||||
|
||||
void test_matrix_coord_operator_subtraction_assignment() {
|
||||
cutlass::MatrixCoord::Index row_a = 13;
|
||||
cutlass::MatrixCoord::Index column_a = 42;
|
||||
cutlass::MatrixCoord::Index row_b = 20;
|
||||
cutlass::MatrixCoord::Index column_b = 15;
|
||||
|
||||
cutlass::MatrixCoord matrix_coord_a(row_a, column_a);
|
||||
cutlass::MatrixCoord matrix_coord_b(row_b, column_b);
|
||||
|
||||
matrix_coord_a -= matrix_coord_b;
|
||||
|
||||
EXPECT_EQ(matrix_coord_a.row(), row_a - row_b);
|
||||
EXPECT_EQ(matrix_coord_a.column(), column_a - column_b);
|
||||
}
|
||||
|
||||
void test_matrix_coord_operator_multiply_assignment() {
|
||||
cutlass::MatrixCoord::Index row_a = 13;
|
||||
cutlass::MatrixCoord::Index column_a = 42;
|
||||
cutlass::MatrixCoord::Index row_b = 20;
|
||||
cutlass::MatrixCoord::Index column_b = 15;
|
||||
|
||||
cutlass::MatrixCoord matrix_coord_a(row_a, column_a);
|
||||
cutlass::MatrixCoord matrix_coord_b(row_b, column_b);
|
||||
|
||||
matrix_coord_a *= matrix_coord_b;
|
||||
|
||||
EXPECT_EQ(matrix_coord_a.row(), row_a * row_b);
|
||||
EXPECT_EQ(matrix_coord_a.column(), column_a * column_b);
|
||||
}
|
||||
|
||||
void test_matrix_coord_operator_division_assignment() {
|
||||
cutlass::MatrixCoord::Index row_a = 13;
|
||||
cutlass::MatrixCoord::Index column_a = 42;
|
||||
cutlass::MatrixCoord::Index row_b = 20;
|
||||
cutlass::MatrixCoord::Index column_b = 15;
|
||||
|
||||
cutlass::MatrixCoord matrix_coord_a(row_a, column_a);
|
||||
cutlass::MatrixCoord matrix_coord_b(row_b, column_b);
|
||||
|
||||
matrix_coord_a /= matrix_coord_b;
|
||||
|
||||
EXPECT_EQ(matrix_coord_a.row(), row_a / row_b);
|
||||
EXPECT_EQ(matrix_coord_a.column(), column_a / column_b);
|
||||
}
|
||||
}
|
||||
} // namespace test
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(Matrix_Coord, basic_row12_column24) {
|
||||
cutlass::MatrixCoord::Index row = 12;
|
||||
cutlass::MatrixCoord::Index column = 24;
|
||||
test::core::test_matrix_coord(row, column);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(Matrix_Coord, basic_operator_addition) {
|
||||
test::core::test_matrix_coord_operator_addition();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(Matrix_Coord, basic_operator_subtraction) {
|
||||
test::core::test_matrix_coord_operator_subtraction();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(Matrix_Coord, basic_operator_multiply) {
|
||||
test::core::test_matrix_coord_operator_multiply();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(Matrix_Coord, basic_operator_division) {
|
||||
test::core::test_matrix_coord_operator_division();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(Matrix_Coord, basic_operator_addition_assignment) {
|
||||
test::core::test_matrix_coord_operator_addition_assignment();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(Matrix_Coord, basic_operator_subtraction_assignment) {
|
||||
test::core::test_matrix_coord_operator_subtraction_assignment();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(Matrix_Coord, basic_operator_multiply_assignment) {
|
||||
test::core::test_matrix_coord_operator_multiply_assignment();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(Matrix_Coord, basic_operator_division_assignment) {
|
||||
test::core::test_matrix_coord_operator_division_assignment();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -0,0 +1,185 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright (c) 2017-2019, 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 conversion operators.
|
||||
*/
|
||||
|
||||
#include "../common/cutlass_unit_test.h"
|
||||
|
||||
#include "cutlass/numeric_conversion.h"
|
||||
|
||||
#include "cutlass/layout/matrix.h"
|
||||
#include "cutlass/util/host_tensor.h"
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace test {
|
||||
namespace core {
|
||||
namespace kernel {
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Conversion template
|
||||
template <typename Destination, typename Source, int Count>
|
||||
__global__ void convert(
|
||||
cutlass::Array<Destination, Count> *destination,
|
||||
cutlass::Array<Source, Count> const *source) {
|
||||
|
||||
cutlass::NumericArrayConverter<Destination, Source, Count> convert;
|
||||
|
||||
*destination = convert(*source);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
} // namespace kernel
|
||||
} // namespace core
|
||||
} // namespace test
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(NumericConversion, f32_to_f16_rn) {
|
||||
|
||||
int const kN = 1;
|
||||
using Source = float;
|
||||
using Destination = cutlass::half_t;
|
||||
|
||||
dim3 grid(1, 1);
|
||||
dim3 block(1, 1);
|
||||
|
||||
cutlass::HostTensor<cutlass::half_t, cutlass::layout::RowMajor> destination({1, kN});
|
||||
cutlass::HostTensor<float, cutlass::layout::RowMajor> source({1, kN});
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
source.host_data()[i] = float(i);
|
||||
}
|
||||
|
||||
source.sync_device();
|
||||
|
||||
test::core::kernel::convert<Destination, Source, 1><<< grid, block >>>(
|
||||
reinterpret_cast<cutlass::Array<Destination, 1> *>(destination.device_data()),
|
||||
reinterpret_cast<cutlass::Array<Source, 1> const *>(source.device_data())
|
||||
);
|
||||
|
||||
destination.sync_host();
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
EXPECT_TRUE(float(destination.host_data()[i]) == source.host_data()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NumericConversion, f32x8_to_f16x8_rn) {
|
||||
|
||||
int const kN = 8;
|
||||
using Source = float;
|
||||
using Destination = cutlass::half_t;
|
||||
|
||||
dim3 grid(1, 1);
|
||||
dim3 block(1, 1);
|
||||
|
||||
cutlass::HostTensor<Destination, cutlass::layout::RowMajor> destination({1, kN});
|
||||
cutlass::HostTensor<Source, cutlass::layout::RowMajor> source({1, kN});
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
source.host_data()[i] = float(i);
|
||||
}
|
||||
|
||||
source.sync_device();
|
||||
|
||||
test::core::kernel::convert<Destination, Source, kN><<< grid, block >>>(
|
||||
reinterpret_cast<cutlass::Array<Destination, kN> *>(destination.device_data()),
|
||||
reinterpret_cast<cutlass::Array<Source, kN> const *>(source.device_data())
|
||||
);
|
||||
|
||||
destination.sync_host();
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
EXPECT_TRUE(float(destination.host_data()[i]) == source.host_data()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(NumericConversion, f16_to_f32_rn) {
|
||||
|
||||
int const kN = 1;
|
||||
using Source = cutlass::half_t;
|
||||
using Destination = float;
|
||||
|
||||
dim3 grid(1, 1);
|
||||
dim3 block(1, 1);
|
||||
|
||||
cutlass::HostTensor<float, cutlass::layout::RowMajor> destination({1, kN});
|
||||
cutlass::HostTensor<cutlass::half_t, cutlass::layout::RowMajor> source({1, kN});
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
source.host_data()[i] = Source(i);
|
||||
}
|
||||
|
||||
source.sync_device();
|
||||
|
||||
test::core::kernel::convert<Destination, Source, kN><<< grid, block >>>(
|
||||
reinterpret_cast<cutlass::Array<Destination, kN> *>(destination.device_data()),
|
||||
reinterpret_cast<cutlass::Array<Source, kN> const *>(source.device_data())
|
||||
);
|
||||
|
||||
destination.sync_host();
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
EXPECT_TRUE(float(destination.host_data()[i]) == float(source.host_data()[i]));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NumericConversion, f16x8_to_f32x8_rn) {
|
||||
|
||||
int const kN = 8;
|
||||
using Source = cutlass::half_t;
|
||||
using Destination = float;
|
||||
|
||||
dim3 grid(1, 1);
|
||||
dim3 block(1, 1);
|
||||
|
||||
cutlass::HostTensor<float, cutlass::layout::RowMajor> destination({1, kN});
|
||||
cutlass::HostTensor<cutlass::half_t, cutlass::layout::RowMajor> source({1, kN});
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
source.host_data()[i] = float(i);
|
||||
}
|
||||
|
||||
source.sync_device();
|
||||
|
||||
test::core::kernel::convert<Destination, Source, kN><<< grid, block >>>(
|
||||
reinterpret_cast<cutlass::Array<Destination, kN> *>(destination.device_data()),
|
||||
reinterpret_cast<cutlass::Array<Source, kN> const *>(source.device_data())
|
||||
);
|
||||
|
||||
destination.sync_host();
|
||||
|
||||
for (int i = 0; i < kN; ++i) {
|
||||
EXPECT_TRUE(float(destination.host_data()[i]) == float(source.host_data()[i]));
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -0,0 +1,243 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright (c) 2017-2019, 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.
|
||||
*
|
||||
**************************************************************************************************/
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "../common/cutlass_unit_test.h"
|
||||
|
||||
#include "cutlass/predicate_vector.h"
|
||||
#include "cutlass/util/host_tensor.h"
|
||||
|
||||
namespace test {
|
||||
|
||||
template <typename PredicateVector>
|
||||
__global__ void load_predicates(unsigned *output, unsigned const *input) {
|
||||
|
||||
PredicateVector predicates;
|
||||
|
||||
int const word_count = (PredicateVector::kPredicates + 31) / 32;
|
||||
|
||||
int i = 0;
|
||||
for (int word_idx = 0; word_idx < word_count; ++word_idx) {
|
||||
unsigned word = input[word_idx];
|
||||
|
||||
CUTLASS_PRAGMA_UNROLL
|
||||
for (int bit = 0; bit < sizeof(unsigned) * 8; ++bit) {
|
||||
bool pred = ((word >> bit) & 1);
|
||||
predicates.set(i, pred);
|
||||
|
||||
if (predicates.at(i) != pred) {
|
||||
printf("ERROR - cannot read back predicate\n");
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
__syncthreads();
|
||||
|
||||
i = 0;
|
||||
for (int word_idx = 0; word_idx < word_count; ++word_idx) {
|
||||
|
||||
unsigned result = 0;
|
||||
for (int bit = 0; bit < sizeof(unsigned) * 8; ++bit) {
|
||||
bool pred = predicates.at(i ++);
|
||||
result |= (unsigned(pred) << bit);
|
||||
}
|
||||
output[word_idx] = result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PredicateVector, Basic) {
|
||||
|
||||
static int const Bits = 32;
|
||||
static int const Words = (Bits + 31) / 32;
|
||||
|
||||
typedef cutlass::PredicateVector<Bits> PredicateVector;
|
||||
|
||||
cutlass::HostTensor<unsigned, cutlass::IdentityTensorLayout<1> > output;
|
||||
cutlass::HostTensor<unsigned, cutlass::IdentityTensorLayout<1>> input;
|
||||
|
||||
output.reserve(Words);
|
||||
input.reserve(Words);
|
||||
|
||||
// some arbitrary test bits
|
||||
unsigned values[] = {
|
||||
0xdeadbeef,
|
||||
0xa0070032,
|
||||
0x9076d001,
|
||||
0x00000000,
|
||||
0xabdfc0ad
|
||||
};
|
||||
|
||||
for (int test = 0; test < 5; ++test) {
|
||||
|
||||
input.host_data(0) = values[test];
|
||||
output.host_data(0) = 0;
|
||||
|
||||
input.sync_device();
|
||||
output.sync_device();
|
||||
|
||||
test::load_predicates<PredicateVector><<<
|
||||
dim3(1,1,1), dim3(1,1,1)
|
||||
>>>(
|
||||
output.device_data(),
|
||||
input.device_data()
|
||||
);
|
||||
|
||||
output.sync_host();
|
||||
|
||||
for (int word = 0; word < Words; ++word) {
|
||||
EXPECT_EQ(input.host_data(word), output.host_data(word))
|
||||
<< "Expected: 0x" << std::hex << input.host_data(word)
|
||||
<< ", got: 0x" << output.host_data(word)
|
||||
<< std::dec;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PredicateVector, Count) {
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<4, 8> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 1)
|
||||
<< "PredicateVector<4, 8> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<4, 4> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 1)
|
||||
<< "PredicateVector<4, 4> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<4, 2> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 1)
|
||||
<< "PredicateVector<4, 2> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<4, 1> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 1)
|
||||
<< "PredicateVector<4, 1> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<8, 8> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 1)
|
||||
<< "PredicateVector<8, 8> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<8, 4> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 1)
|
||||
<< "PredicateVector<8, 4> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<8, 2> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 1)
|
||||
<< "PredicateVector<8, 2> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<8, 1> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 2)
|
||||
<< "PredicateVector<8, 1> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<16, 8> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 1)
|
||||
<< "PredicateVector<16, 8> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<16, 4> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 1)
|
||||
<< "PredicateVector<16, 4> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<16, 2> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 2)
|
||||
<< "PredicateVector<16, 2> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<16, 1> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 4)
|
||||
<< "PredicateVector<16, 1> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<32, 8> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 1)
|
||||
<< "PredicateVector<32, 8> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<32, 4> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 2)
|
||||
<< "PredicateVector<32, 4> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<32, 2> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 4)
|
||||
<< "PredicateVector<32, 2> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<32, 1> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 8)
|
||||
<< "PredicateVector<32, 1> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<64, 8> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 2)
|
||||
<< "PredicateVector<64, 8> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<64, 4> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 4)
|
||||
<< "PredicateVector<64, 4> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<64, 2> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 8)
|
||||
<< "PredicateVector<64, 2> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
|
||||
{
|
||||
typedef cutlass::PredicateVector<64, 1> PredicateVector;
|
||||
EXPECT_EQ(int(PredicateVector::kWordCount), 16)
|
||||
<< "PredicateVector<64, 1> word count: " << int(PredicateVector::kWordCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright (c) 2017-2019, 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.
|
||||
*
|
||||
**************************************************************************************************/
|
||||
#include "../common/cutlass_unit_test.h"
|
||||
|
||||
#include "cutlass/tensor_ref.h"
|
||||
#include "cutlass/layout/matrix.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(TensorRef, basic_rank2) {
|
||||
int const M = 8;
|
||||
int const N = 16;
|
||||
|
||||
int matrix_data[M * N] = {0};
|
||||
|
||||
cutlass::TensorRef<
|
||||
int,
|
||||
cutlass::IdentityTensorLayout<2> > matrix_ref(matrix_data, cutlass::make_Coord(N, 1));
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
matrix_ref.at(cutlass::make_Coord(m, n)) = m * N + n;
|
||||
}
|
||||
}
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
EXPECT_EQ(matrix_data[m * N + n], int(m * N + n));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(TensorRef, rank2_column_major) {
|
||||
int const M = 8;
|
||||
int const N = 8;
|
||||
|
||||
int matrix_data[M * N];
|
||||
|
||||
cutlass::TensorRef<int, cutlass::layout::ColumnMajor> ref(matrix_data, M);
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
ref.at(cutlass::make_Coord(m, n)) = m * N + n;
|
||||
}
|
||||
}
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
EXPECT_EQ(matrix_data[m + n * M], int(m * N + n));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(TensorRef, rank2_row_major) {
|
||||
int const M = 8;
|
||||
int const N = 16;
|
||||
|
||||
int matrix_data[M * N] = { 0 };
|
||||
|
||||
cutlass::TensorRef<int, cutlass::layout::RowMajor> ref(matrix_data, N);
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
ref.at(cutlass::make_Coord(m, n)) = m * N + n;
|
||||
}
|
||||
}
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
EXPECT_EQ(matrix_data[m * N + n], int(m * N + n));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(TensorRef, rank2_contiguous_dynamic) {
|
||||
int const M = 8;
|
||||
int const N = 16;
|
||||
|
||||
typedef cutlass::TensorRef<int, cutlass::layout::ContiguousMatrix> ContiguousTensorRef;
|
||||
|
||||
cutlass::layout::Matrix layouts[] = {
|
||||
cutlass::layout::Matrix::kColumnMajor,
|
||||
cutlass::layout::Matrix::kRowMajor
|
||||
};
|
||||
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
|
||||
int matrix_data[M * N] = { 0 };
|
||||
|
||||
int row_stride;
|
||||
int col_stride;
|
||||
|
||||
if (layouts[i] == cutlass::layout::Matrix::kColumnMajor) {
|
||||
row_stride = 1;
|
||||
col_stride = M;
|
||||
}
|
||||
else {
|
||||
row_stride = N;
|
||||
col_stride = 1;
|
||||
}
|
||||
|
||||
// Use helper to determine stride vector from leading dimension
|
||||
ContiguousTensorRef ref(
|
||||
matrix_data,
|
||||
cutlass::layout::ContiguousMatrix::packed(cutlass::make_Coord(M, N), layouts[i]));
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
ref.at(cutlass::make_Coord(m, n)) = m * N + n;
|
||||
}
|
||||
}
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
EXPECT_EQ(matrix_data[m * row_stride + n * col_stride], int(m * N + n));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(TensorRef, rank2_column_major_interleaved) {
|
||||
int const M = 16;
|
||||
int const N = 16;
|
||||
int const kInterleave = 4;
|
||||
|
||||
int matrix_data[M * N] = {0};
|
||||
|
||||
// Define the Layout for a column-major interleaved matrix format
|
||||
using Layout = cutlass::layout::ColumnMajorInterleaved<kInterleave>;
|
||||
|
||||
// Construct a TensorRef
|
||||
cutlass::TensorRef<
|
||||
int,
|
||||
Layout> ref(matrix_data, Layout::packed(cutlass::make_Coord(M, N)));
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
ref.at(cutlass::make_Coord(m, n)) = m + n * M;
|
||||
}
|
||||
}
|
||||
|
||||
// Verify
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; n += kInterleave) {
|
||||
for (int i = 0; i < kInterleave; ++i) {
|
||||
EXPECT_EQ(matrix_data[m * kInterleave + n * M + i], int(m + (n + i) * M));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(TensorRef, rank2_row_major_interleaved) {
|
||||
int const M = 16;
|
||||
int const N = 16;
|
||||
int const kInterleave = 4;
|
||||
|
||||
int matrix_data[M * N] = {0};
|
||||
|
||||
// Define the Layout for a row-major interleaved matrix format
|
||||
using Layout = cutlass::layout::RowMajorInterleaved<kInterleave>;
|
||||
|
||||
// Construct a TensorRef
|
||||
cutlass::TensorRef<
|
||||
int,
|
||||
Layout> ref(matrix_data, Layout::packed(cutlass::make_Coord(M, N)));
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
ref.at(cutlass::make_Coord(m, n)) = m + n * M;
|
||||
}
|
||||
}
|
||||
|
||||
// Verify
|
||||
for (int m = 0; m < M; m += kInterleave) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
for (int i = 0; i < kInterleave; ++i) {
|
||||
EXPECT_EQ(matrix_data[m * N + i + n * kInterleave], int((m + i) + n * M));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -0,0 +1,283 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright (c) 2017-2019, 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.
|
||||
*
|
||||
**************************************************************************************************/
|
||||
#include "../common/cutlass_unit_test.h"
|
||||
|
||||
#include "cutlass/tensor_view.h"
|
||||
#include "cutlass/layout/matrix.h"
|
||||
|
||||
#include "cutlass/util/tensor_view_io.h"
|
||||
#include "cutlass/util/host_tensor.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(TensorView, rank2_contiguous_dynamic) {
|
||||
int const M = 8;
|
||||
int const N = 16;
|
||||
|
||||
typedef cutlass::TensorView<int, cutlass::layout::ContiguousMatrix> ContiguousTensorView;
|
||||
|
||||
cutlass::layout::Matrix layouts[] = {
|
||||
cutlass::layout::Matrix::kColumnMajor,
|
||||
cutlass::layout::Matrix::kRowMajor
|
||||
};
|
||||
|
||||
cutlass::Coord<2> bounds = cutlass::make_Coord(M - 2, N - 2);
|
||||
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
|
||||
int matrix_data[M * N] = { 0 };
|
||||
|
||||
int row_stride;
|
||||
int col_stride;
|
||||
|
||||
if (layouts[i] == cutlass::layout::Matrix::kColumnMajor) {
|
||||
row_stride = 1;
|
||||
col_stride = M;
|
||||
}
|
||||
else {
|
||||
row_stride = N;
|
||||
col_stride = 1;
|
||||
}
|
||||
|
||||
// Use helper to determine stride vector from leading dimension
|
||||
ContiguousTensorView view(
|
||||
matrix_data,
|
||||
cutlass::layout::ContiguousMatrix::packed(cutlass::make_Coord(M, N), layouts[i]),
|
||||
bounds);
|
||||
|
||||
ASSERT_TRUE(view.good());
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
cutlass::Coord<2> coord = cutlass::make_Coord(m, n);
|
||||
if (view.contains(coord)) {
|
||||
view.at(coord) = m * N + n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
int expected = 0;
|
||||
if (m < bounds[0] && n < bounds[1]) {
|
||||
expected = int(m * N + n);
|
||||
}
|
||||
EXPECT_EQ(matrix_data[m * row_stride + n * col_stride], expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Uncomment the following line to observe output from printing TensorView objects
|
||||
//
|
||||
|
||||
// #define OBSERVE_TENSORVIEW_IO // uncomment to enable printing
|
||||
|
||||
#ifdef OBSERVE_TENSORVIEW_IO
|
||||
|
||||
// This test construct a TensorView of rank=2 with matrix layouts known at runtime. This
|
||||
// uses TensorRefMapFunc classes defined in cutlass/matrix_traits.h to define the mapping
|
||||
// from logical tensor indices to storage in memory.
|
||||
//
|
||||
// Helpers in tools/util/tensor_view_io.h print both the logical TensorView and the
|
||||
// linear memory of the tensor.
|
||||
TEST(TensorView, contiguous) {
|
||||
|
||||
int const M = 8;
|
||||
int const N = 16;
|
||||
|
||||
typedef cutlass::TensorView<
|
||||
int32_t,
|
||||
cutlass::layout::ContiguousLayout> ContiguousTensorView;
|
||||
|
||||
cutlass::MatrixLayout layouts[] = {
|
||||
cutlass::MatrixLayout::kColumnMajor,
|
||||
cutlass::MatrixLayout::kRowMajor
|
||||
};
|
||||
|
||||
cutlass::Coord<2> bounds = cutlass::make_Coord(M, N);
|
||||
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
|
||||
int matrix_data[M * N] = { 0 };
|
||||
|
||||
int ldm;
|
||||
int row_stride;
|
||||
int col_stride;
|
||||
|
||||
if (layouts[i] == cutlass::MatrixLayout::kColumnMajor) {
|
||||
row_stride = 1;
|
||||
col_stride = M;
|
||||
ldm = col_stride;
|
||||
}
|
||||
else {
|
||||
row_stride = N;
|
||||
col_stride = 1;
|
||||
ldm = row_stride;
|
||||
}
|
||||
|
||||
// Use helper to determine stride vector from leading dimension
|
||||
ContiguousTensorView view(
|
||||
matrix_data,
|
||||
cutlass::layout::ContiguousLayout::stride(layouts[i], ldm),
|
||||
bounds);
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
cutlass::Coord<2> coord = cutlass::make_Coord(m, n);
|
||||
if (view.contains(coord)) {
|
||||
view.at(coord) = m * N + n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "---------\n";
|
||||
std::cout << (layouts[i] == cutlass::MatrixLayout::kColumnMajor ?
|
||||
"Column-major:" : "Row-major:") << "\n\n";
|
||||
|
||||
std::cout << "Logical view:\n";
|
||||
std::cout.width(4);
|
||||
std::cout << view << "\n" << std::endl; // Print TensorView object.
|
||||
|
||||
std::cout << "Linear memory:";
|
||||
for (int idx = 0; idx < view.capacity(); ++idx) {
|
||||
if (!(idx % (layouts[i] == cutlass::MatrixLayout::kColumnMajor ? M : N))) {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << std::setw(4) << view.at(idx) << " ";
|
||||
}
|
||||
|
||||
std::cout << "\n" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// This test is similar to the previous except it uses a column-major, interleaved data
|
||||
// layout. The test prints both the logical representation (a typical column-major matrix)
|
||||
// and a representation of linear memory.
|
||||
//
|
||||
// Note, the interleave=4 structure implies that every four consecutive elements in the
|
||||
// same row shall be adjacent in memory followed by the next row.
|
||||
TEST(TensorView, rank2_column_major_interleaved) {
|
||||
int const M = 16;
|
||||
int const N = 16;
|
||||
int const kInterleave = 4;
|
||||
|
||||
int matrix_data[M * N] = {0};
|
||||
|
||||
cutlass::Coord<2> bounds = cutlass::make_Coord(M, N);
|
||||
|
||||
// Define the TensorRefMapFunc for a column-major interleaved matrix format
|
||||
typedef cutlass::layout::ColumnMajorInterleaved<kInterleave> TensorRefMapFunc;
|
||||
|
||||
// Define a TensorView of rank=2 using the column-major interleaved mapping function
|
||||
typedef cutlass::TensorView<
|
||||
int,
|
||||
TensorRefMapFunc> InterleavedTensorView;
|
||||
|
||||
InterleavedTensorView view(
|
||||
matrix_data,
|
||||
TensorRefMapFunc::stride(M),
|
||||
bounds);
|
||||
|
||||
// Initialize
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
view.at(cutlass::make_Coord(m, n)) = m + n * M;
|
||||
}
|
||||
}
|
||||
|
||||
// Print logical view
|
||||
std::cout << "Column-major, interleave=" << kInterleave << " (logical view):\n";
|
||||
|
||||
std::cout << std::setw(4) << view << "\n" << std::endl;
|
||||
|
||||
// Now define a linear view of the same data in memory
|
||||
typedef cutlass::TensorView<int, 2, cutlass::layout::RowMajor> LinearTensorView;
|
||||
|
||||
LinearTensorView linear_view(matrix_data, cutlass::make_Coord(N), bounds);
|
||||
|
||||
std::cout << "Linear view in memory:\n";
|
||||
std::cout << std::setw(4) << linear_view << std::endl;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(TensorView, int4) {
|
||||
|
||||
int const M = 4;
|
||||
int const N = 8;
|
||||
|
||||
using T = cutlass::int4b_t;
|
||||
|
||||
cutlass::HostTensor<T, cutlass::layout::RowMajor> tensor({M, N});
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
T x = T(n ^ m); // some simple hash
|
||||
tensor.host_view().at({m, n}) = x;
|
||||
}
|
||||
}
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
int x = (n ^ m); // some simple hash
|
||||
EXPECT_TRUE(int(tensor.host_view().at({m, n})) == x);
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_EQ(tensor.size(), M * N);
|
||||
}
|
||||
|
||||
TEST(TensorView, uint4) {
|
||||
|
||||
int const M = 4;
|
||||
int const N = 8;
|
||||
|
||||
using T = cutlass::uint4b_t;
|
||||
|
||||
cutlass::HostTensor<T, cutlass::layout::RowMajor> tensor({M, N});
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
T x = T(n ^ m); // some simple hash
|
||||
tensor.host_view().at({m, n}) = x;
|
||||
}
|
||||
}
|
||||
|
||||
for (int m = 0; m < M; ++m) {
|
||||
for (int n = 0; n < N; ++n) {
|
||||
int x = (n ^ m); // some simple hash
|
||||
EXPECT_TRUE(int(tensor.host_view().at({m, n})) == x);
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_EQ(tensor.size(), M * N);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -0,0 +1,35 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright (c) 2017-2019, 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 CUTLASS core
|
||||
*/
|
||||
|
||||
#include "../common/cutlass_unit_test.h"
|
||||
|
||||
int main(int argc, char* arg[]) {
|
||||
FilterArchitecture();
|
||||
::testing::InitGoogleTest(&argc, arg);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in New Issue
Block a user