Automatic Differentiation
 
Loading...
Searching...
No Matches
diag_post_multiply.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_REV_DIAG_POST_MULTIPLY_HPP
2#define STAN_MATH_OPENCL_REV_DIAG_POST_MULTIPLY_HPP
3#ifdef STAN_OPENCL
4
11
12namespace stan {
13namespace math {
14
27template <
28 typename T1, typename T2, require_any_var_t<T1, T2>* = nullptr,
29 require_all_nonscalar_prim_or_rev_kernel_expression_t<T1, T2>* = nullptr>
31 arena_t<T1> v1_arena = std::forward<T1>(v1);
32 arena_t<T2> v2_arena = std::forward<T2>(v2);
33
34 matrix_cl<double> res_val
35 = diag_post_multiply(value_of(v1_arena), value_of(v2_arena));
36
37 return make_callback_var(
38 res_val,
39 [v1_arena, v2_arena](const vari_value<matrix_cl<double>>& res) mutable {
40 if (v2_arena.rows() == 1) {
41 auto v1_adj_inc
42 = elt_multiply(res.adj(), colwise_broadcast(value_of(v2_arena)));
43 auto v2_adj_inc
44 = colwise_sum(elt_multiply(res.adj(), value_of(v1_arena)));
45 matrix_cl<double> tmp;
46 auto&& v1_adj = adjoint_of(v1_arena);
47 results(v1_adj, tmp) = expressions(
48 calc_if<!is_constant<std::decay_t<T1>>::value>(v1_adj
49 + v1_adj_inc),
50 calc_if<!is_constant<std::decay_t<T2>>::value>(v2_adj_inc));
51
52 if (!is_constant<std::decay_t<T2>>::value) {
53 while (tmp.rows() > 1) {
54 tmp = eval(colwise_sum(tmp));
55 }
56 adjoint_of(v2_arena) += tmp;
57 }
58 } else {
59 auto v1_adj_inc = elt_multiply(
60 res.adj(), colwise_broadcast(transpose(value_of(v2_arena))));
61 auto v2_adj_inc
62 = colwise_sum(elt_multiply(res.adj(), value_of(v1_arena)));
64 auto&& v1_adj = adjoint_of(v1_arena);
65 results(v1_adj, tmp) = expressions(
66 calc_if<!is_constant<std::decay_t<T1>>::value>(v1_adj
67 + v1_adj_inc),
68 calc_if<!is_constant<std::decay_t<T2>>::value>(v2_adj_inc));
69 if (!is_constant<std::decay_t<T2>>::value) {
70 while (tmp.rows() > 1) {
71 tmp = eval(colwise_sum(tmp));
72 }
73 adjoint_of(v2_arena) += transpose(tmp);
74 }
75 }
76 });
77}
78
79} // namespace math
80} // namespace stan
81
82#endif
83#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
results_cl< T_results... > results(T_results &&... results)
Deduces types for constructing results_cl object.
auto transpose(Arg &&a)
Transposes a kernel generator expression.
calc_if_< true, as_operation_cl_t< T > > calc_if(T &&a)
Definition calc_if.hpp:121
auto colwise_broadcast(T &&a)
Broadcast an expression in colwise dimmension.
auto colwise_sum(T &&a)
Column wise sum - reduction of a kernel generator expression.
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
auto diag_post_multiply(const T1 &m1, const T2 &m2)
Return the product of a matrix and the diagonal matrix formed from the vector or row_vector.
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
auto & adjoint_of(const T &x)
Returns a reference to a variable's adjoint.
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
Definition eval.hpp:20
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...