1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
import operator
import numpy
from mako.template import Template
from unittest import TestCase
from pygpu import gpuarray, ndgpuarray as elemary
from pygpu.dtypes import dtype_to_ctype, get_common_dtype
from pygpu.elemwise import as_argument, ielemwise2
from pygpu._elemwise import GpuElemwise, arg
from six import PY2
from .support import (guard_devsup, context, gen_gpuarray, check_meta_content)
dtypes_test = ['float32', 'int8', 'uint64']
operators1 = [operator.neg, operator.pos, operator.abs]
operators2 = [operator.add, operator.sub, operator.floordiv,
operator.mod, operator.mul, operator.truediv,
operator.eq, operator.ne, operator.lt, operator.le,
operator.gt, operator.ge]
if PY2:
operators2.append(operator.div)
ioperators2 = [operator.iadd, operator.isub, operator.ifloordiv,
operator.imod, operator.imul, operator.itruediv]
if PY2:
ioperators2.append(operator.idiv)
elems = [2, 0.3, numpy.asarray(3, dtype='int8'),
numpy.asarray(7, dtype='uint32'),
numpy.asarray(2.45, dtype='float32')]
def test_elemwise1_ops_array():
for op in operators1:
for dtype in dtypes_test:
elemwise1_ops_array(op, dtype)
@guard_devsup
def elemwise1_ops_array(op, dtype):
c, g = gen_gpuarray((50,), dtype, ctx=context, cls=elemary)
out_c = op(c)
out_g = op(g)
assert out_c.shape == out_g.shape
assert out_c.dtype == out_g.dtype
assert numpy.allclose(out_c, numpy.asarray(out_g))
def test_elemwise2_ops_array():
for op in operators2:
for dtype1 in dtypes_test:
for dtype2 in dtypes_test:
elemwise2_ops_array(op, dtype1, dtype2, (50,))
def test_ielemwise2_ops_array():
for op in ioperators2:
for dtype1 in dtypes_test:
for dtype2 in dtypes_test:
ielemwise2_ops_array(op, dtype1, dtype2, (50,))
class test_elemwise_output_not_broadcasted(TestCase):
def test_all(self):
test_values = [((1, 4), (6, 4)), ((2, 1, 8, 7), (2, 2, 8, 7))]
for shapea, shapeb in test_values:
# Sould fail: dimensions are not all equal.
self.assertRaises(ValueError, self.run_ielemwise2, shapea, shapeb,
False)
# Should fail: broascast should not be done on output.
self.assertRaises(ValueError, self.run_ielemwise2, shapea, shapeb,
True)
# Should fail: dimensions are not all equal.
self.assertRaises(ValueError, self.check_elemwise2, shapeb, shapeb,
shapea, False)
# Should fail: broadcast should not be done on output.
self.assertRaises(ValueError, self.check_elemwise2, shapeb, shapeb,
shapea, True)
# Should pass: output would be done on read-only input.
self.run_ielemwise2(shapeb, shapea, broadcast=True)
# Should pass: output would be done on read-only inputs.
self.check_elemwise2(shapea, shapea, shapeb, broadcast=True)
self.check_elemwise2(shapea, shapeb, shapeb, broadcast=True)
self.check_elemwise2(shapeb, shapea, shapeb, broadcast=True)
@guard_devsup
def run_ielemwise2(self, shapea, shapeb, broadcast=True):
na, ga = gen_gpuarray(shapea, ctx=context, cls=elemary)
nb, gb = gen_gpuarray(shapeb, ctx=context, cls=elemary)
ielemwise2(ga, '+', gb, broadcast=broadcast)
na += nb
assert numpy.allclose(na, numpy.asarray(ga), atol=1e-6)
@guard_devsup
def check_elemwise2(self, shapea, shapeb, output_shape, broadcast=True):
# We rewrite this version of elemwise2 to skip the scaling of output
# that is done in the official elemwise2 function.
na, ga = gen_gpuarray(shapea, ctx=context, cls=elemary)
nb, gb = gen_gpuarray(shapeb, ctx=context, cls=elemary)
odtype = get_common_dtype(ga, gb, True)
res = gpuarray.empty(output_shape, dtype=odtype, context=ga.context,
cls=ga.__class__)
a_arg = as_argument(ga, 'a', read=True)
b_arg = as_argument(gb, 'b', read=True)
res_arg = as_argument(res, 'res', write=True)
args = [res_arg, a_arg, b_arg]
oper = "res = (%(out_t)s)a %(op)s (%(out_t)s)b" % {
'op': '+', 'out_t': dtype_to_ctype(odtype)}
k = GpuElemwise(ga.context, oper, args, convert_f16=True)
k(res, ga, gb, broadcast=broadcast)
nres = na + nb
assert numpy.allclose(nres, numpy.asarray(res), atol=1e-6)
@guard_devsup
def elemwise2_ops_array(op, dtype1, dtype2, shape):
ac, ag = gen_gpuarray(shape, dtype1, ctx=context, cls=elemary)
bc, bg = gen_gpuarray(shape, dtype2, nozeros=True, ctx=context,
cls=elemary)
out_c = op(ac, bc)
out_g = op(ag, bg)
assert out_c.shape == out_g.shape
assert out_c.dtype == out_g.dtype
assert numpy.allclose(out_c, numpy.asarray(out_g))
@guard_devsup
def ielemwise2_ops_array(op, dtype1, dtype2, shape):
incr = 0
if op == operator.isub and dtype1[0] == 'u':
# array elements are smaller than 10 by default, so we avoid underflow
incr = 10
ac, ag = gen_gpuarray(shape, dtype1, incr=incr, ctx=context,
cls=elemary)
bc, bg = gen_gpuarray(shape, dtype2, nozeros=True, ctx=context,
cls=elemary)
try:
out_c = op(ac, bc)
except TypeError:
# TODO: currently, we use old Numpy semantic and tolerate more case.
# So we can't test that we raise the same error
return
out_g = op(ag, bg)
assert out_g is ag
assert numpy.allclose(out_c, numpy.asarray(out_g), atol=1e-6)
def test_elemwise_f16():
elemwise1_ops_array(operator.neg, 'float16')
elemwise2_ops_array(operator.add, 'float16', 'float16', (50,))
ielemwise2_ops_array(operator.iadd, 'float16', 'float16', (50,))
def test_elemwise2_ops_mixed():
for op in operators2:
for dtype in dtypes_test:
for elem in elems:
elemwise2_ops_mixed(op, dtype, (50,), elem)
def test_ielemwise2_ops_mixed():
for op in ioperators2:
for dtype in dtypes_test:
for elem in elems:
ielemwise2_ops_mixed(op, dtype, (50,), elem)
@guard_devsup
def elemwise2_ops_mixed(op, dtype, shape, elem):
c, g = gen_gpuarray(shape, dtype, ctx=context, cls=elemary)
out_c = op(c, elem)
out_g = op(g, elem)
assert out_c.shape == out_g.shape
assert out_c.dtype == out_g.dtype
assert numpy.allclose(out_c, numpy.asarray(out_g))
c, g = gen_gpuarray(shape, dtype, nozeros=True, ctx=context,
cls=elemary)
out_c = op(elem, c)
out_g = op(elem, g)
assert out_c.shape == out_g.shape
assert out_c.dtype == out_g.dtype
assert numpy.allclose(out_c, numpy.asarray(out_g))
@guard_devsup
def ielemwise2_ops_mixed(op, dtype, shape, elem):
incr = 0
if op == operator.isub and dtype[0] == 'u':
# array elements are smaller than 10 by default, so we avoid underflow
incr = 10
c, g = gen_gpuarray(shape, dtype, incr=incr, ctx=context,
cls=elemary)
try:
out_c = op(c, elem)
except TypeError:
# TODO: currently, we use old Numpy semantic and tolerate more case.
# So we can't test that we raise the same error
return
out_g = op(g, elem)
assert out_g is g
assert out_c.shape == out_g.shape
assert out_c.dtype == out_g.dtype
assert numpy.allclose(out_c, numpy.asarray(out_g))
def test_divmod():
for dtype1 in dtypes_test:
for dtype2 in dtypes_test:
divmod_array(dtype1, dtype2, (50,))
for dtype in dtypes_test:
for elem in elems:
divmod_mixed(dtype, (50,), elem)
@guard_devsup
def divmod_array(dtype1, dtype2, shape):
ac, ag = gen_gpuarray(shape, dtype1, ctx=context, cls=elemary)
bc, bg = gen_gpuarray(shape, dtype2, nozeros=True, ctx=context,
cls=elemary)
out_c = divmod(ac, bc)
out_g = divmod(ag, bg)
assert out_c[0].shape == out_g[0].shape
assert out_c[1].shape == out_g[1].shape
assert out_c[0].dtype == out_g[0].dtype
assert out_c[1].dtype == out_g[1].dtype
assert numpy.allclose(out_c[0], numpy.asarray(out_g[0]))
assert numpy.allclose(out_c[1], numpy.asarray(out_g[1]))
@guard_devsup
def divmod_mixed(dtype, shape, elem):
c, g = gen_gpuarray(shape, dtype, nozeros=True, ctx=context,
cls=elemary)
out_c = divmod(c, elem)
out_g = divmod(g, elem)
assert out_c[0].shape == out_g[0].shape
assert out_c[1].shape == out_g[1].shape
assert out_c[0].dtype == out_g[0].dtype
assert out_c[1].dtype == out_g[1].dtype
assert numpy.allclose(out_c[0], numpy.asarray(out_g[0]))
assert numpy.allclose(out_c[1], numpy.asarray(out_g[1]))
out_c = divmod(elem, c)
out_g = divmod(elem, g)
assert out_c[0].shape == out_g[0].shape
assert out_c[1].shape == out_g[1].shape
assert out_c[0].dtype == out_g[0].dtype
assert out_c[1].dtype == out_g[1].dtype
assert numpy.allclose(out_c[0], numpy.asarray(out_g[0]))
assert numpy.allclose(out_c[1], numpy.asarray(out_g[1]))
def test_elemwise_bool():
a = gpuarray.empty((2,), context=context)
exc = None
try:
bool(a)
except ValueError as e:
exc = e
assert exc is not None
a = gpuarray.zeros((1,), context=context)
assert not bool(a)
a = gpuarray.zeros((), context=context)
assert not bool(a)
def test_broadcast():
for shapea, shapeb in [((3, 5), (3, 5)),
((1, 5), (3, 5)),
((3, 5), (3, 1)),
((1, 5), (3, 1)),
((3, 1), (3, 5)),
((3, 5), (3, 1)),
((1, 1), (1, 1)),
((3, 4, 5), (4, 5)),
((4, 5), (3, 4, 5)),
((), ())]:
broadcast(shapea, shapeb)
def broadcast(shapea, shapeb):
ac, ag = gen_gpuarray(shapea, 'float32', ctx=context, cls=elemary)
bc, bg = gen_gpuarray(shapeb, 'float32', ctx=context, cls=elemary)
rc = ac + bc
rg = ag + bg
check_meta_content(rg, rc)
_inf_preamb_tpl = Template('''
WITHIN_KERNEL ${flt}
infinity() {return INFINITY;}
WITHIN_KERNEL ${flt}
neg_infinity() {return -INFINITY;}
''')
def test_infinity():
for dtype in ['float32', 'float64']:
infinity(dtype)
@guard_devsup
def infinity(dtype):
ac, ag = gen_gpuarray((2,), dtype, ctx=context, cls=elemary)
out_g = ag._empty_like_me()
flt = 'ga_float' if dtype == 'float32' else 'ga_double'
out_arg = arg('out', out_g.dtype, scalar=False, read=False, write=True)
preamble = _inf_preamb_tpl.render(flt=flt)
# +infinity
ac[:] = numpy.inf
expr_inf = 'out = infinity()'
kernel = GpuElemwise(context, expr_inf, [out_arg],
preamble=preamble)
kernel(out_g)
assert numpy.array_equal(ac, numpy.asarray(out_g))
# -infinity
ac[:] = -numpy.inf
expr_neginf = 'out = neg_infinity()'
kernel = GpuElemwise(context, expr_neginf, [out_arg],
preamble=preamble)
kernel(out_g)
assert numpy.array_equal(ac, numpy.asarray(out_g))
|