Open bbbales2 opened 4 years ago
Actually maybe I made mat<var>
slower with this lol. I think it was only like 80-90us before (Edit: at N = 16384
).
@JamesYang007 I was converting more of these, in the StochasticVolatility example as-is I'm getting outputs like:
BM_stan<StochasticVolatilityFunc, matvar>/32 2013 ns 2013 ns 352799 N=35
WARNING (stan-stochastic_volatility) MAX ABS ERROR PROP: 2.50972e-15
WARNING (stan-stochastic_volatility) MAX ABS ERROR PROP: 2.69971e-15
WARNING (stan-stochastic_volatility) MAX ABS ERROR PROP: 0.815661
WARNING (stan-stochastic_volatility) MAX ABS ERROR PROP: 3.19019e-15
WARNING (stan-stochastic_volatility) MAX ABS ERROR PROP: 2.10088e-15
WARNING (stan-stochastic_volatility) MAX ABS ERROR PROP: 1.87386e-15
WARNING (stan-stochastic_volatility) MAX ABS ERROR PROP: 2.452e-15
The 0.815 makes me think something is broken, and so I'll look into that, but the way this is written the h
variable is kinda part of the input. Like why is this:
auto operator()(Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>& x) const
{
using namespace stan::math;
using vec_t = Eigen::Matrix<var, Eigen::Dynamic, 1>;
size_t N = (x.size() - 3) / 2;
Eigen::Map<vec_t> h_std(x.data(), N);
Eigen::Map<vec_t> h(x.data() + N, N);
auto& phi = x(2*N);
auto& sigma = x(2*N + 1);
auto& mu = x(2*N + 2);
h = h_std * sigma;
...;
}
Not something like:
auto operator()(Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>& x) const
{
using namespace stan::math;
using vec_t = Eigen::Matrix<var, Eigen::Dynamic, 1>;
size_t N = (x.size() - 3) / 2;
Eigen::Map<vec_t> h_std(x.data(), N);
auto& phi = x(N);
auto& sigma = x(N + 1);
auto& mu = x(N + 2);
vec_t h = h_std * sigma;
...;
}
I see the default implementation is like this too. I wanna change it :D.
Ah I didn't want to allocate more than I needed to. The parameter x for operator() is supposed to represent the entire parameter vector and h is a (transformed) parameter. Some libraries (like Stan) allow for this kind of "viewer" logic which generally saves time so I wanted to give them the advantage if they supported it.
Converted one! As we get the rest of the
var<mat>
stuff in place we can convert the rest.FastAD sum:
Stan sum: