Skip to content

Commit 6fdd64a

Browse files
authored
Merge pull request #3428 from jachymb/bugfix/opencl-neg-binomial-2-log-glm
Fix OpenCL `neg_binomial_2_log_glm_lpmf` with vector phi and scalar alpha
2 parents e4745c8 + 10cbe4e commit 6fdd64a

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎stan/math/opencl/prim/neg_binomial_2_log_glm_lpmf.hpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ neg_binomial_2_log_glm_lpmf(const T_y_cl& y, const T_x_cl& x,
124124
const bool need_theta_derivative_sum
125125
= need_theta_derivative && !is_alpha_vector;
126126
matrix_cl<double> theta_derivative_sum_cl(wgs, 1);
127-
const bool need_phi_derivative_sum = !is_alpha_vector;
128-
const bool need_phi_derivative
129-
= is_autodiff_v<T_phi_cl> || need_phi_derivative_sum;
127+
const bool need_phi_derivative = is_autodiff_v<T_phi_cl>;
128+
const bool need_phi_derivative_sum = need_phi_derivative && !is_phi_vector;
130129
matrix_cl<double> phi_derivative_cl(
131130
need_phi_derivative ? (need_phi_derivative_sum ? wgs : N) : 0, 1);
132131
const bool need_logp1 = include_summand<propto>::value;

‎test/unit/math/opencl/rev/neg_binomial_2_log_glm_lpmf_test.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,29 @@ TEST(ProbDistributionsNegBinomial2LogGLM, opencl_matches_cpu_big) {
237237
stan::math::test::compare_cpu_opencl_prim_rev(
238238
neg_binomial_2_log_glm_lpmf_functor_propto, y, x, alpha, beta, phi);
239239
}
240+
241+
TEST(ProbDistributionsNegBinomial2LogGLM,
242+
opencl_matches_cpu_small_vector_phi_scalar_alpha) {
243+
int N = 3;
244+
int M = 2;
245+
246+
vector<int> y{0, 1, 5};
247+
Matrix<double, Dynamic, Dynamic> x(N, M);
248+
x << -12, 46, -42, 24, 25, 27;
249+
Matrix<double, Dynamic, 1> beta(M, 1);
250+
beta << 0.3, 2;
251+
double alpha = 0.3;
252+
Matrix<double, Dynamic, 1> phi(N, 1);
253+
phi << 0.1, 0.5, 1.2;
254+
Matrix<double, Dynamic, 1> alpha_vec(N, 1);
255+
alpha_vec << 0.3, -0.8, 1.8;
256+
257+
stan::math::test::compare_cpu_opencl_prim_rev(
258+
neg_binomial_2_log_glm_lpmf_functor, y, x, alpha, beta, phi);
259+
stan::math::test::compare_cpu_opencl_prim_rev(
260+
neg_binomial_2_log_glm_lpmf_functor_propto, y, x, alpha, beta, phi);
261+
// and vector alpha with scalar phi
262+
stan::math::test::compare_cpu_opencl_prim_rev(
263+
neg_binomial_2_log_glm_lpmf_functor, y, x, alpha_vec, beta, 1.2);
264+
}
240265
#endif

0 commit comments

Comments
 (0)