提交 67e1c377 authored 作者: Frederic's avatar Frederic

some pep8

上级 9fd1db06
...@@ -190,7 +190,6 @@ def do_setup(): ...@@ -190,7 +190,6 @@ def do_setup():
packages=find_packages(), packages=find_packages(),
install_requires=['numpy>=1.5.0', 'scipy>=0.7.2'], install_requires=['numpy>=1.5.0', 'scipy>=0.7.2'],
package_data={ package_data={
'': ['*.txt', '*.rst', '*.cu', '*.cuh', '*.c', '*.sh', '*.pkl',
'': ['*.txt', '*.rst', '*.cu', '*.cuh', '*.c', '*.sh', '*.pkl', '': ['*.txt', '*.rst', '*.cu', '*.cuh', '*.c', '*.sh', '*.pkl',
'ChangeLog'], 'ChangeLog'],
'theano.misc': ['*.sh'] 'theano.misc': ['*.sh']
......
...@@ -5,7 +5,9 @@ Generator code in SSJ package (L'Ecuyer & Simard) ...@@ -5,7 +5,9 @@ Generator code in SSJ package (L'Ecuyer & Simard)
http://www.iro.umontreal.ca/~simardr/ssj/indexe.html http://www.iro.umontreal.ca/~simardr/ssj/indexe.html
""" """
import sys, warnings import sys
import warnings
import numpy import numpy
from theano import Op, Apply, shared, config, Variable from theano import Op, Apply, shared, config, Variable
...@@ -36,6 +38,7 @@ def matVecModM(A, s, m): ...@@ -36,6 +38,7 @@ def matVecModM(A, s, m):
x[i] = r + m x[i] = r + m
return x return x
def multMatVect(v, A, m1, B, m2): def multMatVect(v, A, m1, B, m2):
#multiply the first half of v by A with a modulo of m1 #multiply the first half of v by A with a modulo of m1
#and the second half by B with a modulo of m2 #and the second half by B with a modulo of m2
...@@ -79,9 +82,11 @@ A2p134 = numpy.asarray( ...@@ -79,9 +82,11 @@ A2p134 = numpy.asarray(
[1401213391, 1178684362, 1431130166]]) [1401213391, 1178684362, 1431130166]])
np_int32_vals = [numpy.int32(i) for i in (0, 7, 9, 15, 16, 22, 24)] np_int32_vals = [numpy.int32(i) for i in (0, 7, 9, 15, 16, 22, 24)]
def ff_2p134(rstate): def ff_2p134(rstate):
return multMatVect(rstate, A1p134, M1, A2p134, M2) return multMatVect(rstate, A1p134, M1, A2p134, M2)
def ff_2p72(rstate): def ff_2p72(rstate):
return multMatVect(rstate, A1p72, M1, A2p72, M2) return multMatVect(rstate, A1p72, M1, A2p72, M2)
...@@ -93,8 +98,8 @@ def mrg_next_value(rstate, new_rstate): ...@@ -93,8 +98,8 @@ def mrg_next_value(rstate, new_rstate):
#i0, i7, i9, i15, i16, i22, i24 = [numpy.int32(i) for i in (0, 7, 9, 15, 16, 22, 24)] #i0, i7, i9, i15, i16, i22, i24 = [numpy.int32(i) for i in (0, 7, 9, 15, 16, 22, 24)]
i0, i7, i9, i15, i16, i22, i24 = np_int32_vals i0, i7, i9, i15, i16, i22, i24 = np_int32_vals
#first component #first component
y1 = (((x12 & MASK12) << i22) + (x12 >> i9) y1 = (((x12 & MASK12) << i22) + (x12 >> i9) +
+ ((x13 & MASK13) << i7) + (x13 >> i24)) ((x13 & MASK13) << i7) + (x13 >> i24))
assert type(y1) == numpy.int32 assert type(y1) == numpy.int32
if (y1 < 0 or y1 >= M1): #must also check overflow if (y1 < 0 or y1 >= M1): #must also check overflow
...@@ -135,6 +140,7 @@ def mrg_next_value(rstate, new_rstate): ...@@ -135,6 +140,7 @@ def mrg_next_value(rstate, new_rstate):
else: else:
return (x11 - x21) * NORM return (x11 - x21) * NORM
class mrg_uniform_base(Op): class mrg_uniform_base(Op):
def __init__(self, output_type, inplace=False): def __init__(self, output_type, inplace=False):
Op.__init__(self) Op.__init__(self)
...@@ -145,17 +151,19 @@ class mrg_uniform_base(Op): ...@@ -145,17 +151,19 @@ class mrg_uniform_base(Op):
self.warned_numpy_version = False self.warned_numpy_version = False
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) \ return (type(self) == type(other) and
and self.output_type == other.output_type \ self.output_type == other.output_type and
and self.inplace == other.inplace self.inplace == other.inplace)
def __hash__(self): def __hash__(self):
return hash(type(self)) ^ hash(self.output_type) ^ hash(self.inplace) return hash(type(self)) ^ hash(self.output_type) ^ hash(self.inplace)
def __str__(self): def __str__(self):
if self.inplace: if self.inplace:
s = "inplace" s = "inplace"
else: s = "no_inplace" else:
return self.__class__.__name__+"{%s,%s}"%(self.output_type,s) s = "no_inplace"
return self.__class__.__name__ + "{%s,%s}" % (self.output_type, s)
def make_node(self, rstate, size): def make_node(self, rstate, size):
# error checking slightly redundant here, since # error checking slightly redundant here, since
...@@ -166,7 +174,7 @@ class mrg_uniform_base(Op): ...@@ -166,7 +174,7 @@ class mrg_uniform_base(Op):
[rstate, size], [rstate, size],
[rstate.type(), self.output_type()]) [rstate.type(), self.output_type()])
def grad(self,inputs,ograd): def grad(self, inputs, ograd):
return [None for i in inputs] return [None for i in inputs]
def R_op(self, inputs, eval_points): def R_op(self, inputs, eval_points):
...@@ -187,8 +195,8 @@ class mrg_uniform(mrg_uniform_base): ...@@ -187,8 +195,8 @@ class mrg_uniform(mrg_uniform_base):
def perform(self, node, inp, out): def perform(self, node, inp, out):
rstate, size = inp rstate, size = inp
o_rstate, o_sample = out o_rstate, o_sample = out
numpy_version=numpy.__version__.split('.') numpy_version = numpy.__version__.split('.')
if not self.warned_numpy_version and int(numpy_version[0])<=1 and int(numpy_version[1])<3: if not self.warned_numpy_version and int(numpy_version[0]) <= 1 and int(numpy_version[1]) <3 :
print "Warning: you must use numpy version 1.3.0 or higher with the python version of this op. Otherwise numpy leak memory. and numpy" print "Warning: you must use numpy version 1.3.0 or higher with the python version of this op. Otherwise numpy leak memory. and numpy"
self.warned_numpy_version = True self.warned_numpy_version = True
...@@ -201,20 +209,21 @@ class mrg_uniform(mrg_uniform_base): ...@@ -201,20 +209,21 @@ class mrg_uniform(mrg_uniform_base):
for s in size: for s in size:
n_elements *= s n_elements *= s
n_streams,_ = rstate.shape n_streams, _ = rstate.shape
rval = numpy.zeros(n_elements, dtype=self.output_type.dtype) rval = numpy.zeros(n_elements, dtype=self.output_type.dtype)
err_orig = numpy.seterr(over='ignore') err_orig = numpy.seterr(over='ignore')
try: try:
for i in xrange(n_elements): for i in xrange(n_elements):
sample = mrg_next_value(rstate[i%n_streams], rstate[i%n_streams]) sample = mrg_next_value(rstate[i % n_streams],
rstate[i % n_streams])
rval[i] = sample rval[i] = sample
finally: finally:
numpy.seterr(**err_orig) numpy.seterr(**err_orig)
o_rstate[0] = node.outputs[0].type.filter(rstate) # send to GPU if necessary o_rstate[0] = node.outputs[0].type.filter(rstate) # send to GPU if necessary
o_sample[0] = node.outputs[1].type.filter(rval.reshape(size))# send to GPU if necessary o_sample[0] = node.outputs[1].type.filter(rval.reshape(size)) # send to GPU if necessary
def c_code(self, node, name, inp, out, sub): def c_code(self, node, name, inp, out, sub):
rstate, size = inp rstate, size = inp
...@@ -228,7 +237,7 @@ class mrg_uniform(mrg_uniform_base): ...@@ -228,7 +237,7 @@ class mrg_uniform(mrg_uniform_base):
fail = sub['fail'] fail = sub['fail']
if self.output_type.dtype == 'float32': if self.output_type.dtype == 'float32':
otype = 'float' otype = 'float'
NORM = '4.6566126e-10f' #numpy.float32(1.0/(2**31+65)) NORM = '4.6566126e-10f' # numpy.float32(1.0/(2**31+65))
# this was determined by finding the biggest number such that # this was determined by finding the biggest number such that
# numpy.float32(number * M1) < 1.0 # numpy.float32(number * M1) < 1.0
else: else:
...@@ -392,7 +401,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp): ...@@ -392,7 +401,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
def c_support_code_apply(self, node, nodename): def c_support_code_apply(self, node, nodename):
if self.output_type.dtype == 'float32': if self.output_type.dtype == 'float32':
otype = 'float' otype = 'float'
NORM = '4.6566126e-10f' #numpy.float32(1.0/(2**31+65)) NORM = '4.6566126e-10f' # numpy.float32(1.0/(2**31+65))
# this was determined by finding the biggest number such that # this was determined by finding the biggest number such that
# numpy.float32(number * M1) < 1.0 # numpy.float32(number * M1) < 1.0
else: else:
...@@ -476,7 +485,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp): ...@@ -476,7 +485,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
} }
} }
""" %locals() """ % locals()
def c_code(self, node, nodename, inp, out, sub): def c_code(self, node, nodename, inp, out, sub):
rstate, size = inp rstate, size = inp
...@@ -491,7 +500,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp): ...@@ -491,7 +500,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
else: else:
otype = 'double' otype = 'double'
SYNC="CNDA_THREAD_SYNC"; SYNC = "CNDA_THREAD_SYNC"
return """ return """
//////// <code generated by mrg_uniform> //////// <code generated by mrg_uniform>
...@@ -593,7 +602,8 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp): ...@@ -593,7 +602,8 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
} }
//////// </ code generated by mrg_uniform> //////// </ code generated by mrg_uniform>
""" %locals() """ % locals()
def c_code_cache_version(self): def c_code_cache_version(self):
return (7,) return (7,)
...@@ -662,7 +672,7 @@ class MRG_RandomStreams(object): ...@@ -662,7 +672,7 @@ class MRG_RandomStreams(object):
elif seed >= M2: elif seed >= M2:
raise ValueError('seed should be less than %i' % M2, seed) raise ValueError('seed should be less than %i' % M2, seed)
self.rstate = numpy.asarray([seed]*6, dtype='int32') self.rstate = numpy.asarray([seed]*6, dtype='int32')
elif len(seed)==6: elif len(seed) == 6:
if seed[0] == 0 and seed[1] == 0 and seed[2] == 0: if seed[0] == 0 and seed[1] == 0 and seed[2] == 0:
raise ValueError('The first 3 values of seed should not be all 0', seed) raise ValueError('The first 3 values of seed should not be all 0', seed)
if seed[3] == 0 and seed[4] == 0 and seed[5] == 0: if seed[3] == 0 and seed[4] == 0 and seed[5] == 0:
...@@ -690,7 +700,7 @@ class MRG_RandomStreams(object): ...@@ -690,7 +700,7 @@ class MRG_RandomStreams(object):
""" """
assert n_streams < 2**72 assert n_streams < 2**72
assert n_streams > 0 assert n_streams > 0
rval = numpy.zeros((n_streams,6), dtype='int32') rval = numpy.zeros((n_streams, 6), dtype='int32')
rval[0] = self.rstate rval[0] = self.rstate
for i in xrange(1, n_streams): for i in xrange(1, n_streams):
rval[i] = ff_2p72(rval[i - 1]) rval[i] = ff_2p72(rval[i - 1])
...@@ -776,11 +786,13 @@ class MRG_RandomStreams(object): ...@@ -776,11 +786,13 @@ class MRG_RandomStreams(object):
# currently no Theano node that will do a frombuffer # currently no Theano node that will do a frombuffer
# reinterpretation. # reinterpretation.
u = self.pretty_return(node_rstate, u = self.pretty_return(node_rstate,
*GPU_mrg_uniform.new(node_rstate, ndim, dtype, size)) *GPU_mrg_uniform.new(node_rstate,
ndim, dtype, size))
else: else:
node_rstate = shared(self.get_substream_rstates(nstreams)) node_rstate = shared(self.get_substream_rstates(nstreams))
u = self.pretty_return(node_rstate, u = self.pretty_return(node_rstate,
*mrg_uniform.new(node_rstate, ndim, dtype, size)) *mrg_uniform.new(node_rstate,
ndim, dtype, size))
r = u * (high - low) + low r = u * (high - low) + low
if u.type.broadcastable != r.type.broadcastable: if u.type.broadcastable != r.type.broadcastable:
...@@ -934,4 +946,6 @@ def mrg_random_make_inplace(node): ...@@ -934,4 +946,6 @@ def mrg_random_make_inplace(node):
new_op = op.__class__(op.output_type, inplace=True) new_op = op.__class__(op.output_type, inplace=True)
return new_op.make_node(*node.inputs).outputs return new_op.make_node(*node.inputs).outputs
return False return False
optdb.register('random_make_inplace_mrg', opt.in2out(mrg_random_make_inplace, ignore_newtrees=True), 99, 'fast_run', 'inplace') optdb.register('random_make_inplace_mrg',
opt.in2out(mrg_random_make_inplace, ignore_newtrees=True),
99, 'fast_run', 'inplace')
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论