From d057de100b672866c71a788fcc6b29267d135eab Mon Sep 17 00:00:00 2001 From: lassepe Date: Mon, 17 Nov 2025 17:56:34 +0100 Subject: [PATCH 1/6] Test for forward mode jacobians --- test/autodiff.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/autodiff.jl b/test/autodiff.jl index 10daa32ee8..a2ea8d3fbf 100644 --- a/test/autodiff.jl +++ b/test/autodiff.jl @@ -102,6 +102,19 @@ end @test res[1] ≈ ones(2, 2) end +f_sincos(x) = map(sin, x) + map(cos, reverse(x)) +f_sincos_jac(x) = [cos(x[i]) * (i == j ? 1 : 0) - sin(x[end-i+1]) * (i == (length(x) - j + 1) ? 1 : 0) for i in 1:length(x), j in 1:length(x)] + +@testset "Forward Jacobian" begin + jac(x) = only(Enzyme.jacobian(Enzyme.Forward, f_sincos, x)) + x_r = Reactant.to_rarray(rand(10)) + + j_gt = f_sincos_jac(x_r) + j_reactant = Reactant.@jit jac(x_r) + + @test j_reactant ≈ j_gt +end + mutable struct StateReturn st::Any end From ad662d378b476930fba08ef68e4fff8a8f6dded2 Mon Sep 17 00:00:00 2001 From: lassepe Date: Fri, 21 Nov 2025 23:32:40 +0100 Subject: [PATCH 2/6] Add at-allowscalar --- test/autodiff.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/autodiff.jl b/test/autodiff.jl index a2ea8d3fbf..5fd0188c0d 100644 --- a/test/autodiff.jl +++ b/test/autodiff.jl @@ -109,7 +109,7 @@ f_sincos_jac(x) = [cos(x[i]) * (i == j ? 1 : 0) - sin(x[end-i+1]) * (i == (lengt jac(x) = only(Enzyme.jacobian(Enzyme.Forward, f_sincos, x)) x_r = Reactant.to_rarray(rand(10)) - j_gt = f_sincos_jac(x_r) + j_gt = Reactant.@allowscalar f_sincos_jac(x_r) j_reactant = Reactant.@jit jac(x_r) @test j_reactant ≈ j_gt From b6d85091dfdb71c0995ae0e3ecec57905d4352c8 Mon Sep 17 00:00:00 2001 From: lassepe Date: Fri, 21 Nov 2025 23:36:39 +0100 Subject: [PATCH 3/6] Add Enzyme.tupsack dispatch for Reactant.RArray --- src/Enzyme.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Enzyme.jl b/src/Enzyme.jl index e263e4d716..b7d24dc141 100644 --- a/src/Enzyme.jl +++ b/src/Enzyme.jl @@ -69,6 +69,20 @@ end return Base.zero(x) end +@inline function Enzyme.tupstack( + data::Tuple{Vararg{<:Reactant.RArray{T}}}, + outshape::Tuple{Vararg{Int}}, + inshape::Tuple{Vararg{Int}}, +) where {T} + res = similar(first(data), outshape..., inshape...) + c = CartesianIndices(outshape) + tail_dims = map(Returns(:), inshape) + for (i, val) in enumerate(data) + @inbounds res[c[i], tail_dims...] = val + end + return res +end + macro register_make_zero_inplace(sym) quote @inline function $sym(prev::RArray{T,N})::Nothing where {T<:AbstractFloat,N} From 2e06801ef30ecac79f5e12daaf0d5b268dc0a9b7 Mon Sep 17 00:00:00 2001 From: William Moses Date: Sat, 22 Nov 2025 11:15:33 -0600 Subject: [PATCH 4/6] Update Enzyme.jl --- src/Enzyme.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Enzyme.jl b/src/Enzyme.jl index b7d24dc141..1da1c7dbc9 100644 --- a/src/Enzyme.jl +++ b/src/Enzyme.jl @@ -70,7 +70,7 @@ end end @inline function Enzyme.tupstack( - data::Tuple{Vararg{<:Reactant.RArray{T}}}, + data::Tuple{Vararg{<:RArray{T}}}, outshape::Tuple{Vararg{Int}}, inshape::Tuple{Vararg{Int}}, ) where {T} From 5204feaa77c19e58d24402ab5d3cc48ea9f1af25 Mon Sep 17 00:00:00 2001 From: William Moses Date: Sat, 22 Nov 2025 13:57:43 -0600 Subject: [PATCH 5/6] Update Enzyme.jl --- src/Enzyme.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Enzyme.jl b/src/Enzyme.jl index 1da1c7dbc9..43553add5b 100644 --- a/src/Enzyme.jl +++ b/src/Enzyme.jl @@ -70,10 +70,10 @@ end end @inline function Enzyme.tupstack( - data::Tuple{Vararg{<:RArray{T}}}, + data::Tuple{Vararg{<:RArray}}, outshape::Tuple{Vararg{Int}}, inshape::Tuple{Vararg{Int}}, -) where {T} +) res = similar(first(data), outshape..., inshape...) c = CartesianIndices(outshape) tail_dims = map(Returns(:), inshape) From 97c29f3c1baae080531c5a78526775977106e8a2 Mon Sep 17 00:00:00 2001 From: William Moses Date: Sat, 22 Nov 2025 17:48:02 -0600 Subject: [PATCH 6/6] Update Enzyme.jl --- src/Enzyme.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Enzyme.jl b/src/Enzyme.jl index 43553add5b..6be770720f 100644 --- a/src/Enzyme.jl +++ b/src/Enzyme.jl @@ -70,7 +70,7 @@ end end @inline function Enzyme.tupstack( - data::Tuple{Vararg{<:RArray}}, + data::Tuple{<:RArray, Vararg{<:RArray}}, outshape::Tuple{Vararg{Int}}, inshape::Tuple{Vararg{Int}}, )