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

some pep8

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