-
Notifications
You must be signed in to change notification settings - Fork 75
Add PSO training mode to ScalarQuantizationTrainer #966
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
Open
HuXin0817
wants to merge
13
commits into
antgroup:main
Choose a base branch
from
HuXin0817:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0f546fa
Add PSO training mode to ScalarQuantizationTrainer
2865df3
Add unit tests for ScalarQuantizationTrainer, validating different tr…
b97ee0e
rollback default training mode in ScalarQuantizationTrainer from PSO …
HuXin0817 bf3cd4a
Add additional checks in ScalarQuantizationTrainer tests to ensure lo…
HuXin0817 6dc63ee
Integrate classic training method into PSO training in ScalarQuantiza…
HuXin0817 aadd4aa
Add pso_train_impl method to ScalarQuantizationTrainer for improved P…
HuXin0817 adff012
Refactor pso_train and pso_train_impl in ScalarQuantizationTrainer to…
HuXin0817 136e871
Fix parameter order in ScalarQuantizationTrainer tests for CLASSIC, T…
HuXin0817 bde4b2d
Update ScalarQuantizationTrainer tests to use non-strict inequality f…
HuXin0817 080f3a6
Refactor PSO training implementation in ScalarQuantizationTrainer to …
HuXin0817 e883f4e
Update ScalarQuantizationTrainer tests to use uniform distribution fo…
HuXin0817 dcf0253
Refactor access modifiers in ScalarQuantizationTrainer header to impr…
HuXin0817 a292865
Refactor ScalarQuantizationTrainer tests to utilize a unified Train m…
HuXin0817 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
74 changes: 74 additions & 0 deletions
74
src/quantization/scalar_quantization/scalar_quantization_trainer_test.cpp
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,74 @@ | ||
|
|
||
| // Copyright 2024-present the vsag project | ||
| // | ||
| // 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 "scalar_quantization_trainer.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <catch2/catch_message.hpp> | ||
| #include <catch2/catch_test_macros.hpp> | ||
| #include <cstdint> | ||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <numeric> | ||
| #include <random> | ||
| #include <vector> | ||
|
|
||
| using namespace vsag; | ||
|
|
||
| float | ||
| compute_mse(const std::vector<float>& data, float lower, float upper, int bits) { | ||
| float div = (1 << bits) - 1; | ||
| float step = (upper - lower) / div; | ||
| float mse = 0.0f; | ||
| for (float v : data) { | ||
| float code = std::round((v - lower) / step); | ||
| code = std::min(std::max(code, 0.0f), div); | ||
| float recon = lower + code * step; | ||
| mse += (v - recon) * (v - recon); | ||
| } | ||
| return mse / data.size(); | ||
| } | ||
|
|
||
| TEST_CASE("ScalarQuantizationTrainer", "[ft][scalar_quantization_trainer]") { | ||
| std::vector<float> data; | ||
| std::mt19937 gen(42); | ||
| std::uniform_real_distribution<float> dist(0.0f, 1.0f); | ||
| for (int i = 0; i < 1000; ++i) { | ||
| data.push_back(dist(gen)); | ||
| } | ||
| int bits = 4; | ||
| float lower_c[1], upper_c[1], lower_t[1], upper_t[1], lower_p[1], upper_p[1]; | ||
|
|
||
| ScalarQuantizationTrainer trainer(1, bits); | ||
|
|
||
| // CLASSIC | ||
| trainer.Train(data.data(), data.size(), upper_c, lower_c, false, vsag::CLASSIC); | ||
| float mse_classic = compute_mse(data, lower_c[0], upper_c[0], bits); | ||
|
|
||
| // TRUNC_BOUND | ||
| trainer.Train(data.data(), data.size(), upper_t, lower_t, false, vsag::TRUNC_BOUND); | ||
| float mse_trunc = compute_mse(data, lower_t[0], upper_t[0], bits); | ||
|
|
||
| // PSO | ||
| trainer.Train(data.data(), data.size(), upper_p, lower_p, false, vsag::PSO); | ||
| float mse_pso = compute_mse(data, lower_p[0], upper_p[0], bits); | ||
|
|
||
| REQUIRE(lower_c <= upper_c); | ||
| REQUIRE(lower_t <= upper_t); | ||
| REQUIRE(lower_p <= upper_p); | ||
|
|
||
| REQUIRE(mse_pso < mse_classic * 0.95); | ||
| REQUIRE(mse_pso < mse_trunc); | ||
| } |
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.
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.
It is suggested to split this long function into smaller functions and add unit tests (UT) to cover each one of them
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.
It would be difficult to split it up, so I implemented a
pso_train_implfunction that can pass training parameters to facilitate possible testing later.