Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
93351dfb
提交
93351dfb
authored
10月 05, 2017
作者:
Frédéric Bastien
提交者:
GitHub
10月 05, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #6457 from notoraptor/disallow_float16_precision_for_backward_conv
Disallow float16 precision for convolution gradients functions.
上级
aeb6e3c5
4dd87c9e
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
62 行增加
和
9 行删除
+62
-9
dnn.py
theano/gpuarray/dnn.py
+13
-9
test_dnn.py
theano/gpuarray/tests/test_dnn.py
+49
-0
没有找到文件。
theano/gpuarray/dnn.py
浏览文件 @
93351dfb
...
@@ -329,7 +329,7 @@ handle_type = CUDNNDataType('cudnnHandle_t', 'cudnnDestroy')
...
@@ -329,7 +329,7 @@ handle_type = CUDNNDataType('cudnnHandle_t', 'cudnnDestroy')
cudnn
=
cudnn_defs
.
get_definitions
(
version
(
raises
=
False
))
cudnn
=
cudnn_defs
.
get_definitions
(
version
(
raises
=
False
))
def
get_precision
(
precision
,
inputs
):
def
get_precision
(
precision
,
inputs
,
for_grad
=
False
):
if
precision
is
None
:
if
precision
is
None
:
precision
=
theano
.
config
.
dnn
.
conv
.
precision
precision
=
theano
.
config
.
dnn
.
conv
.
precision
if
precision
==
'as_input'
or
precision
==
'as_input_f32'
:
if
precision
==
'as_input'
or
precision
==
'as_input_f32'
:
...
@@ -338,6 +338,8 @@ def get_precision(precision, inputs):
...
@@ -338,6 +338,8 @@ def get_precision(precision, inputs):
precision
=
'float32'
precision
=
'float32'
else
:
else
:
precision
=
nprec
precision
=
nprec
if
for_grad
and
precision
==
'float16'
:
raise
TypeError
(
"Float16 precision is disabled for cuDNN backward convolutions due to computation errors."
)
return
precision
return
precision
...
@@ -980,9 +982,6 @@ def dnn_conv(img, kerns, border_mode='valid', subsample=(1, 1), dilation=(1, 1),
...
@@ -980,9 +982,6 @@ def dnn_conv(img, kerns, border_mode='valid', subsample=(1, 1), dilation=(1, 1),
"""
"""
# Establish dtype in which to perform the computation of the convolution
precision
=
get_precision
(
precision
,
[
img
,
kerns
])
if
workmem
is
not
None
:
if
workmem
is
not
None
:
if
algo
is
not
None
:
if
algo
is
not
None
:
raise
ValueError
(
"You can't use both algo and workmem"
)
raise
ValueError
(
"You can't use both algo and workmem"
)
...
@@ -1006,6 +1005,7 @@ def dnn_conv(img, kerns, border_mode='valid', subsample=(1, 1), dilation=(1, 1),
...
@@ -1006,6 +1005,7 @@ def dnn_conv(img, kerns, border_mode='valid', subsample=(1, 1), dilation=(1, 1),
shape_i
(
img
,
3
,
fgraph
)
-
shape_i
(
kerns
,
3
,
fgraph
)
+
1
)
shape_i
(
img
,
3
,
fgraph
)
-
shape_i
(
kerns
,
3
,
fgraph
)
+
1
)
out_shp
=
assert_conv_shape
(
out_shp
)
out_shp
=
assert_conv_shape
(
out_shp
)
out
=
GpuAllocEmpty
(
dtype
=
img
.
dtype
,
context_name
=
ctx_name
)(
*
out_shp
)
out
=
GpuAllocEmpty
(
dtype
=
img
.
dtype
,
context_name
=
ctx_name
)(
*
out_shp
)
precision
=
get_precision
(
precision
,
[
img
,
kerns
],
for_grad
=
True
)
desc
=
GpuDnnConvDesc
(
border_mode
=
'valid'
,
subsample
=
(
1
,
1
),
dilation
=
(
1
,
1
),
desc
=
GpuDnnConvDesc
(
border_mode
=
'valid'
,
subsample
=
(
1
,
1
),
dilation
=
(
1
,
1
),
conv_mode
=
'cross'
,
precision
=
precision
)(
out
.
shape
)
conv_mode
=
'cross'
,
precision
=
precision
)(
out
.
shape
)
conv
=
GpuDnnConvGradW
()(
img
,
kerns
,
out
,
desc
)
conv
=
GpuDnnConvGradW
()(
img
,
kerns
,
out
,
desc
)
...
@@ -1025,6 +1025,7 @@ def dnn_conv(img, kerns, border_mode='valid', subsample=(1, 1), dilation=(1, 1),
...
@@ -1025,6 +1025,7 @@ def dnn_conv(img, kerns, border_mode='valid', subsample=(1, 1), dilation=(1, 1),
shape_i
(
img
,
3
,
fgraph
)
+
(
shape_i
(
kerns
,
3
,
fgraph
)
-
1
)
*
dilation
[
1
])
shape_i
(
img
,
3
,
fgraph
)
+
(
shape_i
(
kerns
,
3
,
fgraph
)
-
1
)
*
dilation
[
1
])
out_shp
=
assert_conv_shape
(
out_shp
)
out_shp
=
assert_conv_shape
(
out_shp
)
out
=
GpuAllocEmpty
(
dtype
=
img
.
dtype
,
context_name
=
ctx_name
)(
*
out_shp
)
out
=
GpuAllocEmpty
(
dtype
=
img
.
dtype
,
context_name
=
ctx_name
)(
*
out_shp
)
precision
=
get_precision
(
precision
,
[
img
,
kerns
],
for_grad
=
True
)
desc
=
GpuDnnConvDesc
(
border_mode
=
'valid'
,
subsample
=
(
1
,
1
),
dilation
=
dilation
,
desc
=
GpuDnnConvDesc
(
border_mode
=
'valid'
,
subsample
=
(
1
,
1
),
dilation
=
dilation
,
conv_mode
=
conv_mode
,
precision
=
precision
)(
kerns
.
shape
)
conv_mode
=
conv_mode
,
precision
=
precision
)(
kerns
.
shape
)
return
GpuDnnConvGradI
()(
kerns
,
img
,
out
,
desc
)
return
GpuDnnConvGradI
()(
kerns
,
img
,
out
,
desc
)
...
@@ -1034,6 +1035,8 @@ def dnn_conv(img, kerns, border_mode='valid', subsample=(1, 1), dilation=(1, 1),
...
@@ -1034,6 +1035,8 @@ def dnn_conv(img, kerns, border_mode='valid', subsample=(1, 1), dilation=(1, 1),
# if the img contains negative strides
# if the img contains negative strides
img
=
gpu_contiguous
(
img
)
img
=
gpu_contiguous
(
img
)
kerns
=
gpu_contiguous
(
kerns
)
kerns
=
gpu_contiguous
(
kerns
)
# Establish dtype in which to perform the computation of the convolution
precision
=
get_precision
(
precision
,
[
img
,
kerns
])
desc
=
GpuDnnConvDesc
(
border_mode
=
border_mode
,
subsample
=
subsample
,
dilation
=
dilation
,
desc
=
GpuDnnConvDesc
(
border_mode
=
border_mode
,
subsample
=
subsample
,
dilation
=
dilation
,
conv_mode
=
conv_mode
,
precision
=
precision
,
conv_mode
=
conv_mode
,
precision
=
precision
,
num_groups
=
num_groups
)(
kerns
.
shape
)
num_groups
=
num_groups
)(
kerns
.
shape
)
...
@@ -1107,9 +1110,6 @@ def dnn_conv3d(img, kerns, border_mode='valid', subsample=(1, 1, 1), dilation=(1
...
@@ -1107,9 +1110,6 @@ def dnn_conv3d(img, kerns, border_mode='valid', subsample=(1, 1, 1), dilation=(1
"""
"""
# Establish dtype in which to perform the computation of the convolution
precision
=
get_precision
(
precision
,
[
img
,
kerns
])
fgraph
=
getattr
(
img
,
'fgraph'
,
None
)
or
getattr
(
kerns
,
'fgraph'
,
None
)
fgraph
=
getattr
(
img
,
'fgraph'
,
None
)
or
getattr
(
kerns
,
'fgraph'
,
None
)
ctx_name
=
infer_context_name
(
img
,
kerns
)
ctx_name
=
infer_context_name
(
img
,
kerns
)
if
(
border_mode
==
'valid'
and
subsample
==
(
1
,
1
,
1
)
and
dilation
==
(
1
,
1
,
1
)
and
if
(
border_mode
==
'valid'
and
subsample
==
(
1
,
1
,
1
)
and
dilation
==
(
1
,
1
,
1
)
and
...
@@ -1129,6 +1129,7 @@ def dnn_conv3d(img, kerns, border_mode='valid', subsample=(1, 1, 1), dilation=(1
...
@@ -1129,6 +1129,7 @@ def dnn_conv3d(img, kerns, border_mode='valid', subsample=(1, 1, 1), dilation=(1
shape_i
(
img
,
4
,
fgraph
)
-
shape_i
(
kerns
,
4
,
fgraph
)
+
1
)
shape_i
(
img
,
4
,
fgraph
)
-
shape_i
(
kerns
,
4
,
fgraph
)
+
1
)
out_shp
=
assert_conv_shape
(
out_shp
)
out_shp
=
assert_conv_shape
(
out_shp
)
out
=
GpuAllocEmpty
(
dtype
=
img
.
dtype
,
context_name
=
ctx_name
)(
*
out_shp
)
out
=
GpuAllocEmpty
(
dtype
=
img
.
dtype
,
context_name
=
ctx_name
)(
*
out_shp
)
precision
=
get_precision
(
precision
,
[
img
,
kerns
],
for_grad
=
True
)
desc
=
GpuDnnConvDesc
(
border_mode
=
'valid'
,
subsample
=
(
1
,
1
,
1
),
dilation
=
(
1
,
1
,
1
),
desc
=
GpuDnnConvDesc
(
border_mode
=
'valid'
,
subsample
=
(
1
,
1
,
1
),
dilation
=
(
1
,
1
,
1
),
conv_mode
=
'cross'
,
precision
=
precision
)(
out
.
shape
)
conv_mode
=
'cross'
,
precision
=
precision
)(
out
.
shape
)
conv
=
GpuDnnConvGradW
()(
img
,
kerns
,
out
,
desc
)
conv
=
GpuDnnConvGradW
()(
img
,
kerns
,
out
,
desc
)
...
@@ -1149,6 +1150,7 @@ def dnn_conv3d(img, kerns, border_mode='valid', subsample=(1, 1, 1), dilation=(1
...
@@ -1149,6 +1150,7 @@ def dnn_conv3d(img, kerns, border_mode='valid', subsample=(1, 1, 1), dilation=(1
shape_i
(
img
,
4
,
fgraph
)
+
(
shape_i
(
kerns
,
4
,
fgraph
)
-
1
)
*
dilation
[
2
])
shape_i
(
img
,
4
,
fgraph
)
+
(
shape_i
(
kerns
,
4
,
fgraph
)
-
1
)
*
dilation
[
2
])
out_shp
=
assert_conv_shape
(
out_shp
)
out_shp
=
assert_conv_shape
(
out_shp
)
out
=
GpuAllocEmpty
(
dtype
=
img
.
dtype
,
context_name
=
ctx_name
)(
*
out_shp
)
out
=
GpuAllocEmpty
(
dtype
=
img
.
dtype
,
context_name
=
ctx_name
)(
*
out_shp
)
precision
=
get_precision
(
precision
,
[
img
,
kerns
],
for_grad
=
True
)
desc
=
GpuDnnConvDesc
(
border_mode
=
'valid'
,
subsample
=
(
1
,
1
,
1
),
dilation
=
dilation
,
desc
=
GpuDnnConvDesc
(
border_mode
=
'valid'
,
subsample
=
(
1
,
1
,
1
),
dilation
=
dilation
,
conv_mode
=
conv_mode
,
precision
=
precision
)(
kerns
.
shape
)
conv_mode
=
conv_mode
,
precision
=
precision
)(
kerns
.
shape
)
return
GpuDnnConvGradI
()(
kerns
,
img
,
out
,
desc
)
return
GpuDnnConvGradI
()(
kerns
,
img
,
out
,
desc
)
...
@@ -1158,6 +1160,8 @@ def dnn_conv3d(img, kerns, border_mode='valid', subsample=(1, 1, 1), dilation=(1
...
@@ -1158,6 +1160,8 @@ def dnn_conv3d(img, kerns, border_mode='valid', subsample=(1, 1, 1), dilation=(1
# if the img contains negative strides
# if the img contains negative strides
img
=
gpu_contiguous
(
img
)
img
=
gpu_contiguous
(
img
)
kerns
=
gpu_contiguous
(
kerns
)
kerns
=
gpu_contiguous
(
kerns
)
# Establish dtype in which to perform the computation of the convolution
precision
=
get_precision
(
precision
,
[
img
,
kerns
])
desc
=
GpuDnnConvDesc
(
border_mode
=
border_mode
,
subsample
=
subsample
,
dilation
=
dilation
,
desc
=
GpuDnnConvDesc
(
border_mode
=
border_mode
,
subsample
=
subsample
,
dilation
=
dilation
,
conv_mode
=
conv_mode
,
precision
=
precision
,
conv_mode
=
conv_mode
,
precision
=
precision
,
num_groups
=
num_groups
)(
kerns
.
shape
)
num_groups
=
num_groups
)(
kerns
.
shape
)
...
@@ -1187,7 +1191,7 @@ def dnn_gradweight(img, topgrad, kerns_shp, border_mode='valid',
...
@@ -1187,7 +1191,7 @@ def dnn_gradweight(img, topgrad, kerns_shp, border_mode='valid',
img
=
gpu_contiguous
(
img
)
img
=
gpu_contiguous
(
img
)
topgrad
=
gpu_contiguous
(
topgrad
)
topgrad
=
gpu_contiguous
(
topgrad
)
kerns_shp
=
as_tensor_variable
(
kerns_shp
)
kerns_shp
=
as_tensor_variable
(
kerns_shp
)
precision
=
get_precision
(
precision
,
[
img
,
topgrad
])
precision
=
get_precision
(
precision
,
[
img
,
topgrad
]
,
for_grad
=
True
)
desc
=
GpuDnnConvDesc
(
border_mode
=
border_mode
,
subsample
=
subsample
,
dilation
=
dilation
,
desc
=
GpuDnnConvDesc
(
border_mode
=
border_mode
,
subsample
=
subsample
,
dilation
=
dilation
,
conv_mode
=
conv_mode
,
precision
=
precision
,
conv_mode
=
conv_mode
,
precision
=
precision
,
...
@@ -1219,7 +1223,7 @@ def dnn_gradinput(kerns, topgrad, img_shp, border_mode='valid',
...
@@ -1219,7 +1223,7 @@ def dnn_gradinput(kerns, topgrad, img_shp, border_mode='valid',
kerns
=
gpu_contiguous
(
kerns
)
kerns
=
gpu_contiguous
(
kerns
)
topgrad
=
gpu_contiguous
(
topgrad
)
topgrad
=
gpu_contiguous
(
topgrad
)
img_shp
=
as_tensor_variable
(
img_shp
)
img_shp
=
as_tensor_variable
(
img_shp
)
precision
=
get_precision
(
precision
,
[
kerns
,
topgrad
])
precision
=
get_precision
(
precision
,
[
kerns
,
topgrad
]
,
for_grad
=
True
)
desc
=
GpuDnnConvDesc
(
border_mode
=
border_mode
,
subsample
=
subsample
,
dilation
=
dilation
,
desc
=
GpuDnnConvDesc
(
border_mode
=
border_mode
,
subsample
=
subsample
,
dilation
=
dilation
,
conv_mode
=
conv_mode
,
precision
=
precision
,
conv_mode
=
conv_mode
,
precision
=
precision
,
...
...
theano/gpuarray/tests/test_dnn.py
浏览文件 @
93351dfb
...
@@ -171,6 +171,55 @@ def test_dnn_conv_inplace():
...
@@ -171,6 +171,55 @@ def test_dnn_conv_inplace():
assert
len
([
n
for
n
in
topo
if
isinstance
(
n
.
op
,
GpuAllocEmpty
)])
==
2
assert
len
([
n
for
n
in
topo
if
isinstance
(
n
.
op
,
GpuAllocEmpty
)])
==
2
def
run_dnn_conv_invalid_precision
(
ndim
):
bc
=
(
False
,)
*
(
ndim
+
2
)
img
=
T
.
tensor
(
theano
.
config
.
floatX
,
broadcastable
=
bc
)
kerns
=
T
.
tensor
(
theano
.
config
.
floatX
,
broadcastable
=
bc
)
topgrad
=
T
.
tensor
(
theano
.
config
.
floatX
,
broadcastable
=
bc
)
shape
=
np
.
arange
(
ndim
+
2
)
if
ndim
==
2
:
dnn_conv_func
=
dnn
.
dnn_conv
dnn_gradw_func
=
dnn
.
dnn_gradweight
dnn_gradi_func
=
dnn
.
dnn_gradinput
elif
ndim
==
3
:
dnn_conv_func
=
dnn
.
dnn_conv3d
dnn_gradw_func
=
dnn
.
dnn_gradweight3d
dnn_gradi_func
=
dnn
.
dnn_gradinput3d
def
dnn_gradw
(
precision
):
return
dnn_gradw_func
(
img
,
topgrad
,
shape
,
precision
=
precision
)
def
dnn_gradi
(
precision
):
return
dnn_gradi_func
(
kerns
,
topgrad
,
shape
,
precision
=
precision
)
def
dnn_conv
(
precision
,
border_mode
,
direction_hint
):
return
dnn_conv_func
(
img
,
kerns
,
border_mode
=
border_mode
,
direction_hint
=
direction_hint
,
precision
=
precision
)
dnn_gradw
(
'float64'
)
dnn_gradw
(
'float32'
)
assert_raises
(
TypeError
,
dnn_gradw
,
'float16'
)
dnn_gradi
(
'float64'
)
dnn_gradi
(
'float32'
)
assert_raises
(
TypeError
,
dnn_gradi
,
'float16'
)
for
precision
in
(
'float64'
,
'float32'
):
dnn_conv
(
precision
,
'valid'
,
None
)
dnn_conv
(
precision
,
'valid'
,
'bprop weights'
)
dnn_conv
(
precision
,
'full'
,
None
)
dnn_conv
(
precision
,
'full'
,
'forward!'
)
dnn_conv
(
'float16'
,
'valid'
,
None
)
assert_raises
(
TypeError
,
dnn_conv
,
'float16'
,
'valid'
,
'bprop weights'
)
assert_raises
(
TypeError
,
dnn_conv
,
'float16'
,
'full'
,
None
)
dnn_conv
(
'float16'
,
'full'
,
'forward!'
)
def
test_dnn_conv_invalid_precision
():
yield
(
run_dnn_conv_invalid_precision
,
2
)
yield
(
run_dnn_conv_invalid_precision
,
3
)
def
test_pooling
():
def
test_pooling
():
if
not
dnn
.
dnn_available
(
test_ctx_name
):
if
not
dnn
.
dnn_available
(
test_ctx_name
):
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
raise
SkipTest
(
dnn
.
dnn_available
.
msg
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论