stan-dev / stan-dev/math

All files in prim/fun and prim/constraint should have associated files in rev/fun, rev/constraint and fwd/fun, fwd/constrain

Open
#3,123 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

When we do not have a reverse or forward mode specialization for a function we default to the function available in prim. When we write functions with no reverse or forward mode specializations we assume the sub functions (or sub sub functions) that make up that larger function will then use reverse or forward mode specializations. Take the log1p_exp example below, Stan has reverse and forward mode for this function but I'm just using it as an example. This log1p function is generically templated so that it would accept double, var, and fvar<T> types.

template <typename T>
inline auto log1p_exp(T a) {
  // like log_sum_exp below with b=0.0; prevents underflow
  if (a > 0.0) {
    return a + log1p(exp(-a));
  }
  return log1p(exp(a));
}

If the forward and reverse mode functions for log1p and exp are not included beforehand, Stan would either

  1. Default to the prim version of the function
  2. (worse case) call the standard library function
  3. (best case) Throw a compiler error

To make sure those includes come in correctly, for each file in prim that does not already have a specialization in rev or fwd we should have a file in the equivalent path of rev/fwd. That file will contain includes for any sub functions used in the prim function that have specializations in rev/(fwd). The final include will include the prim function that will then have the specializations for its sub-functions available to it. This will also simplify our include scheme for fwd/prim/rev as well. The new rules will be that all includes in rev(fwd) must come from rev(fwd), besides a prim implementation of the same file name.

For feature requests:

Given log1p above, it's rev file would look like the following

#include <stan/math/rev/core.hpp>
#include <stan/math/rev/fun/log1p.hpp>
#include <stan/math/rev/fun/exp.hpp>
#include <stan/math/prim/fun/log1p_exp.hpp>
Current Version:

v4.9.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 by auditing the headers under prim/fun and prim/constraint, then compare their equivalent paths under rev/fun, rev/constraint, fwd/fun, and fwd/constraint. Use the log1p_exp example and its log1p and exp dependencies as the model; done means each required mode header has the appropriate mode-specific includes and the prim implementation remains available.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Feature
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.