# Foreign Function Interface in Stan's future

**URL:** <https://discourse.mc-stan.org/t/foreign-function-interface-in-stans-future/15024>\
**Category:** Developers\
**Created:** [May 10, 2020, 6:47pm UTC](https://discourse.mc-stan.org/t/foreign-function-interface-in-stans-future/15024 "2020-05-10T18:47:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiehw](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/xiehw/32/7352_2.png) [@xiehw](https://discourse.mc-stan.org/u/xiehw)\
**Post date:** [May 10, 2020, 6:47pm UTC](https://discourse.mc-stan.org/t/foreign-function-interface-in-stans-future/15024/1 "2020-05-10T18:47:03Z")

</div>

Hi @seantalts, I read your “A Path Forward for Stan” document, and reading the section about FFIs, I was wondering whether you were aware of more concrete plans for expanding the usability of FFIs that were being put into place since your departure as Stan’s Technical Working Group Director (I’m not sure who your successor as the Interim Working Director is, if she or he exists at the moment). Tagging @ChrisRackauckas in this, as he’s interested in finding some way of porting his DifferentialEquations.jl in Stan in the future, which would also be related to the benchmarking discussions that he has had with @stevebronder.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/chrisrackauckas/32/748_2.png) [@ChrisRackauckas](https://discourse.mc-stan.org/u/ChrisRackauckas)\
**Post date:** [May 11, 2020, 12:09pm UTC](https://discourse.mc-stan.org/t/foreign-function-interface-in-stans-future/15024/2 "2020-05-11T12:09:17Z")

</div>

Thanks for the ping. Yes I would be interested in setting up DifferentialEquations.jl as something that could be called from Stan. It would be helpful for figuring out what is allowing a given benchmark to be robust in Turing.jl: whether it’s a property of the ODE integrator or whether it’s solely due to the sampler. Right now we have to compare Turing.jl + DifferentialEquations.jl vs Stan (with internal ODE solver). Another interesting option would be to be able to call the sampler directly from other languages, similar to DynamicHMC.jl or AdvancedHMC.jl, without having to go through the Stan language. This would make it more accessible as a library component that could integrate with other PPLs and be interesting for PPL research.

---

<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:** [May 18, 2020, 12:47am UTC](https://discourse.mc-stan.org/t/foreign-function-interface-in-stans-future/15024/3 "2020-05-18T00:47:49Z")

</div>

> [@xiehw](#):
>
> I’m not sure who your successor as the Interim Working Director is, if she or he exists at the moment

It’s not clear there will be such a person.

> [@xiehw](#):
>
> more concrete plans for expanding the usability of FFIs

I don’t think there’s been any work on a foreign function interface.

> [@ChrisRackauckas](#):
>
> It would be helpful for figuring out what is allowing a given benchmark to be robust in Turing.jl: whether it’s a property of the ODE integrator or whether it’s solely due to the sampler.

Is there a writeup of something that’s robust in Turing.jl that isn’t in Stan? Are you using the stiff solver in Stan?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/chrisrackauckas/32/748_2.png) [@ChrisRackauckas](https://discourse.mc-stan.org/u/ChrisRackauckas)\
**Post date:** [May 19, 2020, 6:46pm UTC](https://discourse.mc-stan.org/t/foreign-function-interface-in-stans-future/15024/4 "2020-05-19T18:46:48Z")

</div>

> [@Bob\_Carpenter](#):
>
> Is there a writeup of something that’s robust in Turing.jl that isn’t in Stan? Are you using the stiff solver in Stan?

It was tracked down to be [Errors thrown from ODE solvers are runtime errors. They should probably be domain\_errors · Issue #1887 · stan-dev/math · GitHub](https://github.com/stan-dev/math/issues/1887) . Using the stiff solver doesn’t make sense here since it was just on Lotka-Volterra.

---

<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:** [August 5, 2020, 11:26am UTC](https://discourse.mc-stan.org/t/foreign-function-interface-in-stans-future/15024/5 "2020-08-05T11:26:03Z")

</div>

Hi @ChrisRackauckas

can you elaborate a bit more on how you would envision using such a feature. Would this be an external library linked at compile time? Or would this be written in a user-supplied C++ file? Or something else.

I think something like this was already suggested awhile back [https://github.com/stan-dev/cmdstan/issues/630](https://github.com/stan-dev/cmdstan/issues/630)

I think this would be something worth considering, if we can make it work in a way that does not put the bulk burden on the Stan team to maintain. So basically Stan provides the interface and C++ nuts and bolts while the rest is provided by whoever wants to supply their FFI.

For header only libraries this might be already doable with properly set makefile flags.

---

<div class="post-metadata">

**Author:** ![xiehw](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/xiehw/32/7352_2.png) [@xiehw](https://discourse.mc-stan.org/u/xiehw)\
**Post date:** [August 5, 2020, 3:56pm UTC](https://discourse.mc-stan.org/t/foreign-function-interface-in-stans-future/15024/6 "2020-08-05T15:56:35Z")

</div>

This user post from a Hacker News thread is an overview of one approach for being able to execute at least some of the solvers from DifferentialEquations.jl in Stan, which may be along the lines of what Chris is looking to do: [https://news.ycombinator.com/item?id=23140058](https://news.ycombinator.com/item?id=23140058).

BTW, @ChrisRackauckas, you mentioned needing to be able to evaluate the gradients in Stan – based on that HN post, I think there may be a way to do so?
