mcmc-and-sampling
Use ZhuSuan for HMC, SGLD, PSGLD, SGHMC, SGNHT, annealed importance sampling, and posterior-sampling diagnostics.
- License 许可
- MIT
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- mcmc-and-sampling
- license
- MIT
- metadata
- { "disco-role": "operating" }
- description
- Use ZhuSuan for HMC, SGLD, PSGLD, SGHMC, SGNHT, annealed importance sampling, and posterior-sampling diagnostics.
- disable-model-invocation
- true
02 资源文件 4
- 参考文档references/api-reference.md1.2 KB
- 参考文档references/mcmc-workflows.md1.6 KB
- 参考文档references/troubleshooting.md1.4 KB
- 脚本scripts/mcmc_smoke.py1.6 KB
03 SKILL.md 正文
MCMC and Sampling
Use this sub-skill when the task is about posterior sampling or sampling-based estimation rather than variational training.
Read this when
- the user names
HMC,SGLD,PSGLD,SGHMC,SGNHT,AIS, oreffective_sample_size - the task is about sampling latent variables, posterior chains, or sampling diagnostics
- the request mentions toy posterior examples, topic models, or matrix factorization examples that use HMC or stochastic-gradient MCMC
What this sub-skill owns
HMC.sample(...)and the HMC adaptation knobs- stochastic-gradient MCMC classes:
SGLD,PSGLD,SGHMC,SGNHT AISfor annealed importance sampling over a sequence of temperatureseffective_sample_sizediagnostics for chain quality- toy examples and heavier sampling workflows such as PMF and topic models
What this sub-skill does not own
- core Bayesian-network construction
- ELBO / IWAE / VIMCO / REINFORCE / flow-based variational workflows
- generic training code that does not use a ZhuSuan sampler
Start here
- Read
references/mcmc-workflows.mdfor the sampler map and example matrix. - Read
references/api-reference.mdfor exact constructor and sample signatures. - Read
references/troubleshooting.mdfor adaptation, acceptance, and chain shape issues. - Run
scripts/mcmc_smoke.pyafter environment setup to confirm a short HMC chain, sampler instantiation, and ESS calculation.
Common tasks
Run HMC
- Build a
log_jointfunction or pass aMetaBayesianNet. - Create TensorFlow
Variableobjects to hold the latent state. - Instantiate
zs.HMC(...)with a small step size and a handful of leapfrog steps. - Call
sample(meta_bn, observed, latent)once per sampler instance. - Turn adaptation on only during burn-in.
Run stochastic-gradient MCMC
- Use
zs.SGLD,zs.SGHMC, orzs.SGNHTwhen the gradient is minibatch- based. - Keep the latent variables in
Variableobjects and feed minibatches into the observed data placeholders. - For SGHMC and SGNHT, watch the kinetic-energy / friction diagnostics.
Evaluate a chain
- Flatten the chain to a 2-D matrix when using
effective_sample_size. - For multiple chains or latent dimensions, keep track of the axis ordering so the diagnostic matches the intended latent dimension.
Good entry points
zhusuan/hmc.pyzhusuan/sgmcmc.pyzhusuan/evaluation.pyfor AISzhusuan/diagnostics.pyscripts/mcmc_smoke.py
Routing hints
- If the request is about a Bayesian model but not about sampling, route to
modeling-primitives. - If the request is about a variational posterior or a lower bound, route to
variational-inference. - If the user asks about a plotting-heavy toy example, treat it as a workflow reference rather than a required verification target unless the user explicitly wants the demo run.
04 同仓库更多 Skills
passnet-triton-opt
Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.
passnet-solve
END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.
passnet-skill
PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.
passnet-pattern-fusion
Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.
passnet-orchestrate
Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.
passnet-feedback
Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).