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,58 @@
|
||||
/***************************************************************************************************
|
||||
* 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 Defines layout functions used by TensorRef and derived classes.
|
||||
|
||||
Layout functions map logical coordinates to linear memory. They often require additional
|
||||
data to describe strides between elements.
|
||||
|
||||
Layout functions must implement all members in the public interface of IdentityTensorLayout<>
|
||||
defined in cutlass/tensor_ref.h.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "cutlass/cutlass.h"
|
||||
#include "cutlass/matrix_coord.h"
|
||||
#include "cutlass/layout/matrix.h"
|
||||
#include "cutlass/layout/pitch_linear.h"
|
||||
#include "cutlass/layout/tensor.h"
|
||||
#include "cutlass/layout/tensor_nhwc.h"
|
||||
#include "cutlass/layout/vector.h"
|
||||
|
||||
#include "cutlass/layout/tensor_op_multiplicand_sm70.h"
|
||||
#include "cutlass/layout/tensor_op_multiplicand_sm75.h"
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace cutlass {
|
||||
namespace layout {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
} // namespace layout
|
||||
} // namespace cutlass
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -0,0 +1,938 @@
|
||||
/***************************************************************************************************
|
||||
* 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 Defines layout functions used by TensorRef and derived classes.
|
||||
|
||||
Layout functions map logical coordinates to linear memory. They often require additional
|
||||
data to describe strides between elements.
|
||||
|
||||
Layout functions must implement all members in the public interface of IdentityTensorLayout<>
|
||||
defined in cutlass/tensor_ref.h.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "cutlass/cutlass.h"
|
||||
#include "cutlass/matrix_coord.h"
|
||||
#include "cutlass/matrix_traits.h"
|
||||
|
||||
namespace cutlass {
|
||||
namespace layout {
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Defines data layouts of various matrix formats usable by TensorRef and other classes.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Mapping function for row-major matrices.
|
||||
class RowMajor {
|
||||
public:
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 2;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 1;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = MatrixCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank, Index>;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Stride data member
|
||||
Stride stride_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Constructor
|
||||
CUTLASS_HOST_DEVICE
|
||||
RowMajor(Index ldm = 0): stride_(ldm) { }
|
||||
|
||||
/// Ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
RowMajor(Stride stride): stride_(stride) { }
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static RowMajor packed(MatrixCoord const &extent) {
|
||||
return RowMajor(extent.column());
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory.
|
||||
/// Assumes coordinate has convention (row, column)
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(MatrixCoord const &coord) const {
|
||||
return coord.row() * stride_[0] + coord.column();
|
||||
}
|
||||
|
||||
/// Inverse of layout function, mapping linear offset to logical coordinate
|
||||
CUTLASS_HOST_DEVICE
|
||||
MatrixCoord inverse(LongIndex offset) const {
|
||||
return MatrixCoord(Index(offset / stride_[0]), Index(offset % stride_[0]));
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index stride(int idx) const {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index & stride(int idx) {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(MatrixCoord const &extent) const {
|
||||
return extent.row() * stride_[0];
|
||||
}
|
||||
};
|
||||
|
||||
/// Mapping function for column-major matrices.
|
||||
class ColumnMajor {
|
||||
public:
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 2;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 1;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = MatrixCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank, Index>;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Stride data member
|
||||
Stride stride_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
ColumnMajor(Index ldm = 0): stride_(ldm) { }
|
||||
|
||||
/// Ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
ColumnMajor(Stride stride): stride_(stride) { }
|
||||
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static ColumnMajor packed(MatrixCoord const &extent) {
|
||||
return ColumnMajor(extent.row());
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory.
|
||||
/// Assumes coordinate has convention (row, column)
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(MatrixCoord const &coord) const {
|
||||
return coord.row() + coord.column() * stride_[0];
|
||||
}
|
||||
|
||||
/// Inverse of layout function, mapping linear offset to logical coordinate
|
||||
CUTLASS_HOST_DEVICE
|
||||
MatrixCoord inverse(LongIndex offset) const {
|
||||
return MatrixCoord(Index(offset % stride_[0]), Index(offset / stride_[0]));
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index stride(int idx) const {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index & stride(int idx) {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(MatrixCoord const &extent) const {
|
||||
return extent.column() * stride_[0];
|
||||
}
|
||||
};
|
||||
|
||||
/// Mapping function for interleaved matrices. Matrix is structured
|
||||
/// as row-major arrangement of fixed-size columns.
|
||||
template <int Interleave>
|
||||
struct RowMajorInterleaved {
|
||||
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 2;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 1;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = MatrixCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank, Index>;
|
||||
|
||||
/// Size of interleaved columns
|
||||
static int const kInterleave = Interleave;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Stride data member
|
||||
Stride stride_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
RowMajorInterleaved(Index ldm = 0): stride_(ldm) { }
|
||||
|
||||
/// Ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
RowMajorInterleaved(Stride stride): stride_(stride) { }
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static RowMajorInterleaved packed(MatrixCoord const &extent) {
|
||||
return RowMajorInterleaved(extent.column() * kInterleave);
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory.
|
||||
/// Assumes coordinate has convention (row, column)
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(MatrixCoord const &coord) const {
|
||||
Index row_major = coord.row() / kInterleave;
|
||||
Index row_minor = coord.row() % kInterleave;
|
||||
return row_major * stride_[0] + coord.column() * kInterleave + row_minor;
|
||||
}
|
||||
|
||||
/// Inverse of layout function, mapping linear offset to logical coordinate
|
||||
CUTLASS_HOST_DEVICE
|
||||
MatrixCoord inverse(LongIndex offset) const {
|
||||
|
||||
Index row_major = Index(offset / stride_[0]);
|
||||
Index residual = Index(offset % stride_[0]);
|
||||
|
||||
Index column = residual / kInterleave;
|
||||
Index row_minor = residual % kInterleave;
|
||||
|
||||
return MatrixCoord(row_major * kInterleave + row_minor, column);
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index stride(int idx) const {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index & stride(int idx) {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(MatrixCoord const &extent) const {
|
||||
return (extent.row() + kInterleave - 1) / kInterleave * stride_[0];
|
||||
}
|
||||
};
|
||||
|
||||
/// Mapping function for interleaved matrices. Matrix is structured
|
||||
/// as column-major arrangement of fixed-size rows.
|
||||
template <int Interleave>
|
||||
struct ColumnMajorInterleaved {
|
||||
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 2;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 1;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = MatrixCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank, Index>;
|
||||
|
||||
/// Size of interleaved columns
|
||||
static int const kInterleave = Interleave;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Stride data member
|
||||
Stride stride_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
ColumnMajorInterleaved(Index ldm = 0): stride_(ldm) { }
|
||||
|
||||
/// Ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
ColumnMajorInterleaved(Stride stride): stride_(stride) { }
|
||||
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static ColumnMajorInterleaved packed(MatrixCoord const &extent) {
|
||||
return ColumnMajorInterleaved(extent.row() * kInterleave);
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory.
|
||||
/// Assumes coordinate has convention (row, column)
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(MatrixCoord const &coord) const {
|
||||
Index column_major = coord.column() / kInterleave;
|
||||
Index column_minor = coord.column() % kInterleave;
|
||||
return column_major * stride_[0] + coord.row() * kInterleave + column_minor;
|
||||
}
|
||||
|
||||
/// Inverse of layout function, mapping linear offset to logical coordinate
|
||||
CUTLASS_HOST_DEVICE
|
||||
MatrixCoord inverse(LongIndex offset) const {
|
||||
|
||||
Index column_major = Index(offset / stride_[0]);
|
||||
Index residual = Index(offset % stride_[0]);
|
||||
|
||||
Index row = residual / kInterleave;
|
||||
Index column_minor = residual % kInterleave;
|
||||
|
||||
return MatrixCoord(row, column_major * kInterleave + column_minor);
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index stride(int idx) const {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index & stride(int idx) {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(MatrixCoord const &extent) const {
|
||||
return (extent.column() + kInterleave - 1) / kInterleave * stride_[0];
|
||||
}
|
||||
};
|
||||
|
||||
/// Enumerated type for canonical pitch-linear matrix layouts
|
||||
enum class Matrix {
|
||||
kColumnMajor, ///< leading dimension refers to stride between columns; stride along rows is 1
|
||||
kRowMajor ///< leading dimension refers to stride between rows; stride along columns is 1
|
||||
};
|
||||
|
||||
/// Mapping function for scenario in which layout is row-major or column-major but this information
|
||||
/// is only available at runtime.
|
||||
struct ContiguousMatrix {
|
||||
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 2;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 1;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = MatrixCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank, Index>;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Stride data member
|
||||
Stride stride_;
|
||||
|
||||
/// Enumerated type indicating canonical matrix layout
|
||||
Matrix layout_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
ContiguousMatrix(
|
||||
Index ldm = 0,
|
||||
Matrix layout = Matrix::kColumnMajor
|
||||
):
|
||||
stride_(ldm), layout_(layout) { }
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static ContiguousMatrix packed(
|
||||
MatrixCoord const &extent,
|
||||
Matrix layout = Matrix::kColumnMajor) {
|
||||
|
||||
Index ldm = 0;
|
||||
if (layout == Matrix::kColumnMajor) {
|
||||
ldm = extent.row();
|
||||
}
|
||||
else if (layout == Matrix::kRowMajor) {
|
||||
ldm = extent.column();
|
||||
}
|
||||
return ContiguousMatrix(ldm, layout);
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory.
|
||||
/// Assumes coordinate has convention (row, column)
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(MatrixCoord const &coord) const {
|
||||
if (layout_ == Matrix::kColumnMajor) {
|
||||
return coord.row() + coord.column() * stride_[0];
|
||||
}
|
||||
else if (layout_ == Matrix::kRowMajor) {
|
||||
return coord.row() * stride_[0] + coord.column();
|
||||
}
|
||||
else {
|
||||
// degenerate case
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Inverse of layout function, mapping linear offset to logical coordinate
|
||||
CUTLASS_HOST_DEVICE
|
||||
MatrixCoord inverse(LongIndex offset) const {
|
||||
// TODO
|
||||
return MatrixCoord(0, 0);
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index stride(int idx) const {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index & stride(int idx) {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(MatrixCoord const &extent) const {
|
||||
if (layout_ == Matrix::kColumnMajor) {
|
||||
return stride_[0] * extent.column();
|
||||
}
|
||||
else if (layout_ == Matrix::kRowMajor) {
|
||||
return stride_[0] * extent.row();
|
||||
}
|
||||
else {
|
||||
// degenerate case
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/// Mapping function for block-linear matrices. Matrix is structured
|
||||
/// as column-major arrangement of 2D tiles (that are column-major).
|
||||
template <int BlockRows, int BlockColumns>
|
||||
struct ColumnMajorBlockLinear {
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 2;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 1;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = MatrixCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank, Index>;
|
||||
|
||||
/// Size of a block in rows
|
||||
static int const kBlockRows = BlockRows;
|
||||
|
||||
/// Size of a block in columns
|
||||
static int const kBlockColumns = BlockColumns;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Stride data member
|
||||
Stride stride_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
ColumnMajorBlockLinear(Index ldm = 0): stride_(ldm) { }
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static ColumnMajorBlockLinear packed(MatrixCoord const &extent) {
|
||||
return ColumnMajorBlockLinear(extent.row() * kBlockRows * kBlockColumns);
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory.
|
||||
/// Assumes coordinate has convention (row, column)
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(MatrixCoord const &coord) const {
|
||||
return
|
||||
(coord.row() % kBlockRows) +
|
||||
(coord.column() % kBlockColumns) * kBlockRows +
|
||||
(coord.row() / kBlockRows) * kBlockRows * kBlockColumns +
|
||||
(coord.column() / kBlockColumns) * stride_[0];
|
||||
}
|
||||
|
||||
/// Inverse of layout function, mapping linear offset to logical coordinate
|
||||
CUTLASS_HOST_DEVICE
|
||||
MatrixCoord inverse(LongIndex offset) const {
|
||||
|
||||
// TODO
|
||||
return MatrixCoord(0, 0);
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index stride(int idx) const {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index & stride(int idx) {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(MatrixCoord const &extent) const {
|
||||
return (extent.column() + kBlockColumns - 1) / kBlockColumns * stride_[0];
|
||||
}
|
||||
};
|
||||
|
||||
/// Mapping function for block-linear matrices. Matrix is structured
|
||||
/// as row-major arrangement of 2D tiles (that are row-major)
|
||||
template <int BlockRows, int BlockColumns>
|
||||
struct RowMajorBlockLinear {
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 2;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 1;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = MatrixCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank, Index>;
|
||||
|
||||
/// Size of a block in rows
|
||||
static int const kBlockRows = BlockRows;
|
||||
|
||||
/// Size of a block in columns
|
||||
static int const kBlockColumns = BlockColumns;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Stride data member
|
||||
Stride stride_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
RowMajorBlockLinear(Index ldm = 0): stride_(ldm) { }
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static RowMajorBlockLinear packed(MatrixCoord const &extent) {
|
||||
return RowMajorBlockLinear(extent.column() * kBlockRows * kBlockColumns);
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory.
|
||||
/// Assumes coordinate has convention (row, column)
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(MatrixCoord const &coord) const {
|
||||
return
|
||||
(coord.column() % kBlockColumns) +
|
||||
(coord.row() % kBlockRows) * kBlockColumns +
|
||||
(coord.column() / kBlockColumns) * kBlockRows * kBlockColumns +
|
||||
(coord.row() / kBlockRows) * stride_[0];
|
||||
}
|
||||
|
||||
/// Inverse of layout function, mapping linear offset to logical coordinate
|
||||
CUTLASS_HOST_DEVICE
|
||||
MatrixCoord inverse(LongIndex offset) const {
|
||||
// TODO
|
||||
return MatrixCoord(0, 0);
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index stride(int idx) const {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index & stride(int idx) {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(MatrixCoord const &extent) const {
|
||||
return (extent.row() + kBlockRows - 1) / kBlockRows * stride_[0];
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct GeneralMatrix {
|
||||
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 2;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 2;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = MatrixCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank, Index>;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
MatrixLayout layout_id_;
|
||||
|
||||
/// Stride data member
|
||||
Stride stride_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
GeneralMatrix(): layout_id_(MatrixLayout::kColumnMajor), stride_(make_Coord(0, 1)) { }
|
||||
|
||||
/// Ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
GeneralMatrix(
|
||||
MatrixLayout layout_id,
|
||||
Index ldm,
|
||||
Index interleave): layout_id_(layout_id), stride_(make_Coord(ldm, interleave)) { }
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static GeneralMatrix packed(
|
||||
MatrixCoord const &extent,
|
||||
MatrixLayout layout_id = MatrixLayout::kColumnMajor,
|
||||
Index interleave = 1) {
|
||||
|
||||
Index c;
|
||||
if (layout_id == MatrixLayout::kRowMajor) {
|
||||
c = extent.column();
|
||||
}
|
||||
else {
|
||||
c = extent.row();
|
||||
}
|
||||
|
||||
Index ldm = c * interleave;
|
||||
|
||||
return GeneralMatrix(layout_id, ldm, interleave);
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory.
|
||||
/// Assumes coordinate has convention (row, column)
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(MatrixCoord const &coord) const {
|
||||
Index c, s;
|
||||
if (layout_id_ == MatrixLayout::kRowMajor) {
|
||||
c = coord.column();
|
||||
s = coord.row();
|
||||
}
|
||||
else {
|
||||
s = coord.column();
|
||||
c = coord.row();
|
||||
}
|
||||
|
||||
Index v = s / stride_[1];
|
||||
Index residual = (s % stride_[1]);
|
||||
|
||||
return LongIndex(c) * LongIndex(stride_[1]) + LongIndex(v) * LongIndex(stride_[0]) + residual;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
CUTLASS_HOST_DEVICE
|
||||
MatrixLayout layout_id() const {
|
||||
return layout_id_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
CUTLASS_HOST_DEVICE
|
||||
MatrixLayout & layout_id() {
|
||||
return layout_id_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index stride(int idx) const {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index & stride(int idx) {
|
||||
return stride_[idx];
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(MatrixCoord const &extent) const {
|
||||
Index s;
|
||||
if (layout_id_ == MatrixLayout::kRowMajor) {
|
||||
s = extent.row();
|
||||
}
|
||||
else {
|
||||
s = extent.column();
|
||||
}
|
||||
|
||||
Index v = Index((s + stride_[1] - 1) / stride_[1]);
|
||||
return LongIndex(v) * LongIndex(stride_[0]);
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Defines transposes of matrix layouts
|
||||
template <typename Layout>
|
||||
struct LayoutTranspose;
|
||||
|
||||
/// Transpose of row-major is column-major
|
||||
template <>
|
||||
struct LayoutTranspose<layout::RowMajor> {
|
||||
using type = layout::ColumnMajor;
|
||||
};
|
||||
|
||||
/// Transpose of column-major is row-major
|
||||
template <>
|
||||
struct LayoutTranspose<layout::ColumnMajor> {
|
||||
using type = layout::RowMajor;
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
} // namespace layout
|
||||
} // namespace cutlass
|
||||
@@ -0,0 +1,261 @@
|
||||
/***************************************************************************************************
|
||||
* 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 Defines layout functions used by TensorRef and derived classes for pitch-linear memory.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "cutlass/cutlass.h"
|
||||
#include "cutlass/coord.h"
|
||||
|
||||
namespace cutlass {
|
||||
namespace layout {
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Template defining a shape used by pitch-linear operators
|
||||
template <
|
||||
int Contiguous,
|
||||
int Strided
|
||||
>
|
||||
struct PitchLinearShape {
|
||||
static int const kContiguous = Contiguous;
|
||||
static int const kStrided = Strided;
|
||||
static int const kCount = Contiguous * Strided;
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Coordinate in pitch-linear space
|
||||
struct PitchLinearCoord : public Coord<2, int> {
|
||||
public:
|
||||
|
||||
/// Integer-valued index
|
||||
using Index = int;
|
||||
|
||||
/// Base type is a Coord of rank=2
|
||||
using Base = Coord<2, Index>;
|
||||
|
||||
private:
|
||||
|
||||
/// Rows dimension
|
||||
static int const kContiguous = 0;
|
||||
|
||||
/// Columns dimension
|
||||
static int const kStrided = 1;
|
||||
|
||||
public:
|
||||
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Default ctor
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinearCoord() { }
|
||||
|
||||
/// Constructs from Coord<2>
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinearCoord(Coord<2, Index> const &coord): Base(coord) { }
|
||||
|
||||
/// Helper to construct from a row and column
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinearCoord(Index contiguous_, Index strided_): Base(make_Coord(contiguous_, strided_)) { }
|
||||
|
||||
/// Returns the contiguous dimension
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index const & contiguous() const { return this->at(kContiguous); }
|
||||
|
||||
/// Returns the contiguous dimension
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index & contiguous() { return this->at(kContiguous); }
|
||||
|
||||
/// Returns the column of the coordinate
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index const & strided() const { return this->at(kStrided); }
|
||||
|
||||
/// Returns the column of the coordinate
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index & strided() { return this->at(kStrided); }
|
||||
|
||||
//
|
||||
// Coord operators
|
||||
//
|
||||
|
||||
/// Element-wise addition
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinearCoord operator+(Base const& b) const {
|
||||
return PitchLinearCoord(Base::operator+(b));
|
||||
}
|
||||
|
||||
/// Element-wise subtraction
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinearCoord operator-(Base const& b) const {
|
||||
return PitchLinearCoord(Base::operator-(b));
|
||||
}
|
||||
|
||||
/// Element-wise multiplication
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinearCoord operator*(Base const& b) const {
|
||||
return PitchLinearCoord(Base::operator*(b));
|
||||
}
|
||||
|
||||
/// Element-wise division
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinearCoord operator/(Base const& b) const {
|
||||
return PitchLinearCoord(Base::operator/(b));
|
||||
}
|
||||
|
||||
/// In-place addition
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinearCoord& operator+=(Base const& b) {
|
||||
Base::operator+=(b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// In-place subtraction
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinearCoord& operator-=(Base const& b) {
|
||||
Base::operator-=(b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// In-place multiplication
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinearCoord& operator*=(Base const& b) {
|
||||
Base::operator*=(b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// In-place division
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinearCoord& operator/=(Base const& b) {
|
||||
Base::operator/=(b);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Mapping function for pitch-linear memory
|
||||
class PitchLinear {
|
||||
public:
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 2;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 1;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = PitchLinearCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank, Index>;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Stride data member
|
||||
Stride stride_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Constructor
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinear(Index ldm = 0): stride_(ldm) { }
|
||||
|
||||
/// Constructor
|
||||
CUTLASS_HOST_DEVICE
|
||||
PitchLinear(Stride _stride): stride_(_stride) { }
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static PitchLinear packed(TensorCoord const &extent) {
|
||||
return PitchLinear(extent.contiguous());
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory.
|
||||
/// Assumes coordinate has convention (contiguous, strided)
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(TensorCoord const &coord) const {
|
||||
return coord.contiguous() + coord.strided() * stride_[0];
|
||||
}
|
||||
|
||||
/// Returns the logical coordinate given an offset.
|
||||
CUTLASS_HOST_DEVICE
|
||||
TensorCoord inverse(LongIndex index) const {
|
||||
return make_Coord(
|
||||
Index(index % stride_[0]),
|
||||
Index(index / stride_[0])
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index stride(int rank) const {
|
||||
return stride_[rank];
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Index & stride(int rank) {
|
||||
return stride_[rank];
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(TensorCoord const &extent) const {
|
||||
return extent.strided() * stride_[0];
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
} // namespace layout
|
||||
} // namespace cutlass
|
||||
|
||||
@@ -0,0 +1,446 @@
|
||||
/***************************************************************************************************
|
||||
* 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 Defines layout functions used by TensorRef and derived classes for common 4-D and 5-D
|
||||
tensor formats.
|
||||
|
||||
Layout functions map logical coordinates to linear memory. They often require additional
|
||||
data to describe strides between elements.
|
||||
|
||||
Layout functions must implement all members in the public interface of IdentityTensorLayout<>
|
||||
defined in cutlass/tensor_ref.h.
|
||||
*/
|
||||
#pragma once
|
||||
#include "assert.h"
|
||||
#include "cutlass/cutlass.h"
|
||||
#include "cutlass/fast_math.h"
|
||||
#include "cutlass/layout/matrix.h"
|
||||
#include "cutlass/coord.h"
|
||||
#include "cutlass/tensor_coord.h"
|
||||
|
||||
namespace cutlass {
|
||||
namespace layout {
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Defines data layouts of various tensor formats usable by TensorRef and other classes.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Mapping function for 4-D NHWC tensors.
|
||||
class TensorNHWC {
|
||||
public:
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 4;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 3;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate (n, h, w, c)
|
||||
using TensorCoord = Tensor4DCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank>;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Stride data member - [c, wc, hwc]
|
||||
Stride stride_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Constructor
|
||||
CUTLASS_HOST_DEVICE
|
||||
TensorNHWC(Stride const &stride = Stride(0)): stride_(stride) { }
|
||||
|
||||
/// Constructor
|
||||
CUTLASS_HOST_DEVICE
|
||||
TensorNHWC(typename Stride::Index c, typename Stride::Index wc, typename Stride::Index hwc): stride_(make_Coord(c, wc, hwc)) { }
|
||||
|
||||
/// Helper returns a layout to a tightly packed NHWC tensor.
|
||||
CUTLASS_HOST_DEVICE
|
||||
static TensorNHWC packed(TensorCoord const &extent) {
|
||||
return TensorNHWC(
|
||||
make_Coord(
|
||||
extent.c(),
|
||||
extent.w() * extent.c(),
|
||||
extent.h() * extent.w() * extent.c()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate (n, h, w, c) in linear memory.
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(TensorCoord const &coord) const {
|
||||
return coord.c() +
|
||||
LongIndex(stride_[0] * coord.w()) +
|
||||
LongIndex(stride_[1] * coord.h()) +
|
||||
LongIndex(stride_[2] * coord.n());
|
||||
}
|
||||
|
||||
/// Returns a RowMajor equivalent for a TensorNHWC layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
explicit operator RowMajor() {
|
||||
return RowMajor(stride_[0]);
|
||||
}
|
||||
|
||||
/// Returns the logical coordinate (n, h, w, c) from a given offset in linear memory.
|
||||
CUTLASS_HOST_DEVICE
|
||||
TensorCoord inverse(LongIndex index) const {
|
||||
|
||||
int n = 0, h = 0, w = 0, c = 0;
|
||||
|
||||
#if defined(__CUDA_ARCH__)
|
||||
int tmp = 0;
|
||||
c = int(index % static_cast<int>(stride_[0]));
|
||||
|
||||
unsigned int hw_mul, hw_shr, w_mul, w_shr, c_mul, c_shr;
|
||||
|
||||
find_divisor(hw_mul, hw_shr, stride_[2]);
|
||||
find_divisor(w_mul, w_shr, stride_[1]);
|
||||
find_divisor(c_mul, c_shr, stride_[0]);
|
||||
|
||||
fast_divmod(n, tmp, index, int(stride_[2]), hw_mul, hw_shr);
|
||||
fast_divmod(h, w, tmp, int(stride_[1]), w_mul, w_shr);
|
||||
fast_divmod(w, tmp, w, int(stride_[0]), c_mul, c_shr);
|
||||
#else
|
||||
|
||||
n = int(index / (stride_[0] * stride_[1] * stride_[2]));
|
||||
LongIndex residual = index % (stride_[0] * stride_[1] * stride_[2]);
|
||||
|
||||
h = int(residual / (stride_[0] * stride_[1]));
|
||||
residual = (residual % (stride_[0] * stride_[1]));
|
||||
|
||||
w = int(residual / stride_[0]);
|
||||
c = int(residual % stride_[0]);
|
||||
|
||||
#endif
|
||||
return TensorCoord(n, h, w, c);
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(TensorCoord const &extent) const {
|
||||
// it does not make sense if the extent is larger than stride
|
||||
// and we could not rely on the capacity calculation in such cases
|
||||
// we could move this checkers to debug code only
|
||||
if ((extent.c() > stride_[0])
|
||||
|| (extent.w() * stride_[0] > stride_[1])
|
||||
|| (extent.h() * stride_[1] > stride_[2])) {
|
||||
assert(0);
|
||||
}
|
||||
return extent.n() * stride_[2];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Mapping function for 4-D NCHW tensors.
|
||||
class TensorNCHW {
|
||||
public:
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 4;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 3;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = Tensor4DCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank>;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Stride data member - [c, wc, hwc]
|
||||
Stride stride_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Constructor
|
||||
CUTLASS_HOST_DEVICE
|
||||
TensorNCHW(Stride const &stride = Stride(0)): stride_(stride) { }
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static TensorNCHW packed(TensorCoord const &extent) {
|
||||
return TensorNCHW(
|
||||
make_Coord(
|
||||
extent.w(),
|
||||
extent.w() * extent.h(),
|
||||
extent.h() * extent.w() * extent.c()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory.
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(TensorCoord const &coord) const {
|
||||
return coord.w() +
|
||||
LongIndex(stride_[0] * coord.h()) +
|
||||
LongIndex(stride_[1] * coord.c()) +
|
||||
LongIndex(stride_[2] * coord.n());
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(TensorCoord const &extent) const {
|
||||
return extent.n() * stride_[2];
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Mapping function for 4-D NC/xHWx tensors.
|
||||
template <int Interleave>
|
||||
class TensorNCxHWx {
|
||||
public:
|
||||
|
||||
/// Interleaving quantity
|
||||
static int const kInterleave = Interleave;
|
||||
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 4;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 3;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = Tensor4DCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank>;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Stride data member - [c, wc, hwc]
|
||||
Stride stride_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Constructor
|
||||
CUTLASS_HOST_DEVICE
|
||||
TensorNCxHWx(Stride const &stride = Stride(0)): stride_(stride) { }
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static TensorNCxHWx packed(TensorCoord const &extent) {
|
||||
return TensorNCxHWx(
|
||||
make_Coord(
|
||||
kInterleave * extent.w(),
|
||||
kInterleave * extent.w() * extent.h(),
|
||||
extent.h() * extent.w() * extent.c()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory.
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(TensorCoord const &coord) const {
|
||||
|
||||
Index c_minor = (coord.c() % kInterleave);
|
||||
Index c_major = (coord.c() / kInterleave);
|
||||
|
||||
return c_minor +
|
||||
LongIndex(kInterleave * coord.w()) +
|
||||
LongIndex(stride_[0] * coord.h()) +
|
||||
LongIndex(stride_[1] * c_major) +
|
||||
LongIndex(stride_[2] * coord.n());
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(TensorCoord const &extent) const {
|
||||
return extent.n() * stride_[2];
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Mapping function for 4-D CxRSKx tensors.
|
||||
template <int Interleave>
|
||||
class TensorCxRSKx {
|
||||
public:
|
||||
|
||||
/// Interleaving quantity
|
||||
static int const kInterleave = Interleave;
|
||||
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 4;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 3;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = Tensor4DCoord;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank>;
|
||||
|
||||
private:
|
||||
//
|
||||
// Data members
|
||||
//
|
||||
|
||||
/// Stride data member - [c, wc, hwc]
|
||||
Stride stride_;
|
||||
|
||||
public:
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
/// Constructor
|
||||
CUTLASS_HOST_DEVICE
|
||||
TensorCxRSKx(Stride const &stride = Stride(0)): stride_(stride) { }
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static TensorCxRSKx packed(TensorCoord const &extent) {
|
||||
return TensorCxRSKx(
|
||||
make_Coord(
|
||||
kInterleave * extent.n(),
|
||||
kInterleave * extent.n() * extent.w(),
|
||||
kInterleave * extent.n() * extent.w() * extent.h()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory.
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(TensorCoord const &coord) const {
|
||||
|
||||
Index c_minor = (coord.c() % kInterleave);
|
||||
Index c_major = (coord.c() / kInterleave);
|
||||
|
||||
return c_minor +
|
||||
LongIndex(kInterleave * coord.n()) +
|
||||
LongIndex(stride_[0] * coord.w()) +
|
||||
LongIndex(stride_[1] * coord.h()) +
|
||||
LongIndex(stride_[2] * c_major);
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride & stride() {
|
||||
return stride_;
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(TensorCoord const &extent) const {
|
||||
return (extent.c() / kInterleave * stride_[2]);
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
} // namespace layout
|
||||
} // namespace cutlass
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,98 @@
|
||||
/***************************************************************************************************
|
||||
* 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 Defines layout functions used for rank=1 vectors.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "cutlass/cutlass.h"
|
||||
#include "cutlass/coord.h"
|
||||
|
||||
namespace cutlass {
|
||||
namespace layout {
|
||||
|
||||
/// Tensor layout for densely packed vectors.
|
||||
class PackedVectorLayout {
|
||||
public:
|
||||
/// Logical rank of tensor
|
||||
static int const kRank = 1;
|
||||
|
||||
/// Rank of stride vector
|
||||
static int const kStrideRank = 1;
|
||||
|
||||
/// Index type used for coordinates
|
||||
using Index = int32_t;
|
||||
|
||||
/// Long index type used for offsets
|
||||
using LongIndex = int64_t;
|
||||
|
||||
/// Logical coordinate
|
||||
using TensorCoord = Coord<kRank, Index>;
|
||||
|
||||
/// Stride vector
|
||||
using Stride = Coord<kStrideRank, Index>;
|
||||
|
||||
private:
|
||||
|
||||
//
|
||||
// No actual stride vector stored
|
||||
//
|
||||
|
||||
public:
|
||||
|
||||
//
|
||||
// Methods
|
||||
//
|
||||
|
||||
CUTLASS_HOST_DEVICE
|
||||
PackedVectorLayout() { }
|
||||
|
||||
/// Helper returns a layout to a tightly packed tensor
|
||||
CUTLASS_HOST_DEVICE
|
||||
static PackedVectorLayout packed(TensorCoord const &size) {
|
||||
return PackedVectorLayout();
|
||||
}
|
||||
|
||||
/// Returns the offset of a coordinate in linear memory
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex operator()(TensorCoord const &coord) const {
|
||||
return coord[0];
|
||||
}
|
||||
|
||||
/// Returns the stride of the layout
|
||||
CUTLASS_HOST_DEVICE
|
||||
Stride stride() const {
|
||||
return make_Coord(1);
|
||||
}
|
||||
|
||||
/// Compute the number of contiguous elements needed to store a tensor with the given size
|
||||
CUTLASS_HOST_DEVICE
|
||||
LongIndex capacity(TensorCoord const &size) const {
|
||||
return size[0];
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace layout
|
||||
} // namespace cutlass
|
||||
Reference in New Issue
Block a user