-
Notifications
You must be signed in to change notification settings - Fork 68
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
Automate use of deferred in Higher order derivatives #1005
Comments
It is! The inner derivative calls must use autodiff_deferred though |
Just ran into this as well with the following error followed by a Assertion failed: (whatType(argType, Mode) == DIFFE_TYPE::DUP_ARG || whatType(argType, Mode) == DIFFE_TYPE::CONSTANT), function recursivelyHandleSubfunction, file /workspace/srcdir/Enzyme/enzyme/Enzyme/AdjointGenerator.h, line 7109. Using It would really improve the user experience if this low-level error was caught and handled smoothly with a high-level error that points the user to |
Also. Would it be possible in the future to avoid the need for calling I am just thinking this might cause issues in the distant future when Enzyme.jl is more commonplace, if someone differentiates a large program that has an internal |
It would be nice to have an option in autodiff_deferred to set the order of the derivative as well, like if I wanted a second or a third derivative with relation to a variable. Thank you guys for the amazing work you are doing. |
Apologies if this would be better discussed in a new issue, but I'm also trying to play with higher-order derivatives and having some issues. My primary concern is non-allocating derivatives, and so I have the following little code setup: module EnzymeHigherOrder
using Enzyme
struct DefDerivative{F}
fn::F
end
function (dd::DefDerivative{F})(x) where{F}
autodiff_deferred(Forward, dd.fn, DuplicatedNoNeed,
Duplicated(x, one(x)))[1]
end
struct Derivative{F}
fn::F
end
function (dv::Derivative{F})(x) where{F}
autodiff(Forward, dv.fn, DuplicatedNoNeed, Duplicated(x, one(x)))[1]
end
derivative(fn::F, ::Val{0}) where{F} = fn
derivative(fn::F, ::Val{1}) where{F} = Derivative(fn)
derivative(fn::F, ::Val{N}) where{F,N} = derivative(DefDerivative(fn), Val(N-1))
end And I can use this to get second derivatives as const ddcos = EnzymeHigherOrder.derivative(cos, Val(2))
@btime ddcos($(1.1)); # no allocations, great perf, woohoo! But when I ask for a third derivative I get an error: const dddcos = EnzymeHigherOrder.derivative(cos, Val(3))
dddcos(1.1) gives ERROR: Enzyme execution failed.
Enzyme: Not yet implemented, mixed activity for jl_new_struct constants=Bool[1, 0] %5 = call noalias nonnull {} addrspace(10)* ({} addrspace(10)* ({} addrspace(10)*, {} addrspace(10)**, i32)*, {} addrspace(10)*, ...) @julia.call({} addrspace(10)* ({} addrspace(10)*, {} addrspace(10)**, i32)* noundef nonnull @jl_f_tuple, {} addrspace(10)* noundef null, {} addrspace(10)* nonnull %0, {} addrspace(10)* nonnull %3) #11
Stacktrace:
[1] throwerr(cstr::Cstring)
@ Enzyme.Compiler ~/.julia/packages/Enzyme/rbuCz/src/compiler.jl:3044
[2] macro expansion
@ ~/.julia/packages/Enzyme/rbuCz/src/compiler.jl:9778 [inlined]
[3] enzyme_call
@ ~/.julia/packages/Enzyme/rbuCz/src/compiler.jl:9456 [inlined]
[4] ForwardModeThunk
@ ~/.julia/packages/Enzyme/rbuCz/src/compiler.jl:9422 [inlined]
[5] autodiff
@ ~/.julia/packages/Enzyme/rbuCz/src/Enzyme.jl:330 [inlined]
[6] autodiff
@ ~/.julia/packages/Enzyme/rbuCz/src/Enzyme.jl:222 [inlined]
[7] (::Main.EnzymeHigherOrder.Derivative{Main.EnzymeHigherOrder.DefDerivative{Main.EnzymeHigherOrder.DefDerivative{typeof(cos)}}})(x::Float64)
@ Main.EnzymeHigherOrder ~/Scratch/higherorderad/enzyme_ho.jl:18
[8] top-level scope
@ REPL[3]:1 Does this mean that Please let me know if this should be its own issue! |
@cgeoga can you open a separate issue |
Should be resolved by #1005 |
Actual PR was #1839 ? |
Hello folks,
I tried to make calculate a second order derivative using Enzyme and wasn't able to. Is it possible?
Here is the code
When I try to run the second derivative the Julia REPL crashes and shows an allocation error. I can try to paste it here but it exceeds the maximum number of characters, so I'd have to upload a file.
The text was updated successfully, but these errors were encountered: