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

Replace usage of typeof (inferred) by Core.Typeof (runtime) #627

Closed
wants to merge 4 commits into from

Conversation

torfjelde
Copy link
Member

This is per @wsmoses suggestion after a chat we (and @mhauru ) had earlier today.

It's an attempt to fix some issues we've had with Enzyme.jl-integration; in particular, #643

@coveralls
Copy link

coveralls commented Jul 1, 2024

Pull Request Test Coverage Report for Build 9749798418

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 2 files are covered.
  • 24 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.9%) to 76.624%

Files with Coverage Reduction New Missed Lines %
src/model.jl 1 88.35%
src/abstract_varinfo.jl 9 82.68%
src/threadsafe.jl 14 48.25%
Totals Coverage Status
Change from base Build 9678167735: -0.9%
Covered Lines: 2642
Relevant Lines: 3448

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jul 1, 2024

Pull Request Test Coverage Report for Build 9749858036

Details

  • 3 of 3 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 77.531%

Totals Coverage Status
Change from base Build 9678167735: 0.0%
Covered Lines: 2657
Relevant Lines: 3427

💛 - Coveralls

@torfjelde
Copy link
Member Author

Unfortately doesn't seem to address the issue in #643 nor the failing one herehttps://github.com/torfjelde/EnzymeCon2024-Turing.jl-benchmarks/blob/main/src/models/fails/satellite-ssm-matrix.jl 😕

@@ -363,7 +363,8 @@ Determine the default `eltype` of the values returned by `vi[spl]`.
This method is considered legacy, and is likely to be deprecated in the future.
"""
function Base.eltype(vi::AbstractVarInfo, spl::Union{AbstractSampler,SampleFromPrior})
T = Base.promote_op(getindex, typeof(vi), typeof(spl))
# TODO(torfjelde): Is there _any_ scenario where this isn't just `typeof(getlogp(vi))`?
Copy link
Member

Choose a reason for hiding this comment

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

promote_op was introduced when fixing TuringLang/Turing.jl#2151. If getindex errors it will return Union{}, which - probably - is not equal to typeof(getlogp(vi)).

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah true 👍

@@ -363,7 +363,8 @@ Determine the default `eltype` of the values returned by `vi[spl]`.
This method is considered legacy, and is likely to be deprecated in the future.
"""
function Base.eltype(vi::AbstractVarInfo, spl::Union{AbstractSampler,SampleFromPrior})
T = Base.promote_op(getindex, typeof(vi), typeof(spl))
# TODO(torfjelde): Is there _any_ scenario where this isn't just `typeof(getlogp(vi))`?
T = Base.promote_op(getindex, Core.Typeof(vi), Core.Typeof(spl))
Copy link
Member

Choose a reason for hiding this comment

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

It's not clear to me why this would be helpful or needed here - typeof is commonly used in this way to avoid where clauses (which is even recommended in the Julia docs). There also shouldn't be any problem to infer the result of typeof here?

@@ -737,7 +737,7 @@ For a `context` that is _not_ a `SamplingContext`, we fall back to
`matchingvalue(SampleFromPrior(), vi, value)`.
"""
function matchingvalue(sampler, vi, value)
T = typeof(value)
T = Core.Typeof(value)
Copy link
Member

Choose a reason for hiding this comment

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

Same here, I don't see why this would be beneficial in this place? Maybe the problem is rather that value is not inferred in the first place?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed it might be the case that value is not inferred properly.

If I've understood @wsmoses correctly, Enzyme should be using the runtime value rather than the inferred value, and so this is the discrepancy between how we do it in DPPL: we use inferred instead of runtime. However, this didn't seem to help 😕

@@ -217,7 +217,7 @@ function SimpleVarInfo(θ::Union{<:NamedTuple,<:AbstractDict})
SimpleVarInfo{SIMPLEVARINFO_DEFAULT_ELTYPE}(θ)
else
# Infer from `values`.
SimpleVarInfo{float_type_with_fallback(infer_nested_eltype(typeof(θ)))}(θ)
SimpleVarInfo{float_type_with_fallback(infer_nested_eltype(Core.Typeof(θ)))}(θ)
Copy link
Member

Choose a reason for hiding this comment

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

Same here?

@torfjelde
Copy link
Member Author

I think I'll have to defer to @wsmoses for your comments @devmotion . From convo with him, I sort of got the impression that we wanted to avoid typeof and we were hypothesizing if that might have been a cause of an Enzyme issue; seems like it's not though 😕

@torfjelde torfjelde marked this pull request as draft July 2, 2024 09:15
@torfjelde
Copy link
Member Author

Converted to DRAFT though because it wasn't my intention to have this be merged into master atm; just for Enzyme testing

@wsmoses
Copy link
Contributor

wsmoses commented Jul 2, 2024 via email

@torfjelde
Copy link
Member Author

I recommended Tor see if propagating the use of the actual runtime type
would resolve it here, but if not Tor you're going to have to see what
other use of undefined behavior results in the matrix any in your example

Noted! But I'm unfortunately short on time these days, so uncertain if I'll be the best one to have a go at this. Might be better for @mhauru to have a look at it? In particular given that you two are co-hacking atm:)

@mhauru
Copy link
Member

mhauru commented Jul 3, 2024

I can take a look, may take may a few days to get to this.

@yebai
Copy link
Member

yebai commented Aug 21, 2024

Ref: #643

@yebai yebai closed this Aug 21, 2024
@yebai yebai deleted the torfjelde/typeof branch August 21, 2024 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants