Checkpointing CUTLASS 1.1 release.

This commit is contained in:
akerr
2018-09-18 16:58:03 -07:00
parent cf0301e00f
commit 461f417b9d
193 changed files with 29495 additions and 4770 deletions
+102
View File
@@ -0,0 +1,102 @@
/***************************************************************************************************
* Copyright (c) 2017-2018, 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 <complex>
#include "cutlass_unit_test.h"
#include "cutlass/util/complex.h"
#include "tools/util/half.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
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::platform::complex<T> Ae(T(ar) / T(M), T(ai) / T(M));
cutlass::platform::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::platform::complex<T> add_e = Ae + Be;
cutlass::platform::complex<T> sub_e = Ae - Be;
cutlass::platform::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::platform::complex<T> div_e = Ae * Be;
std::complex<T> div_r = Ar * Br;
EXPECT_EQ(real(div_e), real(div_r));
EXPECT_EQ(imag(div_e), imag(div_r));
}
}
}
}
}
}
};
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(Complex, host_float) {
test::ComplexOperators<float, 32, 8> test;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(Complex, host_double) {
test::ComplexOperators<double, 32, 8> test;
}
///////////////////////////////////////////////////////////////////////////////////////
TEST(Complex, host_half) {
// Fewer test cases since half_t is emulated
test::ComplexOperators<cutlass::half_t, 14, 4> test;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
+330 -54
View File
@@ -1,66 +1,342 @@
/******************************************************************************
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are not permitted.
*
* 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 TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
/***************************************************************************************************
* Copyright (c) 2017-2018, 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
/*! \file
\brief Tests for Host_tensor, Host_tensor_view, and Tensor_view
\brief Defines unit tests for HostTensor and HostMatrix.
HostTensor is a utility class for allocating memory on the host and on the selected CUDA device
and presenting a TensorView of this memory.
HostMatrix is new in CUTLASS 1.1 that offers a matrix-like interface to a HostTensor with rank 2.
Several examples are shown in this source file.
*/
//#include <gtest/gtest.h>
#include <cutlass_unit_test.h>
#include <tools/util/host_tensor.h>
#include <tools/util/tensor_view_io.h>
#include "cutlass_unit_test.h"
/// Random number generator
struct RandomGenerator {
RandomGenerator(int seed = 17) {
srand(seed);
}
#include "cutlass/matrix_traits.h"
float operator()() {
return float(rand() % 64) / 8.0f;
}
};
#include "tools/util/tensor_view_io.h"
#include "tools/util/host_tensor.h"
#include "tools/util/host_matrix.h"
TEST(HostTensor, gemm) {
////////////////////////////////////////////////////////////////////////////////////////////////////
int const M = 16;
int const N = 16;
int const K = 16;
namespace test {
typedef cutlass::HostTensor<float, false> HostTensor;
/// Kernel to compute a thread's unique coordinate within a CUDA kernel grid and write a value
/// using a CUTLASS TensorView.
template <typename TensorView>
__global__ void fill_sequential(TensorView view) {
// allocate a host tensor
HostTensor A(
cutlass::make_Coord(1, K, M, 1)
);
// Compute the thread's coordinate in the 2D CUDA kernel grid
cutlass::Coord<2> coord = cutlass::make_Coord(
blockIdx.x * blockDim.x + threadIdx.x,
blockIdx.y * blockDim.y + threadIdx.y
);
HostTensor B(
cutlass::make_Coord(1, N, K, 1)
);
HostTensor C(
cutlass::make_Coord(1, N, M, 1)
);
A.fill_random(RandomGenerator());
B.fill_random(RandomGenerator());
C.gemm<float, float, float, float>(A, B, 1.0f, 0.0f);
// Write a value into the view
if (view.contains(coord)) {
view.at(coord) = coord[0] + view.size(0) * coord[1];
}
}
} // namespace test
////////////////////////////////////////////////////////////////////////////////////////////////////
// This test constructs a CUTLASS HostTensor with column-major layout.
TEST(HostTensor, fill_sequential_column_major) {
int const M = 16;
int const N = 32;
cutlass::Coord<2> bounds = cutlass::make_Coord(M, N);
// Construct a rank=2 host tensor of size M-by-N with leading dimension M
cutlass::HostTensor<
int,
2,
cutlass::MatrixLayout::ColumnMajor> host_tensor(cutlass::make_Coord(M, 1), bounds);
// Fill it with zeros and synchronize device
host_tensor.fill(0);
host_tensor.sync_device();
// Launch a CUDA kernel by obtaining a TensorView of the device memory
dim3 block(16, 16);
dim3 grid((M + block.x - 1) / block.x, (N + block.y - 1) / block.y);
test::fill_sequential<<< grid, block >>>(host_tensor.device_view());
ASSERT_EQ(cudaDeviceSynchronize(), cudaSuccess);
// Synchronize the host data
host_tensor.sync_host();
// Verify host_tensor contains sequential elements
int errors = 0;
for (int n = 0; n < N; ++n) {
for (int m = 0; m < M; ++m) {
int expected = m + n * M;
int got = host_tensor.at(cutlass::make_Coord(m, n));
if (expected != got) {
++errors;
}
}
}
EXPECT_EQ(errors, 0) << std::setw(4) << host_tensor << std::endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// This test constructs a CUTLASS HostTensor with column-major interleaved layout
TEST(HostTensor, fill_sequential_column_major_interleaved) {
int const M = 16;
int const N = 16;
int const kInterleave = 4;
cutlass::Coord<2> bounds = cutlass::make_Coord(M, N);
// Define a mapping function for column-major interleaved layout
typedef cutlass::MatrixLayout::ColumnMajorInterleaved<kInterleave> TensorRefMapFunc;
// Construct a rank=2 host tensor of size M-by-N
cutlass::HostTensor<
int,
2,
TensorRefMapFunc > host_tensor(TensorRefMapFunc::stride(M), bounds);
// Fill it with zeros and synchronize device
host_tensor.fill(0);
host_tensor.sync_device();
// Launch a CUDA kernel by obtaining a TensorView of the device memory
dim3 block(16, 16);
dim3 grid((M + block.x - 1) / block.x, (N + block.y - 1) / block.y);
test::fill_sequential<<< grid, block >>>(host_tensor.device_view());
ASSERT_EQ(cudaDeviceSynchronize(), cudaSuccess);
// Synchronize the host data
host_tensor.sync_host();
// Verify host_tensor contains sequential elements
int errors = 0;
for (int n = 0; n < N; ++n) {
for (int m = 0; m < M; ++m) {
int expected = m + n * M;
int got = host_tensor.at(cutlass::make_Coord(m, n));
if (got != expected) {
++errors;
}
}
}
EXPECT_EQ(errors, 0) << std::setw(4) << host_tensor << std::endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// cutlass::HostMatrix extends cutlass::HostTensor of rank=2 to facilitate allocate and operating
// on matrices in device memory.
//
// cutlass::HostMatrix<T> accommodates both row-major and column-major matrices with a single
// leading dimension.
//
// The first test demonstrates use of HostMatrix<> in the same circumstances as HostTensor but with
// simplifcations to the calling interface.
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// This test constructs a CUTLASS cutlass::HostMatrix with column-major layout.
TEST(HostMatrix, fill_sequential_column_major) {
int const M = 16;
int const N = 32;
int const ldm = M + 2; // define leading dimension with padding
cutlass::Coord<2> bounds = cutlass::make_Coord(M, N);
// Construct a HostMatrix of size M-by-N with leading dimension ldm
cutlass::HostMatrix<int> host_matrix(bounds, cutlass::MatrixLayout::kColumnMajor, ldm);
// Fill it with zeros and synchronize device
host_matrix.fill(0);
host_matrix.sync_device();
// Launch a CUDA kernel by obtaining a TensorView of the device memory
dim3 block(16, 16);
dim3 grid((M + block.x - 1) / block.x, (N + block.y - 1) / block.y);
test::fill_sequential<<< grid, block >>>(host_matrix.device_view());
ASSERT_EQ(cudaDeviceSynchronize(), cudaSuccess);
// Synchronize the host data
host_matrix.sync_host();
// Verify host_matrix contains sequential elements
int errors = 0;
for (int n = 0; n < N; ++n) {
for (int m = 0; m < M; ++m) {
int expected = m + n * M;
int got = host_matrix.at(cutlass::make_Coord(m, n));
if (expected != got) {
++errors;
}
}
}
EXPECT_EQ(errors, 0) << std::setw(4) << host_matrix << std::endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Previously, cutlass::HostTensorView<> offered a gemm() method defined for the H and W dimensions.
// The other dimensions were ignored.
//
// To improve the interface, we We have moved this into the HostMatrixView<> and HostMatrix<>
// classes which require rank=2. To accommodate matrix operands of differing layout, we have extracted
// the host-side GEMM implementation into cutlass::reference::host::Gemm() which can compute the
// general matrix product of matrices with arbitrary layout.
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// This test constructs a CUTLASS cutlass::HostMatrix with column-major layout.
TEST(HostMatrix, gemm) {
// Problem size intentionally small, as reference check has complexity O(MNK).
int const M = 32;
int const N = 16;
int const K = 4;
int const lda = M;
int const ldb = N;
int const ldc = M;
// Construct matrix operands
cutlass::HostMatrix<int> A(cutlass::make_Coord(M, K), cutlass::MatrixLayout::kColumnMajor, lda);
cutlass::HostMatrix<int> B(cutlass::make_Coord(K, N), cutlass::MatrixLayout::kRowMajor, ldb);
cutlass::HostMatrix<int> C(cutlass::make_Coord(M, N), cutlass::MatrixLayout::kColumnMajor, ldc);
A.fill_sequential();
B.fill_sequential();
C.fill(0);
int alpha = 1;
// Compute host-side GEMM reference
cutlass::reference::host::Gemm(
cutlass::gemm::GemmCoord(K, N, M),
alpha,
A.host_ref(),
B.host_ref(),
int(0), // beta
C.host_ref());
// Verify result
int errors = 0;
// Primitive reference implementation for matrix product
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
int result = 0;
for (int k = 0; k < K; ++k) {
result += A.at(cutlass::make_Coord(i, k)) * B.at(cutlass::make_Coord(k, j));
}
if (C.at(cutlass::make_Coord(i, j)) != alpha * result) {
++errors;
}
}
}
EXPECT_EQ(errors, 0) << "GEMM error\n"
<< "A =\n" << A << "\nB = \n" << B << "\nC =\n" << C << "\n";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// When layout is known at compile time, we may be use the corresponding helper classes to smplify
// matrix instantiation. The matrix layout becomes part of the type which reduces the StorageRank
// of the internal stride vector.
//
// Apart from specifying the matrix layout at compile time, this test is functionally identical to
// HostMatrix.gemm.
//
TEST(HostMatrix, gemm_compile_time_layout) {
// Problem size intentionally small, as reference check has complexity O(MNK).
int const M = 32;
int const N = 16;
int const K = 4;
int const lda = M;
int const ldb = N;
int const ldc = M;
// Construct matrix operands
cutlass::HostMatrixColumnMajor<int> A(cutlass::make_Coord(M, K), lda);
cutlass::HostMatrixRowMajor<int> B(cutlass::make_Coord(K, N), ldb);
cutlass::HostMatrixColumnMajor<int> C(cutlass::make_Coord(M, N), ldc);
A.fill_sequential();
B.fill_sequential();
C.fill(0);
int alpha = 1;
// Compute host-side GEMM reference
cutlass::reference::host::Gemm(
cutlass::gemm::GemmCoord(K, N, M),
alpha,
A.host_ref(),
B.host_ref(),
int(0), // beta
C.host_ref());
// Verify result
int errors = 0;
// Primitive reference implementation for matrix product
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
int result = 0;
for (int k = 0; k < K; ++k) {
result += A.at(cutlass::make_Coord(i, k)) * B.at(cutlass::make_Coord(k, j));
}
if (C.at(cutlass::make_Coord(i, j)) != alpha * result) {
++errors;
}
}
}
EXPECT_EQ(errors, 0) << "GEMM error\n"
<< "A =\n" << A << "\nB = \n" << B << "\nC =\n" << C << "\n";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
+324
View File
@@ -0,0 +1,324 @@
/***************************************************************************************************
* Copyright (c) 2017-2018, 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
These tests initialize host- and device-side tensors according to several random distributions.
*/
#include "cutlass_unit_test.h"
#include "cutlass/matrix_traits.h"
#include "tools/util/tensor_view_io.h"
#include "tools/util/host_tensor.h"
#include "tools/util/host_matrix.h"
#include "tools/util/reference/device/tensor_foreach.h"
#include "tools/util/reference/device/tensor_elementwise.h"
#include "tools/util/reference/host/tensor_foreach.h"
#include "tools/util/reference/host/tensor_elementwise.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
#define ENABLE_OUTPUT 0 // Supress output by default.
///////////////////////////////////////////////////////////////////////////////////////////////////
TEST(TensorInitialize, uniform_device) {
// Define the problem size
int const M = 517;
int const N = 117;
// Define HostMatrix type
typedef cutlass::HostMatrix<float> HostMatrix;
// Construct the host matrix
HostMatrix source(cutlass::MatrixCoord(M, N), cutlass::MatrixLayout::kRowMajor);
source.fill(0);
// Initialize the source matrix with a uniform distribution
cutlass::Distribution dist;
dist.set_uniform(0, 128, -1);
// RNG seed is hard-coded for determinism in the test.
unsigned seed = 2080;
cutlass::reference::device::TensorInitialize(source.device_view(), seed, dist);
source.sync_host();
if (ENABLE_OUTPUT) {
std::ofstream result("TensorInitialize_uniform_device.csv");
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
result << source.at(cutlass::make_Coord(i, j)) << "\n";
}
}
}
}
TEST(TensorInitialize, uniform_host) {
// Define the problem size
int const M = 517;
int const N = 117;
bool const kDeviceBacked = false;
// Define HostMatrix type
typedef cutlass::HostMatrix<float> HostMatrix;
// Construct the host matrix
HostMatrix source(cutlass::MatrixCoord(M, N), cutlass::MatrixLayout::kRowMajor, kDeviceBacked);
source.fill(0);
// Initialize the source matrix with a uniform distribution
cutlass::Distribution dist;
dist.set_uniform(0, 128, -1);
// RNG seed is hard-coded for determinism in the test.
unsigned seed = 2080;
cutlass::reference::host::TensorInitialize(source.host_view(), seed, dist);
if (ENABLE_OUTPUT) {
std::ofstream result("TensorInitialize_uniform_host.csv");
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
result << source.at(cutlass::make_Coord(i, j)) << "\n";
}
}
}
}
TEST(TensorInitialize, gaussian_device) {
// Define the problem size
int const M = 517;
int const N = 117;
// Define HostMatrix type
typedef cutlass::HostMatrix<float> HostMatrix;
// Construct the host matrix
HostMatrix source(cutlass::MatrixCoord(M, N), cutlass::MatrixLayout::kRowMajor);
source.fill(0);
// Initialize the source matrix with a uniform distribution
cutlass::Distribution dist;
dist.set_gaussian(1, 2, -1);
// RNG seed is hard-coded for determinism in the test.
unsigned seed = 2080;
cutlass::reference::device::TensorInitialize(source.device_view(), seed, dist);
source.sync_host();
if (ENABLE_OUTPUT) {
std::ofstream result("TensorInitialize_gaussian_device.csv");
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
result << source.at(cutlass::make_Coord(i, j)) << "\n";
}
}
}
}
TEST(TensorInitialize, gaussian_host) {
// Define the problem size
int const M = 517;
int const N = 117;
bool const kDeviceBacked = false;
// Define HostMatrix type
typedef cutlass::HostMatrix<float> HostMatrix;
// Construct the host matrix
HostMatrix source(cutlass::MatrixCoord(M, N), cutlass::MatrixLayout::kRowMajor, kDeviceBacked);
source.fill(0);
// Initialize the source matrix with a uniform distribution
cutlass::Distribution dist;
dist.set_gaussian(1, 2, -1);
// RNG seed is hard-coded for determinism in the test.
unsigned seed = 2080;
cutlass::reference::host::TensorInitialize(source.host_view(), seed, dist);
if (ENABLE_OUTPUT) {
std::ofstream result("TensorInitialize_gaussian_host.csv");
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
result << source.at(cutlass::make_Coord(i, j)) << "\n";
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Interleaved matrix layouts
//
///////////////////////////////////////////////////////////////////////////////////////////////////
TEST(TensorInitialize, interleaved_gaussian_device) {
// Define the problem size
int const M = 512;
int const N = 128;
// Define a mapping function for column-major interleaved layout
int const kInterleave = 4;
typedef cutlass::MatrixLayout::ColumnMajorInterleaved<kInterleave> TensorRefMapFunc;
// Construct a rank=2 host tensor of size M-by-N
cutlass::HostTensor<
float,
2,
TensorRefMapFunc > source(TensorRefMapFunc::stride(M), cutlass::make_Coord(M, N));
source.fill(0);
// Initialize the source matrix with a uniform distribution
cutlass::Distribution dist;
dist.set_gaussian(1, 2, -1);
// RNG seed is hard-coded for determinism in the test.
unsigned seed = 2080;
cutlass::reference::device::TensorInitialize(source.device_view(), seed, dist);
source.sync_host();
if (ENABLE_OUTPUT) {
std::ofstream result("TensorInitialize_interleaved_gaussian_device.csv");
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
result << source.at(cutlass::make_Coord(i, j)) << "\n";
}
}
}
}
TEST(TensorInitialize, interleaved_gaussian_host) {
// Define the problem size
int const M = 512;
int const N = 128;
bool const kDeviceBacked = false;
// Define a mapping function for column-major interleaved layout
int const kInterleave = 4;
typedef cutlass::MatrixLayout::ColumnMajorInterleaved<kInterleave> TensorRefMapFunc;
// Construct a rank=2 host tensor of size M-by-N
cutlass::HostTensor<
float,
2,
TensorRefMapFunc > source(TensorRefMapFunc::stride(M), cutlass::make_Coord(M, N), kDeviceBacked);
// Construct the host matrix
source.fill(0);
// Initialize the source matrix with a uniform distribution
cutlass::Distribution dist;
dist.set_gaussian(1, 2, -1);
// RNG seed is hard-coded for determinism in the test.
unsigned seed = 2080;
cutlass::reference::host::TensorInitialize(source.host_view(), seed, dist);
if (ENABLE_OUTPUT) {
std::ofstream result("TensorInitialize_interleaved_gaussian_host.csv");
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
result << source.at(cutlass::make_Coord(i, j)) << "\n";
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Comparison operator
//
///////////////////////////////////////////////////////////////////////////////////////////////////
TEST(TensorEquals, interleaved_device) {
// Define the problem size
int const M = 512;
int const N = 128;
// Define a mapping function for column-major interleaved layout
int const kInterleave = 4;
typedef cutlass::MatrixLayout::ColumnMajorInterleaved<kInterleave> TensorRefMapFunc;
// Construct two rank=2 host tensor of size M-by-N
cutlass::HostTensor<
float,
2,
TensorRefMapFunc > left(TensorRefMapFunc::stride(M), cutlass::make_Coord(M, N));
cutlass::HostTensor<
float,
2,
TensorRefMapFunc > right(TensorRefMapFunc::stride(M), cutlass::make_Coord(M, N));
// Initialize
left.fill_sequential();
right.fill_sequential();
// Assert equality
EXPECT_TRUE(cutlass::reference::device::TensorEquals(left.device_view(), right.device_view()));
// Overwrite one with an unexpected element
left.at(cutlass::make_Coord(24, 17)) = -1;
left.sync_device();
// Assert inequality
EXPECT_FALSE(cutlass::reference::device::TensorEquals(left.device_view(), right.device_view()));
}
TEST(TensorEquals, interleaved_host) {
}
///////////////////////////////////////////////////////////////////////////////////////////////////
+217
View File
@@ -0,0 +1,217 @@
/***************************************************************************************************
* Copyright (c) 2017-2018, 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
These tests are intended to demonstrate the CUTLASS reference implementation for basic for-each
operators on the index space of TensorView objects. They instantiate a HostMatrix, initialize
its elements with random data according to specified random distributions, and clamp the
elements using a TensorForEach() operation.
Both device-side and host-side reference implementations are called.
*/
#include "cutlass_unit_test.h"
#include "cutlass/matrix_traits.h"
#include "tools/util/tensor_view_io.h"
#include "tools/util/host_tensor.h"
#include "tools/util/host_matrix.h"
#include "tools/util/reference/device/tensor_foreach.h"
#include "tools/util/reference/device/tensor_elementwise.h"
#include "tools/util/reference/host/tensor_foreach.h"
#include "tools/util/reference/host/tensor_elementwise.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace test {
/// Define a functor that computes the ReLu operation on a tensor.
template <typename View>
struct ReLuFunc {
/// Coordinate of index space
typedef typename View::TensorCoord TensorCoord;
/// Scalar type
typedef typename View::Storage T;
//
// Data members
//
/// Tensor view
View view;
/// ReLu threshold
T threshold;
//
// Methods
//
/// Constructor
CUTLASS_HOST_DEVICE
ReLuFunc(View const &view, T threshold): view(view), threshold(threshold) { }
/// ReLu function
CUTLASS_HOST_DEVICE
void operator()(TensorCoord const &coord) {
T value = view.at(coord);
if (value < threshold) {
value = threshold;
}
view.at(coord) = value;
}
};
} // namespace test
///////////////////////////////////////////////////////////////////////////////////////////////////
/// This tests models the computation of ReLu using reference utility code.
TEST(TensorForEach, ReLu_device) {
// Define HostMatrix type
typedef cutlass::HostMatrix<float> HostMatrix;
typedef typename HostMatrix::DeviceTensorView View;
// Define the problem size
int const M = 517;
int const N = 117;
float threshold = 0;
// Construct the host matrix
HostMatrix source(cutlass::MatrixCoord(M, N), cutlass::MatrixLayout::kRowMajor);
source.fill(0);
// Initialize the source matrix with a uniform distribution
cutlass::Distribution dist;
dist.set_uniform(-16, 16);
// RNG seed is hard-coded for determinism in the test.
int64_t seed = 2080;
cutlass::reference::device::TensorInitialize(source.device_view(), seed, dist);
// Define a functor called by TensorForEach<>
typedef test::ReLuFunc<View> ReLuFunc;
// Instantiate on host with TensorView and threshold value
ReLuFunc relu_func(source.device_view(), threshold);
// Launch kernel that applies the element-wise operator over the tensor's index space.
cutlass::reference::device::TensorForEach<
ReLuFunc,
View::kRank,
ReLuFunc>(source.size(), relu_func);
// Verify no element is less than the ReLu threshold.
source.sync_host();
int errors = 0;
for (cutlass::MatrixCoord coord(0, 0); coord.row() < M; ++coord.row()) {
for (coord.column() = 0; coord.column() < N; ++coord.column()) {
if (source.at(coord) < threshold) {
++errors;
if (errors < 10) {
std::cout << "Error - source(" << coord << ") = "
<< source.at(coord) << " is less than threshold " << threshold << std::endl;
}
}
}
}
EXPECT_EQ(errors, 0)
<< "Result: " << source;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Test to apply the ReLu operation using host-side utilities
TEST(TensorForEach, ReLu_host) {
// Define HostMatrix type
typedef cutlass::HostMatrix<float> HostMatrix;
typedef typename HostMatrix::HostTensorView View;
// Define the problem size
int const M = 517;
int const N = 117;
float threshold = 0;
bool const kDeviceBacked = false;
// Construct the host matrix
HostMatrix source(cutlass::MatrixCoord(M, N), cutlass::MatrixLayout::kRowMajor, kDeviceBacked);
source.fill(0);
// Initialize the source matrix with a uniform distribution
cutlass::Distribution dist;
dist.set_gaussian(-1, 4);
// RNG seed is hard-coded for determinism in the test.
unsigned seed = 2080;
cutlass::reference::host::TensorInitialize(source.host_view(), seed, dist);
// Define a functor called by TensorForEach<>
typedef test::ReLuFunc<View> ReLuFunc;
// Instantiate on host with TensorView and threshold value
ReLuFunc relu_func(source.host_view(), threshold);
// Invoke host-side for-each computation on the tensor
cutlass::reference::host::TensorForEach<
ReLuFunc,
View::kRank,
ReLuFunc>(source.size(), relu_func);
int errors = 0;
for (cutlass::MatrixCoord coord(0, 0); coord.row() < M; ++coord.row()) {
for (coord.column() = 0; coord.column() < N; ++coord.column()) {
if (source.at(coord) < threshold) {
++errors;
if (errors < 10) {
std::cout << "Error - source(" << coord << ") = "
<< source.at(coord) << " is less than threshold " << threshold << std::endl;
}
}
}
}
EXPECT_EQ(errors, 0)
<< "Result: " << source;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
+25
View File
@@ -0,0 +1,25 @@
/******************************************************************************
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are not permitted.
*
* 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 TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
#include "cutlass_unit_test.h"
#include "cutlass/util/platform.h"
TEST(unique_ptr, basic) {
cutlass::platform::unique_ptr<int> ptr(new int);
}