Hi! I was at StanCon this week and really appreciated all of the discourse and presentations. One thing that came up in a conversation with @WardBrian was the ability to benchmark other tools’ samplers with Stan-based models; for example, BlackJAX for JAX-based computation. While I think there’s a lot of meat to chew on regarding Brian’s talk about making a stan-like interface via, e.g., a lightweight interface like densityjax, I wanted to contribute my own two-cents on the problem of “we already have a well-understood stan model and want to see how X tool works on it”. The best tool I could figure out for this was a custom fork of Pystan (see here: GitHub - dannys4/pystan at change_function_interface · GitHub ), which slightly reformulates the log_prob interface a few ways.
The problem I originally ran into, and this was a bit niche so I don’t think very common, was that I needed to access both the value and gradient of log_prob for the same point, so I just made a simple wrapper for that as well. Second, a major point here is that samplers often prefer batching several evaluations simultaneously—consider, e.g., vmap transformations, and sampling from chains in parallel. An issue with using httpstan, though, is that asyncio really doesn’t like sending out thousands of separate requests!!! So I changed the interface in a way suitable for me, where you put in a numpy array and, if it’s a vector, each function does whatever it was going to do. If it’s a matrix, though, the loop over each parameter value happens inside the asyncio call instead of queueing up possibly a lot of parallel requests simultaneously.
you can see the diff here: Comparing stan-dev:main...dannys4:change_function_interface · stan-dev/pystan · GitHub . Not really much, and pretty readable, but it does slightly change the interface (and type hinting). I also created a really coarse connection example to JAX here, showing how to really poorly connect pystan to BlackJAX—I’m sure this probably exists somewhere else, but I didn’t immediately see it—and I did run into some issues that I think have to do with the HTTP requests being issued too quickly, but I’m not really sure.
Regardless, I did use my fork of pystan to pretty constructively compare between different sampling methods using posteriordb for a paper. I had a lot more luck using it with torch compared to JAX, actually, but that’s neither here nor there (torch’s approach to graph breaks is significantly more stan-friendly than JAX’s really strict rulebook).
Anyway, this may be of interest/use to people. Happy to work towards merging something in or just letting the branch sit unmaintained and stable.