forked from stan-dev/stanc3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStan_math_code_gen_tests.ml
245 lines (229 loc) · 7.79 KB
/
Stan_math_code_gen_tests.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
open Middle
open Stan_math_backend
open Core_kernel
open Fmt
open Stan_math_code_gen
let pp_fun_def ppf b =
pp_forward_decl String.Set.empty ppf b ;
cut ppf () ;
match get_impl b with Some b -> pp_function_body ppf b | None -> ()
let%expect_test "udf" =
let with_no_loc stmt =
Stmt.Fixed.{pattern= stmt; meta= Locations.no_span_num}
in
let w e = Expr.{Fixed.pattern= e; meta= Typed.Meta.empty} in
{ fdrt= None
; fdname= "sars"
; fdsuffix= FnPlain
; fdcaptures= None
; fdargs= [(DataOnly, "x", UMatrix); (AutoDiffable, "y", URowVector)]
; fdbody=
Stmt.Fixed.Pattern.Return
(Some
( w
@@ FunApp
(StanLib ("add", FnPlain), [w @@ Var "x"; w @@ Lit (Int, "1")])
))
|> with_no_loc |> List.return |> Stmt.Fixed.Pattern.Block |> with_no_loc
|> Some
; fdloc= Location_span.empty }
|> strf "@[<v>%a" pp_fun_def |> print_endline ;
[%expect
{|
template <typename T0__, typename T1__>
void
sars(const T0__& x, const T1__& y, std::ostream* pstream__) ;
struct sars_functor__ {
template <typename T0__, typename T1__>
void
operator()(const T0__& x, const T1__& y, std::ostream* pstream__) const
{
return sars(x, y, pstream__);
}
};
template <typename T0__, typename T1__>
void
sars(const T0__& x_arg__, const T1__& y_arg__, std::ostream* pstream__) {
using local_scalar_t__ = stan::promote_args_t<stan::value_type_t<T0__>,
stan::value_type_t<T1__>>;
int current_statement__ = 0;
const auto& x = to_ref(x_arg__);
const auto& y = to_ref(y_arg__);
static constexpr bool propto__ = true;
(void) propto__;
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
(void) DUMMY_VAR__; // suppress unused var warning
try {
return add(x, 1);
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
// Next line prevents compiler griping about no return
throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
}
} |}]
let%expect_test "udf-expressions" =
let with_no_loc stmt =
Stmt.Fixed.{pattern= stmt; meta= Locations.no_span_num}
in
let w e = Expr.{Fixed.pattern= e; meta= Typed.Meta.empty} in
{ fdrt= Some UMatrix
; fdname= "sars"
; fdsuffix= FnPlain
; fdcaptures= None
; fdargs=
[ (DataOnly, "x", UMatrix)
; (AutoDiffable, "y", URowVector)
; (AutoDiffable, "z", URowVector)
; (AutoDiffable, "w", UArray UMatrix) ]
; fdbody=
Stmt.Fixed.Pattern.Return
(Some
( w
@@ FunApp
(StanLib ("add", FnPlain), [w @@ Var "x"; w @@ Lit (Int, "1")])
))
|> with_no_loc |> List.return |> Stmt.Fixed.Pattern.Block |> with_no_loc
|> Some
; fdloc= Location_span.empty }
|> strf "@[<v>%a" pp_fun_def |> print_endline ;
[%expect
{|
template <typename T0__, typename T1__, typename T2__, typename T3__>
Eigen::Matrix<stan::promote_args_t<stan::value_type_t<T0__>, stan::value_type_t<T1__>,
stan::value_type_t<T2__>,
T3__>, -1, -1>
sars(const T0__& x, const T1__& y, const T2__& z,
const std::vector<Eigen::Matrix<T3__, -1, -1>>& w,
std::ostream* pstream__) ;
struct sars_functor__ {
template <typename T0__, typename T1__, typename T2__, typename T3__>
Eigen::Matrix<stan::promote_args_t<stan::value_type_t<T0__>, stan::value_type_t<T1__>,
stan::value_type_t<T2__>,
T3__>, -1, -1>
operator()(const T0__& x, const T1__& y, const T2__& z,
const std::vector<Eigen::Matrix<T3__, -1, -1>>& w,
std::ostream* pstream__) const
{
return sars(x, y, z, w, pstream__);
}
};
template <typename T0__, typename T1__, typename T2__, typename T3__>
Eigen::Matrix<stan::promote_args_t<stan::value_type_t<T0__>, stan::value_type_t<T1__>,
stan::value_type_t<T2__>,
T3__>, -1, -1>
sars(const T0__& x_arg__, const T1__& y_arg__, const T2__& z_arg__,
const std::vector<Eigen::Matrix<T3__, -1, -1>>& w,
std::ostream* pstream__) {
using local_scalar_t__ = stan::promote_args_t<stan::value_type_t<T0__>,
stan::value_type_t<T1__>,
stan::value_type_t<T2__>,
T3__>;
int current_statement__ = 0;
const auto& x = to_ref(x_arg__);
const auto& y = to_ref(y_arg__);
const auto& z = to_ref(z_arg__);
static constexpr bool propto__ = true;
(void) propto__;
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
(void) DUMMY_VAR__; // suppress unused var warning
try {
return add(x, 1);
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
// Next line prevents compiler griping about no return
throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
}
} |}]
let%expect_test "closure" =
let with_no_loc stmt =
Stmt.Fixed.{pattern= stmt; meta= Locations.no_span_num}
in
let w e = Expr.{Fixed.pattern= e; meta= Typed.Meta.empty} in
{ fdrt= None
; fdname= "sars"
; fdsuffix= FnPlain
; fdcaptures=
Some
[(Ref, DataOnly, "x", UMatrix); (Copy, AutoDiffable, "y", URowVector)]
; fdargs= []
; fdbody=
Stmt.Fixed.Pattern.Return
(Some
( w
@@ FunApp
(StanLib ("add", FnPlain), [w @@ Var "x"; w @@ Lit (Int, "1")])
))
|> with_no_loc |> List.return |> Stmt.Fixed.Pattern.Block |> with_no_loc
|> Some
; fdloc= Location_span.empty }
|> strf "@[<v>%a" pp_fun_def |> print_endline ;
[%expect
{|
template <typename T0__, typename T1__>
void
sars_impl__(const T0__& x, const T1__& y, std::ostream* pstream__) ;
template<bool ref__, typename F1__>
class sars_cfunctor__ {
const Eigen::Matrix<double, -1, -1>& x;
stan::capture_type_t<Eigen::Matrix<F1__, 1, -1>, ref__> y;
public:
const size_t vars_count__;
sars_cfunctor__(const sars_cfunctor__<ref__, F1__>&) = default ;
sars_cfunctor__(sars_cfunctor__<ref__, F1__>&&) = default ;
sars_cfunctor__(const Eigen::Matrix<double, -1, -1>& x__,
const Eigen::Matrix<F1__, 1, -1>& y__)
: x(x__), y(y__), vars_count__(count_vars(x__, y__)) {}
void
operator()(std::ostream* pstream__) const
{
return sars_impl__(x, y, pstream__);
}
using captured_scalar_t__ = stan::return_type_t<F1__>;
using ValueOf__ = sars_cfunctor__<false, double>;
using CopyOf__ = sars_cfunctor__<false, stan::capture_type_t<F1__, false>>;
size_t count_vars__() const {
return vars_count__;
}
auto value_of__() const {
return ValueOf__(x, eval(value_of(y)));
}
auto deep_copy_vars__() const {
return CopyOf__(x, eval(deep_copy_vars(y)));
}
void zero_adjoints__() {
stan::math::zero_adjoints(x);
stan::math::zero_adjoints(y);
}
double* accumulate_adjoints__(double *dest) const {
return stan::math::accumulate_adjoints(dest, x, y);
}
stan::math::vari** save_varis__(stan::math::vari **dest) const {
return stan::math::save_varis(dest, x, y);
}
};
template<typename F1__>
auto sars_make__(const Eigen::Matrix<double, -1, -1>& x,
const Eigen::Matrix<F1__, 1, -1>& y) {
return sars_cfunctor__<false,
F1__>(x, y);
}
template <typename T0__, typename T1__>
void
sars_impl__(const T0__& x_arg__, const T1__& y_arg__, std::ostream* pstream__) {
using local_scalar_t__ = stan::promote_args_t<stan::value_type_t<T0__>,
stan::value_type_t<T1__>>;
int current_statement__ = 0;
const auto& x = to_ref(x_arg__);
const auto& y = to_ref(y_arg__);
static constexpr bool propto__ = true;
(void) propto__;
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
(void) DUMMY_VAR__; // suppress unused var warning
try {
return add(x, 1);
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
// Next line prevents compiler griping about no return
throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
}
} |}]