Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
dd0d4274
提交
dd0d4274
authored
9月 30, 2016
作者:
Arnaud Bergeron
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix elemwise on float16 (hopefully).
上级
df1234f0
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
34 行增加
和
52 行删除
+34
-52
elemwise.py
theano/gpuarray/elemwise.py
+34
-52
没有找到文件。
theano/gpuarray/elemwise.py
浏览文件 @
dd0d4274
...
@@ -33,6 +33,12 @@ def as_C_string_const(s):
...
@@ -33,6 +33,12 @@ def as_C_string_const(s):
for
l
in
s
.
split
(
'
\n
'
))
for
l
in
s
.
split
(
'
\n
'
))
def
get_scal
(
dt
):
if
dt
==
'float16'
:
dt
=
'float32'
return
scalar
.
get_scalar_type
(
dt
)
class
GpuElemwise
(
HideC
,
Elemwise
):
class
GpuElemwise
(
HideC
,
Elemwise
):
"""
"""
Elemwise on the GPU.
Elemwise on the GPU.
...
@@ -60,23 +66,18 @@ class GpuElemwise(HideC, Elemwise):
...
@@ -60,23 +66,18 @@ class GpuElemwise(HideC, Elemwise):
zip
(
out_info
[
0
],
out_info
[
1
])]
zip
(
out_info
[
0
],
out_info
[
1
])]
if
len
(
outputs
)
>
1
:
if
len
(
outputs
)
>
1
:
raise
NotImplementedError
()
raise
NotImplementedError
()
node
=
Apply
(
self
,
inputs
,
outputs
)
# Try to generate the kernel to catch SupportCodeErrors
# Try to generate the kernel to catch SupportCodeErrors
scal_ins
=
[
get_scal
(
i
.
dtype
)
for
i
in
inputs
]
fake_node
=
self
.
scalar_op
.
make_node
(
*
[
i
()
for
i
in
scal_ins
])
try
:
try
:
scal_ins
=
[
scalar
.
get_scalar_type
(
i
.
dtype
)
for
i
in
node
.
inputs
]
code
=
fake_node
.
op
.
c_support_code_apply
(
fake_node
,
"test"
)
scal_out
=
[
scalar
.
get_scalar_type
(
o
.
dtype
)
for
o
in
node
.
outputs
]
fake_node
=
Apply
(
self
.
scalar_op
,
[
i
()
for
i
in
scal_ins
],
[
o
()
for
o
in
scal_out
])
code
=
self
.
scalar_op
.
c_support_code_apply
(
fake_node
,
"test"
)
if
code
:
if
code
:
raise
SupportCodeError
(
code
)
raise
SupportCodeError
(
code
)
except
MethodNotDefined
:
except
MethodNotDefined
:
pass
pass
try
:
try
:
support_code
=
self
.
scalar_
op
.
c_support_code
()
support_code
=
fake_node
.
op
.
c_support_code
()
if
"struct"
in
support_code
:
if
"struct"
in
support_code
:
# The macro is fine, the C++ struct is not.
# The macro is fine, the C++ struct is not.
raise
SupportCodeError
(
raise
SupportCodeError
(
...
@@ -85,6 +86,15 @@ class GpuElemwise(HideC, Elemwise):
...
@@ -85,6 +86,15 @@ class GpuElemwise(HideC, Elemwise):
except
MethodNotDefined
:
except
MethodNotDefined
:
pass
pass
if
fake_node
.
op
!=
self
.
scalar_op
:
# If the new op is different due to type changes, we make a new
# op for it.
elem
=
GpuElemwise
(
fake_node
.
op
,
self
.
inplace_pattern
,
self
.
name
,
self
.
nfunc_spec
,
self
.
openmp
)
else
:
elem
=
self
node
=
Apply
(
elem
,
inputs
,
outputs
)
return
node
return
node
def
get_params
(
self
,
node
):
def
get_params
(
self
,
node
):
...
@@ -92,59 +102,31 @@ class GpuElemwise(HideC, Elemwise):
...
@@ -92,59 +102,31 @@ class GpuElemwise(HideC, Elemwise):
def
_get_vnames
(
self
,
node
):
def
_get_vnames
(
self
,
node
):
inps
=
[
'i
%
d'
%
(
n
,)
for
n
,
_
in
enumerate
(
node
.
inputs
)]
inps
=
[
'i
%
d'
%
(
n
,)
for
n
,
_
in
enumerate
(
node
.
inputs
)]
outs
=
[
'o
%
d'
%
(
n
,)
for
n
,
_
in
enumerate
(
node
.
outputs
)
if
n
not
in
self
.
inplace_pattern
]
outs
=
[
'o
%
d'
%
(
n
,)
if
n
not
in
self
.
inplace_pattern
else
inps
[
self
.
inplace_pattern
[
n
]]
for
n
,
_
in
enumerate
(
node
.
outputs
)]
return
inps
,
outs
return
inps
,
outs
def
_generate_op_string
(
self
,
node
):
def
_generate_op_string
(
self
,
node
):
scal_v_ins
=
[
scalar
.
get_scalar_type
(
i
.
dtype
)
for
i
in
node
.
inputs
]
scal_v_outs
=
[
scalar
.
get_scalar_type
(
o
.
dtype
)
for
o
in
node
.
outputs
]
inps
,
outs
=
self
.
_get_vnames
(
node
)
inps
,
outs
=
self
.
_get_vnames
(
node
)
scal_v_ins
=
[
get_scal
(
i
.
dtype
)()
for
i
in
node
.
inputs
]
fake_node
=
Apply
(
self
.
scalar_op
,
[
i
()
for
i
in
scal_v_ins
],
fake_node
=
self
.
scalar_op
.
make_node
(
*
scal_v_ins
)
[
o
()
for
o
in
scal_v_outs
])
scal_v_out
=
fake_node
.
outputs
assert
len
(
scal_v_out
)
==
len
(
node
.
outputs
)
scal_in
=
[
i
if
si
.
dtype
!=
'float16'
else
'load_half(&'
+
i
+
')'
for
i
,
si
in
zip
(
inps
,
scal_v_ins
)]
scal_out
=
[]
oi
=
0
scal_f16
=
[]
for
n
in
range
(
len
(
node
.
outputs
)):
if
n
in
self
.
inplace_pattern
:
arg
=
inps
[
self
.
inplace_pattern
[
n
]]
else
:
arg
=
outs
[
oi
]
oi
+=
1
if
node
.
outputs
[
n
]
.
dtype
==
'float16'
:
scal_f16
.
append
((
'tmpf16
%
i'
%
(
len
(
scal_f16
),),
arg
))
scal_out
.
append
(
scal_f16
[
-
1
][
0
])
else
:
scal_out
.
append
(
arg
)
kop
=
self
.
scalar_
op
.
c_code
(
fake_node
,
'elem_scalar'
,
kop
=
fake_node
.
op
.
c_code
(
fake_node
,
'elem_scalar'
,
scal_in
,
scal_out
,
inps
,
outs
,
dict
(
fail
=
'return;'
))
dict
(
fail
=
'return;'
))
if
scal_f16
:
# Some ops like cast will reintroduce float16 in the internal graph.
# if we have float16 scalars on output we have to wrap
kop
=
kop
.
replace
(
'npy_float16'
,
'ga_float'
)
# them and insert a stand-in float32 variable since
# float16 arithemtic is not available
code
=
[
"{"
]
for
f
in
scal_f16
:
code
.
append
(
'ga_float
%
s;'
%
(
f
[
0
],))
# XXX: The replace is an ugly hack to make sure temp
# variables inthe middle are float32
code
.
append
(
kop
.
replace
(
'npy_float16'
,
'ga_float'
))
for
f
in
scal_f16
:
code
.
append
(
'store_half(&
%
s,
%
s);'
%
(
f
[
1
],
f
[
0
]))
code
.
append
(
'}'
)
kop
=
'
\n
'
.
join
(
code
)
support_code
=
""
support_code
=
""
try
:
try
:
# We accept only some c_support_code().
# We accept only some c_support_code().
# This filter is done in the make_node()
# This filter is done in the make_node()
support_code
+=
self
.
scalar_
op
.
c_support_code
()
support_code
+=
fake_node
.
op
.
c_support_code
()
except
MethodNotDefined
:
except
MethodNotDefined
:
pass
pass
for
npy
,
ga
in
[(
"npy_uint8"
,
"ga_ubyte"
),
for
npy
,
ga
in
[(
"npy_uint8"
,
"ga_ubyte"
),
...
@@ -171,7 +153,7 @@ class GpuElemwise(HideC, Elemwise):
...
@@ -171,7 +153,7 @@ class GpuElemwise(HideC, Elemwise):
def
c_init_code_struct
(
self
,
node
,
name
,
sub
):
def
c_init_code_struct
(
self
,
node
,
name
,
sub
):
inps
,
outs
=
self
.
_get_vnames
(
node
)
inps
,
outs
=
self
.
_get_vnames
(
node
)
nargs
=
len
(
inps
)
+
len
(
outs
)
nargs
=
len
(
inps
)
+
len
(
outs
)
-
len
(
self
.
inplace_pattern
)
support_code
,
kop
=
self
.
_generate_op_string
(
node
)
support_code
,
kop
=
self
.
_generate_op_string
(
node
)
res
=
"""
res
=
"""
gpuelemwise_arg args[
%(nargs)
s] = {{0}};
gpuelemwise_arg args[
%(nargs)
s] = {{0}};
...
@@ -202,7 +184,7 @@ class GpuElemwise(HideC, Elemwise):
...
@@ -202,7 +184,7 @@ class GpuElemwise(HideC, Elemwise):
typecode
=
o
.
type
.
typecode
)
typecode
=
o
.
type
.
typecode
)
res
+=
"""
res
+=
"""
ge = GpuElemwise_new(
%(ctx)
s->ctx,
%(support)
s,
%(kop)
s,
%(nargs)
s, args,
%(nd)
s,
0
);
ge = GpuElemwise_new(
%(ctx)
s->ctx,
%(support)
s,
%(kop)
s,
%(nargs)
s, args,
%(nd)
s,
GE_CONVERT_F16
);
if (ge == NULL) {
if (ge == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Could not initialize elemwise support");
PyErr_SetString(PyExc_RuntimeError, "Could not initialize elemwise support");
%(fail)
s
%(fail)
s
...
@@ -363,7 +345,7 @@ class GpuElemwise(HideC, Elemwise):
...
@@ -363,7 +345,7 @@ class GpuElemwise(HideC, Elemwise):
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
ver
=
self
.
scalar_op
.
c_code_cache_version
()
ver
=
self
.
scalar_op
.
c_code_cache_version
()
if
ver
:
if
ver
:
return
(
7
,
ver
)
return
(
8
,
ver
)
else
:
else
:
return
ver
return
ver
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论