stan-dev / stan-dev/math

change check behavior to only use double types

Open
#1,779 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

code cleanup performance
Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

Description

stan/math/prim/err/check_simplex.hpp is doing its checks on the autodiff type that it's supplied.

Here's the current code for a vector of scalar type T_prob.

  check_nonzero_size(function, name, theta);
  if (!(fabs(1.0 - theta.sum()) <= CONSTRAINT_TOLERANCE)) {
    std::stringstream msg;
    T_prob sum = theta.sum();
    msg << "is not a valid simplex.";
    msg.precision(10);
    msg << " sum(" << name << ") = " << sum << ", but should be ";
    std::string msg_str(msg.str());
    throw_domain_error(function, name, 1.0, msg_str.c_str());
  }

That first sum and subtraction from 1 is being done with autodiff types.

check_cholesky_factor_corr is just as bad. This habit probably started because the first ones like positive and negative didn't require any intermediate expressions.

Someone needs to go through all of these things and make sure that the values are extracted and that we don't factor an autodiff variable for a covariance matrix test.

This one's actually pretty important for performance since it hits every parameter defined with a constrained type other than the simple lower and upper bounds and orderings: unit vectors, simplexes, correlation cholesky factors, and covariance and correlation matrices.

It'd be good to spot check the other ones so there aren't anything like this fabs(1 - x) thing going on anywhere, because that'll also drop things on the stack.

Current Version:

v3.1.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with stan/math/prim/err/check_simplex.hpp and the check_cholesky_factor_corr entry point, then inspect the other constrained-type checks for intermediate expressions such as fabs(1 - x) being evaluated on autodiff types. Done means the relevant validation checks extract values before these calculations and no covariance or correlation matrix check factors an autodiff variable.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.