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

Introduction of model_typed and model_warntype in DebugUtils #708

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c"
LogDensityProblemsAD = "996a588d-648d-4e1f-a8f0-a84b347e47b1"
Expand Down
7 changes: 7 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ And some which might be useful to determine certain properties of the model base
DynamicPPL.has_static_constraints
```

For determining whether one might have type instabilities in the model, the following can be useful

```@docs
DynamicPPL.DebugUtils.model_warntype
DynamicPPL.DebugUtils.model_typed
```

## Advanced

### Variable names
Expand Down
59 changes: 59 additions & 0 deletions src/debug_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using ..DynamicPPL: broadcast_safe, AbstractContext, childcontext

using Random: Random
using Accessors: Accessors
using InteractiveUtils: InteractiveUtils

using DocStringExtensions
using Distributions
Expand Down Expand Up @@ -678,4 +679,62 @@ function has_static_constraints(
return all_the_same(transforms)
end

"""
model_warntype(model[, varinfo, context]; optimize=true)
willtebbutt marked this conversation as resolved.
Show resolved Hide resolved

Check the type stability of the model's evaluator, warning about any potential issues.

This simply calls `@code_warntype` on the model's evaluator, filling in internal arguments where needed.

# Arguments
- `model::Model`: The model to check.
- `varinfo::AbstractVarInfo`: The varinfo to use when evaluating the model. Default: `VarInfo(model)`.
- `context::AbstractContext`: The context to use when evaluating the model. Default: [`DefaultContext`](@ref).

# Keyword Arguments
- `optimize::Bool`: Whether to generate optimized code. Default: `false`.
"""
function model_warntype(
willtebbutt marked this conversation as resolved.
Show resolved Hide resolved
model::Model,
varinfo::AbstractVarInfo=VarInfo(model),
context::AbstractContext=DefaultContext();
optimize::Bool=false,
)
args, kwargs = DynamicPPL.make_evaluate_args_and_kwargs(model, varinfo, context)
return if isempty(kwargs)
InteractiveUtils.@code_warntype optimize = optimize model.f(args...)
else
InteractiveUtils.@code_warntype optimize = optimize model.f(args...; kwargs...)
end
end

"""
willtebbutt marked this conversation as resolved.
Show resolved Hide resolved
model_typed(model[, varinfo, context]; optimize=true)
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 -- needs to specify what the defaults are.


Return the type inference for the model's evaluator.

This simply calls `@code_typed` on the model's evaluator, filling in internal arguments where needed.

# Arguments
- `model::Model`: The model to check.
- `varinfo::AbstractVarInfo`: The varinfo to use when evaluating the model. Default: `VarInfo(model)`.
- `context::AbstractContext`: The context to use when evaluating the model. Default: [`DefaultContext`](@ref).

# Keyword Arguments
- `optimize::Bool`: Whether to generate optimized code. Default: `true`.
"""
function model_typed(
model::Model,
varinfo::AbstractVarInfo=VarInfo(model),
context::AbstractContext=DefaultContext();
optimize::Bool=true,
)
args, kwargs = DynamicPPL.make_evaluate_args_and_kwargs(model, varinfo, context)
return if isempty(kwargs)
InteractiveUtils.@code_typed optimize = optimize model.f(args...)
else
InteractiveUtils.@code_typed optimize = optimize model.f(args...; kwargs...)
end
end

end
14 changes: 14 additions & 0 deletions test/debug_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,18 @@
@test check_model(model; error_on_failure=true)
end
end

@testset "model_warntype & model_codetyped" begin
@model demo_without_kwargs(x) = y ~ Normal(x, 1)
@model demo_with_kwargs(x; z=1) = y ~ Normal(x, z)

for model in [demo_without_kwargs(1.0), demo_with_kwargs(1.0)]
codeinfo, retype = DynamicPPL.DebugUtils.model_typed(model)
@test codeinfo isa Core.CodeInfo
@test retype <: Tuple

# Just make sure the following is runnable.
@test (DynamicPPL.DebugUtils.model_warntype(model); true)
end
end
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ include("test_util.jl")
include("pointwise_logdensities.jl")

include("lkj.jl")

include("debug_utils.jl")
end

@testset "compat" begin
Expand Down
Loading