Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9f392a7
added InteractiveUtils.jl as a dep + `model_typed` and
torfjelde Oct 31, 2024
68295e8
Apply suggestions from code review
torfjelde Oct 31, 2024
6a42613
added `optimize` kwarg to `model_typed` and `model_warntype`
torfjelde Nov 4, 2024
b60aaec
Merge remote-tracking branch 'origin/torfjelde/code-warntype' into to…
torfjelde Nov 4, 2024
a6584c4
formatting
torfjelde Nov 4, 2024
076aee5
expanded docstring for `model_warntype` and `model_codetyped` and
torfjelde Nov 5, 2024
52b4301
added testing for `model_codetyped` and `model_warntype`
torfjelde Nov 5, 2024
bc43b98
Merge remote-tracking branch 'origin/torfjelde/code-warntype' into to…
torfjelde Nov 5, 2024
4794c4f
bump patch version
torfjelde Nov 5, 2024
2925ee1
Merge branch 'master' into torfjelde/code-warntype
torfjelde Nov 5, 2024
afdcd82
added tests for `model_codetyped` and `model_warntype` for model with
torfjelde Nov 5, 2024
5519a83
added test/debug_utils.jl to test/runtests.jl
torfjelde Nov 5, 2024
6b6c7dd
renamed `model_codetyped` to `model_typed` to be consistent with the
torfjelde Nov 5, 2024
0fc3c1e
Merge branch 'master' into torfjelde/code-warntype
torfjelde Nov 6, 2024
fb5f644
avoid usage of macros within `model_warntype` and `model_typed` due
torfjelde Nov 8, 2024
cf750e1
formatting
torfjelde Nov 11, 2024
f6115c7
renamed `_make_evaluate_args_and_kwargs` to
torfjelde Nov 11, 2024
c794ff4
Merge branch 'master' into torfjelde/code-warntype
torfjelde Nov 11, 2024
c6c35d8
formatting
torfjelde Nov 11, 2024
4f882a7
bump patch version
torfjelde Nov 11, 2024
7f2863b
Update src/debug_utils.jl
torfjelde Nov 11, 2024
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_codetyped
```

## 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 Random: Random
using Accessors: Accessors
using InteractiveUtils: InteractiveUtils

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

"""
model_warntype(model[, varinfo, context]; optimize=true)

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(

Check warning on line 697 in src/debug_utils.jl

View check run for this annotation

Codecov / codecov/patch

src/debug_utils.jl#L697

Added line #L697 was not covered by tests
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...)

Check warning on line 705 in src/debug_utils.jl

View check run for this annotation

Codecov / codecov/patch

src/debug_utils.jl#L703-L705

Added lines #L703 - L705 were not covered by tests
else
InteractiveUtils.@code_warntype optimize = optimize model.f(args...; kwargs...)

Check warning on line 707 in src/debug_utils.jl

View check run for this annotation

Codecov / codecov/patch

src/debug_utils.jl#L707

Added line #L707 was not covered by tests
end
end

"""
model_codetyped(model[, varinfo, context]; optimize=true)

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_codetyped(

Check warning on line 726 in src/debug_utils.jl

View check run for this annotation

Codecov / codecov/patch

src/debug_utils.jl#L726

Added line #L726 was not covered by tests
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...)

Check warning on line 734 in src/debug_utils.jl

View check run for this annotation

Codecov / codecov/patch

src/debug_utils.jl#L732-L734

Added lines #L732 - L734 were not covered by tests
else
InteractiveUtils.@code_typed optimize = optimize model.f(args...; kwargs...)

Check warning on line 736 in src/debug_utils.jl

View check run for this annotation

Codecov / codecov/patch

src/debug_utils.jl#L736

Added line #L736 was not covered by tests
end
end

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

@testset "model_warntype & model_codetyped" begin
model = DynamicPPL.TestUtils.DEMO_MODELS[1]
codeinfo, retype = DynamicPPL.DebugUtils.model_codetyped(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