Automatic Differentiation
 
Loading...
Searching...
No Matches
ordered_logistic_glm_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_ORDERED_LOGISTIC_GLM_LPMF_HPP
2#define STAN_MATH_OPENCL_PRIM_ORDERED_LOGISTIC_GLM_LPMF_HPP
3#ifdef STAN_OPENCL
4
12
21#include <cmath>
22
23namespace stan {
24namespace math {
25
48template <bool propto, typename T_y, typename T_x, typename T_beta,
49 typename T_cuts,
51 T_cuts>* = nullptr>
53 const T_y& y, const T_x& x, const T_beta& beta, const T_cuts& cuts) {
54 using Eigen::Array;
55 using Eigen::Dynamic;
56 using Eigen::Matrix;
57 using Eigen::VectorXd;
58 using std::isfinite;
59 using T_partials_return = partials_return_t<T_beta, T_cuts>;
60 constexpr bool is_y_vector = !is_stan_scalar<T_y>::value;
61
62 static constexpr const char* function = "ordered_logistic_glm_lpmf";
63
64 const size_t N_instances = x.rows();
65 const size_t N_attributes = x.cols();
66 const size_t N_classes = math::size(cuts) + 1;
67
68 if (is_y_vector) {
69 check_size_match(function, "Rows of ", "x", N_instances, "rows of ", "y",
70 math::size(y));
71 }
72 check_size_match(function, "Columns of ", "x", N_attributes, "Size of",
73 "beta", math::size(beta));
74
75 const auto& cuts_val = eval(value_of(cuts));
76 if (N_classes >= 2) {
77 auto cuts_head = block_zero_based(cuts_val, 0, 0, math::size(cuts) - 1, 1);
78 auto cuts_tail = block_zero_based(cuts_val, 1, 0, math::size(cuts) - 1, 1);
79 check_cl(function, "Cuts", cuts_head, "ordered and finite")
80 = cuts_head < cuts_tail && isfinite(cuts_head) && isfinite(cuts_tail);
81 } else {
82 check_cl(function, "Cuts", cuts_val, "finite") = isfinite(cuts_val);
83 }
84
85 if (N_instances == 0 || N_classes == 1) {
86 return 0;
87 }
89 return 0;
90 }
91
92 const auto& y_val = eval(value_of(y));
93 const auto& x_val = eval(value_of(x));
94 const auto& beta_val = eval(value_of(beta));
95
96 const auto& y_val_cl = to_matrix_cl(y_val);
97
98 const int local_size
99 = opencl_kernels::ordered_logistic_glm.get_option("LOCAL_SIZE_");
100 const int wgs = (N_instances + local_size - 1) / local_size;
101
102 bool need_location_derivative = !is_constant_all<T_x, T_beta>::value;
103 bool need_cuts_derivative = !is_constant_all<T_cuts>::value;
104 matrix_cl<double> logp_cl(wgs, 1);
105 matrix_cl<double> location_sum_cl(wgs, 1);
106 matrix_cl<double> location_derivative_cl(need_location_derivative ? 1 : 0,
107 N_instances);
108 matrix_cl<double> cuts_derivative_cl(N_classes - 1,
109 need_cuts_derivative ? wgs : 0);
110
111 try {
113 cl::NDRange(local_size * wgs), cl::NDRange(local_size), location_sum_cl,
114 logp_cl, location_derivative_cl, cuts_derivative_cl, y_val_cl, x_val,
115 beta_val, cuts_val, N_instances, N_attributes, N_classes, is_y_vector,
116 need_location_derivative, need_cuts_derivative);
117 } catch (const cl::Error& e) {
118 check_opencl_error(function, e);
119 }
120
121 T_partials_return logp = sum(from_matrix_cl(logp_cl));
122
123 if (!std::isfinite(sum(from_matrix_cl(location_sum_cl)))) {
124 check_cl(function, "Vector of dependent variables", y_val,
125 "between 0 and number of classes")
126 = y_val >= 1 && y_val <= static_cast<int>(N_classes);
127 check_cl(function, "Design matrix", x_val, "finite") = isfinite(x_val);
128 check_cl(function, "Weight vector", beta_val, "finite")
129 = isfinite(beta_val);
130 }
131
132 auto ops_partials = make_partials_propagator(x, beta, cuts);
134 partials<0>(ops_partials)
135 = transpose(location_derivative_cl) * transpose(beta_val);
136 }
138 matrix_cl<double> edge2_partials_transpose = location_derivative_cl * x_val;
139 partials<1>(ops_partials) = matrix_cl<double>(
140 edge2_partials_transpose.buffer(), edge2_partials_transpose.cols(),
141 edge2_partials_transpose.rows());
142 if (beta.rows() != 0) {
143 edge<1>(ops_partials)
144 .partials_.add_write_event(
145 edge2_partials_transpose.write_events().back());
146 }
147 }
149 if (wgs == 1) {
150 partials<2>(ops_partials) = std::move(cuts_derivative_cl);
151 } else {
152 partials<2>(ops_partials) = rowwise_sum(cuts_derivative_cl);
153 }
154 }
155 return ops_partials.build(logp);
156}
157
158} // namespace math
159} // namespace stan
160
161#endif
162#endif
const cl::Buffer & buffer() const
const tbb::concurrent_vector< cl::Event > & write_events() const
Get the events from the event stacks.
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.
auto transpose(Arg &&a)
Transposes a kernel generator expression.
auto rowwise_sum(T &&a)
Rowwise sum reduction of a kernel generator expression.
const kernel_cl< out_buffer, out_buffer, out_buffer, out_buffer, in_buffer, in_buffer, in_buffer, in_buffer, int, int, int, int, int, int > ordered_logistic_glm("ordered_logistic_glm", {log1p_exp_device_function, log1m_exp_device_function, inv_logit_device_function, ordered_logistic_glm_kernel_code}, {{"REDUCTION_STEP_SIZE", 4}, {"LOCAL_SIZE_", 64}})
See the docs for ordered_logistic_glm() .
return_type_t< T_x, T_beta, T_cuts > ordered_logistic_glm_lpmf(const T_y &y, const T_x &x, const T_beta &beta, const T_cuts &cuts)
Returns the log PMF of the ordinal regression Generalized Linear Model (GLM).
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
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.
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.
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition beta.hpp:51
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
typename partials_return_type< Args... >::type partials_return_t
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...