diff --git a/cpp/examples/symreg/symreg_example.cpp b/cpp/examples/symreg/symreg_example.cpp index 99a75c430e..504b36af6c 100644 --- a/cpp/examples/symreg/symreg_example.cpp +++ b/cpp/examples/symreg/symreg_example.cpp @@ -245,7 +245,8 @@ int main(int argc, char* argv[]) dw_test.data(), w_test.data(), sizeof(float) * n_test_rows, cudaMemcpyHostToDevice, stream)); // Initialize AST - raft::allocate(d_finalprogs, params.population_size, stream); + auto curr_mr = rmm::mr::get_current_device_resource(); + d_finalprogs = static_cast(curr_mr->allocate(params.population_size, stream)); std::vector> history; history.reserve(params.generations); @@ -337,8 +338,8 @@ int main(int argc, char* argv[]) /* ======================= Reset data ======================= */ - raft::deallocate(d_finalprogs, stream); + curr_mr->deallocate(d_finalprogs, params.population_size, stream); CUDA_RT_CALL(cudaEventDestroy(start)); CUDA_RT_CALL(cudaEventDestroy(stop)); return 0; -} +} \ No newline at end of file diff --git a/cpp/test/prims/hinge.cu b/cpp/test/prims/hinge.cu index bc3cdb8dfc..89a061487a 100644 --- a/cpp/test/prims/hinge.cu +++ b/cpp/test/prims/hinge.cu @@ -33,216 +33,196 @@ struct HingeLossInputs { template class HingeLossTest : public ::testing::TestWithParam> { + public: + HingeLossTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + in(params.len, stream), + out(1, stream), + out_lasso(1, stream), + out_ridge(1, stream), + out_elasticnet(1, stream), + out_grad(params.n_cols, stream), + out_lasso_grad(params.n_cols, stream), + out_ridge_grad(params.n_cols, stream), + out_elasticnet_grad(params.n_cols, stream), + out_ref(1, stream), + out_lasso_ref(1, stream), + out_ridge_ref(1, stream), + out_elasticnet_ref(1, stream), + out_grad_ref(params.n_cols, stream), + out_lasso_grad_ref(params.n_cols, stream), + out_ridge_grad_ref(params.n_cols, stream), + out_elasticnet_grad_ref(params.n_cols, stream) + { + } + protected: void SetUp() override { - params = ::testing::TestWithParam>::GetParam(); int len = params.len; int n_rows = params.n_rows; int n_cols = params.n_cols; - T *labels, *coef; - - raft::handle_t handle; - cudaStream_t stream = handle.get_stream(); - - raft::allocate(in, len, stream); - raft::allocate(out, 1, stream); - raft::allocate(out_lasso, 1, stream); - raft::allocate(out_ridge, 1, stream); - raft::allocate(out_elasticnet, 1, stream); - raft::allocate(out_grad, n_cols, stream); - raft::allocate(out_lasso_grad, n_cols, stream); - raft::allocate(out_ridge_grad, n_cols, stream); - raft::allocate(out_elasticnet_grad, n_cols, stream); - raft::allocate(out_ref, 1, stream); - raft::allocate(out_lasso_ref, 1, stream); - raft::allocate(out_ridge_ref, 1, stream); - raft::allocate(out_elasticnet_ref, 1, stream); - raft::allocate(out_grad_ref, n_cols, stream); - raft::allocate(out_lasso_grad_ref, n_cols, stream); - raft::allocate(out_ridge_grad_ref, n_cols, stream); - raft::allocate(out_elasticnet_grad_ref, n_cols, stream); - - raft::allocate(labels, params.n_rows, stream); - raft::allocate(coef, params.n_cols, stream); + rmm::device_uvector labels(params.n_rows, stream); + rmm::device_uvector coef(params.n_cols, stream); T h_in[len] = {0.1, 0.35, -0.9, -1.4, 2.0, 3.1}; - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); T h_labels[n_rows] = {0.3, 2.0, -1.1}; - raft::update_device(labels, h_labels, n_rows, stream); + raft::update_device(labels.data(), h_labels, n_rows, stream); T h_coef[n_cols] = {0.35, -0.24}; - raft::update_device(coef, h_coef, n_cols, stream); + raft::update_device(coef.data(), h_coef, n_cols, stream); T h_out_ref[1] = {2.6037}; - raft::update_device(out_ref, h_out_ref, 1, stream); + raft::update_device(out_ref.data(), h_out_ref, 1, stream); T h_out_lasso_ref[1] = {2.9577}; - raft::update_device(out_lasso_ref, h_out_lasso_ref, 1, stream); + raft::update_device(out_lasso_ref.data(), h_out_lasso_ref, 1, stream); T h_out_ridge_ref[1] = {2.71176}; - raft::update_device(out_ridge_ref, h_out_ridge_ref, 1, stream); + raft::update_device(out_ridge_ref.data(), h_out_ridge_ref, 1, stream); T h_out_elasticnet_ref[1] = {2.83473}; - raft::update_device(out_elasticnet_ref, h_out_elasticnet_ref, 1, stream); + raft::update_device(out_elasticnet_ref.data(), h_out_elasticnet_ref, 1, stream); T h_out_grad_ref[n_cols] = {-0.24333, -1.1933}; - raft::update_device(out_grad_ref, h_out_grad_ref, n_cols, stream); + raft::update_device(out_grad_ref.data(), h_out_grad_ref, n_cols, stream); T h_out_lasso_grad_ref[n_cols] = {0.3566, -1.7933}; - raft::update_device(out_lasso_grad_ref, h_out_lasso_grad_ref, n_cols, stream); + raft::update_device(out_lasso_grad_ref.data(), h_out_lasso_grad_ref, n_cols, stream); T h_out_ridge_grad_ref[n_cols] = {0.1766, -1.4813}; - raft::update_device(out_ridge_grad_ref, h_out_ridge_grad_ref, n_cols, stream); + raft::update_device(out_ridge_grad_ref.data(), h_out_ridge_grad_ref, n_cols, stream); T h_out_elasticnet_grad_ref[n_cols] = {0.2666, -1.63733}; - raft::update_device(out_elasticnet_grad_ref, h_out_elasticnet_grad_ref, n_cols, stream); + raft::update_device(out_elasticnet_grad_ref.data(), h_out_elasticnet_grad_ref, n_cols, stream); T alpha = 0.6; T l1_ratio = 0.5; hingeLoss(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out, + labels.data(), + coef.data(), + out.data(), penalty::NONE, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); hingeLossGrads(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_grad, + labels.data(), + coef.data(), + out_grad.data(), penalty::NONE, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); hingeLoss(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_lasso, + labels.data(), + coef.data(), + out_lasso.data(), penalty::L1, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); hingeLossGrads(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_lasso_grad, + labels.data(), + coef.data(), + out_lasso_grad.data(), penalty::L1, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); hingeLoss(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_ridge, + labels.data(), + coef.data(), + out_ridge.data(), penalty::L2, alpha, l1_ratio, stream); hingeLossGrads(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_ridge_grad, + labels.data(), + coef.data(), + out_ridge_grad.data(), penalty::L2, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); hingeLoss(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_elasticnet, + labels.data(), + coef.data(), + out_elasticnet.data(), penalty::ELASTICNET, alpha, l1_ratio, stream); hingeLossGrads(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_elasticnet_grad, + labels.data(), + coef.data(), + out_elasticnet_grad.data(), penalty::ELASTICNET, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); - - CUDA_CHECK(cudaFree(labels)); - CUDA_CHECK(cudaFree(coef)); - } - - void TearDown() override - { - CUDA_CHECK(cudaFree(in)); - CUDA_CHECK(cudaFree(out)); - CUDA_CHECK(cudaFree(out_lasso)); - CUDA_CHECK(cudaFree(out_ridge)); - CUDA_CHECK(cudaFree(out_elasticnet)); - CUDA_CHECK(cudaFree(out_grad)); - CUDA_CHECK(cudaFree(out_lasso_grad)); - CUDA_CHECK(cudaFree(out_ridge_grad)); - CUDA_CHECK(cudaFree(out_elasticnet_grad)); - CUDA_CHECK(cudaFree(out_ref)); - CUDA_CHECK(cudaFree(out_lasso_ref)); - CUDA_CHECK(cudaFree(out_ridge_ref)); - CUDA_CHECK(cudaFree(out_elasticnet_ref)); - CUDA_CHECK(cudaFree(out_grad_ref)); - CUDA_CHECK(cudaFree(out_lasso_grad_ref)); - CUDA_CHECK(cudaFree(out_ridge_grad_ref)); - CUDA_CHECK(cudaFree(out_elasticnet_grad_ref)); + raft::update_device(in.data(), h_in, len, stream); } protected: HingeLossInputs params; - T* in; - T *out, *out_lasso, *out_ridge, *out_elasticnet; - T *out_ref, *out_lasso_ref, *out_ridge_ref, *out_elasticnet_ref; - T *out_grad, *out_lasso_grad, *out_ridge_grad, *out_elasticnet_grad; - T *out_grad_ref, *out_lasso_grad_ref, *out_ridge_grad_ref, *out_elasticnet_grad_ref; + + raft::handle_t handle; + cudaStream_t stream; + + rmm::device_uvector in, out, out_lasso, out_ridge, out_elasticnet; + rmm::device_uvector out_ref, out_lasso_ref, out_ridge_ref, out_elasticnet_ref; + rmm::device_uvector out_grad, out_lasso_grad, out_ridge_grad, out_elasticnet_grad; + rmm::device_uvector out_grad_ref, out_lasso_grad_ref, out_ridge_grad_ref, + out_elasticnet_grad_ref; }; const std::vector> inputsf = {{0.01f, 3, 2, 6}}; @@ -252,32 +232,37 @@ const std::vector> inputsd = {{0.01, 3, 2, 6}}; typedef HingeLossTest HingeLossTestF; TEST_P(HingeLossTestF, Result) { - ASSERT_TRUE(raft::devArrMatch(out_ref, out, 1, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE( - raft::devArrMatch(out_lasso_ref, out_lasso, 1, raft::CompareApprox(params.tolerance))); - - ASSERT_TRUE( - raft::devArrMatch(out_ridge_ref, out_ridge, 1, raft::CompareApprox(params.tolerance))); + raft::devArrMatch(out_ref.data(), out.data(), 1, raft::CompareApprox(params.tolerance))); ASSERT_TRUE(raft::devArrMatch( - out_elasticnet_ref, out_elasticnet, 1, raft::CompareApprox(params.tolerance))); + out_lasso_ref.data(), out_lasso.data(), 1, raft::CompareApprox(params.tolerance))); ASSERT_TRUE(raft::devArrMatch( - out_grad_ref, out_grad, params.n_cols, raft::CompareApprox(params.tolerance))); + out_ridge_ref.data(), out_ridge.data(), 1, raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(raft::devArrMatch(out_elasticnet_ref.data(), + out_elasticnet.data(), + 1, + raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(out_lasso_grad_ref, - out_lasso_grad, + ASSERT_TRUE(raft::devArrMatch(out_grad_ref.data(), + out_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(out_ridge_grad_ref, - out_ridge_grad, + ASSERT_TRUE(raft::devArrMatch(out_lasso_grad_ref.data(), + out_lasso_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(out_elasticnet_grad_ref, - out_elasticnet_grad, + ASSERT_TRUE(raft::devArrMatch(out_ridge_grad_ref.data(), + out_ridge_grad.data(), + params.n_cols, + raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(raft::devArrMatch(out_elasticnet_grad_ref.data(), + out_elasticnet_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); } @@ -285,32 +270,37 @@ TEST_P(HingeLossTestF, Result) typedef HingeLossTest HingeLossTestD; TEST_P(HingeLossTestD, Result) { - ASSERT_TRUE(raft::devArrMatch(out_ref, out, 1, raft::CompareApprox(params.tolerance))); - - ASSERT_TRUE( - raft::devArrMatch(out_lasso_ref, out_lasso, 1, raft::CompareApprox(params.tolerance))); - - ASSERT_TRUE( - raft::devArrMatch(out_ridge_ref, out_ridge, 1, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(raft::devArrMatch( + out_ref.data(), out.data(), 1, raft::CompareApprox(params.tolerance))); ASSERT_TRUE(raft::devArrMatch( - out_elasticnet_ref, out_elasticnet, 1, raft::CompareApprox(params.tolerance))); + out_lasso_ref.data(), out_lasso.data(), 1, raft::CompareApprox(params.tolerance))); ASSERT_TRUE(raft::devArrMatch( - out_grad_ref, out_grad, params.n_cols, raft::CompareApprox(params.tolerance))); + out_ridge_ref.data(), out_ridge.data(), 1, raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(raft::devArrMatch(out_elasticnet_ref.data(), + out_elasticnet.data(), + 1, + raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(raft::devArrMatch(out_grad_ref.data(), + out_grad.data(), + params.n_cols, + raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(out_lasso_grad_ref, - out_lasso_grad, + ASSERT_TRUE(raft::devArrMatch(out_lasso_grad_ref.data(), + out_lasso_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(out_ridge_grad_ref, - out_ridge_grad, + ASSERT_TRUE(raft::devArrMatch(out_ridge_grad_ref.data(), + out_ridge_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(out_elasticnet_grad_ref, - out_elasticnet_grad, + ASSERT_TRUE(raft::devArrMatch(out_elasticnet_grad_ref.data(), + out_elasticnet_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); } diff --git a/cpp/test/prims/jones_transform.cu b/cpp/test/prims/jones_transform.cu index 3451335ceb..44452c629b 100644 --- a/cpp/test/prims/jones_transform.cu +++ b/cpp/test/prims/jones_transform.cu @@ -17,7 +17,9 @@ #include #include #include +#include #include +#include #include namespace MLCommon { @@ -35,15 +37,25 @@ template class JonesTransTest : public ::testing::TestWithParam { + public: + JonesTransTest() + : params(::testing::TestWithParam::GetParam()), + stream(handle.get_stream()), + nElements(params.batchSize * params.pValue), + d_golden_ar_trans(0, stream), + d_computed_ar_trans(0, stream), + d_params(0, stream), + d_golden_ma_trans(0, stream), + d_computed_ma_trans(0, stream), + d_computed_ar_invtrans(0, stream), + d_computed_ma_invtrans(0, stream) + { + } + protected: // the constructor void SetUp() override { - // getting the parameters - params = ::testing::TestWithParam::GetParam(); - - nElements = params.batchSize * params.pValue; - // generating random value test input that is stored in row major std::vector arr1(nElements, 0); std::random_device rd; @@ -91,17 +103,27 @@ template } // allocating and initializing device memory - CUDA_CHECK(cudaStreamCreate(&stream)); - raft::allocate(d_golden_ar_trans, nElements, stream, true); - raft::allocate(d_computed_ar_trans, nElements, stream, true); - raft::allocate(d_params, nElements, stream, true); - - raft::update_device(d_params, &arr1[0], (size_t)nElements, stream); - raft::update_device(d_golden_ar_trans, newParams, (size_t)nElements, stream); + d_golden_ar_trans.resize(nElements, stream); + d_computed_ar_trans.resize(nElements, stream); + d_params.resize(nElements, stream); + CUDA_CHECK(cudaMemsetAsync( + d_golden_ar_trans.data(), 0, d_golden_ar_trans.size() * sizeof(DataT), stream)); + CUDA_CHECK(cudaMemsetAsync( + d_computed_ar_trans.data(), 0, d_computed_ar_trans.size() * sizeof(DataT), stream)); + CUDA_CHECK(cudaMemsetAsync(d_params.data(), 0, d_params.size() * sizeof(DataT), stream)); + + raft::update_device(d_params.data(), &arr1[0], (size_t)nElements, stream); + raft::update_device(d_golden_ar_trans.data(), newParams, (size_t)nElements, stream); // calling the ar_trans_param CUDA implementation - MLCommon::TimeSeries::jones_transform( - d_params, params.batchSize, params.pValue, d_computed_ar_trans, true, false, stream, false); + MLCommon::TimeSeries::jones_transform(d_params.data(), + params.batchSize, + params.pValue, + d_computed_ar_trans.data(), + true, + false, + stream, + false); //>>>>>>>>> MA transform golden output generation<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @@ -138,68 +160,64 @@ template } } - // allocating and initializing device memory - raft::allocate(d_golden_ma_trans, nElements, stream, true); - raft::allocate(d_computed_ma_trans, nElements, stream, true); + d_golden_ma_trans.resize(nElements, stream); + d_computed_ma_trans.resize(nElements, stream); + CUDA_CHECK(cudaMemsetAsync( + d_golden_ma_trans.data(), 0, d_golden_ma_trans.size() * sizeof(DataT), stream)); + CUDA_CHECK(cudaMemsetAsync( + d_computed_ma_trans.data(), 0, d_computed_ma_trans.size() * sizeof(DataT), stream)); - raft::update_device(d_golden_ma_trans, newParams, (size_t)nElements, stream); + raft::update_device(d_golden_ma_trans.data(), newParams, (size_t)nElements, stream); // calling the ma_param_transform CUDA implementation - MLCommon::TimeSeries::jones_transform( - d_params, params.batchSize, params.pValue, d_computed_ma_trans, false, false, stream, false); + MLCommon::TimeSeries::jones_transform(d_params.data(), + params.batchSize, + params.pValue, + d_computed_ma_trans.data(), + false, + false, + stream, + false); //>>>>>>>>> AR inverse transform <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< - // allocating and initializing device memory - raft::allocate(d_computed_ar_invtrans, nElements, stream, true); + d_computed_ar_invtrans.resize(nElements, stream); + CUDA_CHECK(cudaMemsetAsync( + d_computed_ar_invtrans.data(), 0, d_computed_ar_invtrans.size() * sizeof(DataT), stream)); // calling the ar_param_inverse_transform CUDA implementation - MLCommon::TimeSeries::jones_transform(d_computed_ar_trans, + MLCommon::TimeSeries::jones_transform(d_computed_ar_trans.data(), params.batchSize, params.pValue, - d_computed_ar_invtrans, + d_computed_ar_invtrans.data(), true, true, stream); //>>>>>>>>> MA inverse transform <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< - raft::allocate(d_computed_ma_invtrans, nElements, stream, true); + d_computed_ma_invtrans.resize(nElements, stream); + CUDA_CHECK(cudaMemsetAsync( + d_computed_ma_invtrans.data(), 0, d_computed_ma_invtrans.size() * sizeof(DataT), stream)); // calling the ma_param_inverse_transform CUDA implementation - MLCommon::TimeSeries::jones_transform(d_computed_ma_trans, + MLCommon::TimeSeries::jones_transform(d_computed_ma_trans.data(), params.batchSize, params.pValue, - d_computed_ma_invtrans, + d_computed_ma_invtrans.data(), false, true, stream); } - // the destructor - void TearDown() override - { - CUDA_CHECK(cudaFree(d_computed_ar_trans)); - CUDA_CHECK(cudaFree(d_computed_ma_trans)); - CUDA_CHECK(cudaFree(d_computed_ar_invtrans)); - CUDA_CHECK(cudaFree(d_computed_ma_invtrans)); - CUDA_CHECK(cudaFree(d_golden_ar_trans)); - CUDA_CHECK(cudaFree(d_golden_ma_trans)); - CUDA_CHECK(cudaFree(d_params)); - CUDA_CHECK(cudaStreamDestroy(stream)); - } + raft::handle_t handle; + cudaStream_t stream = 0; // declaring the data values JonesTransParam params; - DataT* d_golden_ar_trans = nullptr; - DataT* d_golden_ma_trans = nullptr; - DataT* d_computed_ar_trans = nullptr; - DataT* d_computed_ma_trans = nullptr; - DataT* d_computed_ar_invtrans = nullptr; - DataT* d_computed_ma_invtrans = nullptr; - DataT* d_params = nullptr; - cudaStream_t stream = 0; - int nElements = -1; + rmm::device_uvector d_golden_ar_trans, d_golden_ma_trans, d_computed_ar_trans, + d_computed_ma_trans, d_computed_ar_invtrans, d_computed_ma_invtrans, d_params; + int nElements = -1; }; // setting test parameter values @@ -224,12 +242,12 @@ const std::vector inputs = {{500, 4, 0.001}, typedef JonesTransTest JonesTransTestClass; TEST_P(JonesTransTestClass, Result) { - ASSERT_TRUE(raft::devArrMatch(d_golden_ar_trans, - d_computed_ar_trans, + ASSERT_TRUE(raft::devArrMatch(d_golden_ar_trans.data(), + d_computed_ar_trans.data(), nElements, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(d_golden_ma_trans, - d_computed_ma_trans, + ASSERT_TRUE(raft::devArrMatch(d_golden_ma_trans.data(), + d_computed_ma_trans.data(), nElements, raft::CompareApprox(params.tolerance))); /* @@ -238,10 +256,14 @@ TEST_P(JonesTransTestClass, Result) transformed coefficients -> ar_param_inverse_transform()/ma_param_inverse_transform() -> initially generated random coefficients */ - ASSERT_TRUE(raft::devArrMatch( - d_computed_ma_invtrans, d_params, nElements, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch( - d_computed_ar_invtrans, d_params, nElements, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(raft::devArrMatch(d_computed_ma_invtrans.data(), + d_params.data(), + nElements, + raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(raft::devArrMatch(d_computed_ar_invtrans.data(), + d_params.data(), + nElements, + raft::CompareApprox(params.tolerance))); } INSTANTIATE_TEST_CASE_P(JonesTrans, JonesTransTestClass, ::testing::ValuesIn(inputs)); diff --git a/cpp/test/prims/knn_classify.cu b/cpp/test/prims/knn_classify.cu index d21d57a9bd..a0021d3e68 100644 --- a/cpp/test/prims/knn_classify.cu +++ b/cpp/test/prims/knn_classify.cu @@ -38,24 +38,24 @@ struct KNNClassifyInputs { }; class KNNClassifyTest : public ::testing::TestWithParam { + public: + KNNClassifyTest() + : params(::testing::TestWithParam::GetParam()), + stream(handle.get_stream()), + train_samples(params.rows * params.cols, stream), + train_labels(params.rows, stream), + pred_labels(params.rows, stream), + knn_indices(params.rows * params.k, stream), + knn_dists(params.rows * params.k, stream) + { + basicTest(); + } + protected: void basicTest() { - raft::handle_t handle; - cudaStream_t stream = handle.get_stream(); - - params = ::testing::TestWithParam::GetParam(); - - raft::allocate(train_samples, params.rows * params.cols, stream); - raft::allocate(train_labels, params.rows, stream); - - raft::allocate(pred_labels, params.rows, stream); - - raft::allocate(knn_indices, params.rows * params.k, stream); - raft::allocate(knn_dists, params.rows * params.k, stream); - - MLCommon::Random::make_blobs(train_samples, - train_labels, + MLCommon::Random::make_blobs(train_samples.data(), + train_labels.data(), params.rows, params.cols, params.n_labels, @@ -66,25 +66,26 @@ class KNNClassifyTest : public ::testing::TestWithParam { params.cluster_std); rmm::device_uvector unique_labels(0, stream); - auto n_classes = raft::label::getUniquelabels(unique_labels, train_labels, params.rows, stream); + auto n_classes = + raft::label::getUniquelabels(unique_labels, train_labels.data(), params.rows, stream); std::vector ptrs(1); std::vector sizes(1); - ptrs[0] = train_samples; + ptrs[0] = train_samples.data(); sizes[0] = params.rows; raft::spatial::knn::brute_force_knn(handle, ptrs, sizes, params.cols, - train_samples, + train_samples.data(), params.rows, - knn_indices, - knn_dists, + knn_indices.data(), + knn_dists.data(), params.k); std::vector y; - y.push_back(train_labels); + y.push_back(train_labels.data()); std::vector uniq_labels; uniq_labels.push_back(unique_labels.data()); @@ -93,8 +94,8 @@ class KNNClassifyTest : public ::testing::TestWithParam { n_unique.push_back(n_classes); knn_classify(handle, - pred_labels, - knn_indices, + pred_labels.data(), + knn_indices.data(), y, params.rows, params.rows, @@ -105,35 +106,25 @@ class KNNClassifyTest : public ::testing::TestWithParam { CUDA_CHECK(cudaStreamSynchronize(stream)); } - void SetUp() override { basicTest(); } - - void TearDown() override - { - CUDA_CHECK(cudaFree(train_samples)); - CUDA_CHECK(cudaFree(train_labels)); - - CUDA_CHECK(cudaFree(pred_labels)); - - CUDA_CHECK(cudaFree(knn_indices)); - CUDA_CHECK(cudaFree(knn_dists)); - } - protected: KNNClassifyInputs params; + raft::handle_t handle; + cudaStream_t stream; - float* train_samples; - int* train_labels; + rmm::device_uvector train_samples; + rmm::device_uvector train_labels; - int* pred_labels; + rmm::device_uvector pred_labels; - int64_t* knn_indices; - float* knn_dists; + rmm::device_uvector knn_indices; + rmm::device_uvector knn_dists; }; typedef KNNClassifyTest KNNClassifyTestF; TEST_P(KNNClassifyTestF, Fit) { - ASSERT_TRUE(devArrMatch(train_labels, pred_labels, params.rows, raft::Compare())); + ASSERT_TRUE( + devArrMatch(train_labels.data(), pred_labels.data(), params.rows, raft::Compare())); } const std::vector inputsf = {{100, 10, 2, 0.01f, 2}, diff --git a/cpp/test/prims/knn_regression.cu b/cpp/test/prims/knn_regression.cu index 8371209ba1..73ec9b6ec7 100644 --- a/cpp/test/prims/knn_regression.cu +++ b/cpp/test/prims/knn_regression.cu @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -77,82 +78,69 @@ void generate_data( } class KNNRegressionTest : public ::testing::TestWithParam { + public: + KNNRegressionTest() + : params(::testing::TestWithParam::GetParam()), + stream(handle.get_stream()), + train_samples(params.rows * params.cols, stream), + train_labels(params.rows, stream), + pred_labels(params.rows, stream), + knn_indices(params.rows * params.k, stream), + knn_dists(params.rows * params.k, stream) + { + } + protected: void basicTest() { - raft::handle_t handle; - cudaStream_t stream = handle.get_stream(); - - cublasHandle_t cublas_handle; - CUBLAS_CHECK(cublasCreate(&cublas_handle)); - - cusolverDnHandle_t cusolverDn_handle; - CUSOLVER_CHECK(cusolverDnCreate(&cusolverDn_handle)); - - params = ::testing::TestWithParam::GetParam(); - - raft::allocate(train_samples, params.rows * params.cols, stream); - raft::allocate(train_labels, params.rows, stream); - - raft::allocate(pred_labels, params.rows, stream); - - raft::allocate(knn_indices, params.rows * params.k, stream); - raft::allocate(knn_dists, params.rows * params.k, stream); - - generate_data(train_samples, train_labels, params.rows, params.cols, stream); + generate_data(train_samples.data(), train_labels.data(), params.rows, params.cols, stream); std::vector ptrs(1); std::vector sizes(1); - ptrs[0] = train_samples; + ptrs[0] = train_samples.data(); sizes[0] = params.rows; raft::spatial::knn::brute_force_knn(handle, ptrs, sizes, params.cols, - train_samples, + train_samples.data(), params.rows, - knn_indices, - knn_dists, + knn_indices.data(), + knn_dists.data(), params.k); std::vector y; - y.push_back(train_labels); + y.push_back(train_labels.data()); - knn_regress(handle, pred_labels, knn_indices, y, params.rows, params.rows, params.k); + knn_regress( + handle, pred_labels.data(), knn_indices.data(), y, params.rows, params.rows, params.k); CUDA_CHECK(cudaStreamSynchronize(stream)); } void SetUp() override { basicTest(); } - void TearDown() override - { - CUDA_CHECK(cudaFree(train_samples)); - CUDA_CHECK(cudaFree(train_labels)); - - CUDA_CHECK(cudaFree(pred_labels)); - - CUDA_CHECK(cudaFree(knn_indices)); - CUDA_CHECK(cudaFree(knn_dists)); - } - protected: + raft::handle_t handle; + cudaStream_t stream; + KNNRegressionInputs params; - float* train_samples; - float* train_labels; + rmm::device_uvector train_samples; + rmm::device_uvector train_labels; - float* pred_labels; + rmm::device_uvector pred_labels; - int64_t* knn_indices; - float* knn_dists; + rmm::device_uvector knn_indices; + rmm::device_uvector knn_dists; }; typedef KNNRegressionTest KNNRegressionTestF; TEST_P(KNNRegressionTestF, Fit) { - ASSERT_TRUE(devArrMatch(train_labels, pred_labels, params.rows, raft::CompareApprox(0.3))); + ASSERT_TRUE(devArrMatch( + train_labels.data(), pred_labels.data(), params.rows, raft::CompareApprox(0.3))); } const std::vector inputsf = {{100, 10, 2, 0.01f, 2}, diff --git a/cpp/test/prims/linearReg.cu b/cpp/test/prims/linearReg.cu index 5cc753a9f9..aabe5a413b 100644 --- a/cpp/test/prims/linearReg.cu +++ b/cpp/test/prims/linearReg.cu @@ -33,217 +33,195 @@ struct LinRegLossInputs { template class LinRegLossTest : public ::testing::TestWithParam> { + public: + LinRegLossTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + in(params.len, stream), + out(1, stream), + out_lasso(1, stream), + out_ridge(1, stream), + out_elasticnet(1, stream), + out_grad(params.n_cols, stream), + out_lasso_grad(params.n_cols, stream), + out_ridge_grad(params.n_cols, stream), + out_elasticnet_grad(params.n_cols, stream), + out_ref(1, stream), + out_lasso_ref(1, stream), + out_ridge_ref(1, stream), + out_elasticnet_ref(1, stream), + out_grad_ref(params.n_cols, stream), + out_lasso_grad_ref(params.n_cols, stream), + out_ridge_grad_ref(params.n_cols, stream), + out_elasticnet_grad_ref(params.n_cols, stream) + { + } + protected: void SetUp() override { - params = ::testing::TestWithParam>::GetParam(); int len = params.len; int n_rows = params.n_rows; int n_cols = params.n_cols; - T *labels, *coef; - - raft::handle_t handle; - - cudaStream_t stream = handle.get_stream(); - - raft::allocate(in, len, stream); - raft::allocate(out, 1, stream); - raft::allocate(out_lasso, 1, stream); - raft::allocate(out_ridge, 1, stream); - raft::allocate(out_elasticnet, 1, stream); - raft::allocate(out_grad, n_cols, stream); - raft::allocate(out_lasso_grad, n_cols, stream); - raft::allocate(out_ridge_grad, n_cols, stream); - raft::allocate(out_elasticnet_grad, n_cols, stream); - raft::allocate(out_ref, 1, stream); - raft::allocate(out_lasso_ref, 1, stream); - raft::allocate(out_ridge_ref, 1, stream); - raft::allocate(out_elasticnet_ref, 1, stream); - raft::allocate(out_grad_ref, n_cols, stream); - raft::allocate(out_lasso_grad_ref, n_cols, stream); - raft::allocate(out_ridge_grad_ref, n_cols, stream); - raft::allocate(out_elasticnet_grad_ref, n_cols, stream); - - raft::allocate(labels, params.n_rows, stream); - raft::allocate(coef, params.n_cols, stream); + rmm::device_uvector labels(params.n_rows, stream); + rmm::device_uvector coef(params.n_cols, stream); T h_in[len] = {0.1, 0.35, -0.9, -1.4, 2.0, 3.1}; - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); T h_labels[n_rows] = {0.3, 2.0, -1.1}; - raft::update_device(labels, h_labels, n_rows, stream); + raft::update_device(labels.data(), h_labels, n_rows, stream); T h_coef[n_cols] = {0.35, -0.24}; - raft::update_device(coef, h_coef, n_cols, stream); + raft::update_device(coef.data(), h_coef, n_cols, stream); T h_out_ref[1] = {1.854842}; - raft::update_device(out_ref, h_out_ref, 1, stream); + raft::update_device(out_ref.data(), h_out_ref, 1, stream); T h_out_lasso_ref[1] = {2.2088}; - raft::update_device(out_lasso_ref, h_out_lasso_ref, 1, stream); + raft::update_device(out_lasso_ref.data(), h_out_lasso_ref, 1, stream); T h_out_ridge_ref[1] = {1.9629}; - raft::update_device(out_ridge_ref, h_out_ridge_ref, 1, stream); + raft::update_device(out_ridge_ref.data(), h_out_ridge_ref, 1, stream); T h_out_elasticnet_ref[1] = {2.0858}; - raft::update_device(out_elasticnet_ref, h_out_elasticnet_ref, 1, stream); + raft::update_device(out_elasticnet_ref.data(), h_out_elasticnet_ref, 1, stream); T h_out_grad_ref[n_cols] = {-0.56995, -3.12486}; - raft::update_device(out_grad_ref, h_out_grad_ref, n_cols, stream); + raft::update_device(out_grad_ref.data(), h_out_grad_ref, n_cols, stream); T h_out_lasso_grad_ref[n_cols] = {0.03005, -3.724866}; - raft::update_device(out_lasso_grad_ref, h_out_lasso_grad_ref, n_cols, stream); + raft::update_device(out_lasso_grad_ref.data(), h_out_lasso_grad_ref, n_cols, stream); T h_out_ridge_grad_ref[n_cols] = {-0.14995, -3.412866}; - raft::update_device(out_ridge_grad_ref, h_out_ridge_grad_ref, n_cols, stream); + raft::update_device(out_ridge_grad_ref.data(), h_out_ridge_grad_ref, n_cols, stream); T h_out_elasticnet_grad_ref[n_cols] = {-0.05995, -3.568866}; - raft::update_device(out_elasticnet_grad_ref, h_out_elasticnet_grad_ref, n_cols, stream); + raft::update_device(out_elasticnet_grad_ref.data(), h_out_elasticnet_grad_ref, n_cols, stream); T alpha = 0.6; T l1_ratio = 0.5; linearRegLoss(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out, + labels.data(), + coef.data(), + out.data(), penalty::NONE, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); linearRegLossGrads(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_grad, + labels.data(), + coef.data(), + out_grad.data(), penalty::NONE, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); linearRegLoss(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_lasso, + labels.data(), + coef.data(), + out_lasso.data(), penalty::L1, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); linearRegLossGrads(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_lasso_grad, + labels.data(), + coef.data(), + out_lasso_grad.data(), penalty::L1, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); linearRegLoss(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_ridge, + labels.data(), + coef.data(), + out_ridge.data(), penalty::L2, alpha, l1_ratio, stream); linearRegLossGrads(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_ridge_grad, + labels.data(), + coef.data(), + out_ridge_grad.data(), penalty::L2, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); linearRegLoss(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_elasticnet, + labels.data(), + coef.data(), + out_elasticnet.data(), penalty::ELASTICNET, alpha, l1_ratio, stream); linearRegLossGrads(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_elasticnet_grad, + labels.data(), + coef.data(), + out_elasticnet_grad.data(), penalty::ELASTICNET, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); - - CUDA_CHECK(cudaFree(labels)); - CUDA_CHECK(cudaFree(coef)); - } - - void TearDown() override - { - CUDA_CHECK(cudaFree(in)); - CUDA_CHECK(cudaFree(out)); - CUDA_CHECK(cudaFree(out_lasso)); - CUDA_CHECK(cudaFree(out_ridge)); - CUDA_CHECK(cudaFree(out_elasticnet)); - CUDA_CHECK(cudaFree(out_grad)); - CUDA_CHECK(cudaFree(out_lasso_grad)); - CUDA_CHECK(cudaFree(out_ridge_grad)); - CUDA_CHECK(cudaFree(out_elasticnet_grad)); - CUDA_CHECK(cudaFree(out_ref)); - CUDA_CHECK(cudaFree(out_lasso_ref)); - CUDA_CHECK(cudaFree(out_ridge_ref)); - CUDA_CHECK(cudaFree(out_elasticnet_ref)); - CUDA_CHECK(cudaFree(out_grad_ref)); - CUDA_CHECK(cudaFree(out_lasso_grad_ref)); - CUDA_CHECK(cudaFree(out_ridge_grad_ref)); - CUDA_CHECK(cudaFree(out_elasticnet_grad_ref)); + raft::update_device(in.data(), h_in, len, stream); } protected: LinRegLossInputs params; - T* in; - T *out, *out_lasso, *out_ridge, *out_elasticnet; - T *out_ref, *out_lasso_ref, *out_ridge_ref, *out_elasticnet_ref; - T *out_grad, *out_lasso_grad, *out_ridge_grad, *out_elasticnet_grad; - T *out_grad_ref, *out_lasso_grad_ref, *out_ridge_grad_ref, *out_elasticnet_grad_ref; + raft::handle_t handle; + cudaStream_t stream; + + rmm::device_uvector in, out, out_lasso, out_ridge, out_elasticnet; + rmm::device_uvector out_ref, out_lasso_ref, out_ridge_ref, out_elasticnet_ref; + rmm::device_uvector out_grad, out_lasso_grad, out_ridge_grad, out_elasticnet_grad; + rmm::device_uvector out_grad_ref, out_lasso_grad_ref, out_ridge_grad_ref, + out_elasticnet_grad_ref; }; const std::vector> inputsf = {{0.01f, 3, 2, 6}}; @@ -253,32 +231,37 @@ const std::vector> inputsd = {{0.01, 3, 2, 6}}; typedef LinRegLossTest LinRegLossTestF; TEST_P(LinRegLossTestF, Result) { - ASSERT_TRUE(devArrMatch(out_ref, out, 1, raft::CompareApprox(params.tolerance))); - - ASSERT_TRUE( - devArrMatch(out_lasso_ref, out_lasso, 1, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE( - devArrMatch(out_ridge_ref, out_ridge, 1, raft::CompareApprox(params.tolerance))); + devArrMatch(out_ref.data(), out.data(), 1, raft::CompareApprox(params.tolerance))); ASSERT_TRUE(devArrMatch( - out_elasticnet_ref, out_elasticnet, 1, raft::CompareApprox(params.tolerance))); + out_lasso_ref.data(), out_lasso.data(), 1, raft::CompareApprox(params.tolerance))); ASSERT_TRUE(devArrMatch( - out_grad_ref, out_grad, params.n_cols, raft::CompareApprox(params.tolerance))); + out_ridge_ref.data(), out_ridge.data(), 1, raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(devArrMatch(out_elasticnet_ref.data(), + out_elasticnet.data(), + 1, + raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(devArrMatch(out_lasso_grad_ref, - out_lasso_grad, + ASSERT_TRUE(devArrMatch(out_grad_ref.data(), + out_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(devArrMatch(out_ridge_grad_ref, - out_ridge_grad, + ASSERT_TRUE(devArrMatch(out_lasso_grad_ref.data(), + out_lasso_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(devArrMatch(out_elasticnet_grad_ref, - out_elasticnet_grad, + ASSERT_TRUE(devArrMatch(out_ridge_grad_ref.data(), + out_ridge_grad.data(), + params.n_cols, + raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(devArrMatch(out_elasticnet_grad_ref.data(), + out_elasticnet_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); } @@ -286,32 +269,37 @@ TEST_P(LinRegLossTestF, Result) typedef LinRegLossTest LinRegLossTestD; TEST_P(LinRegLossTestD, Result) { - ASSERT_TRUE(devArrMatch(out_ref, out, 1, raft::CompareApprox(params.tolerance))); - - ASSERT_TRUE( - devArrMatch(out_lasso_ref, out_lasso, 1, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE( - devArrMatch(out_ridge_ref, out_ridge, 1, raft::CompareApprox(params.tolerance))); + devArrMatch(out_ref.data(), out.data(), 1, raft::CompareApprox(params.tolerance))); ASSERT_TRUE(devArrMatch( - out_elasticnet_ref, out_elasticnet, 1, raft::CompareApprox(params.tolerance))); + out_lasso_ref.data(), out_lasso.data(), 1, raft::CompareApprox(params.tolerance))); ASSERT_TRUE(devArrMatch( - out_grad_ref, out_grad, params.n_cols, raft::CompareApprox(params.tolerance))); + out_ridge_ref.data(), out_ridge.data(), 1, raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(devArrMatch(out_elasticnet_ref.data(), + out_elasticnet.data(), + 1, + raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(devArrMatch(out_grad_ref.data(), + out_grad.data(), + params.n_cols, + raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(devArrMatch(out_lasso_grad_ref, - out_lasso_grad, + ASSERT_TRUE(devArrMatch(out_lasso_grad_ref.data(), + out_lasso_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(devArrMatch(out_ridge_grad_ref, - out_ridge_grad, + ASSERT_TRUE(devArrMatch(out_ridge_grad_ref.data(), + out_ridge_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(devArrMatch(out_elasticnet_grad_ref, - out_elasticnet_grad, + ASSERT_TRUE(devArrMatch(out_elasticnet_grad_ref.data(), + out_elasticnet_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); } diff --git a/cpp/test/prims/logisticReg.cu b/cpp/test/prims/logisticReg.cu index d02ea1d71d..8f0fc24c3d 100644 --- a/cpp/test/prims/logisticReg.cu +++ b/cpp/test/prims/logisticReg.cu @@ -33,217 +33,195 @@ struct LogRegLossInputs { template class LogRegLossTest : public ::testing::TestWithParam> { + public: + LogRegLossTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + in(params.len, stream), + out(1, stream), + out_lasso(1, stream), + out_ridge(1, stream), + out_elasticnet(1, stream), + out_grad(params.n_cols, stream), + out_lasso_grad(params.n_cols, stream), + out_ridge_grad(params.n_cols, stream), + out_elasticnet_grad(params.n_cols, stream), + out_ref(1, stream), + out_lasso_ref(1, stream), + out_ridge_ref(1, stream), + out_elasticnet_ref(1, stream), + out_grad_ref(params.n_cols, stream), + out_lasso_grad_ref(params.n_cols, stream), + out_ridge_grad_ref(params.n_cols, stream), + out_elasticnet_grad_ref(params.n_cols, stream) + { + } + protected: void SetUp() override { - params = ::testing::TestWithParam>::GetParam(); int len = params.len; int n_rows = params.n_rows; int n_cols = params.n_cols; - T *labels, *coef; - - raft::handle_t handle; - - cudaStream_t stream = handle.get_stream(); - - raft::allocate(in, len, stream); - raft::allocate(out, 1, stream); - raft::allocate(out_lasso, 1, stream); - raft::allocate(out_ridge, 1, stream); - raft::allocate(out_elasticnet, 1, stream); - raft::allocate(out_grad, n_cols, stream); - raft::allocate(out_lasso_grad, n_cols, stream); - raft::allocate(out_ridge_grad, n_cols, stream); - raft::allocate(out_elasticnet_grad, n_cols, stream); - raft::allocate(out_ref, 1, stream); - raft::allocate(out_lasso_ref, 1, stream); - raft::allocate(out_ridge_ref, 1, stream); - raft::allocate(out_elasticnet_ref, 1, stream); - raft::allocate(out_grad_ref, n_cols, stream); - raft::allocate(out_lasso_grad_ref, n_cols, stream); - raft::allocate(out_ridge_grad_ref, n_cols, stream); - raft::allocate(out_elasticnet_grad_ref, n_cols, stream); - - raft::allocate(labels, params.n_rows, stream); - raft::allocate(coef, params.n_cols, stream); + rmm::device_uvector labels(params.n_rows, stream); + rmm::device_uvector coef(params.n_cols, stream); T h_in[len] = {0.1, 0.35, -0.9, -1.4, 2.0, 3.1}; - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); T h_labels[n_rows] = {0.3, 2.0, -1.1}; - raft::update_device(labels, h_labels, n_rows, stream); + raft::update_device(labels.data(), h_labels, n_rows, stream); T h_coef[n_cols] = {0.35, -0.24}; - raft::update_device(coef, h_coef, n_cols, stream); + raft::update_device(coef.data(), h_coef, n_cols, stream); T h_out_ref[1] = {0.38752545}; - raft::update_device(out_ref, h_out_ref, 1, stream); + raft::update_device(out_ref.data(), h_out_ref, 1, stream); T h_out_lasso_ref[1] = {0.74152}; - raft::update_device(out_lasso_ref, h_out_lasso_ref, 1, stream); + raft::update_device(out_lasso_ref.data(), h_out_lasso_ref, 1, stream); T h_out_ridge_ref[1] = {0.4955854}; - raft::update_device(out_ridge_ref, h_out_ridge_ref, 1, stream); + raft::update_device(out_ridge_ref.data(), h_out_ridge_ref, 1, stream); T h_out_elasticnet_ref[1] = {0.618555}; - raft::update_device(out_elasticnet_ref, h_out_elasticnet_ref, 1, stream); + raft::update_device(out_elasticnet_ref.data(), h_out_elasticnet_ref, 1, stream); T h_out_grad_ref[n_cols] = {-0.58284, 0.207666}; - raft::update_device(out_grad_ref, h_out_grad_ref, n_cols, stream); + raft::update_device(out_grad_ref.data(), h_out_grad_ref, n_cols, stream); T h_out_lasso_grad_ref[n_cols] = {0.0171, -0.39233}; - raft::update_device(out_lasso_grad_ref, h_out_lasso_grad_ref, n_cols, stream); + raft::update_device(out_lasso_grad_ref.data(), h_out_lasso_grad_ref, n_cols, stream); T h_out_ridge_grad_ref[n_cols] = {-0.16284, -0.080333}; - raft::update_device(out_ridge_grad_ref, h_out_ridge_grad_ref, n_cols, stream); + raft::update_device(out_ridge_grad_ref.data(), h_out_ridge_grad_ref, n_cols, stream); T h_out_elasticnet_grad_ref[n_cols] = {-0.07284, -0.23633}; - raft::update_device(out_elasticnet_grad_ref, h_out_elasticnet_grad_ref, n_cols, stream); + raft::update_device(out_elasticnet_grad_ref.data(), h_out_elasticnet_grad_ref, n_cols, stream); T alpha = 0.6; T l1_ratio = 0.5; logisticRegLoss(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out, + labels.data(), + coef.data(), + out.data(), penalty::NONE, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); logisticRegLossGrads(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_grad, + labels.data(), + coef.data(), + out_grad.data(), penalty::NONE, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); logisticRegLoss(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_lasso, + labels.data(), + coef.data(), + out_lasso.data(), penalty::L1, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); logisticRegLossGrads(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_lasso_grad, + labels.data(), + coef.data(), + out_lasso_grad.data(), penalty::L1, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); logisticRegLoss(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_ridge, + labels.data(), + coef.data(), + out_ridge.data(), penalty::L2, alpha, l1_ratio, stream); logisticRegLossGrads(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_ridge_grad, + labels.data(), + coef.data(), + out_ridge_grad.data(), penalty::L2, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); logisticRegLoss(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_elasticnet, + labels.data(), + coef.data(), + out_elasticnet.data(), penalty::ELASTICNET, alpha, l1_ratio, stream); logisticRegLossGrads(handle, - in, + in.data(), params.n_rows, params.n_cols, - labels, - coef, - out_elasticnet_grad, + labels.data(), + coef.data(), + out_elasticnet_grad.data(), penalty::ELASTICNET, alpha, l1_ratio, stream); - raft::update_device(in, h_in, len, stream); - - CUDA_CHECK(cudaFree(labels)); - CUDA_CHECK(cudaFree(coef)); - } - - void TearDown() override - { - CUDA_CHECK(cudaFree(in)); - CUDA_CHECK(cudaFree(out)); - CUDA_CHECK(cudaFree(out_lasso)); - CUDA_CHECK(cudaFree(out_ridge)); - CUDA_CHECK(cudaFree(out_elasticnet)); - CUDA_CHECK(cudaFree(out_grad)); - CUDA_CHECK(cudaFree(out_lasso_grad)); - CUDA_CHECK(cudaFree(out_ridge_grad)); - CUDA_CHECK(cudaFree(out_elasticnet_grad)); - CUDA_CHECK(cudaFree(out_ref)); - CUDA_CHECK(cudaFree(out_lasso_ref)); - CUDA_CHECK(cudaFree(out_ridge_ref)); - CUDA_CHECK(cudaFree(out_elasticnet_ref)); - CUDA_CHECK(cudaFree(out_grad_ref)); - CUDA_CHECK(cudaFree(out_lasso_grad_ref)); - CUDA_CHECK(cudaFree(out_ridge_grad_ref)); - CUDA_CHECK(cudaFree(out_elasticnet_grad_ref)); + raft::update_device(in.data(), h_in, len, stream); } protected: LogRegLossInputs params; - T* in; - T *out, *out_lasso, *out_ridge, *out_elasticnet; - T *out_ref, *out_lasso_ref, *out_ridge_ref, *out_elasticnet_ref; - T *out_grad, *out_lasso_grad, *out_ridge_grad, *out_elasticnet_grad; - T *out_grad_ref, *out_lasso_grad_ref, *out_ridge_grad_ref, *out_elasticnet_grad_ref; + raft::handle_t handle; + cudaStream_t stream = 0; + + rmm::device_uvector in, out, out_lasso, out_ridge, out_elasticnet; + rmm::device_uvector out_ref, out_lasso_ref, out_ridge_ref, out_elasticnet_ref; + rmm::device_uvector out_grad, out_lasso_grad, out_ridge_grad, out_elasticnet_grad; + rmm::device_uvector out_grad_ref, out_lasso_grad_ref, out_ridge_grad_ref, + out_elasticnet_grad_ref; }; const std::vector> inputsf = {{0.01f, 3, 2, 6}}; @@ -253,32 +231,37 @@ const std::vector> inputsd = {{0.01, 3, 2, 6}}; typedef LogRegLossTest LogRegLossTestF; TEST_P(LogRegLossTestF, Result) { - ASSERT_TRUE(raft::devArrMatch(out_ref, out, 1, raft::CompareApprox(params.tolerance))); - - ASSERT_TRUE( - raft::devArrMatch(out_lasso_ref, out_lasso, 1, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE( - raft::devArrMatch(out_ridge_ref, out_ridge, 1, raft::CompareApprox(params.tolerance))); + raft::devArrMatch(out_ref.data(), out.data(), 1, raft::CompareApprox(params.tolerance))); ASSERT_TRUE(raft::devArrMatch( - out_elasticnet_ref, out_elasticnet, 1, raft::CompareApprox(params.tolerance))); + out_lasso_ref.data(), out_lasso.data(), 1, raft::CompareApprox(params.tolerance))); ASSERT_TRUE(raft::devArrMatch( - out_grad_ref, out_grad, params.n_cols, raft::CompareApprox(params.tolerance))); + out_ridge_ref.data(), out_ridge.data(), 1, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(out_lasso_grad_ref, - out_lasso_grad, + ASSERT_TRUE(raft::devArrMatch(out_elasticnet_ref.data(), + out_elasticnet.data(), + 1, + raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(raft::devArrMatch(out_grad_ref.data(), + out_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(out_ridge_grad_ref, - out_ridge_grad, + ASSERT_TRUE(raft::devArrMatch(out_lasso_grad_ref.data(), + out_lasso_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(out_elasticnet_grad_ref, - out_elasticnet_grad, + ASSERT_TRUE(raft::devArrMatch(out_ridge_grad_ref.data(), + out_ridge_grad.data(), + params.n_cols, + raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(raft::devArrMatch(out_elasticnet_grad_ref.data(), + out_elasticnet_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); } @@ -286,32 +269,37 @@ TEST_P(LogRegLossTestF, Result) typedef LogRegLossTest LogRegLossTestD; TEST_P(LogRegLossTestD, Result) { - ASSERT_TRUE(raft::devArrMatch(out_ref, out, 1, raft::CompareApprox(params.tolerance))); - - ASSERT_TRUE( - raft::devArrMatch(out_lasso_ref, out_lasso, 1, raft::CompareApprox(params.tolerance))); - - ASSERT_TRUE( - raft::devArrMatch(out_ridge_ref, out_ridge, 1, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(raft::devArrMatch( + out_ref.data(), out.data(), 1, raft::CompareApprox(params.tolerance))); ASSERT_TRUE(raft::devArrMatch( - out_elasticnet_ref, out_elasticnet, 1, raft::CompareApprox(params.tolerance))); + out_lasso_ref.data(), out_lasso.data(), 1, raft::CompareApprox(params.tolerance))); ASSERT_TRUE(raft::devArrMatch( - out_grad_ref, out_grad, params.n_cols, raft::CompareApprox(params.tolerance))); + out_ridge_ref.data(), out_ridge.data(), 1, raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(raft::devArrMatch(out_elasticnet_ref.data(), + out_elasticnet.data(), + 1, + raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(raft::devArrMatch(out_grad_ref.data(), + out_grad.data(), + params.n_cols, + raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(out_lasso_grad_ref, - out_lasso_grad, + ASSERT_TRUE(raft::devArrMatch(out_lasso_grad_ref.data(), + out_lasso_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(out_ridge_grad_ref, - out_ridge_grad, + ASSERT_TRUE(raft::devArrMatch(out_ridge_grad_ref.data(), + out_ridge_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(raft::devArrMatch(out_elasticnet_grad_ref, - out_elasticnet_grad, + ASSERT_TRUE(raft::devArrMatch(out_elasticnet_grad_ref.data(), + out_elasticnet_grad.data(), params.n_cols, raft::CompareApprox(params.tolerance))); } diff --git a/cpp/test/prims/make_blobs.cu b/cpp/test/prims/make_blobs.cu index eb250e869d..ceb60beaae 100644 --- a/cpp/test/prims/make_blobs.cu +++ b/cpp/test/prims/make_blobs.cu @@ -75,32 +75,43 @@ struct MakeBlobsInputs { template class MakeBlobsTest : public ::testing::TestWithParam> { + public: + MakeBlobsTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + mu_vec(params.cols * params.n_clusters, stream), + mean_var(2 * params.n_clusters * params.cols, stream) + { + } + protected: void SetUp() override { // Tests are configured with their expected test-values sigma. For example, // 4 x sigma indicates the test shouldn't fail 99.9% of the time. num_sigma = 50; - params = ::testing::TestWithParam>::GetParam(); auto len = params.rows * params.cols; - CUDA_CHECK(cudaStreamCreate(&stream)); raft::random::Rng r(params.seed, params.gtype); - raft::allocate(data, len, stream); - raft::allocate(labels, params.rows, stream); - raft::allocate(stats, 2 * params.n_clusters * params.cols, stream, true); - raft::allocate(mean_var, 2 * params.n_clusters * params.cols, stream, true); - raft::allocate(mu_vec, params.cols * params.n_clusters, stream); - raft::allocate(lens, params.n_clusters, stream, true); - r.uniform(mu_vec, params.cols * params.n_clusters, T(-10.0), T(10.0), stream); + + rmm::device_uvector data(len, stream); + rmm::device_uvector labels(params.rows, stream); + rmm::device_uvector stats(2 * params.n_clusters * params.cols, stream); + rmm::device_uvector lens(params.n_clusters, stream); + + CUDA_CHECK(cudaMemsetAsync(stats.data(), 0, stats.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(mean_var.data(), 0, mean_var.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(lens.data(), 0, lens.size() * sizeof(int), stream)); + + r.uniform(mu_vec.data(), params.cols * params.n_clusters, T(-10.0), T(10.0), stream); T* sigma_vec = nullptr; - make_blobs(data, - labels, + make_blobs(data.data(), + labels.data(), params.rows, params.cols, params.n_clusters, stream, params.row_major, - mu_vec, + mu_vec.data(), sigma_vec, params.std, params.shuffle, @@ -109,36 +120,33 @@ class MakeBlobsTest : public ::testing::TestWithParam> { params.seed, params.gtype); static const int threads = 128; - meanKernel<<>>( - stats, lens, data, labels, params.rows, params.cols, params.n_clusters, params.row_major); + meanKernel<<>>(stats.data(), + lens.data(), + data.data(), + labels.data(), + params.rows, + params.cols, + params.n_clusters, + params.row_major); int len1 = params.n_clusters * params.cols; compute_mean_var<<>>( - mean_var, stats, lens, params.n_clusters, params.cols, params.row_major); - } - - void TearDown() override - { - CUDA_CHECK(cudaStreamSynchronize(stream)); - CUDA_CHECK(cudaStreamDestroy(stream)); - CUDA_CHECK(cudaFree(data)); - CUDA_CHECK(cudaFree(labels)); - CUDA_CHECK(cudaFree(stats)); - CUDA_CHECK(cudaFree(mu_vec)); + mean_var.data(), stats.data(), lens.data(), params.n_clusters, params.cols, params.row_major); } void check() { int len = params.n_clusters * params.cols; auto compare = raft::CompareApprox(num_sigma * params.tolerance); - ASSERT_TRUE(raft::devArrMatch(mu_vec, mean_var, len, compare)); - ASSERT_TRUE(raft::devArrMatch(params.std, mean_var + len, len, compare)); + ASSERT_TRUE(raft::devArrMatch(mu_vec.data(), mean_var.data(), len, compare)); + ASSERT_TRUE(raft::devArrMatch(params.std, mean_var.data() + len, len, compare)); } protected: - cudaStream_t stream = 0; MakeBlobsInputs params; - int *labels, *lens; - T *data, *stats, *mu_vec, *mean_var; + raft::handle_t handle; + cudaStream_t stream = 0; + + rmm::device_uvector mu_vec, mean_var; int num_sigma; }; diff --git a/cpp/test/prims/make_regression.cu b/cpp/test/prims/make_regression.cu index c37d5c1716..5a8f11341b 100644 --- a/cpp/test/prims/make_regression.cu +++ b/cpp/test/prims/make_regression.cu @@ -41,32 +41,34 @@ struct MakeRegressionInputs { template class MakeRegressionTest : public ::testing::TestWithParam> { + public: + MakeRegressionTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + values_ret(params.n_samples * params.n_targets, stream), + values_prod(params.n_samples * params.n_targets, stream) + { + } + protected: void SetUp() override { - params = ::testing::TestWithParam>::GetParam(); - // Noise must be zero to compare the actual and expected values T noise = (T)0.0, tail_strength = (T)0.5; - raft::handle_t handle; - stream = handle.get_stream(); - - raft::allocate(data, params.n_samples * params.n_features, stream); - raft::allocate(values_ret, params.n_samples * params.n_targets, stream); - raft::allocate(values_prod, params.n_samples * params.n_targets, stream); - raft::allocate(values_cm, params.n_samples * params.n_targets, stream); - raft::allocate(coef, params.n_features * params.n_targets, stream); + rmm::device_uvector data(params.n_samples * params.n_features, stream); + rmm::device_uvector values_cm(params.n_samples * params.n_targets, stream); + rmm::device_uvector coef(params.n_features * params.n_targets, stream); // Create the regression problem make_regression(handle, - data, - values_ret, + data.data(), + values_ret.data(), params.n_samples, params.n_features, params.n_informative, stream, - coef, + coef.data(), params.n_targets, params.bias, params.effective_rank, @@ -85,41 +87,38 @@ class MakeRegressionTest : public ::testing::TestWithParam __coef = thrust::device_pointer_cast(coef); + thrust::device_ptr __coef = thrust::device_pointer_cast(coef.data()); zero_count = thrust::count(__coef, __coef + params.n_features * params.n_targets, (T)0.0); } - void TearDown() override - { - CUDA_CHECK(cudaFree(data)); - CUDA_CHECK(cudaFree(values_ret)); - CUDA_CHECK(cudaFree(values_prod)); - CUDA_CHECK(cudaFree(values_cm)); - } - protected: + raft::handle_t handle; + cudaStream_t stream = 0; + MakeRegressionInputs params; - T *data, *values_ret, *values_prod, *values_cm, *coef; + rmm::device_uvector values_ret, values_prod; int zero_count; - cudaStream_t stream = 0; }; typedef MakeRegressionTest MakeRegressionTestF; @@ -133,8 +132,8 @@ TEST_P(MakeRegressionTestF, Result) ASSERT_TRUE(match(params.n_targets * (params.n_features - params.n_informative), zero_count, raft::Compare())); - ASSERT_TRUE(devArrMatch(values_ret, - values_prod, + ASSERT_TRUE(devArrMatch(values_ret.data(), + values_prod.data(), params.n_samples, params.n_targets, raft::CompareApprox(params.tolerance), @@ -153,8 +152,8 @@ TEST_P(MakeRegressionTestD, Result) ASSERT_TRUE(match(params.n_targets * (params.n_features - params.n_informative), zero_count, raft::Compare())); - ASSERT_TRUE(devArrMatch(values_ret, - values_prod, + ASSERT_TRUE(devArrMatch(values_ret.data(), + values_prod.data(), params.n_samples, params.n_targets, raft::CompareApprox(params.tolerance), diff --git a/cpp/test/prims/penalty.cu b/cpp/test/prims/penalty.cu index c14c26ee10..c17d41f5a1 100644 --- a/cpp/test/prims/penalty.cu +++ b/cpp/test/prims/penalty.cu @@ -31,86 +31,72 @@ struct PenaltyInputs { template class PenaltyTest : public ::testing::TestWithParam> { + public: + PenaltyTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + in(params.len, stream), + out_lasso(1, stream), + out_ridge(1, stream), + out_elasticnet(1, stream), + out_lasso_grad(params.len, stream), + out_ridge_grad(params.len, stream), + out_elasticnet_grad(params.len, stream), + out_lasso_ref(1, stream), + out_ridge_ref(1, stream), + out_elasticnet_ref(1, stream), + out_lasso_grad_ref(params.len, stream), + out_ridge_grad_ref(params.len, stream), + out_elasticnet_grad_ref(params.len, stream) + { + } + protected: void SetUp() override { - params = ::testing::TestWithParam>::GetParam(); int len = params.len; - cudaStream_t stream = 0; - CUDA_CHECK(cudaStreamCreate(&stream)); - - raft::allocate(in, len, stream); - raft::allocate(out_lasso, 1, stream); - raft::allocate(out_ridge, 1, stream); - raft::allocate(out_elasticnet, 1, stream); - raft::allocate(out_lasso_grad, len, stream); - raft::allocate(out_ridge_grad, len, stream); - raft::allocate(out_elasticnet_grad, len, stream); - raft::allocate(out_lasso_ref, 1, stream); - raft::allocate(out_ridge_ref, 1, stream); - raft::allocate(out_elasticnet_ref, 1, stream); - raft::allocate(out_lasso_grad_ref, len, stream); - raft::allocate(out_ridge_grad_ref, len, stream); - raft::allocate(out_elasticnet_grad_ref, len, stream); - T h_in[len] = {0.1, 0.35, -0.9, -1.4}; - raft::update_device(in, h_in, len, stream); + raft::update_device(in.data(), h_in, len, stream); T h_out_lasso_ref[1] = {1.65}; - raft::update_device(out_lasso_ref, h_out_lasso_ref, 1, stream); + raft::update_device(out_lasso_ref.data(), h_out_lasso_ref, 1, stream); T h_out_ridge_ref[1] = {1.741499}; - raft::update_device(out_ridge_ref, h_out_ridge_ref, 1, stream); + raft::update_device(out_ridge_ref.data(), h_out_ridge_ref, 1, stream); T h_out_elasticnet_ref[1] = {1.695749}; - raft::update_device(out_elasticnet_ref, h_out_elasticnet_ref, 1, stream); + raft::update_device(out_elasticnet_ref.data(), h_out_elasticnet_ref, 1, stream); T h_out_lasso_grad_ref[len] = {0.6, 0.6, -0.6, -0.6}; - raft::update_device(out_lasso_grad_ref, h_out_lasso_grad_ref, len, stream); + raft::update_device(out_lasso_grad_ref.data(), h_out_lasso_grad_ref, len, stream); T h_out_ridge_grad_ref[len] = {0.12, 0.42, -1.08, -1.68}; - raft::update_device(out_ridge_grad_ref, h_out_ridge_grad_ref, len, stream); + raft::update_device(out_ridge_grad_ref.data(), h_out_ridge_grad_ref, len, stream); T h_out_elasticnet_grad_ref[len] = {0.36, 0.51, -0.84, -1.14}; - raft::update_device(out_elasticnet_grad_ref, h_out_elasticnet_grad_ref, len, stream); + raft::update_device(out_elasticnet_grad_ref.data(), h_out_elasticnet_grad_ref, len, stream); T alpha = 0.6; T l1_ratio = 0.5; - lasso(out_lasso, in, len, alpha, stream); - ridge(out_ridge, in, len, alpha, stream); - elasticnet(out_elasticnet, in, len, alpha, l1_ratio, stream); - lassoGrad(out_lasso_grad, in, len, alpha, stream); - ridgeGrad(out_ridge_grad, in, len, alpha, stream); - elasticnetGrad(out_elasticnet_grad, in, len, alpha, l1_ratio, stream); - CUDA_CHECK(cudaStreamDestroy(stream)); - } - - void TearDown() override - { - CUDA_CHECK(cudaFree(in)); - CUDA_CHECK(cudaFree(out_lasso)); - CUDA_CHECK(cudaFree(out_ridge)); - CUDA_CHECK(cudaFree(out_elasticnet)); - CUDA_CHECK(cudaFree(out_lasso_grad)); - CUDA_CHECK(cudaFree(out_ridge_grad)); - CUDA_CHECK(cudaFree(out_elasticnet_grad)); - CUDA_CHECK(cudaFree(out_lasso_ref)); - CUDA_CHECK(cudaFree(out_ridge_ref)); - CUDA_CHECK(cudaFree(out_elasticnet_ref)); - CUDA_CHECK(cudaFree(out_lasso_grad_ref)); - CUDA_CHECK(cudaFree(out_ridge_grad_ref)); - CUDA_CHECK(cudaFree(out_elasticnet_grad_ref)); + lasso(out_lasso.data(), in.data(), len, alpha, stream); + ridge(out_ridge.data(), in.data(), len, alpha, stream); + elasticnet(out_elasticnet.data(), in.data(), len, alpha, l1_ratio, stream); + lassoGrad(out_lasso_grad.data(), in.data(), len, alpha, stream); + ridgeGrad(out_ridge_grad.data(), in.data(), len, alpha, stream); + elasticnetGrad(out_elasticnet_grad.data(), in.data(), len, alpha, l1_ratio, stream); } protected: PenaltyInputs params; - T* in; - T *out_lasso, *out_ridge, *out_elasticnet; - T *out_lasso_ref, *out_ridge_ref, *out_elasticnet_ref; - T *out_lasso_grad, *out_ridge_grad, *out_elasticnet_grad; - T *out_lasso_grad_ref, *out_ridge_grad_ref, *out_elasticnet_grad_ref; + raft::handle_t handle; + cudaStream_t stream; + + rmm::device_uvector in, out_lasso, out_ridge, out_elasticnet; + rmm::device_uvector out_lasso_ref, out_ridge_ref, out_elasticnet_ref; + rmm::device_uvector out_lasso_grad, out_ridge_grad, out_elasticnet_grad; + rmm::device_uvector out_lasso_grad_ref, out_ridge_grad_ref, out_elasticnet_grad_ref; }; const std::vector> inputsf = {{0.01f, 4}}; @@ -120,23 +106,29 @@ const std::vector> inputsd = {{0.01, 4}}; typedef PenaltyTest PenaltyTestF; TEST_P(PenaltyTestF, Result) { - ASSERT_TRUE( - devArrMatch(out_lasso_ref, out_lasso, 1, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(devArrMatch( - out_lasso_grad_ref, out_lasso_grad, params.len, raft::CompareApprox(params.tolerance))); + out_lasso_ref.data(), out_lasso.data(), 1, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE( - devArrMatch(out_ridge_ref, out_ridge, 1, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(devArrMatch(out_lasso_grad_ref.data(), + out_lasso_grad.data(), + params.len, + raft::CompareApprox(params.tolerance))); ASSERT_TRUE(devArrMatch( - out_ridge_grad_ref, out_ridge_grad, params.len, raft::CompareApprox(params.tolerance))); + out_ridge_ref.data(), out_ridge.data(), 1, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(devArrMatch( - out_elasticnet_ref, out_elasticnet, 1, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(devArrMatch(out_ridge_grad_ref.data(), + out_ridge_grad.data(), + params.len, + raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(devArrMatch(out_elasticnet_ref.data(), + out_elasticnet.data(), + 1, + raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(devArrMatch(out_elasticnet_grad_ref, - out_elasticnet_grad, + ASSERT_TRUE(devArrMatch(out_elasticnet_grad_ref.data(), + out_elasticnet_grad.data(), params.len, raft::CompareApprox(params.tolerance))); } @@ -144,23 +136,29 @@ TEST_P(PenaltyTestF, Result) typedef PenaltyTest PenaltyTestD; TEST_P(PenaltyTestD, Result) { - ASSERT_TRUE( - devArrMatch(out_lasso_ref, out_lasso, 1, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(devArrMatch( - out_lasso_grad_ref, out_lasso_grad, params.len, raft::CompareApprox(params.tolerance))); + out_lasso_ref.data(), out_lasso.data(), 1, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE( - devArrMatch(out_ridge_ref, out_ridge, 1, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(devArrMatch(out_lasso_grad_ref.data(), + out_lasso_grad.data(), + params.len, + raft::CompareApprox(params.tolerance))); ASSERT_TRUE(devArrMatch( - out_ridge_grad_ref, out_ridge_grad, params.len, raft::CompareApprox(params.tolerance))); + out_ridge_ref.data(), out_ridge.data(), 1, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(devArrMatch( - out_elasticnet_ref, out_elasticnet, 1, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(devArrMatch(out_ridge_grad_ref.data(), + out_ridge_grad.data(), + params.len, + raft::CompareApprox(params.tolerance))); + + ASSERT_TRUE(devArrMatch(out_elasticnet_ref.data(), + out_elasticnet.data(), + 1, + raft::CompareApprox(params.tolerance))); - ASSERT_TRUE(devArrMatch(out_elasticnet_grad_ref, - out_elasticnet_grad, + ASSERT_TRUE(devArrMatch(out_elasticnet_grad_ref.data(), + out_elasticnet_grad.data(), params.len, raft::CompareApprox(params.tolerance))); } diff --git a/cpp/test/prims/reduce_rows_by_key.cu b/cpp/test/prims/reduce_rows_by_key.cu index 2337aff2b2..2d467d4fdd 100644 --- a/cpp/test/prims/reduce_rows_by_key.cu +++ b/cpp/test/prims/reduce_rows_by_key.cu @@ -85,60 +85,74 @@ template template class ReduceRowTest : public ::testing::TestWithParam> { + public: + ReduceRowTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + in(params.nobs * params.cols, stream), + out(params.nkeys * params.cols, stream), + out_ref(params.nkeys * params.cols, stream), + keys(params.nobs, stream), + scratch_buf(params.nobs, stream) + { + } + protected: void SetUp() override { - params = ::testing::TestWithParam>::GetParam(); raft::random::Rng r(params.seed); raft::random::Rng r_int(params.seed); - CUDA_CHECK(cudaStreamCreate(&stream)); int nobs = params.nobs; uint32_t cols = params.cols; uint32_t nkeys = params.nkeys; - raft::allocate(in, nobs * cols, stream); - raft::allocate(keys, nobs, stream); - raft::allocate(scratch_buf, nobs, stream); - raft::allocate(out_ref, nkeys * cols, stream); - raft::allocate(out, nkeys * cols, stream); - r.uniform(in, nobs * cols, T(0.0), T(2.0 / nobs), stream); - r_int.uniformInt(keys, nobs, (uint32_t)0, nkeys, stream); + r.uniform(in.data(), nobs * cols, T(0.0), T(2.0 / nobs), stream); + r_int.uniformInt(keys.data(), nobs, (uint32_t)0, nkeys, stream); + rmm::device_uvector weight(0, stream); if (params.weighted) { - raft::allocate(weight, nobs, stream); + weight.resize(nobs, stream); raft::random::Rng r(params.seed, raft::random::GeneratorType::GenPhilox); - r.uniform(weight, nobs, T(1), params.max_weight, stream); - } else { - weight = nullptr; + r.uniform(weight.data(), nobs, T(1), params.max_weight, stream); } - naiveReduceRowsByKey(in, cols, keys, weight, scratch_buf, nobs, cols, nkeys, out_ref, stream); + naiveReduceRowsByKey(in.data(), + cols, + keys.data(), + params.weighted ? weight.data() : nullptr, + scratch_buf.data(), + nobs, + cols, + nkeys, + out_ref.data(), + stream); if (params.weighted) { - reduce_rows_by_key(in, cols, keys, weight, scratch_buf, nobs, cols, nkeys, out, stream); + reduce_rows_by_key(in.data(), + cols, + keys.data(), + params.weighted ? weight.data() : nullptr, + scratch_buf.data(), + nobs, + cols, + nkeys, + out.data(), + stream); } else { - reduce_rows_by_key(in, cols, keys, scratch_buf, nobs, cols, nkeys, out, stream); + reduce_rows_by_key( + in.data(), cols, keys.data(), scratch_buf.data(), nobs, cols, nkeys, out.data(), stream); } CUDA_CHECK(cudaStreamSynchronize(stream)); } - void TearDown() override - { - CUDA_CHECK(cudaFree(in)); - CUDA_CHECK(cudaFree(keys)); - CUDA_CHECK(cudaFree(scratch_buf)); - CUDA_CHECK(cudaFree(out_ref)); - CUDA_CHECK(cudaFree(out)); - CUDA_CHECK(cudaStreamDestroy(stream)); - } - protected: - cudaStream_t stream = 0; ReduceRowsInputs params; - T *in, *out_ref, *out; - T* weight; - uint32_t* keys; - char* scratch_buf; + raft::handle_t handle; + cudaStream_t stream = 0; + int device_count = 0; + rmm::device_uvector in, out, out_ref; + rmm::device_uvector keys; + rmm::device_uvector scratch_buf; }; // ReduceRowTestF @@ -149,8 +163,10 @@ const std::vector> inputsf2 = {{0.000001f, 128, 32, 6, 1 typedef ReduceRowTest ReduceRowTestF; TEST_P(ReduceRowTestF, Result) { - ASSERT_TRUE(raft::devArrMatch( - out_ref, out, params.cols * params.nkeys, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(raft::devArrMatch(out_ref.data(), + out.data(), + params.cols * params.nkeys, + raft::CompareApprox(params.tolerance))); } INSTANTIATE_TEST_CASE_P(ReduceRowTests, ReduceRowTestF, ::testing::ValuesIn(inputsf2)); @@ -163,8 +179,10 @@ const std::vector> inputsd2 = { typedef ReduceRowTest ReduceRowTestD; TEST_P(ReduceRowTestD, Result) { - ASSERT_TRUE(raft::devArrMatch( - out_ref, out, params.cols * params.nkeys, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(raft::devArrMatch(out_ref.data(), + out.data(), + params.cols * params.nkeys, + raft::CompareApprox(params.tolerance))); } INSTANTIATE_TEST_CASE_P(ReduceRowTests, ReduceRowTestD, ::testing::ValuesIn(inputsd2)); @@ -177,8 +195,10 @@ const std::vector> inputsf_small_nkey = { typedef ReduceRowTest ReduceRowTestSmallnKey; TEST_P(ReduceRowTestSmallnKey, Result) { - ASSERT_TRUE(raft::devArrMatch( - out_ref, out, params.cols * params.nkeys, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(raft::devArrMatch(out_ref.data(), + out.data(), + params.cols * params.nkeys, + raft::CompareApprox(params.tolerance))); } INSTANTIATE_TEST_CASE_P(ReduceRowTests, ReduceRowTestSmallnKey, @@ -193,8 +213,10 @@ const std::vector> inputsd_big_space = { typedef ReduceRowTest ReduceRowTestBigSpace; TEST_P(ReduceRowTestBigSpace, Result) { - ASSERT_TRUE(raft::devArrMatch( - out_ref, out, params.cols * params.nkeys, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(raft::devArrMatch(out_ref.data(), + out.data(), + params.cols * params.nkeys, + raft::CompareApprox(params.tolerance))); } INSTANTIATE_TEST_CASE_P(ReduceRowTests, ReduceRowTestBigSpace, @@ -209,8 +231,10 @@ const std::vector> inputsf_many_obs = { typedef ReduceRowTest ReduceRowTestManyObs; TEST_P(ReduceRowTestManyObs, Result) { - ASSERT_TRUE(raft::devArrMatch( - out_ref, out, params.cols * params.nkeys, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(raft::devArrMatch(out_ref.data(), + out.data(), + params.cols * params.nkeys, + raft::CompareApprox(params.tolerance))); } INSTANTIATE_TEST_CASE_P(ReduceRowTests, ReduceRowTestManyObs, @@ -225,8 +249,10 @@ const std::vector> inputsf_many_cluster = { typedef ReduceRowTest ReduceRowTestManyClusters; TEST_P(ReduceRowTestManyClusters, Result) { - ASSERT_TRUE(raft::devArrMatch( - out_ref, out, params.cols * params.nkeys, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(raft::devArrMatch(out_ref.data(), + out.data(), + params.cols * params.nkeys, + raft::CompareApprox(params.tolerance))); } INSTANTIATE_TEST_CASE_P(ReduceRowTests, ReduceRowTestManyClusters, diff --git a/cpp/test/prims/score.cu b/cpp/test/prims/score.cu index ed4cffd30a..75523ce19e 100644 --- a/cpp/test/prims/score.cu +++ b/cpp/test/prims/score.cu @@ -20,6 +20,7 @@ #include #include #include +#include #include namespace MLCommon { @@ -120,10 +121,10 @@ class AccuracyTest : public ::testing::TestWithParam { raft::random::Rng r(params.seed); CUDA_CHECK(cudaStreamCreate(&stream)); - raft::allocate(predictions, params.n, stream); - raft::allocate(ref_predictions, params.n, stream); - r.normal(ref_predictions, params.n, (T)0.0, (T)1.0, stream); - raft::copy_async(predictions, ref_predictions, params.n, stream); + rmm::device_uvector predictions(params.n, stream); + rmm::device_uvector ref_predictions(params.n, stream); + r.normal(ref_predictions.data(), params.n, (T)0.0, (T)1.0, stream); + raft::copy_async(predictions.data(), ref_predictions.data(), params.n, stream); CUDA_CHECK(cudaStreamSynchronize(stream)); // Modify params.changed_n unique predictions to a different value. New value is irrelevant. @@ -132,14 +133,14 @@ class AccuracyTest : public ::testing::TestWithParam { int blocks = raft::ceildiv(params.changed_n, threads); //@todo Could also generate params.changed_n unique random positions in [0, n) range, instead // of changing the first ones. - change_vals - <<>>(predictions, ref_predictions, params.changed_n); + change_vals<<>>( + predictions.data(), ref_predictions.data(), params.changed_n); CUDA_CHECK(cudaGetLastError()); CUDA_CHECK(cudaStreamSynchronize(stream)); } - computed_accuracy = - MLCommon::Score::accuracy_score(predictions, ref_predictions, params.n, stream); + computed_accuracy = MLCommon::Score::accuracy_score( + predictions.data(), ref_predictions.data(), params.n, stream); ref_accuracy = (params.n - params.changed_n) * 1.0f / params.n; // std::cout << "computed_accuracy is " << computed_accuracy << " ref_accuracy is " << // ref_accuracy << std::endl; @@ -147,15 +148,12 @@ class AccuracyTest : public ::testing::TestWithParam { void TearDown() override { - CUDA_CHECK(cudaFree(predictions)); - CUDA_CHECK(cudaFree(ref_predictions)); CUDA_CHECK(cudaStreamDestroy(stream)); computed_accuracy = -1.0f; ref_accuracy = -1.0f; } AccuracyInputs params; - T *predictions, *ref_predictions; float computed_accuracy, ref_accuracy; cudaStream_t stream = 0; }; @@ -258,32 +256,36 @@ class RegressionMetricsTest : public ::testing::TestWithParam d_predictions(params.n, stream); + rmm::device_uvector d_ref_predictions(params.n, stream); if (params.hardcoded_preds) { - raft::update_device(d_predictions, params.predictions.data(), params.n, stream); - raft::update_device(d_ref_predictions, params.ref_predictions.data(), params.n, stream); + raft::update_device(d_predictions.data(), params.predictions.data(), params.n, stream); + raft::update_device( + d_ref_predictions.data(), params.ref_predictions.data(), params.n, stream); } else { params.predictions.resize(params.n); params.ref_predictions.resize(params.n); raft::random::Rng r(params.seed); // randomly generate arrays - r.uniform( - d_predictions, params.n, params.predictions_range[0], params.predictions_range[1], stream); - r.uniform(d_ref_predictions, + r.uniform(d_predictions.data(), + params.n, + params.predictions_range[0], + params.predictions_range[1], + stream); + r.uniform(d_ref_predictions.data(), params.n, params.ref_predictions_range[0], params.ref_predictions_range[1], stream); // copy to host to compute reference regression metrics - raft::update_host(params.predictions.data(), d_predictions, params.n, stream); - raft::update_host(params.ref_predictions.data(), d_ref_predictions, params.n, stream); + raft::update_host(params.predictions.data(), d_predictions.data(), params.n, stream); + raft::update_host(params.ref_predictions.data(), d_ref_predictions.data(), params.n, stream); CUDA_CHECK(cudaStreamSynchronize(stream)); } - MLCommon::Score::regression_metrics(d_predictions, - d_ref_predictions, + MLCommon::Score::regression_metrics(d_predictions.data(), + d_ref_predictions.data(), params.n, stream, computed_regression_metrics[0], @@ -295,15 +297,9 @@ class RegressionMetricsTest : public ::testing::TestWithParam params; - T *d_predictions, *d_ref_predictions; std::vector computed_regression_metrics; std::vector ref_regression_metrics; cudaStream_t stream = 0; diff --git a/cpp/test/prims/ternary_op.cu b/cpp/test/prims/ternary_op.cu index e16181437e..3fc8c34963 100644 --- a/cpp/test/prims/ternary_op.cu +++ b/cpp/test/prims/ternary_op.cu @@ -39,49 +39,42 @@ template template class ternaryOpTest : public ::testing::TestWithParam> { public: + ternaryOpTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + out_add_ref(params.len, stream), + out_add(params.len, stream), + out_mul_ref(params.len, stream), + out_mul(params.len, stream) + { + } + void SetUp() override { - params = ::testing::TestWithParam>::GetParam(); raft::random::Rng rng(params.seed); + int len = params.len; + rmm::device_uvector in1(len, stream); + rmm::device_uvector in2(len, stream); + rmm::device_uvector in3(len, stream); - int len = params.len; - cudaStream_t stream = 0; - CUDA_CHECK(cudaStreamCreate(&stream)); - raft::allocate(in1, len, stream); - raft::allocate(in2, len, stream); - raft::allocate(in3, len, stream); - raft::allocate(out_add_ref, len, stream); - raft::allocate(out_mul_ref, len, stream); - raft::allocate(out_add, len, stream); - raft::allocate(out_mul, len, stream); - - rng.fill(out_add_ref, len, T(6.0), stream); - rng.fill(out_mul_ref, len, T(6.0), stream); - rng.fill(in1, len, T(1.0), stream); - rng.fill(in2, len, T(2.0), stream); - rng.fill(in3, len, T(3.0), stream); + rng.fill(out_add_ref.data(), len, T(6.0), stream); + rng.fill(out_mul_ref.data(), len, T(6.0), stream); + rng.fill(in1.data(), len, T(1.0), stream); + rng.fill(in2.data(), len, T(2.0), stream); + rng.fill(in3.data(), len, T(3.0), stream); auto add = [] __device__(T a, T b, T c) { return a + b + c; }; auto mul = [] __device__(T a, T b, T c) { return a * b * c; }; - ternaryOp(out_add, in1, in2, in3, len, add, stream); - ternaryOp(out_mul, in1, in2, in3, len, mul, stream); - CUDA_CHECK(cudaStreamDestroy(stream)); - } - - void TearDown() override - { - CUDA_CHECK(cudaFree(in1)); - CUDA_CHECK(cudaFree(in2)); - CUDA_CHECK(cudaFree(in3)); - CUDA_CHECK(cudaFree(out_mul_ref)); - CUDA_CHECK(cudaFree(out_add_ref)); - CUDA_CHECK(cudaFree(out_add)); - CUDA_CHECK(cudaFree(out_mul)); + ternaryOp(out_add.data(), in1.data(), in2.data(), in3.data(), len, add, stream); + ternaryOp(out_mul.data(), in1.data(), in2.data(), in3.data(), len, mul, stream); } protected: BinaryOpInputs params; - T *in1, *in2, *in3, *out_add_ref, *out_mul_ref, *out_add, *out_mul; + raft::handle_t handle; + cudaStream_t stream = 0; + + rmm::device_uvector out_add_ref, out_add, out_mul_ref, out_mul; }; const std::vector> inputsf = {{0.000001f, 1024 * 1024, 1234ULL}, @@ -90,10 +83,10 @@ const std::vector> inputsf = {{0.000001f, 1024 * 1024, 123 typedef ternaryOpTest ternaryOpTestF; TEST_P(ternaryOpTestF, Result) { - ASSERT_TRUE( - devArrMatch(out_add_ref, out_add, params.len, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE( - devArrMatch(out_mul_ref, out_mul, params.len, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(devArrMatch( + out_add_ref.data(), out_add.data(), params.len, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(devArrMatch( + out_mul_ref.data(), out_mul.data(), params.len, raft::CompareApprox(params.tolerance))); } INSTANTIATE_TEST_CASE_P(ternaryOpTests, ternaryOpTestF, ::testing::ValuesIn(inputsf)); @@ -103,10 +96,10 @@ const std::vector> inputsd = {{0.00000001, 1024 * 1024, 1 typedef ternaryOpTest ternaryOpTestD; TEST_P(ternaryOpTestD, Result) { - ASSERT_TRUE( - devArrMatch(out_add_ref, out_add, params.len, raft::CompareApprox(params.tolerance))); - ASSERT_TRUE( - devArrMatch(out_mul_ref, out_mul, params.len, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(devArrMatch( + out_add_ref.data(), out_add.data(), params.len, raft::CompareApprox(params.tolerance))); + ASSERT_TRUE(devArrMatch( + out_mul_ref.data(), out_mul.data(), params.len, raft::CompareApprox(params.tolerance))); } INSTANTIATE_TEST_CASE_P(ternaryOpTests, ternaryOpTestD, ::testing::ValuesIn(inputsd)); diff --git a/cpp/test/sg/cd_test.cu b/cpp/test/sg/cd_test.cu index 938056764f..452c62f1d7 100644 --- a/cpp/test/sg/cd_test.cu +++ b/cpp/test/sg/cd_test.cu @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -35,42 +36,54 @@ struct CdInputs { template class CdTest : public ::testing::TestWithParam> { + public: + CdTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + data(params.n_row * params.n_col, stream), + labels(params.n_row, stream), + coef(params.n_col, stream), + coef2(params.n_col, stream), + coef3(params.n_col, stream), + coef4(params.n_col, stream), + coef_ref(params.n_col, stream), + coef2_ref(params.n_col, stream), + coef3_ref(params.n_col, stream), + coef4_ref(params.n_col, stream) + { + CUDA_CHECK(cudaMemsetAsync(coef.data(), 0, coef.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(coef2.data(), 0, coef2.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(coef3.data(), 0, coef3.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(coef4.data(), 0, coef4.size() * sizeof(T), stream)); + + CUDA_CHECK(cudaMemsetAsync(coef_ref.data(), 0, coef_ref.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(coef2_ref.data(), 0, coef2_ref.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(coef3_ref.data(), 0, coef3_ref.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(coef4_ref.data(), 0, coef4_ref.size() * sizeof(T), stream)); + } + protected: void lasso() { - auto stream = handle.get_stream(); - - params = ::testing::TestWithParam>::GetParam(); int len = params.n_row * params.n_col; - raft::allocate(data, len, stream); - raft::allocate(labels, params.n_row, stream); - raft::allocate(coef, params.n_col, stream, true); - raft::allocate(coef2, params.n_col, stream, true); - raft::allocate(coef3, params.n_col, stream, true); - raft::allocate(coef4, params.n_col, stream, true); - raft::allocate(coef_ref, params.n_col, stream, true); - raft::allocate(coef2_ref, params.n_col, stream, true); - raft::allocate(coef3_ref, params.n_col, stream, true); - raft::allocate(coef4_ref, params.n_col, stream, true); - T data_h[len] = {1.0, 1.2, 2.0, 2.0, 4.5, 2.0, 2.0, 3.0}; - raft::update_device(data, data_h, len, stream); + raft::update_device(data.data(), data_h, len, stream); T labels_h[params.n_row] = {6.0, 8.3, 9.8, 11.2}; - raft::update_device(labels, labels_h, params.n_row, stream); + raft::update_device(labels.data(), labels_h, params.n_row, stream); T coef_ref_h[params.n_col] = {4.90832, 0.35031}; - raft::update_device(coef_ref, coef_ref_h, params.n_col, stream); + raft::update_device(coef_ref.data(), coef_ref_h, params.n_col, stream); T coef2_ref_h[params.n_col] = {2.53530, -0.36832}; - raft::update_device(coef2_ref, coef2_ref_h, params.n_col, stream); + raft::update_device(coef2_ref.data(), coef2_ref_h, params.n_col, stream); T coef3_ref_h[params.n_col] = {2.932841, 1.15248}; - raft::update_device(coef3_ref, coef3_ref_h, params.n_col, stream); + raft::update_device(coef3_ref.data(), coef3_ref_h, params.n_col, stream); T coef4_ref_h[params.n_col] = {0.569439, -0.00542}; - raft::update_device(coef4_ref, coef4_ref_h, params.n_col, stream); + raft::update_device(coef4_ref.data(), coef4_ref_h, params.n_col, stream); bool fit_intercept = false; bool normalize = false; @@ -83,11 +96,11 @@ class CdTest : public ::testing::TestWithParam> { intercept = T(0); cdFit(handle, - data, + data.data(), params.n_row, params.n_col, - labels, - coef, + labels.data(), + coef.data(), &intercept, fit_intercept, normalize, @@ -102,11 +115,11 @@ class CdTest : public ::testing::TestWithParam> { fit_intercept = true; intercept2 = T(0); cdFit(handle, - data, + data.data(), params.n_row, params.n_col, - labels, - coef2, + labels.data(), + coef2.data(), &intercept2, fit_intercept, normalize, @@ -123,11 +136,11 @@ class CdTest : public ::testing::TestWithParam> { fit_intercept = false; intercept = T(0); cdFit(handle, - data, + data.data(), params.n_row, params.n_col, - labels, - coef3, + labels.data(), + coef3.data(), &intercept, fit_intercept, normalize, @@ -143,11 +156,11 @@ class CdTest : public ::testing::TestWithParam> { normalize = true; intercept2 = T(0); cdFit(handle, - data, + data.data(), params.n_row, params.n_col, - labels, - coef4, + labels.data(), + coef4.data(), &intercept2, fit_intercept, normalize, @@ -162,28 +175,16 @@ class CdTest : public ::testing::TestWithParam> { void SetUp() override { lasso(); } - void TearDown() override - { - CUDA_CHECK(cudaFree(data)); - CUDA_CHECK(cudaFree(labels)); - CUDA_CHECK(cudaFree(coef)); - CUDA_CHECK(cudaFree(coef_ref)); - CUDA_CHECK(cudaFree(coef2)); - CUDA_CHECK(cudaFree(coef2_ref)); - CUDA_CHECK(cudaFree(coef3)); - CUDA_CHECK(cudaFree(coef3_ref)); - CUDA_CHECK(cudaFree(coef4)); - CUDA_CHECK(cudaFree(coef4_ref)); - } - protected: CdInputs params; - T *data, *labels, *coef, *coef_ref; - T *coef2, *coef2_ref; - T *coef3, *coef3_ref; - T *coef4, *coef4_ref; - T intercept, intercept2; raft::handle_t handle; + cudaStream_t stream = 0; + + rmm::device_uvector data, labels, coef, coef_ref; + rmm::device_uvector coef2, coef2_ref; + rmm::device_uvector coef3, coef3_ref; + rmm::device_uvector coef4, coef4_ref; + T intercept, intercept2; }; const std::vector> inputsf2 = {{0.01f, 4, 2}}; @@ -193,35 +194,33 @@ const std::vector> inputsd2 = {{0.01, 4, 2}}; typedef CdTest CdTestF; TEST_P(CdTestF, Fit) { - auto stream = handle.get_stream(); - ASSERT_TRUE(raft::devArrMatch( - coef_ref, coef, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef_ref.data(), coef.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - coef2_ref, coef2, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef2_ref.data(), coef2.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - coef3_ref, coef3, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef3_ref.data(), coef3.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - coef4_ref, coef4, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef4_ref.data(), coef4.data(), params.n_col, raft::CompareApproxAbs(params.tol))); } typedef CdTest CdTestD; TEST_P(CdTestD, Fit) { - ASSERT_TRUE( - raft::devArrMatch(coef_ref, coef, params.n_col, raft::CompareApproxAbs(params.tol))); + ASSERT_TRUE(raft::devArrMatch( + coef_ref.data(), coef.data(), params.n_col, raft::CompareApproxAbs(params.tol))); - ASSERT_TRUE( - raft::devArrMatch(coef2_ref, coef2, params.n_col, raft::CompareApproxAbs(params.tol))); + ASSERT_TRUE(raft::devArrMatch( + coef2_ref.data(), coef2.data(), params.n_col, raft::CompareApproxAbs(params.tol))); - ASSERT_TRUE( - raft::devArrMatch(coef3_ref, coef3, params.n_col, raft::CompareApproxAbs(params.tol))); + ASSERT_TRUE(raft::devArrMatch( + coef3_ref.data(), coef3.data(), params.n_col, raft::CompareApproxAbs(params.tol))); - ASSERT_TRUE( - raft::devArrMatch(coef4_ref, coef4, params.n_col, raft::CompareApproxAbs(params.tol))); + ASSERT_TRUE(raft::devArrMatch( + coef4_ref.data(), coef4.data(), params.n_col, raft::CompareApproxAbs(params.tol))); } INSTANTIATE_TEST_CASE_P(CdTests, CdTestF, ::testing::ValuesIn(inputsf2)); diff --git a/cpp/test/sg/dbscan_test.cu b/cpp/test/sg/dbscan_test.cu index 8d1a2237f6..30bf4c9a7e 100644 --- a/cpp/test/sg/dbscan_test.cu +++ b/cpp/test/sg/dbscan_test.cu @@ -104,12 +104,12 @@ class DbscanTest : public ::testing::TestWithParam> { raft::distance::L2SqrtUnexpanded); } - raft::allocate(labels, params.n_row, stream); - raft::allocate(labels_ref, params.n_row, stream); + rmm::device_uvector labels(params.n_row, stream); + rmm::device_uvector labels_ref(params.n_row, stream); - raft::copy(labels_ref, l.data(), params.n_row, handle.get_stream()); + raft::copy(labels_ref.data(), l.data(), params.n_row, stream); - CUDA_CHECK(cudaStreamSynchronize(handle.get_stream())); + CUDA_CHECK(cudaStreamSynchronize(stream)); Dbscan::fit(handle, params.metric == raft::distance::Precomputed ? dist.data() : out.data(), @@ -118,18 +118,18 @@ class DbscanTest : public ::testing::TestWithParam> { params.eps, params.min_pts, params.metric, - labels, + labels.data(), nullptr, params.max_bytes_per_batch); - CUDA_CHECK(cudaStreamSynchronize(handle.get_stream())); + CUDA_CHECK(cudaStreamSynchronize(stream)); - score = adjusted_rand_index(handle, labels_ref, labels, params.n_row); + score = adjusted_rand_index(handle, labels_ref.data(), labels.data(), params.n_row); if (score < 1.0) { - auto str = raft::arr2Str(labels_ref, params.n_row, "labels_ref", handle.get_stream()); + auto str = raft::arr2Str(labels_ref.data(), params.n_row, "labels_ref", handle.get_stream()); CUML_LOG_DEBUG("y: %s", str.c_str()); - str = raft::arr2Str(labels, params.n_row, "labels", handle.get_stream()); + str = raft::arr2Str(labels.data(), params.n_row, "labels", handle.get_stream()); CUML_LOG_DEBUG("y_hat: %s", str.c_str()); CUML_LOG_DEBUG("Score = %lf", score); } @@ -137,15 +137,8 @@ class DbscanTest : public ::testing::TestWithParam> { void SetUp() override { basicTest(); } - void TearDown() override - { - CUDA_CHECK(cudaFree(labels)); - CUDA_CHECK(cudaFree(labels_ref)); - } - protected: DbscanInputs params; - IdxT *labels, *labels_ref; double score; }; @@ -226,59 +219,48 @@ class Dbscan2DSimple : public ::testing::TestWithParam> { params = ::testing::TestWithParam>::GetParam(); - raft::allocate(inputs, params.n_row * 2, stream); - raft::allocate(labels, params.n_row, stream); - raft::allocate(labels_ref, params.n_out, stream); - raft::allocate(core_sample_indices_d, params.n_row, stream); + rmm::device_uvector inputs(params.n_row * 2, stream); + rmm::device_uvector labels(params.n_row, stream); + rmm::device_uvector labels_ref(params.n_out, stream); + rmm::device_uvector core_sample_indices_d(params.n_row, stream); - raft::copy(inputs, params.points, params.n_row * 2, handle.get_stream()); - raft::copy(labels_ref, params.out, params.n_out, handle.get_stream()); - CUDA_CHECK(cudaStreamSynchronize(handle.get_stream())); + raft::copy(inputs.data(), params.points, params.n_row * 2, stream); + raft::copy(labels_ref.data(), params.out, params.n_out, stream); + CUDA_CHECK(cudaStreamSynchronize(stream)); Dbscan::fit(handle, - inputs, + inputs.data(), (int)params.n_row, 2, params.eps, params.min_pts, raft::distance::L2SqrtUnexpanded, - labels, - core_sample_indices_d); + labels.data(), + core_sample_indices_d.data()); CUDA_CHECK(cudaStreamSynchronize(handle.get_stream())); - score = adjusted_rand_index(handle, labels_ref, labels, (int)params.n_out); + score = adjusted_rand_index(handle, labels_ref.data(), labels.data(), (int)params.n_out); if (score < 1.0) { - auto str = raft::arr2Str(labels_ref, params.n_out, "labels_ref", handle.get_stream()); + auto str = raft::arr2Str(labels_ref.data(), params.n_out, "labels_ref", stream); CUML_LOG_DEBUG("y: %s", str.c_str()); - str = raft::arr2Str(labels, params.n_row, "labels", handle.get_stream()); + str = raft::arr2Str(labels.data(), params.n_row, "labels", stream); CUML_LOG_DEBUG("y_hat: %s", str.c_str()); CUML_LOG_DEBUG("Score = %lf", score); } EXPECT_TRUE(raft::devArrMatchHost(params.core_indices, - core_sample_indices_d, + core_sample_indices_d.data(), params.n_row, raft::Compare(), - handle.get_stream())); + stream)); } void SetUp() override { basicTest(); } - void TearDown() override - { - CUDA_CHECK(cudaFree(labels_ref)); - CUDA_CHECK(cudaFree(labels)); - CUDA_CHECK(cudaFree(inputs)); - CUDA_CHECK(cudaFree(core_sample_indices_d)); - } - protected: DBScan2DArrayInputs params; - int *labels, *labels_ref; - int* core_sample_indices_d; - T* inputs; double score; }; diff --git a/cpp/test/sg/decisiontree_batchedlevel_unittest.cu b/cpp/test/sg/decisiontree_batchedlevel_unittest.cu index 324f7b82f9..0e4c90e67e 100644 --- a/cpp/test/sg/decisiontree_batchedlevel_unittest.cu +++ b/cpp/test/sg/decisiontree_batchedlevel_unittest.cu @@ -104,7 +104,7 @@ class BatchedLevelAlgoUnitTestFixture { // New depth reached by the invocation of nodeSplitKernel() new_depth.resize(1, stream); - raft::allocate(splits, max_batch, stream); + rmm::device_uvector splits(max_batch, stream); raft::update_device(data.data(), h_data.data(), n_row * n_col, stream); raft::update_device(labels.data(), h_labels.data(), n_row, stream); @@ -142,7 +142,6 @@ class BatchedLevelAlgoUnitTestFixture { rmm::device_uvector data, d_quantiles, labels; rmm::device_uvector n_new_nodes, n_new_leaves, new_depth, row_ids; rmm::device_uvector curr_nodes, new_nodes; - SplitT* splits; }; class TestNodeSplitKernel : public ::testing::TestWithParam, @@ -189,11 +188,11 @@ TEST_P(TestNodeSplitKernel, MinSamplesSplitLeaf) CUDA_CHECK(cudaMemsetAsync(n_new_nodes.data(), 0, sizeof(IdxT), stream)); CUDA_CHECK(cudaMemsetAsync(n_new_leaves.data(), 0, sizeof(IdxT), stream)); CUDA_CHECK(cudaMemsetAsync(new_depth.data(), 0, sizeof(IdxT), stream)); - initSplit(splits, batchSize, stream); + initSplit(splits.data(), batchSize, stream); /* { quesval, colid, best_metric_val, nLeft } */ std::vector h_splits{{-1.5f, 0, 0.25f, 1}, {2.0f, 1, 3.555556f, 2}}; - raft::update_device(splits, h_splits.data(), 2, stream); + raft::update_device(splits.data(), h_splits.data(), 2, stream); nodeSplitKernel <<>>(params.max_depth, @@ -205,7 +204,7 @@ TEST_P(TestNodeSplitKernel, MinSamplesSplitLeaf) curr_nodes.data(), new_nodes.data(), n_new_nodes.data(), - splits, + splits.data(), n_new_leaves.data(), h_n_total_nodes, new_depth.data()); @@ -272,7 +271,7 @@ TEST_P(TestMetric, RegressionMetricGain) CUDA_CHECK(cudaMemsetAsync(done_count.data(), 0, sizeof(int) * max_batch * n_col_blks, stream)); CUDA_CHECK(cudaMemsetAsync(hist.data(), 0, 2 * sizeof(DataT) * nPredCounts, stream)); CUDA_CHECK(cudaMemsetAsync(n_new_leaves.data(), 0, sizeof(IdxT), stream)); - initSplit(splits, batchSize, stream); + initSplit(splits.data(), batchSize, stream); std::vector h_splits(1); @@ -302,13 +301,13 @@ TEST_P(TestMetric, RegressionMetricGain) 0, done_count.data(), mutex.data(), - splits, + splits.data(), obj, 0, workload_info.data(), 1234ULL); - raft::update_host(h_splits.data(), splits, 1, stream); + raft::update_host(h_splits.data(), splits.data(), 1, stream); CUDA_CHECK(cudaGetLastError()); CUDA_CHECK(cudaStreamSynchronize(stream)); diff --git a/cpp/test/sg/fil_test.cu b/cpp/test/sg/fil_test.cu index 66aee49b34..8c8db69582 100644 --- a/cpp/test/sg/fil_test.cu +++ b/cpp/test/sg/fil_test.cu @@ -222,12 +222,21 @@ void adjust_threshold_to_treelite( } class BaseFilTest : public testing::TestWithParam { + public: + BaseFilTest() + : ps(::testing::TestWithParam::GetParam()), + stream(handle.get_stream()), + preds_d(0, stream), + want_preds_d(0, stream), + want_proba_d(0, stream), + data_d(ps.num_rows * ps.num_cols, stream), + proba_d(0, stream) + { + } + protected: void setup_helper() { - // setup - ps = testing::TestWithParam::GetParam(); - generate_forest(); generate_data(); predict_on_cpu(); @@ -236,15 +245,6 @@ class BaseFilTest : public testing::TestWithParam { void SetUp() override { setup_helper(); } - void TearDown() override - { - CUDA_CHECK(cudaFree(preds_d)); - CUDA_CHECK(cudaFree(want_preds_d)); - CUDA_CHECK(cudaFree(data_d)); - CUDA_CHECK(cudaFree(want_proba_d)); - CUDA_CHECK(cudaFree(proba_d)); - } - void generate_forest() { auto stream = handle.get_stream(); @@ -253,20 +253,12 @@ class BaseFilTest : public testing::TestWithParam { // helper data /// weights, used as float* or int* - int* weights_d = nullptr; - float* thresholds_d = nullptr; - bool* def_lefts_d = nullptr; - bool* is_leafs_d = nullptr; - bool* def_lefts_h = nullptr; - bool* is_leafs_h = nullptr; + rmm::device_uvector weights_d(num_nodes, stream); + rmm::device_uvector thresholds_d(num_nodes, stream); + rmm::device_uvector def_lefts_d(num_nodes, stream); + rmm::device_uvector is_leafs_d(num_nodes, stream); rmm::device_uvector is_categoricals_d(num_nodes, stream); - // allocate GPU data - raft::allocate(weights_d, num_nodes, stream); - // sizeof(float) == sizeof(int) - raft::allocate(thresholds_d, num_nodes, stream); - raft::allocate(def_lefts_d, num_nodes, stream); - raft::allocate(is_leafs_d, num_nodes, stream); fids_d.resize(num_nodes, stream); fid_num_cats_d.resize(ps.num_cols, stream); @@ -274,7 +266,7 @@ class BaseFilTest : public testing::TestWithParam { raft::random::Rng r(ps.seed); if (ps.leaf_algo == fil::leaf_algo_t::CATEGORICAL_LEAF) { // [0..num_classes) - r.uniformInt((int*)weights_d, num_nodes, 0, ps.num_classes, stream); + r.uniformInt((int*)weights_d.data(), num_nodes, 0, ps.num_classes, stream); } else if (ps.leaf_algo == fil::leaf_algo_t::VECTOR_LEAF) { std::mt19937 gen(3); std::uniform_real_distribution<> dist(0, 1); @@ -290,12 +282,12 @@ class BaseFilTest : public testing::TestWithParam { } } } else { - r.uniform((float*)weights_d, num_nodes, -1.0f, 1.0f, stream); + r.uniform((float*)weights_d.data(), num_nodes, -1.0f, 1.0f, stream); } - r.uniform(thresholds_d, num_nodes, -1.0f, 1.0f, stream); + r.uniform(thresholds_d.data(), num_nodes, -1.0f, 1.0f, stream); r.uniformInt(fids_d.data(), num_nodes, 0, ps.num_cols, stream); - r.bernoulli(def_lefts_d, num_nodes, 0.5f, stream); - r.bernoulli(is_leafs_d, num_nodes, 1.0f - ps.leaf_prob, stream); + r.bernoulli(def_lefts_d.data(), num_nodes, 0.5f, stream); + r.bernoulli(is_leafs_d.data(), num_nodes, 1.0f - ps.leaf_prob, stream); hard_clipped_bernoulli( r, is_categoricals_d.data(), num_nodes, 1.0f - ps.node_categorical_prob, stream); @@ -305,8 +297,8 @@ class BaseFilTest : public testing::TestWithParam { std::vector fid_num_cats_h(ps.num_cols); std::vector feature_categorical(ps.num_cols); // bool vectors are not guaranteed to be stored byte-per-value - def_lefts_h = new bool[num_nodes]; - is_leafs_h = new bool[num_nodes]; + bool* def_lefts_h = new bool[num_nodes]; + bool* is_leafs_h = new bool[num_nodes]; // uniformily distributed in orders of magnitude: smaller models which // still stress large bitfields. @@ -328,11 +320,11 @@ class BaseFilTest : public testing::TestWithParam { cat_sets_h.fid_num_cats[fid] = 0.0f; } } - raft::update_host(weights_h.data(), (int*)weights_d, num_nodes, stream); - raft::update_host(thresholds_h.data(), thresholds_d, num_nodes, stream); + raft::update_host(weights_h.data(), (int*)weights_d.data(), num_nodes, stream); + raft::update_host(thresholds_h.data(), thresholds_d.data(), num_nodes, stream); raft::update_host(fids_h.data(), fids_d.data(), num_nodes, stream); - raft::update_host(def_lefts_h, def_lefts_d, num_nodes, stream); - raft::update_host(is_leafs_h, is_leafs_d, num_nodes, stream); + raft::update_host(def_lefts_h, def_lefts_d.data(), num_nodes, stream); + raft::update_host(is_leafs_h, is_leafs_d.data(), num_nodes, stream); raft::update_host(is_categoricals_h.data(), is_categoricals_d.data(), num_nodes, stream); handle.sync_stream(); @@ -412,10 +404,6 @@ class BaseFilTest : public testing::TestWithParam { // clean up delete[] def_lefts_h; delete[] is_leafs_h; - CUDA_CHECK(cudaFree(is_leafs_d)); - CUDA_CHECK(cudaFree(def_lefts_d)); - CUDA_CHECK(cudaFree(thresholds_d)); - CUDA_CHECK(cudaFree(weights_d)); // cat_sets_h.bits and fid_num_cats_d are now visible to host } @@ -424,32 +412,27 @@ class BaseFilTest : public testing::TestWithParam { auto stream = handle.get_stream(); // allocate arrays size_t num_data = ps.num_rows * ps.num_cols; - raft::allocate(data_d, num_data, stream); - bool* mask_d = nullptr; - raft::allocate(mask_d, num_data, stream); + rmm::device_uvector mask_d(num_data, stream); // generate random data raft::random::Rng r(ps.seed); - r.uniform(data_d, num_data, -1.0f, 1.0f, stream); + r.uniform(data_d.data(), num_data, -1.0f, 1.0f, stream); thrust::transform(thrust::cuda::par.on(stream), - data_d, - data_d + num_data, + data_d.data(), + data_d.data() + num_data, thrust::counting_iterator(0), - data_d, + data_d.data(), replace_some_floating_with_categorical{fid_num_cats_d.data(), ps.num_cols}); - r.bernoulli(mask_d, num_data, ps.nan_prob, stream); + r.bernoulli(mask_d.data(), num_data, ps.nan_prob, stream); int tpb = 256; nan_kernel<<>>( - data_d, mask_d, num_data, std::numeric_limits::quiet_NaN()); + data_d.data(), mask_d.data(), num_data, std::numeric_limits::quiet_NaN()); CUDA_CHECK(cudaPeekAtLastError()); // copy to host data_h.resize(num_data); - raft::update_host(data_h.data(), data_d, num_data, stream); + raft::update_host(data_h.data(), data_d.data(), num_data, stream); handle.sync_stream(); - - // clean up - CUDA_CHECK(cudaFree(mask_d)); } void apply_softmax(float* class_scores) @@ -563,10 +546,10 @@ class BaseFilTest : public testing::TestWithParam { } // copy to GPU - raft::allocate(want_preds_d, ps.num_preds_outputs(), stream); - raft::allocate(want_proba_d, ps.num_proba_outputs(), stream); - raft::update_device(want_preds_d, want_preds_h.data(), ps.num_preds_outputs(), stream); - raft::update_device(want_proba_d, want_proba_h.data(), ps.num_proba_outputs(), stream); + want_preds_d.resize(ps.num_preds_outputs(), stream); + want_proba_d.resize(ps.num_proba_outputs(), stream); + raft::update_device(want_preds_d.data(), want_preds_h.data(), ps.num_preds_outputs(), stream); + raft::update_device(want_proba_d.data(), want_proba_h.data(), ps.num_proba_outputs(), stream); handle.sync_stream(); } @@ -579,10 +562,10 @@ class BaseFilTest : public testing::TestWithParam { init_forest(&forest); // predict - raft::allocate(preds_d, ps.num_preds_outputs(), stream); - raft::allocate(proba_d, ps.num_proba_outputs(), stream); - fil::predict(handle, forest, preds_d, data_d, ps.num_rows); - fil::predict(handle, forest, proba_d, data_d, ps.num_rows, true); + preds_d.resize(ps.num_preds_outputs(), stream); + proba_d.resize(ps.num_proba_outputs(), stream); + fil::predict(handle, forest, preds_d.data(), data_d.data(), ps.num_rows); + fil::predict(handle, forest, proba_d.data(), data_d.data(), ps.num_rows, true); handle.sync_stream(); // cleanup @@ -591,9 +574,8 @@ class BaseFilTest : public testing::TestWithParam { void compare() { - auto stream = handle.get_stream(); - ASSERT_TRUE(raft::devArrMatch(want_proba_d, - proba_d, + ASSERT_TRUE(raft::devArrMatch(want_proba_d.data(), + proba_d.data(), ps.num_proba_outputs(), raft::CompareApprox(ps.tolerance), stream)); @@ -602,8 +584,11 @@ class BaseFilTest : public testing::TestWithParam { : std::numeric_limits::epsilon(); // in multi-class prediction, floats represent the most likely class // and would be generated by converting an int to float - ASSERT_TRUE(raft::devArrMatch( - want_preds_d, preds_d, ps.num_rows, raft::CompareApprox(tolerance), stream)); + ASSERT_TRUE(raft::devArrMatch(want_preds_d.data(), + preds_d.data(), + ps.num_rows, + raft::CompareApprox(tolerance), + stream)); } fil::val_t infer_one_tree(fil::dense_node* root, float* data, const tree_base& tree) @@ -623,14 +608,19 @@ class BaseFilTest : public testing::TestWithParam { int forest_num_nodes() { return tree_num_nodes() * ps.num_trees; } + // parameters + FilTestParams ps; + raft::handle_t handle; + cudaStream_t stream = 0; + // predictions - float* preds_d = nullptr; - float* proba_d = nullptr; - float* want_preds_d = nullptr; - float* want_proba_d = nullptr; + rmm::device_uvector preds_d; + rmm::device_uvector proba_d; + rmm::device_uvector want_preds_d; + rmm::device_uvector want_proba_d; // input data - float* data_d = nullptr; + rmm::device_uvector data_d; std::vector data_h; std::vector want_proba_h; @@ -640,10 +630,6 @@ class BaseFilTest : public testing::TestWithParam { cat_sets_owner cat_sets_h; rmm::device_uvector fids_d = rmm::device_uvector(0, cudaStream_t()); rmm::device_uvector fid_num_cats_d = rmm::device_uvector(0, cudaStream_t()); - - // parameters - raft::handle_t handle; - FilTestParams ps; }; template diff --git a/cpp/test/sg/holtwinters_test.cu b/cpp/test/sg/holtwinters_test.cu index c4f0b49ee3..b17113f23a 100644 --- a/cpp/test/sg/holtwinters_test.cu +++ b/cpp/test/sg/holtwinters_test.cu @@ -22,6 +22,7 @@ #include #include #include +#include #include namespace ML { @@ -43,9 +44,20 @@ struct HoltWintersInputs { template class HoltWintersTest : public ::testing::TestWithParam> { public: + HoltWintersTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + level_ptr(0, stream), + trend_ptr(0, stream), + season_ptr(0, stream), + SSE_error_ptr(0, stream), + forecast_ptr(0, stream), + data(0, stream) + { + } + void basicTest() { - params = ::testing::TestWithParam>::GetParam(); dataset_h = params.dataset_h; test = params.test; n = params.n; @@ -57,8 +69,6 @@ class HoltWintersTest : public ::testing::TestWithParam> { epsilon = params.epsilon; mae_tolerance = params.mae_tolerance; - CUDA_CHECK(cudaStreamCreate(&stream)); - ML::HoltWinters::buffer_size( n, batch_size, @@ -70,14 +80,13 @@ class HoltWintersTest : public ::testing::TestWithParam> { &leveltrend_coef_offset, // = (n-wlen-1)*batch_size (last row) &season_coef_offset); // = (n-wlen-frequency)*batch_size(last freq rows) - raft::allocate(level_ptr, components_len, stream); - raft::allocate(trend_ptr, components_len, stream); - raft::allocate(season_ptr, components_len, stream); - raft::allocate(SSE_error_ptr, batch_size, stream); - raft::allocate(forecast_ptr, batch_size * h, stream); - - raft::allocate(data, batch_size * n, stream); - raft::update_device(data, dataset_h, batch_size * n, stream); + level_ptr.resize(components_len, stream); + trend_ptr.resize(components_len, stream); + season_ptr.resize(components_len, stream); + SSE_error_ptr.resize(batch_size, stream); + forecast_ptr.resize(batch_size * h, stream); + data.resize(batch_size * n, stream); + raft::update_device(data.data(), dataset_h, batch_size * n, stream); raft::handle_t handle{stream}; @@ -88,11 +97,11 @@ class HoltWintersTest : public ::testing::TestWithParam> { start_periods, seasonal, epsilon, - data, - level_ptr, - trend_ptr, - season_ptr, - SSE_error_ptr); + data.data(), + level_ptr.data(), + trend_ptr.data(), + season_ptr.data(), + SSE_error_ptr.data()); ML::HoltWinters::forecast(handle, n, @@ -100,38 +109,29 @@ class HoltWintersTest : public ::testing::TestWithParam> { frequency, h, seasonal, - level_ptr, - trend_ptr, - season_ptr, - forecast_ptr); + level_ptr.data(), + trend_ptr.data(), + season_ptr.data(), + forecast_ptr.data()); CUDA_CHECK(cudaStreamSynchronize(stream)); } void SetUp() override { basicTest(); } - void TearDown() override - { - CUDA_CHECK(cudaFree(data)); - CUDA_CHECK(cudaFree(level_ptr)); - CUDA_CHECK(cudaFree(trend_ptr)); - CUDA_CHECK(cudaFree(season_ptr)); - CUDA_CHECK(cudaFree(SSE_error_ptr)); - CUDA_CHECK(cudaFree(forecast_ptr)); - CUDA_CHECK(cudaStreamDestroy(stream)); - } - public: + raft::handle_t handle; cudaStream_t stream = 0; + HoltWintersInputs params; T *dataset_h, *test; - T* data; + rmm::device_uvector data; int n, h; int leveltrend_seed_len, season_seed_len, components_len; int leveltrend_coef_offset, season_coef_offset; int error_len; int batch_size, frequency, start_periods; - T *SSE_error_ptr, *level_ptr, *trend_ptr, *season_ptr, *forecast_ptr; + rmm::device_uvector SSE_error_ptr, level_ptr, trend_ptr, season_ptr, forecast_ptr; T epsilon, mae_tolerance; }; @@ -250,7 +250,7 @@ typedef HoltWintersTest HoltWintersTestF; TEST_P(HoltWintersTestF, Fit) { std::vector forecast_h(batch_size * h); - raft::update_host(forecast_h.data(), forecast_ptr, batch_size * h, stream); + raft::update_host(forecast_h.data(), forecast_ptr.data(), batch_size * h, stream); raft::print_host_vector("forecast", forecast_h.data(), batch_size * h, std::cout); float mae = calculate_MAE(test, forecast_h.data(), batch_size, h); CUML_LOG_DEBUG("MAE: %f", mae); @@ -261,7 +261,7 @@ typedef HoltWintersTest HoltWintersTestD; TEST_P(HoltWintersTestD, Fit) { std::vector forecast_h(batch_size * h); - raft::update_host(forecast_h.data(), forecast_ptr, batch_size * h, stream); + raft::update_host(forecast_h.data(), forecast_ptr.data(), batch_size * h, stream); raft::print_host_vector("forecast", forecast_h.data(), batch_size * h, std::cout); double mae = calculate_MAE(test, forecast_h.data(), batch_size, h); CUML_LOG_DEBUG("MAE: %f", mae); diff --git a/cpp/test/sg/knn_test.cu b/cpp/test/sg/knn_test.cu index 8ccf849bd0..28117ed57f 100644 --- a/cpp/test/sg/knn_test.cu +++ b/cpp/test/sg/knn_test.cu @@ -124,15 +124,37 @@ __global__ void build_expected_output(int* output, int n_rows, int k, const int* template class KNNTest : public ::testing::TestWithParam { + public: + KNNTest() + : params(::testing::TestWithParam::GetParam()), + stream(handle.get_stream()), + index_data(params.n_rows * params.n_cols * params.n_parts, stream), + index_labels(params.n_rows * params.n_parts, stream), + search_data(params.n_query_row * params.n_cols, stream), + search_labels(params.n_query_row, stream), + output_indices(params.n_query_row * params.n_neighbors * params.n_parts, stream), + output_dists(params.n_query_row * params.n_neighbors * params.n_parts, stream) + + { + CUDA_CHECK(cudaMemsetAsync(index_data.data(), 0, index_data.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(index_labels.data(), 0, index_labels.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(search_data.data(), 0, search_data.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(search_labels.data(), 0, search_labels.size() * sizeof(T), stream)); + CUDA_CHECK( + cudaMemsetAsync(output_indices.data(), 0, output_indices.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(output_dists.data(), 0, output_dists.size() * sizeof(T), stream)); + } + protected: void testBruteForce() { - cudaStream_t stream = handle.get_stream(); - - raft::allocate( - actual_labels, params.n_query_row * params.n_neighbors * params.n_parts, stream, true); - raft::allocate( - expected_labels, params.n_query_row * params.n_neighbors * params.n_parts, stream, true); + rmm::device_uvector actual_labels(params.n_query_row * params.n_neighbors * params.n_parts, + stream); + rmm::device_uvector expected_labels( + params.n_query_row * params.n_neighbors * params.n_parts, stream); + CUDA_CHECK(cudaMemsetAsync(actual_labels.data(), 0, actual_labels.size() * sizeof(T), stream)); + CUDA_CHECK( + cudaMemsetAsync(expected_labels.data(), 0, expected_labels.size() * sizeof(T), stream)); create_data(); @@ -140,10 +162,10 @@ class KNNTest : public ::testing::TestWithParam { part_inputs, part_sizes, params.n_cols, - search_data, + search_data.data(), params.n_query_row, - output_indices, - output_dists, + output_indices.data(), + output_dists.data(), params.n_neighbors, true, true); @@ -151,24 +173,28 @@ class KNNTest : public ::testing::TestWithParam { build_actual_output<<>>( - actual_labels, params.n_query_row, params.n_neighbors, index_labels, output_indices); + stream>>>(actual_labels.data(), + params.n_query_row, + params.n_neighbors, + index_labels.data(), + output_indices.data()); build_expected_output<<>>( - expected_labels, params.n_query_row, params.n_neighbors, search_labels); + expected_labels.data(), params.n_query_row, params.n_neighbors, search_labels.data()); - ASSERT_TRUE(devArrMatch(expected_labels, - actual_labels, + ASSERT_TRUE(devArrMatch(expected_labels.data(), + actual_labels.data(), params.n_query_row * params.n_neighbors, raft::Compare())); } void testClassification() { - cudaStream_t stream = handle.get_stream(); - - raft::allocate(actual_labels, params.n_query_row, stream, true); - raft::allocate(expected_labels, params.n_query_row, stream, true); + rmm::device_uvector actual_labels(params.n_query_row, stream); + rmm::device_uvector expected_labels(params.n_query_row, stream); + CUDA_CHECK(cudaMemsetAsync(actual_labels.data(), 0, actual_labels.size() * sizeof(T), stream)); + CUDA_CHECK( + cudaMemsetAsync(expected_labels.data(), 0, expected_labels.size() * sizeof(T), stream)); create_data(); @@ -176,35 +202,36 @@ class KNNTest : public ::testing::TestWithParam { part_inputs, part_sizes, params.n_cols, - search_data, + search_data.data(), params.n_query_row, - output_indices, - output_dists, + output_indices.data(), + output_dists.data(), params.n_neighbors, true, true); vector full_labels(1); - full_labels[0] = index_labels; + full_labels[0] = index_labels.data(); knn_classify(handle, - actual_labels, - output_indices, + actual_labels.data(), + output_indices.data(), full_labels, params.n_rows * params.n_parts, params.n_query_row, params.n_neighbors); - ASSERT_TRUE( - devArrMatch(search_labels, actual_labels, params.n_query_row, raft::Compare())); + ASSERT_TRUE(devArrMatch( + search_labels.data(), actual_labels.data(), params.n_query_row, raft::Compare())); } void testRegression() { - cudaStream_t stream = handle.get_stream(); - - raft::allocate(actual_labels, params.n_query_row, stream, true); - raft::allocate(expected_labels, params.n_query_row, stream, true); + rmm::device_uvector actual_labels(params.n_query_row, stream); + rmm::device_uvector expected_labels(params.n_query_row, stream); + CUDA_CHECK(cudaMemsetAsync(actual_labels.data(), 0, actual_labels.size() * sizeof(T), stream)); + CUDA_CHECK( + cudaMemsetAsync(expected_labels.data(), 0, expected_labels.size() * sizeof(T), stream)); create_data(); @@ -212,10 +239,10 @@ class KNNTest : public ::testing::TestWithParam { part_inputs, part_sizes, params.n_cols, - search_data, + search_data.data(), params.n_query_row, - output_indices, - output_dists, + output_indices.data(), + output_dists.data(), params.n_neighbors, true, true); @@ -223,9 +250,9 @@ class KNNTest : public ::testing::TestWithParam { rmm::device_uvector index_labels_float(params.n_rows * params.n_parts, stream); rmm::device_uvector query_labels_float(params.n_query_row, stream); to_float<<>>( - index_labels_float.data(), index_labels, index_labels_float.size()); + index_labels_float.data(), index_labels.data(), index_labels_float.size()); to_float<<>>( - query_labels_float.data(), search_labels, params.n_query_row); + query_labels_float.data(), search_labels.data(), params.n_query_row); CUDA_CHECK(cudaStreamSynchronize(stream)); CUDA_CHECK(cudaPeekAtLastError()); @@ -236,7 +263,7 @@ class KNNTest : public ::testing::TestWithParam { knn_regress(handle, actual_labels_float.data(), - output_indices, + output_indices.data(), full_labels, params.n_rows, params.n_query_row, @@ -248,36 +275,6 @@ class KNNTest : public ::testing::TestWithParam { raft::Compare())); } - void SetUp() override - { - cudaStream_t stream = handle.get_stream(); - - params = ::testing::TestWithParam::GetParam(); - - raft::allocate(index_data, params.n_rows * params.n_cols * params.n_parts, stream, true); - raft::allocate(index_labels, params.n_rows * params.n_parts, stream, true); - - raft::allocate(search_data, params.n_query_row * params.n_cols, stream, true); - raft::allocate(search_labels, params.n_query_row, stream, true); - - raft::allocate( - output_indices, params.n_query_row * params.n_neighbors * params.n_parts, stream, true); - raft::allocate( - output_dists, params.n_query_row * params.n_neighbors * params.n_parts, stream, true); - } - - void TearDown() override - { - CUDA_CHECK(cudaFree(index_data)); - CUDA_CHECK(cudaFree(index_labels)); - CUDA_CHECK(cudaFree(search_data)); - CUDA_CHECK(cudaFree(search_labels)); - CUDA_CHECK(cudaFree(output_dists)); - CUDA_CHECK(cudaFree(output_indices)); - CUDA_CHECK(cudaFree(actual_labels)); - CUDA_CHECK(cudaFree(expected_labels)); - } - private: void create_data() { @@ -289,8 +286,8 @@ class KNNTest : public ::testing::TestWithParam { // Create index parts create_index_parts(handle, - index_data, - index_labels, + index_data.data(), + index_labels.data(), part_inputs, part_labels, part_sizes, @@ -298,8 +295,8 @@ class KNNTest : public ::testing::TestWithParam { rand_centers.data()); gen_blobs(handle, - search_data, - search_labels, + search_data.data(), + search_labels.data(), params.n_query_row, params.n_cols, params.n_centers, @@ -307,24 +304,22 @@ class KNNTest : public ::testing::TestWithParam { } raft::handle_t handle; + cudaStream_t stream = 0; KNNInputs params; - float* index_data; - int* index_labels; + rmm::device_uvector index_data; + rmm::device_uvector index_labels; vector part_inputs; vector part_labels; vector part_sizes; - float* search_data; - int* search_labels; - - float* output_dists; - int64_t* output_indices; + rmm::device_uvector search_data; + rmm::device_uvector search_labels; - int* actual_labels; - int* expected_labels; + rmm::device_uvector output_dists; + rmm::device_uvector output_indices; }; const std::vector inputs = {{50, 5, 2, 25, 5, 2}, diff --git a/cpp/test/sg/ols.cu b/cpp/test/sg/ols.cu index 4eaca9cac3..6998f500f0 100644 --- a/cpp/test/sg/ols.cu +++ b/cpp/test/sg/ols.cu @@ -38,186 +38,194 @@ struct OlsInputs { template class OlsTest : public ::testing::TestWithParam> { + public: + OlsTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + coef(params.n_col, stream), + coef2(params.n_col, stream), + coef3(params.n_col, stream), + coef_ref(params.n_col, stream), + coef2_ref(params.n_col, stream), + coef3_ref(params.n_col, stream), + pred(params.n_row_2, stream), + pred_ref(params.n_row_2, stream), + pred2(params.n_row_2, stream), + pred2_ref(params.n_row_2, stream), + pred3(params.n_row_2, stream), + pred3_ref(params.n_row_2, stream), + coef_sc(1, stream), + coef_sc_ref(1, stream) + { + basicTest(); + basicTest2(); + } + protected: void basicTest() { - auto stream = handle.get_stream(); - params = ::testing::TestWithParam>::GetParam(); - int len = params.n_row * params.n_col; - int len2 = params.n_row_2 * params.n_col; - - raft::allocate(data, len, stream); - raft::allocate(labels, params.n_row, stream); - raft::allocate(coef, params.n_col, stream); - raft::allocate(coef2, params.n_col, stream); - raft::allocate(coef3, params.n_col, stream); - raft::allocate(coef_ref, params.n_col, stream); - raft::allocate(coef2_ref, params.n_col, stream); - raft::allocate(coef3_ref, params.n_col, stream); - raft::allocate(pred_data, len2, stream); - raft::allocate(pred, params.n_row_2, stream); - raft::allocate(pred_ref, params.n_row_2, stream); - raft::allocate(pred2, params.n_row_2, stream); - raft::allocate(pred2_ref, params.n_row_2, stream); - raft::allocate(pred3, params.n_row_2, stream); - raft::allocate(pred3_ref, params.n_row_2, stream); + int len = params.n_row * params.n_col; + int len2 = params.n_row_2 * params.n_col; + + rmm::device_uvector data(len, stream); + rmm::device_uvector labels(params.n_row, stream); + rmm::device_uvector pred_data(len2, stream); std::vector data_h = {1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0, 3.0}; data_h.resize(len); - raft::update_device(data, data_h.data(), len, stream); + raft::update_device(data.data(), data_h.data(), len, stream); std::vector labels_h = {6.0, 8.0, 9.0, 11.0}; labels_h.resize(params.n_row); - raft::update_device(labels, labels_h.data(), params.n_row, stream); + raft::update_device(labels.data(), labels_h.data(), params.n_row, stream); std::vector coef_ref_h = {2.090908, 2.5454557}; coef_ref_h.resize(params.n_col); - raft::update_device(coef_ref, coef_ref_h.data(), params.n_col, stream); + raft::update_device(coef_ref.data(), coef_ref_h.data(), params.n_col, stream); std::vector coef2_ref_h = {1.000001, 1.9999998}; coef2_ref_h.resize(params.n_col); - raft::update_device(coef2_ref, coef2_ref_h.data(), params.n_col, stream); + raft::update_device(coef2_ref.data(), coef2_ref_h.data(), params.n_col, stream); std::vector coef3_ref_h = {0.99999, 2.00000}; coef3_ref_h.resize(params.n_col); - raft::update_device(coef3_ref, coef3_ref_h.data(), params.n_col, stream); + raft::update_device(coef3_ref.data(), coef3_ref_h.data(), params.n_col, stream); std::vector pred_data_h = {3.0, 2.0, 5.0, 5.0}; pred_data_h.resize(len2); - raft::update_device(pred_data, pred_data_h.data(), len2, stream); + raft::update_device(pred_data.data(), pred_data_h.data(), len2, stream); std::vector pred_ref_h = {19.0, 16.9090}; pred_ref_h.resize(params.n_row_2); - raft::update_device(pred_ref, pred_ref_h.data(), params.n_row_2, stream); + raft::update_device(pred_ref.data(), pred_ref_h.data(), params.n_row_2, stream); std::vector pred2_ref_h = {16.0, 15.0}; pred2_ref_h.resize(params.n_row_2); - raft::update_device(pred2_ref, pred2_ref_h.data(), params.n_row_2, stream); + raft::update_device(pred2_ref.data(), pred2_ref_h.data(), params.n_row_2, stream); std::vector pred3_ref_h = {16.0, 15.0}; pred3_ref_h.resize(params.n_row_2); - raft::update_device(pred3_ref, pred3_ref_h.data(), params.n_row_2, stream); + raft::update_device(pred3_ref.data(), pred3_ref_h.data(), params.n_row_2, stream); intercept = T(0); olsFit(handle, - data, + data.data(), params.n_row, params.n_col, - labels, - coef, + labels.data(), + coef.data(), &intercept, false, false, stream, params.algo); - gemmPredict(handle, pred_data, params.n_row_2, params.n_col, coef, intercept, pred, stream); + gemmPredict(handle, + pred_data.data(), + params.n_row_2, + params.n_col, + coef.data(), + intercept, + pred.data(), + stream); - raft::update_device(data, data_h.data(), len, stream); - raft::update_device(labels, labels_h.data(), params.n_row, stream); + raft::update_device(data.data(), data_h.data(), len, stream); + raft::update_device(labels.data(), labels_h.data(), params.n_row, stream); intercept2 = T(0); olsFit(handle, - data, + data.data(), params.n_row, params.n_col, - labels, - coef2, + labels.data(), + coef2.data(), &intercept2, true, false, stream, params.algo); - gemmPredict(handle, pred_data, params.n_row_2, params.n_col, coef2, intercept2, pred2, stream); + gemmPredict(handle, + pred_data.data(), + params.n_row_2, + params.n_col, + coef2.data(), + intercept2, + pred2.data(), + stream); - raft::update_device(data, data_h.data(), len, stream); - raft::update_device(labels, labels_h.data(), params.n_row, stream); + raft::update_device(data.data(), data_h.data(), len, stream); + raft::update_device(labels.data(), labels_h.data(), params.n_row, stream); intercept3 = T(0); olsFit(handle, - data, + data.data(), params.n_row, params.n_col, - labels, - coef3, + labels.data(), + coef3.data(), &intercept3, true, true, stream, params.algo); - gemmPredict(handle, pred_data, params.n_row_2, params.n_col, coef3, intercept3, pred3, stream); + gemmPredict(handle, + pred_data.data(), + params.n_row_2, + params.n_col, + coef3.data(), + intercept3, + pred3.data(), + stream); } void basicTest2() { - auto stream = handle.get_stream(); - params = ::testing::TestWithParam>::GetParam(); - int len = params.n_row * params.n_col; + int len = params.n_row * params.n_col; - raft::allocate(data_sc, len, stream); - raft::allocate(labels_sc, len, stream); - raft::allocate(coef_sc, 1, stream); - raft::allocate(coef_sc_ref, 1, stream); + rmm::device_uvector data_sc(len, stream); + rmm::device_uvector labels_sc(len, stream); std::vector data_h = {1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0, 3.0}; data_h.resize(len); - raft::update_device(data_sc, data_h.data(), len, stream); + raft::update_device(data_sc.data(), data_h.data(), len, stream); std::vector labels_h = {6.0, 8.0, 9.0, 11.0, -1.0, 2.0, -3.6, 3.3}; labels_h.resize(len); - raft::update_device(labels_sc, labels_h.data(), len, stream); + raft::update_device(labels_sc.data(), labels_h.data(), len, stream); std::vector coef_sc_ref_h = {-0.29285714}; coef_sc_ref_h.resize(1); - raft::update_device(coef_sc_ref, coef_sc_ref_h.data(), 1, stream); + raft::update_device(coef_sc_ref.data(), coef_sc_ref_h.data(), 1, stream); T intercept_sc = T(0); - olsFit( - handle, data_sc, len, 1, labels_sc, coef_sc, &intercept_sc, true, false, stream, params.algo); - } - - void SetUp() override - { - basicTest(); - basicTest2(); - } - - void TearDown() override - { - CUDA_CHECK(cudaFree(data)); - CUDA_CHECK(cudaFree(labels)); - CUDA_CHECK(cudaFree(coef)); - CUDA_CHECK(cudaFree(coef_ref)); - CUDA_CHECK(cudaFree(coef2)); - CUDA_CHECK(cudaFree(coef2_ref)); - CUDA_CHECK(cudaFree(coef3)); - CUDA_CHECK(cudaFree(coef3_ref)); - CUDA_CHECK(cudaFree(pred_data)); - CUDA_CHECK(cudaFree(pred)); - CUDA_CHECK(cudaFree(pred_ref)); - CUDA_CHECK(cudaFree(pred2)); - CUDA_CHECK(cudaFree(pred2_ref)); - CUDA_CHECK(cudaFree(pred3)); - CUDA_CHECK(cudaFree(pred3_ref)); - - CUDA_CHECK(cudaFree(data_sc)); - CUDA_CHECK(cudaFree(labels_sc)); - CUDA_CHECK(cudaFree(coef_sc)); - CUDA_CHECK(cudaFree(coef_sc_ref)); + olsFit(handle, + data_sc.data(), + len, + 1, + labels_sc.data(), + coef_sc.data(), + &intercept_sc, + true, + false, + stream, + params.algo); } protected: + raft::handle_t handle; + cudaStream_t stream = 0; + OlsInputs params; - T *data, *labels, *coef, *coef_ref, *pred_data, *pred, *pred_ref; - T *coef2, *coef2_ref, *pred2, *pred2_ref; - T *coef3, *coef3_ref, *pred3, *pred3_ref; - T *data_sc, *labels_sc, *coef_sc, *coef_sc_ref; + rmm::device_uvector coef, coef_ref, pred, pred_ref; + rmm::device_uvector coef2, coef2_ref, pred2, pred2_ref; + rmm::device_uvector coef3, coef3_ref, pred3, pred3_ref; + rmm::device_uvector coef_sc, coef_sc_ref; + T *data, *labels, *data_sc, *labels_sc; T intercept, intercept2, intercept3; - raft::handle_t handle; }; const std::vector> inputsf2 = { @@ -229,53 +237,51 @@ const std::vector> inputsd2 = { typedef OlsTest OlsTestF; TEST_P(OlsTestF, Fit) { - auto stream = handle.get_stream(); - ASSERT_TRUE( - devArrMatch(coef_ref, coef, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + ASSERT_TRUE(devArrMatch( + coef_ref.data(), coef.data(), params.n_col, raft::CompareApproxAbs(params.tol))); - ASSERT_TRUE( - devArrMatch(coef2_ref, coef2, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + ASSERT_TRUE(devArrMatch( + coef2_ref.data(), coef2.data(), params.n_col, raft::CompareApproxAbs(params.tol))); - ASSERT_TRUE( - devArrMatch(coef3_ref, coef3, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + ASSERT_TRUE(devArrMatch( + coef3_ref.data(), coef3.data(), params.n_col, raft::CompareApproxAbs(params.tol))); - ASSERT_TRUE( - devArrMatch(pred_ref, pred, params.n_row_2, raft::CompareApproxAbs(params.tol), stream)); + ASSERT_TRUE(devArrMatch( + pred_ref.data(), pred.data(), params.n_row_2, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(devArrMatch( - pred2_ref, pred2, params.n_row_2, raft::CompareApproxAbs(params.tol), stream)); + pred2_ref.data(), pred2.data(), params.n_row_2, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(devArrMatch( - pred3_ref, pred3, params.n_row_2, raft::CompareApproxAbs(params.tol), stream)); + pred3_ref.data(), pred3.data(), params.n_row_2, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE( - devArrMatch(coef_sc_ref, coef_sc, 1, raft::CompareApproxAbs(params.tol), stream)); + devArrMatch(coef_sc_ref.data(), coef_sc.data(), 1, raft::CompareApproxAbs(params.tol))); } typedef OlsTest OlsTestD; TEST_P(OlsTestD, Fit) { - auto stream = handle.get_stream(); ASSERT_TRUE(raft::devArrMatch( - coef_ref, coef, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef_ref.data(), coef.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - coef2_ref, coef2, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef2_ref.data(), coef2.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - coef3_ref, coef3, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef3_ref.data(), coef3.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - pred_ref, pred, params.n_row_2, raft::CompareApproxAbs(params.tol), stream)); + pred_ref.data(), pred.data(), params.n_row_2, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(devArrMatch( - pred2_ref, pred2, params.n_row_2, raft::CompareApproxAbs(params.tol), stream)); + pred2_ref.data(), pred2.data(), params.n_row_2, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - pred3_ref, pred3, params.n_row_2, raft::CompareApproxAbs(params.tol), stream)); + pred3_ref.data(), pred3.data(), params.n_row_2, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE( - devArrMatch(coef_sc_ref, coef_sc, 1, raft::CompareApproxAbs(params.tol), stream)); + devArrMatch(coef_sc_ref.data(), coef_sc.data(), 1, raft::CompareApproxAbs(params.tol))); } INSTANTIATE_TEST_CASE_P(OlsTests, OlsTestF, ::testing::ValuesIn(inputsf2)); diff --git a/cpp/test/sg/pca_test.cu b/cpp/test/sg/pca_test.cu index b2ebb8af0b..d164ab6d3a 100644 --- a/cpp/test/sg/pca_test.cu +++ b/cpp/test/sg/pca_test.cu @@ -49,45 +49,53 @@ template template class PcaTest : public ::testing::TestWithParam> { + public: + PcaTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + explained_vars(params.n_col, stream), + explained_vars_ref(params.n_col, stream), + components(params.n_col * params.n_col, stream), + components_ref(params.n_col * params.n_col, stream), + trans_data(params.len, stream), + trans_data_ref(params.len, stream), + data(params.len, stream), + data_back(params.len, stream), + data2(params.len2, stream), + data2_back(params.len2, stream) + { + basicTest(); + advancedTest(); + } + protected: void basicTest() { - auto stream = handle.get_stream(); - params = ::testing::TestWithParam>::GetParam(); raft::random::Rng r(params.seed, raft::random::GenTaps); int len = params.len; - raft::allocate(data, len, stream); - raft::allocate(data_back, len, stream); - raft::allocate(trans_data, len, stream); - raft::allocate(trans_data_ref, len, stream); - std::vector data_h = {1.0, 2.0, 5.0, 4.0, 2.0, 1.0}; data_h.resize(len); - raft::update_device(data, data_h.data(), len, stream); + raft::update_device(data.data(), data_h.data(), len, stream); std::vector trans_data_ref_h = {-2.3231, -0.3517, 2.6748, -0.3979, 0.6571, -0.2592}; trans_data_ref_h.resize(len); - raft::update_device(trans_data_ref, trans_data_ref_h.data(), len, stream); + raft::update_device(trans_data_ref.data(), trans_data_ref_h.data(), len, stream); int len_comp = params.n_col * params.n_col; - raft::allocate(components, len_comp, stream); - raft::allocate(explained_vars, params.n_col, stream); - raft::allocate(explained_var_ratio, params.n_col, stream); - raft::allocate(singular_vals, params.n_col, stream); - raft::allocate(mean, params.n_col, stream); - raft::allocate(noise_vars, 1, stream); + rmm::device_uvector explained_var_ratio(params.n_col, stream); + rmm::device_uvector singular_vals(params.n_col, stream); + rmm::device_uvector mean(params.n_col, stream); + rmm::device_uvector noise_vars(1, stream); std::vector components_ref_h = {0.8163, 0.5776, -0.5776, 0.8163}; components_ref_h.resize(len_comp); std::vector explained_vars_ref_h = {6.338, 0.3287}; explained_vars_ref_h.resize(params.n_col); - raft::allocate(components_ref, len_comp, stream); - raft::allocate(explained_vars_ref, params.n_col, stream); - - raft::update_device(components_ref, components_ref_h.data(), len_comp, stream); - raft::update_device(explained_vars_ref, explained_vars_ref_h.data(), params.n_col, stream); + raft::update_device(components_ref.data(), components_ref_h.data(), len_comp, stream); + raft::update_device( + explained_vars_ref.data(), explained_vars_ref_h.data(), params.n_col, stream); paramsPCA prms; prms.n_cols = params.n_col; @@ -100,24 +108,35 @@ class PcaTest : public ::testing::TestWithParam> { prms.algorithm = solver::COV_EIG_JACOBI; pcaFit(handle, - data, - components, - explained_vars, - explained_var_ratio, - singular_vals, - mean, - noise_vars, + data.data(), + components.data(), + explained_vars.data(), + explained_var_ratio.data(), + singular_vals.data(), + mean.data(), + noise_vars.data(), prms, stream); - pcaTransform(handle, data, components, trans_data, singular_vals, mean, prms, stream); - pcaInverseTransform( - handle, trans_data, components, singular_vals, mean, data_back, prms, stream); + pcaTransform(handle, + data.data(), + components.data(), + trans_data.data(), + singular_vals.data(), + mean.data(), + prms, + stream); + pcaInverseTransform(handle, + trans_data.data(), + components.data(), + singular_vals.data(), + mean.data(), + data_back.data(), + prms, + stream); } void advancedTest() { - auto stream = handle.get_stream(); - params = ::testing::TestWithParam>::GetParam(); raft::random::Rng r(params.seed, raft::random::GenTaps); int len = params.len2; @@ -131,74 +150,47 @@ class PcaTest : public ::testing::TestWithParam> { else if (params.algo == 1) prms.algorithm = solver::COV_EIG_JACOBI; - raft::allocate(data2, len, stream); - r.uniform(data2, len, T(-1.0), T(1.0), stream); - raft::allocate(data2_trans, prms.n_rows * prms.n_components, stream); + r.uniform(data2.data(), len, T(-1.0), T(1.0), stream); + rmm::device_uvector data2_trans(prms.n_rows * prms.n_components, stream); int len_comp = params.n_col2 * prms.n_components; - raft::allocate(components2, len_comp, stream); - raft::allocate(explained_vars2, prms.n_components, stream); - raft::allocate(explained_var_ratio2, prms.n_components, stream); - raft::allocate(singular_vals2, prms.n_components, stream); - raft::allocate(mean2, prms.n_cols, stream); - raft::allocate(noise_vars2, 1, stream); + rmm::device_uvector components2(len_comp, stream); + rmm::device_uvector explained_vars2(prms.n_components, stream); + rmm::device_uvector explained_var_ratio2(prms.n_components, stream); + rmm::device_uvector singular_vals2(prms.n_components, stream); + rmm::device_uvector mean2(prms.n_cols, stream); + rmm::device_uvector noise_vars2(1, stream); pcaFitTransform(handle, - data2, - data2_trans, - components2, - explained_vars2, - explained_var_ratio2, - singular_vals2, - mean2, - noise_vars2, + data2.data(), + data2_trans.data(), + components2.data(), + explained_vars2.data(), + explained_var_ratio2.data(), + singular_vals2.data(), + mean2.data(), + noise_vars2.data(), prms, stream); - raft::allocate(data2_back, len, stream); - pcaInverseTransform( - handle, data2_trans, components2, singular_vals2, mean2, data2_back, prms, stream); - } - - void SetUp() override - { - basicTest(); - advancedTest(); - } - - void TearDown() override - { - CUDA_CHECK(cudaFree(data)); - CUDA_CHECK(cudaFree(components)); - CUDA_CHECK(cudaFree(trans_data)); - CUDA_CHECK(cudaFree(data_back)); - CUDA_CHECK(cudaFree(trans_data_ref)); - CUDA_CHECK(cudaFree(explained_vars)); - CUDA_CHECK(cudaFree(explained_var_ratio)); - CUDA_CHECK(cudaFree(singular_vals)); - CUDA_CHECK(cudaFree(mean)); - CUDA_CHECK(cudaFree(noise_vars)); - CUDA_CHECK(cudaFree(components_ref)); - CUDA_CHECK(cudaFree(explained_vars_ref)); - CUDA_CHECK(cudaFree(data2)); - CUDA_CHECK(cudaFree(data2_trans)); - CUDA_CHECK(cudaFree(data2_back)); - CUDA_CHECK(cudaFree(components2)); - CUDA_CHECK(cudaFree(explained_vars2)); - CUDA_CHECK(cudaFree(explained_var_ratio2)); - CUDA_CHECK(cudaFree(singular_vals2)); - CUDA_CHECK(cudaFree(mean2)); - CUDA_CHECK(cudaFree(noise_vars2)); + pcaInverseTransform(handle, + data2_trans.data(), + components2.data(), + singular_vals2.data(), + mean2.data(), + data2_back.data(), + prms, + stream); } protected: + raft::handle_t handle; + cudaStream_t stream = 0; + PcaInputs params; - T *data, *trans_data, *data_back, *components, *explained_vars, *explained_var_ratio, - *singular_vals, *mean, *noise_vars, *trans_data_ref, *components_ref, *explained_vars_ref; - T *data2, *data2_trans, *data2_back, *components2, *explained_vars2, *explained_var_ratio2, - *singular_vals2, *mean2, *noise_vars2; - raft::handle_t handle; + rmm::device_uvector explained_vars, explained_vars_ref, components, components_ref, trans_data, + trans_data_ref, data, data_back, data2, data2_back; }; const std::vector> inputsf2 = { @@ -212,8 +204,8 @@ const std::vector> inputsd2 = { typedef PcaTest PcaTestValF; TEST_P(PcaTestValF, Result) { - ASSERT_TRUE(devArrMatch(explained_vars, - explained_vars_ref, + ASSERT_TRUE(devArrMatch(explained_vars.data(), + explained_vars_ref.data(), params.n_col, raft::CompareApproxAbs(params.tolerance), handle.get_stream())); @@ -222,8 +214,8 @@ TEST_P(PcaTestValF, Result) typedef PcaTest PcaTestValD; TEST_P(PcaTestValD, Result) { - ASSERT_TRUE(devArrMatch(explained_vars, - explained_vars_ref, + ASSERT_TRUE(devArrMatch(explained_vars.data(), + explained_vars_ref.data(), params.n_col, raft::CompareApproxAbs(params.tolerance), handle.get_stream())); @@ -232,8 +224,8 @@ TEST_P(PcaTestValD, Result) typedef PcaTest PcaTestLeftVecF; TEST_P(PcaTestLeftVecF, Result) { - ASSERT_TRUE(devArrMatch(components, - components_ref, + ASSERT_TRUE(devArrMatch(components.data(), + components_ref.data(), (params.n_col * params.n_col), raft::CompareApproxAbs(params.tolerance), handle.get_stream())); @@ -242,8 +234,8 @@ TEST_P(PcaTestLeftVecF, Result) typedef PcaTest PcaTestLeftVecD; TEST_P(PcaTestLeftVecD, Result) { - ASSERT_TRUE(devArrMatch(components, - components_ref, + ASSERT_TRUE(devArrMatch(components.data(), + components_ref.data(), (params.n_col * params.n_col), raft::CompareApproxAbs(params.tolerance), handle.get_stream())); @@ -252,8 +244,8 @@ TEST_P(PcaTestLeftVecD, Result) typedef PcaTest PcaTestTransDataF; TEST_P(PcaTestTransDataF, Result) { - ASSERT_TRUE(devArrMatch(trans_data, - trans_data_ref, + ASSERT_TRUE(devArrMatch(trans_data.data(), + trans_data_ref.data(), (params.n_row * params.n_col), raft::CompareApproxAbs(params.tolerance), handle.get_stream())); @@ -262,8 +254,8 @@ TEST_P(PcaTestTransDataF, Result) typedef PcaTest PcaTestTransDataD; TEST_P(PcaTestTransDataD, Result) { - ASSERT_TRUE(devArrMatch(trans_data, - trans_data_ref, + ASSERT_TRUE(devArrMatch(trans_data.data(), + trans_data_ref.data(), (params.n_row * params.n_col), raft::CompareApproxAbs(params.tolerance), handle.get_stream())); @@ -272,8 +264,8 @@ TEST_P(PcaTestTransDataD, Result) typedef PcaTest PcaTestDataVecSmallF; TEST_P(PcaTestDataVecSmallF, Result) { - ASSERT_TRUE(devArrMatch(data, - data_back, + ASSERT_TRUE(devArrMatch(data.data(), + data_back.data(), (params.n_col * params.n_col), raft::CompareApproxAbs(params.tolerance), handle.get_stream())); @@ -282,8 +274,8 @@ TEST_P(PcaTestDataVecSmallF, Result) typedef PcaTest PcaTestDataVecSmallD; TEST_P(PcaTestDataVecSmallD, Result) { - ASSERT_TRUE(devArrMatch(data, - data_back, + ASSERT_TRUE(devArrMatch(data.data(), + data_back.data(), (params.n_col * params.n_col), raft::CompareApproxAbs(params.tolerance), handle.get_stream())); @@ -294,8 +286,8 @@ TEST_P(PcaTestDataVecSmallD, Result) typedef PcaTest PcaTestDataVecF; TEST_P(PcaTestDataVecF, Result) { - ASSERT_TRUE(devArrMatch(data2, - data2_back, + ASSERT_TRUE(devArrMatch(data2.data(), + data2_back.data(), (params.n_col2 * params.n_col2), raft::CompareApproxAbs(params.tolerance), handle.get_stream())); @@ -304,8 +296,8 @@ TEST_P(PcaTestDataVecF, Result) typedef PcaTest PcaTestDataVecD; TEST_P(PcaTestDataVecD, Result) { - ASSERT_TRUE(raft::devArrMatch(data2, - data2_back, + ASSERT_TRUE(raft::devArrMatch(data2.data(), + data2_back.data(), (params.n_col2 * params.n_col2), raft::CompareApproxAbs(params.tolerance), handle.get_stream())); diff --git a/cpp/test/sg/ridge.cu b/cpp/test/sg/ridge.cu index 73896dbd95..75fc76f32d 100644 --- a/cpp/test/sg/ridge.cu +++ b/cpp/test/sg/ridge.cu @@ -38,151 +38,177 @@ struct RidgeInputs { template class RidgeTest : public ::testing::TestWithParam> { + public: + RidgeTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + coef(params.n_col, stream), + coef2(params.n_col, stream), + coef3(params.n_col, stream), + coef_ref(params.n_col, stream), + coef2_ref(params.n_col, stream), + coef3_ref(params.n_col, stream), + pred(params.n_row_2, stream), + pred_ref(params.n_row_2, stream), + pred2(params.n_row_2, stream), + pred2_ref(params.n_row_2, stream), + pred3(params.n_row_2, stream), + pred3_ref(params.n_row_2, stream), + coef_sc(1, stream), + coef_sc_ref(1, stream) + { + basicTest(); + basicTest2(); + } + protected: void basicTest() { - auto stream = handle.get_stream(); - params = ::testing::TestWithParam>::GetParam(); - int len = params.n_row * params.n_col; - int len2 = params.n_row_2 * params.n_col; - - raft::allocate(data, len, stream); - raft::allocate(labels, params.n_row, stream); - raft::allocate(coef, params.n_col, stream); - raft::allocate(coef2, params.n_col, stream); - raft::allocate(coef3, params.n_col, stream); - raft::allocate(coef_ref, params.n_col, stream); - raft::allocate(coef2_ref, params.n_col, stream); - raft::allocate(coef3_ref, params.n_col, stream); - raft::allocate(pred_data, len2, stream); - raft::allocate(pred, params.n_row_2, stream); - raft::allocate(pred_ref, params.n_row_2, stream); - raft::allocate(pred2, params.n_row_2, stream); - raft::allocate(pred2_ref, params.n_row_2, stream); - raft::allocate(pred3, params.n_row_2, stream); - raft::allocate(pred3_ref, params.n_row_2, stream); + int len = params.n_row * params.n_col; + int len2 = params.n_row_2 * params.n_col; + + rmm::device_uvector data(len, stream); + rmm::device_uvector pred_data(len2, stream); + rmm::device_uvector labels(params.n_row, stream); T alpha = params.alpha; T data_h[len] = {0.0, 0.0, 1.0, 0.0, 0.0, 1.0}; - raft::update_device(data, data_h, len, stream); + raft::update_device(data.data(), data_h, len, stream); T labels_h[params.n_row] = {0.0, 0.1, 1.0}; - raft::update_device(labels, labels_h, params.n_row, stream); + raft::update_device(labels.data(), labels_h, params.n_row, stream); T coef_ref_h[params.n_col] = {0.39999998, 0.4}; - raft::update_device(coef_ref, coef_ref_h, params.n_col, stream); + raft::update_device(coef_ref.data(), coef_ref_h, params.n_col, stream); T coef2_ref_h[params.n_col] = {0.3454546, 0.34545454}; - raft::update_device(coef2_ref, coef2_ref_h, params.n_col, stream); + raft::update_device(coef2_ref.data(), coef2_ref_h, params.n_col, stream); T coef3_ref_h[params.n_col] = {0.3799999, 0.38000008}; - raft::update_device(coef3_ref, coef3_ref_h, params.n_col, stream); + raft::update_device(coef3_ref.data(), coef3_ref_h, params.n_col, stream); T pred_data_h[len2] = {0.5, 2.0, 0.2, 1.0}; - raft::update_device(pred_data, pred_data_h, len2, stream); + raft::update_device(pred_data.data(), pred_data_h, len2, stream); T pred_ref_h[params.n_row_2] = {0.28, 1.1999999}; - raft::update_device(pred_ref, pred_ref_h, params.n_row_2, stream); + raft::update_device(pred_ref.data(), pred_ref_h, params.n_row_2, stream); T pred2_ref_h[params.n_row_2] = {0.37818184, 1.1727273}; - raft::update_device(pred2_ref, pred2_ref_h, params.n_row_2, stream); + raft::update_device(pred2_ref.data(), pred2_ref_h, params.n_row_2, stream); T pred3_ref_h[params.n_row_2] = {0.37933332, 1.2533332}; - raft::update_device(pred3_ref, pred3_ref_h, params.n_row_2, stream); + raft::update_device(pred3_ref.data(), pred3_ref_h, params.n_row_2, stream); intercept = T(0); ridgeFit(handle, - data, + data.data(), params.n_row, params.n_col, - labels, + labels.data(), &alpha, 1, - coef, + coef.data(), &intercept, false, false, stream, params.algo); - gemmPredict(handle, pred_data, params.n_row_2, params.n_col, coef, intercept, pred, stream); + gemmPredict(handle, + pred_data.data(), + params.n_row_2, + params.n_col, + coef.data(), + intercept, + pred.data(), + stream); - raft::update_device(data, data_h, len, stream); - raft::update_device(labels, labels_h, params.n_row, stream); + raft::update_device(data.data(), data_h, len, stream); + raft::update_device(labels.data(), labels_h, params.n_row, stream); intercept2 = T(0); ridgeFit(handle, - data, + data.data(), params.n_row, params.n_col, - labels, + labels.data(), &alpha, 1, - coef2, + coef2.data(), &intercept2, true, false, stream, params.algo); - gemmPredict(handle, pred_data, params.n_row_2, params.n_col, coef2, intercept2, pred2, stream); + gemmPredict(handle, + pred_data.data(), + params.n_row_2, + params.n_col, + coef2.data(), + intercept2, + pred2.data(), + stream); - raft::update_device(data, data_h, len, stream); - raft::update_device(labels, labels_h, params.n_row, stream); + raft::update_device(data.data(), data_h, len, stream); + raft::update_device(labels.data(), labels_h, params.n_row, stream); intercept3 = T(0); ridgeFit(handle, - data, + data.data(), params.n_row, params.n_col, - labels, + labels.data(), &alpha, 1, - coef3, + coef3.data(), &intercept3, true, true, stream, params.algo); - gemmPredict(handle, pred_data, params.n_row_2, params.n_col, coef3, intercept3, pred3, stream); + gemmPredict(handle, + pred_data.data(), + params.n_row_2, + params.n_col, + coef3.data(), + intercept3, + pred3.data(), + stream); } void basicTest2() { - auto stream = handle.get_stream(); - params = ::testing::TestWithParam>::GetParam(); - int len = params.n_row * params.n_col; + int len = params.n_row * params.n_col; - raft::allocate(data_sc, len, stream); - raft::allocate(labels_sc, len, stream); - raft::allocate(coef_sc, 1, stream); - raft::allocate(coef_sc_ref, 1, stream); + rmm::device_uvector data_sc(len, stream); + rmm::device_uvector labels_sc(len, stream); std::vector data_h = {1.0, 1.0, 2.0, 2.0, 1.0, 2.0}; data_h.resize(len); - raft::update_device(data_sc, data_h.data(), len, stream); + raft::update_device(data_sc.data(), data_h.data(), len, stream); std::vector labels_h = {6.0, 8.0, 9.0, 11.0, -1.0, 2.0}; labels_h.resize(len); - raft::update_device(labels_sc, labels_h.data(), len, stream); + raft::update_device(labels_sc.data(), labels_h.data(), len, stream); std::vector coef_sc_ref_h = {1.8}; coef_sc_ref_h.resize(1); - raft::update_device(coef_sc_ref, coef_sc_ref_h.data(), 1, stream); + raft::update_device(coef_sc_ref.data(), coef_sc_ref_h.data(), 1, stream); T intercept_sc = T(0); T alpha_sc = T(1.0); ridgeFit(handle, - data_sc, + data_sc.data(), len, 1, - labels_sc, + labels_sc.data(), &alpha_sc, 1, - coef_sc, + coef_sc.data(), &intercept_sc, true, false, @@ -190,44 +216,16 @@ class RidgeTest : public ::testing::TestWithParam> { params.algo); } - void SetUp() override - { - basicTest(); - basicTest2(); - } - - void TearDown() override - { - CUDA_CHECK(cudaFree(data)); - CUDA_CHECK(cudaFree(labels)); - CUDA_CHECK(cudaFree(coef)); - CUDA_CHECK(cudaFree(coef_ref)); - CUDA_CHECK(cudaFree(coef2)); - CUDA_CHECK(cudaFree(coef2_ref)); - CUDA_CHECK(cudaFree(coef3)); - CUDA_CHECK(cudaFree(coef3_ref)); - CUDA_CHECK(cudaFree(pred_data)); - CUDA_CHECK(cudaFree(pred)); - CUDA_CHECK(cudaFree(pred_ref)); - CUDA_CHECK(cudaFree(pred2)); - CUDA_CHECK(cudaFree(pred2_ref)); - CUDA_CHECK(cudaFree(pred3)); - CUDA_CHECK(cudaFree(pred3_ref)); - - CUDA_CHECK(cudaFree(data_sc)); - CUDA_CHECK(cudaFree(labels_sc)); - CUDA_CHECK(cudaFree(coef_sc)); - CUDA_CHECK(cudaFree(coef_sc_ref)); - } - protected: + raft::handle_t handle; + cudaStream_t stream = 0; + RidgeInputs params; - T *data, *labels, *coef, *coef_ref, *pred_data, *pred, *pred_ref; - T *coef2, *coef2_ref, *pred2, *pred2_ref; - T *coef3, *coef3_ref, *pred3, *pred3_ref; - T *data_sc, *labels_sc, *coef_sc, *coef_sc_ref; + rmm::device_uvector coef, coef_ref, pred, pred_ref; + rmm::device_uvector coef2, coef2_ref, pred2, pred2_ref; + rmm::device_uvector coef3, coef3_ref, pred3, pred3_ref; + rmm::device_uvector coef_sc, coef_sc_ref; T intercept, intercept2, intercept3; - raft::handle_t handle; }; const std::vector> inputsf2 = {{0.001f, 3, 2, 2, 0, 0.5f}, @@ -239,53 +237,51 @@ const std::vector> inputsd2 = {{0.001, 3, 2, 2, 0, 0.5}, typedef RidgeTest RidgeTestF; TEST_P(RidgeTestF, Fit) { - auto stream = handle.get_stream(); ASSERT_TRUE(raft::devArrMatch( - coef_ref, coef, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef_ref.data(), coef.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - coef2_ref, coef2, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef2_ref.data(), coef2.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - coef3_ref, coef3, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef3_ref.data(), coef3.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - pred_ref, pred, params.n_row_2, raft::CompareApproxAbs(params.tol), stream)); + pred_ref.data(), pred.data(), params.n_row_2, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - pred2_ref, pred2, params.n_row_2, raft::CompareApproxAbs(params.tol), stream)); + pred2_ref.data(), pred2.data(), params.n_row_2, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - pred3_ref, pred3, params.n_row_2, raft::CompareApproxAbs(params.tol), stream)); + pred3_ref.data(), pred3.data(), params.n_row_2, raft::CompareApproxAbs(params.tol))); - ASSERT_TRUE( - raft::devArrMatch(coef_sc_ref, coef_sc, 1, raft::CompareApproxAbs(params.tol), stream)); + ASSERT_TRUE(raft::devArrMatch( + coef_sc_ref.data(), coef_sc.data(), 1, raft::CompareApproxAbs(params.tol))); } typedef RidgeTest RidgeTestD; TEST_P(RidgeTestD, Fit) { - auto stream = handle.get_stream(); ASSERT_TRUE(raft::devArrMatch( - coef_ref, coef, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef_ref.data(), coef.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - coef2_ref, coef2, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef2_ref.data(), coef2.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - coef3_ref, coef3, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef3_ref.data(), coef3.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - pred_ref, pred, params.n_row_2, raft::CompareApproxAbs(params.tol), stream)); + pred_ref.data(), pred.data(), params.n_row_2, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - pred2_ref, pred2, params.n_row_2, raft::CompareApproxAbs(params.tol), stream)); + pred2_ref.data(), pred2.data(), params.n_row_2, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - pred3_ref, pred3, params.n_row_2, raft::CompareApproxAbs(params.tol), stream)); + pred3_ref.data(), pred3.data(), params.n_row_2, raft::CompareApproxAbs(params.tol))); - ASSERT_TRUE( - raft::devArrMatch(coef_sc_ref, coef_sc, 1, raft::CompareApproxAbs(params.tol), stream)); + ASSERT_TRUE(raft::devArrMatch( + coef_sc_ref.data(), coef_sc.data(), 1, raft::CompareApproxAbs(params.tol))); } INSTANTIATE_TEST_CASE_P(RidgeTests, RidgeTestF, ::testing::ValuesIn(inputsf2)); diff --git a/cpp/test/sg/rproj_test.cu b/cpp/test/sg/rproj_test.cu index 5559203645..8a7882854b 100644 --- a/cpp/test/sg/rproj_test.cu +++ b/cpp/test/sg/rproj_test.cu @@ -30,19 +30,18 @@ namespace ML { template class RPROJTest : public ::testing::Test { - protected: - T* transpose(T* in, int n_rows, int n_cols) + public: + RPROJTest() + : stream(handle.get_stream()), + random_matrix1(stream), + random_matrix2(stream), + d_input(0, stream), + d_output1(0, stream), + d_output2(0, stream) { - cudaStream_t stream = h.get_stream(); - cublasHandle_t cublas_handle = h.get_cublas_handle(); - T* result; - raft::allocate(result, n_rows * n_cols, stream); - raft::linalg::transpose(h, in, result, n_rows, n_cols, stream); - CUDA_CHECK(cudaPeekAtLastError()); - CUDA_CHECK(cudaFree(in)); - return result; } + protected: void generate_data() { std::random_device rd; @@ -53,16 +52,15 @@ class RPROJTest : public ::testing::Test { for (auto& i : h_input) { i = dist(rng); } - raft::allocate(d_input, h_input.size(), h.get_stream()); - raft::update_device(d_input, h_input.data(), h_input.size(), h.get_stream()); - // d_input = transpose(d_input, N, M); + d_input.resize(h_input.size(), stream); + raft::update_device(d_input.data(), h_input.data(), h_input.size(), stream); + // transpose(d_input, d_input, N, M); // From row major to column major (this operation is only useful for non-random datasets) } void gaussianTest() { - params1 = new paramsRPROJ(); - *params1 = { + params1 = { N, // number of samples M, // number of features -1, // number of components @@ -73,18 +71,25 @@ class RPROJTest : public ::testing::Test { 42 // random seed }; - cudaStream_t stream = h.get_stream(); - random_matrix1 = std::make_unique>(stream); - RPROJfit(h, random_matrix1.get(), params1); - raft::allocate(d_output1, N * params1->n_components, stream); - RPROJtransform(h, d_input, random_matrix1.get(), d_output1, params1); - d_output1 = transpose(d_output1, N, params1->n_components); // From column major to row major + RPROJfit(handle, &random_matrix1, ¶ms1); + + d_output1.resize(N * params1.n_components, stream); + rmm::device_uvector tmp(d_output1.size(), stream); + RPROJtransform(handle, d_input.data(), &random_matrix1, tmp.data(), ¶ms1); + + raft::linalg::transpose(handle, + tmp.data(), + d_output1.data(), + N, + params1.n_components, + stream); // From column major to row major + + CUDA_CHECK(cudaStreamSynchronize(stream)); } void sparseTest() { - params2 = new paramsRPROJ(); - *params2 = { + params2 = { N, // number of samples M, // number of features -1, // number of components (-1: auto-deduction) @@ -95,15 +100,20 @@ class RPROJTest : public ::testing::Test { 42 // random seed }; - cudaStream_t stream = h.get_stream(); - random_matrix2 = std::make_unique>(stream); - RPROJfit(h, random_matrix2.get(), params2); + RPROJfit(handle, &random_matrix2, ¶ms2); - raft::allocate(d_output2, N * params2->n_components, stream); + d_output2.resize(N * params2.n_components, stream); + rmm::device_uvector tmp(d_output2.size(), stream); + RPROJtransform(handle, d_input.data(), &random_matrix2, tmp.data(), ¶ms2); - RPROJtransform(h, d_input, random_matrix2.get(), d_output2, params2); + raft::linalg::transpose(handle, + tmp.data(), + d_output2.data(), + N, + params2.n_components, + stream); // From column major to row major - d_output2 = transpose(d_output2, N, params2->n_components); // From column major to row major + CUDA_CHECK(cudaStreamSynchronize(stream)); } void SetUp() override @@ -114,65 +124,50 @@ class RPROJTest : public ::testing::Test { sparseTest(); } - void TearDown() override - { - CUDA_CHECK(cudaFree(d_input)); - CUDA_CHECK(cudaFree(d_output1)); - CUDA_CHECK(cudaFree(d_output2)); - delete params1; - delete params2; - } - void random_matrix_check() { int D = johnson_lindenstrauss_min_dim(N, epsilon); - ASSERT_TRUE(params1->n_components == D); - ASSERT_TRUE(random_matrix1->dense_data.size() > 0); - ASSERT_TRUE(random_matrix1->type == dense); + ASSERT_TRUE(params1.n_components == D); + ASSERT_TRUE(random_matrix1.dense_data.size() > 0); + ASSERT_TRUE(random_matrix1.type == dense); - ASSERT_TRUE(params2->n_components == D); - ASSERT_TRUE(params2->density == 1 / sqrt(M)); - ASSERT_TRUE(random_matrix2->indices.size() > 0); - ASSERT_TRUE(random_matrix2->indptr.size() > 0); - ASSERT_TRUE(random_matrix2->sparse_data.size() > 0); - ASSERT_TRUE(random_matrix2->type == sparse); + ASSERT_TRUE(params2.n_components == D); + ASSERT_TRUE(params2.density == 1 / sqrt(M)); + ASSERT_TRUE(random_matrix2.indices.size() > 0); + ASSERT_TRUE(random_matrix2.indptr.size() > 0); + ASSERT_TRUE(random_matrix2.sparse_data.size() > 0); + ASSERT_TRUE(random_matrix2.type == sparse); } void epsilon_check() { - int D = johnson_lindenstrauss_min_dim(N, epsilon); - + int D = johnson_lindenstrauss_min_dim(N, epsilon); constexpr auto distance_type = raft::distance::DistanceType::L2SqrtUnexpanded; - cudaStream_t stream = h.get_stream(); - - T* d_pdist; - raft::allocate(d_pdist, N * N, stream); - ML::Metrics::pairwise_distance(h, d_input, d_input, d_pdist, N, N, M, distance_type); + rmm::device_uvector d_pdist(N * N, stream); + ML::Metrics::pairwise_distance( + handle, d_input.data(), d_input.data(), d_pdist.data(), N, N, M, distance_type); CUDA_CHECK(cudaPeekAtLastError()); T* h_pdist = new T[N * N]; - raft::update_host(h_pdist, d_pdist, N * N, stream); - CUDA_CHECK(cudaFree(d_pdist)); + raft::update_host(h_pdist, d_pdist.data(), N * N, stream); - T* d_pdist1; - raft::allocate(d_pdist1, N * N, stream); - ML::Metrics::pairwise_distance(h, d_output1, d_output1, d_pdist1, N, N, D, distance_type); + rmm::device_uvector d_pdist1(N * N, stream); + ML::Metrics::pairwise_distance( + handle, d_output1.data(), d_output1.data(), d_pdist1.data(), N, N, D, distance_type); CUDA_CHECK(cudaPeekAtLastError()); T* h_pdist1 = new T[N * N]; - raft::update_host(h_pdist1, d_pdist1, N * N, stream); - CUDA_CHECK(cudaFree(d_pdist1)); + raft::update_host(h_pdist1, d_pdist1.data(), N * N, stream); - T* d_pdist2; - raft::allocate(d_pdist2, N * N, stream); - ML::Metrics::pairwise_distance(h, d_output2, d_output2, d_pdist2, N, N, D, distance_type); + rmm::device_uvector d_pdist2(N * N, stream); + ML::Metrics::pairwise_distance( + handle, d_output2.data(), d_output2.data(), d_pdist2.data(), N, N, D, distance_type); CUDA_CHECK(cudaPeekAtLastError()); T* h_pdist2 = new T[N * N]; - raft::update_host(h_pdist2, d_pdist2, N * N, stream); - CUDA_CHECK(cudaFree(d_pdist2)); + raft::update_host(h_pdist2, d_pdist2.data(), N * N, stream); for (size_t i = 0; i < N; i++) { for (size_t j = 0; j <= i; j++) { @@ -194,19 +189,21 @@ class RPROJTest : public ::testing::Test { } protected: - raft::handle_t h; - paramsRPROJ* params1; + raft::handle_t handle; + cudaStream_t stream = 0; + T epsilon; std::vector h_input; - T* d_input; + rmm::device_uvector d_input; - std::unique_ptr> random_matrix1; - T* d_output1; + paramsRPROJ params1; + rand_mat random_matrix1; + rmm::device_uvector d_output1; - paramsRPROJ* params2; - std::unique_ptr> random_matrix2; - T* d_output2; + paramsRPROJ params2; + rand_mat random_matrix2; + rmm::device_uvector d_output2; }; typedef RPROJTest RPROJTestF1; diff --git a/cpp/test/sg/sgd.cu b/cpp/test/sg/sgd.cu index 98fc093d23..0795452c3c 100644 --- a/cpp/test/sg/sgd.cu +++ b/cpp/test/sg/sgd.cu @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -38,31 +39,44 @@ struct SgdInputs { template class SgdTest : public ::testing::TestWithParam> { + public: + SgdTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + coef(params.n_col, stream), + coef_ref(params.n_col, stream), + coef2(params.n_col, stream), + coef2_ref(params.n_col, stream), + pred_log(0, stream), + pred_log_ref(0, stream), + pred_svm(0, stream), + pred_svm_ref(0, stream) + { + CUDA_CHECK(cudaMemsetAsync(coef.data(), 0, coef.size() * sizeof(T), stream)); + CUDA_CHECK(cudaMemsetAsync(coef2.data(), 0, coef2.size() * sizeof(T), stream)); + linearRegressionTest(); + logisticRegressionTest(); + svmTest(); + } + protected: void linearRegressionTest() { - auto stream = handle.get_stream(); - params = ::testing::TestWithParam>::GetParam(); - int len = params.n_row * params.n_col; - - raft::allocate(data, len, stream); - raft::allocate(labels, params.n_row, stream); - raft::allocate(coef, params.n_col, stream, true); - raft::allocate(coef2, params.n_col, stream, true); - raft::allocate(coef_ref, params.n_col, stream); - raft::allocate(coef2_ref, params.n_col, stream); + int len = params.n_row * params.n_col; + rmm::device_uvector data(len, stream); + rmm::device_uvector labels(params.n_row, stream); T data_h[len] = {1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0, 3.0}; - raft::update_device(data, data_h, len, stream); + raft::update_device(data.data(), data_h, len, stream); T labels_h[params.n_row] = {6.0, 8.0, 9.0, 11.0}; - raft::update_device(labels, labels_h, params.n_row, stream); + raft::update_device(labels.data(), labels_h, params.n_row, stream); T coef_ref_h[params.n_col] = {2.087, 2.5454557}; - raft::update_device(coef_ref, coef_ref_h, params.n_col, stream); + raft::update_device(coef_ref.data(), coef_ref_h, params.n_col, stream); T coef2_ref_h[params.n_col] = {1.000001, 1.9999998}; - raft::update_device(coef2_ref, coef2_ref_h, params.n_col, stream); + raft::update_device(coef2_ref.data(), coef2_ref_h, params.n_col, stream); bool fit_intercept = false; intercept = T(0); @@ -79,11 +93,11 @@ class SgdTest : public ::testing::TestWithParam> { int n_iter_no_change = 10; sgdFit(handle, - data, + data.data(), params.n_row, params.n_col, - labels, - coef, + labels.data(), + coef.data(), &intercept, fit_intercept, params.batch_size, @@ -103,11 +117,11 @@ class SgdTest : public ::testing::TestWithParam> { fit_intercept = true; intercept2 = T(0); sgdFit(handle, - data, + data.data(), params.n_row, params.n_col, - labels, - coef2, + labels.data(), + coef2.data(), &intercept2, fit_intercept, params.batch_size, @@ -127,29 +141,26 @@ class SgdTest : public ::testing::TestWithParam> { void logisticRegressionTest() { - auto stream = handle.get_stream(); - params = ::testing::TestWithParam>::GetParam(); - int len = params.n_row2 * params.n_col2; - - T* coef_class; - raft::allocate(data_logreg, len, stream); - raft::allocate(data_logreg_test, len, stream); - raft::allocate(labels_logreg, params.n_row2, stream); - raft::allocate(coef_class, params.n_col2, stream, true); - raft::allocate(pred_log, params.n_row2, stream); - raft::allocate(pred_log_ref, params.n_row2, stream); + int len = params.n_row2 * params.n_col2; + rmm::device_uvector data_logreg(len, stream); + rmm::device_uvector data_logreg_test(len, stream); + rmm::device_uvector labels_logreg(params.n_row2, stream); + rmm::device_uvector coef_class(params.n_row2, stream); + pred_log.resize(params.n_row2, stream); + pred_log_ref.resize(params.n_row2, stream); + CUDA_CHECK(cudaMemsetAsync(coef_class.data(), 0, coef_class.size() * sizeof(T), stream)); T data_h[len] = {0.1, -2.1, 5.4, 5.4, -1.5, -2.15, 2.65, 2.65, 3.25, -0.15, -7.35, -7.35}; - raft::update_device(data_logreg, data_h, len, stream); + raft::update_device(data_logreg.data(), data_h, len, stream); T data_test_h[len] = {0.3, 1.1, 2.1, -10.1, 0.5, 2.5, -3.55, -20.5, -1.3, 3.0, -5.0, 15.0}; - raft::update_device(data_logreg_test, data_test_h, len, stream); + raft::update_device(data_logreg_test.data(), data_test_h, len, stream); T labels_logreg_h[params.n_row2] = {0.0, 1.0, 1.0, 0.0}; - raft::update_device(labels_logreg, labels_logreg_h, params.n_row2, stream); + raft::update_device(labels_logreg.data(), labels_logreg_h, params.n_row2, stream); T pred_log_ref_h[params.n_row2] = {1.0, 0.0, 1.0, 1.0}; - raft::update_device(pred_log_ref, pred_log_ref_h, params.n_row2, stream); + raft::update_device(pred_log_ref.data(), pred_log_ref_h, params.n_row2, stream); bool fit_intercept = true; T intercept_class = T(0); @@ -166,11 +177,11 @@ class SgdTest : public ::testing::TestWithParam> { int n_iter_no_change = 10; sgdFit(handle, - data_logreg, + data_logreg.data(), params.n_row2, params.n_col2, - labels_logreg, - coef_class, + labels_logreg.data(), + coef_class.data(), &intercept_class, fit_intercept, params.batch_size, @@ -188,43 +199,39 @@ class SgdTest : public ::testing::TestWithParam> { stream); sgdPredictBinaryClass(handle, - data_logreg_test, + data_logreg_test.data(), params.n_row2, params.n_col2, - coef_class, + coef_class.data(), intercept_class, - pred_log, + pred_log.data(), loss, stream); - - CUDA_CHECK(cudaFree(coef_class)); } void svmTest() { - auto stream = handle.get_stream(); - params = ::testing::TestWithParam>::GetParam(); - int len = params.n_row2 * params.n_col2; - - T* coef_class; - raft::allocate(data_svmreg, len, stream); - raft::allocate(data_svmreg_test, len, stream); - raft::allocate(labels_svmreg, params.n_row2, stream); - raft::allocate(coef_class, params.n_col2, stream, true); - raft::allocate(pred_svm, params.n_row2, stream); - raft::allocate(pred_svm_ref, params.n_row2, stream); + int len = params.n_row2 * params.n_col2; + + rmm::device_uvector data_svmreg(len, stream); + rmm::device_uvector data_svmreg_test(len, stream); + rmm::device_uvector labels_svmreg(params.n_row2, stream); + rmm::device_uvector coef_class(params.n_row2, stream); + pred_svm.resize(params.n_row2, stream); + pred_svm_ref.resize(params.n_row2, stream); + CUDA_CHECK(cudaMemsetAsync(coef_class.data(), 0, coef_class.size() * sizeof(T), stream)); T data_h[len] = {0.1, -2.1, 5.4, 5.4, -1.5, -2.15, 2.65, 2.65, 3.25, -0.15, -7.35, -7.35}; - raft::update_device(data_svmreg, data_h, len, stream); + raft::update_device(data_svmreg.data(), data_h, len, stream); T data_test_h[len] = {0.3, 1.1, 2.1, -10.1, 0.5, 2.5, -3.55, -20.5, -1.3, 3.0, -5.0, 15.0}; - raft::update_device(data_svmreg_test, data_test_h, len, stream); + raft::update_device(data_svmreg_test.data(), data_test_h, len, stream); T labels_svmreg_h[params.n_row2] = {0.0, 1.0, 1.0, 0.0}; - raft::update_device(labels_svmreg, labels_svmreg_h, params.n_row2, stream); + raft::update_device(labels_svmreg.data(), labels_svmreg_h, params.n_row2, stream); T pred_svm_ref_h[params.n_row2] = {1.0, 0.0, 1.0, 1.0}; - raft::update_device(pred_svm_ref, pred_svm_ref_h, params.n_row2, stream); + raft::update_device(pred_svm_ref.data(), pred_svm_ref_h, params.n_row2, stream); bool fit_intercept = true; T intercept_class = T(0); @@ -241,11 +248,11 @@ class SgdTest : public ::testing::TestWithParam> { int n_iter_no_change = 10; sgdFit(handle, - data_svmreg, + data_svmreg.data(), params.n_row2, params.n_col2, - labels_svmreg, - coef_class, + labels_svmreg.data(), + coef_class.data(), &intercept_class, fit_intercept, params.batch_size, @@ -263,54 +270,26 @@ class SgdTest : public ::testing::TestWithParam> { stream); sgdPredictBinaryClass(handle, - data_svmreg_test, + data_svmreg_test.data(), params.n_row2, params.n_col2, - coef_class, + coef_class.data(), intercept_class, - pred_svm, + pred_svm.data(), loss, stream); - - CUDA_CHECK(cudaFree(coef_class)); - } - - void SetUp() override - { - linearRegressionTest(); - logisticRegressionTest(); - svmTest(); - } - - void TearDown() override - { - CUDA_CHECK(cudaFree(data)); - CUDA_CHECK(cudaFree(labels)); - CUDA_CHECK(cudaFree(coef)); - CUDA_CHECK(cudaFree(coef_ref)); - CUDA_CHECK(cudaFree(coef2)); - CUDA_CHECK(cudaFree(coef2_ref)); - CUDA_CHECK(cudaFree(data_logreg)); - CUDA_CHECK(cudaFree(data_logreg_test)); - CUDA_CHECK(cudaFree(labels_logreg)); - CUDA_CHECK(cudaFree(data_svmreg)); - CUDA_CHECK(cudaFree(data_svmreg_test)); - CUDA_CHECK(cudaFree(labels_svmreg)); - CUDA_CHECK(cudaFree(pred_svm)); - CUDA_CHECK(cudaFree(pred_svm_ref)); - CUDA_CHECK(cudaFree(pred_log)); - CUDA_CHECK(cudaFree(pred_log_ref)); } protected: + raft::handle_t handle; + cudaStream_t stream = 0; + SgdInputs params; - T *data, *labels, *coef, *coef_ref; - T *coef2, *coef2_ref; - T *data_logreg, *data_logreg_test, *labels_logreg; - T *data_svmreg, *data_svmreg_test, *labels_svmreg; - T *pred_svm, *pred_svm_ref, *pred_log, *pred_log_ref; + rmm::device_uvector coef, coef_ref; + rmm::device_uvector coef2, coef2_ref; + rmm::device_uvector pred_log, pred_log_ref; + rmm::device_uvector pred_svm, pred_svm_ref; T intercept, intercept2; - raft::handle_t handle; }; const std::vector> inputsf2 = {{0.01f, 4, 2, 4, 3, 2}}; @@ -320,35 +299,37 @@ const std::vector> inputsd2 = {{0.01, 4, 2, 4, 3, 2}}; typedef SgdTest SgdTestF; TEST_P(SgdTestF, Fit) { - auto stream = handle.get_stream(); ASSERT_TRUE(raft::devArrMatch( - coef_ref, coef, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef_ref.data(), coef.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - coef2_ref, coef2, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef2_ref.data(), coef2.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - pred_log_ref, pred_log, params.n_row, raft::CompareApproxAbs(params.tol), stream)); + pred_log_ref.data(), pred_log.data(), params.n_row, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - pred_svm_ref, pred_svm, params.n_row, raft::CompareApproxAbs(params.tol), stream)); + pred_svm_ref.data(), pred_svm.data(), params.n_row, raft::CompareApproxAbs(params.tol))); } typedef SgdTest SgdTestD; TEST_P(SgdTestD, Fit) { - auto stream = handle.get_stream(); ASSERT_TRUE(raft::devArrMatch( - coef_ref, coef, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef_ref.data(), coef.data(), params.n_col, raft::CompareApproxAbs(params.tol))); ASSERT_TRUE(raft::devArrMatch( - coef2_ref, coef2, params.n_col, raft::CompareApproxAbs(params.tol), stream)); + coef2_ref.data(), coef2.data(), params.n_col, raft::CompareApproxAbs(params.tol))); - ASSERT_TRUE(raft::devArrMatch( - pred_log_ref, pred_log, params.n_row, raft::CompareApproxAbs(params.tol), stream)); + ASSERT_TRUE(raft::devArrMatch(pred_log_ref.data(), + pred_log.data(), + params.n_row, + raft::CompareApproxAbs(params.tol))); - ASSERT_TRUE(raft::devArrMatch( - pred_svm_ref, pred_svm, params.n_row, raft::CompareApproxAbs(params.tol), stream)); + ASSERT_TRUE(raft::devArrMatch(pred_svm_ref.data(), + pred_svm.data(), + params.n_row, + raft::CompareApproxAbs(params.tol))); } INSTANTIATE_TEST_CASE_P(SgdTests, SgdTestF, ::testing::ValuesIn(inputsf2)); diff --git a/cpp/test/sg/shap_kernel.cu b/cpp/test/sg/shap_kernel.cu index a3524edcdc..9beeb1806e 100644 --- a/cpp/test/sg/shap_kernel.cu +++ b/cpp/test/sg/shap_kernel.cu @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -50,24 +51,24 @@ class MakeKSHAPDatasetTest : public ::testing::TestWithParam::GetParam(); + stream = handle.get_stream(); - params = ::testing::TestWithParam::GetParam(); + int i, j; nrows_X = params.nrows_exact + params.nrows_sampled; - raft::allocate(background, params.nrows_background * params.ncols, stream); - raft::allocate(observation, params.ncols, stream); - raft::allocate(nsamples, params.nrows_sampled / 2, stream); - - raft::allocate(X, nrows_X * params.ncols, stream); - raft::allocate(dataset, nrows_X * params.nrows_background * params.ncols, stream); + rmm::device_uvector background(params.nrows_background * params.ncols, stream); + rmm::device_uvector observation(params.ncols, stream); + rmm::device_uvector nsamples(params.nrows_sampled / 2, stream); + rmm::device_uvector X(nrows_X * params.ncols, stream); + rmm::device_uvector dataset(nrows_X * params.nrows_background * params.ncols, stream); - thrust::device_ptr b_ptr = thrust::device_pointer_cast(background); - thrust::device_ptr o_ptr = thrust::device_pointer_cast(observation); - thrust::device_ptr n_ptr = thrust::device_pointer_cast(nsamples); + thrust::device_ptr b_ptr = thrust::device_pointer_cast(background.data()); + thrust::device_ptr o_ptr = thrust::device_pointer_cast(observation.data()); + thrust::device_ptr n_ptr = thrust::device_pointer_cast(nsamples.data()); - thrust::device_ptr X_ptr = thrust::device_pointer_cast(X); - thrust::device_ptr d_ptr = thrust::device_pointer_cast(dataset); + thrust::device_ptr X_ptr = thrust::device_pointer_cast(X.data()); + thrust::device_ptr d_ptr = thrust::device_pointer_cast(dataset.data()); // Initialize arrays: @@ -100,19 +101,19 @@ class MakeKSHAPDatasetTest : public ::testing::TestWithParam class WorkingSetTest : public ::testing::Test { - protected: - void SetUp() override + public: + WorkingSetTest() + : stream(handle.get_stream()), + f_dev(10, stream), + y_dev(10, stream), + C_dev(10, stream), + alpha_dev(10, stream) { - auto stream = this->handle.get_stream(); - raft::allocate(f_dev, 10, stream); - raft::allocate(y_dev, 10, stream); - raft::allocate(C_dev, 10, stream); - raft::allocate(alpha_dev, 10, stream); - init_C(C, C_dev, 10, stream); - raft::update_device(f_dev, f_host, 10, stream); - raft::update_device(y_dev, y_host, 10, stream); - raft::update_device(alpha_dev, alpha_host, 10, stream); + init_C(C, C_dev.data(), 10, stream); + raft::update_device(f_dev.data(), f_host, 10, stream); + raft::update_device(y_dev.data(), y_host, 10, stream); + raft::update_device(alpha_dev.data(), alpha_host, 10, stream); } - void TearDown() override - { - CUDA_CHECK(cudaFree(f_dev)); - CUDA_CHECK(cudaFree(y_dev)); - CUDA_CHECK(cudaFree(C_dev)); - CUDA_CHECK(cudaFree(alpha_dev)); - } + protected: raft::handle_t handle; + cudaStream_t stream = 0; + WorkingSet* ws; math_t f_host[10] = {1, 3, 10, 4, 2, 8, 6, 5, 9, 7}; - math_t* f_dev; + rmm::device_uvector f_dev; math_t y_host[10] = {-1, -1, -1, -1, -1, 1, 1, 1, 1, 1}; - math_t* y_dev; + rmm::device_uvector y_dev; - math_t* C_dev; + rmm::device_uvector C_dev; math_t C = 1.5; math_t alpha_host[10] = {0, 0, 0.1, 0.2, 1.5, 0, 0.2, 0.4, 1.5, 1.5}; - math_t* alpha_dev; // l l l/u l/u u u l/u l/u l l + rmm::device_uvector alpha_dev; // l l l/u l/u u u l/u l/u l l int expected_idx[4] = {4, 3, 8, 2}; int expected_idx2[4] = {8, 2, 4, 9}; @@ -121,14 +117,17 @@ TYPED_TEST(WorkingSetTest, Select) auto stream = this->handle.get_stream(); this->ws = new WorkingSet(this->handle, stream, 10, 4); EXPECT_EQ(this->ws->GetSize(), 4); - this->ws->SimpleSelect(this->f_dev, this->alpha_dev, this->y_dev, this->C_dev); + this->ws->SimpleSelect( + this->f_dev.data(), this->alpha_dev.data(), this->y_dev.data(), this->C_dev.data()); ASSERT_TRUE(devArrMatchHost( this->expected_idx, this->ws->GetIndices(), this->ws->GetSize(), raft::Compare(), stream)); - this->ws->Select(this->f_dev, this->alpha_dev, this->y_dev, this->C_dev); + this->ws->Select( + this->f_dev.data(), this->alpha_dev.data(), this->y_dev.data(), this->C_dev.data()); ASSERT_TRUE(devArrMatchHost( this->expected_idx, this->ws->GetIndices(), this->ws->GetSize(), raft::Compare(), stream)); - this->ws->Select(this->f_dev, this->alpha_dev, this->y_dev, this->C_dev); + this->ws->Select( + this->f_dev.data(), this->alpha_dev.data(), this->y_dev.data(), this->C_dev.data()); ASSERT_TRUE(devArrMatchHost(this->expected_idx2, this->ws->GetIndices(), @@ -144,24 +143,21 @@ TYPED_TEST(WorkingSetTest, Select) template class KernelCacheTest : public ::testing::Test { - protected: - void SetUp() override - { - auto stream = this->handle.get_stream(); - cublas_handle = handle.get_cublas_handle(); - raft::allocate(x_dev, n_rows * n_cols, stream); - raft::update_device(x_dev, x_host, n_rows * n_cols, stream); - - raft::allocate(ws_idx_dev, 2 * n_ws, stream); - raft::update_device(ws_idx_dev, ws_idx_host, n_ws, stream); - } - - void TearDown() override + public: + KernelCacheTest() + : stream(handle.get_stream()), + cublas_handle(handle.get_cublas_handle()), + n_rows(4), + n_cols(2), + n_ws(3), + x_dev(n_rows * n_cols, stream), + ws_idx_dev(2 * n_ws, stream) { - CUDA_CHECK(cudaFree(x_dev)); - CUDA_CHECK(cudaFree(ws_idx_dev)); + raft::update_device(x_dev.data(), x_host, n_rows * n_cols, stream); + raft::update_device(ws_idx_dev.data(), ws_idx_host, n_ws, stream); } + protected: // Naive host side kernel implementation used for comparison void ApplyNonlin(Matrix::KernelParams params) { @@ -218,13 +214,14 @@ class KernelCacheTest : public ::testing::Test { raft::handle_t handle; cublasHandle_t cublas_handle; + cudaStream_t stream = 0; - int n_rows = 4; - int n_cols = 2; - int n_ws = 3; + int n_rows; + int n_cols; + int n_ws; - math_t* x_dev; - int* ws_idx_dev; + rmm::device_uvector x_dev; + rmm::device_uvector ws_idx_dev; math_t x_host[8] = {1, 2, 3, 4, 5, 6, 7, 8}; int ws_idx_host[4] = {0, 1, 3}; @@ -246,9 +243,15 @@ TYPED_TEST_P(KernelCacheTest, EvalTest) for (auto params : param_vec) { Matrix::GramMatrixBase* kernel = Matrix::KernelFactory::create(params, this->handle.get_cublas_handle()); - KernelCache cache( - this->handle, this->x_dev, this->n_rows, this->n_cols, this->n_ws, kernel, cache_size, C_SVC); - TypeParam* tile_dev = cache.GetTile(this->ws_idx_dev); + KernelCache cache(this->handle, + this->x_dev.data(), + this->n_rows, + this->n_cols, + this->n_ws, + kernel, + cache_size, + C_SVC); + TypeParam* tile_dev = cache.GetTile(this->ws_idx_dev.data()); // apply nonlinearity on tile_host_expected this->ApplyNonlin(params); ASSERT_TRUE(devArrMatchHost(this->tile_host_expected, @@ -267,11 +270,17 @@ TYPED_TEST_P(KernelCacheTest, CacheEvalTest) Matrix::GramMatrixBase* kernel = Matrix::KernelFactory::create(param, this->handle.get_cublas_handle()); - KernelCache cache( - this->handle, this->x_dev, this->n_rows, this->n_cols, this->n_ws, kernel, cache_size, C_SVC); + KernelCache cache(this->handle, + this->x_dev.data(), + this->n_rows, + this->n_cols, + this->n_ws, + kernel, + cache_size, + C_SVC); for (int i = 0; i < 2; i++) { // We calculate cache tile multiple times to see if cache lookup works - TypeParam* tile_dev = cache.GetTile(this->ws_idx_dev); + TypeParam* tile_dev = cache.GetTile(this->ws_idx_dev.data()); this->check(tile_dev, this->n_ws, this->n_rows, cache.GetWsIndices(), cache.GetColIdxMap()); } delete kernel; @@ -279,18 +288,17 @@ TYPED_TEST_P(KernelCacheTest, CacheEvalTest) TYPED_TEST_P(KernelCacheTest, SvrEvalTest) { - auto stream = this->handle.get_stream(); Matrix::KernelParams param{Matrix::LINEAR, 3, 1, 0}; float cache_size = sizeof(TypeParam) * this->n_rows * 32 / (1024.0 * 1024); this->n_ws = 6; int ws_idx_svr[6] = {0, 5, 1, 4, 3, 7}; - raft::update_device(this->ws_idx_dev, ws_idx_svr, 6, stream); + raft::update_device(this->ws_idx_dev.data(), ws_idx_svr, 6, this->stream); Matrix::GramMatrixBase* kernel = Matrix::KernelFactory::create(param, this->handle.get_cublas_handle()); KernelCache cache(this->handle, - this->x_dev, + this->x_dev.data(), this->n_rows, this->n_cols, this->n_ws, @@ -300,7 +308,7 @@ TYPED_TEST_P(KernelCacheTest, SvrEvalTest) for (int i = 0; i < 2; i++) { // We calculate cache tile multiple times to see if cache lookup works - TypeParam* tile_dev = cache.GetTile(this->ws_idx_dev); + TypeParam* tile_dev = cache.GetTile(this->ws_idx_dev.data()); this->check(tile_dev, this->n_ws, this->n_rows, cache.GetWsIndices(), cache.GetColIdxMap()); } delete kernel; @@ -311,6 +319,9 @@ INSTANTIATE_TYPED_TEST_CASE_P(My, KernelCacheTest, FloatTypes); template class GetResultsTest : public ::testing::Test { + public: + GetResultsTest() : stream(handle.get_stream()) {} + protected: void TestResults() { @@ -349,6 +360,10 @@ class GetResultsTest : public ::testing::Test { res.Get(alpha_dev.data(), f_dev.data(), &dual_coefs, &n_coefs, &idx, &x_support, &b); EXPECT_FLOAT_EQ(b, -5.5f); } + + raft::handle_t handle; + cudaStream_t stream = 0; + int n_rows = 10; int n_cols = 2; math_t x_host[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; @@ -363,8 +378,6 @@ class GetResultsTest : public ::testing::Test { int* idx; math_t* x_support; math_t b; - - raft::handle_t handle; }; TYPED_TEST_CASE(GetResultsTest, FloatTypes); @@ -387,39 +400,39 @@ SvmParameter getDefaultSvmParameter() template class SmoUpdateTest : public ::testing::Test { - protected: - void SetUp() override + public: + SmoUpdateTest() + : stream(handle.get_stream()), + n_rows(6), + n_ws(2), + f_dev(n_rows, stream), + kernel_dev(n_rows * n_ws, stream), + delta_alpha_dev(n_ws, stream) { - stream = handle.get_stream(); - cublasHandle_t cublas_handle = handle.get_cublas_handle(); - raft::allocate(f_dev, n_rows, stream, true); - raft::allocate(kernel_dev, n_rows * n_ws, stream); - raft::update_device(kernel_dev, kernel_host, n_ws * n_rows, stream); - raft::allocate(delta_alpha_dev, n_ws, stream); - raft::update_device(delta_alpha_dev, delta_alpha_host, n_ws, stream); + CUDA_CHECK(cudaMemsetAsync(f_dev.data(), 0, f_dev.size() * sizeof(float), stream)); + raft::update_device(kernel_dev.data(), kernel_host, n_ws * n_rows, stream); + raft::update_device(delta_alpha_dev.data(), delta_alpha_host, n_ws, stream); } + + protected: void RunTest() { SvmParameter param = getDefaultSvmParameter(); SmoSolver smo(handle, param, nullptr); - smo.UpdateF(f_dev, n_rows, delta_alpha_dev, n_ws, kernel_dev); + smo.UpdateF(f_dev.data(), n_rows, delta_alpha_dev.data(), n_ws, kernel_dev.data()); float f_host_expected[] = {0.1f, 7.4505806e-9f, 0.3f, 0.2f, 0.5f, 0.4f}; - devArrMatchHost(f_host_expected, f_dev, n_rows, raft::CompareApprox(1e-6)); - } - void TearDown() override - { - CUDA_CHECK(cudaFree(delta_alpha_dev)); - CUDA_CHECK(cudaFree(kernel_dev)); - CUDA_CHECK(cudaFree(f_dev)); + devArrMatchHost(f_host_expected, f_dev.data(), n_rows, raft::CompareApprox(1e-6)); } + raft::handle_t handle; cudaStream_t stream = 0; - int n_rows = 6; - int n_ws = 2; - float* kernel_dev; - float* f_dev; - float* delta_alpha_dev; + + int n_rows; + int n_ws; + rmm::device_uvector kernel_dev; + rmm::device_uvector f_dev; + rmm::device_uvector delta_alpha_dev; float kernel_host[12] = {3, 5, 4, 6, 5, 7, 4, 5, 7, 8, 10, 11}; float delta_alpha_host[2] = {-0.1f, 0.1f}; }; @@ -429,92 +442,88 @@ TYPED_TEST(SmoUpdateTest, Update) { this->RunTest(); } template class SmoBlockSolverTest : public ::testing::Test { - protected: - void SetUp() override + public: + SmoBlockSolverTest() + : stream(handle.get_stream()), + cublas_handle(handle.get_cublas_handle()), + n_rows(4), + n_cols(2), + n_ws(4), + ws_idx_dev(n_ws, stream), + y_dev(n_rows, stream), + C_dev(n_rows, stream), + f_dev(n_rows, stream), + alpha_dev(n_rows, stream), + delta_alpha_dev(n_ws, stream), + kernel_dev(n_ws * n_rows, stream), + return_buff_dev(2, stream) { - auto stream = this->handle.get_stream(); - cublas_handle = handle.get_cublas_handle(); - raft::allocate(ws_idx_dev, n_ws, stream); - raft::allocate(y_dev, n_rows, stream); - raft::allocate(C_dev, n_rows, stream); - raft::allocate(f_dev, n_rows, stream); - raft::allocate(alpha_dev, n_rows, stream, true); - raft::allocate(delta_alpha_dev, n_ws, stream, true); - raft::allocate(kernel_dev, n_ws * n_rows, stream); - raft::allocate(return_buff_dev, 2, stream); - - init_C(C, C_dev, n_rows, stream); - raft::update_device(ws_idx_dev, ws_idx_host, n_ws, stream); - raft::update_device(y_dev, y_host, n_rows, stream); - raft::update_device(f_dev, f_host, n_rows, stream); - raft::update_device(kernel_dev, kernel_host, n_ws * n_rows, stream); + CUDA_CHECK(cudaMemsetAsync(alpha_dev.data(), 0, alpha_dev.size() * sizeof(math_t), stream)); + CUDA_CHECK( + cudaMemsetAsync(delta_alpha_dev.data(), 0, delta_alpha_dev.size() * sizeof(math_t), stream)); + init_C(C, C_dev.data(), n_rows, stream); + raft::update_device(ws_idx_dev.data(), ws_idx_host, n_ws, stream); + raft::update_device(y_dev.data(), y_host, n_rows, stream); + raft::update_device(f_dev.data(), f_host, n_rows, stream); + raft::update_device(kernel_dev.data(), kernel_host, n_ws * n_rows, stream); } public: // because of the device lambda void testBlockSolve() { - auto stream = this->handle.get_stream(); - SmoBlockSolve<<<1, n_ws, 0, stream>>>(y_dev, + SmoBlockSolve<<<1, n_ws, 0, stream>>>(y_dev.data(), n_rows, - alpha_dev, + alpha_dev.data(), n_ws, - delta_alpha_dev, - f_dev, - kernel_dev, - ws_idx_dev, - C_dev, + delta_alpha_dev.data(), + f_dev.data(), + kernel_dev.data(), + ws_idx_dev.data(), + C_dev.data(), 1e-3f, - return_buff_dev, + return_buff_dev.data(), 1); CUDA_CHECK(cudaPeekAtLastError()); math_t return_buff_exp[2] = {0.2, 1}; - devArrMatchHost(return_buff_exp, return_buff_dev, 2, raft::CompareApprox(1e-6), stream); + devArrMatchHost( + return_buff_exp, return_buff_dev.data(), 2, raft::CompareApprox(1e-6), stream); - math_t* delta_alpha_calc; - raft::allocate(delta_alpha_calc, n_rows, stream); + rmm::device_uvector delta_alpha_calc(n_rows, stream); raft::linalg::binaryOp( - delta_alpha_calc, - y_dev, - alpha_dev, + delta_alpha_calc.data(), + y_dev.data(), + alpha_dev.data(), n_rows, [] __device__(math_t a, math_t b) { return a * b; }, stream); - raft::devArrMatch( - delta_alpha_dev, delta_alpha_calc, n_rows, raft::CompareApprox(1e-6), stream); - CUDA_CHECK(cudaFree(delta_alpha_calc)); + raft::devArrMatch(delta_alpha_dev.data(), + delta_alpha_calc.data(), + n_rows, + raft::CompareApprox(1e-6), + stream); math_t alpha_expected[] = {0, 0.1f, 0.1f, 0}; - raft::devArrMatch(alpha_expected, alpha_dev, n_rows, raft::CompareApprox(1e-6), stream); + raft::devArrMatch( + alpha_expected, alpha_dev.data(), n_rows, raft::CompareApprox(1e-6), stream); } protected: - void TearDown() override - { - CUDA_CHECK(cudaFree(y_dev)); - CUDA_CHECK(cudaFree(C_dev)); - CUDA_CHECK(cudaFree(f_dev)); - CUDA_CHECK(cudaFree(ws_idx_dev)); - CUDA_CHECK(cudaFree(alpha_dev)); - CUDA_CHECK(cudaFree(delta_alpha_dev)); - CUDA_CHECK(cudaFree(kernel_dev)); - CUDA_CHECK(cudaFree(return_buff_dev)); - } - raft::handle_t handle; cublasHandle_t cublas_handle; + cudaStream_t stream = 0; - int n_rows = 4; - int n_cols = 2; - int n_ws = 4; + int n_rows; + int n_cols; + int n_ws; - int* ws_idx_dev; - math_t* y_dev; - math_t* f_dev; - math_t* C_dev; - math_t* alpha_dev; - math_t* delta_alpha_dev; - math_t* kernel_dev; - math_t* return_buff_dev; + rmm::device_uvector ws_idx_dev; + rmm::device_uvector y_dev; + rmm::device_uvector f_dev; + rmm::device_uvector C_dev; + rmm::device_uvector alpha_dev; + rmm::device_uvector delta_alpha_dev; + rmm::device_uvector kernel_dev; + rmm::device_uvector return_buff_dev; int ws_idx_host[4] = {0, 1, 2, 3}; math_t y_host[4] = {1, 1, -1, -1}; @@ -661,104 +670,87 @@ void checkResults(SvmModel model, template class SmoSolverTest : public ::testing::Test { - protected: - void SetUp() override + public: + SmoSolverTest() + : stream(handle.get_stream()), + cublas_handle(handle.get_cublas_handle()), + x_dev(n_rows * n_cols, stream), + ws_idx_dev(n_ws, stream), + y_dev(n_rows, stream), + C_dev(n_rows, stream), + y_pred(n_rows, stream), + f_dev(n_rows, stream), + alpha_dev(n_rows, stream), + delta_alpha_dev(n_ws, stream), + kernel_dev(n_ws * n_rows, stream), + return_buff_dev(2, stream), + sample_weights_dev(n_rows, stream) { - auto stream = handle.get_stream(); - raft::allocate(x_dev, n_rows * n_cols, stream); - raft::allocate(ws_idx_dev, n_ws, stream); - raft::allocate(y_dev, n_rows, stream); - raft::allocate(C_dev, n_rows, stream); - raft::allocate(y_pred, n_rows, stream); - raft::allocate(f_dev, n_rows, stream); - raft::allocate(alpha_dev, n_rows, stream, true); - raft::allocate(delta_alpha_dev, n_ws, stream, true); - raft::allocate(kernel_dev, n_ws * n_rows, stream); - raft::allocate(return_buff_dev, 2, stream); - raft::allocate(sample_weights_dev, n_rows, stream); - LinAlg::range(sample_weights_dev, 1, n_rows + 1, stream); - cublas_handle = handle.get_cublas_handle(); - - raft::update_device(x_dev, x_host, n_rows * n_cols, stream); - raft::update_device(ws_idx_dev, ws_idx_host, n_ws, stream); - raft::update_device(y_dev, y_host, n_rows, stream); - init_C(C, C_dev, n_rows, stream); - raft::update_device(f_dev, f_host, n_rows, stream); - raft::update_device(kernel_dev, kernel_host, n_ws * n_rows, stream); - CUDA_CHECK(cudaMemsetAsync(delta_alpha_dev, 0, n_ws * sizeof(math_t), stream)); - - kernel = new Matrix::GramMatrixBase(cublas_handle); + CUDA_CHECK(cudaMemsetAsync(alpha_dev.data(), 0, alpha_dev.size() * sizeof(math_t), stream)); + CUDA_CHECK( + cudaMemsetAsync(delta_alpha_dev.data(), 0, delta_alpha_dev.size() * sizeof(math_t), stream)); } - void FreeResultBuffers() - { - if (dual_coefs_d) CUDA_CHECK(cudaFree(dual_coefs_d)); - if (idx_d) CUDA_CHECK(cudaFree(idx_d)); - if (x_support_d) CUDA_CHECK(cudaFree(x_support_d)); - dual_coefs_d = nullptr; - idx_d = nullptr; - x_support_d = nullptr; - } - void TearDown() override + protected: + void SetUp() override { - delete kernel; - CUDA_CHECK(cudaFree(x_dev)); - CUDA_CHECK(cudaFree(y_dev)); - CUDA_CHECK(cudaFree(C_dev)); - CUDA_CHECK(cudaFree(y_pred)); - CUDA_CHECK(cudaFree(f_dev)); - CUDA_CHECK(cudaFree(ws_idx_dev)); - CUDA_CHECK(cudaFree(alpha_dev)); - CUDA_CHECK(cudaFree(delta_alpha_dev)); - CUDA_CHECK(cudaFree(kernel_dev)); - CUDA_CHECK(cudaFree(return_buff_dev)); - CUDA_CHECK(cudaFree(sample_weights_dev)); - FreeResultBuffers(); + LinAlg::range(sample_weights_dev.data(), 1, n_rows + 1, stream); + + raft::update_device(x_dev.data(), x_host, n_rows * n_cols, stream); + raft::update_device(ws_idx_dev.data(), ws_idx_host, n_ws, stream); + raft::update_device(y_dev.data(), y_host, n_rows, stream); + init_C(C, C_dev.data(), n_rows, stream); + raft::update_device(f_dev.data(), f_host, n_rows, stream); + raft::update_device(kernel_dev.data(), kernel_host, n_ws * n_rows, stream); + CUDA_CHECK(cudaMemsetAsync(delta_alpha_dev.data(), 0, n_ws * sizeof(math_t), stream)); + + kernel = std::make_unique>(cublas_handle); } public: void blockSolveTest() { - auto stream = this->handle.get_stream(); - SmoBlockSolve<<<1, n_ws, 0, stream>>>(y_dev, + SmoBlockSolve<<<1, n_ws, 0, stream>>>(y_dev.data(), n_rows, - alpha_dev, + alpha_dev.data(), n_ws, - delta_alpha_dev, - f_dev, - kernel_dev, - ws_idx_dev, - C_dev, + delta_alpha_dev.data(), + f_dev.data(), + kernel_dev.data(), + ws_idx_dev.data(), + C_dev.data(), 1e-3, - return_buff_dev); + return_buff_dev.data()); CUDA_CHECK(cudaPeekAtLastError()); math_t return_buff[2]; - raft::update_host(return_buff, return_buff_dev, 2, stream); + raft::update_host(return_buff, return_buff_dev.data(), 2, stream); CUDA_CHECK(cudaStreamSynchronize(stream)); EXPECT_FLOAT_EQ(return_buff[0], 2.0f) << return_buff[0]; EXPECT_LT(return_buff[1], 100) << return_buff[1]; // check results won't work, because it expects that GetResults was called - math_t* delta_alpha_calc; - raft::allocate(delta_alpha_calc, n_rows, stream); + rmm::device_uvector delta_alpha_calc(n_rows, stream); raft::linalg::binaryOp( - delta_alpha_calc, - y_dev, - alpha_dev, + delta_alpha_calc.data(), + y_dev.data(), + alpha_dev.data(), n_rows, [] __device__(math_t a, math_t b) { return a * b; }, stream); - raft::devArrMatch( - delta_alpha_dev, delta_alpha_calc, n_rows, raft::CompareApprox(1e-6), stream); - CUDA_CHECK(cudaFree(delta_alpha_calc)); + raft::devArrMatch(delta_alpha_dev.data(), + delta_alpha_calc.data(), + n_rows, + raft::CompareApprox(1e-6), + stream); math_t alpha_expected[] = {0.6f, 0, 1, 1, 0, 0.6f}; // for C=10: {0.25f, 0, 2.25f, 3.75f, 0, 1.75f}; - raft::devArrMatch(alpha_expected, alpha_dev, n_rows, raft::CompareApprox(1e-6), stream); + raft::devArrMatch( + alpha_expected, alpha_dev.data(), n_rows, raft::CompareApprox(1e-6), stream); math_t host_alpha[6]; - raft::update_host(host_alpha, alpha_dev, n_rows, stream); + raft::update_host(host_alpha, alpha_dev.data(), n_rows, stream); math_t w[] = {0, 0}; math_t ay = 0; @@ -789,55 +781,59 @@ class SmoSolverTest : public ::testing::Test { // ws_idx is defined as {0, 1, 2, 3} int kColIdx[4] = {0, 1, 0, 1}; rmm::device_uvector kColIdx_dev(4, stream); - raft::update_device(f_dev, f, 4, stream); - raft::update_device(kernel_dev, kernel, 4, stream); + raft::update_device(f_dev.data(), f, 4, stream); + raft::update_device(kernel_dev.data(), kernel, 4, stream); raft::update_device(kColIdx_dev.data(), kColIdx, 4, stream); - SmoBlockSolve<<<1, n_ws, 0, stream>>>(y_dev, + SmoBlockSolve<<<1, n_ws, 0, stream>>>(y_dev.data(), 2 * n_rows, - alpha_dev, + alpha_dev.data(), n_ws, - delta_alpha_dev, - f_dev, - kernel_dev, - ws_idx_dev, - C_dev, + delta_alpha_dev.data(), + f_dev.data(), + kernel_dev.data(), + ws_idx_dev.data(), + C_dev.data(), 1e-3, - return_buff_dev, + return_buff_dev.data(), 10, EPSILON_SVR, kColIdx_dev.data()); CUDA_CHECK(cudaPeekAtLastError()); math_t return_buff[2]; - raft::update_host(return_buff, return_buff_dev, 2, stream); + raft::update_host(return_buff, return_buff_dev.data(), 2, stream); CUDA_CHECK(cudaStreamSynchronize(stream)); EXPECT_LT(return_buff[1], 10) << return_buff[1]; math_t alpha_exp[] = {0, 0.8, 0.8, 0}; - raft::devArrMatch(alpha_exp, alpha_dev, 4, raft::CompareApprox(1e-6), stream); + raft::devArrMatch(alpha_exp, alpha_dev.data(), 4, raft::CompareApprox(1e-6), stream); math_t dalpha_exp[] = {-0.8, 0.8}; - raft::devArrMatch(dalpha_exp, delta_alpha_dev, 2, raft::CompareApprox(1e-6), stream); + raft::devArrMatch( + dalpha_exp, delta_alpha_dev.data(), 2, raft::CompareApprox(1e-6), stream); } protected: raft::handle_t handle; - Matrix::GramMatrixBase* kernel; + cublasHandle_t cublas_handle; + cudaStream_t stream = 0; + + std::unique_ptr> kernel; int n_rows = 6; const int n_cols = 2; int n_ws = 6; - math_t* x_dev; - int* ws_idx_dev; - math_t* y_dev; - math_t* C_dev; - math_t* y_pred; - math_t* f_dev; - math_t* alpha_dev; - math_t* delta_alpha_dev; - math_t* kernel_dev; - math_t* return_buff_dev; - math_t* sample_weights_dev; + rmm::device_uvector x_dev; + rmm::device_uvector ws_idx_dev; + rmm::device_uvector y_dev; + rmm::device_uvector C_dev; + rmm::device_uvector y_pred; + rmm::device_uvector f_dev; + rmm::device_uvector alpha_dev; + rmm::device_uvector delta_alpha_dev; + rmm::device_uvector kernel_dev; + rmm::device_uvector return_buff_dev; + rmm::device_uvector sample_weights_dev; math_t x_host[12] = {1, 2, 1, 2, 1, 2, 1, 1, 2, 2, 3, 3}; int ws_idx_host[6] = {0, 1, 2, 3, 4, 5}; @@ -847,12 +843,8 @@ class SmoSolverTest : public ::testing::Test { math_t kernel_host[36] = {2, 3, 3, 4, 4, 5, 3, 5, 4, 6, 5, 7, 3, 4, 5, 6, 7, 8, 4, 6, 6, 8, 8, 10, 4, 5, 7, 8, 10, 11, 5, 7, 8, 10, 11, 13}; - cublasHandle_t cublas_handle; - math_t* dual_coefs_d = nullptr; int n_coefs; - int* idx_d = nullptr; - math_t* x_support_d = nullptr; math_t b; }; @@ -903,10 +895,10 @@ TYPED_TEST(SmoSolverTest, SmoSolveTest) KernelFactory::create(p.kernel_params, this->handle.get_cublas_handle()); SmoSolver smo(this->handle, param, kernel); SvmModel model{0, this->n_cols, 0, nullptr, nullptr, nullptr, 0, nullptr}; - smo.Solve(this->x_dev, + smo.Solve(this->x_dev.data(), this->n_rows, this->n_cols, - this->y_dev, + this->y_dev.data(), nullptr, &model.dual_coefs, &model.n_support, @@ -929,8 +921,8 @@ TYPED_TEST(SmoSolverTest, SvcTest) KernelParams{LINEAR, 3, 1, 0}, this->n_rows, this->n_cols, - this->x_dev, - this->y_dev, + this->x_dev.data(), + this->y_dev.data(), true}, smoOutput2{4, {-0.6, 1, -1, 0.6}, @@ -945,8 +937,8 @@ TYPED_TEST(SmoSolverTest, SvcTest) KernelParams{LINEAR, 3, 1, 0}, this->n_rows, this->n_cols, - this->x_dev, - this->y_dev, + this->x_dev.data(), + this->y_dev.data(), true}, smoOutput2{4, {}, @@ -960,8 +952,8 @@ TYPED_TEST(SmoSolverTest, SvcTest) KernelParams{POLYNOMIAL, 3, 1, 0}, this->n_rows, this->n_cols, - this->x_dev, - this->y_dev, + this->x_dev.data(), + this->y_dev.data(), true}, smoOutput2{ 3, @@ -976,8 +968,8 @@ TYPED_TEST(SmoSolverTest, SvcTest) KernelParams{TANH, 3, 0.3, 1.0}, this->n_rows, this->n_cols, - this->x_dev, - this->y_dev, + this->x_dev.data(), + this->y_dev.data(), false}, smoOutput2{ 6, @@ -992,8 +984,8 @@ TYPED_TEST(SmoSolverTest, SvcTest) KernelParams{RBF, 0, 0.15, 0}, this->n_rows, this->n_cols, - this->x_dev, - this->y_dev, + this->x_dev.data(), + this->y_dev.data(), true}, smoOutput2{ 6, @@ -1011,7 +1003,7 @@ TYPED_TEST(SmoSolverTest, SvcTest) TypeParam* sample_weights = nullptr; if (p.C == 0) { p.C = 1; - sample_weights = this->sample_weights_dev; + sample_weights = this->sample_weights_dev.data(); } SVC svc(this->handle, p.C, p.tol, p.kernel_params); svc.fit(p.x_dev, p.n_rows, p.n_cols, p.y_dev, sample_weights); @@ -1019,8 +1011,11 @@ TYPED_TEST(SmoSolverTest, SvcTest) rmm::device_uvector y_pred(p.n_rows, stream); if (p.predict) { svc.predict(p.x_dev, p.n_rows, p.n_cols, y_pred.data()); - EXPECT_TRUE(raft::devArrMatch( - this->y_dev, y_pred.data(), p.n_rows, raft::CompareApprox(1e-6f), stream)); + EXPECT_TRUE(raft::devArrMatch(this->y_dev.data(), + y_pred.data(), + p.n_rows, + raft::CompareApprox(1e-6f), + stream)); } if (exp.decision_function.size() > 0) { svc.decisionFunction(p.x_dev, p.n_rows, p.n_cols, y_pred.data()); @@ -1273,21 +1268,23 @@ std::ostream& operator<<(std::ostream& os, const SvrInput& b) template class SvrTest : public ::testing::Test { + public: + SvrTest() + : stream(handle.get_stream()), + x_dev(n_rows * n_cols, stream), + y_dev(n_rows, stream), + C_dev(2 * n_rows, stream), + yc(n_train, stream), + f(n_train, stream), + alpha(n_train, stream) + { + } + protected: void SetUp() override { - auto stream = handle.get_stream(); - raft::allocate(x_dev, n_rows * n_cols, stream); - raft::allocate(y_dev, n_rows, stream); - raft::allocate(C_dev, 2 * n_rows, stream); - raft::allocate(y_pred, n_rows, stream); - - raft::allocate(yc, n_train, stream); - raft::allocate(f, n_train, stream); - raft::allocate(alpha, n_train, stream); - - raft::update_device(x_dev, x_host, n_rows * n_cols, stream); - raft::update_device(y_dev, y_host, n_rows, stream); + raft::update_device(x_dev.data(), x_host, n_rows * n_cols, stream); + raft::update_device(y_dev.data(), y_host, n_rows, stream); model.n_support = 0; model.dual_coefs = nullptr; @@ -1297,17 +1294,7 @@ class SvrTest : public ::testing::Test { model.unique_labels = nullptr; } - void TearDown() override - { - CUDA_CHECK(cudaFree(x_dev)); - CUDA_CHECK(cudaFree(y_dev)); - CUDA_CHECK(cudaFree(C_dev)); - CUDA_CHECK(cudaFree(y_pred)); - CUDA_CHECK(cudaFree(yc)); - CUDA_CHECK(cudaFree(f)); - CUDA_CHECK(cudaFree(alpha)); - svmFreeBuffers(handle, model); - } + void TearDown() override { svmFreeBuffers(handle, model); } public: void TestSvrInit() @@ -1316,25 +1303,25 @@ class SvrTest : public ::testing::Test { SvmParameter param = getDefaultSvmParameter(); param.svmType = EPSILON_SVR; SmoSolver smo(handle, param, nullptr); - smo.SvrInit(y_dev, n_rows, yc, f); + smo.SvrInit(y_dev.data(), n_rows, yc.data(), f.data()); - EXPECT_TRUE(devArrMatchHost(yc_exp, yc, n_train, raft::CompareApprox(1.0e-9), stream)); - EXPECT_TRUE(devArrMatchHost(f_exp, f, n_train, raft::Compare(), stream)); + EXPECT_TRUE( + devArrMatchHost(yc_exp, yc.data(), n_train, raft::CompareApprox(1.0e-9), stream)); + EXPECT_TRUE(devArrMatchHost(f_exp, f.data(), n_train, raft::Compare(), stream)); } void TestSvrWorkingSet() { - auto stream = this->handle.get_stream(); - init_C((math_t)1.0, C_dev, 2 * n_rows, stream); + init_C((math_t)1.0, C_dev.data(), 2 * n_rows, stream); WorkingSet* ws; ws = new WorkingSet(handle, stream, n_rows, 20, EPSILON_SVR); EXPECT_EQ(ws->GetSize(), 2 * n_rows); - raft::update_device(alpha, alpha_host, n_train, stream); - raft::update_device(f, f_exp, n_train, stream); - raft::update_device(yc, yc_exp, n_train, stream); + raft::update_device(alpha.data(), alpha_host, n_train, stream); + raft::update_device(f.data(), f_exp, n_train, stream); + raft::update_device(yc.data(), yc_exp, n_train, stream); - ws->Select(f, alpha, yc, C_dev); + ws->Select(f.data(), alpha.data(), yc.data(), C_dev.data()); int exp_idx[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; ASSERT_TRUE( devArrMatchHost(exp_idx, ws->GetIndices(), ws->GetSize(), raft::Compare(), stream)); @@ -1343,7 +1330,7 @@ class SvrTest : public ::testing::Test { ws = new WorkingSet(handle, stream, n_rows, 10, EPSILON_SVR); EXPECT_EQ(ws->GetSize(), 10); - ws->Select(f, alpha, yc, C_dev); + ws->Select(f.data(), alpha.data(), yc.data(), C_dev.data()); int exp_idx2[] = {6, 12, 5, 11, 3, 9, 8, 1, 7, 0}; ASSERT_TRUE( devArrMatchHost(exp_idx2, ws->GetIndices(), ws->GetSize(), raft::Compare(), stream)); @@ -1352,16 +1339,15 @@ class SvrTest : public ::testing::Test { void TestSvrResults() { - auto stream = this->handle.get_stream(); - raft::update_device(yc, yc_exp, n_train, stream); - init_C((math_t)0.001, C_dev, n_rows * 2, stream); - Results res(handle, x_dev, yc, n_rows, n_cols, C_dev, EPSILON_SVR); + raft::update_device(yc.data(), yc_exp, n_train, stream); + init_C((math_t)0.001, C_dev.data(), n_rows * 2, stream); + Results res(handle, x_dev.data(), yc.data(), n_rows, n_cols, C_dev.data(), EPSILON_SVR); model.n_cols = n_cols; - raft::update_device(alpha, alpha_host, n_train, stream); - raft::update_device(f, f_exp, n_train, stream); + raft::update_device(alpha.data(), alpha_host, n_train, stream); + raft::update_device(f.data(), f_exp, n_train, stream); - res.Get(alpha, - f, + res.Get(alpha.data(), + f.data(), &model.dual_coefs, &model.n_support, &model.support_idx, @@ -1458,9 +1444,7 @@ class SvrTest : public ::testing::Test { auto p = d.first; auto exp = d.second; SCOPED_TRACE(p); - rmm::device_uvector x_dev(p.n_rows * p.n_cols, stream); raft::update_device(x_dev.data(), p.x.data(), p.n_rows * p.n_cols, stream); - rmm::device_uvector y_dev(p.n_rows, stream); raft::update_device(y_dev.data(), p.y.data(), p.n_rows, stream); rmm::device_uvector sample_weights_dev(0, stream); math_t* sample_weights = nullptr; @@ -1501,18 +1485,18 @@ class SvrTest : public ::testing::Test { protected: raft::handle_t handle; - int n_rows = 7; - int n_train = 2 * n_rows; - const int n_cols = 1; + cudaStream_t stream = 0; + int n_rows = 7; + int n_train = 2 * n_rows; + const int n_cols = 1; SvmModel model; - math_t* x_dev; - math_t* y_dev; - math_t* C_dev; - math_t* y_pred; - math_t* yc; - math_t* f; - math_t* alpha; + rmm::device_uvector x_dev; + rmm::device_uvector y_dev; + rmm::device_uvector C_dev; + rmm::device_uvector yc; + rmm::device_uvector f; + rmm::device_uvector alpha; math_t x_host[7] = {1, 2, 3, 4, 5, 6, 7}; math_t y_host[7] = {0, 2, 3, 4, 5, 6, 8}; diff --git a/cpp/test/sg/tsvd_test.cu b/cpp/test/sg/tsvd_test.cu index c04030019a..7b05047a0f 100644 --- a/cpp/test/sg/tsvd_test.cu +++ b/cpp/test/sg/tsvd_test.cu @@ -47,30 +47,41 @@ template template class TsvdTest : public ::testing::TestWithParam> { + public: + TsvdTest() + : params(::testing::TestWithParam>::GetParam()), + stream(handle.get_stream()), + components(0, stream), + components_ref(0, stream), + data2(0, stream), + data2_back(0, stream) + { + basicTest(); + advancedTest(); + } + protected: void basicTest() { - auto stream = handle.get_stream(); - params = ::testing::TestWithParam>::GetParam(); raft::random::Rng r(params.seed, raft::random::GenTaps); int len = params.len; - raft::allocate(data, len, stream); + rmm::device_uvector data(len, stream); std::vector data_h = {1.0, 2.0, 4.0, 2.0, 4.0, 5.0, 5.0, 4.0, 2.0, 1.0, 6.0, 4.0}; data_h.resize(len); - raft::update_device(data, data_h.data(), len, stream); + raft::update_device(data.data(), data_h.data(), len, stream); int len_comp = params.n_col * params.n_col; - raft::allocate(components, len_comp, stream); - raft::allocate(singular_vals, params.n_col, stream); + components.resize(len_comp, stream); + rmm::device_uvector singular_vals(params.n_col, stream); std::vector components_ref_h = { -0.3951, 0.1532, 0.9058, -0.7111, -0.6752, -0.1959, -0.5816, 0.7215, -0.3757}; components_ref_h.resize(len_comp); - raft::allocate(components_ref, len_comp, stream); - raft::update_device(components_ref, components_ref_h.data(), len_comp, stream); + components_ref.resize(len_comp, stream); + raft::update_device(components_ref.data(), components_ref_h.data(), len_comp, stream); paramsTSVD prms; prms.n_cols = params.n_col; @@ -81,13 +92,11 @@ class TsvdTest : public ::testing::TestWithParam> { else prms.algorithm = solver::COV_EIG_JACOBI; - tsvdFit(handle, data, components, singular_vals, prms, stream); + tsvdFit(handle, data.data(), components.data(), singular_vals.data(), prms, stream); } void advancedTest() { - auto stream = handle.get_stream(); - params = ::testing::TestWithParam>::GetParam(); raft::random::Rng r(params.seed, raft::random::GenTaps); int len = params.len2; @@ -102,57 +111,37 @@ class TsvdTest : public ::testing::TestWithParam> { else prms.n_components = params.n_col2 - 15; - raft::allocate(data2, len, stream); - r.uniform(data2, len, T(-1.0), T(1.0), stream); - raft::allocate(data2_trans, prms.n_rows * prms.n_components, stream); + data2.resize(len, stream); + r.uniform(data2.data(), len, T(-1.0), T(1.0), stream); + rmm::device_uvector data2_trans(prms.n_rows * prms.n_components, stream); int len_comp = params.n_col2 * prms.n_components; - raft::allocate(components2, len_comp, stream); - raft::allocate(explained_vars2, prms.n_components, stream); - raft::allocate(explained_var_ratio2, prms.n_components, stream); - raft::allocate(singular_vals2, prms.n_components, stream); + rmm::device_uvector components2(len_comp, stream); + rmm::device_uvector explained_vars2(prms.n_components, stream); + rmm::device_uvector explained_var_ratio2(prms.n_components, stream); + rmm::device_uvector singular_vals2(prms.n_components, stream); tsvdFitTransform(handle, - data2, - data2_trans, - components2, - explained_vars2, - explained_var_ratio2, - singular_vals2, + data2.data(), + data2_trans.data(), + components2.data(), + explained_vars2.data(), + explained_var_ratio2.data(), + singular_vals2.data(), prms, stream); - raft::allocate(data2_back, len, stream); - tsvdInverseTransform(handle, data2_trans, components2, data2_back, prms, stream); - } - - void SetUp() override - { - basicTest(); - advancedTest(); - } - - void TearDown() override - { - CUDA_CHECK(cudaFree(data)); - CUDA_CHECK(cudaFree(components)); - CUDA_CHECK(cudaFree(singular_vals)); - CUDA_CHECK(cudaFree(components_ref)); - CUDA_CHECK(cudaFree(data2)); - CUDA_CHECK(cudaFree(data2_trans)); - CUDA_CHECK(cudaFree(data2_back)); - CUDA_CHECK(cudaFree(components2)); - CUDA_CHECK(cudaFree(explained_vars2)); - CUDA_CHECK(cudaFree(explained_var_ratio2)); - CUDA_CHECK(cudaFree(singular_vals2)); + data2_back.resize(len, stream); + tsvdInverseTransform( + handle, data2_trans.data(), components2.data(), data2_back.data(), prms, stream); } protected: - TsvdInputs params; - T *data, *components, *singular_vals, *components_ref, *explained_vars_ref; - T *data2, *data2_trans, *data2_back, *components2, *explained_vars2, *explained_var_ratio2, - *singular_vals2; raft::handle_t handle; + cudaStream_t stream = 0; + + TsvdInputs params; + rmm::device_uvector components, components_ref, data2, data2_back; }; const std::vector> inputsf2 = { @@ -170,8 +159,8 @@ const std::vector> inputsd2 = { typedef TsvdTest TsvdTestLeftVecF; TEST_P(TsvdTestLeftVecF, Result) { - ASSERT_TRUE(raft::devArrMatch(components, - components_ref, + ASSERT_TRUE(raft::devArrMatch(components.data(), + components_ref.data(), (params.n_col * params.n_col), raft::CompareApproxAbs(params.tolerance), handle.get_stream())); @@ -180,8 +169,8 @@ TEST_P(TsvdTestLeftVecF, Result) typedef TsvdTest TsvdTestLeftVecD; TEST_P(TsvdTestLeftVecD, Result) { - ASSERT_TRUE(raft::devArrMatch(components, - components_ref, + ASSERT_TRUE(raft::devArrMatch(components.data(), + components_ref.data(), (params.n_col * params.n_col), raft::CompareApproxAbs(params.tolerance), handle.get_stream())); @@ -190,8 +179,8 @@ TEST_P(TsvdTestLeftVecD, Result) typedef TsvdTest TsvdTestDataVecF; TEST_P(TsvdTestDataVecF, Result) { - ASSERT_TRUE(raft::devArrMatch(data2, - data2_back, + ASSERT_TRUE(raft::devArrMatch(data2.data(), + data2_back.data(), (params.n_col2 * params.n_col2), raft::CompareApproxAbs(params.tolerance), handle.get_stream())); @@ -200,8 +189,8 @@ TEST_P(TsvdTestDataVecF, Result) typedef TsvdTest TsvdTestDataVecD; TEST_P(TsvdTestDataVecD, Result) { - ASSERT_TRUE(raft::devArrMatch(data2, - data2_back, + ASSERT_TRUE(raft::devArrMatch(data2.data(), + data2_back.data(), (params.n_col2 * params.n_col2), raft::CompareApproxAbs(params.tolerance), handle.get_stream()));