Skip to content

Automatically minibatch a model - #700

Draft
ricardoV94 wants to merge 3 commits into
pymc-devs:mainfrom
ricardoV94:minibatch_model
Draft

Automatically minibatch a model#700
ricardoV94 wants to merge 3 commits into
pymc-devs:mainfrom
ricardoV94:minibatch_model

Conversation

@ricardoV94

@ricardoV94 ricardoV94 commented Jun 26, 2026

Copy link
Copy Markdown
Member

No description provided.

Special-case Blockwise(Dot) so a known batch dimension on the non-contracted
(m, p) axes maps 1:1 to the output, while a known contracted dimension raises.
Also fix the CAReduce check to use `is not None` so reducing a tracked dim
labeled 0 (the leading axis) is correctly rejected.
@codecov-commenter

codecov-commenter commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.31469% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.94%. Comparing base (86fac3c) to head (c22961f).
⚠️ Report is 52 commits behind head on main.

Files with missing lines Patch % Lines
pymc_extras/model/marginal/graph_analysis.py 75.81% 37 Missing ⚠️
pymc_extras/model/transforms/minibatch.py 96.24% 5 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main     #700       +/-   ##
===========================================
+ Coverage   51.60%   90.94%   +39.33%     
===========================================
  Files          73       91       +18     
  Lines        8003     8700      +697     
===========================================
+ Hits         4130     7912     +3782     
+ Misses       3873      788     -3085     
Files with missing lines Coverage Δ
pymc_extras/model/transforms/minibatch.py 96.24% <96.24%> (ø)
pymc_extras/model/marginal/graph_analysis.py 87.85% <75.81%> (+71.37%) ⬆️

... and 29 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ricardoV94
ricardoV94 force-pushed the minibatch_model branch 2 times, most recently from 3a72ec1 to e3dcf44 Compare June 26, 2026 17:28
Add `subgraph_batch_dim_ancestors`, the input-direction dual of the forward
connection pass: given one batch axis of an output variable, propagate it
backward to the ancestors that feed it, returning which axis of each ancestor
carries it. A reduced or contracted axis is not propagated (the ancestor below
it is not reached); an op whose inverse cannot be traced propagates the axis
conservatively to all its inputs so nothing carrying it is silently dropped.

Rename the forward pass `_subgraph_batch_dim_connection` to
`_subgraph_batch_dim_clients` to name its direction explicitly alongside the new
ancestor trace.
Add `minibatch`, which rewrites a model to draw random minibatches of its
observed data and rescales the affected observed logp by total_size, the
transform-based equivalent of building the model with `pm.Minibatch`.

The minibatched (leading) axis of each observed is traced backward with
`subgraph_batch_dim_ancestors` to find the variables that share it: the data to
slice, and any free RV or Potential that would need rescaling. Forward tracing
from the data and resized observeds finds Deterministics to relabel and terms
that are clients of the minibatch. With `validate=True` the transform rejects
constructs whose logp cannot be correctly rescaled (free RVs / Potentials on the
minibatched axis, partially specified data, non-batchable observeds).
raise TypeError("batch_size must be an integer")

if observed is None:
observed = [rv for rv in model.observed_RVs if rv.ndim > 0]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grab the ones with batch ndim not just rv.ndim > 0

@ricardoV94 ricardoV94 mentioned this pull request Jul 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants