tile_iterator_wmma_tensor_op.h
Go to the documentation of this file.
/***************************************************************************************************
* 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.
*
**************************************************************************************************/
#pragma once
#if !defined(__clang__)
#include "cutlass/cutlass.h"
#include "cutlass/wmma_array.h"
#include "cutlass/layout/matrix.h"
#include "cutlass/layout/pitch_linear.h"
#include "cutlass/tensor_ref.h"
#include "cutlass/epilogue/warp/wmma_tensor_op_policy.h"
namespace cutlass {
namespace epilogue {
namespace warp {
template <
typename WarpShape,
typename OperatorShape,
typename OperatorFragment,
typename Layout
>
class TileIteratorWmmaTensorOp;
template <
typename WarpShape_,
typename OperatorShape_,
typename OperatorFragment_
>
class TileIteratorWmmaTensorOp<WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor> {
public:
using WarpShape = WarpShape_;
using OperatorShape = OperatorShape_;
using OperatorFragment = OperatorFragment_;
using Layout = layout::RowMajor;
//
// Derived types
//
using WmmaDataType = typename OperatorFragment::element_type;
using Element = typename cutlass::arch::WmmaToCutlassDataType<WmmaDataType>::Type;
using TensorRef = TensorRef<Element, Layout>;
using TensorCoord = MatrixCoord;
using Index = typename TensorRef::Index;
using LongIndex = typename TensorRef::LongIndex;
using Policy = WmmaTensorOpPolicy<WarpShape, OperatorShape, Layout>;
using Shape = MatrixShape<
Policy::kRowsPerIteration,
WarpShape::kN
>;
using Fragment = WmmaFragmentArray<OperatorFragment, Policy::OperatorCount::kColumn * Policy::kWmmaFragmentsPerAccess>;
//using AccumulatorTile = typename Operator::FragmentC;
// (Epilogue shared memory padding for WMMA Gemm kernel is set to run optimaly on Turing)
using Padding = MatrixShape<
0,
4 * Policy::kElementsPerAccess
>;
private:
//using AccessType = AlignedArray<Element, Policy::kElementsPerAccess>;
//
// Data members
//
TensorRef ref_;
public:
CUTLASS_HOST_DEVICE
TileIteratorWmmaTensorOp(): ref_(nullptr) {
}
CUTLASS_HOST_DEVICE
TileIteratorWmmaTensorOp(
TensorRef const &ref,
unsigned lane_id
): ref_(ref) {
}
CUTLASS_HOST_DEVICE
TileIteratorWmmaTensorOp & add_pointer_offset(Index pointer_offset) {
ref_.add_pointer_offset(pointer_offset);
return *this;
}
CUTLASS_HOST_DEVICE
TileIteratorWmmaTensorOp & add_tile_offset(TensorCoord const &tile_offset) {
ref_.add_coord_offset({tile_offset.row() * OperatorShape::kM, tile_offset.column() * WarpShape::kN});
return *this;
}
CUTLASS_HOST_DEVICE
TileIteratorWmmaTensorOp & operator+=(TensorCoord const &tile_offset) {
add_tile_offset(tile_offset);
return *this;
}
CUTLASS_HOST_DEVICE
void store_with_pointer_offset(Fragment const &frag, Index pointer_offset) {
for(int n=0; n < Policy::OperatorCount::kColumn; n++) {
WmmaDataType* ptr = reinterpret_cast< WmmaDataType*> (ref_.data() + ref_.offset({0, n * OperatorShape::kN}) + pointer_offset);
nvcuda::wmma::store_matrix_sync(
ptr,
frag[n],
ref_.stride()[0],
nvcuda::wmma::layout_t::mem_row_major
);
}
}
CUTLASS_HOST_DEVICE
void store(Fragment const &frag) {
store_with_pointer_offset(frag, 0);
}
CUTLASS_HOST_DEVICE
void load_with_pointer_offset(Fragment &frag, Index pointer_offset) const {
for(int n=0; n < Policy::OperatorCount::kColumn; n++) {
WmmaDataType* ptr = reinterpret_cast< WmmaDataType*> (ref_.data() + ref_.offset({0, n * OperatorShape::kN}) + pointer_offset);
nvcuda::wmma::load_matrix_sync(
frag[n],
ptr,
ref_.stride()[0],
nvcuda::wmma::layout_t::mem_row_major
);
}
}
CUTLASS_HOST_DEVICE
void load(Fragment &frag) const {
load_with_pointer_offset(frag, 0);
}
};
} // namespace warp
} // namespace epilogue
} // namespace cutlass
#endif // !defined(__clang__)
cutlass::MatrixShape
Describes the size of a matrix tile.
Definition: matrix_shape.h:42
cutlass::MatrixCoord::column
CUTLASS_HOST_DEVICE Index const & column() const
Returns the column of the coordinate.
Definition: matrix_coord.h:85
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::add_pointer_offset
CUTLASS_HOST_DEVICE TileIteratorWmmaTensorOp & add_pointer_offset(Index pointer_offset)
Adds a pointer offset.
Definition: tile_iterator_wmma_tensor_op.h:138
wmma_array.h
Statically sized array of elements that accommodates all CUTLASS-supported numeric types and is safe ...
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::add_tile_offset
CUTLASS_HOST_DEVICE TileIteratorWmmaTensorOp & add_tile_offset(TensorCoord const &tile_offset)
advances in units of whole tiles along the logical coordinate space of the tensor ...
Definition: tile_iterator_wmma_tensor_op.h:145
cutlass
Definition: aligned_buffer.h:35
tensor_ref.h
Defines a structure containing strides, bounds, and a pointer to tensor data.
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::TileIteratorWmmaTensorOp
CUTLASS_HOST_DEVICE TileIteratorWmmaTensorOp()
Default constructor.
Definition: tile_iterator_wmma_tensor_op.h:124
cutlass::TensorRef::data
CUTLASS_HOST_DEVICE Element * data() const
Returns the pointer to referenced data.
Definition: tensor_ref.h:254
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::Policy
WmmaTensorOpPolicy< WarpShape, OperatorShape, Layout > Policy
Definition: tile_iterator_wmma_tensor_op.h:84
cutlass::MatrixCoord::row
CUTLASS_HOST_DEVICE Index const & row() const
Returns the row of the coordinate.
Definition: matrix_coord.h:77
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::Index
typename TensorRef::Index Index
Definition: tile_iterator_wmma_tensor_op.h:81
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::operator+=
CUTLASS_HOST_DEVICE TileIteratorWmmaTensorOp & operator+=(TensorCoord const &tile_offset)
Definition: tile_iterator_wmma_tensor_op.h:152
cutlass::TensorRef::add_coord_offset
CUTLASS_HOST_DEVICE TensorRef & add_coord_offset(TensorCoord const &coord)
Adds an offset to each pointer.
Definition: tensor_ref.h:326
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::WmmaDataType
typename OperatorFragment::element_type WmmaDataType
Definition: tile_iterator_wmma_tensor_op.h:77
cutlass::TensorRef::stride
CUTLASS_HOST_DEVICE Stride stride() const
Returns the layout object&#39;s stride vector.
Definition: tensor_ref.h:277
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::load
CUTLASS_HOST_DEVICE void load(Fragment &frag) const
Load.
Definition: tile_iterator_wmma_tensor_op.h:201
nullptr
#define nullptr
nullptr
Definition: platform.h:144
cutlass::TensorRef< Element, Layout >
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::store_with_pointer_offset
CUTLASS_HOST_DEVICE void store_with_pointer_offset(Fragment const &frag, Index pointer_offset)
Store.
Definition: tile_iterator_wmma_tensor_op.h:159
CUTLASS_HOST_DEVICE
#define CUTLASS_HOST_DEVICE
Definition: cutlass.h:89
cutlass::TensorRef::offset
CUTLASS_HOST_DEVICE LongIndex offset(TensorCoord const &coord) const
Computes the offset of an index from the origin of the tensor.
Definition: tensor_ref.h:301
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::TileIteratorWmmaTensorOp
CUTLASS_HOST_DEVICE TileIteratorWmmaTensorOp(TensorRef const &ref, unsigned lane_id)
Constructor from TensorRef.
Definition: tile_iterator_wmma_tensor_op.h:130
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::WarpShape
WarpShape_ WarpShape
Definition: tile_iterator_wmma_tensor_op.h:69
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::load_with_pointer_offset
CUTLASS_HOST_DEVICE void load_with_pointer_offset(Fragment &frag, Index pointer_offset) const
Load.
Definition: tile_iterator_wmma_tensor_op.h:183
cutlass::TensorRef::Index
typename Layout::Index Index
Index type.
Definition: tensor_ref.h:165
cutlass::layout::RowMajor
Mapping function for row-major matrices.
Definition: layout/matrix.h:50
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::LongIndex
typename TensorRef::LongIndex LongIndex
Definition: tile_iterator_wmma_tensor_op.h:82
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::Fragment
WmmaFragmentArray< OperatorFragment, Policy::OperatorCount::kColumn *Policy::kWmmaFragmentsPerAccess > Fragment
This is the fragment size produced by one access of the iterator.
Definition: tile_iterator_wmma_tensor_op.h:93
wmma_tensor_op_policy.h
Defines basic structures needed for implementing the warp-scoped phase of the epilogue. These quantities assume a &#39;column-major&#39; arrangement of TensorOp instructions, of which a row-oriented slice is visible per iteration.
matrix.h
Defines layout functions used by TensorRef and derived classes.
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::Element
typename cutlass::arch::WmmaToCutlassDataType< WmmaDataType >::Type Element
Data Type of element stored in nvcuda::wmma::frament.
Definition: tile_iterator_wmma_tensor_op.h:78
pitch_linear.h
Defines layout functions used by TensorRef and derived classes for pitch-linear memory.
cutlass::TensorRef::add_pointer_offset
CUTLASS_HOST_DEVICE TensorRef & add_pointer_offset(LongIndex offset_)
Adds an offset to each pointer.
Definition: tensor_ref.h:319
cutlass::epilogue::warp::TileIteratorWmmaTensorOp
Template for reading and writing tiles of accumulators to shared memory.
Definition: tile_iterator_wmma_tensor_op.h:56
cutlass::epilogue::warp::TileIteratorWmmaTensorOp< WarpShape_, OperatorShape_, OperatorFragment_, layout::RowMajor >::store
CUTLASS_HOST_DEVICE void store(Fragment const &frag)
Store.
Definition: tile_iterator_wmma_tensor_op.h:177
cutlass.h
Basic include for CUTLASS.
cutlass::MatrixCoord
Definition: matrix_coord.h:39
cutlass::TensorRef::LongIndex
typename Layout::LongIndex LongIndex
Long index used for pointer offsets.
Definition: tensor_ref.h:168