-
-
Notifications
You must be signed in to change notification settings - Fork 51
Move assignment logic into promotion functionality #1102
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fbfda16
Move assignment logic into promotion functionality
WardBrian 59c5c8f
Promote array/rowvec elements if needed
WardBrian b4af42d
Ignore promotions during optimization eval
WardBrian e7fc285
Move promotions into separate module
WardBrian 651e874
Try to avoid unnecessary promotions in C++
WardBrian 3933c23
Improve ADlevel handling in promotions
WardBrian 0a25e81
Merge branch 'master' into assignment-promotion
WardBrian d72eef0
Properly promote arrays
WardBrian 37b9562
Properly promote vectors and matricies to Var
WardBrian 43ce4e2
Use stan::math::to_complex when we can
WardBrian f822b73
Cleanup
WardBrian a352f47
Only use to_complex for DataOnly
WardBrian da89343
Merge branch 'master' into assignment-promotion
WardBrian 59f0c9c
Merge branch 'master' into assignment-promotion
WardBrian 9e055d0
Merge branch 'master' into assignment-promotion
WardBrian f64e9bd
Remove comment
WardBrian 0a25b5a
Merge branch 'master' into assignment-promotion
WardBrian 225669f
Cleanup
WardBrian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| open Core_kernel | ||
| open Core_kernel.Poly | ||
| module UnsizedType = Middle.UnsizedType | ||
|
|
||
| (** Type to represent promotions in the typechecker. | ||
| This can be used to return information about how to promote | ||
| expressions for use in [Ast.Promotion] *) | ||
| type t = | ||
| | NoPromotion | ||
| | IntToReal | ||
| | ToVar (* used in arrays, not functions *) | ||
| | ToComplexVar (* used in arrays, not functions *) | ||
| | IntToComplex | ||
| | RealToComplex | ||
|
|
||
| let promote_inner (exp : Ast.typed_expression) prom = | ||
| let emeta = exp.emeta in | ||
| match prom with | ||
| | ToVar -> | ||
| Ast. | ||
| { expr= Ast.Promotion (exp, UReal, AutoDiffable) | ||
| ; emeta= | ||
| { emeta with | ||
| type_= UnsizedType.promote_array emeta.type_ UReal | ||
| ; ad_level= AutoDiffable } } | ||
| | ToComplexVar -> | ||
| Ast. | ||
| { expr= Ast.Promotion (exp, UComplex, AutoDiffable) | ||
| ; emeta= | ||
| { emeta with | ||
| type_= UnsizedType.promote_array emeta.type_ UComplex | ||
| ; ad_level= AutoDiffable } } | ||
| | IntToReal when UnsizedType.is_int_type emeta.type_ -> | ||
| Ast. | ||
| { expr= Ast.Promotion (exp, UReal, emeta.ad_level) | ||
| ; emeta= {emeta with type_= UnsizedType.promote_array emeta.type_ UReal} | ||
| } | ||
| | (IntToComplex | RealToComplex) | ||
| when not (UnsizedType.is_complex_type emeta.type_) -> | ||
| (* these two promotions are separated for cost, but are actually the same promotion *) | ||
| { expr= Promotion (exp, UComplex, emeta.ad_level) | ||
| ; emeta= {emeta with type_= UnsizedType.promote_array emeta.type_ UComplex} | ||
| } | ||
| | _ -> exp | ||
|
|
||
| let rec promote (exp : Ast.typed_expression) prom = | ||
| (* promote arrays and rowvector literals at the lowest level to avoid unnecessary copies *) | ||
| let open Ast in | ||
| match exp.expr with | ||
| | ArrayExpr es -> | ||
| let pes = List.map ~f:(fun e -> promote e prom) es in | ||
| let fst = List.hd_exn pes in | ||
| let type_, ad_level = (fst.emeta.type_, fst.emeta.ad_level) in | ||
| { expr= ArrayExpr pes | ||
| ; emeta= | ||
| { exp.emeta with | ||
| type_= UnsizedType.promote_array exp.emeta.type_ type_ | ||
| ; ad_level } } | ||
| | RowVectorExpr (_ :: _ as es) -> | ||
| let pes = List.map ~f:(fun e -> promote e prom) es in | ||
| let fst = List.hd_exn pes in | ||
| let ad_level = fst.emeta.ad_level in | ||
| {expr= RowVectorExpr pes; emeta= {exp.emeta with ad_level}} | ||
| | _ -> promote_inner exp prom | ||
|
|
||
| let promote_list es promotions = List.map2_exn es promotions ~f:promote | ||
|
|
||
| (** Get the promotion needed to make the second type into the first. | ||
| Types NEED to have previously been checked to be promotable | ||
| *) | ||
| let rec get_type_promotion_exn (ad, ty) (ad2, ty2) = | ||
| match (ty, ty2) with | ||
| | UnsizedType.(UReal, (UReal | UInt) | UVector, UVector | UMatrix, UMatrix) | ||
| when ad <> ad2 -> | ||
| ToVar | ||
| | UComplex, (UReal | UInt | UComplex) when ad <> ad2 -> ToComplexVar | ||
| | UReal, UInt -> IntToReal | ||
| | UComplex, UInt -> IntToComplex | ||
| | UComplex, UReal -> RealToComplex | ||
| | UArray nt1, UArray nt2 -> get_type_promotion_exn (ad, nt1) (ad2, nt2) | ||
| | t1, t2 when t1 = t2 -> NoPromotion | ||
| | _, _ -> | ||
| Common.FatalError.fatal_error_msg | ||
| [%message | ||
| "Tried to get promotion of mismatched types!" | ||
| (ty : UnsizedType.t) | ||
| (ty2 : UnsizedType.t)] | ||
|
|
||
| (** Calculate the "cost"/number of promotions performed. | ||
| Used to disambiguate function signatures | ||
| *) | ||
| let promotion_cost p = | ||
| match p with | ||
| | NoPromotion | ToVar | ToComplexVar -> 0 | ||
| | RealToComplex | IntToReal -> 1 | ||
| | IntToComplex -> 2 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.