-
Notifications
You must be signed in to change notification settings - Fork 48
Fix jaccard scaling #700
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
Merged
Merged
Fix jaccard scaling #700
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
| #include <cuda_runtime.h> | ||
| #include "../nb_types.h" | ||
|
|
||
| #include "kernels_jaccard.cuh" | ||
|
|
||
| using namespace nb::literals; | ||
|
|
||
| constexpr int JACCARD_BLOCK_SIZE = 256; | ||
|
|
||
| static inline void launch_jaccard_shared_counts(const int* knn, int n_obs, | ||
| int k, float* jaccard_vals, | ||
| cudaStream_t stream) { | ||
| const long long n_edges = (long long)n_obs * k; | ||
| if (n_edges == 0) return; | ||
| unsigned grid = strided_grid(n_edges, JACCARD_BLOCK_SIZE); | ||
| jaccard_shared_counts_kernel<<<grid, JACCARD_BLOCK_SIZE, 0, stream>>>( | ||
| knn, n_obs, k, jaccard_vals); | ||
| CUDA_CHECK_LAST_ERROR(jaccard_shared_counts_kernel); | ||
| } | ||
|
|
||
| template <typename Device> | ||
| void register_bindings(nb::module_& m) { | ||
| m.def( | ||
| "jaccard_shared_counts", | ||
| [](gpu_array_c<const int, Device> knn, int n_obs, int k, | ||
| gpu_array_c<float, Device> jaccard_vals, std::uintptr_t stream) { | ||
| launch_jaccard_shared_counts(knn.data(), n_obs, k, | ||
| jaccard_vals.data(), | ||
| (cudaStream_t)stream); | ||
| }, | ||
| "knn"_a, nb::kw_only(), "n_obs"_a, "k"_a, "jaccard_vals"_a, | ||
| "stream"_a = 0); | ||
| } | ||
|
|
||
| NB_MODULE(_jaccard_cuda, m) { | ||
| REGISTER_GPU_BINDINGS(register_bindings, m); | ||
| } | ||
This file contains hidden or 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,36 @@ | ||
| #pragma once | ||
|
|
||
| #include <cuda_runtime.h> | ||
|
|
||
| // One thread per KNN entry (i, slot): edge = i*k + slot, j = knn[edge]. | ||
| // Writes |N(i) & N(j)| (shared-neighbor count) for each directed KNN edge. | ||
| // The self entry (j == i) and any out-of-range index write 0 and are dropped | ||
| // downstream. Inner loops skip column 0 so the count matches the exclude-self | ||
| // definition. All row offsets are computed in 64-bit (overflow-proof for any | ||
| // n_obs); the grid-stride loop covers n_edges beyond one launch. | ||
| __global__ void jaccard_shared_counts_kernel(const int* __restrict__ knn, | ||
| int n_obs, int k, | ||
| float* __restrict__ jaccard_vals) { | ||
| const long long n_edges = (long long)n_obs * k; | ||
| const long long stride = (long long)blockDim.x * gridDim.x; | ||
| for (long long edge = (long long)blockIdx.x * blockDim.x + threadIdx.x; | ||
| edge < n_edges; edge += stride) { | ||
| const long long i = edge / k; | ||
| const int j = knn[edge]; | ||
| if (j == (int)i || j < 0 || j >= n_obs) { | ||
| jaccard_vals[edge] = 0.0f; | ||
| continue; | ||
| } | ||
| const int* Ni = knn + i * (long long)k; | ||
| const int* Nj = knn + (long long)j * k; | ||
| int c = 0; | ||
| for (int a = 1; a < k; ++a) { | ||
| const int va = Ni[a]; | ||
| for (int b = 1; b < k; ++b) { | ||
| c += (va == Nj[b]); | ||
| } | ||
| } | ||
| jaccard_vals[edge] = (float)c / (2 * (k - 1) - c); | ||
| ; | ||
|
Intron7 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.