本文整理汇总了Python中pygpu.gpuarray.empty函数的典型用法代码示例。如果您正苦于以下问题:Python empty函数的具体用法?Python empty怎么用?Python empty使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了empty函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: elemwise_layouts_mixed
def elemwise_layouts_mixed(shape, offseted_outer, offseted_inner, sliced,
order):
ac, ag = gen_gpuarray(shape, dtype='float32', sliced=sliced, order=order,
offseted_outer=offseted_outer,
offseted_inner=offseted_inner, ctx=context)
b = numpy.asarray(2.0, dtype='float32')
outg = gpuarray.empty(shape, dtype='float32', context=context)
k = ElemwiseKernel(context, "float *a, float b, float *c",
"c[i] = a[i] + b")
# will use contig or basic
k(ag, b, outg)
outc = ac + b
assert numpy.allclose(numpy.asarray(outg), outc)
# test basic
outg = gpuarray.empty(shape, dtype='float32', context=context)
k.call_basic(ag, b, outg)
assert numpy.allclose(numpy.asarray(outg), outc)
# test dimspec
outg = gpuarray.empty(shape, dtype='float32', context=context)
k.call_dimspec(ag, b, outg)
assert numpy.allclose(numpy.asarray(outg), outc)
# test specialized
outg = gpuarray.empty(shape, dtype='float32', context=context)
k.call_specialized(ag, b, outg)
assert numpy.allclose(numpy.asarray(outg), outc)
示例2: test_elemwise_bool
def test_elemwise_bool():
a = gpuarray.empty((2,), context=context)
exc = None
try:
bool(a)
except ValueError, e:
exc = e
示例3: test_reduction_wrong_type
def test_reduction_wrong_type():
c, g = gen_gpuarray((2, 3), dtype='float32', ctx=context, cls=elemary)
out1 = gpuarray.empty((2, 3), dtype='int32', context=context)
out2 = gpuarray.empty((3, 2), dtype='float32', context=context)
try:
g.sum(out=out1)
assert False, "Expected a TypeError out of the sum"
except TypeError:
pass
try:
g.sum(out=out2)
assert False, "Expected a TypeError out of the sum"
except TypeError:
pass
示例4: test_hash
def test_hash():
g = gpu_ndarray.empty((2, 3), context=ctx)
exc = None
try:
h = hash(g)
except TypeError, e:
exc = e
示例5: perform
def perform(self, node, inputs, outs):
out, = outs
v = inputs[0]
sh = tuple(map(int, inputs[1:]))
if out[0] is None or out[0].shape != sh:
out[0] = gpuarray.empty(sh, dtype=v.dtype)
out[0][...] = v
示例6: test_elemwise_bool
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)
示例7: perform
def perform(self, node, inputs, outs):
out, = outs
v = inputs[0]
sh = tuple(map(int, inputs[1:]))
if out[0] is None or out[0].shape != sh:
if self.memset_0:
out[0] = gpuarray.zeros(sh, dtype=v.dtype)
else:
out[0] = gpuarray.empty(sh, dtype=v.dtype)
out[0][...] = v
else:
out[0][...] = v
if config.gpuarray.sync:
out[0].sync()
示例8: perform
def perform(self, node, inputs, outs):
out, = outs
v = inputs[0]
sh = tuple(map(int, inputs[1:]))
if out[0] is None or out[0].shape != sh:
if v.size == 1 and numpy.asarray(v)[0].item() == 0:
out[0] = gpuarray.zeros(sh, dtype=v.dtype)
else:
out[0] = gpuarray.empty(sh, dtype=v.dtype)
out[0][...] = v
else:
out[0][...] = v
if config.gpuarray.sync:
out[0].sync()
示例9: reduction_op
def reduction_op(op, dtype, axis):
c, g = gen_gpuarray((2, 3), dtype=dtype, ctx=context, cls=elemary)
rc = getattr(c, op)(axis=axis)
rg = getattr(g, op)(axis=axis)
check_meta_content(rg, rc)
outc = numpy.empty(rc.shape, dtype=rc.dtype)
outg = gpuarray.empty(rg.shape, dtype=rg.dtype, context=context)
rc = getattr(c, op)(axis=axis, out=outc)
rg = getattr(g, op)(axis=axis, out=outg)
check_meta_content(outg, outc)
示例10: check_elemwise2
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)
示例11: elemwise_collapse
def elemwise_collapse(dtype1, dtype2, shape1, shape2, expected):
assert len(shape1) == len(shape2)
# int8 does not cause problematic upcasts
scalar = numpy.asarray(1, dtype='int8')
a_cpu, a_gpu = gen_gpuarray(shape1, dtype1, ctx=context)
b_cpu, b_gpu = gen_gpuarray(shape2, dtype2, ctx=context)
o_shape = []
for i in range(len(shape1)):
o_shape.append(max(shape1[i], shape2[i]))
o = gpuarray.empty(o_shape, dtype=(a_cpu + b_cpu).dtype, context=context)
n, nd, dims, strs, offsets, contig = check_args((a_gpu, b_gpu),
collapse=True,
broadcast=True)
assert nd == expected, (shape1, shape2, dims, nd, expected)
k = ElemwiseKernel(context, [ArrayArg(numpy.dtype(dtype1), 'a'),
ArrayArg(numpy.dtype(dtype2), 'b'),
ArrayArg(o.dtype, 'o')], "o[i] = a[i] + b[i]")
out_cpu = a_cpu + b_cpu
k(a_gpu, b_gpu, o, collapse=True, broadcast=True)
assert numpy.allclose(numpy.asarray(o), out_cpu)
k(a_gpu, b_gpu, o, collapse=False, broadcast=True)
assert numpy.allclose(numpy.asarray(o), out_cpu)
broadcast = any([True for i in shape1 + shape2
if i == 1])
n, nd, dims, strs, offsets, contig = check_args((a_gpu, b_gpu, scalar),
collapse=True,
broadcast=True)
assert nd == expected
k = ElemwiseKernel(context, [ArrayArg(numpy.dtype(dtype1), 'a'),
ArrayArg(numpy.dtype(dtype2), 'b'),
ScalarArg(scalar.dtype, 's'),
ArrayArg(o.dtype, 'o')],
"o[i] = a[i] + b[i] + s")
out_cpu = a_cpu + b_cpu + scalar
k(a_gpu, b_gpu, scalar, o, collapse=True, broadcast=True)
assert numpy.allclose(numpy.asarray(o), out_cpu)
k(a_gpu, b_gpu, scalar, o, collapse=False, broadcast=True)
assert numpy.allclose(numpy.asarray(o), out_cpu)
if expected == 1:
expected2 = 2
else:
expected2 = expected
if len(shape1) != 4:
return
if shape1[0] != 1:
c_cpu, c_gpu = gen_gpuarray(shape1, dtype=dtype1, sliced=2, ctx=context)
n, nd, dims, strs, offsets,contig = check_args((c_gpu, b_gpu),
collapse=True,
broadcast=True)
if broadcast:
assert nd >= expected
else:
assert nd == expected2
示例12: gpu_alloc_expected
def gpu_alloc_expected(x, *shp):
g = gpuarray.empty(shp, dtype=x.dtype, context=get_context(test_ctx_name))
g[:] = x
return g
示例13: test_reduce_scatter
def test_reduce_scatter(self):
texp = self.size * np.arange(5 * self.size) + sum(range(self.size))
exp = texp[self.rank * 5:self.rank * 5 + 5]
# order c
cpu = np.arange(5 * self.size) + self.rank
np.reshape(cpu, (self.size, 5), order='C')
gpu = gpuarray.asarray(cpu, context=self.ctx)
resgpu = gpuarray.empty((5,), dtype='int64', order='C', context=self.ctx)
self.gpucomm.reduce_scatter(gpu, 'sum', resgpu)
assert np.allclose(resgpu, exp)
# order f
cpu = np.arange(5 * self.size) + self.rank
np.reshape(cpu, (5, self.size), order='F')
gpu = gpuarray.asarray(cpu, context=self.ctx)
resgpu = gpuarray.empty((5,), dtype='int64', order='F', context=self.ctx)
self.gpucomm.reduce_scatter(gpu, 'sum', resgpu)
assert np.allclose(resgpu, exp)
# make result order c (one less dim)
cpu = np.arange(5 * self.size) + self.rank
np.reshape(cpu, (self.size, 5), order='C')
gpu = gpuarray.asarray(cpu, context=self.ctx)
resgpu = self.gpucomm.reduce_scatter(gpu, 'sum')
check_all(resgpu, exp)
assert resgpu.flags['C_CONTIGUOUS'] is True
# c-contiguous split problem (for size == 1, it can always be split)
if self.size != 1:
cpu = np.arange(5 * (self.size + 1), dtype='int32') + self.rank
np.reshape(cpu, (self.size + 1, 5), order='C')
gpu = gpuarray.asarray(cpu, context=self.ctx)
with self.assertRaises(TypeError):
resgpu = self.gpucomm.reduce_scatter(gpu, 'sum')
# make result order f (one less dim)
cpu = np.arange(5 * self.size) + self.rank
np.reshape(cpu, (5, self.size), order='F')
gpu = gpuarray.asarray(cpu, context=self.ctx)
resgpu = self.gpucomm.reduce_scatter(gpu, 'sum')
check_all(resgpu, exp)
assert resgpu.flags['F_CONTIGUOUS'] is True
# f-contiguous split problem (for size == 1, it can always be split)
if self.size != 1:
cpu = np.arange(5 * (self.size + 1), dtype='int32') + self.rank
np.reshape(cpu, (5, self.size + 1), order='F')
gpu = gpuarray.asarray(cpu, context=self.ctx)
with self.assertRaises(TypeError):
resgpu = self.gpucomm.reduce_scatter(gpu, 'sum')
# make result order c (same dim - less size)
texp = self.size * np.arange(5 * self.size * 3) + sum(range(self.size))
exp = texp[self.rank * 15:self.rank * 15 + 15]
np.reshape(exp, (3, 5), order='C')
cpu = np.arange(5 * self.size * 3) + self.rank
np.reshape(cpu, (self.size * 3, 5), order='C')
gpu = gpuarray.asarray(cpu, context=self.ctx)
resgpu = self.gpucomm.reduce_scatter(gpu, 'sum')
check_all(resgpu, exp)
assert resgpu.flags['C_CONTIGUOUS'] is True
# make result order f (same dim - less size)
texp = self.size * np.arange(5 * self.size * 3) + sum(range(self.size))
exp = texp[self.rank * 15:self.rank * 15 + 15]
np.reshape(exp, (5, 3), order='F')
cpu = np.arange(5 * self.size * 3) + self.rank
np.reshape(cpu, (5, self.size * 3), order='F')
gpu = gpuarray.asarray(cpu, context=self.ctx)
resgpu = self.gpucomm.reduce_scatter(gpu, 'sum')
check_all(resgpu, exp)
assert resgpu.flags['F_CONTIGUOUS'] is True
示例14: test_empty_no_params
def test_empty_no_params():
try:
gpu_ndarray.empty()
assert False
except TypeError:
pass
示例15: test_empty_no_dtype
def test_empty_no_dtype():
x = gpu_ndarray.empty((), context=ctx)# no dtype and order param
y = numpy.empty(())
check_meta(x, y)