-
-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not checking if _lpmf functions are only used within other _lp* functions and generating invalid C++ #335
Comments
This looks like it's failing because Stan 2 assumed that a function ending in |
But stanc3 creates non compile-able c++ code. I would expect an error message from the parser at least. |
Fair enough! This does seem like something the type checker should be catching. |
After fixing the problem with
Modified model is here: |
Re: the post-fix model - it looks like that's because you have a function ending in |
@VMatthijs Do you have a sense for where this should happen in Semantic_check? Basically need to check that only functions that end in |
The old compiler allowed calling Stan User Guide section 18.8 says
Stan Reference Manual section 9.3 says
and later
Section 9.5 also has
While it does say that
there is no claim that sampling statements can be used inside Section 13.4 says that So the documentation is univocal: Also, it's not just Semantic_check. Modifying |
That's right---the _lpdf and _lpmf and _lcdf and _lccdf probability
functions don't have access to target.
If there is a function foo_lpdf, then foo_lpdf(y | ...) can be used
like a regular function, the only difference being the | notation rather
than , to separate the variate from the parameters.
If there is a function foo_lpdf(y | ...) (or lpmf), then it can be called in
a sampling statement y ~ foo(...).
The functions with _log suffixes are deprecated. I wouldn't mind if
they were removed from stanc3 along with other deprecated functionality.
Then we'd bump the version to 3.0 when releasing and break backward compatibility.
The functions ending in _lp are user-defined---there aren't any such functions
built in as far as I know.
This makes me realize how much we need to write a clean language spec. The
reference manual grew up organically with the code and was never precise enough
to be a proper spec.
… On Dec 6, 2019, at 6:33 AM, Niko Huurre ***@***.***> wrote:
The old compiler allowed calling _lpdf functions anywhere. Stan Math library _lpdf functions do not access target.
Stan User Guide section 18.8 says
Only functions ending in _lpmf or _lpdf and with return type real may be used as probability functions in sampling statements.
Only functions ending in _lp may access the log probability accumulator through sampling statements or target += statements. Such functions may only be used in the transformed parameters or model blocks.
Stan Reference Manual section 9.3 says
Functions whose name ends in _lpdf or _lpmf (density and mass functions) may be used as probability functions and may be used in place of parameterized distributions on the right-hand-side of sampling statements. There is no restriction on where such functions may be used.
and later
Functions whose names end in _lp assume access to the log probability accumulator and are only available in the transformed parameter and model blocks. Functions whose names end in _rng assume access to the random number generator and may only be used within the generated quantities block, transformed data block, and within user-defined functions ending in _rng.
Section 9.5 also has
Functions that include sampling statements or log probability increment statements must have a name that ends in _lp. Attempts to use sampling statements or increment log probability statements in other functions leads to a compile-time error.
While it does say that
Functions whose names end in _lpdf and _lpmf (density and mass functions) can be used as probability functions in sampling statements.
there is no claim that sampling statements can be used inside _lpdf functions.
Section 13.4 says that _log functions are deprecated in favour of _lpdf/_lpmf functions. But how would that work if _lpdf functions are more restricted?
So the documentation is univocal: _lpdf functions are just regular pure functions; they don't have access to target.
Also, it's not just Semantic_check. Modifying target is a side effect and the optimizer assumes that only _lp functions can have side effects. (This is a bit wrong though, any function can reject() which is sort of a "side effect".)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Closed by #397. |
The old compiler previously allowed functions containing "lp" to access target; the new compiler follows the manual and only allows functions ending in
_lp
,_lpmf
,_lpdf
, or_log
to access them. Right now this is generating bad C++ code that gives a compile error complaining abouterror: use of undeclared identifier 'lp__'
orerror: use of undeclared identifier 'lp_accum__'
.Example
This model runs just fine with 2.20, but the new stanc 2.21 compiler seems to create non-compiling C++ code.
fail log, code and data attached.
stanc3-failure-oncobayes.zip
The text was updated successfully, but these errors were encountered: