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

Add Range Validation for SQFP16 #1493

Conversation

naveentatikonda
Copy link
Member

@naveentatikonda naveentatikonda commented Feb 21, 2024

Description

This PR adds range validation for Faiss SQFP16 where by default it validates the user input if it is in the FP16 range [-65504.0, 65504.0] and if it is out of range throws an exception. Users can still provide data that is out of fp16 range which will be clipped to fp16 range(without throwing any exception) when they set this encoder parameter, clip as true.

{
    "name": "sq",
    "parameters": {
           "type": "fp16",
           "clip": true
      }
 } 

Issues Resolved

#1138

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed as per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Copy link

codecov bot commented Feb 21, 2024

Codecov Report

Attention: Patch coverage is 71.42857% with 16 lines in your changes are missing coverage. Please review.

Project coverage is 84.99%. Comparing base (7a144b8) to head (e84d598).

Files Patch % Lines
...nsearch/knn/index/mapper/KNNVectorFieldMapper.java 72.72% 6 Missing and 3 partials ⚠️
.../main/java/org/opensearch/knn/index/Parameter.java 36.36% 5 Missing and 2 partials ⚠️
Additional details and impacted files
@@                    Coverage Diff                     @@
##             feature/faiss_sqfp16    #1493      +/-   ##
==========================================================
- Coverage                   85.14%   84.99%   -0.16%     
- Complexity                   1281     1295      +14     
==========================================================
  Files                         168      168              
  Lines                        5232     5286      +54     
  Branches                      495      510      +15     
==========================================================
+ Hits                         4455     4493      +38     
- Misses                        570      580      +10     
- Partials                      207      213       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@naveentatikonda
Copy link
Member Author

The failing Rolling Upgrade Tests and CI on Windows is not related to these changes, created an issue to track it #1494

Copy link
Member

@jmazanec15 jmazanec15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of clip_to_range, what about truncate? Is clip used anywhere else?

@naveentatikonda
Copy link
Member Author

Instead of clip_to_range, what about truncate? Is clip used anywhere else?

I took numpy as a reference, where they call it as clip. As discussed offline will rename it from clip_to_range to clip.

@naveentatikonda naveentatikonda force-pushed the add_fp16_range_validation branch 3 times, most recently from abf7436 to c20750e Compare March 8, 2024 21:13
@naveentatikonda naveentatikonda force-pushed the add_fp16_range_validation branch from c20750e to 62c8e29 Compare March 8, 2024 21:16
@naveentatikonda naveentatikonda changed the base branch from main to feature/faiss_sqfp16 March 11, 2024 17:29
@vibrantvarun
Copy link
Member

Can we add bwc tests here?

src/main/java/org/opensearch/knn/index/Parameter.java Outdated Show resolved Hide resolved
MethodComponentContext methodComponentContext = (MethodComponentContext) methodComponentParams.get(
METHOD_ENCODER_PARAMETER
);
if (ENCODER_SQ.equals(methodComponentContext.getName())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this case can be combined with a previous one if (methodComponentParams.containsKey(METHOD_ENCODER_PARAMETER)) {, maybe put it to it's own method if it became hard to read

@SneakyThrows
public void testHNSWSQFP16_whenIndexedWithOutOfFP16Range_thenThrowException() {
String indexName = "test-index-sqfp16";
String fieldName = "test-field-sqfp16";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think those two fields should be declared at the class level

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that class, all the test variables are initialized at test level. So, tried to maintain consistency.

@navneet1v
Copy link
Collaborator

@naveentatikonda what is clip parameter going to do?

Signed-off-by: Naveen Tatikonda <[email protected]>
@naveentatikonda naveentatikonda force-pushed the add_fp16_range_validation branch from 62c8e29 to 06dc3b1 Compare March 11, 2024 21:53
@naveentatikonda naveentatikonda force-pushed the add_fp16_range_validation branch from 50adf0d to e90fa0d Compare March 11, 2024 23:20
@naveentatikonda
Copy link
Member Author

@naveentatikonda what is clip parameter going to do?

If the clip parameter is set to true and if any of the input vector is outside of the FP16 range, it will be clipped to FP16 range.

Like if the vector value is > 65504.0, then it is indexed as 65504.0
if the vector value is < -65504.0, then it is indexed as -65504.0

@navneet1v
Copy link
Collaborator

@naveentatikonda what is clip parameter going to do?

If the clip parameter is set to true and if any of the input vector is outside of the FP16 range, it will be clipped to FP16 range.

Like if the vector value is > 65504.0, then it is indexed as 65504.0 if the vector value is < -65504.0, then it is indexed as -65504.0

So anything outside the range of fp16 is actually getting mapping to 1 single value. Can we do something better here? I am pretty unsure about this. What experiments we have done to understand the impact of this clipping?

cc: @jmazanec15 , @vamshin

@naveentatikonda
Copy link
Member Author

naveentatikonda commented Mar 12, 2024

@naveentatikonda what is clip parameter going to do?

If the clip parameter is set to true and if any of the input vector is outside of the FP16 range, it will be clipped to FP16 range.
Like if the vector value is > 65504.0, then it is indexed as 65504.0 if the vector value is < -65504.0, then it is indexed as -65504.0

So anything outside the range of fp16 is actually getting mapping to 1 single value. Can we do something better here? I am pretty unsure about this. What experiments we have done to understand the impact of this clipping?

cc: @jmazanec15 , @vamshin

Clipping out of range values to MIN or MAX values is also one of the standard quantization technique and we implemented this after discussing with Science team. Do you have any suggestions to implement it even better ?

As of now we didn't run any experiments with clipping as we don't have any standard datasets with data outside of Fp16 range. But, the impact depends on the distribution of vectors and if most of them lies outside of the range then the impact on recall will be high. In such scenarios we won't recommend customers to enable this field or use this feature.

@jmazanec15
Copy link
Member

@navneet1v I think there are a few alternatives, but they probably should be iterated on. In the initial phase, we should not recommend using this version of sqfp16 if the majority of the data lies outside -Float16.Max or Float16.Max. Adding the clip functionality just ensures that if a user does get an occasional value outside of the range, the system will not break or reject the document.

Alternative 1 I can think of is instead of clipping to 1 value, we could clip to 1,000 values and assign based on range outside of float max. Thus, we get a little bit better granularity in our values outside of the Float16 range. However, the benefit of this is probably minuscule and very data set dependent.

Alternative 2 I can think of is to take approach that can be taken with uint8 and Lucene. Where you more or less look at the distribution of the data and come up with a mapping scheme based on this from fp32 to 16-bit. This may offer a better tradeoff, but would require a good amount of work on our side as well as user side and benefit is unknown.

So, I think this approach is fine for an initial release. I like clip-to-max better than clip-to-inf because inf values will result in vector never being returned and I also think that more tricky bugs can be introduced when inf is considered a valid value (think divide by 0 sorts of things and other segfault behavior).

@naveentatikonda
Copy link
Member Author

Alternative 2 I can think of is to take approach that can be taken with uint8 and Lucene. Where you more or less look at the distribution of the data and come up with a mapping scheme based on this from fp32 to 16-bit. This may offer a better tradeoff, but would require a good amount of work on our side as well as user side and benefit is unknown.

@jmazanec15 Do you mean normalizing the complete data and bucket them into the FP16 range ?

@jmazanec15
Copy link
Member

@naveentatikonda yes, but I dont think this is in scope for now

@jmazanec15
Copy link
Member

I think its good to merge to feature branch. Lets add IVF support once #1527 is merged.


private void validateGraphEviction() throws Exception {
// Search every 5 seconds 14 times to confirm graph gets evicted
int intervals = 14;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 14? and 5 seconds?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, I'm not sure. I replicated this from FaissIT. May be for some tests with less number of docs the graphs gets evicted quickly. For some tests with more docs it might take more time. So, checking it multiple times in short duration.

Signed-off-by: Naveen Tatikonda <[email protected]>
@naveentatikonda naveentatikonda force-pushed the add_fp16_range_validation branch from e90fa0d to e84d598 Compare March 14, 2024 02:50
@naveentatikonda naveentatikonda merged commit 8cad13b into opensearch-project:feature/faiss_sqfp16 Mar 14, 2024
48 of 50 checks passed
@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.x failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.x 2.x
# Navigate to the new working tree
cd .worktrees/backport-2.x
# Create a new branch
git switch --create backport/backport-1493-to-2.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 8cad13b9a961048a90c632d8ba71b4ca46490a09
# Push it to GitHub
git push --set-upstream origin backport/backport-1493-to-2.x
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.x

Then, create a pull request where the base branch is 2.x and the compare/head branch is backport/backport-1493-to-2.x.

naveentatikonda added a commit to naveentatikonda/k-NN that referenced this pull request Mar 15, 2024
* Add Range Validation for SQFP16 Vector Data

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add index setting to clip vector data to FP16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add CHANGELOG

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add an encoder parameter to clip fp16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Address Review Comments

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add BWC Tests

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>
naveentatikonda added a commit that referenced this pull request Mar 18, 2024
* Add Range Validation for SQFP16 Vector Data

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add index setting to clip vector data to FP16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add CHANGELOG

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add an encoder parameter to clip fp16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Address Review Comments

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add BWC Tests

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>
naveentatikonda added a commit that referenced this pull request Mar 18, 2024
* Add Range Validation for SQFP16 Vector Data

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add index setting to clip vector data to FP16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add CHANGELOG

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add an encoder parameter to clip fp16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Address Review Comments

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add BWC Tests

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>
naveentatikonda added a commit that referenced this pull request Mar 18, 2024
* Add Range Validation for SQFP16 Vector Data

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add index setting to clip vector data to FP16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add CHANGELOG

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add an encoder parameter to clip fp16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Address Review Comments

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add BWC Tests

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>
naveentatikonda added a commit that referenced this pull request Mar 18, 2024
* Add Range Validation for SQFP16 (#1493)

* Add Range Validation for SQFP16 Vector Data

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add index setting to clip vector data to FP16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add CHANGELOG

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add an encoder parameter to clip fp16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Address Review Comments

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add BWC Tests

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>

* SQFP16 Range Validation for Faiss IVF Models (#1557)

* SQFP16 Range Validation for Faiss IVF Models

Signed-off-by: Naveen Tatikonda <[email protected]>

* Address Review Comments

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>

* Rebase Changes

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>
opensearch-trigger-bot bot pushed a commit that referenced this pull request Mar 18, 2024
* Add Range Validation for SQFP16 (#1493)

* Add Range Validation for SQFP16 Vector Data

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add index setting to clip vector data to FP16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add CHANGELOG

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add an encoder parameter to clip fp16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Address Review Comments

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add BWC Tests

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>

* SQFP16 Range Validation for Faiss IVF Models (#1557)

* SQFP16 Range Validation for Faiss IVF Models

Signed-off-by: Naveen Tatikonda <[email protected]>

* Address Review Comments

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>

* Rebase Changes

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>
(cherry picked from commit d63ce27)
naveentatikonda added a commit that referenced this pull request Mar 18, 2024
* Faiss SQFP16 Range Validation and Clipping (#1562)

* Add Range Validation for SQFP16 (#1493)

* Add Range Validation for SQFP16 Vector Data

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add index setting to clip vector data to FP16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add CHANGELOG

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add an encoder parameter to clip fp16 range

Signed-off-by: Naveen Tatikonda <[email protected]>

* Address Review Comments

Signed-off-by: Naveen Tatikonda <[email protected]>

* Add BWC Tests

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>

* SQFP16 Range Validation for Faiss IVF Models (#1557)

* SQFP16 Range Validation for Faiss IVF Models

Signed-off-by: Naveen Tatikonda <[email protected]>

* Address Review Comments

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>

* Rebase Changes

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>
(cherry picked from commit d63ce27)

* Fix EntityUtils package

Signed-off-by: Naveen Tatikonda <[email protected]>

---------

Signed-off-by: Naveen Tatikonda <[email protected]>
Co-authored-by: Naveen Tatikonda <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants