提交 ae9e4693 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Make copy(WrapLinker) copy the wrapped linkers

It avoids adding an fgraph to the default linker. Add test.
上级 101be6f8
"""
Test compilation modes
"""
import copy
import unittest
import theano
......@@ -25,7 +26,6 @@ class T_bunch_of_modes(unittest.TestCase):
modes = predef_modes + [Mode(linker, 'fast_run') for linker in linkers]
for mode in modes:
x = T.matrix()
y = T.vector()
f = theano.function([x, y], x + y, mode=mode)
......@@ -33,6 +33,7 @@ class T_bunch_of_modes(unittest.TestCase):
f([[1, 2], [3, 4]], [5, 6])
linker_classes_involved.append(f.maker.mode.linker.__class__)
#print 'MODE:', mode, f.maker.mode.linker, 'stop'
# regression check:
# there should be
# - VM_Linker
......@@ -42,5 +43,26 @@ class T_bunch_of_modes(unittest.TestCase):
# - DebugMode's Linker (DEBUG_MODE)
assert 5 == len(set(linker_classes_involved))
class T_ProfileMode_WrapLinker(unittest.TestCase):
def test_1(self):
# First, compile a function with a new ProfileMode() object
# No need to call that function
x = T.matrix()
mode = ProfileMode()
theano.function([x], x * 2, mode=mode)
# Then, build a mode with the same linker, and a modified optimizer
default_mode = theano.compile.mode.get_default_mode()
modified_mode = default_mode.including('specialize')
# The following line used to fail, with Python 2.4, in July 2012,
# because an fgraph was associated to the default linker
copy.deepcopy(modified_mode)
# More straightforward test
assert theano.compile.mode.get_default_mode().linker.fgraph is None
if __name__ == '__main__':
unittest.main()
......@@ -553,6 +553,22 @@ class WrapLinker(Linker):
self.linkers = linkers
self.wrapper = wrapper
def __copy__(self):
"""
Shallow copy of a WrapLinker.
@returns: A copy of self, where each of the linkers in self.linkers
have been shallow-copied.
It is useful because in FunctionMaker, copy.copy is called on the
Mode's linker, so that it is not modified inplace when linker.accept()
is called. In this case, we want the wrapped linkers to be copied too.
"""
other = self.__class__(
linkers=[copy(l) for l in self.linkers],
wrapper=self.wrapper)
return other
def accept(self, fgraph, no_recycling=None):
"""
@type fgraph: gof.FunctionGraph
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论