-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Allow using custom index readers and writers #4180
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7911632
Allow using custom index readers and writers
kaivalnp 5a3ba50
Merge branch 'main' into custom_io_c
kaivalnp 0504d3a
Merge branch 'main' into custom_io_c
kaivalnp 9578d35
Merge branch 'main' into custom_io_c
kaivalnp 62bbb19
Merge branch 'main' into custom_io_c
kaivalnp bbc77c9
Merge branch 'main' into custom_io_c
mnorris11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| // -*- c++ -*- | ||
|
|
||
| #include "io_c.h" | ||
| #include <faiss/impl/io.h> | ||
| #include "../macros_impl.h" | ||
|
|
||
| using faiss::IOReader; | ||
| using faiss::IOWriter; | ||
|
|
||
| struct CustomIOReader : IOReader { | ||
| size_t (*func)(void* ptr, size_t size, size_t nitems) = nullptr; | ||
|
|
||
| CustomIOReader(size_t (*func_in)(void* ptr, size_t size, size_t nitems)); | ||
|
|
||
| size_t operator()(void* ptr, size_t size, size_t nitems) override; | ||
| }; | ||
|
|
||
| CustomIOReader::CustomIOReader( | ||
| size_t (*func_in)(void* ptr, size_t size, size_t nitems)) | ||
| : func(func_in) {} | ||
|
|
||
| size_t CustomIOReader::operator()(void* ptr, size_t size, size_t nitems) { | ||
| return func(ptr, size, nitems); | ||
| } | ||
|
|
||
| int faiss_CustomIOReader_new( | ||
| FaissCustomIOReader** p_out, | ||
| size_t (*func_in)(void* ptr, size_t size, size_t nitems)) { | ||
| try { | ||
| *p_out = reinterpret_cast<FaissCustomIOReader*>( | ||
| new CustomIOReader(func_in)); | ||
| } | ||
| CATCH_AND_HANDLE | ||
| } | ||
|
|
||
| void faiss_CustomIOReader_free(FaissCustomIOReader* obj) { | ||
| delete reinterpret_cast<CustomIOReader*>(obj); | ||
| } | ||
|
|
||
| struct CustomIOWriter : IOWriter { | ||
| size_t (*func)(const void* ptr, size_t size, size_t nitems) = nullptr; | ||
|
|
||
| CustomIOWriter( | ||
| size_t (*func_in)(const void* ptr, size_t size, size_t nitems)); | ||
|
|
||
| size_t operator()(const void* ptr, size_t size, size_t nitems) override; | ||
| }; | ||
|
|
||
| CustomIOWriter::CustomIOWriter( | ||
| size_t (*func_in)(const void* ptr, size_t size, size_t nitems)) | ||
| : func(func_in) {} | ||
|
|
||
| size_t CustomIOWriter::operator()(const void* ptr, size_t size, size_t nitems) { | ||
| return func(ptr, size, nitems); | ||
| } | ||
|
|
||
| int faiss_CustomIOWriter_new( | ||
| FaissCustomIOWriter** p_out, | ||
| size_t (*func_in)(const void* ptr, size_t size, size_t nitems)) { | ||
| try { | ||
| *p_out = reinterpret_cast<FaissCustomIOWriter*>( | ||
| new CustomIOWriter(func_in)); | ||
| } | ||
| CATCH_AND_HANDLE | ||
| } | ||
|
|
||
| void faiss_CustomIOWriter_free(FaissCustomIOWriter* obj) { | ||
| delete reinterpret_cast<CustomIOWriter*>(obj); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| // -*- c -*- | ||
|
|
||
| #ifndef FAISS_IO_C_H | ||
| #define FAISS_IO_C_H | ||
|
|
||
| #include <stddef.h> | ||
| #include "../faiss_c.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| FAISS_DECLARE_CLASS(IOReader) | ||
| FAISS_DECLARE_DESTRUCTOR(IOReader) | ||
|
|
||
| FAISS_DECLARE_CLASS(IOWriter) | ||
| FAISS_DECLARE_DESTRUCTOR(IOWriter) | ||
|
|
||
| /******************************************************* | ||
| * Custom reader + writer | ||
| * | ||
| * Reader and writer which wraps a function pointer, | ||
| * primarily for FFI use. | ||
| *******************************************************/ | ||
|
|
||
| FAISS_DECLARE_CLASS(CustomIOReader) | ||
| FAISS_DECLARE_DESTRUCTOR(CustomIOReader) | ||
|
|
||
| int faiss_CustomIOReader_new( | ||
| FaissCustomIOReader** p_out, | ||
| size_t (*func_in)(void* ptr, size_t size, size_t nitems)); | ||
|
|
||
| FAISS_DECLARE_CLASS(CustomIOWriter) | ||
| FAISS_DECLARE_DESTRUCTOR(CustomIOWriter) | ||
|
|
||
| int faiss_CustomIOWriter_new( | ||
| FaissCustomIOWriter** p_out, | ||
| size_t (*func_in)(const void* ptr, size_t size, size_t nitems)); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.