提交 e93c61d1 authored 作者: abergeron's avatar abergeron

Merge pull request #1779 from nouiz/faster_opt

Faster opt
...@@ -1047,10 +1047,6 @@ class PatternSub(LocalOptimizer): ...@@ -1047,10 +1047,6 @@ class PatternSub(LocalOptimizer):
self.__name__ = name self.__name__ = name
self.pdb = pdb self.pdb = pdb
def skip_identities(self, expr):
if self.skip_identities_fn:
return self.skip_identities_fn(expr)
def op_key(self): def op_key(self):
return self.op return self.op
...@@ -1064,10 +1060,13 @@ class PatternSub(LocalOptimizer): ...@@ -1064,10 +1060,13 @@ class PatternSub(LocalOptimizer):
""" """
if node.op != self.op: if node.op != self.op:
return False return False
#TODO: if we remove pdb, do this speed things up?
def match(pattern, expr, u, allow_multiple_clients=False, pdb=False): def match(pattern, expr, u, allow_multiple_clients=False, pdb=False):
#TODO move outside match
def retry_with_equiv(): def retry_with_equiv():
expr_equiv = self.skip_identities(expr) if not self.skip_identities_fn:
return False
expr_equiv = self.skip_identities_fn(expr)
if expr_equiv is None: if expr_equiv is None:
return False return False
#TODO: Not sure how to handle multiple_clients flag #TODO: Not sure how to handle multiple_clients flag
...@@ -1126,19 +1125,19 @@ class PatternSub(LocalOptimizer): ...@@ -1126,19 +1125,19 @@ class PatternSub(LocalOptimizer):
pdb.set_trace() pdb.set_trace()
return u return u
def build(pattern, u):
if isinstance(pattern, (list, tuple)):
args = [build(p, u) for p in pattern[1:]]
return pattern[0](*args)
elif isinstance(pattern, basestring):
return u[unify.Var(pattern)]
elif isinstance(pattern, (int, float)):
return pattern
else:
return pattern.clone()
u = match(self.in_pattern, node.out, unify.Unification(), True, u = match(self.in_pattern, node.out, unify.Unification(), True,
self.pdb) self.pdb)
if u: if u:
def build(pattern, u):
if isinstance(pattern, (list, tuple)):
args = [build(p, u) for p in pattern[1:]]
return pattern[0](*args)
elif isinstance(pattern, basestring):
return u[unify.Var(pattern)]
elif isinstance(pattern, (int, float)):
return pattern
else:
return pattern.clone()
p = self.out_pattern p = self.out_pattern
new = build(p, u) new = build(p, u)
####print "PatternSub matched:", new ####print "PatternSub matched:", new
...@@ -1520,19 +1519,23 @@ class EquilibriumOptimizer(NavigatorOptimizer): ...@@ -1520,19 +1519,23 @@ class EquilibriumOptimizer(NavigatorOptimizer):
def __init__(self, def __init__(self,
optimizers, optimizers,
failure_callback=None, failure_callback=None,
ignore_newtrees=True,
max_use_ratio=None): max_use_ratio=None):
""" """ Apply optimizations until equilibrium point.
:param optimizers: list or set of local or global optimizations to :param optimizers: list or set of local or global optimizations to
apply until equilibrium. apply until equilibrium.
:param max_use_ratio: each optimizer can be applied at most :param max_use_ratio: each optimizer can be applied at most
(size of graph * this number) times (size of graph * this number) times
:param ignore_newtrees: See EquilibriumDB ignore_newtrees
parameter definition
""" """
super(EquilibriumOptimizer, self).__init__( super(EquilibriumOptimizer, self).__init__(
None, None,
ignore_newtrees=True, ignore_newtrees=ignore_newtrees,
failure_callback=failure_callback) failure_callback=failure_callback)
self.local_optimizers_map = dict() self.local_optimizers_map = dict()
self.local_optimizers_all = [] self.local_optimizers_all = []
......
...@@ -179,23 +179,33 @@ class Query(object): ...@@ -179,23 +179,33 @@ class Query(object):
class EquilibriumDB(DB): class EquilibriumDB(DB):
""" A set of potential optimizations which should be applied in an """A set of potential optimizations which should be applied in an
arbitrary order until equilibrium is reached. arbitrary order until equilibrium is reached.
Canonicalize, Stabilize, and Specialize are all equilibrium optimizations. Canonicalize, Stabilize, and Specialize are all equilibrium optimizations.
:param ignore_newtrees: If False, we will apply local opt on new
node introduced during local optimization application. This
could result in less fgraph iterations, but this don't mean it
will be faster globally.
.. note:: .. note::
We can put LocalOptimizer and Optimizer as EquilibriumOptimizer We can put LocalOptimizer and Optimizer as EquilibriumOptimizer
suppor both. suppor both.
""" """
def __init__(self, ignore_newtrees=True):
super(EquilibriumDB, self).__init__()
self.ignore_newtrees = ignore_newtrees
def query(self, *tags, **kwtags): def query(self, *tags, **kwtags):
opts = super(EquilibriumDB, self).query(*tags, **kwtags) opts = super(EquilibriumDB, self).query(*tags, **kwtags)
return opt.EquilibriumOptimizer(opts, return opt.EquilibriumOptimizer(
max_use_ratio=config.optdb.max_use_ratio, opts,
failure_callback=opt.NavigatorOptimizer.warn_inplace) max_use_ratio=config.optdb.max_use_ratio,
ignore_newtrees=self.ignore_newtrees,
failure_callback=opt.NavigatorOptimizer.warn_inplace)
class SequenceDB(DB): class SequenceDB(DB):
......
差异被折叠。
...@@ -1190,32 +1190,31 @@ def _beta_L_plus_alpha_M(beta, L, alpha, M, recurse_flip=True): ...@@ -1190,32 +1190,31 @@ def _beta_L_plus_alpha_M(beta, L, alpha, M, recurse_flip=True):
# it also might be the case that there is a dimshuffle between the + # it also might be the case that there is a dimshuffle between the +
# and the dot22. local_dot_to_dot22 in particular will put in such things. # and the dot22. local_dot_to_dot22 in particular will put in such things.
if M.owner and isinstance(M.owner.op, T.DimShuffle): if (M.owner and isinstance(M.owner.op, T.DimShuffle) and
M.owner.inputs[0].owner and
isinstance(M.owner.inputs[0].owner.op, Dot22)):
MM = M.owner.inputs[0] MM = M.owner.inputs[0]
if tuple(M.owner.op.new_order) == (0,): if M.owner.op.new_order == (0,):
# it is making a column MM into a vector # it is making a column MM into a vector
if MM.owner and MM.owner.op == _dot22: MMl, MMr = MM.owner.inputs
MMl, MMr = MM.owner.inputs g = gemm_no_inplace(L.dimshuffle(0, 'x'),
g = gemm_no_inplace(L.dimshuffle(0, 'x'), alpha, MMl, MMr, beta)
alpha, MMl, MMr, beta) rval = [g.dimshuffle(0)]
rval = [g.dimshuffle(0)] return rval, MM
return rval, MM if M.owner.op.new_order == (1,):
if tuple(M.owner.op.new_order) == (1,):
# it is making a row MM into a vector # it is making a row MM into a vector
if MM.owner and MM.owner.op == _dot22: MMl, MMr = MM.owner.inputs
MMl, MMr = MM.owner.inputs g = gemm_no_inplace(L.dimshuffle('x', 0),
g = gemm_no_inplace(L.dimshuffle('x', 0), alpha, MMl, MMr, beta)
alpha, MMl, MMr, beta) rval = [g.dimshuffle(1)]
rval = [g.dimshuffle(1)] return rval, MM
return rval, MM if len(M.owner.op.new_order) == 0:
if tuple(M.owner.op.new_order) == ():
# it is making a row MM into a vector # it is making a row MM into a vector
if MM.owner and MM.owner.op == _dot22: MMl, MMr = MM.owner.inputs
MMl, MMr = MM.owner.inputs g = gemm_no_inplace(L.dimshuffle('x', 'x'),
g = gemm_no_inplace(L.dimshuffle('x', 'x'), alpha, MMl, MMr, beta)
alpha, MMl, MMr, beta) rval = [g.dimshuffle()]
rval = [g.dimshuffle()] return rval, MM
return rval, MM
# this is False'd out because of inadequate testing. # this is False'd out because of inadequate testing.
# TODO see ticket #237 # TODO see ticket #237
...@@ -1379,29 +1378,31 @@ def _gemm_from_factored_list(lst): ...@@ -1379,29 +1378,31 @@ def _gemm_from_factored_list(lst):
"""Returns None, or a list to replace node.outputs """Returns None, or a list to replace node.outputs
""" """
# Make every pair in list have matching dtypes
# sM can be a tuple of 2 elements or a theano variable.
# We should not use __len__ as theano variables don't support
# it. I don't want to change this to isinstance(sM, tuple)
# as I'm not able to make a test that triggers this case.
def is_pair(sM):
try:
s, M = sM
return True
except Exception:
return False
lst2 = [] lst2 = []
# Remove the tuple that can't be cast correctly. # Remove the tuple that can't be cast correctly.
# This can happen when we try to cast a complex to a real # This can happen when we try to cast a complex to a real
for sM in lst: for sM in lst:
if is_pair(sM): # Make every pair in list have matching dtypes
# sM can be a tuple of 2 elements or a theano variable.
if isinstance(sM, tuple):
sm0, sm1 = sM sm0, sm1 = sM
sm0 = T.as_tensor_variable(sm0) sm0 = T.as_tensor_variable(sm0)
if theano.scalar.upcast(sm0.dtype, sm1.dtype) == sm1.dtype: if theano.scalar.upcast(sm0.dtype, sm1.dtype) == sm1.dtype:
lst2.append((T.cast(sm0, sm1.dtype), sM[1])) lst2.append((T.cast(sm0, sm1.dtype), sM[1]))
lst = lst2 lst = lst2
def item_to_var(t):
try:
s, M = t
except Exception:
return t
if s == 1:
return M
if s == -1:
return -M
return s * M
# Try every pair in the sM_list, trying to turn it into a gemm operation # Try every pair in the sM_list, trying to turn it into a gemm operation
for i in xrange(len(lst) - 1): for i in xrange(len(lst) - 1):
s_i, M_i = lst[i] s_i, M_i = lst[i]
...@@ -1418,16 +1419,6 @@ def _gemm_from_factored_list(lst): ...@@ -1418,16 +1419,6 @@ def _gemm_from_factored_list(lst):
s_j, M_j) s_j, M_j)
#print 'GOT IT', gemm_of_sM_list #print 'GOT IT', gemm_of_sM_list
if gemm_of_sM_list: if gemm_of_sM_list:
def item_to_var(t):
try:
s, M = t
except Exception:
return t
if s == 1:
return M
if s == -1:
return -M
return s * M
assert len(gemm_of_sM_list) == 1 assert len(gemm_of_sM_list) == 1
add_inputs = [item_to_var(input) add_inputs = [item_to_var(input)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论