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 MultinomialSampler overflow #1744

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Fix MultinomialSampler overflow #1744

wants to merge 2 commits into from

Conversation

0
Copy link

@0 0 commented Jun 30, 2023

n^2 doesn't fit into Int64 for n larger than 3_037_000_499.

@codecov-commenter
Copy link

codecov-commenter commented Jun 30, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (b9d3093) 85.90% compared to head (77e132a) 85.90%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1744   +/-   ##
=======================================
  Coverage   85.90%   85.90%           
=======================================
  Files         142      142           
  Lines        8578     8579    +1     
=======================================
+ Hits         7369     7370    +1     
  Misses       1209     1209           
Impacted Files Coverage Δ
src/samplers/multinomial.jl 87.50% <100.00%> (+0.32%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@0
Copy link
Author

0 commented Jul 2, 2023

Changed sqrt to isqrt.

@devmotion
Copy link
Member

OK 🙂 Now the main remaining question is: is it possible to add a test for the fix? I assume it could be a bit challenging since the change only affects whether an alias table is used or not, but should not affect correctness (I assume).

@0
Copy link
Author

0 commented Jul 3, 2023

Right, this change is about performance and not correctness, since the overflow only affects the heuristic. I initially wrote a test along the lines of

for n in [3_037_000_499, 3_037_000_500]
    @test all(n .== rand(Multinomial(n, 1), 2))
end

but didn't end up including it, because it should pass with both versions of the code anyway. It takes significantly longer using the alias table, but that seems like the wrong thing to check in a unit test.

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.

None yet

3 participants