-
Notifications
You must be signed in to change notification settings - Fork 104
Add FAQ section to improve user guidance #608
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,72 @@ | ||||||
--- | ||||||
title: "Frequently Asked Questions" | ||||||
description: "Common questions and answers about using Turing.jl" | ||||||
--- | ||||||
|
||||||
## Why is this variable being treated as random instead of observed? | ||||||
|
||||||
This is a common source of confusion. In Turing.jl, you can only manipulate expressions that explicitly appear on the left-hand side (LHS) of a `~` statement. | ||||||
|
||||||
For example, if your model contains: | ||||||
```julia | ||||||
x ~ filldist(Normal(), 2) | ||||||
``` | ||||||
|
||||||
You cannot directly condition on `x[2]` using `condition(model, @varname(x[2]) => 1.0)` because `x[2]` never appears on the LHS of a `~` statement. Only `x` as a whole appears there. | ||||||
|
||||||
Comment on lines
+15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good to have a bit more subtlety here, as there is a case where this isn't true: if you use @model function f1()
x = Vector{Float64}(undef, 3)
x .~ Normal()
end
m1 = f1() | (@varname(x[1]) => 1.0)
sample(m1, NUTS(), 100) # OK However, you can't condition on part of a distribution, so because @model function f2()
x = Vector{Float64}(undef, 3)
x ~ MvNormal(zeros(3), I)
end
m2 = f2() | (@varname(x[1]) => 1.0)
sample(m2, NUTS(), 100) # Not OK And It's quite confusing, and hence why I think it's important to explain this carefully. |
||||||
To understand more about how Turing determines whether a variable is treated as random or observed, see: | ||||||
- [Compiler Design Overview](../developers/compiler/design-overview/) - explains the heuristics Turing uses | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This one is also very outdated |
||||||
- [DynamicPPL Transformations](../developers/transforms/dynamicppl/) - details about variable transformations and the `@varname` macro | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I don't think this is relevant? |
||||||
- [Core Functionality](../core-functionality/) - basic explanation of the `~` notation and conditioning | ||||||
|
||||||
## How do I implement a sampler for a Turing.jl model? | ||||||
|
||||||
We have comprehensive guides on implementing custom samplers: | ||||||
- [Implementing Samplers Tutorial](../developers/inference/implementing-samplers/) - step-by-step guide on implementing samplers in the AbstractMCMC framework | ||||||
- [AbstractMCMC-Turing Interface](../developers/inference/abstractmcmc-turing/) - how to integrate your sampler with Turing | ||||||
- [AbstractMCMC Interface](../developers/inference/abstractmcmc-interface/) - the underlying interface documentation | ||||||
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
These docs are severely outdated |
||||||
|
||||||
## Can I use parallelism / threads in my model? | ||||||
|
||||||
Yes! Turing.jl supports both multithreaded and distributed sampling. See the [Core Functionality guide](../core-functionality/#sampling-multiple-chains) for detailed examples showing: | ||||||
- Multithreaded sampling using `MCMCThreads()` | ||||||
- Distributed sampling using `MCMCDistributed()` | ||||||
Comment on lines
+31
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one also needs more nuance. Sampling one chain per thread is indeed done with @model function f(x)
Threads.@threads for i in eachindex(x)
x[i] ~ Normal()
end
end That means that the execution of the model itself is inherently multithreaded (as opposed to sampling with MCMCThreads(), where each chain runs the model many times but only on one thread). The TLDR of this in the model is that threaded observe statements are OK but threaded assume statements are not (they often crash in unpredictable ways, or sometimes they actually work if you're really lucky). Another aspect maybe worth mentioning is that threads inside models don't work with many AD backends (see |
||||||
|
||||||
## How do I check the type stability of my Turing model? | ||||||
|
||||||
Type stability is crucial for performance. Check out: | ||||||
- [Performance Tips](../usage/performance-tips/) - includes specific advice on type stability | ||||||
- [Automatic Differentiation](../usage/automatic-differentiation/) - contains benchmarking utilities using `DynamicPPL.TestUtils.AD` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The AD page doesn't discuss type stability. I think the best 'high-level' way to determine type stability is https://turinglang.org/DynamicPPL.jl/stable/api/#DynamicPPL.DebugUtils.model_warntype although I've not used it myself. |
||||||
|
||||||
## How do I debug my Turing model? | ||||||
|
||||||
For debugging both statistical and syntactical issues: | ||||||
- [Troubleshooting Guide](../usage/troubleshooting/) - common errors and their solutions | ||||||
- For more advanced debugging, DynamicPPL provides `DynamicPPL.DebugUtils` for inspecting model internals | ||||||
|
||||||
## What are the main differences between Turing vs BUGS vs Stan syntax? | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The phrasing “differences between Turing vs BUGS vs Stan” is awkward. Consider rewording to “differences between Turing, BUGS, and Stan syntax.”
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
While there are many syntactic differences, key advantages of Turing include: | ||||||
- **Julia ecosystem**: Full access to Julia's profiling and debugging tools | ||||||
- **Parallel computing**: Much easier to use distributed and parallel computing inside models | ||||||
- **Flexibility**: Can use arbitrary Julia code within models | ||||||
- **Extensibility**: Easy to implement custom distributions and samplers | ||||||
Comment on lines
+49
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't answer the stated question? If it's a question about syntax, I would personally think it's best to have Turing vs Stan in a single section, and Turing vs (other PPL) can be a different section. Apart from the parameter block, two of the common things that Stan models have that Turing doesn't are |
||||||
|
||||||
## Which automatic differentiation backend should I use? | ||||||
|
||||||
The choice of AD backend can significantly impact performance. See: | ||||||
- [Automatic Differentiation Guide](../usage/automatic-differentiation/) - comprehensive comparison of ForwardDiff, Mooncake, ReverseDiff, and other backends | ||||||
- [Performance Tips](../usage/performance-tips/#choose-your-ad-backend) - quick guide on choosing backends | ||||||
- [AD Backend Benchmarks](https://turinglang.org/ADTests/) - performance comparisons across various models | ||||||
|
||||||
For more specific recommendations, check out the [DifferentiationInterface.jl tutorial](https://juliadiff.org/DifferentiationInterface.jl/DifferentiationInterfaceTest/stable/tutorial/). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think DITest is relevant yet --- to use the benchmarking functionality in there we would need to have a way of constructing |
||||||
|
||||||
## I changed one line of my model and now it's so much slower; why? | ||||||
|
||||||
Small changes can have big performance impacts. Common culprits include: | ||||||
- Type instability introduced by the change | ||||||
- Switching from vectorized to scalar operations (or vice versa) | ||||||
- Inadvertently causing AD backend incompatibilities | ||||||
- Breaking assumptions that allowed compiler optimizations | ||||||
|
||||||
See our [Performance Tips](../usage/performance-tips/) and [Troubleshooting Guide](../usage/troubleshooting/) for debugging performance regressions. | ||||||
Comment on lines
+64
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know you took the list questions from somewhere else, but I don't really like this question. I don't get the intent behind it (what is the answer supposed to be?) and the result of this is, I think, reflected in the text, which is very vague and IMO not very helpful. If the answer is basically to read the performance section. IME interactions with AD backend don't often lead to performance differences, usually it either runs fine or it crashes. If the AD is unusually slow it usually reflects slowness in the model itself. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.