1 #ifdef STAN_OPENCL
2 #include <stan/math/opencl/rev.hpp>
3 #include <stan/math.hpp>
4 #include <gtest/gtest.h>
5 #include <test/unit/math/opencl/util.hpp>
6 #include <vector>
7 
TEST(ProbDistributionsGamma,error_checking)8 TEST(ProbDistributionsGamma, error_checking) {
9   int N = 3;
10 
11   Eigen::VectorXd y(N);
12   y << -2.0, 0.5, 1;
13   Eigen::VectorXd y_size(N - 1);
14   y_size << 0.1, 0.5;
15   Eigen::VectorXd y_value(N);
16   y_value << NAN, 0.5, 0.99;
17 
18   Eigen::VectorXd alpha(N);
19   alpha << 0.3, 0.8, 1.0;
20   Eigen::VectorXd alpha_size(N - 1);
21   alpha_size << 0.3, 0.8;
22   Eigen::VectorXd alpha_value1(N);
23   alpha_value1 << 0.3, -0.8, 0.5;
24   Eigen::VectorXd alpha_value2(N);
25   alpha_value2 << 0.3, INFINITY, 0.5;
26 
27   Eigen::VectorXd beta(N);
28   beta << 0.3, 0.8, 1.0;
29   Eigen::VectorXd beta_size(N - 1);
30   beta_size << 0.3, 0.8;
31   Eigen::VectorXd beta_value1(N);
32   beta_value1 << 0.3, -0.8, 0.5;
33   Eigen::VectorXd beta_value2(N);
34   beta_value2 << 0.3, INFINITY, 0.5;
35 
36   stan::math::matrix_cl<double> y_cl(y);
37   stan::math::matrix_cl<double> y_size_cl(y_size);
38   stan::math::matrix_cl<double> y_value_cl(y_value);
39 
40   stan::math::matrix_cl<double> alpha_cl(alpha);
41   stan::math::matrix_cl<double> alpha_size_cl(alpha_size);
42   stan::math::matrix_cl<double> alpha_value1_cl(alpha_value1);
43   stan::math::matrix_cl<double> alpha_value2_cl(alpha_value2);
44 
45   stan::math::matrix_cl<double> beta_cl(beta);
46   stan::math::matrix_cl<double> beta_size_cl(beta_size);
47   stan::math::matrix_cl<double> beta_value1_cl(beta_value1);
48   stan::math::matrix_cl<double> beta_value2_cl(beta_value2);
49 
50   EXPECT_NO_THROW(stan::math::gamma_lpdf(y_cl, alpha_cl, beta_cl));
51 
52   EXPECT_THROW(stan::math::gamma_lpdf(y_size_cl, alpha_cl, beta_cl),
53                std::invalid_argument);
54   EXPECT_THROW(stan::math::gamma_lpdf(y_cl, alpha_size_cl, beta_cl),
55                std::invalid_argument);
56   EXPECT_THROW(stan::math::gamma_lpdf(y_cl, alpha_cl, beta_size_cl),
57                std::invalid_argument);
58 
59   EXPECT_THROW(stan::math::gamma_lpdf(y_value_cl, alpha_cl, beta_cl),
60                std::domain_error);
61   EXPECT_THROW(stan::math::gamma_lpdf(y_cl, alpha_value1_cl, beta_cl),
62                std::domain_error);
63   EXPECT_THROW(stan::math::gamma_lpdf(y_cl, alpha_cl, beta_value1_cl),
64                std::domain_error);
65   EXPECT_THROW(stan::math::gamma_lpdf(y_cl, alpha_value2_cl, beta_cl),
66                std::domain_error);
67   EXPECT_THROW(stan::math::gamma_lpdf(y_cl, alpha_cl, beta_value2_cl),
68                std::domain_error);
69 }
70 
71 auto gamma_lpdf_functor
__anon6f1668850102(const auto& y, const auto& alpha, const auto& beta) 72     = [](const auto& y, const auto& alpha, const auto& beta) {
73         return stan::math::gamma_lpdf(y, alpha, beta);
74       };
75 auto gamma_lpdf_functor_propto
__anon6f1668850202(const auto& y, const auto& alpha, const auto& beta) 76     = [](const auto& y, const auto& alpha, const auto& beta) {
77         return stan::math::gamma_lpdf<true>(y, alpha, beta);
78       };
79 
TEST(ProbDistributionsGamma,opencl_matches_cpu_small)80 TEST(ProbDistributionsGamma, opencl_matches_cpu_small) {
81   int N = 3;
82 
83   Eigen::VectorXd y(N);
84   y << 0, 0.5, 1;
85   Eigen::VectorXd alpha(N);
86   alpha << 0.3, 0.8, 1.0;
87   Eigen::VectorXd beta(N);
88   beta << 0.3, 0.8, 1.0;
89 
90   stan::math::test::compare_cpu_opencl_prim_rev(gamma_lpdf_functor, y, alpha,
91                                                 beta);
92   stan::math::test::compare_cpu_opencl_prim_rev(gamma_lpdf_functor_propto, y,
93                                                 alpha, beta);
94   stan::math::test::compare_cpu_opencl_prim_rev(
95       gamma_lpdf_functor, y.transpose().eval(), alpha.transpose().eval(),
96       beta.transpose().eval());
97   stan::math::test::compare_cpu_opencl_prim_rev(
98       gamma_lpdf_functor_propto, y.transpose().eval(), alpha.transpose().eval(),
99       beta.transpose().eval());
100 }
TEST(ProbDistributionsGamma,opencl_matches_cpu_small_negative_y)101 TEST(ProbDistributionsGamma, opencl_matches_cpu_small_negative_y) {
102   int N = 3;
103 
104   Eigen::VectorXd y(N);
105   y << -10, 0.5, 1;
106   Eigen::VectorXd alpha(N);
107   alpha << 0.3, 0.8, 1.0;
108   Eigen::VectorXd beta(N);
109   beta << 0.3, 0.8, 1.0;
110 
111   stan::math::test::compare_cpu_opencl_prim_rev(gamma_lpdf_functor, y, alpha,
112                                                 beta);
113   stan::math::test::compare_cpu_opencl_prim_rev(gamma_lpdf_functor_propto, y,
114                                                 alpha, beta);
115 }
116 
TEST(ProbDistributionsGamma,opencl_broadcast_y)117 TEST(ProbDistributionsGamma, opencl_broadcast_y) {
118   int N = 3;
119 
120   double y = 0.3;
121   Eigen::VectorXd alpha(N);
122   alpha << 0.3, 0.8, 1.0;
123   Eigen::VectorXd beta(N);
124   beta << 0.3, 0.8, 1.0;
125 
126   stan::math::test::test_opencl_broadcasting_prim_rev<0>(gamma_lpdf_functor, y,
127                                                          alpha, beta);
128   stan::math::test::test_opencl_broadcasting_prim_rev<0>(
129       gamma_lpdf_functor_propto, y, alpha, beta);
130   stan::math::test::test_opencl_broadcasting_prim_rev<0>(
131       gamma_lpdf_functor, y, alpha.transpose().eval(), beta);
132   stan::math::test::test_opencl_broadcasting_prim_rev<0>(
133       gamma_lpdf_functor_propto, y, alpha, beta.transpose().eval());
134 }
135 
TEST(ProbDistributionsGamma,opencl_broadcast_alpha)136 TEST(ProbDistributionsGamma, opencl_broadcast_alpha) {
137   int N = 3;
138 
139   Eigen::VectorXd y(N);
140   y << 0.3, 0.8, 1.0;
141   double alpha = 0.3;
142   Eigen::VectorXd beta(N);
143   beta << 0.3, 0.8, 1.0;
144 
145   stan::math::test::test_opencl_broadcasting_prim_rev<1>(gamma_lpdf_functor, y,
146                                                          alpha, beta);
147   stan::math::test::test_opencl_broadcasting_prim_rev<1>(
148       gamma_lpdf_functor_propto, y, alpha, beta);
149   stan::math::test::test_opencl_broadcasting_prim_rev<1>(
150       gamma_lpdf_functor, y.transpose().eval(), alpha, beta);
151   stan::math::test::test_opencl_broadcasting_prim_rev<1>(
152       gamma_lpdf_functor_propto, y, alpha, beta.transpose().eval());
153 }
154 
TEST(ProbDistributionsGamma,opencl_broadcast_beta)155 TEST(ProbDistributionsGamma, opencl_broadcast_beta) {
156   int N = 3;
157 
158   Eigen::VectorXd y(N);
159   y << 0.3, 0.8, 1.0;
160   Eigen::VectorXd alpha(N);
161   alpha << 0.3, 0.8, 1.0;
162   double beta = 0.3;
163 
164   stan::math::test::test_opencl_broadcasting_prim_rev<2>(gamma_lpdf_functor, y,
165                                                          alpha, beta);
166   stan::math::test::test_opencl_broadcasting_prim_rev<2>(
167       gamma_lpdf_functor_propto, y, alpha, beta);
168   stan::math::test::test_opencl_broadcasting_prim_rev<2>(
169       gamma_lpdf_functor, y.transpose().eval(), alpha, beta);
170   stan::math::test::test_opencl_broadcasting_prim_rev<2>(
171       gamma_lpdf_functor_propto, y, alpha.transpose().eval(), beta);
172 }
173 
TEST(ProbDistributionsGamma,opencl_matches_cpu_big)174 TEST(ProbDistributionsGamma, opencl_matches_cpu_big) {
175   int N = 153;
176 
177   Eigen::Matrix<double, Eigen::Dynamic, 1> y
178       = Eigen::Array<double, Eigen::Dynamic, 1>::Random(N, 1).abs();
179   Eigen::Matrix<double, Eigen::Dynamic, 1> alpha
180       = Eigen::Array<double, Eigen::Dynamic, 1>::Random(N, 1).abs();
181   Eigen::Matrix<double, Eigen::Dynamic, 1> beta
182       = Eigen::Array<double, Eigen::Dynamic, 1>::Random(N, 1).abs();
183 
184   stan::math::test::compare_cpu_opencl_prim_rev(gamma_lpdf_functor, y, alpha,
185                                                 beta);
186   stan::math::test::compare_cpu_opencl_prim_rev(gamma_lpdf_functor_propto, y,
187                                                 alpha, beta);
188   stan::math::test::compare_cpu_opencl_prim_rev(
189       gamma_lpdf_functor, y.transpose().eval(), alpha.transpose().eval(),
190       beta.transpose().eval());
191   stan::math::test::compare_cpu_opencl_prim_rev(
192       gamma_lpdf_functor_propto, y.transpose().eval(), alpha.transpose().eval(),
193       beta.transpose().eval());
194 }
195 
196 #endif
197