Skip to content

Adopt a NumPy RandomState in NumpyBackend.seed - #881

Open
shaneraphel wants to merge 2 commits into
PythonOT:masterfrom
shaneraphel:feature/numpy-backend-random-state
Open

shaneraphel wants to merge 2 commits into
PythonOT:masterfrom
shaneraphel:feature/numpy-backend-random-state

Conversation

@shaneraphel

@shaneraphel shaneraphel commented Oct 2, 2026 •

Copy link
Copy Markdown

Types of changes

  • Bug fix
  • New feature
  • Breaking change

Motivation and context / Related issue

NumpyBackend.seed forwards its argument to RandomState.seed. That method accepts an integer, and it rejects a numpy.random.RandomState with TypeError. TorchBackend.seed already adopts a torch.Generator, and TensorflowBackend.seed already adopts a tf.random.Generator. Issue #848 asks for the same on the NumPy backend.

An integer still seeds the current generator. A RandomState replaces rng_ on that instance, so later rand, randn, and randperm draws come from the object the caller passed. None still leaves the generator unchanged. Adopting the object does not reseed the class-level generator shared by other instances.

How has this been tested (if it applies)

test_numpy_backend_adopts_random_state checks four things: a RandomState(42) produces the same first four uniform draws as a fresh RandomState(42); those draws do not advance the class-level generator; randn continues that generator; an integer seed is still repeatable; seed(None) does not change the state.

I ran that scenario by loading ot/backend.py directly. I did not build the package extension and I did not run the full test suite.

PR checklist

  • I have read the CONTRIBUTING document.
  • The documentation is up-to-date with the changes I made (check build artifacts).
  • 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.

RandomState.seed rejects another RandomState, so callers can pass the generator itself and later draws use it.
@codecov

codecov Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.87%. Comparing base (98d09a1) to head (3f9d060).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #881   +/-   ##
=======================================
  Coverage   96.86%   96.87%           
=======================================
  Files         128      128           
  Lines       26304    26330   +26     
=======================================
+ Hits        25480    25506   +26     
  Misses        824      824           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant