Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
avolkov-intel committed Aug 29, 2024
1 parent aaeada8 commit a48b92e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ services::Status SpectralEmbeddingKernel<algorithmFPType, method, cpu>::compute(
NumericTable * eigenTable, const KernelParameter & par)
{
services::Status status;
std::cout << "inside DAAL kernel" << std::endl;
std::cout << "Params: " << par.numberOfEmbeddings << " " << par.numberOfNeighbors << std::endl;
// std::cout << "inside DAAL kernel" << std::endl;
// std::cout << "Params: " << par.numberOfEmbeddings << " " << par.numberOfNeighbors << std::endl;
size_t k = par.numberOfEmbeddings;
size_t filtNum = par.numberOfNeighbors + 1;
size_t n = xTable->getNumberOfRows(); /* Number of input feature vectors */
Expand Down Expand Up @@ -162,12 +162,14 @@ services::Status SpectralEmbeddingKernel<algorithmFPType, method, cpu>::compute(
}
}

// std::cout << "Laplacian matrix" << std::endl;
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < n; ++j) {
// std::cout << x[i * n + j] << " ";
// }
// std::cout << std::endl;
// }
// std::cout << "------" << std::endl;

// Find the eigen vectors and eigne values of the matix
//TArray<algorithmFPType, cpu> eigenvalues(n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ static compute_result<Task> call_daal_kernel(const context_cpu& ctx,
const table& data) {
const auto daal_data = interop::convert_to_daal_table<Float>(data);

const std::int64_t p = data.get_column_count();
// const std::int64_t p = data.get_column_count();
const std::int64_t n = data.get_row_count();
std::int64_t k = desc.get_component_count();

std::cout << "inside oneDAL kernel: " << n << " " << p << std::endl;
// std::cout << "inside oneDAL kernel: " << n << " " << p << std::endl;

auto result = compute_result<Task>{}.set_result_options(desc.get_result_options());

Expand Down
6 changes: 2 additions & 4 deletions cpp/oneapi/dal/algo/spectral_embedding/test/batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ class spectral_embedding_batch_test
};

TEMPLATE_LIST_TEST_M(spectral_embedding_batch_test,
"spectral_embedding tests",
"spectral_embedding gold test",
"[spectral embedding][integration][cpu]",
spectral_embedding_types) {
SKIP_IF(this->not_float64_friendly());
SKIP_IF(this->get_policy().is_gpu());

this->gen_dimensions();
this->gen_input();
this->test_default();
this->test_gold_input();
}

} // namespace oneapi::dal::spectral_embedding::test
82 changes: 64 additions & 18 deletions cpp/oneapi/dal/algo/spectral_embedding/test/fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ class spectral_embedding_test : public te::crtp_algo_fixture<TestType, Derived>
using result_t = sp_emb::compute_result<>;
using descriptor_t = sp_emb::descriptor<Float, Method>;

auto get_descriptor(sp_emb::result_option_id compute_mode) const {
return descriptor_t().set_component_count(4).set_neighbor_count(2).set_result_options(
compute_mode);
auto get_descriptor(std::int64_t component_count,
std::int64_t neighbor_count,
sp_emb::result_option_id compute_mode) const {
return descriptor_t()
.set_component_count(component_count)
.set_neighbor_count(neighbor_count)
.set_result_options(compute_mode);
}

void gen_input() {
Expand All @@ -54,24 +58,66 @@ class spectral_embedding_test : public te::crtp_algo_fixture<TestType, Derived>
data_ = data_df.get_table(this->get_policy(), this->get_homogen_table_id());
}

void test_default() {
std::cout << "Input" << std::endl;
// std::cout << data_ << std::endl;
auto desc = get_descriptor(sp_emb::result_options::embedding |
sp_emb::result_options::eigen_values);
//desc.set_component_count(5);
//desc.set_neighbor_count(4);
void test_gold_input(Float tol = 1e-5) {
constexpr std::int64_t n = 8;
constexpr std::int64_t p = 4;
constexpr std::int64_t neighbor_count = 5;
constexpr std::int64_t component_count = 4;

constexpr Float data[n * p] = { 0.49671415, -0.1382643, 0.64768854, 1.52302986,
-0.23415337, -0.23413696, 1.57921282, 0.76743473,
-0.46947439, 0.54256004, -0.46341769, -0.46572975,
0.24196227, -1.91328024, -1.72491783, -0.56228753,
-1.01283112, 0.31424733, -0.90802408, -1.4123037,
1.46564877, -0.2257763, 0.0675282, -1.42474819,
-0.54438272, 0.11092259, -1.15099358, 0.37569802,
-0.60063869, -0.29169375, -0.60170661, 1.85227818 };

constexpr Float gth_embedding[n * component_count] = {
-0.353553391, 0.442842965, 0.190005876, 0.705830111, -0.353553391, 0.604392576,
-0.247517958, -0.595235173, -0.353553391, -0.391745507, 0.0443633719, -0.150208165,
-0.353553391, -0.142548722, 0.0125222995, -0.0318482841, -0.353553391, -0.499390711,
-0.20194266, -0.000639679859, -0.353553391, 0.00809834849, -0.683462258, 0.273398265,
-0.353553391, -0.0977843445, 0.449358299, 0.0195905172, -0.353553391, 0.0761353959,
0.436673029, -0.220887591
};

constexpr Float gth_eigen_vals[n] = { 0, 3.32674524, 4.70361338, 5.26372220,
5.69343808, 6.63074948, 6.80173994, 7.57999167 };

auto desc = get_descriptor(
component_count,
neighbor_count,
sp_emb::result_options::embedding | sp_emb::result_options::eigen_values);

table data_ = homogen_table::wrap(data, n, p);

INFO("run compute");
auto compute_result = this->compute(desc, data_);
check_compute_result(compute_result);
std::cout << "Output" << std::endl;
std::cout << compute_result.get_embedding() << std::endl;
std::cout << "Eigen values:" << std::endl;
std::cout << compute_result.get_eigen_values() << std::endl;
}
auto embedding = compute_result.get_embedding();
// std::cout << "Output" << std::endl;
// std::cout << embedding << std::endl;

array<Float> emb_arr = row_accessor<const Float>(embedding).pull({ 0, -1 });
for (int j = 0; j < component_count; ++j) {
Float diff = 0, diff_rev = 0;
for (int i = 0; i < n; ++i) {
Float val = emb_arr[i * component_count + j];
Float gth_val = gth_embedding[i * component_count + j];
diff = std::max(diff, std::abs(val - gth_val));
diff_rev = std::max(diff_rev, std::abs(val + gth_val));
}
REQUIRE((diff < tol || diff_rev < tol));
}

auto eigen_values = compute_result.get_eigen_values();
// std::cout << "Eigen values:" << std::endl;
// std::cout << eigen_values << std::endl;

void check_compute_result(const spectral_embedding::compute_result<>& result) {
array<Float> data_arr = row_accessor<const Float>(data_).pull({ 0, -1 });
array<Float> eig_val_arr = row_accessor<const Float>(eigen_values).pull({ 0, -1 });
for (int i = 0; i < n; ++i) {
REQUIRE(std::abs(eig_val_arr[i] - gth_eigen_vals[i]) < tol);
}
}

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ int main(int argc, char const *argv[]) {
dal::spectral_embedding::descriptor<>()
.set_neighbor_count(n_neighbors)
.set_component_count(num_spks)
.set_result_options(dal::spectral_embedding::result_options::embedding);
.set_result_options(dal::spectral_embedding::result_options::embedding |
dal::spectral_embedding::result_options::eigen_values);

const auto spectral_embedding_result = dal::compute(spectral_embedding_desc, x_train);

Expand All @@ -63,6 +64,8 @@ int main(int argc, char const *argv[]) {

std::cout << "Spectral embeddings:\n" << spectral_embeddings << std::endl;

std::cout << "Eigen values:\n" << spectral_embedding_result.get_eigen_values() << std::endl;

const auto kmeans_init_desc =
dal::kmeans_init::descriptor<float, dal::kmeans_init::method::plus_plus_dense>()
.set_cluster_count(cluster_count)
Expand Down

0 comments on commit a48b92e

Please sign in to comment.