How to implement AD for this function?

yeah, i missed that bang at the end, see A bug in ForwardDiff? - #4 by amrods