Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a4906222
提交
a4906222
authored
4月 13, 2016
作者:
Harm de Vries
提交者:
Frederic Bastien
4月 22, 2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
created the new op, not working ye
上级
e3569d12
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
185 行增加
和
0 行删除
+185
-0
multinomial.c
theano/sandbox/gpuarray/multinomial.c
+140
-0
multinomial.py
theano/sandbox/gpuarray/multinomial.py
+25
-0
test_multinomial.py
theano/sandbox/gpuarray/tests/test_multinomial.py
+20
-0
没有找到文件。
theano/sandbox/gpuarray/multinomial.c
0 → 100644
浏览文件 @
a4906222
#section support_code_apply
static
__global__
void
k_multi_warp_APPLYSPECIFIC
(
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
)
{
if
(
PyGpuArray_NDIM
(
pvals
)
!=
2
)
{
PyErr_Format
(
PyExc_TypeError
,
"pvals wrong rank"
);
FAIL
;
}
if
(
PyGpuArray_NDIM
(
unis
)
!=
1
)
{
PyErr_Format
(
PyExc_TypeError
,
"unis wrong rank"
);
FAIL
;
}
if
(
PyGpuArray_HOST_DIMS
(
unis
)[
0
]
!=
PyGpuArray_HOST_DIMS
(
pvals
)[
0
])
{
PyErr_Format
(
PyExc_ValueError
,
"unis.shape[0] != pvals.shape[0]"
);
FAIL
;
}
//N.B. that the output is TRANSPOSED compared with pvals
if
((
NULL
==
*
out
)
||
(
PyGpuArray_HOST_DIMS
(
*
out
[
0
]
!=
PyGpuArray_HOST_DIMS
(
pvals
)[
1
]
||
(
PyGpuAarray_HOST_DIMS
(
*
out
[
1
]
!=
PyGpuArray_HOST_DIMS
(
pvals
)[
0
])
{
Py_XDECREF
(
*
out
);
npy_intp
dims
[
2
];
dims
[
0
]
=
(
PyGpuArray_HOST_DIMS
(
pvals
)[
1
];
dims
[
1
]
=
(
PyGpuArray_HOST_DIMS
(
pvals
)[
0
]);
*
out
=
(
PyGpuarray
*
)
PyGpuArray_NewDims
(
2
,
dims
);
if
(
!*
out
)
{
PyErr_SetString
(
PyExc_MemoryError
,
"failed to alloc z output"
);
FAIL
;
}
}
{
// NESTED SCOPE
int
nb_multi
=
PyGpuArray_HOST_DIMS
(
pvals
)[
0
];
int
nb_outcomes
=
PyGpuArray_HOST_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
,
"Mutinomial is not implemented for so many rows in the matrix (%%i)"
,
nb_multi
);
FAIL
;
}
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_APPLYSPECIFIC
(
multinomial
)
<<<
n_blocks
,
n_threads
,
n_shared
>>>
(
CudaNdarray_HOST_DIMS
(
%
(
z
)
s
)[
1
],
CudaNdarray_HOST_DIMS
(
%
(
z
)
s
)[
0
],
CudaNdarray_DEV_DATA
(
%
(
pvals
)
s
),
CudaNdarray_HOST_STRIDES
(
%
(
pvals
)
s
)[
0
],
CudaNdarray_HOST_STRIDES
(
%
(
pvals
)
s
)[
1
],
CudaNdarray_DEV_DATA
(
%
(
unis
)
s
),
CudaNdarray_HOST_STRIDES
(
%
(
unis
)
s
)[
0
],
CudaNdarray_DEV_DATA
(
%
(
z
)
s
),
CudaNdarray_HOST_STRIDES
(
%
(
z
)
s
)[
0
],
CudaNdarray_HOST_STRIDES
(
%
(
z
)
s
)[
1
]
);
CNDA_THREAD_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
);
FAIL
;
}
}
// END NESTED SCOPE
}
\ No newline at end of file
theano/sandbox/gpuarray/multinomial.py
0 → 100644
浏览文件 @
a4906222
from
theano
import
Apply
from
theano.gof
import
COp
from
.basic_ops
import
as_gpuarray_variable
,
infer_context_name
from
.type
import
GpuArrayType
class
GPUAMultinomialFromUniform
(
COp
):
def
__init__
(
self
):
COp
.
__init__
(
self
,
[
'multinomial.c'
],
'APPLY_SPECIFIC(multinomial)'
)
def
make_node
(
self
,
pvals
,
unis
):
assert
pvals
.
dtype
==
'float32'
assert
unis
.
dtype
==
'float32'
ctx_name
=
infer_context_name
(
pvals
,
unis
)
pvals
=
as_gpuarray_variable
(
pvals
,
ctx_name
)
unis
=
as_gpuarray_variable
(
unis
,
ctx_name
)
br
=
(
pvals
.
broadcastable
[
1
],
pvals
.
broadcastable
[
0
])
out
=
GpuArrayType
(
broadcastable
=
br
,
dtype
=
"float32"
)()
return
Apply
(
self
,
[
pvals
,
unis
],
[
out
])
def
c_code_cache_version
(
self
):
return
(
8
,)
theano/sandbox/gpuarray/tests/test_multinomial.py
0 → 100644
浏览文件 @
a4906222
import
numpy
import
theano
from
theano
import
tensor
from
theano.sandbox.gpuarray.multinomial
import
GPUAMultinomialFromUniform
from
.config
import
mode_with_gpu
def
test_multinomial0
():
# This tests the MultinomialFromUniform Op directly, not going through the
# multinomial() call in GPU random generation.
p
=
tensor
.
fmatrix
()
u
=
tensor
.
fvector
()
m
=
GPUAMultinomialFromUniform
()(
p
,
u
)
f
=
theano
.
function
([
p
,
u
],
m
,
mode
=
mode_with_gpu
)
assert
f
(
numpy
.
array
([[
0.1
,
0.2
,
0.3
,
0.4
],
[
0.1
,
0.2
,
0.3
,
0.4
]]),
numpy
.
array
([
0.05
,
0.05
]))
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论