提交 312dbc6f authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Merge pull request #2206 from nouiz/adv_incsub_serialize

Optimize AdvancedIncSubtensor[1].
......@@ -27,7 +27,8 @@ from theano.configparser import config
from theano.tensor.elemwise import Elemwise, DimShuffle
from theano.tensor.subtensor import (get_idx_list, get_canonical_form_slice,
Subtensor, IncSubtensor, make_constant,
AdvancedIncSubtensor1)
AdvancedIncSubtensor1,
AdvancedIncSubtensor)
from theano import scalar
from theano.tensor import basic as T
from theano import compile # to register the optimizer built by this file
......@@ -2283,7 +2284,10 @@ def local_IncSubtensor_serialize(node):
def movable(i):
# Return True iff this is a incsubtensor that we can move
return i.owner \
and isinstance(i.owner.op, IncSubtensor) \
and isinstance(i.owner.op, (IncSubtensor,
AdvancedIncSubtensor1,
AdvancedIncSubtensor,
)) \
and i.type == o_type \
and len(i.clients) == 1 \
and not i.owner.op.set_instead_of_inc
......@@ -2355,10 +2359,11 @@ compile.optdb.register('local_inplace_incsubtensor1',
60, 'fast_run', 'inplace') # DEBUG
@register_canonicalize
@register_stabilize
# Register old name
@register_canonicalize("local_incsubtensor_of_allocs")
@register_stabilize("local_incsubtensor_of_allocs")
@gof.local_optimizer([IncSubtensor])
def local_incsubtensor_of_allocs(node):
def local_incsubtensor_of_zeros(node):
"""
IncSubtensor(x, zeros, idx) -> x
"""
......@@ -2378,10 +2383,10 @@ def local_incsubtensor_of_allocs(node):
return False
@register_canonicalize
@register_stabilize
@register_canonicalize('local_setsubtensor_of_allocs')
@register_stabilize('local_setsubtensor_of_allocs')
@gof.local_optimizer([IncSubtensor])
def local_setsubtensor_of_allocs(node):
def local_setsubtensor_of_constants(node):
"""
SetSubtensor(x, x[idx], idx) -> x
......
......@@ -2372,8 +2372,8 @@ class test_local_subtensor_merge(unittest.TestCase):
class Test_alloc_zero(unittest.TestCase):
def setUp(self):
mode = theano.compile.mode.get_default_mode()
self.mode = mode.including("local_incsubtensor_of_allocs",
"local_setsubtensor_of_allocs",
self.mode = mode.including("local_incsubtensor_of_zeros",
"local_setsubtensor_of_constants",
"local_0_dot_x")
def test_setsubtensor_allocs0(self):
......@@ -2477,6 +2477,34 @@ class Test_alloc_zero(unittest.TestCase):
_e1[2], _e2[1])
def test_local_IncSubtensor_serialize():
d = numpy.random.normal(0, 0.01, size=(100, 100))
d = d.astype(theano.config.floatX)
W = theano.shared(d, name='W')
i = T.vector('i', dtype='int64')
j = T.vector('j', dtype='int64')
t = T.scalar('t')
if theano.tensor.subtensor.inplace_increment:
y = (W[i] + W[j] + W[1] + W[i, j]).sum()
else:
y = (W[i] + W[j] + W[1]).sum()
cost = T.sqr(t - y)
dW = theano.grad(cost, W)
mode = theano.compile.mode.get_default_mode().excluding('fusion')
f = theano.function([i, j, t], updates=[(W, W - 0.01 * dW)], mode=mode)
topo = f.maker.fgraph.toposort()
adds = [n for n in topo if isinstance(n.op, T.Elemwise) and
isinstance(n.op.scalar_op, theano.scalar.Add)]
for a in adds:
assert not any([inp.owner and
isinstance(inp.owner.op,
(tensor.IncSubtensor,
tensor.AdvancedIncSubtensor,
tensor.AdvancedIncSubtensor1))
for inp in a.inputs])
def test_local_subtensor_of_dot():
m1 = theano.tensor.matrix()
m2 = theano.tensor.matrix()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论