Skip to content

optimize_ad_levels: more codegen fixes - #1723

Open
WardBrian wants to merge 8 commits into
masterfrom
fix/opt-tuple-ad-again
Open

WardBrian wants to merge 8 commits into
masterfrom
fix/opt-tuple-ad-again

Conversation

@WardBrian

Copy link
Copy Markdown
Member

This is an improvement on #1713 and #1715 and fixes the --Oexperimental compilability of several models:

  • ./test/integration/good/tuples/tuple-promotion.stan
  • ./test/integration/good/tuples/tuple-constraints-data.stan
  • ./test/integration/good/tuples/tuple-dataonly.stan
  • ./test/integration/good/code-gen/container-promotion.stan
  • ./test/integration/good/code-gen/mixed_type_arrays.stan
  • ./test/integration/good/target_good.stan
  • ./test/integration/good/validate_conditional_op_good.stan

And possibly more, I'm running additional builds now.

The main change is to be stricter about how we assign autodiff types to tuples, and to improve the handling of promotions (especially inside of containers) in the optimize_ad_levels pass.

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 several more issues with --Oexperimental generating invalid 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

Copy link
Copy Markdown
Member Author

@SteveBronder mind having another look? I made a few small changes and now all of our tested models compile with --Oexperimental

@SteveBronder SteveBronder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You do a lot of overwriting of names here that would be easier to follow if the code did not do that. i.e. in TernaryIf (e1, e2, e3) there is a lot of overwriting of e1, e2, and e3

Besides that one small thing and I think it looks good

let e2, e3 =
match all_or_nothing [e2; e3] with
| [e2; e3] -> (e2, e3)
| _ -> assert false in

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why the assert false here? Should we have a more meaningful error message?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The function is essentially a map, so it’s impossible for the value to change length compared to the input

Comment on lines +1593 to +1597
(* hack: optimize_ad_levels should only need to run after optimize_soa,
but because it can turn variables into data (which are all AoS) it can
lead to compilation errors due to missing overloads in math. So we run
it twice to prevent variables that will interact with demoted variables
from ever getting tagged as SoA compatible. *)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need this but for ocaml comments

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Steve I wrote that comment all by myself

@WardBrian

Copy link
Copy Markdown
Member Author

I like name shadowing for these purposes because it prevents you from making the easy mistake of using the old value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants