提交 51d44d22 authored 作者: Brandon T. Willard's avatar Brandon T. Willard 提交者: Brandon T. Willard

Use copy.copy instead of assuming a .copy method during test value evaluation

上级 f375a0e9
...@@ -6,10 +6,16 @@ import pytest ...@@ -6,10 +6,16 @@ import pytest
import theano import theano
import theano.tensor as tt import theano.tensor as tt
from theano import config, scalar from theano import config, scalar
from theano.gof import Apply, Op, utils from theano.gof import Apply, Op, Type, utils
from theano.tensor.basic import _allclose from theano.tensor.basic import _allclose
@pytest.fixture(scope="module", autouse=True)
def set_theano_flags():
with theano.change_flags(compute_test_value="raise"):
yield
# Used in TestComputeTestValue.test_no_perform # Used in TestComputeTestValue.test_no_perform
class IncOneC(Op): class IncOneC(Op):
""" """
...@@ -33,7 +39,36 @@ class IncOneC(Op): ...@@ -33,7 +39,36 @@ class IncOneC(Op):
class TestComputeTestValue: class TestComputeTestValue:
@theano.change_flags(compute_test_value="raise") def test_destroy_map(self):
class SomeType(Type):
def filter(self, data, strict=False, allow_downcast=None):
return data
class InplaceOp(Op):
__props__ = ()
def __init__(self, inplace):
if inplace:
self.destroy_map = {0: [0]}
super().__init__()
def make_node(self, input):
return Apply(self, [input], [input.type()])
def perform(self, node, inputs, outputs):
outputs[0][0] = inputs[0]
test_input = SomeType()()
orig_object = object()
test_input.tag.test_value = orig_object
res = InplaceOp(False)(test_input)
assert res.tag.test_value is orig_object
res = InplaceOp(True)(test_input)
assert res.tag.test_value is not orig_object
def test_variable_only(self): def test_variable_only(self):
x = tt.matrix("x") x = tt.matrix("x")
x.tag.test_value = np.random.rand(3, 4).astype(config.floatX) x.tag.test_value = np.random.rand(3, 4).astype(config.floatX)
...@@ -51,7 +86,6 @@ class TestComputeTestValue: ...@@ -51,7 +86,6 @@ class TestComputeTestValue:
with pytest.raises(ValueError): with pytest.raises(ValueError):
tt.dot(x, y) tt.dot(x, y)
@theano.change_flags(compute_test_value="raise")
def test_compute_flag(self): def test_compute_flag(self):
x = tt.matrix("x") x = tt.matrix("x")
y = tt.matrix("y") y = tt.matrix("y")
...@@ -79,7 +113,6 @@ class TestComputeTestValue: ...@@ -79,7 +113,6 @@ class TestComputeTestValue:
# Theano drops support of Python 2.4 and 2.5. # Theano drops support of Python 2.4 and 2.5.
warnings.simplefilter("default", UserWarning) warnings.simplefilter("default", UserWarning)
@theano.change_flags(compute_test_value="raise")
def test_string_var(self): def test_string_var(self):
x = tt.matrix("x") x = tt.matrix("x")
x.tag.test_value = np.random.rand(3, 4).astype(config.floatX) x.tag.test_value = np.random.rand(3, 4).astype(config.floatX)
...@@ -102,7 +135,6 @@ class TestComputeTestValue: ...@@ -102,7 +135,6 @@ class TestComputeTestValue:
with pytest.raises(ValueError): with pytest.raises(ValueError):
f(x, y, z) f(x, y, z)
@theano.change_flags(compute_test_value="raise")
def test_shared(self): def test_shared(self):
x = tt.matrix("x") x = tt.matrix("x")
x.tag.test_value = np.random.rand(3, 4).astype(config.floatX) x.tag.test_value = np.random.rand(3, 4).astype(config.floatX)
...@@ -119,7 +151,6 @@ class TestComputeTestValue: ...@@ -119,7 +151,6 @@ class TestComputeTestValue:
with pytest.raises(ValueError): with pytest.raises(ValueError):
tt.dot(x, y) tt.dot(x, y)
@theano.change_flags(compute_test_value="raise")
def test_ndarray(self): def test_ndarray(self):
x = np.random.rand(2, 3).astype(config.floatX) x = np.random.rand(2, 3).astype(config.floatX)
y = theano.shared(np.random.rand(3, 6).astype(config.floatX), "y") y = theano.shared(np.random.rand(3, 6).astype(config.floatX), "y")
...@@ -135,7 +166,6 @@ class TestComputeTestValue: ...@@ -135,7 +166,6 @@ class TestComputeTestValue:
with pytest.raises(ValueError): with pytest.raises(ValueError):
tt.dot(x, y) tt.dot(x, y)
@theano.change_flags(compute_test_value="raise")
def test_empty_elemwise(self): def test_empty_elemwise(self):
x = theano.shared(np.random.rand(0, 6).astype(config.floatX), "x") x = theano.shared(np.random.rand(0, 6).astype(config.floatX), "x")
...@@ -145,7 +175,6 @@ class TestComputeTestValue: ...@@ -145,7 +175,6 @@ class TestComputeTestValue:
f = theano.function([], z) f = theano.function([], z)
assert _allclose(f(), z.tag.test_value) assert _allclose(f(), z.tag.test_value)
@theano.change_flags(compute_test_value="raise")
def test_constant(self): def test_constant(self):
x = tt.constant(np.random.rand(2, 3), dtype=config.floatX) x = tt.constant(np.random.rand(2, 3), dtype=config.floatX)
y = theano.shared(np.random.rand(3, 6).astype(config.floatX), "y") y = theano.shared(np.random.rand(3, 6).astype(config.floatX), "y")
...@@ -161,7 +190,6 @@ class TestComputeTestValue: ...@@ -161,7 +190,6 @@ class TestComputeTestValue:
with pytest.raises(ValueError): with pytest.raises(ValueError):
tt.dot(x, y) tt.dot(x, y)
@theano.change_flags(compute_test_value="raise")
def test_incorrect_type(self): def test_incorrect_type(self):
x = tt.vector("x") x = tt.vector("x")
...@@ -174,7 +202,6 @@ class TestComputeTestValue: ...@@ -174,7 +202,6 @@ class TestComputeTestValue:
# Incorrect dtype (float64) for test value # Incorrect dtype (float64) for test value
x.tag.test_value = np.random.rand(3, 4) x.tag.test_value = np.random.rand(3, 4)
@theano.change_flags(compute_test_value="raise")
def test_overided_function(self): def test_overided_function(self):
# We need to test those as they mess with Exception # We need to test those as they mess with Exception
# And we don't want the exception to be changed. # And we don't want the exception to be changed.
...@@ -185,7 +212,6 @@ class TestComputeTestValue: ...@@ -185,7 +212,6 @@ class TestComputeTestValue:
with pytest.raises(ValueError): with pytest.raises(ValueError):
x.__mul__(y) x.__mul__(y)
@theano.change_flags(compute_test_value="raise")
def test_scan(self): def test_scan(self):
# Test the compute_test_value mechanism Scan. # Test the compute_test_value mechanism Scan.
k = tt.iscalar("k") k = tt.iscalar("k")
...@@ -207,7 +233,6 @@ class TestComputeTestValue: ...@@ -207,7 +233,6 @@ class TestComputeTestValue:
final_result = result[-1] final_result = result[-1]
assert hasattr(final_result.tag, "test_value") assert hasattr(final_result.tag, "test_value")
@theano.change_flags(compute_test_value="raise")
def test_scan_err1(self): def test_scan_err1(self):
# This test should fail when building fx for the first time # This test should fail when building fx for the first time
k = tt.iscalar("k") k = tt.iscalar("k")
...@@ -225,7 +250,6 @@ class TestComputeTestValue: ...@@ -225,7 +250,6 @@ class TestComputeTestValue:
# We should be in the "fx" function defined above # We should be in the "fx" function defined above
assert e.traceback[2].name == "fx" assert e.traceback[2].name == "fx"
@theano.change_flags(compute_test_value="raise")
def test_scan_err2(self): def test_scan_err2(self):
# This test should not fail when building fx for the first time, # This test should not fail when building fx for the first time,
# but when calling the scan's perform() # but when calling the scan's perform()
...@@ -247,7 +271,6 @@ class TestComputeTestValue: ...@@ -247,7 +271,6 @@ class TestComputeTestValue:
fn=fx, outputs_info=tt.ones_like(A.T), non_sequences=A, n_steps=k fn=fx, outputs_info=tt.ones_like(A.T), non_sequences=A, n_steps=k
) )
@theano.change_flags(compute_test_value="raise")
def test_no_c_code(self): def test_no_c_code(self):
class IncOnePython(Op): class IncOnePython(Op):
""" """
...@@ -281,7 +304,6 @@ class TestComputeTestValue: ...@@ -281,7 +304,6 @@ class TestComputeTestValue:
@pytest.mark.skipif( @pytest.mark.skipif(
not theano.config.cxx, reason="G++ not available, so we need to skip this test." not theano.config.cxx, reason="G++ not available, so we need to skip this test."
) )
@theano.change_flags(compute_test_value="raise")
def test_no_perform(self): def test_no_perform(self):
i = scalar.int32("i") i = scalar.int32("i")
i.tag.test_value = 3 i.tag.test_value = 3
...@@ -297,7 +319,6 @@ class TestComputeTestValue: ...@@ -297,7 +319,6 @@ class TestComputeTestValue:
assert hasattr(o.tag, "test_value") assert hasattr(o.tag, "test_value")
assert o.tag.test_value == 4 assert o.tag.test_value == 4
@theano.change_flags(compute_test_value="raise")
def test_disabled_during_compilation(self): def test_disabled_during_compilation(self):
# We test that it is disabled when we include deep copy in the code # We test that it is disabled when we include deep copy in the code
# This don't test that it is disabled during optimization, but the code do it. # This don't test that it is disabled during optimization, but the code do it.
......
...@@ -5,6 +5,7 @@ The `Op` class is the base interface for all operations ...@@ -5,6 +5,7 @@ The `Op` class is the base interface for all operations
compatible with `gof`'s :doc:`graph` routines. compatible with `gof`'s :doc:`graph` routines.
""" """
import copy
import inspect import inspect
import logging import logging
import os import os
...@@ -98,7 +99,7 @@ def compute_test_value(node): ...@@ -98,7 +99,7 @@ def compute_test_value(node):
destroyed_inputs_idx.update(i_pos_list) destroyed_inputs_idx.update(i_pos_list)
for inp_idx in destroyed_inputs_idx: for inp_idx in destroyed_inputs_idx:
inp = node.inputs[inp_idx] inp = node.inputs[inp_idx]
storage_map[inp] = [storage_map[inp][0].copy()] storage_map[inp] = [copy.copy(storage_map[inp][0])]
# Prepare `storage_map` and `compute_map` for the outputs # Prepare `storage_map` and `compute_map` for the outputs
for o in node.outputs: for o in node.outputs:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论