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

fix: cosine distance #231

Merged
merged 4 commits into from
Jan 6, 2024
Merged

fix: cosine distance #231

merged 4 commits into from
Jan 6, 2024

Conversation

kemingy
Copy link
Member

@kemingy kemingy commented Jan 5, 2024

Signed-off-by: Keming <kemingyang@tensorchord.ai>
gaocegege
gaocegege previously approved these changes Jan 5, 2024
@VoVAllen
Copy link
Member

VoVAllen commented Jan 5, 2024

How about PQ and SQ distance?

Signed-off-by: Keming <kemingyang@tensorchord.ai>
@kemingy
Copy link
Member Author

kemingy commented Jan 5, 2024

How about PQ and SQ distance?

Fixed in the new commit. PTAL

@usamoi
Copy link
Collaborator

usamoi commented Jan 5, 2024

Can we just use 1.0-cos and sqrt(squared l2) for PostgreSQL operator?

Copy link
Collaborator

@usamoi usamoi left a comment

Choose a reason for hiding this comment

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

I suggest add a display_distance function to the G trait and use it for displaying.

@kemingy
Copy link
Member Author

kemingy commented Jan 5, 2024

Can we just use 1.0-cos and sqrt(squared l2) for PostgreSQL operator?

I don't have the plan to replace the squared L2 distance.

@gaocegege
Copy link
Member

Please resolve the conflicts.

Can we just use 1.0-cos and sqrt(squared l2) for PostgreSQL operator?

It will add more overhead to the compute, but we do not see the benefits. Thus let's keep no sqrt for now.

Signed-off-by: Keming <kemingyang@tensorchord.ai>
@kemingy
Copy link
Member Author

kemingy commented Jan 6, 2024

Please resolve the conflicts.

fixed

@gaocegege
Copy link
Member

PTAL @usamoi @VoVAllen

Copy link
Collaborator

@usamoi usamoi left a comment

Choose a reason for hiding this comment

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

Please fix:
https://github.com/tensorchord/pgvecto.rs/blob/a7a9a592c1ef8db0c41ff106b62c85359c10a3b1/README.md?plain=1#L109
https://github.com/tensorchord/pgvecto.rs/blob/a7a9a592c1ef8db0c41ff106b62c85359c10a3b1/README.md?plain=1#L118
https://github.com/tensorchord/pgvecto.rs/blob/a7a9a592c1ef8db0c41ff106b62c85359c10a3b1/crates/service/src/instance/metadata.rs#L21

Signed-off-by: Keming <kemingyang@tensorchord.ai>
@VoVAllen
Copy link
Member

VoVAllen commented Jan 6, 2024

Can we just use 1.0-cos and sqrt(squared l2) for PostgreSQL operator?

I think this is the better way. We can add sqrt to PostgresSQL operator. If user didn't use L2 instance in the query, no extra cost will be introduced? And we can make it consistent with pgvector at all.

@usamoi Will the cost of sqrt happen when user doesn't need distance in the query?

@usamoi
Copy link
Collaborator

usamoi commented Jan 6, 2024

@usamoi Will the cost of sqrt happen when user doesn't need distance in the query?

No, there will be no extra cost. What I cares is consistency: we use l2 for opeator name but it's l2s(l2-squared) in fact.

@kemingy
Copy link
Member Author

kemingy commented Jan 6, 2024

Cosine distance has to be fixed because it's defined as 1 - cosine_similarity. For the L2 distance, if you choose to impl squared L2, it's also widely accepted. I don't think it's necessary to get the square root.

@usamoi
Copy link
Collaborator

usamoi commented Jan 6, 2024

We didn't implement Cosine distance. What we implemented before is negative cosine similarity and we take cos for short. It's consistent with users who choose to normalize the vector and use negative dot product.

@VoVAllen VoVAllen added this pull request to the merge queue Jan 6, 2024
Merged via the queue into tensorchord:main with commit 4aafd8c Jan 6, 2024
7 checks passed
@kemingy kemingy deleted the cos_dis branch January 7, 2024 15:09
@cutecutecat cutecutecat mentioned this pull request Feb 20, 2024
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.

4 participants