Skip to content
Merged
12 changes: 12 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,18 @@ if(NOT BUILD_CPU_ONLY)
src/neighbors/detail/cagra/compute_distance_standard_InnerProduct_uint8_uint32_dim128_t8.cu
src/neighbors/detail/cagra/compute_distance_standard_InnerProduct_uint8_uint32_dim256_t16.cu
src/neighbors/detail/cagra/compute_distance_standard_InnerProduct_uint8_uint32_dim512_t32.cu
src/neighbors/detail/cagra/compute_distance_standard_L1_float_uint32_dim128_t8.cu
src/neighbors/detail/cagra/compute_distance_standard_L1_float_uint32_dim256_t16.cu
src/neighbors/detail/cagra/compute_distance_standard_L1_float_uint32_dim512_t32.cu
src/neighbors/detail/cagra/compute_distance_standard_L1_half_uint32_dim128_t8.cu
src/neighbors/detail/cagra/compute_distance_standard_L1_half_uint32_dim256_t16.cu
src/neighbors/detail/cagra/compute_distance_standard_L1_half_uint32_dim512_t32.cu
src/neighbors/detail/cagra/compute_distance_standard_L1_int8_uint32_dim128_t8.cu
src/neighbors/detail/cagra/compute_distance_standard_L1_int8_uint32_dim256_t16.cu
src/neighbors/detail/cagra/compute_distance_standard_L1_int8_uint32_dim512_t32.cu
src/neighbors/detail/cagra/compute_distance_standard_L1_uint8_uint32_dim128_t8.cu
src/neighbors/detail/cagra/compute_distance_standard_L1_uint8_uint32_dim256_t16.cu
src/neighbors/detail/cagra/compute_distance_standard_L1_uint8_uint32_dim512_t32.cu
src/neighbors/detail/cagra/compute_distance_standard_L2Expanded_float_uint32_dim128_t8.cu
src/neighbors/detail/cagra/compute_distance_standard_L2Expanded_float_uint32_dim256_t16.cu
src/neighbors/detail/cagra/compute_distance_standard_L2Expanded_float_uint32_dim512_t32.cu
Expand Down
48 changes: 47 additions & 1 deletion cpp/src/neighbors/detail/cagra/compute_distance-ext.cuh

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion cpp/src/neighbors/detail/cagra/compute_distance.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION.
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -22,61 +22,73 @@ template struct instance_selector<
standard_descriptor_spec<DistanceType::L2Expanded, 8, 128, float, uint32_t, float>,
standard_descriptor_spec<DistanceType::InnerProduct, 8, 128, float, uint32_t, float>,
standard_descriptor_spec<DistanceType::CosineExpanded, 8, 128, float, uint32_t, float>,
standard_descriptor_spec<DistanceType::L1, 8, 128, float, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 8, 128, 8, 2, half, float, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 8, 128, 8, 4, half, float, uint32_t, float>,
standard_descriptor_spec<DistanceType::L2Expanded, 16, 256, float, uint32_t, float>,
standard_descriptor_spec<DistanceType::InnerProduct, 16, 256, float, uint32_t, float>,
standard_descriptor_spec<DistanceType::CosineExpanded, 16, 256, float, uint32_t, float>,
standard_descriptor_spec<DistanceType::L1, 16, 256, float, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 16, 256, 8, 2, half, float, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 16, 256, 8, 4, half, float, uint32_t, float>,
standard_descriptor_spec<DistanceType::L2Expanded, 32, 512, float, uint32_t, float>,
standard_descriptor_spec<DistanceType::InnerProduct, 32, 512, float, uint32_t, float>,
standard_descriptor_spec<DistanceType::CosineExpanded, 32, 512, float, uint32_t, float>,
standard_descriptor_spec<DistanceType::L1, 32, 512, float, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 32, 512, 8, 2, half, float, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 32, 512, 8, 4, half, float, uint32_t, float>,
standard_descriptor_spec<DistanceType::L2Expanded, 8, 128, half, uint32_t, float>,
standard_descriptor_spec<DistanceType::InnerProduct, 8, 128, half, uint32_t, float>,
standard_descriptor_spec<DistanceType::CosineExpanded, 8, 128, half, uint32_t, float>,
standard_descriptor_spec<DistanceType::L1, 8, 128, half, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 8, 128, 8, 2, half, half, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 8, 128, 8, 4, half, half, uint32_t, float>,
standard_descriptor_spec<DistanceType::L2Expanded, 16, 256, half, uint32_t, float>,
standard_descriptor_spec<DistanceType::InnerProduct, 16, 256, half, uint32_t, float>,
standard_descriptor_spec<DistanceType::CosineExpanded, 16, 256, half, uint32_t, float>,
standard_descriptor_spec<DistanceType::L1, 16, 256, half, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 16, 256, 8, 2, half, half, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 16, 256, 8, 4, half, half, uint32_t, float>,
standard_descriptor_spec<DistanceType::L2Expanded, 32, 512, half, uint32_t, float>,
standard_descriptor_spec<DistanceType::InnerProduct, 32, 512, half, uint32_t, float>,
standard_descriptor_spec<DistanceType::CosineExpanded, 32, 512, half, uint32_t, float>,
standard_descriptor_spec<DistanceType::L1, 32, 512, half, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 32, 512, 8, 2, half, half, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 32, 512, 8, 4, half, half, uint32_t, float>,
standard_descriptor_spec<DistanceType::L2Expanded, 8, 128, int8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::InnerProduct, 8, 128, int8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::CosineExpanded, 8, 128, int8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::L1, 8, 128, int8_t, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 8, 128, 8, 2, half, int8_t, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 8, 128, 8, 4, half, int8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::L2Expanded, 16, 256, int8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::InnerProduct, 16, 256, int8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::CosineExpanded, 16, 256, int8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::L1, 16, 256, int8_t, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 16, 256, 8, 2, half, int8_t, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 16, 256, 8, 4, half, int8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::L2Expanded, 32, 512, int8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::InnerProduct, 32, 512, int8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::CosineExpanded, 32, 512, int8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::L1, 32, 512, int8_t, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 32, 512, 8, 2, half, int8_t, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 32, 512, 8, 4, half, int8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::L2Expanded, 8, 128, uint8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::InnerProduct, 8, 128, uint8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::CosineExpanded, 8, 128, uint8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::L1, 8, 128, uint8_t, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 8, 128, 8, 2, half, uint8_t, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 8, 128, 8, 4, half, uint8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::L2Expanded, 16, 256, uint8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::InnerProduct, 16, 256, uint8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::CosineExpanded, 16, 256, uint8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::L1, 16, 256, uint8_t, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 16, 256, 8, 2, half, uint8_t, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 16, 256, 8, 4, half, uint8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::L2Expanded, 32, 512, uint8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::InnerProduct, 32, 512, uint8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::CosineExpanded, 32, 512, uint8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::L1, 32, 512, uint8_t, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 32, 512, 8, 2, half, uint8_t, uint32_t, float>,
vpq_descriptor_spec<DistanceType::L2Expanded, 32, 512, 8, 4, half, uint8_t, uint32_t, float>,
standard_descriptor_spec<DistanceType::BitwiseHamming, 8, 128, uint8_t, uint32_t, float>,
Expand Down
12 changes: 6 additions & 6 deletions cpp/src/neighbors/detail/cagra/compute_distance_00_generate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION.
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
# SPDX-License-Identifier: Apache-2.0
import datetime
import os
Expand All @@ -19,14 +19,14 @@
*
*/

{{includes}}
{includes}

namespace cuvs::neighbors::cagra::detail {{{{
namespace cuvs::neighbors::cagra::detail {{

using namespace cuvs::distance;
{{content}}
{content}

}}}} // namespace cuvs::neighbors::cagra::detail
}} // namespace cuvs::neighbors::cagra::detail
"""

mxdim_team = [(128, 8), (256, 16), (512, 32)]
Expand Down Expand Up @@ -65,7 +65,7 @@
for type_path, (data_t, idx_t, distance_t) in search_types.items():
for mxdim, team in mxdim_team:
# CAGRA
for metric in ["L2Expanded", "InnerProduct", "CosineExpanded"]:
for metric in ["L2Expanded", "InnerProduct", "CosineExpanded", "L1"]:
path = f"compute_distance_standard_{metric}_{type_path}_dim{mxdim}_t{team}.cu"
includes = '#include "compute_distance_standard-impl.cuh"'
params = f"{metric_prefix}{metric}, {team}, {mxdim}, {data_t}, {idx_t}, {distance_t}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION.
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
Expand All @@ -15,32 +15,37 @@
namespace cuvs::neighbors::cagra::detail {
namespace {
template <typename DATA_T, typename DISTANCE_T, cuvs::distance::DistanceType Metric>
requires(Metric == cuvs::distance::DistanceType::L2Expanded)
RAFT_DEVICE_INLINE_FUNCTION constexpr auto dist_op(DATA_T a, DATA_T b)
-> std::enable_if_t<Metric == cuvs::distance::DistanceType::L2Expanded, DISTANCE_T>
{
DISTANCE_T diff = a - b;
return diff * diff;
}

template <typename DATA_T, typename DISTANCE_T, cuvs::distance::DistanceType Metric>
requires(Metric == cuvs::distance::DistanceType::InnerProduct ||
Metric == cuvs::distance::DistanceType::CosineExpanded)
RAFT_DEVICE_INLINE_FUNCTION constexpr auto dist_op(DATA_T a, DATA_T b)
-> std::enable_if_t<Metric == cuvs::distance::DistanceType::InnerProduct ||
Metric == cuvs::distance::DistanceType::CosineExpanded,
DISTANCE_T>
{
return -static_cast<DISTANCE_T>(a) * static_cast<DISTANCE_T>(b);
}

template <typename DATA_T, typename DISTANCE_T, cuvs::distance::DistanceType Metric>
requires(Metric == cuvs::distance::DistanceType::BitwiseHamming && std::is_integral_v<DATA_T>)
RAFT_DEVICE_INLINE_FUNCTION constexpr auto dist_op(DATA_T a, DATA_T b)
-> std::enable_if_t<Metric == cuvs::distance::DistanceType::BitwiseHamming &&
std::is_integral_v<DATA_T>,
DISTANCE_T>
{
// mask the result of xor for the integer promotion
const auto v = (a ^ b) & 0xffu;
return __popc(v);
}

template <typename DATA_T, typename DISTANCE_T, cuvs::distance::DistanceType Metric>
requires(Metric == cuvs::distance::DistanceType::L1)
RAFT_DEVICE_INLINE_FUNCTION constexpr auto dist_op(DATA_T a, DATA_T b)
{
DISTANCE_T diff = a - b;
return raft::abs(diff);
}
} // namespace

template <cuvs::distance::DistanceType Metric,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

/*
* NOTE: this file is generated by compute_distance_00_generate.py
*
* Make changes there and run in this directory:
*
* > python compute_distance_00_generate.py
*
*/

#include "compute_distance_standard-impl.cuh"

namespace cuvs::neighbors::cagra::detail {

using namespace cuvs::distance;
template struct standard_descriptor_spec<DistanceType::L1, 8, 128, float, uint32_t, float>;

} // namespace cuvs::neighbors::cagra::detail
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

/*
* NOTE: this file is generated by compute_distance_00_generate.py
*
* Make changes there and run in this directory:
*
* > python compute_distance_00_generate.py
*
*/

#include "compute_distance_standard-impl.cuh"

namespace cuvs::neighbors::cagra::detail {

using namespace cuvs::distance;
template struct standard_descriptor_spec<DistanceType::L1, 16, 256, float, uint32_t, float>;

} // namespace cuvs::neighbors::cagra::detail
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

/*
* NOTE: this file is generated by compute_distance_00_generate.py
*
* Make changes there and run in this directory:
*
* > python compute_distance_00_generate.py
*
*/

#include "compute_distance_standard-impl.cuh"

namespace cuvs::neighbors::cagra::detail {

using namespace cuvs::distance;
template struct standard_descriptor_spec<DistanceType::L1, 32, 512, float, uint32_t, float>;

} // namespace cuvs::neighbors::cagra::detail
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

/*
* NOTE: this file is generated by compute_distance_00_generate.py
*
* Make changes there and run in this directory:
*
* > python compute_distance_00_generate.py
*
*/

#include "compute_distance_standard-impl.cuh"

namespace cuvs::neighbors::cagra::detail {

using namespace cuvs::distance;
template struct standard_descriptor_spec<DistanceType::L1, 8, 128, half, uint32_t, float>;

} // namespace cuvs::neighbors::cagra::detail
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

/*
* NOTE: this file is generated by compute_distance_00_generate.py
*
* Make changes there and run in this directory:
*
* > python compute_distance_00_generate.py
*
*/

#include "compute_distance_standard-impl.cuh"

namespace cuvs::neighbors::cagra::detail {

using namespace cuvs::distance;
template struct standard_descriptor_spec<DistanceType::L1, 16, 256, half, uint32_t, float>;

} // namespace cuvs::neighbors::cagra::detail
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

/*
* NOTE: this file is generated by compute_distance_00_generate.py
*
* Make changes there and run in this directory:
*
* > python compute_distance_00_generate.py
*
*/

#include "compute_distance_standard-impl.cuh"

namespace cuvs::neighbors::cagra::detail {

using namespace cuvs::distance;
template struct standard_descriptor_spec<DistanceType::L1, 32, 512, half, uint32_t, float>;

} // namespace cuvs::neighbors::cagra::detail
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

/*
* NOTE: this file is generated by compute_distance_00_generate.py
*
* Make changes there and run in this directory:
*
* > python compute_distance_00_generate.py
*
*/

#include "compute_distance_standard-impl.cuh"

namespace cuvs::neighbors::cagra::detail {

using namespace cuvs::distance;
template struct standard_descriptor_spec<DistanceType::L1, 8, 128, int8_t, uint32_t, float>;

} // namespace cuvs::neighbors::cagra::detail
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

/*
* NOTE: this file is generated by compute_distance_00_generate.py
*
* Make changes there and run in this directory:
*
* > python compute_distance_00_generate.py
*
*/

#include "compute_distance_standard-impl.cuh"

namespace cuvs::neighbors::cagra::detail {

using namespace cuvs::distance;
template struct standard_descriptor_spec<DistanceType::L1, 16, 256, int8_t, uint32_t, float>;

} // namespace cuvs::neighbors::cagra::detail
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

/*
* NOTE: this file is generated by compute_distance_00_generate.py
*
* Make changes there and run in this directory:
*
* > python compute_distance_00_generate.py
*
*/

#include "compute_distance_standard-impl.cuh"

namespace cuvs::neighbors::cagra::detail {

using namespace cuvs::distance;
template struct standard_descriptor_spec<DistanceType::L1, 32, 512, int8_t, uint32_t, float>;

} // namespace cuvs::neighbors::cagra::detail
Loading