1#ifndef STAN_MATH_OPENCL_PRIM_NEG_BINOMIAL_2_LOG_GLM_LPMF_HPP
2#define STAN_MATH_OPENCL_PRIM_NEG_BINOMIAL_2_LOG_GLM_LPMF_HPP
58template <
bool propto,
typename T_y_cl,
typename T_x_cl,
typename T_alpha_cl,
59 typename T_beta_cl,
typename T_phi_cl,
61 T_x_cl, T_y_cl, T_alpha_cl, T_beta_cl, T_phi_cl>* =
nullptr>
62inline return_type_t<T_x_cl, T_alpha_cl, T_beta_cl, T_phi_cl>
64 const T_alpha_cl& alpha,
const T_beta_cl&
beta,
65 const T_phi_cl& phi) {
66 static constexpr const char* function =
"neg_binomial_2_log_glm_lpmf(OpenCL)";
67 using T_partials_return
75 const size_t N = x.rows();
76 const size_t M = x.cols();
78 if constexpr (is_y_vector) {
84 if constexpr (is_phi_vector) {
88 if constexpr (is_alpha_vector) {
113 const int wgs = (N + local_size - 1) / local_size;
115 const bool need_theta_derivative
116 = is_any_autodiff_v<T_x_cl, T_beta_cl, T_alpha_cl>;
118 const bool need_theta_derivative_sum
119 = need_theta_derivative && !is_alpha_vector;
121 const bool need_phi_derivative = is_autodiff_v<T_phi_cl>;
122 const bool need_phi_derivative_sum = need_phi_derivative && !is_phi_vector;
124 need_phi_derivative ? (need_phi_derivative_sum ? wgs : N) : 0, 1);
126 const bool need_logp2
128 const bool need_logp3
131 && (is_y_vector || is_phi_vector);
136 cl::NDRange(local_size * wgs), cl::NDRange(local_size), logp_cl,
137 theta_derivative_cl, theta_derivative_sum_cl, phi_derivative_cl,
138 y_val_cl, x_val, alpha_val_cl, beta_val, phi_val_cl, N, M, is_y_vector,
139 is_alpha_vector, is_phi_vector, need_theta_derivative,
140 need_theta_derivative_sum, need_phi_derivative, need_phi_derivative_sum,
141 need_logp1, need_logp2, need_logp3, need_logp4);
142 }
catch (
const cl::Error&
e) {
147 if (!std::isfinite(logp)) {
149 check_cl(function,
"Vector of dependent variables", y_val,
151 check_cl(function,
"Intercept", alpha_val,
"finite"),
152 check_cl(function,
"Precision parameter", phi_val,
"positive finite"))
156 check_cl(function,
"Weight vector", beta_val,
"finite")
159 check_cl(function,
"Precision parameter", phi_val,
"positive finite")
168 logp +=
lgamma(y_val + phi_val) * N;
173 if constexpr (is_autodiff_v<T_x_cl>) {
174 partials<0>(ops_partials)
177 if constexpr (is_autodiff_v<T_beta_cl>) {
180 theta_derivative_cl.
buffer(), 1, theta_derivative_cl.
rows());
182 = theta_derivative_transpose_cl * x_val;
183 partials<2>(ops_partials)
185 edge3_partials_transpose_cl.
cols(), 1);
186 if (beta_val.rows() != 0) {
187 edge<2>(ops_partials)
188 .partials_.add_write_event(
192 if constexpr (is_autodiff_v<T_alpha_cl>) {
193 if constexpr (is_alpha_vector) {
194 partials<1>(ops_partials) = std::move(theta_derivative_cl);
196 partials<1>(ops_partials)[0]
200 if constexpr (is_autodiff_v<T_phi_cl>) {
201 if constexpr (is_phi_vector) {
202 partials<3>(ops_partials) = std::move(phi_derivative_cl);
207 return ops_partials.build(logp);
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.
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.
results_cl< T_results... > results(T_results &&... results)
Deduces types for constructing results_cl object.
auto transpose(Arg &&a)
Transposes 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, out_buffer, in_buffer, in_buffer, in_buffer, in_buffer, in_buffer, int, int, int, int, int, int, int, int, int, int, int, int, int > neg_binomial_2_log_glm("neg_binomial_2_log_glm", {digamma_device_function, log1p_exp_device_function, neg_binomial_2_log_glm_kernel_code}, {{"REDUCTION_STEP_SIZE", 4}, {"LOCAL_SIZE_", 64}})
See the docs for neg_binomial_2_log_glm_lpmf() .
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...
return_type_t< T_x_cl, T_alpha_cl, T_beta_cl, T_phi_cl > neg_binomial_2_log_glm_lpmf(const T_y_cl &y, const T_x_cl &x, const T_alpha_cl &alpha, const T_beta_cl &beta, const T_phi_cl &phi)
Returns the log PMF of the Generalized Linear Model (GLM) with Negative-Binomial-2 distribution and l...
auto from_matrix_cl(const T &src)
Copies the source matrix that is stored on the OpenCL device to the destination Eigen matrix.
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.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
static constexpr double e()
Return the base of the natural logarithm.
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > lmultiply(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
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.
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 ...
Checks if decayed type is a var, fvar, or arithmetic.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...