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 for #596 #597

Merged
merged 16 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
43 changes: 41 additions & 2 deletions src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,43 @@ hasmissing(::Type{>:Missing}) = true
hasmissing(::Type{<:AbstractArray{TA}}) where {TA} = hasmissing(TA)
hasmissing(::Type{Union{}}) = false # issue #368

"""
TypeWrap{T}

A wrapper type used internally to make expressions such as `::Type{TV}` in the model arguments
ending up as a `DataType`.
torfjelde marked this conversation as resolved.
Show resolved Hide resolved
"""
struct TypeWrap{T} end

arg_type_is_type(e) = false
function arg_type_is_type(e::Expr)
return Meta.isexpr(e, :curly) && length(e.args) > 1 && e.args[1] === :Type
end
torfjelde marked this conversation as resolved.
Show resolved Hide resolved

function splitarg_to_expr((arg_name, arg_type, is_splat, default))
return is_splat ? :($arg_name...) : arg_name
end

"""
transform_args(args)

Return transformed `args` used in both the model constructor and evaluator.

Specifically, this replaces expressions of the form `::Type{TV}=Vector{Float64}`
with `::TypeWrap{TV}=TypeWrap{Vector{Float64}}()` to avoid introducing `DataType`.
"""
function transform_args(args)
splitargs = map(args) do arg
arg_name, arg_type, is_splat, default = MacroTools.splitarg(arg)
return if arg_type_is_type(arg_type)
arg_name, :($TypeWrap{$(arg_type.args[2])}), is_splat, :($TypeWrap{$default}())
else
arg_name, arg_type, is_splat, default
end
end
return map(Base.splat(MacroTools.combinearg), splitargs)
end

function namedtuple_from_splitargs(splitargs)
names = map(splitargs) do (arg_name, arg_type, is_splat, default)
is_splat ? Symbol("#splat#$(arg_name)") : arg_name
Expand All @@ -599,7 +632,7 @@ is_splat_symbol(s::Symbol) = startswith(string(s), "#splat#")
Builds the output expression.
"""
function build_output(modeldef, linenumbernode)
args = modeldef[:args]
args = transform_args(modeldef[:args])
kwargs = modeldef[:kwargs]
Copy link
Member

Choose a reason for hiding this comment

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

I think this should probably be applied to keyword arguments as well?

Suggested change
kwargs = modeldef[:kwargs]
kwargs = transform_args(modeldef[:kwargs])

(not sure if it can be applied directly here in this way)

Copy link
Member Author

Choose a reason for hiding this comment

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

Why? Do we care if we specialize on the kwargs?

Copy link
Member

Choose a reason for hiding this comment

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

IIRC we store them in the same way as pos arguments in the model.

Copy link
Member

Choose a reason for hiding this comment

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

So the Model will generally still operate with DataTypes unless we apply the same fix to kwarga.

Copy link
Member Author

Choose a reason for hiding this comment

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

No you're right 👍 Working on it now

Copy link
Member Author

Choose a reason for hiding this comment

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

Added this 👍


## Build the anonymous evaluator from the user-provided model definition.
Expand Down Expand Up @@ -645,6 +678,8 @@ function build_output(modeldef, linenumbernode)
args_nt = namedtuple_from_splitargs(args_split)
kwargs_inclusion = map(splitarg_to_expr, kwargs_split)

# Need to update `args` since we might have added `TypeWrap` to the types.
modeldef[:args] = args
# Update the function body of the user-specified model.
# We use `MacroTools.@q begin ... end` instead of regular `quote ... end` to ensure
# that no new `LineNumberNode`s are added apart from the reference `linenumbernode`
Expand Down Expand Up @@ -689,9 +724,13 @@ function matchingvalue(sampler, vi, value)
return value
end
end
# If we hit `Type` or `TypeWrap`, we immediately jump to `get_matching_type`.
function matchingvalue(sampler::AbstractSampler, vi, value::FloatOrArrayType)
return get_matching_type(sampler, vi, value)
end
function matchingvalue(sampler::AbstractSampler, vi, value::TypeWrap{T}) where {T}
return TypeWrap{get_matching_type(sampler, vi, T)}()
end

function matchingvalue(context::AbstractContext, vi, value)
return matchingvalue(NodeTrait(matchingvalue, context), context, vi, value)
Expand All @@ -707,7 +746,7 @@ function matchingvalue(context::SamplingContext, vi, value)
end

"""
get_matching_type(spl::AbstractSampler, vi, ::Type{T}) where {T}
get_matching_type(spl::AbstractSampler, vi, ::TypeWrap{T}) where {T}

Get the specialized version of type `T` for sampler `spl`.

Expand Down
4 changes: 1 addition & 3 deletions test/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,7 @@ is_typed_varinfo(varinfo::DynamicPPL.SimpleVarInfo{<:NamedTuple}) = true

@testset "Type stability of models" begin
models_to_test = [
# FIXME: Fix issues with type-stability in `DEMO_MODELS`.
# DynamicPPL.TestUtils.DEMO_MODELS...,
DynamicPPL.TestUtils.demo_lkjchol(2),
DynamicPPL.TestUtils.DEMO_MODELS..., DynamicPPL.TestUtils.demo_lkjchol(2)
]
@testset "$(model.f)" for model in models_to_test
vns = DynamicPPL.TestUtils.varnames(model)
Expand Down
Loading