-
Notifications
You must be signed in to change notification settings - Fork 34
fix: store data members & make faiss tests pass #209
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
base: rfsaliev/cpp-runtime-binding
Are you sure you want to change the base?
Changes from all commits
a3ecf1f
faf3d65
6db834b
7af7137
8d8b99a
32e882e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright 2025 Intel Corporation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
| #include "IndexSVSImplDefs.h" | ||
|
|
||
| #include <iostream> | ||
| #include <memory> | ||
|
|
||
| namespace svs { | ||
| namespace runtime { | ||
|
|
||
| namespace detail { | ||
| struct TrainingInfoImpl; | ||
| } | ||
|
|
||
| struct SVS_RUNTIME_API IndexSVSTrainingInfo { | ||
| IndexSVSTrainingInfo() noexcept = default; | ||
|
|
||
| IndexSVSTrainingInfo(std::unique_ptr<svs::runtime::detail::TrainingInfoImpl> impl | ||
| ) noexcept; | ||
|
|
||
| static void destroy(IndexSVSTrainingInfo* impl) noexcept; | ||
| virtual ~IndexSVSTrainingInfo(); | ||
|
|
||
| Status serialize(std::ostream& out) const noexcept; | ||
| Status deserialize(std::istream& in) noexcept; | ||
|
|
||
| protected: | ||
| std::unique_ptr<svs::runtime::detail::TrainingInfoImpl> impl_{nullptr}; | ||
| }; | ||
|
|
||
| } // namespace runtime | ||
| } // namespace svs |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright 2025 Intel Corporation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <svs/leanvec/leanvec.h> | ||
|
|
||
| #include <iostream> | ||
| #include <memory> | ||
| #include <span> | ||
|
|
||
| namespace svs { | ||
| namespace runtime { | ||
| namespace detail { | ||
|
|
||
| struct TrainingInfoImpl { | ||
| virtual ~TrainingInfoImpl() = default; | ||
|
|
||
| virtual void serialize(std::ostream& out) const = 0; | ||
| virtual void deserialize(std::istream& in) = 0; | ||
| }; | ||
|
|
||
| // TrainingInfo wrapper around pre-computed leanvec matrix | ||
| struct LeanVecTrainingInfoImpl : public TrainingInfoImpl { | ||
| LeanVecTrainingInfoImpl(svs::leanvec::LeanVecMatrices<svs::Dynamic> matrix); | ||
|
|
||
| void serialize(std::ostream& out) const override; | ||
| void deserialize(std::istream& in) override; | ||
|
|
||
| svs::leanvec::LeanVecMatrices<svs::Dynamic> leanvec_matrix; | ||
| }; | ||
|
|
||
| } // namespace detail | ||
| } // namespace runtime | ||
| } // namespace svs |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright 2025 Intel Corporation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "IndexSVSTrainingInfo.h" | ||
| #include "detail/TrainingInfoImpl.h" | ||
|
|
||
| namespace svs { | ||
| namespace runtime { | ||
|
|
||
| IndexSVSTrainingInfo::IndexSVSTrainingInfo( | ||
| std::unique_ptr<svs::runtime::detail::TrainingInfoImpl> impl | ||
| ) noexcept | ||
| : impl_(std::move(impl)) {} | ||
|
|
||
| void IndexSVSTrainingInfo::destroy(IndexSVSTrainingInfo* impl) noexcept { delete impl; } | ||
|
|
||
| Status IndexSVSTrainingInfo::serialize(std::ostream& out) const noexcept { | ||
| if (impl_ == nullptr) { | ||
| return Status_Ok; | ||
| } | ||
| try { | ||
| impl_->serialize(out); | ||
| } catch (std::exception& e) { | ||
| return Status{ErrorCode::IO_ERROR, e.what()}; | ||
| } catch (...) { | ||
| return Status{ErrorCode::IO_ERROR, "Failed to serialize IndexSVSTrainingInfo"}; | ||
| } | ||
| return Status_Ok; | ||
| } | ||
|
|
||
| Status IndexSVSTrainingInfo::deserialize(std::istream& in) noexcept { | ||
| if (!impl_) { | ||
| return Status_Ok; | ||
| } | ||
| try { | ||
| impl_->deserialize(in); | ||
| } catch (std::exception& e) { | ||
| return Status{ErrorCode::IO_ERROR, e.what()}; | ||
| } catch (...) { | ||
| return Status{ErrorCode::IO_ERROR, "Failed to deserialize IndexSVSTrainingInfo"}; | ||
| } | ||
| return Status_Ok; | ||
| } | ||
|
|
||
| } // namespace runtime | ||
| } // namespace svs |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,13 +171,31 @@ Status IndexSVSVamanaLVQImpl::init_impl(size_t n, const float* x) noexcept { | |
| ); | ||
| } | ||
|
|
||
| Status IndexSVSVamanaLVQImpl::serialize_impl(std::ostream& out) const noexcept { | ||
| // Also store LVQ specific members | ||
| out.write(reinterpret_cast<const char*>(&lvq_level), sizeof(LVQLevel)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So far, to make everything consistent, shouldn't we save There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, I guess it makes sense to add the public members too. |
||
|
|
||
| // This will also write whether or not we're initialized | ||
| return IndexSVSVamanaImpl::serialize_impl(out); | ||
|
|
||
| return Status_Ok; | ||
| } | ||
|
|
||
| Status IndexSVSVamanaLVQImpl::deserialize_impl(std::istream& in) noexcept { | ||
| if (impl) { | ||
| return Status{ | ||
| ErrorCode::INVALID_ARGUMENT, | ||
| "Cannot deserialize: SVS index already initialized."}; | ||
| } | ||
|
|
||
| in.read(reinterpret_cast<char*>(&lvq_level), sizeof(LVQLevel)); | ||
|
|
||
| bool initialized = false; | ||
| in.read(reinterpret_cast<char*>(&initialized), sizeof(bool)); | ||
| if (!initialized) { | ||
| return Status_Ok; | ||
| } | ||
|
|
||
| if (svs::detail::intel_enabled()) { | ||
| switch (lvq_level) { | ||
| case LVQLevel::LVQ4x0: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please explain, why we have to serializae/deserialize empty inidices?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the use cases in this test
https://github.com/ahuber21/faiss/blob/b709fa114afc522b3d10ffd1356df1d9a9548951/tests/test_svs.cpp#L111
which is created to validate the scenario in this issue
ahuber21/faiss#37
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @ahuber21 for the problem description
After some time of investigation ana analysis, I got understanding, that inside the implementation code in SVS side we do not need to call
DynamicVamana::save(std::ostream&)anymore, instead we can use filesystem-basedDynamicVamana::save(std::filesystem::path, ...)method which allows to manage serialization in a more controlled and simpler way.