Skip to content

Unifying the nation under one backend - #1

Merged
gurjeetjagwani merged 6 commits into
mainfrom
multi-backend
Nov 4, 2026
Merged

gurjeetjagwani merged 6 commits into
mainfrom
multi-backend

Conversation

@gurjeetjagwani

Copy link
Copy Markdown
Member

Merges the jax , tensorflow and pytorch backends into one codebase. Each of them can now be called with affine.jax , affine.tensorflow or affine.torch . Simple unit tests are integrated for the affine sampler in the tests directory. CHANGELOG.md details the exact changes implemented in this branch. setup.py is now replaced with pyproject.toml.

gurjeetjagwani and others added 4 commits July 29, 2026 14:12
Merge the main (TensorFlow), torch, and jax branches into a single
multi-backend package with a common sample/sample_batch API per backend
(affine.backends.{tensorflow,torch,jax}), imported lazily so only the
framework in use needs to be installed. The historical entry points of
all three branches are preserved at the top level.

Bug fixes relative to the old branches:
- torch: NaN log-probs were mapped to +inf in sample(), so proposals in
  invalid regions were accepted with probability 1; now -inf (rejected).
- torch: sample_batch() left the final chain row uninitialized (all
  zeros) with the default n_burnin/thin; the chain now stores the
  initial state and every step with no uninitialized entries.
- torch: sample() crashed on GPU walkers (random draws were CPU-only).
- All backends: acceptance is computed in log space, avoiding float32
  overflow-to-NaN (silent rejection) for ~130+ parameters.
- All backends: dtype-agnostic (float64 works), no in-place mutation of
  user tensors, single RNG source, optional seeding for reproducibility.
- Gibbs example notebook: latent chains were split with n_hyper_walkers
  instead of n_latent_walkers; tf.concat of scalars fails on modern TF.

Packaging: pyproject.toml with per-backend extras, pytest suite covering
all backends (moments recovery, NaN rejection regression, trailing-zeros
regression, reproducibility, legacy APIs), GitHub Actions CI, examples/
directory, CHANGELOG, expanded README.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@gurjeetjagwani gurjeetjagwani self-assigned this Sep 4, 2026
@gurjeetjagwani gurjeetjagwani added the enhancement New feature or request label Sep 4, 2026
@stevet40

Copy link
Copy Markdown
Member

Sorry for taking a while to get to this. I think it would be good if the basic example included all backends. Will try to make some modifications to that as I test it. Also, would be good if the tf backend used the same naming convention (i.e. from affine.tensorflow import sample), but there may be a reason/convention for not doing this?

stevet40 and others added 2 commits October 2, 2026 15:03
All six samplers now share one signature, with the PyTorch module as the
reference:

    sample([rng,] log_prob, n_steps, current_state, n_burnin=0, thin=1,
           args=(), progress=True, save_lp=False, [device=None,] *,
           save_extras=False, progressbar=None)

- tensorflow: affine_sample/affine_sample_batch renamed sample/sample_batch;
  the old names are kept as module-level aliases and the top-level wrappers
  still work.
- torch: sample takes (log_prob, n_steps, current_state, ...) like
  sample_batch, infers n_walkers and n_params from the walker shape, and
  accepts device=. The old positional style is no longer accepted.
- jax, tensorflow: keyword order follows torch; progress is the primary
  spelling of the progress-bar flag, progressbar remains an alias.

Tests, example notebooks, README and CHANGELOG updated to match; the stray
copy of the README torch snippet in the sampling example is removed.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@gurjeetjagwani
gurjeetjagwani merged commit 5fdd384 into main Nov 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants