Skip to content

IP space roundup bug fix #361

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 1 commit into from
Feb 14, 2022

Conversation

GuyAv46
Copy link
Contributor

@GuyAv46 GuyAv46 commented Jan 19, 2022

Fixing a bug causing IP functions in some optimizations and cases to round up the calculated result.

when using InnerProductSIMD16ExtResiduals or InnerProductSIMD4ExtResiduals, we call InnerProductSIMD16Ext or InnerProductSIMD4Ext respectively, and then also calling InnerProduct. each result already calculated 1.0f - {IP} so we need to sum them and then subtract one 1.0f back to get 1.0f - {IP16/4 + IPrest} and not 2.0f - {IP16/4 + IPrest}.

When comparing close vectors, this can lead to losing the actual IP and rounding up the result, and getting inconsistent results in different optimizations.

@yurymalkov
Copy link
Member

Hi @GuyAv46. Thank you for the PR!

One major factor is that hopefully #357 is going to be merged pretty soon with some changes in the same file, so probably it would be needed to be updated.

I guess it also might make sense to rename InnerProduct_impl* to just DotProduct* (or InnerProduct*), and probably it is a good time to add to rename InnerProduct* to InnerProductDistance*, so that is it is clear that some methods are computing inner product and some - inner product distance.

I also wonder if you've know what is the difference between avx and non-avx computation on the same cpu?

@GuyAv46
Copy link
Contributor Author

GuyAv46 commented Jan 23, 2022

Happy to help. :)
I will subscribe to the PR you've mentioned and make the needed changes. I also agree about the naming of the functions.

about the last question - do you mean what is the difference in performance?

@GuyAv46
Copy link
Contributor Author

GuyAv46 commented Jan 23, 2022

---------------------------------------------------------------------
Benchmark                           Time             CPU   Iterations
---------------------------------------------------------------------
BM_VecSimBasicsIP/IP             76.4 ns         76.4 ns      9018969
BM_VecSimBasicsIP/IPAVX512       7.34 ns         7.34 ns     94764434
BM_VecSimBasicsIP/IPAVX          12.4 ns         12.4 ns     56307633
BM_VecSimBasicsIP/IPSSE          15.2 ns         15.2 ns     46112636

tested on Lenovo Thinkpad P15v with AVX512 capabilities, on 128 dimension float vector, using InnerProductSIMD16Ext

@yurymalkov
Copy link
Member

@GuyAv46 Thank you!
I meant if there is a difference in terms of numeric accuracy (I guess there might be some), though the performance numbers are also valuable.

@GuyAv46
Copy link
Contributor Author

GuyAv46 commented Jan 24, 2022

As far as I can tell, my PR fix this problem; there is no more difference in result accuracy between different optimization. Every function eventually multiply each same-index elements pair and then sum them all up, using floats, even if they use two functions internally, and only afterward calculating 1 - res.

Before the change, I notice that when I used InnerProductSIMD16ExtResiduals with two close vectors, the first internal function returned 1 (the dot product was 0), and the second internal (on the "tail") returned ~0 (1-{almost 1}), and instead returning ~0 (5.9604645e-08), the function returned 1 + ~0 - 1 and it got round up to 0.

Now I'm getting the same result regardless the specific function.

@yurymalkov
Copy link
Member

Got it! Thanks!

@GuyAv46 GuyAv46 force-pushed the ip_space_roundup_bug_fix branch from 7b41eca to 49ef6bc Compare January 30, 2022 12:12
@GuyAv46 GuyAv46 changed the base branch from master to develop January 30, 2022 12:13
@yurymalkov
Copy link
Member

Hi @GuyAv46.
The PR looks good to me. Wanted to check that you think it is ready to be merged?

@GuyAv46
Copy link
Contributor Author

GuyAv46 commented Feb 13, 2022

Yes, It is ready

@yurymalkov
Copy link
Member

Thanks!

@yurymalkov yurymalkov merged commit 33772c2 into nmslib:develop Feb 14, 2022
@GuyAv46 GuyAv46 deleted the ip_space_roundup_bug_fix branch February 14, 2022 07:54
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.

2 participants