提交 705806f4 authored 作者: Frederic's avatar Frederic

Register to the gpu tag more opt, as we don't have all the other case on the GPU…

Register to the gpu tag more opt, as we don't have all the other case on the GPU otherwise in fast_compile.
上级 9de8a09d
"""Provides neural-network specific Ops. """Provides neural-network specific Ops.
:note: TODO: factor this out into a neural-network toolbox. :note: TODO: factor this out into a neural-network toolbox.
:note: We register all optimization with the gpu tag as we don't
implement all the intermediate case on the GPU (in particular
AdvancedSubtensor). So to make sure it run well on the gpu with
fast_compile, we register them as needed for the GPU. This can be
revisited later when all the intermediate part are on the GPU.
""" """
import logging import logging
import numpy import numpy
...@@ -570,7 +577,6 @@ class Softmax(gof.Op): ...@@ -570,7 +577,6 @@ class Softmax(gof.Op):
softmax = Softmax() softmax = Softmax()
@opt.register_specialize
@gof.local_optimizer([softmax]) @gof.local_optimizer([softmax])
def local_softmax_with_bias(node): def local_softmax_with_bias(node):
"""Try to turn softmax(sum_of_stuff) -> softmax_w_bias(matrix, bias) """Try to turn softmax(sum_of_stuff) -> softmax_w_bias(matrix, bias)
...@@ -629,6 +635,7 @@ def local_softmax_with_bias(node): ...@@ -629,6 +635,7 @@ def local_softmax_with_bias(node):
#This condition is not always true. See the test #This condition is not always true. See the test
#nnet/tests/test_nnet.py:T_SoftmaxWithBias.test_broadcast #nnet/tests/test_nnet.py:T_SoftmaxWithBias.test_broadcast
return [sm_bias] return [sm_bias]
opt.register_specialize(local_softmax_with_bias, 'gpu')
def softmax_simplifier(numerators, denominators): def softmax_simplifier(numerators, denominators):
...@@ -1323,8 +1330,6 @@ class CrossentropyCategorical1Hot(gof.Op): ...@@ -1323,8 +1330,6 @@ class CrossentropyCategorical1Hot(gof.Op):
crossentropy_categorical_1hot = CrossentropyCategorical1Hot() crossentropy_categorical_1hot = CrossentropyCategorical1Hot()
@opt.register_stabilize
@opt.register_specialize
@gof.optimizer @gof.optimizer
def crossentropy_to_crossentropy_with_softmax_with_bias(fgraph): def crossentropy_to_crossentropy_with_softmax_with_bias(fgraph):
"""This is a stabilization optimization """This is a stabilization optimization
...@@ -1352,6 +1357,10 @@ def crossentropy_to_crossentropy_with_softmax_with_bias(fgraph): ...@@ -1352,6 +1357,10 @@ def crossentropy_to_crossentropy_with_softmax_with_bias(fgraph):
while search_make_one_sub(): while search_make_one_sub():
pass pass
return return
opt.register_stabilize(crossentropy_to_crossentropy_with_softmax_with_bias,
'gpu')
opt.register_specialize(crossentropy_to_crossentropy_with_softmax_with_bias,
'gpu')
@gof.optimizer @gof.optimizer
...@@ -1397,7 +1406,7 @@ def crossentropy_to_crossentropy_with_softmax(fgraph): ...@@ -1397,7 +1406,7 @@ def crossentropy_to_crossentropy_with_softmax(fgraph):
optdb.register('crossentropy_to_crossentropy_with_softmax', optdb.register('crossentropy_to_crossentropy_with_softmax',
crossentropy_to_crossentropy_with_softmax, 2.01, crossentropy_to_crossentropy_with_softmax, 2.01,
'fast_run', 'xent') 'fast_run', 'xent', 'gpu')
@gof.local_optimizer([softmax_grad]) @gof.local_optimizer([softmax_grad])
...@@ -1410,10 +1419,10 @@ def local_crossentropy_to_crossentropy_with_softmax_grad(node): ...@@ -1410,10 +1419,10 @@ def local_crossentropy_to_crossentropy_with_softmax_grad(node):
dx = crossentropy_softmax_1hot_with_bias_dx(g_nll, dx = crossentropy_softmax_1hot_with_bias_dx(g_nll,
coding_dist, true_one_of_n) coding_dist, true_one_of_n)
return [dx] return [dx]
opt.register_specialize(local_crossentropy_to_crossentropy_with_softmax_grad) opt.register_specialize(local_crossentropy_to_crossentropy_with_softmax_grad,
'gpu')
@opt.register_specialize
@gof.local_optimizer([tensor._max_and_argmax]) @gof.local_optimizer([tensor._max_and_argmax])
def local_argmax_pushdown(node): def local_argmax_pushdown(node):
if node.op == tensor._max_and_argmax and node.inputs[0].owner and \ if node.op == tensor._max_and_argmax and node.inputs[0].owner and \
...@@ -1444,6 +1453,7 @@ def local_argmax_pushdown(node): ...@@ -1444,6 +1453,7 @@ def local_argmax_pushdown(node):
tensor.DimShuffle( tensor.DimShuffle(
pre_bias.broadcastable, pre_bias.broadcastable,
('x', 0))(pre_bias), axis) ('x', 0))(pre_bias), axis)
opt.register_specialize(local_argmax_pushdown, 'gpu')
# Utility function used by the two next optimizations # Utility function used by the two next optimizations
...@@ -1499,7 +1509,6 @@ def _is_const(z, val, approx=False): ...@@ -1499,7 +1509,6 @@ def _is_const(z, val, approx=False):
return numpy.all(maybe == val) return numpy.all(maybe == val)
@opt.register_specialize
@gof.local_optimizer([subtensor.AdvancedSubtensor, tensor.log]) @gof.local_optimizer([subtensor.AdvancedSubtensor, tensor.log])
def local_advanced_indexing_crossentropy_onehot(node): def local_advanced_indexing_crossentropy_onehot(node):
log = None log = None
...@@ -1538,9 +1547,9 @@ def local_advanced_indexing_crossentropy_onehot(node): ...@@ -1538,9 +1547,9 @@ def local_advanced_indexing_crossentropy_onehot(node):
return [-crossentropy_softmax_argmax_1hot_with_bias(x_var, return [-crossentropy_softmax_argmax_1hot_with_bias(x_var,
b_var, b_var,
labels)[0]] labels)[0]]
opt.register_specialize(local_advanced_indexing_crossentropy_onehot, 'gpu')
@opt.register_specialize
@gof.local_optimizer([softmax_grad]) @gof.local_optimizer([softmax_grad])
def local_advanced_indexing_crossentropy_onehot_grad(node): def local_advanced_indexing_crossentropy_onehot_grad(node):
if not (node.op == softmax_grad): if not (node.op == softmax_grad):
...@@ -1761,9 +1770,10 @@ def local_advanced_indexing_crossentropy_onehot_grad(node): ...@@ -1761,9 +1770,10 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
return [crossentropy_softmax_1hot_with_bias_dx(out_grad, sm, labels)] return [crossentropy_softmax_1hot_with_bias_dx(out_grad, sm, labels)]
else: else:
return return
opt.register_specialize(local_advanced_indexing_crossentropy_onehot_grad,
'gpu')
@opt.register_specialize
@gof.local_optimizer([softmax_with_bias]) @gof.local_optimizer([softmax_with_bias])
def graph_merge_softmax_with_crossentropy_softmax(node): def graph_merge_softmax_with_crossentropy_softmax(node):
if node.op == softmax_with_bias: if node.op == softmax_with_bias:
...@@ -1775,6 +1785,8 @@ def graph_merge_softmax_with_crossentropy_softmax(node): ...@@ -1775,6 +1785,8 @@ def graph_merge_softmax_with_crossentropy_softmax(node):
xx, bb, ll = big_client.inputs xx, bb, ll = big_client.inputs
mergeable_client = big_client.op(x, b, ll) mergeable_client = big_client.op(x, b, ll)
return [mergeable_client[1]] return [mergeable_client[1]]
opt.register_specialize(graph_merge_softmax_with_crossentropy_softmax,
'gpu')
def binary_crossentropy(output, target): def binary_crossentropy(output, target):
...@@ -1969,4 +1981,4 @@ local_log_softmax = gof.PatternSub(in_pattern=(tensor.log, (softmax, 'x')), ...@@ -1969,4 +1981,4 @@ local_log_softmax = gof.PatternSub(in_pattern=(tensor.log, (softmax, 'x')),
#don't do register_stabilize, this is to make local_log_softmax run #don't do register_stabilize, this is to make local_log_softmax run
#only after another more specific optimization that stabilizes cross entropy #only after another more specific optimization that stabilizes cross entropy
#opt.register_stabilize(local_log_softmax, name = 'local_log_softmax') #opt.register_stabilize(local_log_softmax, name = 'local_log_softmax')
opt.register_specialize(local_log_softmax, name='local_log_softmax') opt.register_specialize(local_log_softmax, name='local_log_softmax', 'gpu')
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论