提交 fb20e58a authored 作者: Brendan Murphy's avatar Brendan Murphy 提交者: Ricardo Vieira

Changed copy to deepcopy for rng

This was done for the python linker and numba linker. deepcopy seems to be the recommended method for copying a numpy Generator. After this numpy PR: https://github.com/numpy/numpy/pull/26293/commits/44ba7ca07984557f2006f9a6916adb8e3ecfca61 `copy` didn't seem to actually make an independent copy of the `np.random.Generator` objects spawned by `RandomStream`. This was causing the "test values" computed by e.g. `RandomStream.uniform` to increment the RNG state, which was causing tests that rely on `RandomStream` to fail. Here is some related discussion: https://github.com/numpy/numpy/issues/24086 I didn't see any official documentation about a change in numpy that would make copy stop working.
上级 48894fae
from collections.abc import Callable from collections.abc import Callable
from copy import copy from copy import copy, deepcopy
from functools import singledispatch from functools import singledispatch
from textwrap import dedent from textwrap import dedent
...@@ -34,7 +34,7 @@ def copy_NumPyRandomGenerator(rng): ...@@ -34,7 +34,7 @@ def copy_NumPyRandomGenerator(rng):
def impl(rng): def impl(rng):
# TODO: Open issue on Numba? # TODO: Open issue on Numba?
with numba.objmode(new_rng=types.npy_rng): with numba.objmode(new_rng=types.npy_rng):
new_rng = copy(rng) new_rng = deepcopy(rng)
return new_rng return new_rng
......
import warnings import warnings
from collections.abc import Sequence from collections.abc import Sequence
from copy import copy from copy import deepcopy
from typing import Any, cast from typing import Any, cast
import numpy as np import numpy as np
...@@ -395,7 +395,7 @@ class RandomVariable(Op): ...@@ -395,7 +395,7 @@ class RandomVariable(Op):
# Draw from `rng` if `self.inplace` is `True`, and from a copy of `rng` otherwise. # Draw from `rng` if `self.inplace` is `True`, and from a copy of `rng` otherwise.
if not self.inplace: if not self.inplace:
rng = copy(rng) rng = deepcopy(rng)
outputs[0][0] = rng outputs[0][0] = rng
outputs[1][0] = np.asarray( outputs[1][0] = np.asarray(
......
import pickle import pickle
import re import re
from copy import copy from copy import deepcopy
import numpy as np import numpy as np
import pytest import pytest
...@@ -114,7 +114,9 @@ def compare_sample_values(rv, *params, rng=None, test_fn=None, **kwargs): ...@@ -114,7 +114,9 @@ def compare_sample_values(rv, *params, rng=None, test_fn=None, **kwargs):
pt_rng = shared(rng, borrow=True) pt_rng = shared(rng, borrow=True)
numpy_res = np.asarray(test_fn(*param_vals, random_state=copy(rng), **kwargs_vals)) numpy_res = np.asarray(
test_fn(*param_vals, random_state=deepcopy(rng), **kwargs_vals)
)
pytensor_res = rv(*params, rng=pt_rng, **kwargs) pytensor_res = rv(*params, rng=pt_rng, **kwargs)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论