# Annoucing BridgeStan: a bridge to a Stan model's log\_prob and gradient functions

**URL:** <https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213>\
**Category:** Publicity\
**Tags:** cmdstan\
**Created:** [July 15, 2022, 1:26pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213 "2022-07-15T13:26:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![roualdes](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/roualdes/32/1594_2.png) [@roualdes](https://discourse.mc-stan.org/u/roualdes)\
**Post date:** [July 15, 2022, 1:26pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/1 "2022-07-15T13:26:20Z")

</div>

[BridgeStan](https://github.com/roualdes/bridgestan) provides a bridge from a Stan program to any language with a C foreign function interface (FFI), in order to expose log density evaluations and gradients of the contained Stan model.

So far BridgeStan has focused on a few core components, instead of features or doc.

- Force a Stan model through a C interface to enable any higher level language with a C FFI to call BridgeStan. There are currently examples for Python and Julia.
- Ensure calls from multi-threaded code (say from Julia) are handled correctly. Calls from parallelized code should also work. Much thanks to @wds15.
- Tweak the Makefile to work on macOS, Linux, and Windows. Much thanks to @WardBrian.
- Once [Stan math issue #2743](https://github.com/stan-dev/math/pull/2743) is more widely available, BridgeStan should be entirely copy free. Right now the position vector is copied once for every call to the gradient function.

Short term goals for BridgeStan are feature and doc parity with @Bob_Carpenter’s [Stan Model Server](https://github.com/bob-carpenter/stan-model-server).

Give it a try. If you have any feedback, please let me know either on discourse or in the repo itself.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [July 15, 2022, 6:26pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/2 "2022-07-15T18:26:14Z")

</div>

Thanks, @roualdes. I can attest to the utility of this package because it’s what Edward’s been using with Julia to evaluate Matt Hoffman’s new highly parallel samplers (CheeS, Snaper, MEADS). [MEADS](https://proceedings.mlr.press/v151/hoffman22a/hoffman22a.pdf) is particularly cool, being an adaptation- and tuning-parameter free version of [Radford Neal’s new non-reversible acceptance sampler](https://www.cs.toronto.edu/~radford/ftp/nrevu.pdf), which itself supports partial-momentum refresh Metropolis-adjusted Langevin (MALA).

---

<div class="post-metadata">

**Author:** ![defjaf](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/defjaf/32/413_2.png) [@defjaf](https://discourse.mc-stan.org/u/defjaf)\
**Post date:** [July 21, 2022, 3:30pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/3 "2022-07-21T15:30:28Z")

</div>

This is very cool. One useful feature (I assume it’s a related problem, but perhaps not) might be access to definitions in the `functions` block. If I am doing straightforward but nonlinear curve-fitting, I often find myself defining the same function in Stan as well as in python, e.g., so I can plot smooth fits through the data.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [July 21, 2022, 7:57pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/4 "2022-07-21T19:57:30Z")

</div>

> [@defjaf](#):
>
> One useful feature (I assume it’s a related problem, but perhaps not) might be access to definitions in the `functions` block.

Thanks for posting, @defjaf. RStan already lets you do this, so others have also asked for the functionality.

---

<div class="post-metadata">

**Author:** ![defjaf](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/defjaf/32/413_2.png) [@defjaf](https://discourse.mc-stan.org/u/defjaf)\
**Post date:** [July 22, 2022, 7:03am UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/6 "2022-07-22T07:03:56Z")

</div>

> RStan already lets you do this, so others have also asked for the functionality.

Yes, I experimented with getting it to work within pystan a while back, but I couldn’t get it working except for very simple cases. (And in any case I imagine that it wouldn’t really be possible with cmdstanpy – or even cmdstanr? – which now seem to be favoured.)

But are you implying that this in the roadmap for BridgeStan? Or something separate?

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [July 22, 2022, 7:14am UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/7 "2022-07-22T07:14:26Z")

</div>

Do you mean exposing the user-defined functions to the environment? For cmdstanr, that is possible but requires some extra work, that I have in an online tutorial: [Exposing Stan user-defined functions using CmdStanR and Rcpp](https://rok-cesnovar.github.io/misc/exposing_cmdstanr_udf.html)

I dont know if this is something that is doable in Python, I dont have enough experience with C++ - Python interoperability.

---

<div class="post-metadata">

**Author:** ![defjaf](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/defjaf/32/413_2.png) [@defjaf](https://discourse.mc-stan.org/u/defjaf)\
**Post date:** [July 22, 2022, 10:13am UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/8 "2022-07-22T10:13:51Z")

</div>

Thanks for that example. There is quite a bit of C/C++/Python interop so I suspect it’s possible but I imagine very different from R. Anyone out there with ideas or pointers?

I think if it can be made [cffi-compatible](https://cffi.readthedocs.io/en/latest/) a la BridgeStan then it is probably straightforward.

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [July 22, 2022, 1:30pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/9 "2022-07-22T13:30:20Z")

</div>

I think trying to expose the UDFs of a Stan program in something like bridgestan is not possible. The key idea that makes bridgestan work is that the model class always has some functions which are easy to expose to a C wrapper. The UDFs will be different for each and every model.

That said, in some sense the problem is actually easier, since you don’t need to worry about sharing memory with an instantiated object - all those functions can be called without instantiating a model. I suspect you could do it with something like the standalone functions trick @rok_cesnovar links to above and [pybind11 documentation](https://pybind11.readthedocs.io/en/stable/)

---

<div class="post-metadata">

**Author:** ![roualdes](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/roualdes/32/1594_2.png) [@roualdes](https://discourse.mc-stan.org/u/roualdes)\
**Post date:** [July 29, 2022, 5:13pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/10 "2022-07-29T17:13:02Z")

</div>

> [@WardBrian](#):
>
> The key idea that makes bridgestan work is that the model class always has some functions which are easy to expose to a C wrapper. The UDFs will be different for each and every model.

Right. Plus there are issues of matching container types and memory layout from the arguments of the Stan function(s) to Python (or which ever higher level language). So exposing user defined functions is more complicated than what BridgeStan does.

@defjaf, I’m happy to help you work up a prototype if you want, though I myself would have to rely on help from either or both of Brian and/or Rok. If so, maybe a new thread would be best with a minimal example Stan program containing a user defined function of interest.

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [September 14, 2022, 1:25pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/11 "2022-09-14T13:25:19Z")

</div>

I wanted to bump this thread with two new things:

1. Bridgestan now lives on Github at [GitHub - roualdes/bridgestan](https://github.com/roualdes/bridgestan)
2. Bridgestan has had an overhaul internally and is now feature complete. An R interface was added, the remaining model class functionality was exposed, and important bugs around topics like exception handling have been fixed.

We still haven’t formally declared a versioned release, but it is moving toward being production ready. Documentation is one of the bigger remaining hurdles.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [September 14, 2022, 9:28pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/12 "2022-09-14T21:28:44Z")

</div>

@WardBrian: Didn’t you say you had a prototype that exposed Stan functions in BridgeStan (but didn’t work on Windows)?

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [September 14, 2022, 9:35pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/13 "2022-09-14T21:35:12Z")

</div>

@Bob_Carpenter - I posted that as a separate topic recently

> [@Exposing Stan functions in Python via PyBind](https://discourse.mc-stan.org/t/exposing-stan-functions-in-python-via-pybind/28830):
>
> Hi all - I’ve recently done some experimenting in how to expose functions written in a Stan model to Python. I’ve cleaned up the results of these and put them up here: [GitHub - WardBrian/pybind\_expose\_stan\_fns: Exposing Stan functions in Python](https://github.com/WardBrian/pybind_expose_stan_fns). This is modeled after the [similar functionality Rok has built for cmdstanr users](https://discourse.mc-stan.org/t/exposing-stan-user-defined-functions-using-cmdstanr-and-rcpp/27104). A Stan model like functions { real my\_log1p\_exp(real x) { return log1p\_exp(x); } real array\_fun(array[] real a) { return sum(a); } real int\_array\_fun…

It’s not terribly related to Bridgestan other than that Bridgestan had me thinking more about foreign function interfaces

---

<div class="post-metadata">

**Author:** ![roualdes](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/roualdes/32/1594_2.png) [@roualdes](https://discourse.mc-stan.org/u/roualdes)\
**Post date:** [October 4, 2022, 10:01pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/14 "2022-10-04T22:01:34Z")

</div>

BridgeStan continues to move forward and is (hopefully) approaching battle tested.

Thanks to @Niko for finding a subtle bug, and big big up to @WardBrian and @stevebronder for squashing it within a day. They simultaneously solved another issue, found by @alphillips (I think), that showed up only on newer versions of gcc.

Thanks to all for your help.

---

<div class="post-metadata">

**Author:** ![Niko](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/niko/32/12409_2.png) [@Niko](https://discourse.mc-stan.org/u/Niko)\
**Post date:** [October 5, 2022, 5:52am UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/15 "2022-10-05T05:52:50Z")

</div>

Excellent work @WardBrian and @stevebronder!

---

<div class="post-metadata">

**Author:** ![Charles\_Driver](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charles_driver/32/6524_2.png) [@Charles\_Driver](https://discourse.mc-stan.org/u/Charles_Driver)\
**Post date:** [October 5, 2022, 7:44am UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/16 "2022-10-05T07:44:26Z")

</div>

Nice. Is there any hope /plans for being able to use bridgeStan in a CRAN package for R?

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [October 5, 2022, 1:29pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/17 "2022-10-05T13:29:55Z")

</div>

> [@Charles\_Driver](#):
>
> CRAN package for R?

At the moment we’re not thinking of any release avenues besides Github. Using it from R/Python/Julia will always require downloading the C++ code from there, and a working copy of CmdStan (or possibly just Stan, we’re investigating loosening the requirements there)

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [November 30, 2022, 5:03pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/18 "2022-11-30T17:03:13Z")

</div>

Bumping this thread again to mention some major updates:

- BridgeStan no longer depends on CmdStan but is a standalone repo which includes it’s own copy of the `stan` repo.
- Autodiff Hessians are now available for supported models. Enable this at compile time with the `BRIDGESTAN_AD_HESSIAN=true` `make` argument.
- The C-level API has had all symbols prefixed with `bs_`. The Python/Julia/R interfaces did not change

We are planning on a 1.0 release of the package soon, so if anyone has been sitting on issues or feedback please do share them soon!

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [December 1, 2022, 8:15am UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/19 "2022-12-01T08:15:41Z")

</div>

> [@WardBrian](#):
>
> Autodiff Hessians are now available for supported models.

Is this for log\_prob with Jacobian adjustment or without? (I wish with, or at least to have an option to choose)

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [December 1, 2022, 1:13pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/20 "2022-12-01T13:13:47Z")

</div>

You can choose, the default is with the Jacobian

---

<div class="post-metadata">

**Author:** ![Charles\_Driver](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charles_driver/32/6524_2.png) [@Charles\_Driver](https://discourse.mc-stan.org/u/Charles_Driver)\
**Post date:** [December 1, 2022, 6:05pm UTC](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213/21 "2022-12-01T18:05:58Z")

</div>

What does supported models mean?

[Next page](https://discourse.mc-stan.org/t/annoucing-bridgestan-a-bridge-to-a-stan-models-log-prob-and-gradient-functions/28213.md?page=2)
