提交 6f030393 authored 作者: Vincent Michalski's avatar Vincent Michalski

added copy_stack_trace calls, todo: unit tests

上级 a62b8883
...@@ -3,6 +3,7 @@ from theano.gradient import DisconnectedType ...@@ -3,6 +3,7 @@ from theano.gradient import DisconnectedType
from theano.gof import Op, Apply, TopoOptimizer from theano.gof import Op, Apply, TopoOptimizer
from theano import tensor from theano import tensor
import theano.sandbox.cuda as cuda import theano.sandbox.cuda as cuda
from theano.tensor.opt import copy_stack_trace
def get_diagonal_subtensor_view(x, i0, i1): def get_diagonal_subtensor_view(x, i0, i1):
...@@ -328,7 +329,9 @@ def make_gpu_optimizer(op, to_gpu): ...@@ -328,7 +329,9 @@ def make_gpu_optimizer(op, to_gpu):
new_inp = list(node.inputs) new_inp = list(node.inputs)
for idx in to_gpu: for idx in to_gpu:
new_inp[idx] = cuda.gpu_from_host(new_inp[idx]) new_inp[idx] = cuda.gpu_from_host(new_inp[idx])
return [cuda.host_from_gpu(op()(*new_inp))] new_node = cuda.host_from_gpu(op()(*new_inp))
copy_stack_trace(node.outputs[0], new_node)
return [new_node]
if node.op == cuda.gpu_from_host: if node.op == cuda.gpu_from_host:
# gpu_from_host(op) -> op(gpu_from_host) # gpu_from_host(op) -> op(gpu_from_host)
host_input = node.inputs[0] host_input = node.inputs[0]
...@@ -338,7 +341,9 @@ def make_gpu_optimizer(op, to_gpu): ...@@ -338,7 +341,9 @@ def make_gpu_optimizer(op, to_gpu):
new_inp = list(op_node.inputs) new_inp = list(op_node.inputs)
for idx in to_gpu: for idx in to_gpu:
new_inp[idx] = cuda.gpu_from_host(new_inp[idx]) new_inp[idx] = cuda.gpu_from_host(new_inp[idx])
return [op()(*new_inp)] new_node = op()(*new_inp)
copy_stack_trace(node.outputs[0], new_node)
return [new_node]
return False return False
local_to_gpu.__name__ = "local_to_gpu_" + op.__name__ local_to_gpu.__name__ = "local_to_gpu_" + op.__name__
cuda.opt.register_opt()(local_to_gpu) cuda.opt.register_opt()(local_to_gpu)
...@@ -355,6 +360,7 @@ def local_inplace_DiagonalSubtensor(node): ...@@ -355,6 +360,7 @@ def local_inplace_DiagonalSubtensor(node):
not node.op.inplace): not node.op.inplace):
new_op = node.op.__class__(inplace=True) new_op = node.op.__class__(inplace=True)
new_node = new_op(*node.inputs) new_node = new_op(*node.inputs)
copy_stack_trace(node.outputs[0], new_node)
return [new_node] return [new_node]
return False return False
theano.compile.optdb.register( theano.compile.optdb.register(
......
...@@ -752,7 +752,8 @@ def local_logsoftmax(node): ...@@ -752,7 +752,8 @@ def local_logsoftmax(node):
inVars = node.inputs[0].owner.inputs[0] inVars = node.inputs[0].owner.inputs[0]
new_op = LogSoftmax() new_op = LogSoftmax()
ret = new_op(inVars) ret = new_op(inVars)
ret.tag.values_eq_approx = values_eq_approx_remove_inf ret .tag.values_eq_approx = values_eq_approx_remove_inf
copy_stack_trace(node.outputs[0], ret)
return [ret] return [ret]
...@@ -785,9 +786,9 @@ def local_logsoftmax_grad(node): ...@@ -785,9 +786,9 @@ def local_logsoftmax_grad(node):
grads = node.inputs[0].owner.inputs[0] grads = node.inputs[0].owner.inputs[0]
if grads.broadcastable[1] and not sm.broadcastable[1]: if grads.broadcastable[1] and not sm.broadcastable[1]:
grads = tensor.alloc(grads, grads.shape[0], sm.shape[1]) grads = tensor.alloc(grads, grads.shape[0], sm.shape[1])
ret = grads - tensor.sum(grads, axis=1, keepdims=True) * sm ret = grads - tensor.sum(grads, axis=1, keepdims=True) * sm
ret.tag.values_eq_approx = values_eq_approx_remove_nan ret.tag.values_eq_approx = values_eq_approx_remove_nan
copy_stack_trace(node.outputs[0], ret)
return [ret] return [ret]
...@@ -866,6 +867,7 @@ def local_softmax_with_bias(node): ...@@ -866,6 +867,7 @@ def local_softmax_with_bias(node):
if sm_bias.type == node.outputs[0].type: if sm_bias.type == node.outputs[0].type:
# 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
copy_stack_trace(node.outputs[0], sm_bias)
return [sm_bias] return [sm_bias]
......
...@@ -36,6 +36,7 @@ def local_inplace_sparse_block_gemv(node): ...@@ -36,6 +36,7 @@ def local_inplace_sparse_block_gemv(node):
""" """
if isinstance(node.op, SparseBlockGemv) and not node.op.inplace: if isinstance(node.op, SparseBlockGemv) and not node.op.inplace:
new_node = sparse_block_gemv_inplace(*node.inputs) new_node = sparse_block_gemv_inplace(*node.inputs)
copy_stack_trace(node.outputs[0], new_node)
return [new_node] return [new_node]
return False return False
compile.optdb.register('local_inplace_sparse_block_gemv', compile.optdb.register('local_inplace_sparse_block_gemv',
...@@ -52,6 +53,7 @@ def local_inplace_sparse_block_outer(node): ...@@ -52,6 +53,7 @@ def local_inplace_sparse_block_outer(node):
""" """
if isinstance(node.op, SparseBlockOuter) and not node.op.inplace: if isinstance(node.op, SparseBlockOuter) and not node.op.inplace:
new_node = sparse_block_outer_inplace(*node.inputs) new_node = sparse_block_outer_inplace(*node.inputs)
copy_stack_trace(node.outputs[0], new_node)
return [new_node] return [new_node]
return False return False
compile.optdb.register('local_inplace_sparse_block_outer', compile.optdb.register('local_inplace_sparse_block_outer',
......
...@@ -18,7 +18,7 @@ from theano.printing import pprint ...@@ -18,7 +18,7 @@ from theano.printing import pprint
from theano.tensor import basic as tensor from theano.tensor import basic as tensor
from theano.tensor import elemwise, opt, NotScalarConstantError from theano.tensor import elemwise, opt, NotScalarConstantError
from theano.tensor.type import values_eq_approx_remove_inf from theano.tensor.type import values_eq_approx_remove_inf
from theano.tensor.opt import copy_stack_trace
############ ############
# #
...@@ -262,6 +262,7 @@ def local_ultra_fast_sigmoid(node): ...@@ -262,6 +262,7 @@ def local_ultra_fast_sigmoid(node):
if (isinstance(node.op, tensor.Elemwise) and if (isinstance(node.op, tensor.Elemwise) and
node.op.scalar_op == scalar_sigmoid): node.op.scalar_op == scalar_sigmoid):
out = ultra_fast_sigmoid(node.inputs[0]) out = ultra_fast_sigmoid(node.inputs[0])
copy_stack_trace(node.outputs[0], out)
def values_eq_approx_remove_low_prec(a, b): def values_eq_approx_remove_low_prec(a, b):
# atol is found by trial/error. # atol is found by trial/error.
...@@ -301,6 +302,7 @@ def local_hard_sigmoid(node): ...@@ -301,6 +302,7 @@ def local_hard_sigmoid(node):
if (isinstance(node.op, tensor.Elemwise) and if (isinstance(node.op, tensor.Elemwise) and
node.op.scalar_op == scalar_sigmoid): node.op.scalar_op == scalar_sigmoid):
out = hard_sigmoid(node.inputs[0]) out = hard_sigmoid(node.inputs[0])
copy_stack_trace(node.outputs[0], out)
def values_eq_approx_remove_low_prec(a, b): def values_eq_approx_remove_low_prec(a, b):
# atol is found by trial/error. # atol is found by trial/error.
...@@ -925,7 +927,10 @@ def local_sigm_times_exp(node): ...@@ -925,7 +927,10 @@ def local_sigm_times_exp(node):
# get rid of them. # get rid of them.
mul_tree = simplify_mul(mul_tree) mul_tree = simplify_mul(mul_tree)
# Recompute final output based on the updated tree. # Recompute final output based on the updated tree.
return [compute_mul(mul_tree)] out = compute_mul(mul_tree)
# keep the stack trace
copy_stack_trace(node.outputs[0], out)
return [out]
@opt.register_stabilize @opt.register_stabilize
...@@ -946,10 +951,13 @@ def local_inv_1_plus_exp(node): ...@@ -946,10 +951,13 @@ def local_inv_1_plus_exp(node):
if len(nonconsts) == 1: if len(nonconsts) == 1:
if nonconsts[0].owner and nonconsts[0].owner.op == tensor.exp: if nonconsts[0].owner and nonconsts[0].owner.op == tensor.exp:
if scalars and numpy.allclose(numpy.sum(scalars), 1): if scalars and numpy.allclose(numpy.sum(scalars), 1):
return opt._fill_chain( out = opt._fill_chain(
sigmoid( sigmoid(
tensor.neg(nonconsts[0].owner.inputs[0])), tensor.neg(nonconsts[0].owner.inputs[0])),
scalar_inputs) scalar_inputs)
# keep stack trace
copy_stack_trace(node.outputs[0], out)
return out
# Registration is below, and conditional. # Registration is below, and conditional.
...@@ -970,7 +978,9 @@ def local_1msigmoid(node): ...@@ -970,7 +978,9 @@ def local_1msigmoid(node):
except Exception: except Exception:
return return
if numpy.allclose(numpy.sum(val_l), 1): if numpy.allclose(numpy.sum(val_l), 1):
return [sigmoid(-sub_r.owner.inputs[0])] out = sigmoid(-sub_r.owner.inputs[0])
copy_stack_trace(node.outputs[0], out)
return [out]
register_local_1msigmoid = False register_local_1msigmoid = False
# This is False because the Stabilize pattern above # This is False because the Stabilize pattern above
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论