Skip to content

Commit 0e9e096

Browse files
authored
Merge pull request #2418 from stan-dev/feature/constexpr-double-ops-partials
Cleanup Ops and Partials
2 parents 51669a6 + 30cbdf7 commit 0e9e096

22 files changed

Lines changed: 350 additions & 374 deletions

make/cpplint

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ PYTHON ?= python
22

33
.PHONY: cpplint
44
cpplint:
5-
@$(PYTHON) $(CPPLINT)/cpplint.py --output=vs7 --counting=detailed --root=stan --extension=hpp,cpp --filter=-runtime/indentation_namespace,-build/c++11,-readability/namespace,-legal/copyright,-whitespace/indent,-runtime/reference,-build/header_guard,-build/include_order,-build/include_what_you_use,-runtime/string,-build/namespaces $(shell (find test/unit -name '*.hpp' -o -name '*.cpp') && (find stan -name '*.hpp' -o -name '*.cpp'))
5+
@$(PYTHON) $(CPPLINT)/cpplint.py --output=vs7 --counting=detailed --root=stan --extension=hpp,cpp --filter=-runtime/indentation_namespace,-build/c++11,-readability/namespace,-legal/copyright,-whitespace/indent,-runtime/reference,-build/header_guard,-build/include_order,-build/include_what_you_use,-runtime/string,-build/namespaces $(shell (find stan -name '*.hpp' -o -name '*.cpp'))
6+
@$(PYTHON) $(CPPLINT)/cpplint.py --output=vs7 --counting=detailed --root=stan --extension=hpp,cpp --filter=-runtime/indentation_namespace,-build/c++11,-readability/namespace,-legal/copyright,-whitespace/indent,-runtime/reference,-build/header_guard,-build/include_order,-build/include_what_you_use,-runtime/string,-build/namespaces $(shell (find test/unit -name '*.hpp' -o -name '*.cpp'))

makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,22 @@ doxygen:
9595
##
9696
.PHONY: clean clean-doxygen clean-deps clean-all
9797
clean:
98+
@echo ' removing generated test files'
99+
@$(RM) $(wildcard test/prob/generate_tests$(EXE))
100+
@$(RM) $(call findfiles,test/prob,*_generated_v_test.cpp)
101+
@$(RM) $(call findfiles,test/prob,*_generated_vv_test.cpp)
102+
@$(RM) $(call findfiles,test/prob,*_generated_fd_test.cpp)
103+
@$(RM) $(call findfiles,test/prob,*_generated_fv_test.cpp)
104+
@$(RM) $(call findfiles,test/prob,*_generated_ffd_test.cpp)
105+
@$(RM) $(call findfiles,test/prob,*_generated_ffv_test.cpp)
106+
@$(RM) $(call findfiles,test/prob,*_generated_*_test.cpp)
98107
@echo ' removing test executables'
99108
@$(RM) $(call findfiles,test,*_test$(EXE))
100109
@$(RM) $(call findfiles,test,*_test.d)
101110
@$(RM) $(call findfiles,test,*_test.d.*)
102111
@$(RM) $(call findfiles,test,*_test.xml)
103112
@$(RM) $(call findfiles,test,*.o)
104113
@$(RM) $(wildcard $(GTEST)/src/gtest-all.o)
105-
@echo ' removing generated test files'
106-
@$(RM) $(wildcard test/prob/generate_tests$(EXE))
107-
@$(RM) $(call findfiles,test/prob,*_generated_*_test.cpp)
108114
@$(RM) $(TEST_STANC)
109115

110116
clean-doxygen:

stan/math/opencl/prim/bernoulli_logit_glm_lpmf.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,13 @@ return_type_t<T_x_cl, T_alpha_cl, T_beta_cl> bernoulli_logit_glm_lpmf(
152152
// transposition of a vector can be done without copying
153153
const matrix_cl<double> theta_derivative_transpose_cl(
154154
theta_derivative_cl.buffer(), 1, theta_derivative_cl.rows());
155-
matrix_cl<double>& edge3_partials
156-
= forward_as<matrix_cl<double>&>(ops_partials.edge3_.partials_);
157155
matrix_cl<double> edge3_partials_transpose_cl
158156
= theta_derivative_transpose_cl * x_val;
159-
edge3_partials = matrix_cl<double>(edge3_partials_transpose_cl.buffer(),
160-
edge3_partials_transpose_cl.cols(), 1);
157+
ops_partials.edge3_.partials_
158+
= matrix_cl<double>(edge3_partials_transpose_cl.buffer(),
159+
edge3_partials_transpose_cl.cols(), 1);
161160
if (beta_val.rows() != 0) {
162-
edge3_partials.add_write_event(
161+
ops_partials.edge3_.partials_.add_write_event(
163162
edge3_partials_transpose_cl.write_events().back());
164163
}
165164
}

stan/math/opencl/prim/categorical_logit_glm_lpmf.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifdef STAN_OPENCL
44

55
#include <stan/math/opencl/prim/size.hpp>
6+
#include <stan/math/opencl/rev/arena_matrix_cl.hpp>
67
#include <stan/math/opencl/rev/operands_and_partials.hpp>
78
#include <stan/math/opencl/matrix_cl.hpp>
89
#include <stan/math/opencl/copy.hpp>
@@ -150,8 +151,9 @@ return_type_t<T_x, T_alpha, T_beta> categorical_logit_glm_lpmf(
150151
try {
151152
opencl_kernels::categorical_logit_glm_beta_derivative(
152153
cl::NDRange(local_size * N_attributes), cl::NDRange(local_size),
153-
forward_as<matrix_cl<double>>(ops_partials.edge3_.partials_), temp,
154-
y_val_cl, x_val, N_instances, N_attributes, N_classes, is_y_vector);
154+
forward_as<arena_matrix_cl<double>>(ops_partials.edge3_.partials_),
155+
temp, y_val_cl, x_val, N_instances, N_attributes, N_classes,
156+
is_y_vector);
155157
} catch (const cl::Error& e) {
156158
check_opencl_error(function, e);
157159
}

stan/math/opencl/prim/multi_normal_cholesky_lpdf.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,14 @@ inline return_type_t<T_y_cl, T_loc_cl, T_covar_cl> multi_normal_cholesky_lpdf(
120120

121121
if (!is_constant_all<T_y_cl>::value) {
122122
if (y_val.cols() == 1) {
123-
forward_as<matrix_cl<double>>(ops_partials.edge1_.partials_)
124-
= -rowwise_sum(scaled_diff);
123+
ops_partials.edge1_.partials_ = -rowwise_sum(scaled_diff);
125124
} else {
126125
ops_partials.edge1_.partials_ = -scaled_diff;
127126
}
128127
}
129128
if (!is_constant_all<T_loc_cl>::value) {
130129
if (mu_val.cols() == 1) {
131-
forward_as<matrix_cl<double>>(ops_partials.edge2_.partials_)
132-
= rowwise_sum(scaled_diff);
130+
ops_partials.edge2_.partials_ = rowwise_sum(scaled_diff);
133131
} else {
134132
ops_partials.edge2_.partials_ = scaled_diff;
135133
}

stan/math/opencl/prim/neg_binomial_2_log_glm_lpmf.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,13 @@ neg_binomial_2_log_glm_lpmf(const T_y_cl& y, const T_x_cl& x,
188188
// transposition of a vector can be done without copying
189189
const matrix_cl<double> theta_derivative_transpose_cl(
190190
theta_derivative_cl.buffer(), 1, theta_derivative_cl.rows());
191-
matrix_cl<double>& edge3_partials
192-
= forward_as<matrix_cl<double>&>(ops_partials.edge3_.partials_);
193191
matrix_cl<double> edge3_partials_transpose_cl
194192
= theta_derivative_transpose_cl * x_val;
195-
edge3_partials = matrix_cl<double>(edge3_partials_transpose_cl.buffer(),
196-
edge3_partials_transpose_cl.cols(), 1);
193+
ops_partials.edge3_.partials_
194+
= matrix_cl<double>(edge3_partials_transpose_cl.buffer(),
195+
edge3_partials_transpose_cl.cols(), 1);
197196
if (beta_val.rows() != 0) {
198-
edge3_partials.add_write_event(
197+
ops_partials.edge3_.partials_.add_write_event(
199198
edge3_partials_transpose_cl.write_events().back());
200199
}
201200
}

stan/math/opencl/prim/normal_id_glm_lpdf.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,13 @@ normal_id_glm_lpdf(const T_y_cl& y, const T_x_cl& x, const T_alpha_cl& alpha,
171171
// transposition of a vector can be done without copying
172172
const matrix_cl<double> mu_derivative_transpose_cl(
173173
mu_derivative_cl.buffer(), 1, mu_derivative_cl.rows());
174-
matrix_cl<double>& edge4_partials
175-
= forward_as<matrix_cl<double>&>(ops_partials.edge4_.partials_);
176174
matrix_cl<double> edge4_partials_transpose_cl
177175
= mu_derivative_transpose_cl * x_val;
178-
edge4_partials = matrix_cl<double>(edge4_partials_transpose_cl.buffer(),
179-
edge4_partials_transpose_cl.cols(), 1);
176+
ops_partials.edge4_.partials_
177+
= matrix_cl<double>(edge4_partials_transpose_cl.buffer(),
178+
edge4_partials_transpose_cl.cols(), 1);
180179
if (beta_val.rows() != 0) {
181-
edge4_partials.add_write_event(
180+
ops_partials.edge4_.partials_.add_write_event(
182181
edge4_partials_transpose_cl.write_events().back());
183182
}
184183
}

stan/math/opencl/prim/ordered_logistic_glm_lpmf.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ return_type_t<T_x, T_beta, T_cuts> ordered_logistic_glm_lpmf(
140140
edge2_partials_transpose.buffer(), edge2_partials_transpose.cols(),
141141
edge2_partials_transpose.rows());
142142
if (beta.rows() != 0) {
143-
forward_as<matrix_cl<double>>(ops_partials.edge2_.partials_)
144-
.add_write_event(edge2_partials_transpose.write_events().back());
143+
ops_partials.edge2_.partials_.add_write_event(
144+
edge2_partials_transpose.write_events().back());
145145
}
146146
}
147147
if (!is_constant_all<T_cuts>::value) {

stan/math/opencl/prim/poisson_log_glm_lpmf.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,13 @@ return_type_t<T_x_cl, T_alpha_cl, T_beta_cl> poisson_log_glm_lpmf(
141141
// transposition of a vector can be done without copying
142142
const matrix_cl<double> theta_derivative_transpose_cl(
143143
theta_derivative_cl.buffer(), 1, theta_derivative_cl.rows());
144-
matrix_cl<double>& edge3_partials
145-
= forward_as<matrix_cl<double>&>(ops_partials.edge3_.partials_);
146144
matrix_cl<double> edge3_partials_transpose_cl
147145
= theta_derivative_transpose_cl * x_val;
148-
edge3_partials = matrix_cl<double>(edge3_partials_transpose_cl.buffer(),
149-
edge3_partials_transpose_cl.cols(), 1);
146+
ops_partials.edge3_.partials_
147+
= matrix_cl<double>(edge3_partials_transpose_cl.buffer(),
148+
edge3_partials_transpose_cl.cols(), 1);
150149
if (beta_val.rows() != 0) {
151-
edge3_partials.add_write_event(
150+
ops_partials.edge3_.partials_.add_write_event(
152151
edge3_partials_transpose_cl.write_events().back());
153152
}
154153
}

stan/math/opencl/rev/operands_and_partials.hpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stan/math/prim/functor/operands_and_partials.hpp>
66
#include <stan/math/prim/meta.hpp>
77
#include <stan/math/rev/core/var.hpp>
8+
#include <stan/math/opencl/rev/arena_matrix_cl.hpp>
89
#include <stan/math/opencl/kernel_generator.hpp>
910
#include <stan/math/opencl/rev/arena_type.hpp>
1011
#include <stan/math/opencl/rev/to_arena.hpp>
@@ -17,7 +18,7 @@ template <typename Op>
1718
class ops_partials_edge<double, var_value<Op>,
1819
require_kernel_expression_lhs_t<Op>> {
1920
public:
20-
using partials_t = plain_type_t<Op>;
21+
using partials_t = arena_matrix_cl<value_type_t<Op>>;
2122
partials_t partials_; // For univariate use-cases
2223
broadcast_array<partials_t> partials_vec_; // For multivariate
2324
explicit ops_partials_edge(const var_value<Op>& ops)
@@ -28,17 +29,10 @@ class ops_partials_edge<double, var_value<Op>,
2829
private:
2930
template <typename, typename, typename, typename, typename, typename>
3031
friend class stan::math::operands_and_partials;
31-
const var_value<Op>& operands_;
32-
33-
void dump_operands(vari** varis) {}
34-
void dump_partials(double* partials) {}
35-
int size() { return 0; }
36-
std::tuple<var_value<Op>> container_operands() {
37-
return std::make_tuple(operands_);
38-
}
39-
std::tuple<partials_t> container_partials() {
40-
return std::make_tuple(partials_);
41-
}
32+
var_value<Op> operands_;
33+
static constexpr int size() noexcept { return 0; }
34+
inline auto& operand() noexcept { return this->operands_; }
35+
inline auto& partial() noexcept { return this->partials_; }
4236
};
4337

4438
} // namespace internal

0 commit comments

Comments
 (0)