Repository navigation
[C API] Expose leanvec ood #358
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
77914de
rebase
ethanglaser 8e47f7e
[C API] LeanVec training data handling refactoring (#378)
rfsaliev 2a985c4
Address review comments and move training matrices computation to the…
rfsaliev a38e7af
Merge branch 'main' into dev/eglaser-leanvec-ood-capi
rfsaliev 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
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
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,61 @@ | ||
| /* | ||
| * Copyright 2026 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. | ||
| */ | ||
|
|
||
| #ifdef SVS_RUNTIME_ENABLE_LVQ_LEANVEC | ||
|
|
||
| #include "leanvec_training_data.hpp" | ||
|
|
||
| #include "error.hpp" | ||
|
|
||
| #include <svs/core/medioid.h> | ||
| #include <svs/lib/static.h> | ||
|
|
||
| namespace svs::c_runtime { | ||
|
|
||
| LeanVecTrainingData::matrices_type LeanVecTrainingData::compute_matrices( | ||
| svs::data::ConstSimpleDataView<float> data, | ||
| svs::data::ConstSimpleDataView<float> queries, | ||
| size_t leanvec_dims, | ||
| svs::threads::ThreadPoolHandle& pool | ||
| ) { | ||
| if (!enabled()) { | ||
| throw svs::c_runtime::unsupported_hw( | ||
| "LeanVec training is not supported on this hardware" | ||
| ); | ||
| } | ||
|
|
||
| // If queries are provided, compute out-of-distribution (OOD) matrices. | ||
| if (queries.size() > 0) { | ||
| return svs::leanvec::compute_leanvec_matrices_ood<svs::Dynamic>( | ||
| data, queries, pool, svs::lib::MaybeStatic{leanvec_dims} | ||
| ); | ||
| } | ||
|
|
||
| // Else: PCA path (in-distribution) | ||
| auto means = svs::utils::compute_medioid(data, pool); | ||
| auto matrix = svs::leanvec::compute_leanvec_matrix<svs::Dynamic, svs::Dynamic>( | ||
| data, means, pool, svs::lib::MaybeStatic{leanvec_dims} | ||
| ); | ||
| // A copy is used for the query matrix: in PCA mode data and query | ||
| // transforms are identical, and passing the same object twice trips | ||
| // use-after-move warnings and DenseArray double-free issues. | ||
| auto query_matrix = matrix; | ||
| return matrices_type{std::move(matrix), std::move(query_matrix)}; | ||
| } | ||
|
|
||
| } // namespace svs::c_runtime | ||
|
|
||
| #endif // SVS_RUNTIME_ENABLE_LVQ_LEANVEC |
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,85 @@ | ||
| /* | ||
| * Copyright 2026 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 | ||
|
|
||
| #ifdef SVS_RUNTIME_ENABLE_LVQ_LEANVEC | ||
|
|
||
| #include "svs/c/svs_c.h" | ||
|
|
||
| #include <svs/core/data/simple.h> | ||
| #include <svs/cpuid.h> | ||
| #include <svs/lib/threads/threadpool.h> | ||
|
|
||
| #ifdef SVS_LEANVEC_HEADER | ||
| #include SVS_LEANVEC_HEADER | ||
| #else | ||
| #include <svs/leanvec/leanvec.h> | ||
| #endif | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| namespace svs::c_runtime { | ||
|
|
||
| // Holds LeanVec dimensionality-reduction matrices trained from a data sample. | ||
| // Mirrors the runtime bindings' LeanVecTrainingData: matrices are computed once | ||
| // and later handed to LeanVecDataBuilder to reduce the dataset. When training | ||
| // queries are supplied the matrices are learned out-of-distribution (OOD), | ||
| // otherwise in-distribution (PCA) matrices are used for both data and queries. | ||
| class LeanVecTrainingData { | ||
| public: | ||
| using matrices_type = svs::leanvec::LeanVecMatrices<svs::Dynamic>; | ||
|
|
||
| LeanVecTrainingData( | ||
| svs::data::ConstSimpleDataView<float> data, | ||
| svs::data::ConstSimpleDataView<float> queries, | ||
| size_t leanvec_dims, | ||
| svs::threads::ThreadPoolHandle& pool | ||
| ) | ||
| : matrices_{compute_matrices(data, queries, leanvec_dims, pool)} {} | ||
|
|
||
| size_t leanvec_dims() const { return matrices_.num_cols(); } | ||
| const matrices_type& matrices() const { return matrices_; } | ||
|
|
||
| static bool enabled() { return svs::detail::intel_enabled(); } | ||
|
|
||
| private: | ||
| matrices_type matrices_; | ||
|
|
||
| static matrices_type compute_matrices( | ||
| svs::data::ConstSimpleDataView<float> data, | ||
| svs::data::ConstSimpleDataView<float> queries, | ||
| size_t leanvec_dims, | ||
| svs::threads::ThreadPoolHandle& pool | ||
| ); | ||
| }; | ||
|
|
||
| } // namespace svs::c_runtime | ||
|
|
||
| #else // SVS_RUNTIME_ENABLE_LVQ_LEANVEC | ||
| namespace svs::c_runtime { | ||
| class LeanVecTrainingData { | ||
| public: | ||
| template <typename... Args> LeanVecTrainingData(Args&&...) { | ||
| throw svs::c_runtime::not_implemented( | ||
| "LeanVec training is not implemented in this build" | ||
| ); | ||
| } | ||
|
|
||
| static bool enabled() { return false; } | ||
| }; | ||
| } // namespace svs::c_runtime | ||
|
|
||
| #endif // SVS_RUNTIME_ENABLE_LVQ_LEANVEC |
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.
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.