-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
[WIP] Xoshiro: allow any non-negative integer as a seed, via SHA2_256 #41558
Conversation
|
||
function seed!(rng::Union{TaskLocalRNG,Xoshiro}, seed::Vector{UInt32}) | ||
c = SHA.SHA2_256_CTX() | ||
SHA.update!(c, reinterpret(UInt8, seed)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is endian-dependent, which I don't think we want. Maybe do hton.(seed)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, thanks. I will update if this PR gets support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we've ever run on a big endian system, and probably never will.
function seed!(rng::Union{TaskLocalRNG,Xoshiro}, seed::Vector{UInt32}) | ||
c = SHA.SHA2_256_CTX() | ||
SHA.update!(c, reinterpret(UInt8, seed)) | ||
s0, s1, s2, s3 = reinterpret(UInt64, SHA.digest!(c)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, use ntoh.(reinterpret(...))
?
If we want to get this in it should probably be done for 1.7 since the rng stream had to change again due to #42150 |
I'm in favor. I think this is a significantly safer and better way to do seeding. One question: it struck me that the SHA hashing should maybe go in the |
We just changed the random stream anyway, so might as well do this too. |
6177901
to
248fcc0
Compare
c025875
to
0535975
Compare
0535975
to
f69daf1
Compare
@test cond(a,2) ≈ 78.44837047684149 atol=0.5 | ||
@test cond(a,Inf) ≈ 174.10761543202744 atol=0.4 | ||
@test cond(a[:,1:5]) ≈ 6.7492840150789135 atol=0.01 | ||
@test cond(a,1) ≈ 198.3324294531168 atol=0.5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are the worst...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seriously. What a badly designed test...
(cherry picked from commit 2174ee1)
That sounds like a good idea to me, it would make |
And thanks @JeffBezanson for finishing this up! |
This converts any integer to a vector, the vector is hashed with SHA2_256, and the resulting 256 bits are used to initialize the RNG state.
This also fixes a
But of course, seeding becomes much slower: e.g.
seed!(123)
goes from 18ns to 959ns (which can be reduced easily to 800ns with a couple of tricks). Also this means that all the random streams change again.