-
Notifications
You must be signed in to change notification settings - Fork 106
Add debugging page and benchmarking and profiling guidance #701
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
Open
shravanngoswamii
wants to merge
3
commits into
main
Choose a base branch
from
sg/perf-debug-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| --- | ||
| title: Debugging Models | ||
| engine: julia | ||
| --- | ||
|
|
||
| ```{julia} | ||
| #| echo: false | ||
| #| output: false | ||
| using Pkg; | ||
| Pkg.instantiate(); | ||
| ``` | ||
|
|
||
| This page describes the tools available for checking that a Turing model is defined correctly. | ||
| It complements the [Troubleshooting]({{<meta usage-troubleshooting>}}) page, which explains specific error messages: the tools here help you find problems before (or without) hitting an error. | ||
|
|
||
| ```{julia} | ||
| using Turing | ||
| ``` | ||
|
|
||
| ## Checking model validity with `check_model` | ||
|
|
||
| `DynamicPPL.check_model` evaluates a model once and checks it for common problems. | ||
| It returns `true` if the check passes, and `false` (with warnings explaining the problem) otherwise. | ||
|
|
||
| Specifically, it checks for: | ||
|
|
||
| - the same variable, or overlapping variables (such as `x` and `x[1]`), appearing on the left-hand side of more than one tilde statement; | ||
| - `NaN` values on the left-hand side of observe statements; | ||
| - with the keyword argument `fail_if_discrete=true`, the presence of discrete random variables, which gradient-based samplers such as HMC and NUTS cannot handle. | ||
|
|
||
| It is equally important to know what it does not catch. | ||
| The model is evaluated a single time, with values drawn from the prior, so problems that only occur for some parameter values (for example, in a branch that this particular evaluation does not take) can be missed. | ||
| It also checks only the structure of the model, not its statistical soundness: it cannot tell you that a prior is unreasonable or that parameters are not identifiable. | ||
| Type instability and incorrect gradients are separate concerns, covered by the checks in the following sections. | ||
|
|
||
| For example, this model observes a `NaN` value, which usually indicates a bug in the data preparation: | ||
|
|
||
| ```{julia} | ||
| using DynamicPPL: check_model | ||
|
|
||
| @model function gaussian(y) | ||
| x ~ Normal() | ||
| y ~ Normal(x) | ||
| end | ||
|
|
||
| check_model(gaussian(NaN)) | ||
| ``` | ||
|
|
||
| whereas the same model with valid data passes: | ||
|
|
||
| ```{julia} | ||
| check_model(gaussian(1.5)) | ||
| ``` | ||
|
|
||
| Turing runs this check automatically at the start of `sample` (and the mode estimation functions). | ||
| For gradient-based samplers, this includes the discrete-variable check. | ||
| If the check reports a false positive for your model, you can disable it by passing `check_model=false` to `sample`. | ||
|
|
||
| The keyword argument `error_on_failure=true` makes `check_model` throw an error instead of returning `false`, which is useful in scripts and tests. | ||
|
|
||
| ## Checking type stability | ||
|
|
||
| Type instability is one of the most common reasons for a model being slower than expected. | ||
| `DynamicPPL.DebugUtils.model_warntype(model)` works like `@code_warntype`, but for an entire model: types that the compiler cannot infer are marked in red in the output. | ||
| The [Performance Tips]({{<meta usage-performance-tips>}}#ensure-that-types-in-your-model-can-be-inferred) page has a worked example, along with advice on how to fix the instabilities it finds. | ||
| There is also `DynamicPPL.DebugUtils.model_typed`, which returns the typed representation instead of printing it. | ||
|
|
||
| ## Checking gradients | ||
|
|
||
| If a model samples poorly with a gradient-based sampler such as NUTS, it is worth checking whether the AD backend computes correct gradients for it. | ||
| `DynamicPPL.TestUtils.AD.run_ad` evaluates the gradient of a model with a given backend and, by default, tests it against a reference backend: | ||
|
|
||
| ```{julia} | ||
| using ADTypes: AutoReverseDiff | ||
| using DynamicPPL.TestUtils.AD: run_ad | ||
| using ReverseDiff | ||
|
|
||
| run_ad(gaussian(1.5), AutoReverseDiff()) | ||
| ``` | ||
|
|
||
| If the gradient is incorrect, this errors. | ||
| For debugging `NaN` or infinite gradients, see the [Troubleshooting]({{<meta usage-troubleshooting>}}) page. | ||
|
|
||
| ## Further help | ||
|
|
||
| This page covers the most commonly useful tools. | ||
| DynamicPPL provides a few more, such as `DynamicPPL.has_static_constraints`; the full list is in [the DynamicPPL API documentation](https://turinglang.org/DynamicPPL.jl/stable/api/#Debugging-Utilities). | ||
|
|
||
| If these tools do not surface the problem, feel free to [open an issue](https://github.com/TuringLang/Turing.jl/issues), ideally with the smallest model that reproduces it. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.