-
Notifications
You must be signed in to change notification settings - Fork 197
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
Internal library to share headers between test and bench #1162
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
85589ac
Squash-merge enh-matrix-topk
achirkin 0e3b93c
Add an internal library to share headers between test and bench
achirkin 8894697
Merge remote-tracking branch 'rapidsai/branch-23.02' into fea-interna…
achirkin 0e05542
Rename the root folder of internal->raft_internal
achirkin 9ff0b2a
Add a short description for the new folder {internal}
achirkin 19e4238
Merge remote-tracking branch 'rapidsai/branch-23.02' into fea-interna…
achirkin 5cbd086
Merge branch 'branch-23.02' into fea-internal-lib
cjnolet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# ============================================================================= | ||
# Copyright (c) 2023, 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. | ||
# ============================================================================= | ||
|
||
if(BUILD_TESTS OR BUILD_BENCH) | ||
add_library(raft_internal INTERFACE) | ||
target_include_directories( | ||
raft_internal INTERFACE "$<BUILD_INTERFACE:${RAFT_SOURCE_DIR}/internal>" | ||
) | ||
target_compile_features(raft_internal INTERFACE cxx_std_17 $<BUILD_INTERFACE:cuda_std_17>) | ||
endif() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright (c) 2023, 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 <raft/distance/distance_types.hpp> | ||
#include <raft/matrix/detail/select_k.cuh> | ||
#include <raft/spatial/knn/detail/ann_utils.cuh> | ||
#include <raft/util/cuda_utils.cuh> | ||
|
||
#include <rmm/cuda_stream_view.hpp> | ||
#include <rmm/device_uvector.hpp> | ||
#include <rmm/mr/device/device_memory_resource.hpp> | ||
|
||
namespace raft::neighbors { | ||
|
||
template <typename EvalT, typename DataT, typename IdxT> | ||
__global__ void naive_distance_kernel(EvalT* dist, | ||
const DataT* x, | ||
const DataT* y, | ||
IdxT m, | ||
IdxT n, | ||
IdxT k, | ||
raft::distance::DistanceType metric) | ||
{ | ||
IdxT midx = IdxT(threadIdx.x) + IdxT(blockIdx.x) * IdxT(blockDim.x); | ||
if (midx >= m) return; | ||
IdxT grid_size = IdxT(blockDim.y) * IdxT(gridDim.y); | ||
for (IdxT nidx = threadIdx.y + blockIdx.y * blockDim.y; nidx < n; nidx += grid_size) { | ||
EvalT acc = EvalT(0); | ||
for (IdxT i = 0; i < k; ++i) { | ||
IdxT xidx = i + midx * k; | ||
IdxT yidx = i + nidx * k; | ||
auto xv = EvalT(x[xidx]); | ||
auto yv = EvalT(y[yidx]); | ||
switch (metric) { | ||
case raft::distance::DistanceType::InnerProduct: { | ||
acc += xv * yv; | ||
} break; | ||
case raft::distance::DistanceType::L2SqrtExpanded: | ||
case raft::distance::DistanceType::L2SqrtUnexpanded: | ||
case raft::distance::DistanceType::L2Expanded: | ||
case raft::distance::DistanceType::L2Unexpanded: { | ||
auto diff = xv - yv; | ||
acc += diff * diff; | ||
} break; | ||
default: break; | ||
} | ||
} | ||
switch (metric) { | ||
case raft::distance::DistanceType::L2SqrtExpanded: | ||
case raft::distance::DistanceType::L2SqrtUnexpanded: { | ||
acc = raft::sqrt(acc); | ||
} break; | ||
default: break; | ||
} | ||
dist[midx * n + nidx] = acc; | ||
} | ||
} | ||
|
||
/** | ||
* Naive, but flexible bruteforce KNN search. | ||
* | ||
* TODO: either replace this with brute_force_knn or with distance+select_k | ||
* when either distance or brute_force_knn support 8-bit int inputs. | ||
*/ | ||
template <typename EvalT, typename DataT, typename IdxT> | ||
void naive_knn(EvalT* dist_topk, | ||
IdxT* indices_topk, | ||
const DataT* x, | ||
const DataT* y, | ||
size_t n_inputs, | ||
size_t input_len, | ||
size_t dim, | ||
uint32_t k, | ||
raft::distance::DistanceType type, | ||
rmm::cuda_stream_view stream) | ||
{ | ||
rmm::mr::device_memory_resource* mr = nullptr; | ||
auto pool_guard = raft::get_pool_memory_resource(mr, 1024 * 1024); | ||
|
||
dim3 block_dim(16, 32, 1); | ||
// maximum reasonable grid size in `y` direction | ||
auto grid_y = | ||
static_cast<uint16_t>(std::min<size_t>(raft::ceildiv<size_t>(input_len, block_dim.y), 32768)); | ||
|
||
// bound the memory used by this function | ||
size_t max_batch_size = | ||
std::min<size_t>(n_inputs, raft::ceildiv<size_t>(size_t(1) << size_t(27), input_len)); | ||
rmm::device_uvector<EvalT> dist(max_batch_size * input_len, stream, mr); | ||
|
||
for (size_t offset = 0; offset < n_inputs; offset += max_batch_size) { | ||
size_t batch_size = std::min(max_batch_size, n_inputs - offset); | ||
dim3 grid_dim(raft::ceildiv<size_t>(batch_size, block_dim.x), grid_y, 1); | ||
|
||
naive_distance_kernel<EvalT, DataT, IdxT><<<grid_dim, block_dim, 0, stream>>>( | ||
dist.data(), x + offset * dim, y, batch_size, input_len, dim, type); | ||
|
||
matrix::detail::select_k<EvalT, IdxT>(dist.data(), | ||
nullptr, | ||
batch_size, | ||
input_len, | ||
static_cast<int>(k), | ||
dist_topk + offset * k, | ||
indices_topk + offset * k, | ||
type != raft::distance::DistanceType::InnerProduct, | ||
stream, | ||
mr); | ||
} | ||
RAFT_CUDA_TRY(cudaStreamSynchronize(stream)); | ||
} | ||
|
||
} // namespace raft::neighbors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm particularly not sure and welcome any suggestions regarding the naming of the component. Currently:
raft_internal
, because I wanted to call itinternal
, but it may be a reserved word in cmake.internal/internal
. The firstinternal
is for cmake to isolate it from the other components, the second is for includes, so that all includes from this component are easily identifiable, e.g.:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking through this a little bit- what do you think about keeping the outer directory
cpp/internal
as-is but nesting everything insidecpp/internal/raft
instead ofcpp/internal/internal
. This would effectively allows us to include thecpp/internal
directory in the build for the bench and test source files whilst making the include paths themselves look the same as the other raft includes. So for example, this would still allow us to include thenaive_knn.cuh
file as#include <raft/neighbors/naive_knn.cuh>
but the file would be coming fromcpp/internal
instead. This assumes we wouldn't have name collisions there, which I highly doubt we would.Another option could be to do something like
cpp/internal/raft/internal
but I kind of prefer the option above more. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I'd prefer to have
raft
in the name is so that we don't have to worry about potential header collisions if, for example, a package namedinternal
is ever shipped for linux. Separating the namespace by includingraft
makes it very clear where our scope ends.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it turns out we have name collisions already! In branch-23.02 we have
include/raft/matrix/select_k.cuh
and its corresponding testtest/raft/matrix/select_k.cuh
; the latter is included using quotes syntax, so it's not a problem so far. In this PR I moved it to theinternal
component and changed the include syntax, so it would be a problem if we change the folder name to raft. So I see two options:test/raft/matrix/select_k.cuh
to something likeselect_k_helpers.cuh
orselect_k_utils.cuh
internal
to something different fromraft
, yet not potentially clashing with third-party libraries.In the new commit I opted for the option (2) with
raft_internal
as an experiment. Though I don't have a strong preference in this question. I only think that the trailinginternal
in the folder structure is not the best option, because it feels somewhat harder to maintain.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I can live w/
raft_internal
in the meantime. We can always revisit if we find this is presenting maintenance challenges.