Automatic Differentiation
 
Loading...
Searching...
No Matches
ordered_logistic_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_ORDERED_LOGISTIC_LPMF_HPP
2#define STAN_MATH_OPENCL_PRIM_ORDERED_LOGISTIC_LPMF_HPP
3#ifdef STAN_OPENCL
4
15
16namespace stan {
17namespace math {
18
67template <bool propto, typename T_y_cl, typename T_loc_cl, typename T_cuts_cl,
69 T_cuts_cl>* = nullptr>
71 const T_y_cl& y, const T_loc_cl& lambda, const T_cuts_cl& cuts) {
72 constexpr bool is_y_vector = !is_stan_scalar<T_y_cl>::value;
73 static constexpr const char* function = "ordered_logistic_lpmf(OpenCL)";
74
75 if (size(y) != 1) {
76 check_size_match(function, "Size of ", "y", math::size(y), "Size of",
77 "lambda", math::size(lambda));
78 }
79
80 int N_instances = max_size(y, lambda);
81 int N_classes = cuts.rows() + 1;
82 int N_cut_sets = cuts.cols();
83
84 if (N_cut_sets > 1) {
85 check_size_match(function, "Length of lambda variables ", N_instances,
86 "Number of cutpoint vectors ", N_cut_sets);
87 }
88 if (N_instances == 0 || N_classes == 1) {
89 return 0.0;
90 }
91 const auto& cuts_val = eval(value_of(cuts));
92 if (N_classes >= 2) {
93 auto cuts_head
94 = block_zero_based(cuts_val, 0, 0, cuts.rows() - 1, N_cut_sets);
95 auto cuts_tail
96 = block_zero_based(cuts_val, 1, 0, cuts.rows() - 1, N_cut_sets);
97 check_cl(function, "Cuts", cuts_head, "ordered and finite")
98 = cuts_head < cuts_tail && isfinite(cuts_head) && isfinite(cuts_tail);
99 } else if (N_classes == 1) {
100 check_cl(function, "Cuts", cuts_val, "finite") = isfinite(cuts_val);
101 }
102
104 return 0.0;
105 }
106
107 const auto& y_val = eval(value_of(y));
108 const auto& lambda_val = eval(value_of(lambda));
109
110 const auto& y_val_cl = to_matrix_cl(y_val);
111
112 const int local_size
113 = opencl_kernels::ordered_logistic.get_option("LOCAL_SIZE_");
114 const int wgs = (N_instances + local_size - 1) / local_size;
115
116 bool need_lambda_derivative = !is_constant_all<T_loc_cl>::value;
117 bool need_cuts_derivative = !is_constant_all<T_cuts_cl>::value;
118 bool need_broadcasting = N_cut_sets == 1 && N_instances != 1;
119 matrix_cl<double> logp_cl(wgs, 1);
120 matrix_cl<double> lambda_derivative_cl(N_instances,
121 need_lambda_derivative ? 1 : 0);
122 matrix_cl<double> cuts_derivative_cl(
123 N_classes - 1,
124 need_cuts_derivative ? (need_broadcasting ? wgs : N_cut_sets) : 0);
125
126 try {
128 cl::NDRange(local_size * wgs), cl::NDRange(local_size), logp_cl,
129 lambda_derivative_cl, cuts_derivative_cl, y_val_cl, lambda_val,
130 cuts_val, N_instances, N_classes, is_y_vector, !need_broadcasting,
131 need_lambda_derivative, need_cuts_derivative);
132 } catch (const cl::Error& e) {
133 check_opencl_error(function, e);
134 }
135
136 double logp = sum(from_matrix_cl(logp_cl));
137
138 if (!std::isfinite(logp)) {
139 results(check_cl(function, "Vector of dependent variables", y_val,
140 "between 0 and number of classes"),
141 check_cl(function, "lambda vector", lambda_val, "finite"))
142 = expressions(y_val >= 1 && y_val <= static_cast<int>(N_classes),
143 isfinite(lambda_val));
144 }
145 auto ops_partials = make_partials_propagator(lambda, cuts);
146
148 partials<0>(ops_partials) = lambda_derivative_cl;
149 }
151 if (need_broadcasting) {
152 partials<1>(ops_partials) = rowwise_sum(cuts_derivative_cl);
153 } else {
154 partials<1>(ops_partials) = std::move(cuts_derivative_cl);
155 }
156 }
157 return ops_partials.build(logp);
158}
159
160} // namespace math
161} // namespace stan
162#endif
163#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
void check_opencl_error(const char *function, const cl::Error &e)
Throws the domain error with specifying the OpenCL error that occurred.
isfinite_< as_operation_cl_t< T > > isfinite(T &&a)
auto check_cl(const char *function, const char *var_name, T &&y, const char *must_be)
Constructs a check on opencl matrix or expression.
Definition check_cl.hpp:219
auto block_zero_based(T &&a, int start_row, int start_col, int rows, int cols)
Block of a kernel generator expression.
results_cl< T_results... > results(T_results &&... results)
Deduces types for constructing results_cl object.
auto rowwise_sum(T &&a)
Rowwise sum reduction of a kernel generator expression.
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
const kernel_cl< out_buffer, out_buffer, out_buffer, in_buffer, in_buffer, in_buffer, int, int, int, int, int, int > ordered_logistic("ordered_logistic", {log1p_exp_device_function, log1m_exp_device_function, inv_logit_device_function, ordered_logistic_kernel_code}, {{"REDUCTION_STEP_SIZE", 4}, {"LOCAL_SIZE_", 64}})
See the docs for ordered_logistic() .
matrix_cl< scalar_type_t< T > > to_matrix_cl(T &&src)
Copies the source Eigen matrix, std::vector or scalar to the destination matrix that is stored on the...
Definition copy.hpp:45
return_type_t< T_y_cl, T_loc_cl, T_cuts_cl > ordered_logistic_lpmf(const T_y_cl &y, const T_loc_cl &lambda, const T_cuts_cl &cuts)
Returns the (natural) log probability of the specified array of integers given the vector of continuo...
auto from_matrix_cl(const T &src)
Copies the source matrix that is stored on the OpenCL device to the destination Eigen matrix.
Definition copy.hpp:61
require_all_t< is_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_prim_or_rev_kernel_expression_t
Require type satisfies is_prim_or_rev_kernel_expression.
size_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:18
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
size_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:19
static constexpr double e()
Return the base of the natural logarithm.
Definition constants.hpp:20
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
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
Checks if decayed type is a var, fvar, or arithmetic.
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...