stan-dev / httpstan

HTTP interface to Stan, a package for Bayesian inference.
ISC License
40 stars 15 forks source link

CompileError when passing model with integer division #566

Closed amas0 closed 2 years ago

amas0 commented 3 years ago

Found an issue where httpstan will return a error code 400 "Bad Request" when passing model code that contains integer division, which generates the return message:

CompileError(DistutilsExecError(\"command '/usr/bin/gcc' failed with exit code 1\"))`, traceback: `['  File \"/home/amas/projects/httpstan/env/lib/python3.9/site-packages/httpstan/views.py\", line 114, in handle_create_model\\n    compiler_output = await httpstan.models.build_services_extension_module(program_code)\\n', '  File \"/home/amas/projects/httpstan/env/lib/python3.9/site-packages/httpstan/models.py\", line 168, in build_services_extension_module\\n    compiler_output = await asyncio.get_running_loop().run_in_executor(\\n', '  File \"/usr/lib/python3.9/concurrent/futures/thread.py\", line 52, in run\\n    result = self.fn(*self.args, **self.kwargs)\\n', '  File \"/home/amas/projects/httpstan/env/lib/python3.9/site-packages/httpstan/build_ext.py\", line 80, in run_build_ext\\n    build_extension.run()\\n', '  File \"/usr/lib/python3.9/distutils/command/build_ext.py\", line 340, in run\\n    self.build_extensions()\\n', '  File \"/usr/lib/python3.9/distutils/command/build_ext.py\", line 449, in build_extensions\\n    self._build_extensions_serial()\\n', '  File \"/usr/lib/python3.9/distutils/command/build_ext.py\", line 474, in _build_extensions_serial\\n    self.build_extension(ext)\\n', '  File \"/usr/lib/python3.9/distutils/command/build_ext.py\", line 529, in build_extension\\n    objects = self.compiler.compile(sources,\\n', '  File \"/usr/lib/python3.9/distutils/ccompiler.py\", line 574, in compile\\n    self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)\\n', '  File \"/usr/lib/python3.9/distutils/unixccompiler.py\", line 120, in _compile\\n    raise CompileError(msg)\\n']

To recreate, run httpstan as normal and run the following curl command:

curl -H "Content-Type: application/json" \
    --data '{"program_code":"parameters {real y;} model {y ~ normal(0,1/5);}"}' \
    http://localhost:8080/v1/models

Note, this is the basic usage example in the README but with standard deviation 1/5 instead of 1. It should run for a bit as if it is building and then return the error. I conformed that the above model code will compile and sample properly if done with cmdstan directly.

The model will compile without issue if the division is float division instead so the following works:

curl -H "Content-Type: application/json" \
    --data '{"program_code":"parameters {real y;} model {y ~ normal(0,1/5.0);}"}' \
    http://localhost:8080/v1/models

Somewhat of an odd issue, might have to do with how the model code is parsed when it receives it over HTTP? Not sure.

(As a side note, I found this issue when trying to run a model of mine -- turns out I had mis-specified the model and I didn't want integer division! Correctly specifying the model with float division fixed the issue... so in a way, this bug helped me find an error on my part :) still a bug though, it would seem )

riddell-stan commented 3 years ago

Thanks for the report. This does indeed look like a bug.

The fix should be easy. Will need tests in httpstan and pystan.

ahartikainen commented 3 years ago

I wonder if this error is actually raised by stanc.

riddell-stan commented 3 years ago

Likely relevant https://github.com/stan-dev/stanc3/pull/869

riddell-stan commented 3 years ago

Yes, I think this will be solved by the PR referenced above. The root cause of the problem is that the integer division warning is somehow ending up in the C++ code. Such code will, of course, not compile.

rok-cesnovar commented 3 years ago

The warning is printed to stdout in stanc3, it should be printed to stderr. This is what the PR is trying to address.

However I am not sure why that would cause it to end in the C++ code? Is pystan calling stanc3 so that it prints the C++ and consume the printed C++?

riddell-stan commented 3 years ago

Yes, that's exactly what PyStan does.

https://github.com/stan-dev/httpstan/blob/417812992cd25a5e97bd03020cece57518b2cc2b/httpstan/compile.py#L37

rok-cesnovar commented 3 years ago

The PR in stanc3 was merged so this issue was addressed upstream. I guess we can close?

ahartikainen commented 3 years ago

I think we can close this after we upgrade our stanc.

rok-cesnovar commented 3 years ago

Cool, then it has to wait for the next release.

riddell-stan commented 3 years ago

That's correct. Let's keep this open until stanc makes a new release and httpstan uses it.

On Sun, Jul 11, 2021, at 10:18 AM, Rok Češnovar wrote:

Cool, then it has to wait for the next release.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/stan-dev/httpstan/issues/566#issuecomment-877807912, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJQUBVUYMJCN3V5AM5FXU5LTXGR3NANCNFSM446DAYRA.