def local_inplace_remove0(node): """ Optimization to insert inplace versions of Remove0. """ if isinstance(node.op, Remove0) and not node.op.inplace: new_op = node.op.__class__(inplace=True) new_node = new_op(*node.inputs) return [new_node] return False theano.compile.optdb.register('local_inplace_remove0', gof.TopoOptimizer(local_inplace_remove0, failure_callback=gof.TopoOptimizer.warn_inplace), 60, 'fast_run', 'inplace') @gof.local_optimizer([csm_properties]) def local_csm_properties_csm(node): """if we find csm_properties(CSM(*args)), then we can replace that with the *args directly""" if node.op == csm_properties: csm, = node.inputs if csm.owner and (csm.owner.op == CSC or csm.owner.op == CSR): # csm.owner.inputs could be broadcastable. In that case, we have # to adjust the broadcasting flag here. ret_var = [theano.tensor.patternbroadcast(i, o.broadcastable) for i, o in izip(csm.owner.inputs, node.outputs)] return ret_var return False register_specialize(local_csm_properties_csm)
csm_grad, csm_grad_c, usmm_csc_dense, usmm) from theano.sparse import basic as sparse from basic import _is_sparse_variable # This is tested in tests/test_basic.py:UsmmTests local_usmm = gof.opt.PatternSub( (theano.tensor.sub, 'z', (theano.tensor.mul, {'pattern': 'alpha', 'constraint': lambda expr: numpy.all(expr.type.broadcastable)}, (sparse._dot, 'x', 'y'))), (usmm, (theano.tensor.neg, 'alpha'), 'x', 'y', 'z')) register_specialize(local_usmm, name="local_usmm") # This is tested in tests/test_opt.py:test_local_csm_grad_c @gof.local_optimizer([csm_grad(None)]) def local_csm_grad_c(node): """ csm_grad(None) -> csm_grad_c """ if node.op == csm_grad(None): return [csm_grad_c(*node.inputs)] return False register_specialize(local_csm_grad_c) # This is tested in tests/test_opt.py:test_local_csm_properties_csm @gof.local_optimizer([csm_properties]) def local_csm_properties_csm(node):
return [new_node] return False theano.compile.optdb.register( 'local_inplace_remove0', gof.TopoOptimizer(local_inplace_remove0, failure_callback=gof.TopoOptimizer.warn_inplace), 60, 'fast_run', 'inplace') @gof.local_optimizer([csm_properties]) def local_csm_properties_csm(node): """if we find csm_properties(CSM(*args)), then we can replace that with the *args directly""" if node.op == csm_properties: csm, = node.inputs if csm.owner and (csm.owner.op == CSC or csm.owner.op == CSR): # csm.owner.inputs could be broadcastable. In that case, we have # to adjust the broadcasting flag here. ret_var = [ theano.tensor.patternbroadcast(i, o.broadcastable) for i, o in izip(csm.owner.inputs, node.outputs) ] return ret_var return False register_specialize(local_csm_properties_csm)