Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Failed to run c++ examples. #414

Closed
3 tasks done
SheldonFung98 opened this issue May 20, 2024 · 2 comments
Closed
3 tasks done

Bug: Failed to run c++ examples. #414

SheldonFung98 opened this issue May 20, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@SheldonFung98
Copy link

Describe the bug

Thanks for the hard work. I run the provided C++ example:

using namespace unum::usearch;

metric_punned_t metric(256, metric_kind_t::l2sq_k, scalar_kind_t::f32_k);

// If you plan to store more than 4 Billion entries - use `index_dense_big_t`.
// Or directly instantiate the template variant you need - `index_dense_gt<vector_key_t, internal_id_t>`.
index_dense_t index = index_dense_t::make(metric);
float vec[3] = {0.1, 0.3, 0.2};

index.reserve(10); // Pre-allocate memory for 10 vectors
index.add(42, &vec[0]); // Pass a key and a vector
auto results = index.search(&vec[0], 5); // Pass a query and limit number of results

for (std::size_t i = 0; i != results.size(); ++i)
    results[i].element.key, results[i].element.vector, results[i].distance;

It gives an error saying:
error: ‘struct unum::usearch::index_gt<float, long unsigned int, unsigned int, unum::usearch::aligned_allocator_gt<>, unum::usearch::memory_mapping_allocator_gt<64> >::match_t’ has no member named ‘element’

Then I did some modification:

#include <usearch/index.hpp>
#include <usearch/index_dense.hpp>
#include <usearch/index_plugins.hpp>
#include <iostream>


using namespace unum::usearch;
int main(){

metric_punned_t metric(256, metric_kind_t::l2sq_k, scalar_kind_t::f32_k);

// If you plan to store more than 4 Billion entries - use `index_dense_big_t`.
// Or directly instantiate the template variant you need - `index_dense_gt<vector_key_t, internal_id_t>`.
index_dense_t index = index_dense_t::make(metric);
float vec[3] = {0.1, 0.3, 0.2};

index.reserve(10); // Pre-allocate memory for 10 vectors
index.add(42, &vec[0]); // Pass a key and a vector
//auto results = index_.search(&vec[0], 5); // Pass a query and limit number of results

std::uint64_t matched_keys[10] = {0};
float matched_distances[10] = {0};

std::size_t matched_count = index.search(&vec[2], 5).dump_to(matched_keys, matched_distances);  // Pass a query and limit number of results

for (std::size_t i = 0; i != matched_count; ++i)
	std::cout << matched_keys[i] << " " << matched_distances[i] << std::endl;


return 0;
}

It compile successfully, however the output gives:

42 -nan

Could you help me fix it?
Also, is it possible that you could provide an example of using C++ Eigen lib?

Steps to reproduce

At the repo root folder, I add a test.cpp file:

#include <usearch/index.hpp>
#include <usearch/index_dense.hpp>
#include <usearch/index_plugins.hpp>
#include <iostream>


using namespace unum::usearch;
int main(){

metric_punned_t metric(256, metric_kind_t::l2sq_k, scalar_kind_t::f32_k);

// If you plan to store more than 4 Billion entries - use `index_dense_big_t`.
// Or directly instantiate the template variant you need - `index_dense_gt<vector_key_t, internal_id_t>`.
index_dense_t index = index_dense_t::make(metric);
float vec[3] = {0.1, 0.3, 0.2};

index.reserve(10); // Pre-allocate memory for 10 vectors
index.add(42, &vec[0]); // Pass a key and a vector
//auto results = index_.search(&vec[0], 5); // Pass a query and limit number of results

std::uint64_t matched_keys[10] = {0};
float matched_distances[10] = {0};

std::size_t matched_count = index.search(&vec[2], 5).dump_to(matched_keys, matched_distances);  // Pass a query and limit number of results

for (std::size_t i = 0; i != matched_count; ++i)
	std::cout << matched_keys[i] << " " << matched_distances[i] << std::endl;


return 0;
}

Then compile:

g++ -Iinclude -Ifp16/include -o test test.cpp

Expected behavior

Expect to give correct distance value.

USearch version

v2.12.0

Operating System

Ubuntu 22.04

Hardware architecture

x86

Which interface are you using?

C++ implementation

Contact Details

No response

Are you open to being tagged as a contributor?

  • I am open to being mentioned in the project .git history as a contributor

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct
@SheldonFung98 SheldonFung98 added the bug Something isn't working label May 20, 2024
@ashvardanian
Copy link
Contributor

Hey @SheldonFung98! You are creating and index with 256 dimensions and passing a pointer to a buffer with only 3 floats. That's undefined behavior.

error: ‘struct unum::usearch::index_gt<float, long unsigned int, unsigned int, unum::usearch::aligned_allocator_gt<>, unum::usearch::memory_mapping_allocator_gt<64> >::match_t’ has no member named ‘element’

As for this, can you please open a PR patching the documentation? Much appreciated 🤗

@SheldonFung98
Copy link
Author

Appreciate it! I will!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants