Skip to content

[MRG] Support RandomState instances in NumpyBackend.seed - #849

Open
CunjieLee wants to merge 2 commits into
PythonOT:masterfrom
CunjieLee:fix/numpy-backend-randomstate
Open

[MRG] Support RandomState instances in NumpyBackend.seed#849
CunjieLee wants to merge 2 commits into
PythonOT:masterfrom
CunjieLee:fix/numpy-backend-randomstate

Conversation

@CunjieLee

@CunjieLee CunjieLee commented Sep 7, 2026

Copy link
Copy Markdown

Types of changes

  • Bug fix
  • Tests
  • Code cleanup
  • New feature
  • Documentation update

Motivation and context / Related issue

Closes #848.

NumpyBackend.seed() currently forwards every non-None value to
RandomState.seed(). Passing an existing np.random.RandomState
instance therefore raises:

TypeError: Cannot cast scalar from dtype('O') to dtype('int64')
according to the rule 'safe'

This forces callers that support both integer seeds and existing
RandomState instances to special-case the NumPy backend.

This change makes NumpyBackend adopt an externally supplied
RandomState, matching the generator-handling behavior of the other
backends. The NumPy-specific RandomState branches in the sliced
sampling utilities are removed accordingly.

The fourth occurrence mentioned in #848, in get_projections_spiral,
belongs to the still-open PR #838 and is not present on the current
master branch. That occurrence can use the unified backend path once
this fix is incorporated into #838.

How has this been tested (if it applies)

The issue was reproduced on the unmodified master branch using:

import numpy as np
from ot.backend import NumpyBackend

nx = NumpyBackend()
rng = np.random.RandomState(42)
nx.seed(rng)

The original code raises the reported TypeError.

The same code succeeds after the fix. Additional verification confirms
that:

  • NumpyBackend.rng_ is the exact supplied RandomState instance;
  • subsequent nx.randn(...) calls use that generator's sequence;
  • integer seeding retains its previous deterministic behavior.

A regression test was added in test/test_backend.py.

The following checks pass:

pre-commit run --all-files
pytest --durations=20 -v test/ --doctest-modules

The full test run completed with 1456 passed, 96 skipped, and 4 xfailed.
The skipped tests require optional dependencies that are not installed in
the local test environment.

PR checklist

  • I have read the CONTRIBUTING document.
  • The documentation is up-to-date with the changes I made (no public API documentation changes are required).
  • All tests passed, and additional code has been covered with new tests.
  • I have added the PR and Issue fix to the RELEASES.md file.

@CunjieLee CunjieLee changed the title [WIP] Support RandomState instances in NumpyBackend.seed [MRG] Support RandomState instances in NumpyBackend.seed Sep 7, 2026
@CunjieLee
CunjieLee marked this pull request as ready for review September 7, 2026 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NumpyBackend.seed() rejects a np.random.RandomState instance, forcing callers to special-case it

1 participant