Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
fcc0ae39
提交
fcc0ae39
authored
9月 16, 2016
作者:
Amjad Almahairi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
allowing for more dtypes
上级
a116149c
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
39 行增加
和
27 行删除
+39
-27
multinomial.py
theano/gpuarray/multinomial.py
+9
-14
test_multinomial.py
theano/gpuarray/tests/test_multinomial.py
+30
-13
没有找到文件。
theano/gpuarray/multinomial.py
浏览文件 @
fcc0ae39
...
@@ -52,11 +52,6 @@ class GPUAMultinomialFromUniform(GpuKernelBase, Op):
...
@@ -52,11 +52,6 @@ class GPUAMultinomialFromUniform(GpuKernelBase, Op):
odtype
=
pvals
.
dtype
odtype
=
pvals
.
dtype
else
:
else
:
odtype
=
self
.
odtype
odtype
=
self
.
odtype
assert
odtype
==
'float32'
,
odtype
if
odtype
!=
pvals
.
dtype
:
raise
NotImplementedError
(
'GpuMultinomialFromUniform works only if '
'self.odtype == pvals.dtype'
,
odtype
,
pvals
.
dtype
)
br
=
(
pvals
.
broadcastable
[
1
],
pvals
.
broadcastable
[
0
])
br
=
(
pvals
.
broadcastable
[
1
],
pvals
.
broadcastable
[
0
])
out
=
GpuArrayType
(
broadcastable
=
br
,
out
=
GpuArrayType
(
broadcastable
=
br
,
dtype
=
odtype
,
dtype
=
odtype
,
...
@@ -74,7 +69,7 @@ KERNEL void k_multi_warp_multinomial(
...
@@ -74,7 +69,7 @@ KERNEL void k_multi_warp_multinomial(
const ga_ssize pvals_col_stride,
const ga_ssize pvals_col_stride,
GLOBAL_MEM float * global_unis,
GLOBAL_MEM float * global_unis,
const ga_ssize unis_stride,
const ga_ssize unis_stride,
GLOBAL_MEM
float
* global_outs,
GLOBAL_MEM
%(out_ctype)
s
* global_outs,
const ga_ssize outs_row_stride,
const ga_ssize outs_row_stride,
const ga_ssize outs_col_stride
const ga_ssize outs_col_stride
)
)
...
@@ -88,14 +83,14 @@ KERNEL void k_multi_warp_multinomial(
...
@@ -88,14 +83,14 @@ KERNEL void k_multi_warp_multinomial(
const float unis_n = global_unis[n*unis_stride];
const float unis_n = global_unis[n*unis_stride];
for (ga_size m = 0; m < nb_outcomes; ++m)
for (ga_size m = 0; m < nb_outcomes; ++m)
{
{
float current_out = 0.
;
%(out_ctype)
s current_out = 0
;
if (!done)
if (!done)
{
{
cummul += global_pvals[m * pvals_col_stride +
cummul += global_pvals[m * pvals_col_stride +
n * pvals_row_stride];
n * pvals_row_stride];
if (unis_n < cummul)
if (unis_n < cummul)
{
{
current_out = 1
.
;
current_out = 1;
done = true;
done = true;
}
}
}
}
...
@@ -105,7 +100,7 @@ KERNEL void k_multi_warp_multinomial(
...
@@ -105,7 +100,7 @@ KERNEL void k_multi_warp_multinomial(
}
}
}
}
}
}
"""
"""
%
dict
(
out_ctype
=
pygpu
.
gpuarray
.
dtype_to_ctype
(
node
.
outputs
[
0
]
.
dtype
))
return
[
Kernel
(
return
[
Kernel
(
code
=
code
,
name
=
"k_multi_warp_multinomial"
,
code
=
code
,
name
=
"k_multi_warp_multinomial"
,
params
=
[
pygpu
.
gpuarray
.
SIZE
,
params
=
[
pygpu
.
gpuarray
.
SIZE
,
...
@@ -128,6 +123,7 @@ KERNEL void k_multi_warp_multinomial(
...
@@ -128,6 +123,7 @@ KERNEL void k_multi_warp_multinomial(
ctx
=
sub
[
'params'
]
ctx
=
sub
[
'params'
]
sync
=
bool
(
config
.
gpuarray
.
sync
)
sync
=
bool
(
config
.
gpuarray
.
sync
)
kname
=
self
.
gpu_kernels
(
node
,
name
)[
0
]
.
objvar
kname
=
self
.
gpu_kernels
(
node
,
name
)[
0
]
.
objvar
out_typecode
=
pygpu
.
gpuarray
.
dtype_to_typecode
(
node
.
outputs
[
0
]
.
dtype
)
s
=
"""
s
=
"""
PyGpuArrayObject * pvals =
%(pvals)
s;
PyGpuArrayObject * pvals =
%(pvals)
s;
PyGpuArrayObject * unis =
%(unis)
s;
PyGpuArrayObject * unis =
%(unis)
s;
...
@@ -152,7 +148,7 @@ KERNEL void k_multi_warp_multinomial(
...
@@ -152,7 +148,7 @@ KERNEL void k_multi_warp_multinomial(
dims[0] = PyGpuArray_DIMS(pvals)[1];
dims[0] = PyGpuArray_DIMS(pvals)[1];
dims[1] = PyGpuArray_DIMS(pvals)[0];
dims[1] = PyGpuArray_DIMS(pvals)[0];
if (theano_prep_output(&out, 2, dims,
unis->ga.typecode
,
if (theano_prep_output(&out, 2, dims,
%(out_typecode)
s
,
GA_C_ORDER,
%(ctx)
s) != 0){
GA_C_ORDER,
%(ctx)
s) != 0){
%(fail)
s
%(fail)
s
}
}
...
@@ -194,8 +190,8 @@ KERNEL void k_multi_warp_multinomial(
...
@@ -194,8 +190,8 @@ KERNEL void k_multi_warp_multinomial(
PyGpuArray_STRIDES(pvals)[0]/sizeof(float),
PyGpuArray_STRIDES(pvals)[0]/sizeof(float),
PyGpuArray_STRIDES(pvals)[1]/sizeof(float),
PyGpuArray_STRIDES(pvals)[1]/sizeof(float),
PyGpuArray_STRIDES(unis)[0]/sizeof(float),
PyGpuArray_STRIDES(unis)[0]/sizeof(float),
PyGpuArray_STRIDES(out)[0]/
sizeof(float
),
PyGpuArray_STRIDES(out)[0]/
gpuarray_get_elsize(
%(out_typecode)
s
),
PyGpuArray_STRIDES(out)[1]/
sizeof(float
)
PyGpuArray_STRIDES(out)[1]/
gpuarray_get_elsize(
%(out_typecode)
s
)
};
};
int err;
int err;
args[0] = (void*)&PyGpuArray_DIMS(out)[1];
args[0] = (void*)&PyGpuArray_DIMS(out)[1];
...
@@ -226,7 +222,7 @@ KERNEL void k_multi_warp_multinomial(
...
@@ -226,7 +222,7 @@ KERNEL void k_multi_warp_multinomial(
return
s
return
s
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
1
,)
return
(
2
,)
class
GPUAMultinomialWOReplacementFromUniform
(
GpuKernelBase
,
Op
):
class
GPUAMultinomialWOReplacementFromUniform
(
GpuKernelBase
,
Op
):
...
@@ -479,7 +475,6 @@ def local_gpua_multinomial(op, context_name, inputs, outputs):
...
@@ -479,7 +475,6 @@ def local_gpua_multinomial(op, context_name, inputs, outputs):
except
NotScalarConstantError
:
except
NotScalarConstantError
:
return
None
return
None
m
,
=
outputs
m
,
=
outputs
if
(
p
.
dtype
==
u
.
dtype
==
m
.
dtype
==
'float32'
):
gpu_op
=
GPUAMultinomialFromUniform
(
op
.
odtype
)
gpu_op
=
GPUAMultinomialFromUniform
(
op
.
odtype
)
return
GpuDimShuffle
([
False
,
False
],
[
1
,
0
])(
return
GpuDimShuffle
([
False
,
False
],
[
1
,
0
])(
gpu_op
(
p
,
u
))
gpu_op
(
p
,
u
))
...
...
theano/gpuarray/tests/test_multinomial.py
浏览文件 @
fcc0ae39
...
@@ -80,15 +80,12 @@ def test_multinomial_large():
...
@@ -80,15 +80,12 @@ def test_multinomial_large():
utt
.
assert_allclose
(
mval
,
asdf
)
# broadcast over all rows
utt
.
assert_allclose
(
mval
,
asdf
)
# broadcast over all rows
def
test_gpu_opt
():
def
test_gpu_opt
_dtypes
():
# Does have some overlap with test_multinomial_0
# Does have some overlap with test_multinomial_0
for
dtype
in
[
'float32'
,
'int64'
,
'float64'
]:
# We test the case where we put the op on the gpu when the output
# is moved to the gpu.
p
=
tensor
.
fmatrix
()
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
u
=
tensor
.
fvector
()
m
=
theano
.
sandbox
.
multinomial
.
MultinomialFromUniform
(
'auto'
)(
p
,
u
)
m
=
theano
.
sandbox
.
multinomial
.
MultinomialFromUniform
(
dtype
)(
p
,
u
)
assert
m
.
dtype
==
'float32'
,
m
.
dtype
f
=
function
([
p
,
u
],
m
,
allow_input_downcast
=
True
,
mode
=
mode_with_gpu
)
f
=
function
([
p
,
u
],
m
,
allow_input_downcast
=
True
,
mode
=
mode_with_gpu
)
assert
any
([
type
(
node
.
op
)
is
GPUAMultinomialFromUniform
assert
any
([
type
(
node
.
op
)
is
GPUAMultinomialFromUniform
...
@@ -96,21 +93,41 @@ def test_gpu_opt():
...
@@ -96,21 +93,41 @@ def test_gpu_opt():
pval
=
numpy
.
arange
(
10000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
pval
=
numpy
.
arange
(
10000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
uval
=
numpy
.
ones_like
(
pval
[:,
0
])
*
0.5
uval
=
numpy
.
ones_like
(
pval
[:,
0
])
*
0.5
f
(
pval
,
uval
)
samples
=
f
(
pval
,
uval
)
assert
samples
.
dtype
==
dtype
,
"
%
s !=
%
s"
%
(
samples
.
dtype
,
dtype
)
# Test with a row, it was failing in the past.
r
=
tensor
.
frow
()
m
=
theano
.
sandbox
.
multinomial
.
MultinomialFromUniform
(
'auto'
)(
r
,
u
)
assert
m
.
dtype
==
'float32'
,
m
.
dtype
f
=
function
([
r
,
u
],
m
,
allow_input_downcast
=
True
,
mode
=
mode_with_gpu
)
def
test_gpu_opt
():
# Does have some overlap with test_multinomial_0
# We test the case where we put the op on the gpu when the output
# is moved to the gpu.
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
m
=
theano
.
sandbox
.
multinomial
.
MultinomialFromUniform
(
'float32'
)(
p
,
u
)
# assert m.dtype == 'float32', m.dtype
f
=
function
([
p
,
u
],
m
,
allow_input_downcast
=
True
,
mode
=
mode_with_gpu
)
assert
any
([
type
(
node
.
op
)
is
GPUAMultinomialFromUniform
assert
any
([
type
(
node
.
op
)
is
GPUAMultinomialFromUniform
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
for
node
in
f
.
maker
.
fgraph
.
toposort
()])
pval
=
numpy
.
arange
(
1
*
4
,
dtype
=
'float32'
)
.
reshape
((
1
,
4
))
+
0.1
pval
=
numpy
.
arange
(
1
0000
*
4
,
dtype
=
'float32'
)
.
reshape
((
10000
,
4
))
+
0.1
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
pval
=
pval
/
pval
.
sum
(
axis
=
1
)[:,
None
]
uval
=
numpy
.
ones_like
(
pval
[:,
0
])
*
0.5
uval
=
numpy
.
ones_like
(
pval
[:,
0
])
*
0.5
f
(
pval
,
uval
)
f
(
pval
,
uval
)
# # Test with a row, it was failing in the past.
# r = tensor.frow()
# m = theano.sandbox.multinomial.MultinomialFromUniform('auto')(r, u)
# assert m.dtype == 'float32', m.dtype
# f = function([r, u], m, allow_input_downcast=True, mode=mode_with_gpu)
# assert any([type(node.op) is GPUAMultinomialFromUniform
# for node in f.maker.fgraph.toposort()])
# pval = numpy.arange(1 * 4, dtype='float32').reshape((1, 4)) + 0.1
# pval = pval / pval.sum(axis=1)[:, None]
# uval = numpy.ones_like(pval[:, 0]) * 0.5
# f(pval, uval)
class
test_OP_wor
(
unittest
.
TestCase
):
class
test_OP_wor
(
unittest
.
TestCase
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论