You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add an argument to condition function to skip generating and evaling log density function (#394)
## Motivation
`condition` currently regenerates the log-density function on every
call, invoking MacroTools transforms and `eval`. This is too slow for
hot loops.
We want a fast path for repeated conditioning that:
- Avoids regeneration
- Lets users update observed values in-place without changing graph
structure
- Allows explicit regeneration later to regain compiled performance when
desired
## Summary of Changes
- `condition` now supports a fast path via a keyword flag:
- `condition(model, conditioning_spec;
regenerate_log_density::Bool=true)`
- When `false`: skips regeneration, nulls the compiled function, and
forces graph evaluation mode
- New helper for value updates without reconditioning:
- `set_observed_values!(model, obs::Dict{<:VarName,<:Any})`
- Updates evaluation environment values of already-observed stochastic
variables in-place
- Validates existence, stochasticity, and observation status
- New function to regenerate the compiled log-density function without
changing the evaluation mode:
- `regenerate_log_density_function(model; force=false)`
- Re-generates for the model’s current graph + environment and refreshes
graph evaluation data
- Exports added:
- `set_observed_values!` and `regenerate_log_density_function` (from
`Model` module)
## Tiny Benchmark for Sanity Check
with
```julia
@bugs begin
for i in 1:N
x[i] ~ Normal(0, 1)
end
y ~ Normal(sum(x[:]), 1)
end
```
```
BenchmarkTools results (N=100, iters=200):
Single condition: regular = 13.96 ms, fast = 0.15 ms, speedup ≈ 92.3x
Loop conditioning: regular = 28139.5 ms, fast(update only) = 0.2 ms, speedup ≈ 112840.1x
```
Interpretation:
- Single call to `condition`: ~90x faster when skipping regeneration
- Hot loop: one fast `condition` + repeated `set_observed_values!` is
~1e5x faster than repeatedly reconditioning
0 commit comments