@@ -117,37 +117,50 @@ with_logabsdet_jacobian(f, x) = NoLogAbsDetJacobian(f, x)
117117end
118118
119119
120- function _with_ladj_on_mapped (@nospecialize (map_or_bc:: F ), y_with_ladj:: NoLogAbsDetJacobian ) where {F<: Union{typeof(map),typeof(broadcast)} }
121- return y_with_ladj
122- end
120+ _with_ladj_on_mapped (y_with_ladj:: NoLogAbsDetJacobian ) = y_with_ladj
123121
124- function _with_ladj_on_mapped (map_or_bc :: F , y_with_ladj:: Tuple{Any,Real} ) where {F <: Union{typeof(map),typeof(broadcast)} }
125- return y_with_ladj
126- end
122+ _with_ladj_on_mapped (y_with_ladj:: Tuple{Any,Real} ) = y_with_ladj
123+
124+ _with_ladj_on_mapped ( :: AbstractArray{T} ) where {T <: NoLogAbsDetJacobian } = T ()
127125
128126_get_all_first (x) = map (first, x)
129- # Use x -> x[2] instead of last, using last causes horrible performance in Zygote here:
130- _sum_over_second (x) = sum (x -> x[2 ], x)
127+ _get_second (x) = x[2 ]
128+ # Use _get_second instead of last, using last causes horrible performance in Zygote here:
129+ _sum_over_second (x) = sum (_get_second, x)
131130
132- function _with_ladj_on_mapped (map_or_bc :: F , y_with_ladj) where {F <: Union{typeof(map),typeof(broadcast)} }
131+ function _with_ladj_on_mapped (y_with_ladj)
133132 y = _get_all_first (y_with_ladj)
134133 ladj = _sum_over_second (y_with_ladj)
135134 (y, ladj)
136135end
137136
137+ _combine_y_ladj (l:: NoLogAbsDetJacobian , @nospecialize (r:: NoLogAbsDetJacobian )) = l
138+ _combine_y_ladj (l:: NoLogAbsDetJacobian , @nospecialize (r:: Tuple{Any,Real} )) = l
139+ _combine_y_ladj (@nospecialize (l:: Tuple{Any,Real} ), r:: NoLogAbsDetJacobian ) = r
140+ _combine_y_ladj (l:: Tuple{Any,Real} , r:: Tuple{Any,Real} ) = ((l[1 ]. .. , r[1 ]), l[2 ] + r[2 ])
141+
142+ function _with_ladj_on_mapped (y_with_ladj:: Tuple {NoLogAbsDetJacobian, Vararg{Union{NoLogAbsDetJacobian, Tuple{Any,Real}}}})
143+ return first (y_with_ladj)
144+ end
145+
146+ function _with_ladj_on_mapped (y_with_ladj:: Tuple {Tuple{Any,Real}, Vararg{Union{NoLogAbsDetJacobian, Tuple{Any,Real}}}})
147+ a, bs = first (y_with_ladj), Base. tail (y_with_ladj)
148+ return foldl (_combine_y_ladj, bs, init = ((a[1 ],), a[2 ]))
149+ end
150+
138151@static if VERSION >= v " 1.6"
139152 function with_logabsdet_jacobian (mapped_f:: Base.Broadcast.BroadcastFunction , X)
140153 f = mapped_f. f
141154 y_with_ladj = broadcast (Base. Fix1 (with_logabsdet_jacobian, f), X)
142- _with_ladj_on_mapped (broadcast, y_with_ladj)
155+ _with_ladj_on_mapped (y_with_ladj)
143156 end
144157end
145158
146159function with_logabsdet_jacobian (mapped_f:: Base.Fix1{<:Union{typeof(map),typeof(broadcast)}} , X)
147160 map_or_bc = mapped_f. f
148161 f = mapped_f. x
149162 y_with_ladj = map_or_bc (Base. Fix1 (with_logabsdet_jacobian, f), X)
150- _with_ladj_on_mapped (map_or_bc, y_with_ladj)
163+ _with_ladj_on_mapped (y_with_ladj)
151164end
152165
153166
0 commit comments