Skip to content
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

Fix code generation issue with overloaded distributions in ~ statements #1466

Merged
merged 5 commits into from
Nov 13, 2024

Conversation

WardBrian
Copy link
Member

Initially reported in https://discourse.mc-stan.org/t/use-of-for-overloaded-distribution-statements/37238

I tried a few solutions. Essentially, when lowering from the AST to MIR, a tilde needs to know:

  1. Is the distribution user defined
  2. Is the distribution a PDF or PMF

This information is resolved during typechecking, but currently gets thrown away.

The simplest solution I settled on is 1) adding a separate FnLpmf case to the Fun_kind.suffix type (it always bothered me that FnLpdf lumped these two together, anyway) and then 2) storing this in the Tilde node during typechecking

Submission Checklist

  • Run unit tests
  • Documentation
    • If a user-facing facing change was made, the documentation PR is here:
    • OR, no user-facing changes were made

Release notes

Fixed an issue where certain overloads of distributions could lead to the ~ statement producing uncompilable C++.

Copyright and Licensing

By submitting this pull request, the copyright holder is agreeing to
license the submitted work under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause)

@WardBrian WardBrian added bug Something isn't working cpp-codegen labels Nov 13, 2024
@WardBrian WardBrian requested a review from nhuurre November 13, 2024 16:00
Copy link

codecov bot commented Nov 13, 2024

Codecov Report

Attention: Patch coverage is 77.46479% with 16 lines in your changes missing coverage. Please review.

Project coverage is 89.76%. Comparing base (5692dc4) to head (c4aad21).
Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
src/frontend/Typechecker.ml 78.94% 4 Missing ⚠️
src/stan_math_backend/Lower_functions.ml 42.85% 4 Missing ⚠️
src/frontend/Ast_to_Mir.ml 66.66% 2 Missing ⚠️
src/frontend/Info.ml 50.00% 2 Missing ⚠️
src/middle/Fun_kind.ml 87.50% 2 Missing ⚠️
src/analysis_and_optimization/Pedantic_analysis.ml 75.00% 1 Missing ⚠️
src/frontend/SignatureMismatch.ml 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1466      +/-   ##
==========================================
- Coverage   89.82%   89.76%   -0.07%     
==========================================
  Files          63       63              
  Lines       10598    10613      +15     
==========================================
+ Hits         9520     9527       +7     
- Misses       1078     1086       +8     
Files with missing lines Coverage Δ
src/analysis_and_optimization/Optimize.ml 92.32% <100.00%> (+0.06%) ⬆️
src/frontend/Ast.ml 70.78% <100.00%> (+0.12%) ⬆️
src/frontend/Pretty_printing.ml 92.28% <100.00%> (ø)
src/middle/Stan_math_signatures.ml 98.10% <ø> (+0.07%) ⬆️
src/stan_math_backend/Lower_expr.ml 93.55% <100.00%> (ø)
src/analysis_and_optimization/Pedantic_analysis.ml 93.36% <75.00%> (+0.05%) ⬆️
src/frontend/SignatureMismatch.ml 81.70% <80.00%> (-0.19%) ⬇️
src/frontend/Ast_to_Mir.ml 93.75% <66.66%> (-0.41%) ⬇️
src/frontend/Info.ml 91.66% <50.00%> (-1.99%) ⬇️
src/middle/Fun_kind.ml 90.90% <87.50%> (+0.58%) ⬆️
... and 2 more

... and 1 file with indirect coverage changes

Copy link
Collaborator

@nhuurre nhuurre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(it always bothered me that FnLpdf lumped these two together, anyway)

They're lumpled together because, as far as the backend is concerned, they behave identically. I'm not sure if having different names in the frontend was a good idea.

Technically, you don't need a new Fun_kind variant here either--you could decide which suffix to use based on UnsizedType.is_discrete_type arg.

The first argument must be a pure function but got a probability density or mass function. These function types are not compatible.
The first argument must be a pure function but got a probability density function. These function types are not compatible.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks nicer, though.

@WardBrian
Copy link
Member Author

I'm not sure if having different names in the frontend was a good idea.

Certainly up for debate. @bob-carpenter agrees, for what it's worth, but it's probably not worth changing now

Technically, you don't need a new Fun_kind variant here either--you could decide which suffix to use based on UnsizedType.is_discrete_type arg.

True -- and my first attempt used only that, until I realized that we also need to differentiate between StanLib and UserDefined. If you prefer that solution I can marry the two

@WardBrian WardBrian merged commit 6915754 into master Nov 13, 2024
3 checks passed
@WardBrian WardBrian deleted the fix/tilde-overloading branch November 13, 2024 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cpp-codegen
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants