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 support for Lucene inbuilt Scalar Quantizer #1848

Conversation

naveentatikonda
Copy link
Member

@naveentatikonda naveentatikonda commented Jul 17, 2024

Description

This PR adds support for inbuilt Scalar Quantizer with Lucene engine which takes fp32 vectors as input and dynamically quantizes them based on the number of bits(default 7) and other parameters like confidence_interval provided in the index mapping. For now, we are not supporting 8 bits due to a recall issue and only supporting 7 bits.

UX changes

To keep it consistent with Faiss, we are adding sq encoder under parameters field. The sq encoder in Lucene supports 3 optional parameters:

  • bits - Which decides the number of buckets for the input vector data to be quantized into; Defaults to 7.
  • confidence_interval - Which is used to determine the minQuantile and maxQuantile params used in the Quantization process. Acceptable values are 0, >=0.9 && <=1.0. If the confidence_interval is not provided we will set default value as null which is computed later in Lucene from the dimension of the vector as 1 - 1/(1 + dimension).

Sample Index mapping using sq encoder is shown below:

    "mappings": {
        "properties": {
            "my_vector1": {
                "type": "knn_vector",
                "dimension": 2,
                "method": {
                    "name": "hnsw",
                    "space_type": "l2",
                    "engine": "lucene",
                    "parameters": {
                        "encoder": {
                            "name": "sq",
                            "parameters": {
                                "bits": 7,
                                "confidence_interval": 1.0
                            }
                        }
                        "ef_construction": 128,
                        "m": 24,
                    }
                }
            }
        }
    }

Related Issues

#1277

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

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.

@naveentatikonda naveentatikonda added Features Introduces a new unit of functionality that satisfies a requirement backport 2.x v2.16.0 labels Jul 17, 2024
@naveentatikonda naveentatikonda self-assigned this Jul 17, 2024
@naveentatikonda naveentatikonda force-pushed the add_support_for_lucene_inbuilt_sq branch 2 times, most recently from 7b192cf to dcbe6ad Compare July 17, 2024 23:59
@naveentatikonda
Copy link
Member Author

Note - I'm still running some benchmarking tests for 4 bits, will complete by weekend and include 4 bits if there are no performance issues. That will be a very small change.

@navneet1v
Copy link
Collaborator

  • bits - Which decides the number of buckets for the input vector data to be quantized into; Defaults to 7.
  • confidence_interval - Which is used to determine the minQuantile and maxQuantile params used in the Quantization process. Acceptable values are 0, >=0.9 && <=1.0. If the confidence_interval is not provided we will set default value as null which is computed later in Lucene from the dimension of the vector as 1 - 1/(1 + dimension).
  • compress - Default value is false, which is useful only with 4 bits to compress and provide more memory reduction.

Seeing these different parameters and how they work. Lets ensure documentation is very clear otherwise there will be a lot of confusions.

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.

Whats the validation on the bits parameter?

@jmazanec15
Copy link
Member

Is 7-bit quantization implemented significantly different than 8-bit?

@naveentatikonda naveentatikonda force-pushed the add_support_for_lucene_inbuilt_sq branch from a9fd88b to d3e98cc Compare July 18, 2024 22:09
@naveentatikonda
Copy link
Member Author

Whats the validation on the bits parameter?

In Lucene.java as part of parameter validation, created a list of integers which has the bits that we are supporting and validating against it.

@naveentatikonda
Copy link
Member Author

Is 7-bit quantization implemented significantly different than 8-bit?

no, the difference is the number of buckets we are quantizing the given vectors into, which is 2^(bits) - 1 buckets

@naveentatikonda naveentatikonda force-pushed the add_support_for_lucene_inbuilt_sq branch from d3e98cc to 2beaba6 Compare July 18, 2024 22:31
Signed-off-by: Naveen Tatikonda <[email protected]>
Signed-off-by: Naveen Tatikonda <[email protected]>
Signed-off-by: Naveen Tatikonda <[email protected]>
Signed-off-by: Naveen Tatikonda <[email protected]>
Signed-off-by: Naveen Tatikonda <[email protected]>
@naveentatikonda naveentatikonda force-pushed the add_support_for_lucene_inbuilt_sq branch from afea7cc to 4f7ae01 Compare July 22, 2024 23:17
Signed-off-by: Naveen Tatikonda <[email protected]>
@naveentatikonda naveentatikonda force-pushed the add_support_for_lucene_inbuilt_sq branch from 4f7ae01 to 8631f19 Compare July 22, 2024 23:19
@naveentatikonda naveentatikonda merged commit 71fff47 into opensearch-project:main Jul 23, 2024
52 checks passed
String validationErrorMsg = String.format(Locale.ROOT, "Null value provided for Double " + "parameter \"%s\".", getName());
return getValidationException(validationErrorMsg);
}
if (value.equals(0)) value = 0.0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit. This style is error prune.

Suggested change
if (value.equals(0)) value = 0.0;
if (value.equals(0)) {
value = 0.0;
}

String validationErrorMsg = String.format(Locale.ROOT, "Null value provided for Double " + "parameter \"%s\".", getName());
return getValidationException(validationErrorMsg);
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

No conversion here?

if (value.equals(0)) {
  value = 0.0;
  }

opensearch-trigger-bot bot pushed a commit that referenced this pull request Jul 23, 2024
* Add support for Lucene Inbuilt Scalar Quantizer

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

* Refactor code

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

* Add Tests

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

* Address Review Comments

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

* Refactoring changes

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

* Remove compress as an input parameter and set default as true

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

* Add Constructor overloading and other refactoring changes

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

* Add more unit tests

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

* Set default encoder as encoder flat

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

---------

Signed-off-by: Naveen Tatikonda <[email protected]>
(cherry picked from commit 71fff47)
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jul 23, 2024
* Add support for Lucene Inbuilt Scalar Quantizer

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

* Refactor code

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

* Add Tests

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

* Address Review Comments

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

* Refactoring changes

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

* Remove compress as an input parameter and set default as true

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

* Add Constructor overloading and other refactoring changes

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

* Add more unit tests

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

* Set default encoder as encoder flat

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

---------

Signed-off-by: Naveen Tatikonda <[email protected]>
(cherry picked from commit 71fff47)
@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-1848-to-2.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 71fff475cd76f3fe7fd195a15f54138c9127ad6a
# Push it to GitHub
git push --set-upstream origin backport/backport-1848-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-1848-to-2.x.

@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.16 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.16 2.16
# Navigate to the new working tree
cd .worktrees/backport-2.16
# Create a new branch
git switch --create backport/backport-1848-to-2.16
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 71fff475cd76f3fe7fd195a15f54138c9127ad6a
# Push it to GitHub
git push --set-upstream origin backport/backport-1848-to-2.16
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.16

Then, create a pull request where the base branch is 2.16 and the compare/head branch is backport/backport-1848-to-2.16.

@heemin32
Copy link
Collaborator

heemin32 commented Jul 23, 2024

For now, we are not supporting 8 bits due to a recall issue and only supporting 7 bits.

Does that mean, user can only set 7 bits but nothing else?

@naveentatikonda
Copy link
Member Author

For now, we are not supporting 8 bits due to a recall issue and only supporting 7 bits.

Does that mean, user can only set 7 bits but nothing else?

yes, for now only 7 bits. We will support 8 bits and 4 bits after the underlying quantization issues are resolved in lucene.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x backport 2.16 Features Introduces a new unit of functionality that satisfies a requirement skip-changelog v2.16.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants