Skip to content

Commit

Permalink
review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
mfoerste4 committed Sep 25, 2024
1 parent 610bdeb commit 6fc3eaf
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 108 deletions.
27 changes: 15 additions & 12 deletions cpp/src/svm/results.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ class Results {
void Get(const math_t* alpha,
const math_t* f,
rmm::device_buffer& dual_coefs,
int* n_support,
int& n_support,
rmm::device_buffer& idx,
SupportStorage& support_matrix,
math_t* b)
math_t& b)
{
CombineCoefs(alpha, val_tmp.data());
GetDualCoefs(val_tmp.data(), dual_coefs, n_support);
*b = CalcB(alpha, f, *n_support);
GetSupportVectorIndices(idx, val_tmp.data(), *n_support);
CollectSupportVectorMatrix(support_matrix, idx, *n_support);
b = CalcB(alpha, f, n_support);
GetSupportVectorIndices(idx, val_tmp.data(), n_support);
CollectSupportVectorMatrix(support_matrix, idx, n_support);
// Make sure that all pending GPU calculations finished before we return
handle.sync_stream(stream);
}
Expand All @@ -151,16 +151,19 @@ class Results {
support_matrix.indices.resize(0, stream);
support_matrix.data.resize(n_support * n_cols * sizeof(math_t), stream);
if (n_support > 0) {
ML::SVM::extractRows<math_t>(
matrix, (math_t*)support_matrix.data.data(), (int*)idx.data(), n_support, handle);
ML::SVM::extractRows<math_t>(matrix,
reinterpret_cast<math_t*>(support_matrix.data.data()),
reinterpret_cast<int*>(idx.data()),
n_support,
handle);
}
} else {
ML::SVM::extractRows<math_t>(matrix,
support_matrix.indptr,
support_matrix.indices,
support_matrix.data,
&(support_matrix.nnz),
(int*)idx.data(),
reinterpret_cast<int*>(idx.data()),
n_support,
handle);
}
Expand Down Expand Up @@ -202,13 +205,13 @@ class Results {
* unallocated on entry, on exit size [n_support]
* @param [out] n_support number of support vectors
*/
void GetDualCoefs(const math_t* val_tmp, rmm::device_buffer& dual_coefs, int* n_support)
void GetDualCoefs(const math_t* val_tmp, rmm::device_buffer& dual_coefs, int& n_support)
{
// Return only the non-zero coefficients
auto select_op = [] __device__(math_t a) { return 0 != a; };
*n_support = SelectByCoef(val_tmp, n_rows, val_tmp, select_op, val_selected.data());
dual_coefs.resize(*n_support * sizeof(math_t), stream);
raft::copy((math_t*)dual_coefs.data(), val_selected.data(), *n_support, stream);
n_support = SelectByCoef(val_tmp, n_rows, val_tmp, select_op, val_selected.data());
dual_coefs.resize(n_support * sizeof(math_t), stream);
raft::copy((math_t*)dual_coefs.data(), val_selected.data(), n_support, stream);
handle.sync_stream(stream);
}

Expand Down
12 changes: 6 additions & 6 deletions cpp/src/svm/smosolver.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ void SmoSolver<math_t>::Solve(MatrixViewType matrix,
int n_cols,
math_t* y,
const math_t* sample_weight,
rmm::device_buffer* dual_coefs,
int* n_support,
SupportStorage* support_matrix,
rmm::device_buffer* idx,
math_t* b,
rmm::device_buffer& dual_coefs,
int& n_support,
SupportStorage& support_matrix,
rmm::device_buffer& idx,
math_t& b,
int max_outer_iter,
int max_inner_iter)
{
Expand Down Expand Up @@ -210,7 +210,7 @@ void SmoSolver<math_t>::Solve(MatrixViewType matrix,
diff_prev);

Results<math_t, MatrixViewType> res(handle, matrix, n_rows, n_cols, y, C_vec.data(), svmType);
res.Get(alpha.data(), f.data(), *dual_coefs, n_support, *idx, *support_matrix, b);
res.Get(alpha.data(), f.data(), dual_coefs, n_support, idx, support_matrix, b);

ReleaseBuffers();
}
Expand Down
10 changes: 5 additions & 5 deletions cpp/src/svm/smosolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ class SmoSolver {
int n_cols,
math_t* y,
const math_t* sample_weight,
rmm::device_buffer* dual_coefs,
int* n_support,
SupportStorage* support_matrix,
rmm::device_buffer* idx,
math_t* b,
rmm::device_buffer& dual_coefs,
int& n_support,
SupportStorage& support_matrix,
rmm::device_buffer& idx,
math_t& b,
int max_outer_iter = -1,
int max_inner_iter = 10000);

Expand Down
17 changes: 11 additions & 6 deletions cpp/src/svm/svc_impl.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ void svcFitX(const raft::handle_t& handle,
n_cols,
y.data(),
sample_weight,
&(model.dual_coefs),
&(model.n_support),
&(model.support_matrix),
&(model.support_idx),
&(model.b),
model.dual_coefs,
model.n_support,
model.support_matrix,
model.support_idx,
model.b,
param.max_iter);
model.n_cols = n_cols;
handle_impl.sync_stream(stream);
Expand Down Expand Up @@ -356,17 +356,22 @@ void svmFreeBuffers(const raft::handle_t& handle, SvmModel<math_t>& m)
{
cudaStream_t stream = handle.get_stream();

// Note that the underlying allocations are not *freed* but rather reset
m.n_support = 0;
m.n_cols = 0;
m.b = (math_t)0;
m.dual_coefs.resize(0, stream);
m.dual_coefs.shrink_to_fit(stream);
m.support_idx.resize(0, stream);
m.support_idx.shrink_to_fit(stream);
m.support_matrix.indptr.resize(0, stream);
m.support_matrix.indptr.shrink_to_fit(stream);
m.support_matrix.indices.resize(0, stream);
m.support_matrix.indices.shrink_to_fit(stream);
m.support_matrix.data.resize(0, stream);
m.support_matrix.data.shrink_to_fit(stream);
m.support_matrix.nnz = -1;
m.unique_labels.resize(0, stream);
m.unique_labels.shrink_to_fit(stream);
}

}; // end namespace SVM
Expand Down
94 changes: 36 additions & 58 deletions cpp/src/svm/svm_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,41 +87,34 @@ cumlError_t cumlSpSvcFit(cumlHandle_t handle,
if (model.dual_coefs.size() > 0) {
*dual_coefs = (float*)rmm_alloc.allocate_async(
model.dual_coefs.size(), rmm::CUDA_ALLOCATION_ALIGNMENT, stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(
dual_coefs, model.dual_coefs.data(), model.dual_coefs.size(), cudaMemcpyDefault, stream));
raft::copy(
*dual_coefs, reinterpret_cast<float*>(model.dual_coefs.data()), *n_support, stream);
} else {
*dual_coefs = nullptr;
}
if (model.support_matrix.data.size() > 0) {
*x_support = (float*)rmm_alloc.allocate_async(
model.support_matrix.data.size(), rmm::CUDA_ALLOCATION_ALIGNMENT, stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(x_support,
model.support_matrix.data.data(),
model.support_matrix.data.size(),
cudaMemcpyDefault,
stream));
raft::copy(*x_support,
reinterpret_cast<float*>(model.support_matrix.data.data()),
*n_support * n_cols,
stream);
} else {
*x_support = nullptr;
}
if (model.support_idx.size() > 0) {
*support_idx = (int*)rmm_alloc.allocate_async(
model.support_idx.size(), rmm::CUDA_ALLOCATION_ALIGNMENT, stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(support_idx,
model.support_idx.data(),
model.support_idx.size(),
cudaMemcpyDefault,
stream));
raft::copy(
*support_idx, reinterpret_cast<int*>(model.support_idx.data()), *n_support, stream);
} else {
*support_idx = nullptr;
}
if (model.unique_labels.size() > 0) {
*unique_labels = (float*)rmm_alloc.allocate_async(
model.unique_labels.size(), rmm::CUDA_ALLOCATION_ALIGNMENT, stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(unique_labels,
model.unique_labels.data(),
model.unique_labels.size(),
cudaMemcpyDefault,
stream));
raft::copy(
*unique_labels, reinterpret_cast<float*>(model.unique_labels.data()), *n_classes, stream);
} else {
*unique_labels = nullptr;
}
Expand Down Expand Up @@ -203,41 +196,36 @@ cumlError_t cumlDpSvcFit(cumlHandle_t handle,
if (model.dual_coefs.size() > 0) {
*dual_coefs = (double*)rmm_alloc.allocate_async(
model.dual_coefs.size(), rmm::CUDA_ALLOCATION_ALIGNMENT, stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(
dual_coefs, model.dual_coefs.data(), model.dual_coefs.size(), cudaMemcpyDefault, stream));
raft::copy(
*dual_coefs, reinterpret_cast<double*>(model.dual_coefs.data()), *n_support, stream);
} else {
*dual_coefs = nullptr;
}
if (model.support_matrix.data.size() > 0) {
*x_support = (double*)rmm_alloc.allocate_async(
model.support_matrix.data.size(), rmm::CUDA_ALLOCATION_ALIGNMENT, stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(x_support,
model.support_matrix.data.data(),
model.support_matrix.data.size(),
cudaMemcpyDefault,
stream));
raft::copy(*x_support,
reinterpret_cast<double*>(model.support_matrix.data.data()),
*n_support * n_cols,
stream);
} else {
*x_support = nullptr;
}
if (model.support_idx.size() > 0) {
*support_idx = (int*)rmm_alloc.allocate_async(
model.support_idx.size(), rmm::CUDA_ALLOCATION_ALIGNMENT, stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(support_idx,
model.support_idx.data(),
model.support_idx.size(),
cudaMemcpyDefault,
stream));
raft::copy(
*support_idx, reinterpret_cast<int*>(model.support_idx.data()), *n_support, stream);
} else {
*support_idx = nullptr;
}
if (model.unique_labels.size() > 0) {
*unique_labels = (double*)rmm_alloc.allocate_async(
model.unique_labels.size(), rmm::CUDA_ALLOCATION_ALIGNMENT, stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(unique_labels,
model.unique_labels.data(),
model.unique_labels.size(),
cudaMemcpyDefault,
stream));
raft::copy(*unique_labels,
reinterpret_cast<double*>(model.unique_labels.data()),
*n_classes,
stream);
} else {
*unique_labels = nullptr;
}
Expand Down Expand Up @@ -291,24 +279,19 @@ cumlError_t cumlSpSvcPredict(cumlHandle_t handle,
model.n_classes = n_classes;
if (n_support > 0) {
model.dual_coefs.resize(n_support * sizeof(float), stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(
model.dual_coefs.data(), dual_coefs, n_support * sizeof(float), cudaMemcpyDefault, stream));
raft::copy(reinterpret_cast<float*>(model.dual_coefs.data()), dual_coefs, n_support, stream);

model.support_matrix.data.resize(n_support * n_cols * sizeof(float), stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(model.support_matrix.data.data(),
x_support,
n_support * n_cols * sizeof(float),
cudaMemcpyDefault,
stream));
raft::copy(reinterpret_cast<float*>(model.support_matrix.data.data()),
x_support,
n_support * n_cols,
stream);
}

if (n_classes > 0) {
model.unique_labels.resize(n_classes * sizeof(float), stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(model.unique_labels.data(),
unique_labels,
n_classes * sizeof(float),
cudaMemcpyDefault,
stream));
raft::copy(
reinterpret_cast<float*>(model.unique_labels.data()), unique_labels, n_classes, stream);
}

if (status == CUML_SUCCESS) {
Expand Down Expand Up @@ -364,24 +347,19 @@ cumlError_t cumlDpSvcPredict(cumlHandle_t handle,
model.n_classes = n_classes;
if (n_support > 0) {
model.dual_coefs.resize(n_support * sizeof(double), stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(
model.dual_coefs.data(), dual_coefs, n_support * sizeof(double), cudaMemcpyDefault, stream));
raft::copy(reinterpret_cast<double*>(model.dual_coefs.data()), dual_coefs, n_support, stream);

model.support_matrix.data.resize(n_support * n_cols * sizeof(double), stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(model.support_matrix.data.data(),
x_support,
n_support * n_cols * sizeof(double),
cudaMemcpyDefault,
stream));
raft::copy(reinterpret_cast<double*>(model.support_matrix.data.data()),
x_support,
n_support * n_cols,
stream);
}

if (n_classes > 0) {
model.unique_labels.resize(n_classes * sizeof(double), stream);
RAFT_CUDA_TRY(cudaMemcpyAsync(model.unique_labels.data(),
unique_labels,
n_classes * sizeof(double),
cudaMemcpyDefault,
stream));
raft::copy(
reinterpret_cast<double*>(model.unique_labels.data()), unique_labels, n_classes, stream);
}

if (status == CUML_SUCCESS) {
Expand Down
10 changes: 5 additions & 5 deletions cpp/src/svm/svr_impl.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ void svrFitX(const raft::handle_t& handle,
n_cols,
y,
sample_weight,
&(model.dual_coefs),
&(model.n_support),
&(model.support_matrix),
&(model.support_idx),
&(model.b),
model.dual_coefs,
model.n_support,
model.support_matrix,
model.support_idx,
model.b,
param.max_iter);
model.n_cols = n_cols;
delete kernel;
Expand Down
Loading

0 comments on commit 6fc3eaf

Please sign in to comment.