Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cutlass 3xTF32,DMMA based L2/cosine distance kernels for SM 8.0 or higher #939

Merged
merged 28 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
20648c5
cutlass based euclidean expanded, cosine kernels
mdoijade Oct 20, 2022
a9dabc8
add prior ampere pairwisedistmat kernel to prevent redundant kernel c…
mdoijade Oct 21, 2022
1a45bfa
add noexcept to the functor methods
mdoijade Oct 21, 2022
c6f091b
merge branch 22.12 and resolve conflicts
mdoijade Oct 21, 2022
7786fcb
fix comments, remove redundant code and fix formatting issues
mdoijade Oct 27, 2022
181fc40
add cutlass cmake support for raft with custom namespace, fix formati…
mdoijade Oct 28, 2022
3d34545
fix formatting issues
mdoijade Oct 28, 2022
02c23ed
fix the cutlass_include_dir path in cmake
mdoijade Nov 3, 2022
7933436
fix bugs in get_cutlass cmake to use cutlass provided properties corr…
mdoijade Nov 4, 2022
d4bdec5
remove the cutlass namespace setting in test cmakefiles as it is not …
mdoijade Nov 4, 2022
d26bcef
temp remove dist dependency from cutlass to check if it works in ci/cd
mdoijade Nov 4, 2022
4df4185
merge branch-22.12 latest changes
mdoijade Nov 7, 2022
451c3c0
fix get_cutlass.cmake to work with pylibraft by using NvidiaCutlass i…
mdoijade Nov 10, 2022
7b512f9
fix get_cutlass install path, make changes as per review comments
mdoijade Nov 10, 2022
a05e1e2
merge branch-22.12
mdoijade Nov 10, 2022
d32b4c0
fix clang format issues
mdoijade Nov 10, 2022
f7c440a
temp fix to check if python build works
mdoijade Nov 11, 2022
b1a1fd7
add raft-exports instead of raft-distance-exports as other raft compo…
mdoijade Nov 15, 2022
4ef44e7
make cutlass to depend only on raft_distance and add raft_distance de…
mdoijade Nov 16, 2022
186fcc7
fix cmake formatting issues
mdoijade Nov 16, 2022
8aa8909
prevent cutlass based pairwise dist kernels to be disabled on cuda 12…
mdoijade Nov 16, 2022
abfd493
Moving cutlass dependency to distance and nn to keep them separate.
cjnolet Nov 16, 2022
f1b1239
Adding CUTLASS to build docs as dependency
cjnolet Nov 16, 2022
32e6052
Updating to export to both distance and nn
cjnolet Nov 16, 2022
f6de9ee
Adding cutlass as private dependency
cjnolet Nov 16, 2022
9bf0647
Making cutlass INTERFACE in raft::nn and raft::distance
cjnolet Nov 16, 2022
8f0119a
Using proper exports per Robert Maynard's suggestion.
cjnolet Nov 16, 2022
6ad4fd1
Adding cutlass as private dependency of lib targets
cjnolet Nov 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix comments, remove redundant code and fix formatting issues
  • Loading branch information
mdoijade committed Oct 27, 2022
commit 7786fcb882bda7a9b243a57de5544f1a09b349de
8 changes: 5 additions & 3 deletions cpp/include/raft/distance/detail/cosine.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ namespace detail {

template <typename DataT, typename AccT>
struct CosineOp {
__device__ __host__ CosineOp() noexcept {}
__device__ __host__ AccT operator()(DataT& aNorm, const DataT& bNorm, DataT& accVal) const noexcept
__device__ CosineOp() noexcept {}
__device__ AccT operator()(DataT& aNorm,
const DataT& bNorm,
DataT& accVal) const noexcept
{
return static_cast<AccT>(1.0) - (AccT)(accVal / (aNorm * bNorm));
}
__device__ __host__ AccT operator()(DataT aData) const noexcept { return aData; }
__device__ AccT operator()(DataT aData) const noexcept { return aData; }
};

/**
Expand Down
4 changes: 2 additions & 2 deletions cpp/include/raft/distance/detail/distance.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -616,15 +616,15 @@ void distance(const InType* x,
* worksize, the number of bytes of workspace required
*/

// Default final op functor which facilitates elementwise operation on
// Default final op functor which facilitates elementwise operation on
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, then even the previous overload of distance requires its FinalLambda to be such a functor. Could you amend the docstring at line 568 accordingly?

// final distance value if any.
template <typename AccType, typename OutType, typename Index>
struct default_fin_op {
__host__ __device__ default_fin_op() noexcept {};
// functor signature.
__host__ __device__ OutType operator()(AccType d_val, Index g_d_idx) const noexcept
{
return d_val;
return d_val;
}
};

Expand Down
10 changes: 6 additions & 4 deletions cpp/include/raft/distance/detail/euclidean.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ template <typename DataT, typename AccT>
struct L2ExpandedOp {
bool sqrt;

__device__ __host__ L2ExpandedOp() noexcept : sqrt(false) {}
__device__ __host__ L2ExpandedOp(bool isSqrt) noexcept : sqrt(isSqrt) {}
__device__ __host__ AccT operator()(DataT& aNorm, const DataT& bNorm, DataT& accVal) const noexcept
__device__ L2ExpandedOp() noexcept : sqrt(false) {}
__device__ L2ExpandedOp(bool isSqrt) noexcept : sqrt(isSqrt) {}
__device__ AccT operator()(DataT& aNorm,
const DataT& bNorm,
DataT& accVal) const noexcept
{
AccT outVal = aNorm + bNorm - DataT(2.0) * accVal;
return sqrt ? raft::mySqrt(outVal) : outVal;
}

__device__ __host__ AccT operator()(DataT aData) const noexcept { return aData; }
__device__ AccT operator()(DataT aData) const noexcept { return aData; }
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ void cutlassDistanceKernel(const DataT* x,
DistanceFn dist_op,
cudaStream_t stream)
{
static_assert(!(std::is_same<OutT, bool>::value),
"OutType bool is not supported use uint8_t instead");

using EpilogueOutputOp =
cutlass::epilogue::thread::PairwiseDistanceEpilogueElementwise<DataT, // ElementC_
AccT, // ElementAccumulator_
Expand All @@ -95,8 +98,7 @@ void cutlassDistanceKernel(const DataT* x,
constexpr int Alignment = VecLen;

// default initialize problem size with row major inputs
auto problem_size =
cutlass::gemm::GemmCoord(static_cast<int>(n), static_cast<int>(m), static_cast<int>(k));
auto problem_size = cutlass::gemm::GemmCoord(n, m, k);

using cutlassDistKernel =
typename cutlass::gemm::kernel::PairwiseDistanceGemm<DataT,
Expand All @@ -117,12 +119,11 @@ void cutlassDistanceKernel(const DataT* x,
gemm_lda = ldb;
gemm_ldb = lda;
} else {
problem_size =
cutlass::gemm::GemmCoord(static_cast<int>(m), static_cast<int>(n), static_cast<int>(k));
a = x;
b = y;
gemm_lda = lda;
gemm_ldb = ldb;
problem_size = cutlass::gemm::GemmCoord(m, n, k);
a = x;
b = y;
gemm_lda = lda;
gemm_ldb = ldb;
}

typename cutlassDist::Arguments arguments{
Expand Down
40 changes: 13 additions & 27 deletions cpp/include/raft/distance/detail/pairwise_distance_epilogue.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
/***************************************************************************************************
* Copyright (c) 2017 - 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
/*
* Copyright (c) 2018-2022, NVIDIA CORPORATION.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* http://www.apache.org/licenses/LICENSE-2.0
*
* 2. 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.
*
* 3. Neither the name of the copyright holder 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*
**************************************************************************************************/
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*! \file
\brief Epilogue for threadblock scoped GEMMs using Tensor Ops.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2018-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//
/*! \file
\brief Functor performing distance operations used by epilogues of pairwise distance
Expand Down Expand Up @@ -106,18 +122,7 @@ class PairwiseDistanceEpilogueElementwise {

/// Functionally required for serial reduction in the epilogue
CUTLASS_HOST_DEVICE
void set_k_partition(int k_partition, int k_partition_count)
{
#if 0
if (k_partition) {
beta_ = ElementCompute(1);
}

if (k_partition != k_partition_count - 1) {
skip_elementwise_ = true;
}
#endif
}
void set_k_partition(int k_partition, int k_partition_count) {}

/// Applies the operation when is_source_needed() is true
CUTLASS_HOST_DEVICE
Expand Down
23 changes: 19 additions & 4 deletions cpp/include/raft/distance/detail/pairwise_distance_gemm.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2018-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "cutlass/cutlass.h"
Expand All @@ -7,7 +23,6 @@
#include "cutlass/layout/matrix.h"
#include "cutlass/layout/tensor.h"

//#include "./epilogue_with_bcast_threadblock.h"
#include "pairwise_distance_epilogue.h"

/////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -44,7 +59,7 @@ struct PairwiseDistanceGemm {

/// Threadblock-level tile size (concept: GemmShape)
using ThreadblockShape =
cutlass::gemm::GemmShape<128, 128, 16>; // <- threadblock tile M = 128, N = 64, K = 16
cutlass::gemm::GemmShape<128, 128, 16>; // <- threadblock tile M = 128, N = 128, K = 16
/// Warp-level tile size (concept: GemmShape)
// This code section describes tile size a warp will compute
using WarpShape = cutlass::gemm::GemmShape<64, 64, 16>; // <- warp tile M = 64, N = 64, K = 16
Expand Down Expand Up @@ -145,10 +160,10 @@ struct PairwiseDistanceGemm<double,
// using Transform = cutlass::ComplexTransform::kNone;
// Threadblock-level tile size (concept: GemmShape)
using ThreadblockShape =
cutlass::gemm::GemmShape<64, 64, 16>; // <- threadblock tile M = 128, N = 64, K = 16
cutlass::gemm::GemmShape<64, 64, 16>; // <- threadblock tile M = 64, N = 64, K = 16
/// Warp-level tile size (concept: GemmShape)
// This code section describes tile size a warp will compute
using WarpShape = cutlass::gemm::GemmShape<32, 32, 16>; // <- warp tile M = 64, N = 64, K = 16
using WarpShape = cutlass::gemm::GemmShape<32, 32, 16>; // <- warp tile M = 32, N = 32, K = 16
/// Warp-level tile size (concept: GemmShape)
// This code section describes the size of MMA op
using InstructionShape = cutlass::gemm::GemmShape<8, 8, 4>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
/***************************************************************************************************
* Copyright (c) 2017 - 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
/*
* Copyright (c) 2018-2022, NVIDIA CORPORATION.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* http://www.apache.org/licenses/LICENSE-2.0
*
* 2. 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.
*
* 3. Neither the name of the copyright holder 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*
**************************************************************************************************/
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*! \file
\brief Epilogue for threadblock scoped GEMMs using Tensor Ops.

Expand Down Expand Up @@ -78,7 +64,6 @@ class PredicatedTileIteratorNormVec {

using Element = Element_;

// using Layout = layout::RowMajor;
using Layout = Layout_;
using TensorRef = TensorRef<Element, Layout>;
using ConstTensorRef = typename TensorRef::ConstTensorRef;
Expand Down
2 changes: 0 additions & 2 deletions cpp/include/raft/distance/distance.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ void pairwise_distance(const raft::handle_t& handle,
detail::pairwise_distance_impl<Type, Index_, raft::distance::DistanceType::CosineExpanded>(
x, y, dist, m, n, k, workspace, handle.get_stream(), isRowMajor);
break;
#if 0
case raft::distance::DistanceType::L1:
detail::pairwise_distance_impl<Type, Index_, raft::distance::DistanceType::L1>(
x, y, dist, m, n, k, workspace, handle.get_stream(), isRowMajor);
Expand Down Expand Up @@ -365,7 +364,6 @@ void pairwise_distance(const raft::handle_t& handle,
pairwise_distance_impl<Type, Index_, raft::distance::DistanceType::CorrelationExpanded>(
x, y, dist, m, n, k, workspace, handle.get_stream(), isRowMajor);
break;
#endif
default: THROW("Unknown or unsupported distance metric '%d'!", (int)metric);
};
}
Expand Down