Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@ DoubleFloats = "497a8b3b-efae-58df-a0af-a86822472b78"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RandomNumbers = "e6cf234a-135c-5ec9-84dd-332b85af5143"

[weakdeps]
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"

[extensions]
EnzymeExt = "Enzyme"

[compat]
BFloat16s = "0.1, 0.2, 0.3, 0.4, 0.5"
DoubleFloats = "1"
Enzyme = "0.13"
Random = "1"
RandomNumbers = "^1.4"
julia = "1"

[extras]
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"

[targets]
test = ["Test", "DifferentialEquations"]
45 changes: 45 additions & 0 deletions ext/EnzymeExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module EnzymeExt

using Enzyme
using StochasticRounding
import StochasticRounding: stochastic_round

function Enzyme.typetree_inner(::Type{Float64sr}, ctx, dl, seen::Enzyme.Compiler.TypeTreeTable)
return Enzyme.TypeTree(Enzyme.API.DT_Double, -1, ctx)
end

function Enzyme.get_offsets(::Type{Float64sr})
return ((Enzyme.API.DT_Double, 0),)
end

function Enzyme.typetree_inner(::Type{Float32sr}, ctx, dl, seen::Enzyme.Compiler.TypeTreeTable)
Copy link

Choose a reason for hiding this comment

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

so as is this can result in some errors, specifically if an all-zero bits is not a valid zero, or in reverse mode of a 32-bit floating point add is not a correct add.

return Enzyme.TypeTree(Enzyme.API.DT_Float, -1, ctx)
end

function Enzyme.get_offsets(::Type{Float32sr})
return ((Enzyme.API.DT_Float, 0),)
end

function Enzyme.typetree_inner(::Type{Float16sr}, ctx, dl, seen::Enzyme.Compiler.TypeTreeTable)
return Enzyme.TypeTree(Enzyme.API.DT_Half, -1, ctx)
end

function Enzyme.get_offsets(::Type{Float16sr})
return ((Enzyme.API.DT_Half, 0),)
end

import Enzyme: EnzymeRules
import Enzyme: Const, Duplicated

function EnzymeRules.forward(config, ::Const{typeof(stochastic_round)}, ::Type{<:DuplicatedNoNeed}, ::Const{Type{T}}, x::Duplicated) where T
stochastic_round(T, x.dval)
end

function EnzymeRules.forward(config, ::Const{typeof(stochastic_round)}, ::Type{<:Duplicated}, ::Const{Type{T}}, x::Duplicated) where T
Duplicated(
stochastic_round(T, x.val),
stochastic_round(T, x.dval)
)
end

end # module