Skip to content

Commit

Permalink
Do not pirate Base.rand/Base.randn methods (#54)
Browse files Browse the repository at this point in the history
I forgot to use `@device_override` for these methods.
  • Loading branch information
giordano authored Mar 17, 2024
1 parent 40e723c commit f20ac7e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "IPUToolkit"
uuid = "92e0b95a-4011-435a-96f4-10064551ddbe"
authors = ["Emily Dietrich <jakibaki@live.com>", "Luk Burchard <luk.burchard@gmail.com>", "Mosè Giordano <mose@gnu.org>"]
version = "1.4.1"
version = "1.4.2"

[deps]
Clang = "40e3b903-d033-50b4-a0cc-940c62c95e31"
Expand Down
22 changes: 13 additions & 9 deletions src/compiler/runtime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,26 @@ end
# works only when targeting a Colossus-aware LLVM, so for the general case we call fake
# external `_llvm_colossus_...` intrinsics and then rename them before writing to file. Not
# great, but it does the job.
get_scount_l() = ccall("extern _llvm_colossus_get_scount_l", llvmcall, Cuint, ())
get_tile_id() = ccall("extern _llvm_colossus_get_tile_id", llvmcall, Cuint, ())
get_scount_l() = ccall("extern _llvm_colossus_get_scount_l", llvmcall, Cuint, ())
get_tile_id() = ccall("extern _llvm_colossus_get_tile_id", llvmcall, Cuint, ())
# Random functions, based on IPU intrinsics
Base.rand(T::Type{Float16}) = ccall("extern _llvm_colossus_urand_f16", llvmcall, Float16, ()) + T(0.5)
Base.rand(T::Type{Float32}) = ccall("extern _llvm_colossus_urand_f32", llvmcall, Float32, ()) + T(0.5)
Base.rand(T::Type{UInt32}) = ccall("extern _llvm_colossus_urand32", llvmcall, UInt32, ()) + T(0.5)
Base.rand(T::Type{UInt64}) = ccall("extern _llvm_colossus_urand64", llvmcall, UInt64, ()) + T(0.5)
@device_override Base.rand(T::Type{Float16}) = ccall("extern _llvm_colossus_urand_f16", llvmcall, Float16, ()) + T(0.5)
@device_override Base.rand(T::Type{Float32}) = ccall("extern _llvm_colossus_urand_f32", llvmcall, Float32, ()) + T(0.5)
@device_override Base.rand(T::Type{UInt32}) = ccall("extern _llvm_colossus_urand32", llvmcall, UInt32, ()) + T(0.5)
@device_override Base.rand(T::Type{UInt64}) = ccall("extern _llvm_colossus_urand64", llvmcall, UInt64, ()) + T(0.5)
# Note: `llvm.colossus.f{16,32}v2grand` return 2-tuples of numbers, but Julia's `Base.randn`
# returns a single number at a time, sadly we have to discard one of the numbers to keep the
# same semantic.
Base.randn(T::Type{Float16}) = @inbounds ccall("extern _llvm_colossus_f16v2grand", llvmcall, NTuple{2, VecElement{Float16}}, ())[1].value
Base.randn(T::Type{Float32}) = @inbounds ccall("extern _llvm_colossus_f32v2grand", llvmcall, NTuple{2, VecElement{Float32}}, ())[1].value
@device_override Base.randn(T::Type{Float16}) = @inbounds ccall("extern _llvm_colossus_f16v2grand", llvmcall, NTuple{2, VecElement{Float16}}, ())[1].value
@device_override Base.randn(T::Type{Float32}) = @inbounds ccall("extern _llvm_colossus_f32v2grand", llvmcall, NTuple{2, VecElement{Float32}}, ())[1].value

## Math functions.
# There are different reasons why we prefer LLVM intrinsics on the IPU: implementations in
# Julia's Base either require promotion to double (very slow) or require non-existing
# symbols (maybe because they aren't implemented for `double`s on the IPU).
@device_override Base.sin(x::Float32) = ccall("llvm.sin.f32", llvmcall, Float32, (Float32,), x)
@device_override Base.cos(x::Float32) = ccall("llvm.cos.f32", llvmcall, Float32, (Float32,), x)
@device_override Base.tan(x::Float32) = ccall("extern tanf", llvmcall, Float32, (Float32,), x)
@device_override Base.tan(x::Float32) = ccall("extern tanf", llvmcall, Float32, (Float32,), x)
@device_override Base.exp(x::Float32) = ccall("llvm.exp.f32", llvmcall, Float32, (Float32,), x)
@device_override Base.exp2(x::Float32) = ccall("llvm.exp2.f32", llvmcall, Float32, (Float32,), x)
@device_override Base.log(x::Float32) = ccall("llvm.log.f32", llvmcall, Float32, (Float32,), x)
Expand Down Expand Up @@ -92,6 +92,10 @@ Base.randn(T::Type{Float32}) = @inbounds ccall("extern _llvm_colossus_f32v2grand
@device_override Base.min(a::Float32, b::Float32) = ccall("llvm.minnum.f32", llvmcall, Float32, (Float32, Float32), a, b)
@device_override Base.max(a::Float32, b::Float32) = ccall("llvm.maxnum.f32", llvmcall, Float32, (Float32, Float32), a, b)
@device_override Base.tanh(x::Float32) = ccall("extern _llvm_colossus_tanh_f32", llvmcall, Float32, (Float32,), x)
# For some reasons I didn't have the time to investigate the `==` and `!=` methods below cause
# crashes. But also, quick benchmarks didn't show significant performance improvements compared
# to the default behaviour in Julia (also for the other comparison operators), so that they
# don't seem to be too much worth the effort, we keep the code below just for reference.
# @device_override Base.:(==)(a::Float32, b::Float32) = Bool(ccall("extern _llvm_colossus_f32cmpeq", llvmcall, Float32, (Float32, Float32), a, b))
# @device_override Base.:(!=)(a::Float32, b::Float32) = Bool(ccall("extern _llvm_colossus_f32cmpne", llvmcall, Float32, (Float32, Float32), a, b))

Expand Down

2 comments on commit f20ac7e

@giordano
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/103429

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.4.2 -m "<description of version>" f20ac7e741f85794162b7b365c5c1aeae6cc3a42
git push origin v1.4.2

Please sign in to comment.