Skip to content

Commit ff9e736

Browse files
committed
Python 3 compat, fixes Theano#35
1 parent a7aae9f commit ff9e736

9 files changed

Lines changed: 59 additions & 38 deletions

File tree

‎pygpu/gpuarray.pxd‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ cdef int kernel_binary(GpuKernel k, size_t *, void **) except -1
245245
cdef int kernel_property(GpuKernel k, int prop_id, void *res) except -1
246246

247247
cdef int ctx_property(GpuContext c, int prop_id, void *res) except -1
248-
cdef const gpuarray_buffer_ops *get_ops(kind) except NULL
248+
cdef const gpuarray_buffer_ops *get_ops(bytes kind) except NULL
249249
cdef ops_kind(const gpuarray_buffer_ops *ops)
250250
cdef GpuContext ensure_context(GpuContext c)
251251

‎pygpu/gpuarray.pyx‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ cdef object PyArray_Empty(int a, np.npy_intp *b, np.dtype c, int d):
2323
Py_INCREF(c)
2424
return _PyArray_Empty(a, b, c, d)
2525

26+
cdef bytes _s(s):
27+
if isinstance(s, unicode):
28+
return (<unicode>s).encode('ascii')
29+
if isinstance(s, bytes):
30+
return s
31+
raise TypeError("Expected a string")
32+
2633
cdef object call_compiler_fn = None
2734

2835
cdef void *call_compiler_python(const char *src, size_t sz,
@@ -107,7 +114,7 @@ def cl_wrap_ctx(size_t ptr):
107114
if cl_make_ctx == NULL:
108115
raise RuntimeError, "cl_make_ctx extension is absent"
109116
res = GpuContext.__new__(GpuContext)
110-
res.ops = get_ops('opencl')
117+
res.ops = get_ops(b"opencl")
111118
res.ctx = cl_make_ctx(<void *>ptr)
112119
if res.ctx == NULL:
113120
raise RuntimeError, "cl_make_ctx call failed"
@@ -132,7 +139,7 @@ def cuda_wrap_ctx(size_t ptr, bint own):
132139
if cuda_make_ctx == NULL:
133140
raise RuntimeError, "cuda_make_ctx extension is absent"
134141
res = GpuContext.__new__(GpuContext)
135-
res.ops = get_ops('cuda')
142+
res.ops = get_ops(b"cuda")
136143
flags = 0
137144
if not own:
138145
flags |= GPUARRAY_CUDA_CTX_NOFREE
@@ -261,9 +268,11 @@ def dtype_to_ctype(dtype):
261268
"""
262269
cdef int typecode = dtype_to_typecode(dtype)
263270
cdef const gpuarray_type *t = gpuarray_get_type(typecode)
271+
cdef bytes res
264272
if t.cluda_name == NULL:
265273
raise ValueError, "No mapping for %s"%(dtype,)
266-
return t.cluda_name
274+
res = t.cluda_name
275+
return res.decode('ascii')
267276

268277
cdef ga_order to_ga_order(ord) except <ga_order>-2:
269278
if ord == "C" or ord == "c":
@@ -498,7 +507,7 @@ cdef int ctx_property(GpuContext c, int prop_id, void *res) except -1:
498507
if err != GA_NO_ERROR:
499508
raise get_exc(err), Gpu_error(c.ops, c.ctx, err)
500509

501-
cdef const gpuarray_buffer_ops *get_ops(kind) except NULL:
510+
cdef const gpuarray_buffer_ops *get_ops(bytes kind) except NULL:
502511
cdef const gpuarray_buffer_ops *res
503512
res = gpuarray_get_ops(kind)
504513
if res == NULL:
@@ -972,7 +981,7 @@ cdef class GpuContext:
972981
def __cinit__(self, kind, devno):
973982
cdef int err = GA_NO_ERROR
974983
cdef void *ctx
975-
self.ops = get_ops(kind)
984+
self.ops = get_ops(_s(kind))
976985
self.ctx = self.ops.buffer_init(devno, 0, &err)
977986
if (err != GA_NO_ERROR):
978987
if err == GA_VALUE_ERROR:
@@ -1843,10 +1852,8 @@ cdef class GpuKernel:
18431852
cdef const gpuarray_buffer_ops *ops
18441853
cdef int flags = 0
18451854

1846-
if not isinstance(source, (str, unicode)):
1847-
raise TypeError, "Expected a string for the kernel source"
1848-
if not isinstance(name, (str, unicode)):
1849-
raise TypeError, "Expected a string for the kernel name"
1855+
source = _s(source)
1856+
name = _s(name)
18501857

18511858
self.context = ensure_context(context)
18521859

‎pygpu/operations.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from six.moves import range
2+
13
from .gpuarray import _split, _concatenate, dtype_to_typecode
24
from .dtypes import upcast
35
from . import array, asarray
@@ -83,8 +85,8 @@ def array_split(ary, indices_or_sections, axis=0):
8385
neach, extra = divmod(ary.shape[axis], nsec)
8486
# this madness is to support the numpy interface
8587
# it is supported by tests, but little else
86-
divs = list(range(neach + 1, (neach + 1) * extra + 1, neach + 1) +
87-
range((neach + 1) * extra + neach, ary.shape[axis], neach))
88+
divs = (list(range(neach + 1, (neach + 1) * extra + 1, neach + 1)) +
89+
list(range((neach + 1) * extra + neach, ary.shape[axis], neach)))
8890
res = _split(ary, divs, axis)
8991
return _replace_0_with_empty(res, ary)
9092

‎pygpu/reduction.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def _ceil_log2(x):
103103
104104
<% cur_size = local_size %>
105105
% while cur_size > 1:
106-
<% cur_size = cur_size / 2 %>
106+
<% cur_size = cur_size // 2 %>
107107
local_barrier();
108108
if (lid < ${cur_size}) {
109109
ldata[lid] = REDUCE(ldata[lid], ldata[lid+${cur_size}]);
@@ -180,15 +180,15 @@ def __init__(self, context, dtype_out, neutral, reduce_expr, redux,
180180
def _find_kernel_ls(self, tmpl, max_ls, *tmpl_args):
181181
local_size = min(self.init_local_size, max_ls)
182182
count_lim = _ceil_log2(local_size)
183-
local_size = 2**count_lim
183+
local_size = int(2**count_lim)
184184
loop_count = 0
185185
while loop_count <= count_lim:
186186
k, src, spec = tmpl(local_size, *tmpl_args)
187187

188188
if local_size <= k.maxlsize:
189189
return k, src, spec, local_size
190190
else:
191-
local_size /= 2
191+
local_size //= 2
192192

193193
loop_count += 1
194194

‎pygpu/tests/support.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import print_function
2+
13
import os, sys
24
import numpy
35
from nose.plugins.skip import SkipTest
@@ -30,7 +32,7 @@ def get_env_dev():
3032

3133

3234
context = gpuarray.init(get_env_dev())
33-
print >>sys.stderr, "*** Testing for", context.devname
35+
print("*** Testing for", context.devname, file=sys.stderr)
3436

3537

3638
def guard_devsup(func):

‎pygpu/tests/test_elemwise.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@
55
from pygpu.elemwise import ElemwiseKernel
66
from pygpu.tools import check_args, ArrayArg, ScalarArg
77

8+
from six import PY2
9+
810
from .support import (guard_devsup, rand, check_flags, check_meta, check_all,
911
context, gen_gpuarray, dtypes_no_complex,
1012
check_meta_content)
1113

1214

1315
operators1 = [operator.neg, operator.pos, operator.abs]
14-
operators2 = [operator.add, operator.sub, operator.div, operator.floordiv,
16+
operators2 = [operator.add, operator.sub, operator.floordiv,
1517
operator.mod, operator.mul, operator.truediv,
1618
operator.eq, operator.ne, operator.lt, operator.le,
1719
operator.gt, operator.ge]
18-
ioperators2 = [operator.iadd, operator.isub, operator.idiv, operator.ifloordiv,
20+
if PY2:
21+
operators2.append(operators.div)
22+
23+
ioperators2 = [operator.iadd, operator.isub, operator.ifloordiv,
1924
operator.imod, operator.imul, operator.itruediv]
25+
if PY2:
26+
ioperators2.append(operators.idiv)
27+
2028
elems = [2, 0.3, numpy.asarray(3, dtype='int8'),
2129
numpy.asarray(7, dtype='uint32'),
2230
numpy.asarray(2.45, dtype='float32')]
@@ -276,9 +284,9 @@ def test_elemwise_bool():
276284
exc = None
277285
try:
278286
bool(a)
279-
except ValueError, e:
287+
except ValueError as e:
280288
exc = e
281-
assert e is not None
289+
assert exc is not None
282290
a = gpuarray.zeros((1,), context=context)
283291
assert bool(a) == False
284292
a = gpuarray.zeros((), context=context)

‎pygpu/tests/test_gpu_ndarray.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from __future__ import print_function
2+
13
import copy
4+
from six.moves import range
25

36
import numpy
47

@@ -33,7 +36,7 @@ def test_hash():
3336
exc = None
3437
try:
3538
h = hash(g)
36-
except TypeError, e:
39+
except TypeError as e:
3740
exc = e
3841
assert exc is not None
3942

@@ -177,9 +180,6 @@ def asfortranarray(shp, dtype, offseted_outer, offseted_inner, sliced, order):
177180
if (sliced != 1 or shp == () or (offseted_outer and len(shp) > 1) or
178181
(order != 'f' and len(shp) > 1)):
179182
assert b is not gpu
180-
if (sliced == 1 and not offseted_outer and order != 'c'):
181-
assert ((a.data == cpu.data) ==
182-
(b.gpudata == gpu.gpudata))
183183
else:
184184
assert b is gpu
185185

@@ -390,7 +390,7 @@ def test_transpose():
390390
for order in ['c', 'f']:
391391
for sliced in [1, 2, -2, -1]:
392392
yield transpose, shp, offseted, sliced, order
393-
for perm in permutations(range(len(shp))):
393+
for perm in permutations(list(range(len(shp)))):
394394
yield transpose_perm, shp, perm, offseted, sliced, order
395395

396396

@@ -546,8 +546,8 @@ def _cmp(x,y):
546546
assert x.dtype == y.dtype
547547
assert x.strides == y.strides
548548
if not numpy.all(x == y):
549-
print x
550-
print y
549+
print(x)
550+
print(y)
551551
assert numpy.all(x == y), (x, y)
552552

553553

‎pygpu/tests/test_reduction.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import operator
21
import numpy
32

43
from pygpu import gpuarray, ndgpuarray as elemary
54
from pygpu.reduction import ReductionKernel
65

7-
from .support import (guard_devsup, rand, check_flags, check_meta, check_all,
8-
check_meta_content, context, gen_gpuarray,
6+
from .support import (guard_devsup, check_meta_content, context, gen_gpuarray,
97
dtypes_no_complex_big, dtypes_no_complex)
108

9+
1110
def test_red_array_basic():
1211
for dtype in dtypes_no_complex_big:
1312
for shape, redux in [((10,), [True]),
@@ -28,7 +27,7 @@ def test_red_array_basic():
2827
@guard_devsup
2928
def red_array_sum(dtype, shape, redux):
3029
c, g = gen_gpuarray(shape, dtype, ctx=context)
31-
30+
3231
axes = [i for i in range(len(redux)) if redux[i]]
3332
axes.reverse()
3433
out_c = c
@@ -56,10 +55,11 @@ def test_reduction_ops():
5655
for axis in [None, 0, 1]:
5756
for op in ['all', 'any']:
5857
yield reduction_op, op, 'bool', axis
59-
for op in ['prod', 'sum']: # 'min', 'max']:
58+
for op in ['prod', 'sum']: # 'min', 'max']:
6059
for dtype in dtypes_no_complex:
6160
yield reduction_op, op, dtype, axis
6261

62+
6363
def reduction_op(op, dtype, axis):
6464
c, g = gen_gpuarray((2, 3), dtype=dtype, ctx=context, cls=elemary)
6565

@@ -76,19 +76,20 @@ def reduction_op(op, dtype, axis):
7676

7777
check_meta_content(outg, outc)
7878

79+
7980
def test_reduction_wrong_type():
8081
c, g = gen_gpuarray((2, 3), dtype='float32', ctx=context, cls=elemary)
8182
out1 = gpuarray.empty((2, 3), dtype='int32', context=context)
8283
out2 = gpuarray.empty((3, 2), dtype='float32', context=context)
8384

8485
try:
85-
r = g.sum(out=out1)
86+
g.sum(out=out1)
8687
assert False, "Expected a TypeError out of the sum"
8788
except TypeError:
8889
pass
8990

9091
try:
91-
r = g.sum(out=out2)
92+
g.sum(out=out2)
9293
assert False, "Expected a TypeError out of the sum"
9394
except TypeError:
9495
pass

‎pygpu/tools.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import six
3+
from six.moves import reduce
34

45
from heapq import nsmallest
56
from operator import itemgetter, mul
@@ -38,10 +39,10 @@ def __eq__(self, other):
3839

3940
class ArrayArg(Argument):
4041
def decltype(self):
41-
return "GLOBAL_MEM %s *" % (self.ctype(),)
42+
return "GLOBAL_MEM {} *".format(self.ctype())
4243

4344
def expr(self):
44-
return "%s[i]" % (self.name,)
45+
return "{}[i]".format(self.name)
4546

4647
def isarray(self):
4748
return True
@@ -215,7 +216,7 @@ def wrapper(*key):
215216
# purge least frequently used cache entry
216217
if len(cache) > wrapper.maxsize:
217218
for key, _ in nsmallest(wrapper.maxsize // 10,
218-
use_count.iteritems(),
219+
six.iteritems(use_count),
219220
key=itemgetter(1)):
220221
del cache[key], use_count[key]
221222

@@ -263,7 +264,7 @@ def wrapper(*key):
263264
# purge least recently used cache entries
264265
if len(cache) > wrapper.maxsize:
265266
for key, _ in nsmallest(wrapper.maxsize // 10,
266-
last_use.iteritems(),
267+
six.iteritems(last_use),
267268
key=itemgetter(1)):
268269
del cache[key], last_use[key]
269270

0 commit comments

Comments
 (0)