提交 0dd1ca98 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Make maxsize available for python < 2.6, use it

上级 0882b46a
...@@ -130,5 +130,22 @@ if sys.version_info[:2] < (2,6): ...@@ -130,5 +130,22 @@ if sys.version_info[:2] < (2,6):
for prod in result: for prod in result:
yield tuple(prod) yield tuple(prod)
# For maxsize
class __Dummy(object):
"""
Dummy class used to know what is the max index of a slice.
This way, we do not have to rely on guesses for untested
architectures.
"""
def __getslice__(self, *args):
return args
# This "slice" should be a (1, maxsize) tuple
__dummy_slice = __Dummy()[1:]
maxsize = __dummy_slice[1]
del __dummy_slice, __Dummy
else: else:
from itertools import combinations, product from itertools import combinations, product
from sys import maxsize
...@@ -14,12 +14,12 @@ __contact__ = "Razvan Pascanu <r.pascanu@gmail>" ...@@ -14,12 +14,12 @@ __contact__ = "Razvan Pascanu <r.pascanu@gmail>"
import logging import logging
import numpy import numpy
import sys
import theano import theano
from theano import tensor from theano import tensor
from theano.tensor import opt, get_constant_value from theano.tensor import opt, get_constant_value
from theano import gof from theano import gof
from theano.gof.python25 import maxsize
from theano.compile import optdb from theano.compile import optdb
from theano import config from theano import config
from theano.compile.function_module import deep_copy_op from theano.compile.function_module import deep_copy_op
...@@ -497,7 +497,7 @@ class ScanSaveMem(gof.Optimizer): ...@@ -497,7 +497,7 @@ class ScanSaveMem(gof.Optimizer):
stop = tensor.basic.extract_constant(cf_slice[0].stop) stop = tensor.basic.extract_constant(cf_slice[0].stop)
else: else:
stop = tensor.basic.extract_constant(cf_slice[0]) + 1 stop = tensor.basic.extract_constant(cf_slice[0]) + 1
if stop == sys.maxsize or stop == length: if stop == maxsize or stop == length:
stop = None stop = None
else: else:
# there is a **gotcha** here ! Namely, scan returns an # there is a **gotcha** here ! Namely, scan returns an
...@@ -517,7 +517,7 @@ class ScanSaveMem(gof.Optimizer): ...@@ -517,7 +517,7 @@ class ScanSaveMem(gof.Optimizer):
global_nsteps['sym'] += [stop] global_nsteps['sym'] += [stop]
# not if it is maxsize # not if it is maxsize
elif (type(stop) in (int, long) and elif (type(stop) in (int, long) and
stop == sys.maxsize): stop == maxsize):
global_nsteps = None global_nsteps = None
# yes if it is a int k, 0 < k < maxsize # yes if it is a int k, 0 < k < maxsize
elif (type(stop) in (int, long) and elif (type(stop) in (int, long) and
...@@ -756,7 +756,7 @@ class ScanSaveMem(gof.Optimizer): ...@@ -756,7 +756,7 @@ class ScanSaveMem(gof.Optimizer):
start = (cnf_slice[0].start - nw_steps - start = (cnf_slice[0].start - nw_steps -
init_l[pos] + store_steps[pos]) init_l[pos] + store_steps[pos])
if (cnf_slice[0].stop is not None and if (cnf_slice[0].stop is not None and
cnf_slice[0].stop != sys.maxsize): cnf_slice[0].stop != maxsize):
stop = (cnf_slice[0].stop - nw_steps - stop = (cnf_slice[0].stop - nw_steps -
init_l[pos] + store_steps[pos]) init_l[pos] + store_steps[pos])
else: else:
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
__docformat__ = "restructuredtext en" __docformat__ = "restructuredtext en"
import __builtin__ import __builtin__
import sys # for sys.maxsize import sys
from theano.configparser import config from theano.configparser import config
import warnings import warnings
from itertools import izip from itertools import izip
...@@ -17,7 +17,7 @@ from theano.gof import Apply, Constant, Op, Type, Value, Variable ...@@ -17,7 +17,7 @@ from theano.gof import Apply, Constant, Op, Type, Value, Variable
import elemwise import elemwise
from theano import scalar as scal from theano import scalar as scal
from theano.gof.python25 import partial, any, all from theano.gof.python25 import partial, any, all, maxsize
from theano import compile, printing from theano import compile, printing
from theano.printing import pprint, min_informative_str from theano.printing import pprint, min_informative_str
from theano.tensor.utils import hash_from_ndarray from theano.tensor.utils import hash_from_ndarray
...@@ -3124,7 +3124,7 @@ def get_canonical_form_slice(theslice, length): ...@@ -3124,7 +3124,7 @@ def get_canonical_form_slice(theslice, length):
start = switch(ge(start,length) start = switch(ge(start,length)
, switch(lt(step,0),length-1,length) , switch(lt(step,0),length-1,length)
, start) , start)
if stop in [None, sys.maxsize]: if stop in [None, maxsize]:
# The special "maxsize" case is probably not needed here, # The special "maxsize" case is probably not needed here,
# as slices containing maxsize are not generated by # as slices containing maxsize are not generated by
# __getslice__ anymore. # __getslice__ anymore.
...@@ -3249,7 +3249,7 @@ class Subtensor(Op): ...@@ -3249,7 +3249,7 @@ class Subtensor(Op):
else: else:
slice_a = None slice_a = None
if b is not None and b != sys.maxsize: if b is not None and b != maxsize:
# The special "maxsize" case is probably not needed here, # The special "maxsize" case is probably not needed here,
# as slices containing maxsize are not generated by # as slices containing maxsize are not generated by
# __getslice__ anymore. # __getslice__ anymore.
...@@ -3350,7 +3350,7 @@ class Subtensor(Op): ...@@ -3350,7 +3350,7 @@ class Subtensor(Op):
# If it is the default (None, None, None) slice, or a variant, # If it is the default (None, None, None) slice, or a variant,
# the shape will be xl # the shape will be xl
if ( (idx.start in [None, 0]) if ( (idx.start in [None, 0])
and (idx.stop in [None, sys.maxsize]) and (idx.stop in [None, maxsize])
and (idx.step is None or idx.step == 1) ): and (idx.step is None or idx.step == 1) ):
outshp.append(xl) outshp.append(xl)
else: else:
...@@ -3424,7 +3424,7 @@ class Subtensor(Op): ...@@ -3424,7 +3424,7 @@ class Subtensor(Op):
is_slice = [] is_slice = []
#TODO: change that, it might lead to unexpected results, #TODO: change that, it might lead to unexpected results,
# see assembla-#767 # see assembla-#767
NONE_CODE = sys.maxsize - 1 NONE_CODE = maxsize - 1
pos = [0,1] #annoying version of global variable for init_entry pos = [0,1] #annoying version of global variable for init_entry
def inc_spec_pos(amt): pos[0] += amt def inc_spec_pos(amt): pos[0] += amt
...@@ -3698,7 +3698,7 @@ class SubtensorPrinter: ...@@ -3698,7 +3698,7 @@ class SubtensorPrinter:
else: else:
msg1 = entry.start msg1 = entry.start
if entry.stop is None or entry.stop == sys.maxsize: if entry.stop is None or entry.stop == maxsize:
msg2 = "" msg2 = ""
else: else:
msg2 = entry.stop msg2 = entry.stop
......
...@@ -20,6 +20,7 @@ import theano ...@@ -20,6 +20,7 @@ import theano
from theano import gof from theano import gof
from theano.gof import opt, InconsistencyError, TopoOptimizer, graph from theano.gof import opt, InconsistencyError, TopoOptimizer, graph
from theano.gof import Variable, Constant from theano.gof import Variable, Constant
from theano.gof.python25 import maxsize
from theano.gof.utils import MethodNotDefined from theano.gof.utils import MethodNotDefined
from theano.configparser import config from theano.configparser import config
from elemwise import Elemwise, DimShuffle from elemwise import Elemwise, DimShuffle
...@@ -1507,7 +1508,7 @@ def local_useless_subtensor(node): ...@@ -1507,7 +1508,7 @@ def local_useless_subtensor(node):
# is not a useless subtensor # is not a useless subtensor
return False return False
length_pos_data = sys.maxsize length_pos_data = maxsize
length_pos = shape_of[node.inputs[0]][pos] length_pos = shape_of[node.inputs[0]][pos]
try: try:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论