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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/abstract_varinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 👍

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?

if T === Union{}
# In this case `getindex(vi, spl)` errors
# Let us throw a more descriptive error message
Expand Down
2 changes: 1 addition & 1 deletion src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 😕

if hasmissing(T)
_value = convert(get_matching_type(sampler, vi, T), value)
if _value === value
Expand Down
2 changes: 1 addition & 1 deletion src/simple_varinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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?

end
end

Expand Down
Loading