Skip to content
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

Support sparse input for SVC and SVR #5273

Merged
merged 40 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
cc52cc6
Remove caching, calc only nnz delta alpha, disable SVR test
tfeher Nov 25, 2022
02ac8b4
initial support for SVC sparse input
mfoerste4 Dec 5, 2022
d7ac752
added conversion check
mfoerste4 Dec 6, 2022
97f1812
forward matrix from python
mfoerste4 Dec 7, 2022
e5b39a6
extract sparse rows enable sparse kernel computation
mfoerste4 Dec 8, 2022
b90b716
some cython cleanup
mfoerste4 Dec 8, 2022
40ad64a
cleanup memory management, allow dense extract for faster kernel comp…
mfoerste4 Feb 2, 2023
0e63851
some fixes for batched dense RBF and CSR*CSR
mfoerste4 Feb 3, 2023
cfd01b2
allow support vectors as CSR
mfoerste4 Feb 20, 2023
e4fae32
moved matrix wrapper to raft -- simplified kernel API
mfoerste4 Feb 21, 2023
9879f97
extract rowNorm util
mfoerste4 Feb 22, 2023
63f47ff
allow for python export of sparse support vectors
mfoerste4 Feb 23, 2023
b70e77d
some fixes after merge
mfoerste4 Feb 27, 2023
062a118
fixed SVR, adjusted tests
mfoerste4 Mar 6, 2023
078ba5d
remove handle from kernel and add as runtime arg, also bump copyright…
mfoerste4 Mar 15, 2023
a76e3bc
Merge branch 'rapidsai:branch-23.04' into svm_sparse_support
mfoerste4 Mar 27, 2023
a0a3da5
Merge branch 'rapidsai:branch-23.04' into svm_sparse_support
mfoerste4 Mar 29, 2023
d307995
adapted to raft update, re-added matrix and adjust interface to mdspa…
mfoerste4 Apr 3, 2023
19bd708
add tests for all sparse x dense combinations
mfoerste4 Apr 3, 2023
45ad663
Merge branch 'rapidsai:branch-23.06' into svm_sparse_support
mfoerste4 Apr 17, 2023
72760a2
merge with 23.06
mfoerste4 May 3, 2023
04f8b65
moved all kernel computation to KernelCache class
mfoerste4 May 3, 2023
58fc7be
initial cache implementation
mfoerste4 May 5, 2023
7beab5b
some documentation and cleanup
mfoerste4 May 11, 2023
977fded
added KernelCache tests, fixes, cleanup
mfoerste4 May 12, 2023
74d42cb
added pickle test for sparse support, fixed sparse load
mfoerste4 May 16, 2023
f6664ed
merge 23.06
mfoerste4 May 21, 2023
0d07126
Merge branch 'branch-23.06' into svm_sparse_support
mfoerste4 May 21, 2023
b63d7d2
make BatchCache derive from raft::Cache and allow batch processing wi…
mfoerste4 May 22, 2023
1e319da
switch cython layer to pointer based API
mfoerste4 May 22, 2023
ae8f176
general review suggestions
mfoerste4 May 23, 2023
fc5f3cb
remove matrix class, switch all structures to device_matrix_view or d…
mfoerste4 May 25, 2023
6cbc395
Merge branch 'branch-23.06' into svm_sparse_support
mfoerste4 May 26, 2023
622fcde
review suggestions
mfoerste4 May 26, 2023
fc0f291
Merge branch 'svm_sparse_support' of github.com:mfoerste4/cuml into s…
mfoerste4 May 26, 2023
7385c8e
minor changes
mfoerste4 May 26, 2023
a16ead3
Merge branch 'branch-23.06' into svm_sparse_support
mfoerste4 May 30, 2023
22a3b34
extract unpack svm model to function
mfoerste4 May 31, 2023
5835578
Merge branch 'branch-23.06' into svm_sparse_support
mfoerste4 May 31, 2023
a3c0257
Merge branch 'branch-23.06' into svm_sparse_support
mfoerste4 Jun 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion cpp/include/cuml/svm/svc.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
* Copyright (c) 2019-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.
Expand Down Expand Up @@ -61,6 +61,42 @@ void svcFit(const raft::handle_t& handle,
SvmModel<math_t>& model,
const math_t* sample_weight);

/**
* @brief Fit a support vector classifier to the training data.
*
* Each row of the input data stores a feature vector.
* We use the SMO method to fit the SVM.
*
* The output device buffers in model shall be unallocated on entry.
*
* @tparam math_t floating point type
* @param [in] handle the cuML handle
* @param [in] indptr device pointer for CSR row positions. Size [n_rows + 1].
* @param [in] indices device pointer for CSR column indices. Size [nnz].
* @param [in] data device pointer for the CSR data. Size [nnz].
* @param [in] n_rows number of rows
* @param [in] n_cols number of columns
* @param [in] nnz number of stored entries.
* @param [in] labels device pointer for the labels. Size [n_rows].
* @param [in] param parameters for training
* @param [in] kernel_params parameters for the kernel function
* @param [out] model parameters of the trained model
* @param [in] sample_weight optional sample weights, size [n_rows]
*/
template <typename math_t>
void svcFitSparse(const raft::handle_t& handle,
int* indptr,
int* indices,
math_t* data,
int n_rows,
int n_cols,
int nnz,
math_t* labels,
const SvmParameter& param,
raft::distance::kernels::KernelParams& kernel_params,
SvmModel<math_t>& model,
const math_t* sample_weight);

/**
* @brief Predict classes or decision function value for samples in input.
*
Expand Down Expand Up @@ -101,6 +137,51 @@ void svcPredict(const raft::handle_t& handle,
math_t buffer_size,
bool predict_class);

/**
* @brief Predict classes or decision function value for samples in input.
*
* We evaluate the decision function f(x_i). Depending on the parameter
* predict_class, we either return f(x_i) or the label corresponding to
* sign(f(x_i)).
*
* The predictions are calculated according to the following formulas:
* \f[
* f(x_i) = \sum_{j=1}^n_support K(x_i, x_j) * dual_coefs[j] + b)
* \f]
*
* pred(x_i) = label[sign(f(x_i))], if predict_class==true, or
* pred(x_i) = f(x_i), if predict_class==falsee.
mfoerste4 marked this conversation as resolved.
Show resolved Hide resolved
*
* @tparam math_t floating point type
* @param handle the cuML handle
* @param [in] indptr device pointer for CSR row positions. Size [n_rows + 1].
* @param [in] indices device pointer for CSR column indices. Size [nnz].
* @param [in] data device pointer for the CSR data. Size [nnz].
* @param [in] n_rows number of rows
* @param [in] n_cols number of columns
* @param [in] nnz number of stored entries.
* @param [in] kernel_params parameters for the kernel function
* @param [in] model SVM model parameters
* @param [out] preds device pointer to store the predicted class labels.
* Size [n_rows]. Should be allocated on entry.
* @param [in] buffer_size size of temporary buffer in MiB
* @param [in] predict_class whether to predict class label (true), or just
* return the decision function value (false)
*/
template <typename math_t>
void svcPredictSparse(const raft::handle_t& handle,
int* indptr,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we accept such a limited set of types for this, we could probably eventually use the raft::csr_matrix_view but raw pointers from the Python->C++ hand-off is fine too since we really have not start porting over any of our other C++ APIs to accept mdspan directly yet.

int* indices,
math_t* data,
int n_rows,
int n_cols,
int nnz,
raft::distance::kernels::KernelParams& kernel_params,
const SvmModel<math_t>& model,
math_t* preds,
math_t buffer_size,
bool predict_class);

/**
* Deallocate device buffers in the SvmModel struct.
*
Expand Down
16 changes: 13 additions & 3 deletions cpp/include/cuml/svm/svm_model.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
* Copyright (c) 2019-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.
Expand All @@ -18,6 +18,15 @@
namespace ML {
namespace SVM {

// Contains array(s) for matrix storage
template <typename math_t>
struct SupportStorage {
int nnz = -1;
int* indptr = nullptr;
int* indices = nullptr;
math_t* data = nullptr;
};

/**
* Parameters that describe a trained SVM model.
* All pointers are device pointers.
Expand All @@ -32,8 +41,9 @@ struct SvmModel {
//! Size [n_support].
math_t* dual_coefs;

//! Support vectors in column major format. Size [n_support x n_cols].
math_t* x_support;
//! Support vector storage - can contain either CSR or dense
SupportStorage<math_t>* support_matrix;
mfoerste4 marked this conversation as resolved.
Show resolved Hide resolved

//! Indices (from the training set) of the support vectors, size [n_support].
int* support_idx;

Expand Down
37 changes: 36 additions & 1 deletion cpp/include/cuml/svm/svr.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
* Copyright (c) 2019-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.
Expand Down Expand Up @@ -57,6 +57,41 @@ void svrFit(const raft::handle_t& handle,
SvmModel<math_t>& model,
const math_t* sample_weight = nullptr);

/**
* @brief Fit a support vector regressor to the training data.
*
* Each row of the input data stores a feature vector.
*
* The output buffers in model shall be unallocated on entry.
*
* @tparam math_t floating point type
* @param [in] handle the cuML handle
* @param [in] indptr device pointer for CSR row positions. Size [n_rows + 1].
* @param [in] indices device pointer for CSR column indices. Size [nnz].
* @param [in] data device pointer for the CSR data. Size [nnz].
* @param [in] n_rows number of rows
* @param [in] n_cols number of columns
* @param [in] nnz number of stored entries.
* @param [in] y device pointer for target values. Size [n_rows].
* @param [in] param parameters for training
* @param [in] kernel_params parameters for the kernel function
* @param [out] model parameters of the trained model
* @param [in] sample_weight optional sample weights, size [n_rows]
*/
template <typename math_t>
void svrFitSparse(const raft::handle_t& handle,
int* indptr,
int* indices,
math_t* data,
int n_rows,
int n_cols,
int nnz,
math_t* y,
const SvmParameter& param,
raft::distance::kernels::KernelParams& kernel_params,
SvmModel<math_t>& model,
const math_t* sample_weight = nullptr);

// For prediction we use svcPredict

}; // end namespace SVM
Expand Down
Loading