Skip to content

Avoid extra philox calls with ElementRNG - #723

Open
nhz2 wants to merge 18 commits into
JuliaGPU:mainfrom
nhz2:nz/rng-perf-improvements
Open

Avoid extra philox calls with ElementRNG#723
nhz2 wants to merge 18 commits into
JuliaGPU:mainfrom
nhz2:nz/rng-perf-improvements

Conversation

@nhz2

@nhz2 nhz2 commented May 15, 2026

Copy link
Copy Markdown
Contributor

Fixes #721

This PR avoids some extra philox calls when using ElementRNG. ElementRNG is used as an AbstractRNG when sampling any non batched type including types with custom samplers defined in different packages.

This PR also makes ComplexF16 a batched type.

Comment thread test/testsuite/random.jl Outdated
@test false in Array(A)

# Int128 is not supported on many backends yet
if nameof(AT) == :JLArray

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ehh, this is ugly. Normally we do this through eltypes, which shouldn't list Int128.

Comment thread src/host/random.jl Outdated
Comment on lines +545 to +546
for T in (Float16, Float32, Float64, ComplexF32, ComplexF64)
@eval @inline function Random.randn(rng::ElementRNG, ::Type{$T})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How do we reach this for Complex types? ComplexF32 and ComplexF64 are in BatchedRandnTypes. Meanwhile, ComplexF16 isn't, so that's probably the case which should be listed here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This can be reached if a type with a custom sampling method calls randn. I can try and also add ComplexF16 to the BatchedRandnTypes.

Comment thread src/host/random.jl Outdated
@nhz2
nhz2 marked this pull request as draft July 24, 2026 19:37
@nhz2
nhz2 marked this pull request as ready for review July 25, 2026 05:19
@nhz2

nhz2 commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

There are many interesting bugs with using Float16 and Int128, but I don't think they are related to this PR.

@nhz2

nhz2 commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

Due to JuliaLang/julia#62507 I'm restricting Float16 tests to Julia versions 1.13 or later.

@nhz2
nhz2 requested a review from maleadt July 27, 2026 13:13
@maleadt

maleadt commented Jul 28, 2026

Copy link
Copy Markdown
Member

Due to JuliaLang/julia#62507 I'm restricting Float16 tests to Julia versions 1.13 or later.

That's excessive. Float16 had been working fine before; why does it only fail here? If it's specific to a test, you can always add a specific version-gated filter on top of the eltypes passed into the testsuite

@nhz2

nhz2 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

The Julia 1.12 miscompile only happens on CPUs with avx512_fp16. Github actions will sometimes use a CPU with avx512_fp16 now. If you run the existing tests enough times you can see the failure. I've limited the removal of Float16 tests to only Julia 1.12 and avx512_fp16 CPUs.

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.

rand! with RNG and Complex{Int128} isn't random

2 participants