提交 45a84389 authored 作者: abergeron's avatar abergeron

Merge pull request #2746 from nouiz/shared

fix gh-855.
...@@ -2,17 +2,12 @@ import gc ...@@ -2,17 +2,12 @@ import gc
import sys import sys
import time import time
import unittest import unittest
try:
import line_profiler
except ImportError:
pass
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
import numpy import numpy
from theano import function from theano import function
from theano.gof import vm from theano.gof import vm
from theano.gof import link
from theano.gof import OpWiseCLinker from theano.gof import OpWiseCLinker
from theano.compile import Mode from theano.compile import Mode
...@@ -43,7 +38,8 @@ class TestCallbacks(unittest.TestCase): ...@@ -43,7 +38,8 @@ class TestCallbacks(unittest.TestCase):
f(1, 2, 3) f(1, 2, 3)
assert sum(self.n_callbacks.values()) == len(f.maker.fgraph.toposort()) assert sum(self.n_callbacks.values()) == len(f.maker.fgraph.toposort())
f(1, 2, 3) f(1, 2, 3)
assert sum(self.n_callbacks.values()) == len(f.maker.fgraph.toposort()) * 2 assert (sum(self.n_callbacks.values()) ==
len(f.maker.fgraph.toposort()) * 2)
def test_callback_with_ifelse(self): def test_callback_with_ifelse(self):
a, b, c = tensor.scalars('abc') a, b, c = tensor.scalars('abc')
...@@ -89,7 +85,7 @@ def test_speed(): ...@@ -89,7 +85,7 @@ def test_speed():
print "%s takes %f s/Kop" % ( print "%s takes %f s/Kop" % (
'numpy', 'numpy',
(1000*(t_b-t_a) / (steps_b - steps_a))) (1000 * (t_b - t_a) / (steps_b - steps_a)))
def time_linker(name, linker): def time_linker(name, linker):
steps_a = 5 steps_a = 5
...@@ -99,13 +95,9 @@ def test_speed(): ...@@ -99,13 +95,9 @@ def test_speed():
b = build_graph(x, steps_b) b = build_graph(x, steps_b)
f_a = function([x], a, f_a = function([x], a,
mode=Mode(optimizer=None, linker=linker()), mode=Mode(optimizer=None, linker=linker()))
#profile='f_a speed test %s'%name,
)
f_b = function([x], b, f_b = function([x], b,
mode=Mode(optimizer=None, linker=linker()), mode=Mode(optimizer=None, linker=linker()))
#profile='f_b speed test %s'%name,
)
f_a([2.0, 3.0]) f_a([2.0, 3.0])
t0 = time.time() t0 = time.time()
...@@ -151,14 +143,10 @@ def test_speed_lazy(): ...@@ -151,14 +143,10 @@ def test_speed_lazy():
f_a = function([x], a, f_a = function([x], a,
mode=Mode(optimizer=None, mode=Mode(optimizer=None,
linker=linker()), linker=linker()))
#profile='f_a lazy ifelse %s'%name,
)
f_b = function([x], b, f_b = function([x], b,
mode=Mode(optimizer=None, mode=Mode(optimizer=None,
linker=linker()), linker=linker()))
#profile='f_b lazy ifelse %s'%name,
)
f_a([2.0]) f_a([2.0])
t0 = time.time() t0 = time.time()
...@@ -220,6 +208,7 @@ if run_memory_usage_tests: ...@@ -220,6 +208,7 @@ if run_memory_usage_tests:
c = sys.getrefcount(n) c = sys.getrefcount(n)
a = cuda.CudaNdarray(n) a = cuda.CudaNdarray(n)
assert c == sys.getrefcount(n) assert c == sys.getrefcount(n)
del a
if not i % 1000: if not i % 1000:
print '.', print '.',
print gc.collect(), print gc.collect(),
...@@ -258,20 +247,20 @@ if run_memory_usage_tests: ...@@ -258,20 +247,20 @@ if run_memory_usage_tests:
def build_graph(x, depth=5): def build_graph(x, depth=5):
z = x z = x
for d in range(depth): for d in range(depth):
z = ifelse(z > 0, -z, z) z = ifelse(z.mean() > 0.5, -z, z)
return z return z
def time_linker(name, linker): def time_linker(name, linker):
steps_a = 10 steps_a = 10
x = tensor.vector() x = tensor.dvector()
a = build_graph(x, steps_a) a = build_graph(x, steps_a)
f_a = function([x], a, f_a = function([x], a,
mode=Mode(optimizer=None, mode=Mode(optimizer=None,
linker=linker())) linker=linker()))
inp = numpy.random.rand(1000000)
for i in xrange(100000): for i in xrange(100):
f_a([2.0]) f_a(inp)
if 0: # this doesn't seem to work, prints 0 for everything if 0: # this doesn't seem to work, prints 0 for everything
import resource import resource
pre = resource.getrusage(resource.RUSAGE_SELF) pre = resource.getrusage(resource.RUSAGE_SELF)
...@@ -279,9 +268,12 @@ if run_memory_usage_tests: ...@@ -279,9 +268,12 @@ if run_memory_usage_tests:
print pre.ru_ixrss, post.ru_ixrss print pre.ru_ixrss, post.ru_ixrss
print pre.ru_idrss, post.ru_idrss print pre.ru_idrss, post.ru_idrss
print pre.ru_maxrss, post.ru_maxrss print pre.ru_maxrss, post.ru_maxrss
print 1
time_linker('vmLinker_C', time_linker('vmLinker_C',
lambda: vm.VM_Linker(allow_gc=False, use_cloop=True)) lambda: vm.VM_Linker(allow_gc=False, use_cloop=True))
print 2
time_linker('vmLinker',
lambda: vm.VM_Linker(allow_gc=False, use_cloop=False))
def test_no_leak_many_call_nonlazy(): def test_no_leak_many_call_nonlazy():
# Verify no memory leaks when calling a function a lot of times # Verify no memory leaks when calling a function a lot of times
...@@ -297,18 +289,21 @@ if run_memory_usage_tests: ...@@ -297,18 +289,21 @@ if run_memory_usage_tests:
def time_linker(name, linker): def time_linker(name, linker):
steps_a = 10 steps_a = 10
x = tensor.vector() x = tensor.dvector()
a = build_graph(x, steps_a) a = build_graph(x, steps_a)
f_a = function([x], a, f_a = function([x], a,
mode=Mode(optimizer=None, mode=Mode(optimizer=None,
linker=linker())) linker=linker()))
inp = numpy.random.rand(1000000)
for i in xrange(500000): for i in xrange(500):
f_a([2.0]) f_a(inp)
print 1
time_linker('vmLinker_C', time_linker('vmLinker_C',
lambda: vm.VM_Linker(allow_gc=False, use_cloop=True)) lambda: vm.VM_Linker(allow_gc=False, use_cloop=True))
print 2
time_linker('vmLinker',
lambda: vm.VM_Linker(allow_gc=False, use_cloop=False))
class RunOnce(theano.Op): class RunOnce(theano.Op):
......
...@@ -878,7 +878,7 @@ def test_gpujoin_assert_cndas(): ...@@ -878,7 +878,7 @@ def test_gpujoin_assert_cndas():
c = cuda.basic_ops.gpu_join(1, a) c = cuda.basic_ops.gpu_join(1, a)
# can't "assert False" here, as we want the assertion # can't "assert False" here, as we want the assertion
# error from gpu_join # error from gpu_join
except AssertionError: except TypeError:
assert True assert True
return return
......
...@@ -42,6 +42,7 @@ from ..basic_ops import ( ...@@ -42,6 +42,7 @@ from ..basic_ops import (
gpu_alloc, GpuAlloc, gpu_alloc, GpuAlloc,
gpu_from_cuda, gpu_from_cuda,
cuda_from_gpu, HostFromGpu, cuda_from_gpu, HostFromGpu,
GpuContiguous,
GpuFromHost, GpuReshape, GpuFromHost, GpuReshape,
gpu_join, GpuJoin, GpuSplit, GpuEye, gpu_contiguous) gpu_join, GpuJoin, GpuSplit, GpuEye, gpu_contiguous)
from ..subtensor import GpuSubtensor from ..subtensor import GpuSubtensor
...@@ -332,10 +333,13 @@ def test_gpu_contiguous(): ...@@ -332,10 +333,13 @@ def test_gpu_contiguous():
a = T.fmatrix('a') a = T.fmatrix('a')
i = T.iscalar('i') i = T.iscalar('i')
a_val = numpy.asarray(numpy.random.rand(4, 5), dtype='float32') a_val = numpy.asarray(numpy.random.rand(4, 5), dtype='float32')
f = theano.function([a, i], gpu_contiguous(a[::i]), # The reshape is needed otherwise we make the subtensor on the CPU
# to transfer less data.
f = theano.function([a, i], gpu_contiguous(a.reshape((5, 4))[::i]),
mode=mode_with_gpu) mode=mode_with_gpu)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert any([isinstance(node.op, GpuSubtensor) for node in topo]) assert any([isinstance(node.op, GpuSubtensor) for node in topo])
assert any([isinstance(node.op, GpuContiguous) for node in topo])
assert f(a_val, 1).flags.c_contiguous assert f(a_val, 1).flags.c_contiguous
assert f(a_val, 2).flags.c_contiguous assert f(a_val, 2).flags.c_contiguous
assert f(a_val, 2).flags.c_contiguous assert f(a_val, 2).flags.c_contiguous
......
...@@ -59,11 +59,16 @@ class ScalarSharedVariable(_tensor_py_operators, SharedVariable): ...@@ -59,11 +59,16 @@ class ScalarSharedVariable(_tensor_py_operators, SharedVariable):
@shared_constructor @shared_constructor
def scalar_constructor(value, name=None, strict=False, allow_downcast=None): def scalar_constructor(value, name=None, strict=False, allow_downcast=None,
borrow=False):
"""SharedVariable constructor for scalar values. Default: int64 or float64. """SharedVariable constructor for scalar values. Default: int64 or float64.
:note: We implement this using 0-d tensors for now. :note: We implement this using 0-d tensors for now.
:note: We ignore the borrow parameter as we convert ``value`` to an
ndarray (this is a new object). This respects the semantic of
borrow, as it is a hint to Theano that we can reuse it.
""" """
if not isinstance(value, (numpy.number, float, int, complex)): if not isinstance(value, (numpy.number, float, int, complex)):
raise TypeError() raise TypeError()
......
...@@ -61,7 +61,7 @@ class SortOp(theano.Op): ...@@ -61,7 +61,7 @@ class SortOp(theano.Op):
inp_grad = theano.gradient.grad_not_implemented( inp_grad = theano.gradient.grad_not_implemented(
self, 0, axis, self, 0, axis,
"Currently, we only implement the gradient on sort for vector" "Currently, we only implement the gradient on sort for vector"
" and matrix (and axis is None)") " and matrix (and axis is None or 0)")
if a.ndim == 1: if a.ndim == 1:
idx = argsort(*inputs, kind=self.kind, order=self.order) idx = argsort(*inputs, kind=self.kind, order=self.order)
# rev_idx = numpy.where(idx[None, :]==numpy.arange(5)[:,None])[1] # rev_idx = numpy.where(idx[None, :]==numpy.arange(5)[:,None])[1]
......
...@@ -617,3 +617,9 @@ test_shared_options = makeSharedTester( ...@@ -617,3 +617,9 @@ test_shared_options = makeSharedTester(
cast_value_=numpy.asarray, cast_value_=numpy.asarray,
op_by_matrix_=False, op_by_matrix_=False,
name='test_shared_options') name='test_shared_options')
def test_scalar_shared_options():
# Simple test to make sure we do not loose that fonctionality.
theano.shared(value=0., name='lk', borrow=True)
theano.shared(value=numpy.float32(0.), name='lk', borrow=True)
...@@ -320,7 +320,6 @@ whitelist_flake8 = [ ...@@ -320,7 +320,6 @@ whitelist_flake8 = [
"gof/tests/test_destroyhandler.py", "gof/tests/test_destroyhandler.py",
"gof/tests/test_opt.py", "gof/tests/test_opt.py",
"gof/tests/test_lazy.py", "gof/tests/test_lazy.py",
"gof/tests/test_vm.py",
"gof/tests/test_toolbox.py", "gof/tests/test_toolbox.py",
"gof/tests/test_link.py", "gof/tests/test_link.py",
"gof/tests/test_fg.py", "gof/tests/test_fg.py",
......
...@@ -68,11 +68,11 @@ class RopLop_checker(unittest.TestCase): ...@@ -68,11 +68,11 @@ class RopLop_checker(unittest.TestCase):
self.x = tensor.vector('x') self.x = tensor.vector('x')
self.v = tensor.vector('v') self.v = tensor.vector('v')
self.rng = numpy.random.RandomState(utt.fetch_seed()) self.rng = numpy.random.RandomState(utt.fetch_seed())
self.in_shape = (5 + self.rng.randint(30),) self.in_shape = (5 + self.rng.randint(3),)
self.mx = tensor.matrix('mx') self.mx = tensor.matrix('mx')
self.mv = tensor.matrix('mv') self.mv = tensor.matrix('mv')
self.mat_in_shape = (5 + self.rng.randint(30), self.mat_in_shape = (5 + self.rng.randint(3),
5 + self.rng.randint(30)) 5 + self.rng.randint(3))
def check_nondiff_rop(self, y): def check_nondiff_rop(self, y):
""" If your op is not differentiable(so you can't define Rop) """ If your op is not differentiable(so you can't define Rop)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论