Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
015b42a4
提交
015b42a4
authored
8月 18, 2014
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pep8
上级
c3a461f7
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
64 行增加
和
48 行删除
+64
-48
GpuConv3D.py
theano/sandbox/cuda/GpuConv3D.py
+15
-6
GpuConvGrad3D.py
theano/sandbox/cuda/GpuConvGrad3D.py
+23
-17
GpuConvTransp3D.py
theano/sandbox/cuda/GpuConvTransp3D.py
+26
-25
没有找到文件。
theano/sandbox/cuda/GpuConv3D.py
浏览文件 @
015b42a4
...
@@ -3,12 +3,14 @@ import numpy
...
@@ -3,12 +3,14 @@ import numpy
import
theano
import
theano
import
theano.tensor
as
T
import
theano.tensor
as
T
from
theano.gof
import
local_optimizer
from
theano.gof
import
local_optimizer
from
theano.sandbox.cuda.basic_ops
import
as_cuda_ndarray_variable
,
host_from_gpu
,
HostFromGpu
from
theano.sandbox.cuda.basic_ops
import
(
as_cuda_ndarray_variable
,
host_from_gpu
,
HostFromGpu
)
from
theano.misc
import
strutil
from
theano.misc
import
strutil
from
theano.tensor.nnet.Conv3D
import
Conv3D
from
theano.tensor.nnet.Conv3D
import
Conv3D
from
theano.sandbox.cuda.opt
import
register_opt
from
theano.sandbox.cuda.opt
import
register_opt
from
theano.sandbox.cuda
import
CudaNdarrayType
,
GpuOp
from
theano.sandbox.cuda
import
CudaNdarrayType
,
GpuOp
class
GpuConv3D
(
GpuOp
):
class
GpuConv3D
(
GpuOp
):
""" GPU implementation of Conv3D """
""" GPU implementation of Conv3D """
...
@@ -32,12 +34,14 @@ class GpuConv3D(GpuOp):
...
@@ -32,12 +34,14 @@ class GpuConv3D(GpuOp):
W_
=
as_cuda_ndarray_variable
(
W
)
W_
=
as_cuda_ndarray_variable
(
W
)
b_
=
as_cuda_ndarray_variable
(
b
)
b_
=
as_cuda_ndarray_variable
(
b
)
d_
=
T
.
as_tensor_variable
(
d
)
d_
=
T
.
as_tensor_variable
(
d
)
broad
=
(
V_
.
broadcastable
[
0
],
W_
.
broadcastable
[
0
],
False
,
False
,
False
)
return
theano
.
Apply
(
self
,
inputs
=
[
V_
,
W_
,
b_
,
d_
],
return
theano
.
Apply
(
self
,
inputs
=
[
V_
,
W_
,
b_
,
d_
],
outputs
=
[
CudaNdarrayType
(
dtype
=
V_
.
dtype
,
broadcastable
=
(
V_
.
broadcastable
[
0
],
W_
.
broadcastable
[
0
],
False
,
False
,
False
))()
]
)
outputs
=
[
CudaNdarrayType
(
dtype
=
V_
.
dtype
,
broadcastable
=
broad
)()])
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
()
return
()
def
c_code
(
self
,
node
,
nodename
,
inputs
,
outputs
,
sub
):
def
c_code
(
self
,
node
,
nodename
,
inputs
,
outputs
,
sub
):
V
,
W
,
b
,
d
=
inputs
V
,
W
,
b
,
d
=
inputs
fail
=
sub
[
'fail'
]
fail
=
sub
[
'fail'
]
...
@@ -220,7 +224,7 @@ if(!work_complete){
...
@@ -220,7 +224,7 @@ if(!work_complete){
}}}}}}} //extra scope so error handler jumps don't cross declarations
}}}}}}} //extra scope so error handler jumps don't cross declarations
///////////// < /code generated by GpuConv3D >
///////////// < /code generated by GpuConv3D >
"""
"""
return
strutil
.
render_string
(
codeSource
,
locals
())
return
strutil
.
render_string
(
codeSource
,
locals
())
def
c_support_code_apply
(
self
,
node
,
nodename
):
def
c_support_code_apply
(
self
,
node
,
nodename
):
# This code is not sensitive to the ignore_border flag.
# This code is not sensitive to the ignore_border flag.
...
@@ -283,11 +287,16 @@ conv_rows_stack( float* img, float* kern, float* bias, float* out,
...
@@ -283,11 +287,16 @@ conv_rows_stack( float* img, float* kern, float* bias, float* out,
gpu_convd
=
GpuConv3D
()
gpu_convd
=
GpuConv3D
()
@register_opt
()
@register_opt
()
@local_optimizer
([
Conv3D
])
@local_optimizer
([
Conv3D
])
def
local_gpu_conv3d
(
node
):
def
local_gpu_conv3d
(
node
):
if
isinstance
(
node
.
op
,
Conv3D
):
if
isinstance
(
node
.
op
,
Conv3D
):
if
numpy
.
any
([
i
.
owner
and
isinstance
(
i
.
owner
.
op
,
HostFromGpu
)
for
i
in
node
.
inputs
]):
if
numpy
.
any
([
i
.
owner
and
isinstance
(
i
.
owner
.
op
,
HostFromGpu
)
for
i
in
node
.
inputs
]):
if
numpy
.
all
([
o
.
type
.
dtype
==
'float32'
for
o
in
node
.
outputs
]):
if
numpy
.
all
([
o
.
type
.
dtype
==
'float32'
for
o
in
node
.
outputs
]):
V
,
W
,
b
,
d
=
node
.
inputs
V
,
W
,
b
,
d
=
node
.
inputs
return
[
host_from_gpu
(
gpu_convd
(
as_cuda_ndarray_variable
(
V
),
as_cuda_ndarray_variable
(
W
),
as_cuda_ndarray_variable
(
b
),
d
))]
return
[
host_from_gpu
(
gpu_convd
(
as_cuda_ndarray_variable
(
V
),
as_cuda_ndarray_variable
(
W
),
as_cuda_ndarray_variable
(
b
),
d
))]
theano/sandbox/cuda/GpuConvGrad3D.py
浏览文件 @
015b42a4
...
@@ -12,7 +12,6 @@ from theano.sandbox.cuda import (CudaNdarrayType, HostFromGpu,
...
@@ -12,7 +12,6 @@ from theano.sandbox.cuda import (CudaNdarrayType, HostFromGpu,
host_from_gpu
,
GpuOp
)
host_from_gpu
,
GpuOp
)
class
GpuConvGrad3D
(
GpuOp
):
class
GpuConvGrad3D
(
GpuOp
):
""" GPU version of gradient of ConvGrad3D with respect to W """
""" GPU version of gradient of ConvGrad3D with respect to W """
...
@@ -27,9 +26,10 @@ class GpuConvGrad3D(GpuOp):
...
@@ -27,9 +26,10 @@ class GpuConvGrad3D(GpuOp):
d_
=
T
.
as_tensor_variable
(
d
)
d_
=
T
.
as_tensor_variable
(
d
)
WShape_
=
T
.
as_tensor_variable
(
WShape
)
WShape_
=
T
.
as_tensor_variable
(
WShape
)
dCdH_
=
as_cuda_ndarray_variable
(
dCdH
)
dCdH_
=
as_cuda_ndarray_variable
(
dCdH
)
broad
=
(
False
,)
*
5
return
theano
.
Apply
(
self
,
inputs
=
[
V_
,
d_
,
WShape_
,
dCdH_
],
return
theano
.
Apply
(
self
,
inputs
=
[
V_
,
d_
,
WShape_
,
dCdH_
],
outputs
=
[
CudaNdarrayType
(
dtype
=
V_
.
dtype
,
broadcastable
=
(
False
,)
*
5
)()])
outputs
=
[
CudaNdarrayType
(
dtype
=
V_
.
dtype
,
broadcastable
=
broad
)()])
def
perform_
(
self
,
node
,
inputs
,
output_storage
):
def
perform_
(
self
,
node
,
inputs
,
output_storage
):
V
,
d
,
WShape
,
dCdH
=
inputs
V
,
d
,
WShape
,
dCdH
=
inputs
...
@@ -51,18 +51,18 @@ class GpuConvGrad3D(GpuOp):
...
@@ -51,18 +51,18 @@ class GpuConvGrad3D(GpuOp):
dCdW
=
numpy
.
zeros
(
WShape
,
dtype
=
V
.
dtype
)
dCdW
=
numpy
.
zeros
(
WShape
,
dtype
=
V
.
dtype
)
#block
#
block
for
j
in
xrange
(
0
,
WShape
[
0
]):
for
j
in
xrange
(
0
,
WShape
[
0
]):
for
z
in
xrange
(
0
,
WShape
[
1
]):
for
z
in
xrange
(
0
,
WShape
[
1
]):
for
k
in
xrange
(
0
,
WShape
[
2
]):
for
k
in
xrange
(
0
,
WShape
[
2
]):
for
l
in
xrange
(
0
,
WShape
[
3
]):
for
l
in
xrange
(
0
,
WShape
[
3
]):
#threads
#
threads
for
m
in
xrange
(
0
,
WShape
[
4
]):
for
m
in
xrange
(
0
,
WShape
[
4
]):
#thread
#
thread
for
i
in
xrange
(
0
,
batchSize
):
for
i
in
xrange
(
0
,
batchSize
):
for
p
in
xrange
(
0
,
outputHeight
):
for
p
in
xrange
(
0
,
outputHeight
):
for
q
in
xrange
(
0
,
outputWidth
):
for
q
in
xrange
(
0
,
outputWidth
):
for
r
in
xrange
(
0
,
outputDur
):
for
r
in
xrange
(
0
,
outputDur
):
dCdW
[
j
,
z
,
k
,
l
,
m
]
+=
dCdH
[
i
,
j
,
p
,
q
,
r
]
*
V
[
i
,
z
,
dr
*
p
+
k
,
dc
*
q
+
l
,
dt
*
r
+
m
]
dCdW
[
j
,
z
,
k
,
l
,
m
]
+=
dCdH
[
i
,
j
,
p
,
q
,
r
]
*
V
[
i
,
z
,
dr
*
p
+
k
,
dc
*
q
+
l
,
dt
*
r
+
m
]
output_storage
[
0
][
0
]
=
dCdW
output_storage
[
0
][
0
]
=
dCdW
...
@@ -340,11 +340,17 @@ convgrad_rows_stack( float* img, float* dCdH, float* dCdW,
...
@@ -340,11 +340,17 @@ convgrad_rows_stack( float* img, float* dCdH, float* dCdW,
gpu_conv_grad3d
=
GpuConvGrad3D
()
gpu_conv_grad3d
=
GpuConvGrad3D
()
@register_opt
()
@register_opt
()
@local_optimizer
([
ConvGrad3D
])
@local_optimizer
([
ConvGrad3D
])
def
local_gpu_conv_gradd
(
node
):
def
local_gpu_conv_gradd
(
node
):
if
isinstance
(
node
.
op
,
ConvGrad3D
):
if
isinstance
(
node
.
op
,
ConvGrad3D
):
if
numpy
.
any
([
i
.
owner
and
isinstance
(
i
.
owner
.
op
,
HostFromGpu
)
for
i
in
node
.
inputs
]):
if
numpy
.
any
([
i
.
owner
and
isinstance
(
i
.
owner
.
op
,
HostFromGpu
)
for
i
in
node
.
inputs
]):
if
numpy
.
all
([
o
.
type
.
dtype
==
'float32'
for
o
in
node
.
outputs
]):
if
numpy
.
all
([
o
.
type
.
dtype
==
'float32'
for
o
in
node
.
outputs
]):
V
,
d
,
WShape
,
dCdH
=
node
.
inputs
V
,
d
,
WShape
,
dCdH
=
node
.
inputs
return
[
host_from_gpu
(
gpu_conv_grad3d
(
as_cuda_ndarray_variable
(
V
),
d
,
WShape
,
as_cuda_ndarray_variable
(
dCdH
)))]
return
[
host_from_gpu
(
gpu_conv_grad3d
(
as_cuda_ndarray_variable
(
V
),
d
,
WShape
,
as_cuda_ndarray_variable
(
dCdH
)))]
theano/sandbox/cuda/GpuConvTransp3D.py
浏览文件 @
015b42a4
...
@@ -15,13 +15,13 @@ from theano.sandbox.cuda import (CudaNdarrayType, HostFromGpu,
...
@@ -15,13 +15,13 @@ from theano.sandbox.cuda import (CudaNdarrayType, HostFromGpu,
class
GpuConvTransp3D
(
GpuOp
):
class
GpuConvTransp3D
(
GpuOp
):
""" The gpu version of ConvTransp3D """
""" The gpu version of ConvTransp3D """
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
return
type
(
self
)
==
type
(
other
)
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
return
hash
(
type
(
self
))
def
make_node
(
self
,
W
,
b
,
d
,
H
,
RShape
=
None
):
def
make_node
(
self
,
W
,
b
,
d
,
H
,
RShape
=
None
):
W_
=
as_cuda_ndarray_variable
(
W
)
W_
=
as_cuda_ndarray_variable
(
W
)
b_
=
as_cuda_ndarray_variable
(
b
)
b_
=
as_cuda_ndarray_variable
(
b
)
d_
=
T
.
as_tensor_variable
(
d
)
d_
=
T
.
as_tensor_variable
(
d
)
...
@@ -29,22 +29,21 @@ class GpuConvTransp3D(GpuOp):
...
@@ -29,22 +29,21 @@ class GpuConvTransp3D(GpuOp):
if
RShape
:
if
RShape
:
RShape_
=
T
.
as_tensor_variable
(
RShape
)
RShape_
=
T
.
as_tensor_variable
(
RShape
)
else
:
else
:
RShape_
=
T
.
as_tensor_variable
([
-
1
,
-
1
,
-
1
])
RShape_
=
T
.
as_tensor_variable
([
-
1
,
-
1
,
-
1
])
return
theano
.
Apply
(
self
,
inputs
=
[
W_
,
b_
,
d_
,
H_
,
RShape_
],
return
theano
.
Apply
(
self
,
inputs
=
[
W_
,
b_
,
d_
,
H_
,
RShape_
],
outputs
=
[
CudaNdarrayType
(
dtype
=
H_
.
dtype
,
outputs
=
[
CudaNdarrayType
(
dtype
=
H_
.
dtype
,
broadcastable
=
(
False
,)
*
5
)()])
broadcastable
=
(
False
,)
*
5
)()])
def
infer_shape
(
self
,
node
,
input_shapes
):
def
infer_shape
(
self
,
node
,
input_shapes
):
W
,
b
,
d
,
H
,
RShape
=
node
.
inputs
W
,
b
,
d
,
H
,
RShape
=
node
.
inputs
W_shape
,
b_shape
,
d_shape
,
H_shape
,
RShape_shape
=
input_shapes
W_shape
,
b_shape
,
d_shape
,
H_shape
,
RShape_shape
=
input_shapes
return
[(
H_shape
[
0
],
W_shape
[
1
],
RShape
[
0
],
RShape
[
1
],
RShape
[
2
])]
return
[(
H_shape
[
0
],
W_shape
[
1
],
RShape
[
0
],
RShape
[
1
],
RShape
[
2
])]
def
perform_
(
self
,
node
,
inputs
,
output_storage
):
def
perform_
(
self
,
node
,
inputs
,
output_storage
):
W
,
b
,
d
,
H
,
RShape
=
inputs
W
,
b
,
d
,
H
,
RShape
=
inputs
print
"
\t\t\t\t
GpuConvTransp3D python code still uses old format"
print
"
\t\t\t\t
GpuConvTransp3D python code still uses old format"
output_storage
[
0
][
0
]
=
computeR
(
W
,
b
,
d
,
H
,
RShape
)
output_storage
[
0
][
0
]
=
computeR
(
W
,
b
,
d
,
H
,
RShape
)
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
()
return
()
...
@@ -263,7 +262,7 @@ if(!work_complete){
...
@@ -263,7 +262,7 @@ if(!work_complete){
}}}}}} // for fail
}}}}}} // for fail
///////////// < /code generated by GpuConvTransp3D >
///////////// < /code generated by GpuConvTransp3D >
"""
"""
return
strutil
.
render_string
(
codeSource
,
locals
())
return
strutil
.
render_string
(
codeSource
,
locals
())
def
c_support_code_apply
(
self
,
node
,
nodename
):
def
c_support_code_apply
(
self
,
node
,
nodename
):
# This code is not sensitive to the ignore_border flag.
# This code is not sensitive to the ignore_border flag.
...
@@ -347,18 +346,20 @@ conv_transp_rows_stack( float* H, float* kern, float* bias, float* R,
...
@@ -347,18 +346,20 @@ conv_transp_rows_stack( float* H, float* kern, float* bias, float* R,
gpu_conv_transpd
=
GpuConvTransp3D
()
gpu_conv_transpd
=
GpuConvTransp3D
()
@register_opt
()
@register_opt
()
@local_optimizer
([
ConvTransp3D
])
@local_optimizer
([
ConvTransp3D
])
def
local_gpu_conv_transpd
(
node
):
def
local_gpu_conv_transpd
(
node
):
if
isinstance
(
node
.
op
,
ConvTransp3D
):
if
isinstance
(
node
.
op
,
ConvTransp3D
):
if
numpy
.
any
([
i
.
owner
and
isinstance
(
i
.
owner
.
op
,
HostFromGpu
)
for
i
in
node
.
inputs
]):
if
numpy
.
any
([
i
.
owner
and
isinstance
(
i
.
owner
.
op
,
HostFromGpu
)
for
i
in
node
.
inputs
]):
if
numpy
.
all
([
o
.
type
.
dtype
==
'float32'
for
o
in
node
.
outputs
]):
if
numpy
.
all
([
o
.
type
.
dtype
==
'float32'
for
o
in
node
.
outputs
]):
W
,
b
,
d
,
H
,
RShape
=
node
.
inputs
W
,
b
,
d
,
H
,
RShape
=
node
.
inputs
return
[
host_from_gpu
(
gpu_conv_transpd
(
W
,
b
,
d
,
H
,
RShape
))]
return
[
host_from_gpu
(
gpu_conv_transpd
(
W
,
b
,
d
,
H
,
RShape
))]
#If the input size wasn't a multiple of D we may need to cause some automatic padding to get the right size of reconstruction
#If the input size wasn't a multiple of D we may need to cause some automatic padding to get the right size of reconstruction
def
computeR
(
W
,
b
,
d
,
H
,
Rshape
=
None
):
def
computeR
(
W
,
b
,
d
,
H
,
Rshape
=
None
):
assert
len
(
W
.
shape
)
==
5
assert
len
(
W
.
shape
)
==
5
assert
len
(
H
.
shape
)
==
5
assert
len
(
H
.
shape
)
==
5
assert
len
(
b
.
shape
)
==
1
assert
len
(
b
.
shape
)
==
1
...
@@ -370,7 +371,7 @@ def computeR(W,b,d,H,Rshape = None):
...
@@ -370,7 +371,7 @@ def computeR(W,b,d,H,Rshape = None):
assert
outputChannelsAgain
==
outputChannels
assert
outputChannelsAgain
==
outputChannels
assert
b
.
shape
[
0
]
==
inputChannels
assert
b
.
shape
[
0
]
==
inputChannels
dr
,
dc
,
dt
=
d
dr
,
dc
,
dt
=
d
assert
dr
>
0
assert
dr
>
0
assert
dc
>
0
assert
dc
>
0
assert
dt
>
0
assert
dt
>
0
...
@@ -398,14 +399,14 @@ def computeR(W,b,d,H,Rshape = None):
...
@@ -398,14 +399,14 @@ def computeR(W,b,d,H,Rshape = None):
videoWidth
,
videoDur
)
,
dtype
=
H
.
dtype
)
videoWidth
,
videoDur
)
,
dtype
=
H
.
dtype
)
#R[i,j,r,c,t] = b_j + sum_{rc,rk | d \circ rc + rk = r} sum_{cc,ck | ...} sum_{tc,tk | ...} sum_k W[k, j, rk, ck, tk] * H[i,k,rc,cc,tc]
#R[i,j,r,c,t] = b_j + sum_{rc,rk | d \circ rc + rk = r} sum_{cc,ck | ...} sum_{tc,tk | ...} sum_k W[k, j, rk, ck, tk] * H[i,k,rc,cc,tc]
for
i
in
xrange
(
0
,
batchSize
):
for
i
in
xrange
(
0
,
batchSize
):
#print '\texample '+str(i+1)+'/'+str(batchSize)
#print '\texample '+str(i+1)+'/'+str(batchSize)
for
j
in
xrange
(
0
,
inputChannels
):
for
j
in
xrange
(
0
,
inputChannels
):
#print '\t\tfeature map '+str(j+1)+'/'+str(inputChannels)
#print '\t\tfeature map '+str(j+1)+'/'+str(inputChannels)
for
r
in
xrange
(
0
,
videoHeight
):
for
r
in
xrange
(
0
,
videoHeight
):
#print '\t\t\trow '+str(r+1)+'/'+str(videoHeight)
#print '\t\t\trow '+str(r+1)+'/'+str(videoHeight)
for
c
in
xrange
(
0
,
videoWidth
):
for
c
in
xrange
(
0
,
videoWidth
):
for
t
in
xrange
(
0
,
videoDur
):
for
t
in
xrange
(
0
,
videoDur
):
R
[
i
,
j
,
r
,
c
,
t
]
=
b
[
j
]
R
[
i
,
j
,
r
,
c
,
t
]
=
b
[
j
]
ftc
=
max
([
0
,
int
(
numpy
.
ceil
(
float
(
t
-
filterDur
+
1
)
/
float
(
dt
)))
])
ftc
=
max
([
0
,
int
(
numpy
.
ceil
(
float
(
t
-
filterDur
+
1
)
/
float
(
dt
)))
])
...
@@ -432,16 +433,16 @@ def computeR(W,b,d,H,Rshape = None):
...
@@ -432,16 +433,16 @@ def computeR(W,b,d,H,Rshape = None):
R
[
i
,
j
,
r
,
c
,
t
]
+=
numpy
.
dot
(
W
[:,
j
,
rk
,
ck
,
tk
],
H
[
i
,:,
rc
,
cc
,
tc
]
)
R
[
i
,
j
,
r
,
c
,
t
]
+=
numpy
.
dot
(
W
[:,
j
,
rk
,
ck
,
tk
],
H
[
i
,:,
rc
,
cc
,
tc
]
)
tc
+=
1
tc
+=
1
""
#
close loop over tc
""
#
close loop over tc
cc
+=
1
cc
+=
1
""
#
close loop over cc
""
#
close loop over cc
rc
+=
1
rc
+=
1
""
#
close loop over rc
""
#
close loop over rc
""
#
close loop over t
""
#
close loop over t
""
#
close loop over c
""
#
close loop over c
""
#
close loop over r
""
#
close loop over r
""
#
close loop over j
""
#
close loop over j
""
#
close loop over i
""
#
close loop over i
return
R
return
R
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论