Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
24b77b44
提交
24b77b44
authored
9月 30, 2015
作者:
Nicolas Ballas
提交者:
Pascal Lamblin
10月 14, 2015
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
reorganize code
上级
a3e94b40
全部展开
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
211 行增加
和
208 行删除
+211
-208
dnn.py
theano/sandbox/cuda/dnn.py
+50
-0
opt.py
theano/sandbox/cuda/opt.py
+160
-0
test_abstractconv.py
theano/sandbox/cuda/tests/test_abstractconv.py
+0
-0
abstract_conv2d.py
theano/tensor/nnet/abstract_conv2d.py
+1
-208
没有找到文件。
theano/sandbox/cuda/dnn.py
浏览文件 @
24b77b44
...
@@ -13,6 +13,9 @@ from theano.compile.ops import shape_i
...
@@ -13,6 +13,9 @@ from theano.compile.ops import shape_i
from
theano.tensor.nnet
import
SoftmaxGrad
from
theano.tensor.nnet
import
SoftmaxGrad
from
theano.tensor.signal.downsample
import
(
from
theano.tensor.signal.downsample
import
(
DownsampleFactorMax
,
MaxPoolGrad
,
AveragePoolGrad
)
DownsampleFactorMax
,
MaxPoolGrad
,
AveragePoolGrad
)
from
theano.tensor.opt
import
register_specialize_device
from
theano.sandbox.cuda.type
import
CudaNdarrayType
from
theano.sandbox.cuda
import
GpuOp
from
theano.sandbox.cuda
import
GpuOp
from
theano.sandbox.cuda.basic_ops
import
(
as_cuda_ndarray_variable
,
from
theano.sandbox.cuda.basic_ops
import
(
as_cuda_ndarray_variable
,
host_from_gpu
,
host_from_gpu
,
...
@@ -27,6 +30,12 @@ from theano.sandbox.cuda import gpu_seqopt, register_opt
...
@@ -27,6 +30,12 @@ from theano.sandbox.cuda import gpu_seqopt, register_opt
from
theano.sandbox.cuda.nvcc_compiler
import
NVCC_compiler
from
theano.sandbox.cuda.nvcc_compiler
import
NVCC_compiler
from
theano.tensor.nnet.abstract_conv2d
import
(
AbstractConv2d
,
AbstractConv2d_gradWeights
,
AbstractConv2d_gradInputs
)
from
theano.tensor.opt
import
register_specialize_device
def
dnn_available
():
def
dnn_available
():
if
dnn_available
.
avail
is
None
:
if
dnn_available
.
avail
is
None
:
...
@@ -2439,3 +2448,44 @@ if True:
...
@@ -2439,3 +2448,44 @@ if True:
gpu_contiguous
(
ins
[
1
])
gpu_contiguous
(
ins
[
1
])
)
)
return
[
out
.
dimshuffle
(
0
,
1
)]
return
[
out
.
dimshuffle
(
0
,
1
)]
### AbstractConv Optimizations
@local_optimizer
([
AbstractConv2d
,
AbstractConv2d_gradWeights
,
AbstractConv2d_gradInputs
])
def
local_conv2d_cudnn
(
node
):
inp1
=
node
.
inputs
[
0
]
inp2
=
node
.
inputs
[
1
]
if
not
isinstance
(
inp1
.
type
,
CudaNdarrayType
)
or
\
not
isinstance
(
inp2
.
type
,
CudaNdarrayType
):
return
None
if
not
dnn_available
():
return
None
if
node
.
op
.
filters_flip
:
conv_mode
=
'conv'
else
:
conv_mode
=
'cross'
if
(
isinstance
(
node
.
op
,
AbstractConv2d
)):
rval
=
dnn_conv
(
inp1
,
inp2
,
border_mode
=
node
.
op
.
border_mode
,
subsample
=
node
.
op
.
subsample
,
direction_hint
=
'forward'
,
conv_mode
=
conv_mode
)
return
[
rval
]
if
(
isinstance
(
node
.
op
,
AbstractConv2d_gradWeights
)):
shape
=
(
inp2
.
shape
[
1
],
inp1
.
shape
[
1
],
node
.
inputs
[
2
][
0
],
node
.
inputs
[
2
][
1
])
rval
=
dnn_gradweight
(
inp1
,
inp2
,
shape
,
border_mode
=
node
.
op
.
border_mode
,
subsample
=
node
.
op
.
subsample
,
conv_mode
=
conv_mode
)
return
[
rval
]
if
(
isinstance
(
node
.
op
,
AbstractConv2d_gradInputs
)):
shape
=
(
inp2
.
shape
[
0
],
inp1
.
shape
[
1
],
node
.
inputs
[
2
][
0
],
node
.
inputs
[
2
][
1
])
rval
=
dnn_gradinput
(
inp1
,
inp2
,
shape
,
border_mode
=
node
.
op
.
border_mode
,
subsample
=
node
.
op
.
subsample
,
conv_mode
=
conv_mode
)
return
[
rval
]
register_specialize_device
(
local_conv2d_cudnn
,
'cudnn'
)
theano/sandbox/cuda/opt.py
浏览文件 @
24b77b44
...
@@ -75,6 +75,12 @@ from theano.tensor import slinalg
...
@@ -75,6 +75,12 @@ from theano.tensor import slinalg
from
theano.tensor.nnet.Conv3D
import
Conv3D
from
theano.tensor.nnet.Conv3D
import
Conv3D
from
theano.tests.breakpoint
import
PdbBreakpoint
from
theano.tests.breakpoint
import
PdbBreakpoint
from
theano.tensor.nnet.abstract_conv2d
import
(
BaseAbstractConv2d
,
AbstractConv2d
,
AbstractConv2d_gradWeights
,
AbstractConv2d_gradInputs
)
from
theano.tensor.opt
import
register_specialize_device
try
:
try
:
# We need to be able to import this file even if cuda isn't avail.
# We need to be able to import this file even if cuda isn't avail.
from
theano.sandbox.cuda
import
device_properties
from
theano.sandbox.cuda
import
device_properties
...
@@ -2619,3 +2625,157 @@ optdb.register('local_inplace_gpu_sparse_block_outer',
...
@@ -2619,3 +2625,157 @@ optdb.register('local_inplace_gpu_sparse_block_outer',
import
theano.sandbox.cuda.extra_ops
import
theano.sandbox.cuda.extra_ops
### Move to Gpu optimization
@local_optimizer
([
gpu_from_host
,
AbstractConv2d
,
AbstractConv2d_gradWeights
,
AbstractConv2d_gradInputs
])
def
local_conv2d_gpu_conv
(
node
):
"""
gpu_from_host(AbstractConv) -> AbstractConv(gpu_from_host)
AbstractConv(host_from_gpu) -> host_from_gpu(AbstractConv)
"""
if
isinstance
(
node
.
op
,
GpuFromHost
):
host_input
=
node
.
inputs
[
0
]
if
host_input
.
owner
and
isinstance
(
host_input
.
owner
.
op
,
BaseAbstractConv2d
):
conv
=
host_input
.
owner
.
op
inps
=
list
(
host_input
.
owner
.
inputs
)
inps
[
0
]
=
as_cuda_ndarray_variable
(
inps
[
0
])
inps
[
1
]
=
as_cuda_ndarray_variable
(
inps
[
1
])
out
=
conv
(
*
inps
)
# out is on the GPU because both inputs are.
out
=
theano
.
tensor
.
patternbroadcast
(
out
,
node
.
outputs
[
0
]
.
broadcastable
)
out
.
values_eq_approx
=
values_eq_approx_high_tol
return
[
out
]
if
isinstance
(
node
.
op
,
BaseAbstractConv2d
):
# conv(host_from_gpu) -> host_from_gpu(gpu_conv)
inp1
=
node
.
inputs
[
0
]
inp2
=
node
.
inputs
[
1
]
if
((
isinstance
(
inp1
.
type
,
CudaNdarrayType
)
and
isinstance
(
inp2
.
type
,
CudaNdarrayType
))):
# Both inputs are already directly on the GPU, nothing to do
return
inp1_on_gpu
=
(
isinstance
(
inp1
.
type
,
CudaNdarrayType
)
or
(
inp1
.
owner
and
isinstance
(
inp1
.
owner
.
op
,
HostFromGpu
)))
inp2_on_gpu
=
(
isinstance
(
inp2
.
type
,
CudaNdarrayType
)
or
(
inp2
.
owner
and
isinstance
(
inp2
.
owner
.
op
,
HostFromGpu
)))
if
inp1_on_gpu
or
inp2_on_gpu
:
conv
=
node
.
op
inps
=
list
(
node
.
inputs
)
inps
[
0
]
=
as_cuda_ndarray_variable
(
inps
[
0
])
inps
[
1
]
=
as_cuda_ndarray_variable
(
inps
[
1
])
out
=
conv
(
*
inps
)
# out is on the GPU because both inputs are.
out
=
theano
.
tensor
.
patternbroadcast
(
out
,
node
.
outputs
[
0
]
.
broadcastable
)
out
.
values_eq_approx
=
values_eq_approx_high_tol
# If the original output was on CPU, we have to transfer it
if
isinstance
(
node
.
outputs
[
0
]
.
type
,
tensor
.
TensorType
):
return
[
tensor
.
as_tensor_variable
(
out
)]
else
:
return
[
out
]
register_opt
()(
local_conv2d_gpu_conv
)
### Corrmm opt
@local_optimizer
([
AbstractConv2d
])
def
local_conv2d_corrmm
(
node
):
img
,
kern
=
node
.
inputs
if
(
not
isinstance
(
img
.
type
,
CudaNdarrayType
)
or
not
isinstance
(
kern
.
type
,
CudaNdarrayType
)):
return
None
border_mode
=
node
.
op
.
border_mode
subsample
=
node
.
op
.
subsample
if
(
border_mode
==
'full'
)
and
(
subsample
==
(
1
,
1
)):
if
not
node
.
op
.
filters_flip
:
kern
=
kern
[:,
:,
::
-
1
,
::
-
1
]
# need to dimshuffle the kernel for full convolution
kern
=
kern
.
dimshuffle
(
1
,
0
,
2
,
3
)
# call GpuCorrMM_gradInputs
rval
=
GpuCorrMM_gradInputs
(
'valid'
,
subsample
)(
gpu_contiguous
(
kern
),
gpu_contiguous
(
img
))
else
:
# need to flip the kernel if necessary
if
node
.
op
.
filters_flip
:
kern
=
kern
[:,
:,
::
-
1
,
::
-
1
]
# By default use GpuCorrMM
rval
=
GpuCorrMM
(
border_mode
,
subsample
)(
gpu_contiguous
(
img
),
gpu_contiguous
(
kern
))
# call GpuCorrMM_gradWeights if good
# (the latter is faster if batchsize * kernelHeight * kernelWidth
# is larger than inputChannels * outputHeight * outputWidth.
# GpuConv does not always store information on the batchsize and
# channels, though, so we only use what information we have.)
if
((
subsample
==
(
1
,
1
))
and
(
node
.
op
.
imshp
is
not
None
)
and
(
None
not
in
node
.
op
.
imshp
[
-
2
:])
and
(
node
.
op
.
kshp
is
not
None
)
and
(
None
not
in
node
.
op
.
kshp
)):
# we know the kernel and output size
prod1
=
node
.
op
.
kshp
[
0
]
*
node
.
op
.
kshp
[
1
]
prod2
=
((
node
.
op
.
imshp
[
-
2
]
-
node
.
op
.
kshp
[
0
]
+
1
)
*
(
node
.
op
.
imshp
[
-
1
]
-
node
.
op
.
kshp
[
1
]
+
1
))
if
((
node
.
op
.
bsize
is
not
None
)
and
(
len
(
node
.
op
.
imshp
)
==
3
)
and
(
node
.
op
.
imshp
[
0
]
is
not
None
)):
# we also know batchsize and input channels
prod1
*=
node
.
op
.
bsize
prod2
*=
node
.
op
.
imshp
[
0
]
# compare to decide
if
prod1
>
prod2
:
# (we need to wrap the result in as_cuda_ndarray_variable,
# because we are not allowed to replace a CudaNdarray with
# a DimShuffle instance in a graph optimization)
rval
=
theano
.
sandbox
.
cuda
.
as_cuda_ndarray_variable
(
GpuCorrMM_gradWeights
(
border_mode
,
subsample
)(
gpu_contiguous
(
img
.
dimshuffle
(
1
,
0
,
2
,
3
)),
gpu_contiguous
(
kern
.
dimshuffle
(
1
,
0
,
2
,
3
))
)
.
dimshuffle
(
1
,
0
,
2
,
3
))
return
[
rval
]
register_specialize_device
(
local_conv2d_corrmm
,
'conv_gemm'
)
@local_optimizer
([
AbstractConv2d_gradWeights
])
def
local_conv2d_gradweight_corrmm
(
node
):
img
,
topgrad
,
shape
=
node
.
inputs
if
not
isinstance
(
img
.
type
,
CudaNdarrayType
)
or
\
not
isinstance
(
topgrad
.
type
,
CudaNdarrayType
):
return
None
rval
=
GpuCorrMM_gradWeights
(
border_mode
=
node
.
op
.
border_mode
,
subsample
=
node
.
op
.
subsample
)(
gpu_contiguous
(
img
),
gpu_contiguous
(
topgrad
),
shape
)
if
node
.
op
.
filters_flip
:
rval
=
rval
[:,
:,
::
-
1
,
::
-
1
]
rval
=
tensor
.
patternbroadcast
(
rval
,
node
.
outputs
[
0
]
.
broadcastable
)
rval
=
as_cuda_ndarray_variable
(
rval
)
return
[
rval
]
register_specialize_device
(
local_conv2d_gradweight_corrmm
,
'conv_gemm'
)
@local_optimizer
([
AbstractConv2d_gradInputs
])
def
local_conv2d_gradinputs_corrmm
(
node
):
kern
,
topgrad
,
shape
=
node
.
inputs
if
not
isinstance
(
kern
.
type
,
CudaNdarrayType
)
or
\
not
isinstance
(
topgrad
.
type
,
CudaNdarrayType
):
return
None
if
node
.
op
.
filters_flip
:
kern
=
kern
[:,
:,
::
-
1
,
::
-
1
]
rval
=
GpuCorrMM_gradInputs
(
border_mode
=
node
.
op
.
border_mode
,
subsample
=
node
.
op
.
subsample
)(
gpu_contiguous
(
kern
),
gpu_contiguous
(
topgrad
),
shape
)
return
[
rval
]
register_specialize_device
(
local_conv2d_gradinputs_corrmm
,
'conv_gemm'
)
theano/sandbox/cuda/tests/test_abstractconv.py
0 → 100644
浏览文件 @
24b77b44
差异被折叠。
点击展开。
theano/tensor/nnet/abstract_conv2d.py
浏览文件 @
24b77b44
...
@@ -15,20 +15,8 @@ from theano.tensor import TensorType
...
@@ -15,20 +15,8 @@ from theano.tensor import TensorType
from
theano.gof
import
Apply
,
Op
from
theano.gof
import
Apply
,
Op
from
theano.gof
import
local_optimizer
from
theano.gof
import
local_optimizer
from
theano.sandbox.cuda
import
register_opt
as
register_gpu
from
theano.tensor.opt
import
register_specialize_device
from
theano.tensor.opt
import
register_specialize_device
### Gpu related optimization (to be moved in sandbox/cuda)
from
theano.sandbox.cuda.basic_ops
import
(
as_cuda_ndarray_variable
,
gpu_contiguous
,
gpu_from_host
,
host_from_gpu
,
GpuFromHost
,
HostFromGpu
)
from
theano.sandbox.cuda.type
import
CudaNdarrayType
from
theano.sandbox.cuda.type
import
CudaNdarrayType
from
theano.sandbox.cuda.dnn
import
dnn_available
,
dnn_conv
,
dnn_gradweight
,
dnn_gradinput
from
theano.sandbox.cuda.blas
import
GpuCorrMM
,
GpuCorrMM_gradWeights
,
GpuCorrMM_gradInputs
from
theano.sandbox.cuda.opt
import
values_eq_approx_high_tol
## Cpu implementation
## Cpu implementation
...
@@ -36,6 +24,7 @@ from theano.tensor.nnet import conv2d as cpu_conv2d, ConvOp
...
@@ -36,6 +24,7 @@ from theano.tensor.nnet import conv2d as cpu_conv2d, ConvOp
from
theano.tensor.nnet.ConvGrad3D
import
convGrad3D
from
theano.tensor.nnet.ConvGrad3D
import
convGrad3D
from
theano.tensor.nnet.ConvTransp3D
import
convTransp3D
from
theano.tensor.nnet.ConvTransp3D
import
convTransp3D
_logger
=
logging
.
getLogger
(
"theano.tensor.nnet.conv2d"
)
_logger
=
logging
.
getLogger
(
"theano.tensor.nnet.conv2d"
)
...
@@ -330,202 +319,6 @@ class AbstractConv2d_gradInputs(BaseAbstractConv2d):
...
@@ -330,202 +319,6 @@ class AbstractConv2d_gradInputs(BaseAbstractConv2d):
def
connection_pattern
(
self
,
node
):
def
connection_pattern
(
self
,
node
):
return
[[
1
],
[
1
],
[
0
]]
# no connection to height, width
return
[[
1
],
[
1
],
[
0
]]
# no connection to height, width
### Move to Gpu optimization
@local_optimizer
([
gpu_from_host
,
AbstractConv2d
,
AbstractConv2d_gradWeights
,
AbstractConv2d_gradInputs
])
def
local_conv2d_gpu_conv
(
node
):
"""
gpu_from_host(AbstractConv) -> AbstractConv(gpu_from_host)
AbstractConv(host_from_gpu) -> host_from_gpu(AbstractConv)
"""
if
isinstance
(
node
.
op
,
GpuFromHost
):
host_input
=
node
.
inputs
[
0
]
if
host_input
.
owner
and
isinstance
(
host_input
.
owner
.
op
,
BaseAbstractConv2d
):
conv
=
host_input
.
owner
.
op
inps
=
list
(
host_input
.
owner
.
inputs
)
inps
[
0
]
=
as_cuda_ndarray_variable
(
inps
[
0
])
inps
[
1
]
=
as_cuda_ndarray_variable
(
inps
[
1
])
out
=
conv
(
*
inps
)
# out is on the GPU because both inputs are.
out
=
theano
.
tensor
.
patternbroadcast
(
out
,
node
.
outputs
[
0
]
.
broadcastable
)
out
.
values_eq_approx
=
values_eq_approx_high_tol
return
[
out
]
if
isinstance
(
node
.
op
,
BaseAbstractConv2d
):
# conv(host_from_gpu) -> host_from_gpu(gpu_conv)
inp1
=
node
.
inputs
[
0
]
inp2
=
node
.
inputs
[
1
]
if
((
isinstance
(
inp1
.
type
,
CudaNdarrayType
)
and
isinstance
(
inp2
.
type
,
CudaNdarrayType
))):
# Both inputs are already directly on the GPU, nothing to do
return
inp1_on_gpu
=
(
isinstance
(
inp1
.
type
,
CudaNdarrayType
)
or
(
inp1
.
owner
and
isinstance
(
inp1
.
owner
.
op
,
HostFromGpu
)))
inp2_on_gpu
=
(
isinstance
(
inp2
.
type
,
CudaNdarrayType
)
or
(
inp2
.
owner
and
isinstance
(
inp2
.
owner
.
op
,
HostFromGpu
)))
if
inp1_on_gpu
or
inp2_on_gpu
:
conv
=
node
.
op
inps
=
list
(
node
.
inputs
)
inps
[
0
]
=
as_cuda_ndarray_variable
(
inps
[
0
])
inps
[
1
]
=
as_cuda_ndarray_variable
(
inps
[
1
])
out
=
conv
(
*
inps
)
# out is on the GPU because both inputs are.
out
=
theano
.
tensor
.
patternbroadcast
(
out
,
node
.
outputs
[
0
]
.
broadcastable
)
out
.
values_eq_approx
=
values_eq_approx_high_tol
# If the original output was on CPU, we have to transfer it
if
isinstance
(
node
.
outputs
[
0
]
.
type
,
TensorType
):
return
[
as_tensor_variable
(
out
)]
else
:
return
[
out
]
register_gpu
()(
local_conv2d_gpu_conv
)
### Cudnn Opt
@local_optimizer
([
AbstractConv2d
,
AbstractConv2d_gradWeights
,
AbstractConv2d_gradInputs
])
def
local_conv2d_cudnn
(
node
):
inp1
=
node
.
inputs
[
0
]
inp2
=
node
.
inputs
[
1
]
if
not
isinstance
(
inp1
.
type
,
CudaNdarrayType
)
or
\
not
isinstance
(
inp2
.
type
,
CudaNdarrayType
):
return
None
if
not
dnn_available
():
return
None
if
node
.
op
.
filters_flip
:
conv_mode
=
'conv'
else
:
conv_mode
=
'cross'
if
(
isinstance
(
node
.
op
,
AbstractConv2d
)):
rval
=
dnn_conv
(
inp1
,
inp2
,
border_mode
=
node
.
op
.
border_mode
,
subsample
=
node
.
op
.
subsample
,
direction_hint
=
'forward'
,
conv_mode
=
conv_mode
)
return
[
rval
]
if
(
isinstance
(
node
.
op
,
AbstractConv2d_gradWeights
)):
shape
=
(
inp2
.
shape
[
1
],
inp1
.
shape
[
1
],
node
.
inputs
[
2
][
0
],
node
.
inputs
[
2
][
1
])
rval
=
dnn_gradweight
(
inp1
,
inp2
,
shape
,
border_mode
=
node
.
op
.
border_mode
,
subsample
=
node
.
op
.
subsample
,
conv_mode
=
conv_mode
)
return
[
rval
]
if
(
isinstance
(
node
.
op
,
AbstractConv2d_gradInputs
)):
shape
=
(
inp2
.
shape
[
0
],
inp1
.
shape
[
1
],
node
.
inputs
[
2
][
0
],
node
.
inputs
[
2
][
1
])
rval
=
dnn_gradinput
(
inp1
,
inp2
,
shape
,
border_mode
=
node
.
op
.
border_mode
,
subsample
=
node
.
op
.
subsample
,
conv_mode
=
conv_mode
)
return
[
rval
]
register_specialize_device
(
local_conv2d_cudnn
,
'cudnn'
)
### Corrmm opt
@local_optimizer
([
AbstractConv2d
])
def
local_conv2d_corrmm
(
node
):
img
,
kern
=
node
.
inputs
if
(
not
isinstance
(
img
.
type
,
CudaNdarrayType
)
or
not
isinstance
(
kern
.
type
,
CudaNdarrayType
)):
return
None
border_mode
=
node
.
op
.
border_mode
subsample
=
node
.
op
.
subsample
if
(
border_mode
==
'full'
)
and
(
subsample
==
(
1
,
1
)):
if
not
node
.
op
.
filters_flip
:
kern
=
kern
[:,
:,
::
-
1
,
::
-
1
]
# need to dimshuffle the kernel for full convolution
kern
=
kern
.
dimshuffle
(
1
,
0
,
2
,
3
)
# call GpuCorrMM_gradInputs
rval
=
GpuCorrMM_gradInputs
(
'valid'
,
subsample
)(
gpu_contiguous
(
kern
),
gpu_contiguous
(
img
))
else
:
# need to flip the kernel if necessary
if
node
.
op
.
filters_flip
:
kern
=
kern
[:,
:,
::
-
1
,
::
-
1
]
# By default use GpuCorrMM
rval
=
GpuCorrMM
(
border_mode
,
subsample
)(
gpu_contiguous
(
img
),
gpu_contiguous
(
kern
))
# call GpuCorrMM_gradWeights if good
# (the latter is faster if batchsize * kernelHeight * kernelWidth
# is larger than inputChannels * outputHeight * outputWidth.
# GpuConv does not always store information on the batchsize and
# channels, though, so we only use what information we have.)
if
((
subsample
==
(
1
,
1
))
and
(
node
.
op
.
imshp
is
not
None
)
and
(
None
not
in
node
.
op
.
imshp
[
-
2
:])
and
(
node
.
op
.
kshp
is
not
None
)
and
(
None
not
in
node
.
op
.
kshp
)):
# we know the kernel and output size
prod1
=
node
.
op
.
kshp
[
0
]
*
node
.
op
.
kshp
[
1
]
prod2
=
((
node
.
op
.
imshp
[
-
2
]
-
node
.
op
.
kshp
[
0
]
+
1
)
*
(
node
.
op
.
imshp
[
-
1
]
-
node
.
op
.
kshp
[
1
]
+
1
))
if
((
node
.
op
.
bsize
is
not
None
)
and
(
len
(
node
.
op
.
imshp
)
==
3
)
and
(
node
.
op
.
imshp
[
0
]
is
not
None
)):
# we also know batchsize and input channels
prod1
*=
node
.
op
.
bsize
prod2
*=
node
.
op
.
imshp
[
0
]
# compare to decide
if
prod1
>
prod2
:
# (we need to wrap the result in as_cuda_ndarray_variable,
# because we are not allowed to replace a CudaNdarray with
# a DimShuffle instance in a graph optimization)
rval
=
theano
.
sandbox
.
cuda
.
as_cuda_ndarray_variable
(
GpuCorrMM_gradWeights
(
border_mode
,
subsample
)(
gpu_contiguous
(
img
.
dimshuffle
(
1
,
0
,
2
,
3
)),
gpu_contiguous
(
kern
.
dimshuffle
(
1
,
0
,
2
,
3
))
)
.
dimshuffle
(
1
,
0
,
2
,
3
))
return
[
rval
]
register_specialize_device
(
local_conv2d_corrmm
,
'conv_gemm'
)
@local_optimizer
([
AbstractConv2d_gradWeights
])
def
local_conv2d_gradweight_corrmm
(
node
):
img
,
topgrad
,
shape
=
node
.
inputs
if
not
isinstance
(
img
.
type
,
CudaNdarrayType
)
or
\
not
isinstance
(
topgrad
.
type
,
CudaNdarrayType
):
return
None
rval
=
GpuCorrMM_gradWeights
(
border_mode
=
node
.
op
.
border_mode
,
subsample
=
node
.
op
.
subsample
)(
gpu_contiguous
(
img
),
gpu_contiguous
(
topgrad
),
shape
)
if
node
.
op
.
filters_flip
:
rval
=
rval
[:,
:,
::
-
1
,
::
-
1
]
rval
=
patternbroadcast
(
rval
,
node
.
outputs
[
0
]
.
broadcastable
)
rval
=
as_cuda_ndarray_variable
(
rval
)
return
[
rval
]
register_specialize_device
(
local_conv2d_gradweight_corrmm
,
'conv_gemm'
)
@local_optimizer
([
AbstractConv2d_gradInputs
])
def
local_conv2d_gradinputs_corrmm
(
node
):
kern
,
topgrad
,
shape
=
node
.
inputs
if
not
isinstance
(
kern
.
type
,
CudaNdarrayType
)
or
\
not
isinstance
(
topgrad
.
type
,
CudaNdarrayType
):
return
None
if
node
.
op
.
filters_flip
:
kern
=
kern
[:,
:,
::
-
1
,
::
-
1
]
rval
=
GpuCorrMM_gradInputs
(
border_mode
=
node
.
op
.
border_mode
,
subsample
=
node
.
op
.
subsample
)(
gpu_contiguous
(
kern
),
gpu_contiguous
(
topgrad
),
shape
)
return
[
rval
]
register_specialize_device
(
local_conv2d_gradinputs_corrmm
,
'conv_gemm'
)
### Cpu Optmization
### Cpu Optmization
@local_optimizer
([
AbstractConv2d
])
@local_optimizer
([
AbstractConv2d
])
def
local_conv2d_cpu
(
node
):
def
local_conv2d_cpu
(
node
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论