Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f1a3d267
提交
f1a3d267
authored
4月 20, 2016
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Code clean up following code review
上级
d6697f9f
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
2 行增加
和
146 行删除
+2
-146
multinomial.c
theano/sandbox/gpuarray/multinomial.c
+0
-135
multinomial.py
theano/sandbox/gpuarray/multinomial.py
+2
-11
没有找到文件。
theano/sandbox/gpuarray/multinomial.c
deleted
100644 → 0
浏览文件 @
d6697f9f
#section support_code_apply
static
__global__
void
k_multi_warp_multinomial
(
const
int
nb_multi
,
const
int
nb_outcomes
,
float
*
global_pvals
,
const
int
pvals_row_stride
,
const
int
pvals_col_stride
,
float
*
global_unis
,
const
int
unis_stride
,
float
*
global_outs
,
const
int
outs_row_stride
,
const
int
outs_col_stride
)
{
// each thread takes care of one multinomial draw
int
n
=
blockDim
.
x
*
blockIdx
.
x
+
threadIdx
.
x
;
if
(
n
<
nb_multi
)
{
float
cummul
=
0
.;
bool
done
=
false
;
const
float
unis_n
=
global_unis
[
n
*
unis_stride
];
for
(
int
m
=
0
;
m
<
nb_outcomes
;
++
m
)
{
float
current_out
=
0
.;
if
(
!
done
)
{
cummul
+=
global_pvals
[
m
*
pvals_col_stride
+
n
*
pvals_row_stride
];
if
(
unis_n
<
cummul
)
{
current_out
=
1
.;
done
=
true
;
}
}
//write out transposed for speed.
global_outs
[
n
*
outs_col_stride
+
m
*
outs_row_stride
]
=
current_out
;
}
}
}
#section support_code_struct
int
APPLY_SPECIFIC
(
multinomial
)(
PyGpuArrayObject
*
pvals
,
PyGpuArrayObject
*
unis
,
PyGpuArrayObject
**
out
,
PyGpuContextObject
*
c
)
{
size_t
dims
[
2
];
if
(
PyGpuArray_NDIM
(
pvals
)
!=
2
)
{
PyErr_Format
(
PyExc_TypeError
,
"pvals wrong rank"
);
return
1
;
}
if
(
PyGpuArray_NDIM
(
unis
)
!=
1
)
{
PyErr_Format
(
PyExc_TypeError
,
"unis wrong rank"
);
return
1
;
}
if
(
PyGpuArray_DIMS
(
unis
)[
0
]
!=
PyGpuArray_DIMS
(
pvals
)[
0
])
{
PyErr_Format
(
PyExc_ValueError
,
"unis.shape[0] != pvals.shape[0]"
);
return
1
;
}
dims
[
0
]
=
PyGpuArray_DIMS
(
pvals
)[
1
];
dims
[
1
]
=
PyGpuArray_DIMS
(
pvals
)[
0
];
if
(
theano_prep_output
(
out
,
2
,
dims
,
unis
->
ga
.
typecode
,
GA_C_ORDER
,
c
)
!=
0
)
return
1
;
GpuArray_memset
(
&
((
*
out
)
->
ga
),
0
);
{
// NESTED SCOPE
int
nb_multi
=
PyGpuArray_DIMS
(
pvals
)[
0
];
int
nb_outcomes
=
PyGpuArray_DIMS
(
pvals
)[
1
];
//TODO : change this for a beautiful constant
int
max_nb_blocks
=
2
<<
15
-
1
;
int
nb_blocks
=
max_nb_blocks
+
1
;
int
nb_threads
=
16
;
// so it really starts at 32, because of the *2
do
{
nb_threads
*=
2
;
if
(
nb_multi
%
nb_threads
==
0
)
nb_blocks
=
nb_multi
/
nb_threads
;
else
nb_blocks
=
(
int
)((
float
)
nb_multi
/
(
float
)
nb_threads
+
1
.);
}
while
(
nb_blocks
>
max_nb_blocks
);
//printf("\\nN=%i b=%i t=%i t*b=%i", nb_multi, nb_blocks, nb_threads, nb_blocks*nb_threads);
// TODO : next line is a bit hardcoded...
if
(
nb_threads
>
512
)
{
PyErr_Format
(
PyExc_ValueError
,
"Multinomial is not implemented for so many rows in the matrix (%i)"
,
nb_multi
);
return
1
;
}
dim3
n_blocks
(
nb_blocks
,
1
,
1
);
dim3
n_threads
(
nb_threads
,
1
,
1
);
int
n_shared
=
0
;
assert
(
nb_blocks
*
nb_threads
>=
nb_multi
);
k_multi_warp_multinomial
<<<
n_blocks
,
n_threads
,
n_shared
>>>
(
PyGpuArray_DIMS
(
*
out
)[
1
],
PyGpuArray_DIMS
(
*
out
)[
0
],
(
float
*
)
PyGpuArray_DEV_DATA
(
pvals
),
PyGpuArray_STRIDES
(
pvals
)[
0
]
/
sizeof
(
float
),
PyGpuArray_STRIDES
(
pvals
)[
1
]
/
sizeof
(
float
),
(
float
*
)
PyGpuArray_DEV_DATA
(
unis
),
PyGpuArray_STRIDES
(
unis
)[
0
]
/
sizeof
(
float
),
(
float
*
)
PyGpuArray_DEV_DATA
(
*
out
),
PyGpuArray_STRIDES
(
*
out
)[
0
]
/
sizeof
(
float
),
PyGpuArray_STRIDES
(
*
out
)[
1
]
/
sizeof
(
float
)
);
//TODO
//if(false)//SYNC)
// GpuArray_sync((*out)->ga);
// SYNC;
cudaError_t
sts
=
cudaGetLastError
();
if
(
cudaSuccess
!=
sts
)
{
PyErr_Format
(
PyExc_RuntimeError
,
"Cuda error: %s: %s. (grid: %i x %i; block: %i x %i x %i; shared: %i)
\\
n"
,
"k_multi_warp_%(name)s"
,
cudaGetErrorString
(
sts
),
n_blocks
.
x
,
n_blocks
.
y
,
n_threads
.
x
,
n_threads
.
y
,
n_threads
.
z
,
n_shared
);
return
1
;
}
}
// END NESTED SCOPE
return
0
;
}
theano/sandbox/gpuarray/multinomial.py
浏览文件 @
f1a3d267
...
@@ -17,7 +17,6 @@ from .type import gpu_context_type, GpuArrayType
...
@@ -17,7 +17,6 @@ from .type import gpu_context_type, GpuArrayType
class
GPUAMultinomialFromUniform
(
gpuarray
.
basic_ops
.
GpuKernelBase
,
Op
):
class
GPUAMultinomialFromUniform
(
gpuarray
.
basic_ops
.
GpuKernelBase
,
Op
):
__props__
=
(
"odtype"
,)
__props__
=
(
"odtype"
,)
params_type
=
gpu_context_type
def
__init__
(
self
,
odtype
):
def
__init__
(
self
,
odtype
):
Op
.
__init__
(
self
)
Op
.
__init__
(
self
)
...
@@ -30,7 +29,7 @@ class GPUAMultinomialFromUniform(gpuarray.basic_ops.GpuKernelBase, Op):
...
@@ -30,7 +29,7 @@ class GPUAMultinomialFromUniform(gpuarray.basic_ops.GpuKernelBase, Op):
return
[
'<numpy_compat.h>'
,
'gpuarray_helper.h'
]
return
[
'<numpy_compat.h>'
,
'gpuarray_helper.h'
]
def
c_header_dirs
(
self
):
def
c_header_dirs
(
self
):
return
[
os
.
path
.
dirname
(
__file__
)
,
pygpu
.
get_include
()
]
return
[
os
.
path
.
dirname
(
__file__
)]
def
make_node
(
self
,
pvals
,
unis
):
def
make_node
(
self
,
pvals
,
unis
):
assert
pvals
.
dtype
==
'float32'
assert
pvals
.
dtype
==
'float32'
...
@@ -100,15 +99,7 @@ KERNEL void k_multi_warp_multinomial(
...
@@ -100,15 +99,7 @@ KERNEL void k_multi_warp_multinomial(
}
}
}
}
}
}
"""
//KERNEL void k(GLOBAL_MEM
%(ctype)
s *a, ga_size n, ga_size m) {
// ga_size nb = n < m ? n : m;
// for (ga_size i = LID_0; i < nb; i += LDIM_0) {
// a[i*m + i] =
%(write_a)
s(1);
// }
//}"""
%
dict
(
ctype
=
pygpu
.
gpuarray
.
dtype_to_ctype
(
dtype
),
name
=
name
,
write_a
=
write_w
(
dtype
))
return
[
gpuarray
.
basic_ops
.
Kernel
(
return
[
gpuarray
.
basic_ops
.
Kernel
(
code
=
code
,
name
=
"k_multi_warp_multinomial"
,
code
=
code
,
name
=
"k_multi_warp_multinomial"
,
params
=
[
pygpu
.
gpuarray
.
SIZE
,
params
=
[
pygpu
.
gpuarray
.
SIZE
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论