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

Can't sample random Core.BFloat16 #53651

Closed
Seelengrab opened this issue Mar 8, 2024 · 3 comments
Closed

Can't sample random Core.BFloat16 #53651

Seelengrab opened this issue Mar 8, 2024 · 3 comments
Labels
float16 randomness Random number generation and the Random stdlib

Comments

@Seelengrab
Copy link
Contributor

Core.BFloat16 was added as a new AbstractFloat in #51470, but so far it doesn't seem possible to call rand(BFloat16):

julia> rand(Core.BFloat16)
ERROR: MethodError: no method matching Random.Sampler(::Type{TaskLocalRNG}, ::Random.SamplerTrivial{Random.CloseOpen01{…}, Core.BFloat16}, ::Val{1})
This error has been manually thrown, explicitly, so the method may exist but be intentionally marked as unimplemented.

Closest candidates are:
  Random.Sampler(::Type{<:AbstractRNG}, ::Random.Sampler, ::Union{Val{1}, Val{Inf}})
   @ Random ~/julia/usr/share/julia/stdlib/v1.12/Random/src/Random.jl:145
  Random.Sampler(::Type{<:AbstractRNG}, ::Any, ::Union{Val{1}, Val{Inf}})
   @ Random ~/julia/usr/share/julia/stdlib/v1.12/Random/src/Random.jl:181
  Random.Sampler(::Type{<:AbstractRNG}, ::BitSet, ::Union{Val{1}, Val{Inf}})
   @ Random ~/julia/usr/share/julia/stdlib/v1.12/Random/src/generation.jl:488
  ...

Stacktrace:
 [1] Random.Sampler(T::Type{TaskLocalRNG}, sp::Random.SamplerTrivial{Random.CloseOpen01{…}, Core.BFloat16}, r::Val{1})
   @ Random ~/julia/usr/share/julia/stdlib/v1.12/Random/src/Random.jl:145
 [2] Random.Sampler(rng::TaskLocalRNG, x::Random.SamplerTrivial{Random.CloseOpen01{Core.BFloat16}, Core.BFloat16}, r::Val{1})
   @ Random ~/julia/usr/share/julia/stdlib/v1.12/Random/src/Random.jl:139
 [3] rand(rng::TaskLocalRNG, X::Random.SamplerTrivial{Random.CloseOpen01{Core.BFloat16}, Core.BFloat16})
   @ Random ~/julia/usr/share/julia/stdlib/v1.12/Random/src/Random.jl:253
 [4] rand(rng::TaskLocalRNG, ::Type{Core.BFloat16})
   @ Random ~/julia/usr/share/julia/stdlib/v1.12/Random/src/Random.jl:256
 [5] rand(::Type{Core.BFloat16})
   @ Random ~/julia/usr/share/julia/stdlib/v1.12/Random/src/Random.jl:259
 [6] top-level scope
   @ REPL[31]:1
Some type information was truncated. Use `show(err)` to see complete types.

This was found in these nightly tests (CI log is here). I can mark that particular combination of arguments broken for now, but it would be cool to get this functionality at some point :)

I'm not 100% sure the label float16 is correct. This is about BFloat16 after all, which is slightly different. Feel free to adjust as necessary!

@Seelengrab Seelengrab added randomness Random number generation and the Random stdlib float16 labels Mar 8, 2024
Seelengrab added a commit to Seelengrab/PropCheck.jl that referenced this issue Mar 8, 2024
`numsToZero` relies on being able to sample arbitrary `AbstractFloat`
with `rand`, which seemingly isn't possible with the new `Core.BFloat16`
introduced in JuliaLang/julia#51470.
See JuliaLang/julia#53651 for the upstream
issue tracking this.

1.11 also introduces `AnnotatedString`, which interacts badly
with the local scope of `@testset` and trying to lazily `join`
things that may degenerate in inference to `AbstractString`. The type
assertion is a quick "fix", since other than moving `irb` outside of
that scope, inference will continue to mess with the test,
even though no `AnnotatedString` could ever actually be produced.
@gbaraldi
Copy link
Member

gbaraldi commented Mar 8, 2024

This is a type that doesn't have a full implementation in base, it's there to make sure codegen understands it in a packaged like https://github.com/JuliaMath/BFloat16s.jl

@Seelengrab
Copy link
Contributor Author

Seelengrab commented Mar 8, 2024

It's a bit odd to me to define a type in Base that claims to be AbstractFloat, without actually behaving like that at all and instead requiring an external package to access that functionality :/

@giordano
Copy link
Contributor

giordano commented Nov 1, 2024

I'm going to close this ticket as the random sampling method is implemented in BFloat16s.jl. It's true BFloat16s.jl is basically doing type piracy, but at least there's coordination with Base, it was expected to behave like that already in the above mentioned #51470, and if anything that's a separate issue.

@giordano giordano closed this as completed Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
float16 randomness Random number generation and the Random stdlib
Projects
None yet
Development

No branches or pull requests

3 participants