Skip to content

Add self-contained implementation of the Simformer approach - #36

Open
tHarvey303 wants to merge 3 commits into
mainfrom
feature/torch-simformer
Open

tHarvey303 wants to merge 3 commits into
mainfrom
feature/torch-simformer

Conversation

@tHarvey303

@tHarvey303 tHarvey303 commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator

Written in torch rather than jax, this reimplements the Simformer approach to learn full conditional distributions.

Summary by CodeRabbit

  • New Features

    • Added native PyTorch Simformer training and inference.
    • Added joint, posterior, likelihood, masked, and interval-constrained sampling.
    • Added log-probability evaluation and reliable model save/load support.
    • Added a two-moons demonstration with sampling and diagnostics.
  • Documentation

    • Updated Simformer documentation, examples, and project development guidance.
  • Chores

    • Removed JAX from runtime dependencies and installation documentation.

tHarvey303 and others added 2 commits July 5, 2026 00:27
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…iants

Adds src/synference/simformer/, a self-contained PyTorch port of the original
JAX Simformer (Gloeckler et al. 2024, scoresbibm/probjax), and rewires
Simformer_Fitter to use it. Replaces (a) the scoresbibm/JAX wrapper
(src/synference/simformer.py, deleted) and (b) SBI_Fitter.run_single_simformer,
which depended on the unmerged sbi PR #1621 (deleted). jax is removed from the
dependencies.

The new subpackage faithfully replicates the paper architecture and math:
per-variable tokens with frozen node-id embeddings and a mask-gated condition
token, pre-LN transformer with time context injected in every MLP block,
VE-SDE (default) and VP-SDE, masked denoising score matching with
structured-random condition-mask sampling, adaptive gradient clipping, and
early stopping. Inference supports arbitrary condition masks (posterior,
likelihood, joint, missing bands), base attention masks, interval-constrained
sampling via Tweedie guidance, and log probabilities via the probability-flow
ODE. Models serialize to a plain torch payload and reload in a fresh process
(SimformerModel.save/load).

API notes (clean break):
- sample_posterior(condition_mask=...) replaces the misnamed attention_mask
  argument (it was always a condition mask); full sentinel unchanged.
- log_prob(X_test, theta, ...) takes theta as the second positional argument,
  fixing evaluate_model silently passing y_test as a mask.
- Config overrides are validated against known keys instead of being silently
  swallowed.
- Old JAX-era model pickles cannot be loaded by the new implementation.
- fit_catalogue's Simformer branch now transposes samples like the regular
  branch, fixing wrong quantiles for multi-row catalogues.

Validation: analytic 2D-Gaussian conditional gate, TwoMoons (bimodal posterior,
TARP calibration, likelihood/joint conditionals, interval guidance, log-prob),
and the galaxy test library end-to-end (training, artifacts, reload in a fresh
process, fit_catalogue) in tests/test_simformer.py; full suite passes. A demo
lives in examples/simformer/scripts/two_moons_demo.py and the docs notebook
was rewritten for the new API.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 25, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR replaces the legacy JAX Simformer integration with a native PyTorch implementation. It adds transformer and diffusion components, training, conditional sampling, interval guidance, log probabilities, serialization, SBI integration, examples, documentation, dependency updates, and tests.

Changes

Native Simformer foundations

Layer / File(s) Summary
Model foundations and public API
src/synference/simformer/*, src/synference/__init__.py, tests/test_simformer.py
Adds transformer components, SDE implementations, condition and attention masks, package exports, and architecture, SDE, and mask tests.
Training and inference runtime
src/synference/simformer/*, tests/test_simformer.py
Adds training, posterior and likelihood sampling, interval guidance, probability-flow log probabilities, model serialization, uniform-box priors, and runtime tests.
SBI fitter migration and persistence
src/synference/sbi_runner.py, examples/sbi/scripts/train_model.py, examples/simformer/scripts/fit_simformer_model.py, tests/test_simformer.py
Replaces the JAX Simformer path with native training and loading, updates diagnostics and catalogue fitting, changes condition-mask handling, and validates artifacts and reload behavior.
Examples, documentation, and dependency updates
CLAUDE.md, README.md, pyproject.toml, docs/source/..., examples/simformer/scripts/two_moons_demo.py
Documents the native workflow, removes JAX and the Simformer optional dependency group, and adds a two-moons demonstration.

Estimated code review effort: 5 (Critical) | ~120 minutes

Sequence Diagram(s)

sequenceDiagram
  participant TrainingData
  participant train_simformer
  participant SimformerModel
  participant Simformer_Fitter
  participant Sampling
  TrainingData->>train_simformer: theta and x training arrays
  train_simformer->>SimformerModel: trained score network and SDE
  Simformer_Fitter->>SimformerModel: save or load native model
  Simformer_Fitter->>Sampling: condition mask and observations
  Sampling->>Simformer_Fitter: posterior, interval, or log-probability results
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding a self-contained PyTorch implementation of the Simformer approach.
Docstring Coverage ✅ Passed Docstring coverage is 91.89% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/torch-simformer

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (4)
src/synference/sbi_runner.py (1)

8397-8402: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Prefer an exception over assert for this input contract.

sample_posterior is a public entry point and the column/mask check is user-facing validation; assert is removed under python -O, after which a mismatch surfaces deeper inside SimformerModel._prepare_x_o with a less obvious message.

♻️ Proposed change
-        assert X_test.shape[1] == mask.sum(), (
-            f"X_test has {X_test.shape[1]} columns but the condition mask expects "
-            f"{int(mask.sum())} observed values."
-        )
+        if X_test.shape[1] != int(mask.sum()):
+            raise ValueError(
+                f"X_test has {X_test.shape[1]} columns but the condition mask expects "
+                f"{int(mask.sum())} observed values."
+            )
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/sbi_runner.py` around lines 8397 - 8402, In sample_posterior,
replace the assert guarding the X_test column count against mask.sum() with
explicit user-facing validation that raises an appropriate exception containing
the existing mismatch details. Preserve the current shape normalization and
allow valid column/mask matches to proceed unchanged.
tests/test_simformer.py (1)

405-427: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider marking the training fixtures as slow.

Both module-scoped fixtures train real models (3000 steps on 10k samples, plus 600 steps on the galaxy library) and downstream tests draw thousands of samples with 100 integration steps. That makes pytest on the default selection substantially slower. A @pytest.mark.slow on these classes (with a CI opt-in) would keep the fast suite fast without losing coverage.

Also applies to: 533-561

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_simformer.py` around lines 405 - 427, Mark the model-training test
fixtures or their containing test classes, including the two-moons fixture
two_moons_model and the galaxy-library fixture covering the other referenced
range, with pytest.mark.slow so they are excluded from the default fast suite
while remaining available through CI’s slow-test opt-in.
src/synference/simformer/sampling.py (1)

179-203: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low value

Exact divergence cost scales with the number of latent nodes.

One backward pass per latent dimension per ODE step (num_steps × n_latent graph traversals) makes divergence="exact" expensive for galaxy models with many parameters, and it is the default in SimformerModel.log_prob and Simformer_Fitter.log_prob. Consider defaulting to Hutchinson above a dimension threshold, or documenting the cost at the public entry points.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/simformer/sampling.py` around lines 179 - 203, Address the
scalability of exact divergence in _divergence_exact by either switching to
Hutchinson divergence above a clearly defined latent-dimension threshold or
documenting the num_steps × n_latent backward-pass cost at the public
SimformerModel.log_prob and Simformer_Fitter.log_prob entry points. Preserve
exact divergence behavior below any threshold and keep the existing
log-probability API consistent.
src/synference/simformer/model.py (1)

538-578: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Harden SimformerModel.load to avoid unpickling arbitrary objects.

weights_only=False keeps full pickle execution available even though the saved payload is just model weights/config and numpy arrays. Use weights_only=True with explicit safe globals for the numpy dtypes/arrays needed by the format, or switch the saved format to an unsafe-free serialization; passing map_location to torch.load is also clearer.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/simformer/model.py` around lines 538 - 578, Harden
SimformerModel.load by replacing unsafe full-pickle loading with
weights_only=True and registering the NumPy dtype/array globals required by the
saved payload, while passing the method’s map_location argument directly to
torch.load. Preserve the existing format-version validation and reconstruction
flow.

Source: Linters/SAST tools

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/synference/simformer/masks.py`:
- Around line 57-69: Update random_condition_mask so the Beta probability is
derived from generator-controlled Gamma draws rather than beta_dist.sample(),
which uses the global RNG. Construct the Beta-equivalent ratio using alpha and
beta parameters, pass the supplied generator to the underlying draws, and
preserve the existing mask shape and _fix_all_true_rows behavior.

In `@src/synference/simformer/model.py`:
- Around line 152-160: The _as_condition_mask method currently returns CPU masks
that are later used to index device-local tensors. Move the validated mask to
self.device before returning it, and update sample_intervals so constraint_mask
and effective_condition conversions follow the same device placement; ensure
_init_x_T, _prepare_x_o, _extract_latents, and log_prob can index without
cross-device errors.

In `@src/synference/simformer/train.py`:
- Around line 374-394: Update the final checkpoint restoration condition around
best_state so the model loads the best validation checkpoint whenever best_state
is available, regardless of whether stats["early_stopped"] is true. Preserve the
existing guard that avoids loading when no validation checkpoint was recorded.
- Around line 277-281: Shuffle the input rows with a seeded permutation before
computing the validation split in train_simformer, so data_val is sampled
independently of caller ordering while preserving reproducibility. Apply the
same permuted tensor to both data_val and data_train, and retain the existing
validation repeat and empty-validation behavior.

---

Nitpick comments:
In `@src/synference/sbi_runner.py`:
- Around line 8397-8402: In sample_posterior, replace the assert guarding the
X_test column count against mask.sum() with explicit user-facing validation that
raises an appropriate exception containing the existing mismatch details.
Preserve the current shape normalization and allow valid column/mask matches to
proceed unchanged.

In `@src/synference/simformer/model.py`:
- Around line 538-578: Harden SimformerModel.load by replacing unsafe
full-pickle loading with weights_only=True and registering the NumPy dtype/array
globals required by the saved payload, while passing the method’s map_location
argument directly to torch.load. Preserve the existing format-version validation
and reconstruction flow.

In `@src/synference/simformer/sampling.py`:
- Around line 179-203: Address the scalability of exact divergence in
_divergence_exact by either switching to Hutchinson divergence above a clearly
defined latent-dimension threshold or documenting the num_steps × n_latent
backward-pass cost at the public SimformerModel.log_prob and
Simformer_Fitter.log_prob entry points. Preserve exact divergence behavior below
any threshold and keep the existing log-probability API consistent.

In `@tests/test_simformer.py`:
- Around line 405-427: Mark the model-training test fixtures or their containing
test classes, including the two-moons fixture two_moons_model and the
galaxy-library fixture covering the other referenced range, with
pytest.mark.slow so they are excluded from the default fast suite while
remaining available through CI’s slow-test opt-in.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 567b7286-faa7-4b66-8679-be3d1d2d15e8

📥 Commits

Reviewing files that changed from the base of the PR and between 0e24fd6 and 452ac97.

📒 Files selected for processing (18)
  • CLAUDE.md
  • README.md
  • docs/source/advanced_topics/simformer.ipynb
  • docs/source/getting_started/installation.rst
  • examples/sbi/scripts/train_model.py
  • examples/simformer/scripts/fit_simformer_model.py
  • examples/simformer/scripts/two_moons_demo.py
  • pyproject.toml
  • src/synference/__init__.py
  • src/synference/sbi_runner.py
  • src/synference/simformer/__init__.py
  • src/synference/simformer/masks.py
  • src/synference/simformer/model.py
  • src/synference/simformer/nn.py
  • src/synference/simformer/sampling.py
  • src/synference/simformer/sde.py
  • src/synference/simformer/train.py
  • tests/test_simformer.py
💤 Files with no reviewable changes (4)
  • pyproject.toml
  • README.md
  • docs/source/getting_started/installation.rst
  • examples/sbi/scripts/train_model.py

Comment on lines +57 to +69
def random_condition_mask(
num_samples: int,
theta_dim: int,
x_dim: int,
generator: Optional[torch.Generator] = None,
alpha: float = 1.0,
beta: float = 4.0,
) -> torch.Tensor:
"""Bernoulli masks with per-batch probability drawn from ``Beta(alpha, beta)``."""
beta_dist = torch.distributions.Beta(alpha, beta)
prob = beta_dist.sample()
mask = torch.rand(num_samples, theta_dim + x_dim, generator=generator) < prob
return _fix_all_true_rows(mask)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🌐 Web query:

Does torch.distributions.Beta.sample accept a generator argument?

💡 Result:

As of July 25, 2026, the torch.distributions.Beta.sample method does not natively accept a generator argument [1][2][3]. While some updates to the PyTorch torch.distributions library have been initiated to standardize generator support across various distributions [4][5], the sample and rsample methods for the Beta distribution currently maintain their standard signature, which typically accepts only sample_shape [1][2]. Users requiring control over the random number generation process for reproducibility generally rely on setting the global PyTorch manual seed (torch.manual_seed()) before calling the sample() method [6]. Recent development activity in the PyTorch repository (as of March 2026) indicates ongoing efforts to add a generator parameter to the base distribution classes [5], but users should verify the specific version of PyTorch they are using to determine if this has been implemented for the Beta class. To check your current environment's capabilities, you can inspect the signature of your distribution instance's sample method using Python's inspect module.

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Locate and inspect the relevant files/interfaces without executing repository code.
printf 'Tracked python files matching paths:\n'
git ls-files | rg '(^|/)masks\.py$|(^|/)train_simformer\.py$|(^|/)synference\.py$' || true

printf '\nRelevant definitions/usages:\n'
rg -n "def random_condition_mask|random_condition_mask\(|torch\.manual_seed|random_generatr|generator|Beta\\(" -S .

printf '\nTarget masked section:\n'
sed -n '1,120p' "$(git ls-files | rg '/masks\.py$' | head -n 1)"

printf '\nTrain caller references:\n'
for f in $(git ls-files | rg 'train_simformer\.py$|simformer.*\.py$'); do
  if rg -q "random_condition_mask" "$f"; then
    echo "--- $f"
    rg -n -C 5 "random_condition_mask|torch\.manual_seed|random_generatr|Generator" "$f"
  fi
done

Repository: synthesizer-project/synference

Length of output: 24253


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the Simformer training path around the generator seed and mask call.
sed -n '235,330p' src/synference/simformer/train.py

printf '\nConfiguration/name references for random condition mask:\n'
rg -n --hidden -S '"random"|random_condition_mask|condition_mask.*random|condition_mask_fn|random"%' . || true

Repository: synthesizer-project/synference

Length of output: 7343


Generate the Beta probability with the passed RNG.

random_condition_mask re-seeds generator inside the call, but torch.distributions.Beta(...).sample() does not accept a generator argument, so its global RNG still determines prob. Use a seeded Beta proxy, e.g. Gamma draws, to keep this sampler fully reproducible.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/simformer/masks.py` around lines 57 - 69, Update
random_condition_mask so the Beta probability is derived from
generator-controlled Gamma draws rather than beta_dist.sample(), which uses the
global RNG. Construct the Beta-equivalent ratio using alpha and beta parameters,
pass the supplied generator to the underlying draws, and preserve the existing
mask shape and _fix_all_true_rows behavior.

Comment thread src/synference/simformer/model.py Resolved
Comment thread src/synference/simformer/train.py Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/synference/simformer/train.py`:
- Around line 277-283: Validate the input dataset and validation_fraction before
computing n_val in the training setup: reject zero-row data and require 0 <=
train_config["validation_fraction"] < 1. Raise a clear configuration/value error
before the permutation and split logic so data_train always has at least one row
and the existing training flow remains unchanged.
- Around line 279-280: Remove the trailing whitespace from the lines assigning
perm and reordering data in the training flow, then rerun Ruff, the formatter,
and pre-commit checks to confirm the W291 violations are resolved.
- Around line 279-280: Update the torch.randperm call in the data permutation
flow to explicitly create the permutation tensor on the CPU, matching the
CPU-based generator and ensuring the subsequent .numpy() conversion remains
valid.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 7e626086-6a7f-4422-99d9-e0da3c9859c7

📥 Commits

Reviewing files that changed from the base of the PR and between 452ac97 and ff0ad12.

📒 Files selected for processing (3)
  • src/synference/simformer.py
  • src/synference/simformer/model.py
  • src/synference/simformer/train.py
💤 Files with no reviewable changes (1)
  • src/synference/simformer.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/synference/simformer/model.py

Comment on lines +277 to +283
# Validation split (first fraction of rows, as in the original implementation).
n_val = max(int(train_config["validation_fraction"] * data.shape[0]), 0)
perm = torch.randperm(data.shape[0], generator=generator).numpy()
data = data[perm]
data_t = torch.as_tensor(data, device=device)
data_val = data_t[:n_val].repeat(train_config["val_repeat"], 1) if n_val > 0 else None
data_train = data_t[n_val:]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Reject configurations that leave no training rows.

If validation_fraction >= 1 or the input has zero rows, data_train is empty. Line 345 then calls torch.randint with high=0 and training fails before the first step. Validate a non-empty dataset and 0 <= validation_fraction < 1 before computing n_val.

Proposed fix
     theta_dim, x_dim = theta.shape[1], x.shape[1]
     num_nodes = theta_dim + x_dim
+    if data.shape[0] == 0:
+        raise ValueError("theta and x must contain at least one row")
+
+    validation_fraction = float(train_config["validation_fraction"])
+    if not 0.0 <= validation_fraction < 1.0:
+        raise ValueError("validation_fraction must be in [0, 1)")
+
     data = np.hstack([theta, x])
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/simformer/train.py` around lines 277 - 283, Validate the input
dataset and validation_fraction before computing n_val in the training setup:
reject zero-row data and require 0 <= train_config["validation_fraction"] < 1.
Raise a clear configuration/value error before the permutation and split logic
so data_train always has at least one row and the existing training flow remains
unchanged.

Comment on lines +279 to +280
perm = torch.randperm(data.shape[0], generator=generator).numpy()
data = data[perm]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

ruff check src/synference/simformer/train.py
ruff format --check src/synference/simformer/train.py
pre-commit run --all-files

Repository: synthesizer-project/synference

Length of output: 2663


Remove trailing whitespace from lines 279–280 and rerun Ruff, formatting, and pre-commit checks. Ruff reports two W291 errors.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/simformer/train.py` around lines 279 - 280, Remove the
trailing whitespace from the lines assigning perm and reordering data in the
training flow, then rerun Ruff, the formatter, and pre-commit checks to confirm
the W291 violations are resolved.

Source: Coding guidelines


🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- train.py relevant sections ---'
sed -n '240,315p' src/synference/simformer/train.py
printf '%s\n' '--- randperm and default-device usage ---'
rg -n -C 3 'randperm|set_default_device|default_device|generator' src/synference/simformer src || true
printf '%s\n' '--- installed PyTorch behavior probe ---'
python3 - <<'PY'
try:
    import torch
except Exception as exc:
    print(f"torch import unavailable: {exc}")
else:
    print(f"torch version: {torch.__version__}")
    print(f"has set_default_device: {hasattr(torch, 'set_default_device')}")
    if hasattr(torch, 'set_default_device'):
        torch.set_default_device("cpu")
        generator = torch.Generator(device="cpu").manual_seed(1)
        a = torch.randperm(4, generator=generator)
        print(f"cpu default: device={a.device}, numpy_ok={a.numpy().shape == (4,)}")
        if torch.cuda.is_available():
            torch.set_default_device("cuda")
            try:
                b = torch.randperm(4, generator=generator)
                print(f"cuda default: device={b.device}, numpy_ok={b.numpy().shape == (4,)}")
            except Exception as exc:
                print(f"cuda default error: {type(exc).__name__}: {exc}")
        else:
            print("CUDA unavailable; default-device CUDA case not executable here")
PY

Repository: synthesizer-project/synference

Length of output: 50386


🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- prior probe tail ---'
tail -n 40 /tmp/coderabbit-shell-logs/shell-output-lYELPb || true

printf '%s\n' '--- focused randperm probe ---'
python3 - <<'PY'
try:
    import torch
except Exception as exc:
    print(f"torch import unavailable: {exc}")
    raise SystemExit(0)

print(f"torch version: {torch.__version__}")
print(f"has set_default_device: {hasattr(torch, 'set_default_device')}")
g = torch.Generator(device="cpu").manual_seed(123)

for default in ("cpu", "cuda"):
    if default == "cuda" and not torch.cuda.is_available():
        print("cuda default: skipped because CUDA is unavailable")
        continue
    if hasattr(torch, "set_default_device"):
        torch.set_default_device(default)
    try:
        p = torch.randperm(4, generator=g)
        print(f"{default} default: result_device={p.device}")
        try:
            print(f"{default} default: numpy_result={p.numpy().tolist()}")
        except Exception as exc:
            print(f"{default} default: numpy_error={type(exc).__name__}: {exc}")
    except Exception as exc:
        print(f"{default} default: randperm_error={type(exc).__name__}: {exc}")

if hasattr(torch, "set_default_device"):
    torch.set_default_device("cpu")
g = torch.Generator(device="cpu").manual_seed(123)
p = torch.randperm(4, device="cpu", generator=g)
print(f"explicit cpu: result_device={p.device}, numpy_result={p.numpy().tolist()}")
PY

Repository: synthesizer-project/synference

Length of output: 3003


🌐 Web query:

PyTorch torch.randperm device=None default device torch.set_default_device generator device CPU CUDA numpy documentation

💡 Result:

In PyTorch, the device for torch.randperm is determined by the device parameter, which defaults to None [1][2]. When device is None, the function uses the current default device, which can be configured globally using torch.set_default_device(device) [1][3]. If no default device has been set using torch.set_default_device, the default is 'cpu' [4][5]. However, if torch.set_default_device has been used to set a global device (such as 'cuda'), torch.randperm will allocate the resulting tensor on that device by default [1][4]. Specifically, for 'cuda' types, it will target the current active CUDA device [1]. Regarding the interaction between the generator and the device: 1. Generator Device Matching: PyTorch requires that the device of the provided generator matches the target device of the operation [6][7]. If you specify a device (e.g., device='cuda') but provide a generator on a different device (e.g., a CPU generator), PyTorch will raise a RuntimeError [6][7]. 2. Implicit Behavior: When no generator is provided, PyTorch handles the generation internally using the appropriate device-specific generator [6]. 3. Numpy: There is no direct integration between numpy random number generators and torch.randperm; torch.randperm uses the PyTorch Generator class [1][8]. In summary, to ensure successful execution when specifying a device, you should ensure that if a generator is provided, its device is compatible with the target device [7]. Using torch.set_default_device is the primary way to influence the default allocation without explicitly passing the device argument to every call [4][5].

Citations:


Pin torch.randperm to the CPU.

generator is CPU-based, and .numpy() requires a CPU tensor. Set device="cpu" explicitly to avoid failures when PyTorch’s default device is CUDA.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/synference/simformer/train.py` around lines 279 - 280, Update the
torch.randperm call in the data permutation flow to explicitly create the
permutation tensor on the CPU, matching the CPU-based generator and ensuring the
subsequent .numpy() conversion remains valid.

Source: MCP tools

This branch has not been deployed

No deployments
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.

1 participant