# CmdStan on ARM error

**URL:** <https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485>\
**Category:** General\
**Created:** [October 7, 2020, 11:39pm UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485 "2020-10-07T23:39:42Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![asceles](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/asceles/32/6698_2.png) [@asceles](https://discourse.mc-stan.org/u/asceles)\
**Post date:** [October 7, 2020, 11:39pm UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/1 "2020-10-07T23:39:43Z")

</div>

I am attempting to install CmdStan via CmdStanR on a AWS Graviton2 instance running Ubuntu 20.04. install\_cmdstan() happily runs until trying to compile one of the examples, failing with:

/bin/bash: bin/stanc: cannot execute binary file: Exec format error  
make: \*\*\* [make/program:53: examples/bernoulli/bernoulli.hpp] Error 126

The problem is that stanc was not properly compiled and cannot run:

~/.cmdstanr/cmdstan-2.24.1/bin$ ./stanc  
-bash: ./stanc: cannot execute binary file: Exec format error

Is this a bug in how the compilation for stanc is working in a non-x86 environment? Is this supported?

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [October 8, 2020, 12:44am UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/2 "2020-10-08T00:44:35Z")

</div>

stanc3 is currently downloaded as a binary. I think it’s a bit of a trick to build and distributing it as a binary was an easy solution.

If you can build your own version, you could probably use that. The repository is over here: [https://github.com/stan-dev/stanc3](https://github.com/stan-dev/stanc3)

There are build directions there. @rybern are those up to date?

---

<div class="post-metadata">

**Author:** ![asceles](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/asceles/32/6698_2.png) [@asceles](https://discourse.mc-stan.org/u/asceles)\
**Post date:** [October 8, 2020, 1:04am UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/3 "2020-10-08T01:04:00Z")

</div>

I was hoping the Nix build would be straightforward, but it fails with:

error: cannot coerce a set to a string, at /nix/store/h497gh2wc1zfv92928i7i5djrrfa76d6-nixpkgs-19.09/pkgs/build-support/ocaml/dune.nix:30:3

I am using the release/v2.24.1 branch. Is there a potentially easy fix, or is this a more complicated debugging endeavor?

---

<div class="post-metadata">

**Author:** ![rybern](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/rybern/32/5888_2.png) [@rybern](https://discourse.mc-stan.org/u/rybern)\
**Post date:** [October 8, 2020, 1:09am UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/4 "2020-10-08T01:09:41Z")

</div>

Shoot, Nix shouldn’t be giving any trouble. I don’t recognize that error. I can look into it tomorrow morning. The non-Nix instructions should also work but I don’t use them myself.

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [October 8, 2020, 1:27am UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/5 "2020-10-08T01:27:13Z")

</div>

> [@asceles](#):
>
> a more complicated debugging endeavor

Not sure. I think the reason we distribute the binaries is it can be a bit of a trick.

---

<div class="post-metadata">

**Author:** ![asceles](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/asceles/32/6698_2.png) [@asceles](https://discourse.mc-stan.org/u/asceles)\
**Post date:** [October 8, 2020, 1:31am UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/6 "2020-10-08T01:31:03Z")

</div>

Thanks for taking a look. Unfortunately setting up dependencies via setup\_dev\_env.sh also fails:

[ERROR] The compilation of core failed at “/home/ubuntu/.opam/opam-init/hooks/sandbox.sh build jbuilder build -p core -j 31”.

Fatal error: exception (runtime-lib/runtime.ml.E “comparison failed”  
((switch true) vs (switch false) (Loc src/config/discover.ml:205:15)))  
Raised at file “src/configurator.ml”, line 511, characters 13-22  
Called from file “src/config/discover.ml”, line 136, characters 2-1023

---

<div class="post-metadata">

**Author:** ![asceles](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/asceles/32/6698_2.png) [@asceles](https://discourse.mc-stan.org/u/asceles)\
**Post date:** [October 8, 2020, 2:14am UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/7 "2020-10-08T02:14:18Z")

</div>

As a quick followup,

```
nix-shell --command "dune build"

```

appears to have been more successful than nix-build. However, when compiling my Stan program, the compiler runs for some time and eventually ends with a seg fault. I guess running Stan on ARM is not an easy feat. This would be great to investigate further given the low cost of Graviton2 and Stan’s new within-chain parallelization.

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [October 8, 2020, 6:34am UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/8 "2020-10-08T06:34:37Z")

</div>

The easiest fix here is to use stanc2. This won’t give you the latest features, but that compiles from the c++ sources. I think setting STANC2=true In make local suffices.

---

<div class="post-metadata">

**Author:** ![asceles](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/asceles/32/6698_2.png) [@asceles](https://discourse.mc-stan.org/u/asceles)\
**Post date:** [October 8, 2020, 4:20pm UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/9 "2020-10-08T16:20:06Z")

</div>

Thanks. Unfortunately the motivation for doing this on ARM/Graviton2 was to take advantage of the large multi-core setup and reduce\_sum. Since reduce\_sum was introduced in Stan 2.23 and stanc2 ended with the 2.22 release, I am assuming it is not available there.

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [October 8, 2020, 4:54pm UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/10 "2020-10-08T16:54:11Z")

</div>

Oh…then that is not an option.

The other way to work around this is to transpile the Stan file on a different machine and copy over the hpp then…but I have never done that…

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [October 8, 2020, 6:26pm UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/11 "2020-10-08T18:26:13Z")

</div>

`reduce_sum` is not available in stanc2. stanc2 is there to insure that models developed before 2.22 will be able to be compiled as before; it is slated for deprecation.

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [October 8, 2020, 6:29pm UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/12 "2020-10-08T18:29:59Z")

</div>

> [@wds15](#):
>
> The other way to work around this is to transpile the Stan file on a different machine and copy over the hpp then…but I have never done that…

interesting idea - you’d need to build the exe file directly from the command line (i.e., using the CmdStan makefile outside of cmdstanr) - at which point you’d have a `.stan` and `.exe` file where latter is newer than the former and so it would skip the compile. might work - but what a PITA!

---

<div class="post-metadata">

**Author:** ![rybern](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/rybern/32/5888_2.png) [@rybern](https://discourse.mc-stan.org/u/rybern)\
**Post date:** [October 8, 2020, 9:06pm UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/13 "2020-10-08T21:06:35Z")

</div>

I set up a test with two AWS instances, one ARM and one x86. nix-build did fail on ARM, so I’ll have to work on that, but nix-shell “dune build” worked as you said.

But, after building with Nix, I was not able to replicate the segfault in stanc3 on ARM. I’m betting that issues was downstream of stanc3, is that possible @asceles? Unfortunately that would eliminate the hpp-copying solution.

---

<div class="post-metadata">

**Author:** ![asceles](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/asceles/32/6698_2.png) [@asceles](https://discourse.mc-stan.org/u/asceles)\
**Post date:** [October 8, 2020, 9:13pm UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/14 "2020-10-08T21:13:18Z")

</div>

It’s certainly possible. I am not using stanc3 directly but via CmdStan/CmdStanR. FWIW, the exact same model file compiles and works well on x86. It could be some interaction between CmdStan/CmdStanR and stanc3 causing the issue though.

---

<div class="post-metadata">

**Author:** ![rybern](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/rybern/32/5888_2.png) [@rybern](https://discourse.mc-stan.org/u/rybern)\
**Post date:** [October 8, 2020, 9:30pm UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/15 "2020-10-08T21:30:32Z")

</div>

The test suite for stanc3 passed on ARM, so I suspect the issue is downstream.

(By the way, that issue with Nix should be merged to master soon)

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [October 9, 2020, 2:24pm UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/16 "2020-10-09T14:24:57Z")

</div>

> [@asceles](#):
>
> It could be some interaction between CmdStan/CmdStanR and stanc3 causing the issue though.

I’m not sure what you mean by “interaction”.

stanc3 is a program which given a Stan program as an input file, parses the file and produces a C++ output file. e.g. stanc3 translates `bernoulli.stan` to `bernoulli.hpp`. that’s it. that C++ file is the same regardless of whatever platform is used to do the compilation, and is the same if you’re using CmdStan or any of the interfaces.

> [@asceles](#):
>
> However, when compiling my Stan program, the compiler runs for some time and eventually ends with a seg fault.

if you’ve got CmdStanR installed on AWS, then there should be a hidden folder `$HOME/.cmdstanr` - you’ll find the CmdStan install there - either `$HOME/.cmstanr/cmdstan` or `$HOME/.cmdstanr/cmdstan-2.24.1` or similar. cd to this directory, and then try to compile from the command line. cf. example in the CmdStan manual here: [14 Parallelization | CmdStan User’s Guide](https://mc-stan.org/docs/2_24/cmdstan-guide/parallelization.html)

> [@asceles](#):
>
> FWIW, the exact same model file compiles and works well on x86.

what exactly do you mean by `x86` ? does it run multi-threaded?

---

<div class="post-metadata">

**Author:** ![Ziaeemehr](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@Ziaeemehr](https://discourse.mc-stan.org/u/Ziaeemehr)\
**Post date:** [February 1, 2022, 8:44am UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/17 "2022-02-01T08:44:02Z")

</div>

I borrowed the `stanc` from `cmdstanpy` installation directory.  
`anaconda3/envs/my_env/bin/cmdstan/bin/stanc`  
copy to `cmdstan-2.28.2/bin`.

---

<div class="post-metadata">

**Author:** ![avritt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avritt/32/13672_2.png) [@avritt](https://discourse.mc-stan.org/u/avritt)\
**Post date:** [February 12, 2022, 8:39pm UTC](https://discourse.mc-stan.org/t/cmdstan-on-arm-error/18485/18 "2022-02-12T20:39:20Z")

</div>

I ran into a similar issue, the command in [Pre-complied cmdstan model 'cannot execute binary file' error - #11 by rok\_cesnovar](https://discourse.mc-stan.org/t/pre-complied-cmdstan-model-cannot-execute-binary-file-error/21487/11) worked for me
