-
Notifications
You must be signed in to change notification settings - Fork 201
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 support for bm25 and tfidf #2567
Open
jperez999
wants to merge
10
commits into
rapidsai:branch-25.04
Choose a base branch
from
jperez999:bm25-tfidf
base: branch-25.04
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,319
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
afb74f2
add support for bm25 and tfidf
jperez999 d2cd397
remove unnecessary commented function
jperez999 43b0491
add function comments and change wording to feats from vocab
jperez999 54c6c30
add handle descriptor for comment on function definition
jperez999 44c68f4
add missing comments for functions
jperez999 60a94a1
add missing param comment
jperez999 9dee4ec
Merge branch 'branch-25.04' into bm25-tfidf
jperez999 a6e2f00
Merge branch 'branch-25.04' into bm25-tfidf
jperez999 4175a4d
Merge branch 'branch-25.04' into bm25-tfidf
benfred faef8ab
Merge branch 'branch-25.04' into bm25-tfidf
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
206 changes: 206 additions & 0 deletions
206
cpp/include/raft/sparse/matrix/detail/preprocessing.cuh
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,206 @@ | ||
/* | ||
* Copyright (c) 2024, 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/core/device_coo_matrix.hpp> | ||
#include <raft/core/resource/cuda_stream.hpp> | ||
#include <raft/core/resource/thrust_policy.hpp> | ||
#include <raft/linalg/map_reduce.cuh> | ||
#include <raft/matrix/init.cuh> | ||
#include <raft/sparse/convert/coo.cuh> | ||
#include <raft/sparse/neighbors/cross_component_nn.cuh> | ||
#include <raft/sparse/op/sort.cuh> | ||
|
||
#include <thrust/reduce.h> | ||
|
||
namespace raft::sparse::matrix::detail { | ||
|
||
/** | ||
* This function creates a representation of input data (rows) that identifies when | ||
* value changes in the input data. This function assumes data is sorted. | ||
* | ||
* @param[in] rows | ||
* The input data | ||
* @param[in] nnz | ||
* The size of the input data. | ||
* @param[in] counts | ||
* The resulting representation of the index value changes of the input. Should be | ||
* the same size as the input (nnz) | ||
*/ | ||
__global__ void _scan(int* rows, int nnz, int* counts) | ||
{ | ||
int index = blockIdx.x * blockDim.x + threadIdx.x; | ||
if (index >= nnz) { return; } | ||
if (index == 0) { | ||
counts[index] = 1; | ||
return; | ||
} | ||
if (index < nnz) { | ||
int curr_id = rows[index]; | ||
int old_id = rows[index - 1]; | ||
if (curr_id != old_id) { | ||
counts[index] = 1; | ||
} else { | ||
counts[index] = 0; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This function counts the occurrences of the input array. Uses modulo logic as a | ||
* rudimentary hash (should be changed with better hash function). | ||
* | ||
* @param[in] cols | ||
* The input data | ||
* @param[in] nnz | ||
* The size of the input data. | ||
* @param[in] counts | ||
* The resulting representation of the index value changes of the input. Should be | ||
* the same size as the input (nnz) | ||
* @param[in] feats | ||
* The array that will house the occurrence counts | ||
* @param[in] vocabSize | ||
* The size of the occurrence counts array (feats). | ||
*/ | ||
__global__ void _fit_compute_occurs(int* cols, int nnz, int* counts, int* feats, int vocabSize) | ||
{ | ||
int index = blockIdx.x * blockDim.x + threadIdx.x; | ||
if ((index < nnz) && (counts[index] == 1)) { | ||
int targetVal = cols[index]; | ||
int vocab = targetVal % vocabSize; | ||
while (targetVal == cols[index]) { | ||
feats[vocab] = feats[vocab] + 1; | ||
index++; | ||
if (index >= nnz) { return; } | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This function calculates tfidf or bm25, depending on options supplied, from the | ||
* values input array. | ||
* | ||
* @param[in] rows | ||
* The input rows. | ||
* @param[in] columns | ||
* The input columns (features). | ||
* @param[in] values | ||
* The input values. | ||
* @param[in] feat_id_count | ||
* The array holding the feature(column) occurrence counts for all fitted inputs. | ||
* @param[in] counts | ||
* The array representing value changes in rows input. | ||
* @param[in] out_values | ||
* The array that will store calculated values, should be size NNZ. | ||
* @param[in] vocabSize | ||
* The number of the features (columns). | ||
* @param[in] num_rows | ||
* Total number of rows for all fitted inputs. | ||
* @param[in] avgRowLen | ||
* The average length of a row (sum of all values for each row). | ||
* @param[in] k | ||
* The bm25 formula variable. Helps with optimization. | ||
* @param[in] b | ||
* The bm25 formula variable. Helps with optimization. | ||
* @param[in] nnz | ||
* The size of the input arrays (rows, columns, values). | ||
* @param[in] bm25 | ||
* Boolean that activates bm25 calculation instead of tfidf | ||
*/ | ||
__global__ void _transform(int* rows, | ||
int* columns, | ||
float* values, | ||
int* feat_id_count, | ||
int* counts, | ||
float* out_values, | ||
int num_rows, | ||
float avgRowLen, | ||
float k, | ||
float b, | ||
int nnz, | ||
int vocabSize, | ||
bool bm25 = false) | ||
{ | ||
int start_index = blockIdx.x * blockDim.x + threadIdx.x; | ||
int index = start_index; | ||
if (index < nnz && counts[index] == 1) { | ||
int row_length = 0; | ||
int targetVal = rows[index]; | ||
while (targetVal == rows[index]) { | ||
row_length += values[index]; | ||
index++; | ||
if (index >= nnz) { break; } | ||
} | ||
index = start_index; | ||
float result; | ||
while (targetVal == rows[index]) { | ||
int col = columns[index]; | ||
int vocab = col % vocabSize; | ||
float tf = (float)values[index] / row_length; | ||
double idf_in = (double)num_rows / feat_id_count[vocab]; | ||
float idf = (float)raft::log<double>(idf_in); | ||
result = tf * idf; | ||
if (bm25) { | ||
float bm = ((k + 1) * tf) / (k * ((1.0f - b) + b * (row_length / avgRowLen)) + tf); | ||
result = idf * bm; | ||
} | ||
out_values[index] = result; | ||
index++; | ||
if (index >= nnz) { break; } | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This function converts a raft csr matrix in to a coo (rows, columns,values) | ||
* representation. | ||
* | ||
* @param[in] handle | ||
* The input data | ||
* @param[in] csr_in | ||
* The input raft csr matrix. | ||
* @param[in] rows | ||
* The output rows from the csr conversion. | ||
* @param[in] columns | ||
* The output columns from the csr conversion. | ||
* @param[in] values | ||
* The output values from the csr conversion. | ||
*/ | ||
template <typename ValueType, typename IndexType> | ||
void convert_csr_to_coo(raft::resources& handle, | ||
raft::device_csr_matrix<ValueType, | ||
IndexType, | ||
IndexType, | ||
IndexType, | ||
raft::device_uvector_policy, | ||
raft::PRESERVING> csr_in, | ||
raft::device_vector_view<IndexType, int64_t> rows, | ||
raft::device_vector_view<IndexType, int64_t> columns, | ||
raft::device_vector_view<ValueType, int64_t> values) | ||
{ | ||
cudaStream_t stream = raft::resource::get_cuda_stream(handle); | ||
auto nnz = csr_in.structure_view().get_nnz(); | ||
auto indptr = csr_in.structure_view().get_indptr(); | ||
auto indices = csr_in.structure_view().get_indices(); | ||
auto vals = csr_in.view().get_elements(); | ||
|
||
raft::sparse::convert::csr_to_coo( | ||
indptr.data(), (int)indptr.size(), rows.data_handle(), (int)nnz, stream); | ||
raft::copy(columns.data_handle(), indices.data(), (int)nnz, stream); | ||
raft::copy(values.data_handle(), vals.data(), (int)nnz, stream); | ||
} | ||
|
||
} // namespace raft::sparse::matrix::detail |
Oops, something went wrong.
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.
nitpick: