Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
85b8a90f
提交
85b8a90f
authored
8月 07, 2014
作者:
Arjun Jain
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support for non-square images and kernels
上级
4c55bc4b
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
89 行增加
和
89 行删除
+89
-89
blas.py
theano/sandbox/cuda/blas.py
+7
-3
caffe_common.hpp
theano/sandbox/cuda/caffe_common.hpp
+0
-6
conv_gemm.cu
theano/sandbox/cuda/conv_gemm.cu
+42
-42
test_conv_cuda_ndarray.py
theano/sandbox/cuda/tests/test_conv_cuda_ndarray.py
+40
-38
没有找到文件。
theano/sandbox/cuda/blas.py
浏览文件 @
85b8a90f
...
@@ -606,7 +606,9 @@ class GpuCorrMM(GpuOp):
...
@@ -606,7 +606,9 @@ class GpuCorrMM(GpuOp):
//Optional args
//Optional args
int dx =
%(dx)
s;
int dx =
%(dx)
s;
int dy =
%(dy)
s;
int dy =
%(dy)
s;
int pad = 0;
int padH = 0;
int padW = 0;
CudaNdarray * img =
%(img)
s;
CudaNdarray * img =
%(img)
s;
CudaNdarray * kern =
%(kern)
s;
CudaNdarray * kern =
%(kern)
s;
CudaNdarray * out2 = NULL;
CudaNdarray * out2 = NULL;
...
@@ -640,7 +642,9 @@ class GpuCorrMM(GpuOp):
...
@@ -640,7 +642,9 @@ class GpuCorrMM(GpuOp):
{
{
logical_rows = CudaNdarray_HOST_DIMS(img)[2] + CudaNdarray_HOST_DIMS(kern)[2] - 1;
logical_rows = CudaNdarray_HOST_DIMS(img)[2] + CudaNdarray_HOST_DIMS(kern)[2] - 1;
logical_cols = CudaNdarray_HOST_DIMS(img)[3] + CudaNdarray_HOST_DIMS(kern)[3] - 1;
logical_cols = CudaNdarray_HOST_DIMS(img)[3] + CudaNdarray_HOST_DIMS(kern)[3] - 1;
pad = CudaNdarray_HOST_DIMS(kern)[2] - 1;
padH = CudaNdarray_HOST_DIMS(kern)[2] - 1;
padW = CudaNdarray_HOST_DIMS(kern)[3] - 1;
}
}
out_dim[2] = ceil_intdiv(logical_rows, dx);
out_dim[2] = ceil_intdiv(logical_rows, dx);
out_dim[3] = ceil_intdiv(logical_cols, dy);
out_dim[3] = ceil_intdiv(logical_cols, dy);
...
@@ -658,7 +662,7 @@ class GpuCorrMM(GpuOp):
...
@@ -658,7 +662,7 @@ class GpuCorrMM(GpuOp):
}
}
out2 = corrMM(
%(img)
s,
%(kern)
s,
%(out)
s, pad);
out2 = corrMM(
%(img)
s,
%(kern)
s,
%(out)
s, pad
H, padW
);
if (out2==NULL){
if (out2==NULL){
%(fail)
s
%(fail)
s
}
}
...
...
theano/sandbox/cuda/caffe_common.hpp
浏览文件 @
85b8a90f
...
@@ -30,12 +30,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
...
@@ -30,12 +30,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cuda.h>
#include <cuda.h>
#include <driver_types.h> // cuda driver types
#include <driver_types.h> // cuda driver types
// CUDA: grid stride looping
#define CUDA_KERNEL_LOOP(i, n) \
for (int i = blockIdx.x * blockDim.x + threadIdx.x; \
i < (n); \
i += blockDim.x * gridDim.x)
// CUDA: thread number configuration.
// CUDA: thread number configuration.
// Use 1024 threads per block, which requires cuda sm_2x or above,
// Use 1024 threads per block, which requires cuda sm_2x or above,
// or fall back to attempt compatibility (best of luck to you).
// or fall back to attempt compatibility (best of luck to you).
...
...
theano/sandbox/cuda/conv_gemm.cu
浏览文件 @
85b8a90f
...
@@ -22,30 +22,44 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
...
@@ -22,30 +22,44 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
*/
// Reference code: https://github.com/torch/cunn/blob/master/SpatialConvolutionMM.cu
#undef _GLIBCXX_ATOMIC_BUILTINS
#undef _GLIBCXX_ATOMIC_BUILTINS
#include <Python.h>
#include <Python.h>
#include "cuda_ndarray.cuh"
#include "cuda_ndarray.cuh"
#include "caffe_common.hpp"
#include "caffe_common.hpp"
// CUDA: grid stride looping
#define CUDA_KERNEL_LOOP(i, n) \
for (int i = blockIdx.x * blockDim.x + threadIdx.x; \
i < (n); \
i += blockDim.x * gridDim.x)
// Use 1024 threads per block, which requires cuda sm_2x or above
const int CUDA_NUM_THREADS = 1024;
// CUDA: number of blocks for threads.
inline int GET_BLOCKS(const int N) {
return (N + CUDA_NUM_THREADS - 1) / CUDA_NUM_THREADS;
}
// Kernel for fast unfold+copy
// Kernel for fast unfold+copy
// (borrowed from Caffe: https://github.com/BVLC/caffe/blob/master/src/caffe/layers/conv_layer.cu)
// (borrowed from Caffe: https://github.com/BVLC/caffe/blob/master/src/caffe/layers/conv_layer.cu)
// Reference code: https://github.com/torch/cunn/blob/master/SpatialConvolutionMM.cu
__global__ void im2col_kernel(const int n, const float* data_im,
__global__ void im2col_kernel(const int n, const float* data_im,
const int height, const int width, const int ksize
, const int pad
,
const int height, const int width, const int ksize
_h, const int ksize_w, const int pad_h
,
const int stride
, const int height_col, const int width_col,
const int pad_w, const int stride_h, const int stride_w
, const int height_col, const int width_col,
float* data_col) {
float* data_col) {
CUDA_KERNEL_LOOP(index, n) {
CUDA_KERNEL_LOOP(index, n) {
int w_out = index % width_col;
int w_out = index % width_col;
index /= width_col;
index /= width_col;
int h_out = index % height_col;
int h_out = index % height_col;
int channel_in = index / height_col;
int channel_in = index / height_col;
int channel_out = channel_in * ksize
* ksize
;
int channel_out = channel_in * ksize
_h * ksize_w
;
int h_in = h_out * stride
- pad
;
int h_in = h_out * stride
_h - pad_h
;
int w_in = w_out * stride
- pad
;
int w_in = w_out * stride
_w - pad_w
;
data_col += (channel_out * height_col + h_out) * width_col + w_out;
data_col += (channel_out * height_col + h_out) * width_col + w_out;
data_im += (channel_in * height + h_in) * width + w_in;
data_im += (channel_in * height + h_in) * width + w_in;
for (int i = 0; i < ksize; ++i) {
for (int i = 0; i < ksize
_h
; ++i) {
for (int j = 0; j < ksize; ++j) {
for (int j = 0; j < ksize
_w
; ++j) {
int h = h_in + i;
int h = h_in + i;
int w = w_in + j;
int w = w_in + j;
*data_col = (h >= 0 && w >= 0 && h < height && w < width) ?
*data_col = (h >= 0 && w >= 0 && h < height && w < width) ?
...
@@ -57,20 +71,19 @@ __global__ void im2col_kernel(const int n, const float* data_im,
...
@@ -57,20 +71,19 @@ __global__ void im2col_kernel(const int n, const float* data_im,
}
}
void im2col(const float* data_im, const int channels,
void im2col(const float* data_im, const int channels,
const int height, const int width, const int ksize
, const int pad
,
const int height, const int width, const int ksize
_h, const int ksize_w, const int pad_h
,
const int stride
, float* data_col) {
const int pad_w, const int stride_h, const int stride_w
, float* data_col) {
// We are going to launch channels * height_col * width_col kernels, each
// We are going to launch channels * height_col * width_col kernels, each
// kernel responsible for copying a single-channel grid.
// kernel responsible for copying a single-channel grid.
int height_col = (height + 2 * pad
- ksize) / stride
+ 1;
int height_col = (height + 2 * pad
_h - ksize_h) / stride_h
+ 1;
int width_col = (width + 2 * pad
- ksize) / stride
+ 1;
int width_col = (width + 2 * pad
_w - ksize_w) / stride_w
+ 1;
int num_kernels = channels * height_col * width_col;
int num_kernels = channels * height_col * width_col;
// Launch
// Launch
im2col_kernel <<<
CAFFE_GET_BLOCKS(num_kernels), CAFFE_
CUDA_NUM_THREADS>>> (
im2col_kernel <<<
GET_BLOCKS(num_kernels),
CUDA_NUM_THREADS>>> (
num_kernels, data_im, height, width, ksize
,
num_kernels, data_im, height, width, ksize_h, ksize_w
,
pad, stride
,
pad_h, pad_w, stride_h, stride_w
,
height_col, width_col, data_col
height_col, width_col, data_col
);
);
}
}
...
@@ -79,7 +92,7 @@ void im2col(const float* data_im, const int channels,
...
@@ -79,7 +92,7 @@ void im2col(const float* data_im, const int channels,
CudaNdarray* corrMM(const CudaNdarray *input,
CudaNdarray* corrMM(const CudaNdarray *input,
CudaNdarray *weight,
CudaNdarray *weight,
CudaNdarray *output,
CudaNdarray *output,
int pad
ding
= 0)
int pad
H, int padW
= 0)
{
{
cublasStatus_t status;
cublasStatus_t status;
...
@@ -103,21 +116,6 @@ CudaNdarray* corrMM(const CudaNdarray *input,
...
@@ -103,21 +116,6 @@ CudaNdarray* corrMM(const CudaNdarray *input,
// filters: (number of filters, nInputPlane, rows, columns)
// filters: (number of filters, nInputPlane, rows, columns)
int nOutputPlane = CudaNdarray_HOST_DIMS(weight)[0];
int nOutputPlane = CudaNdarray_HOST_DIMS(weight)[0];
long batchSize = CudaNdarray_HOST_DIMS(input)[0];
long batchSize = CudaNdarray_HOST_DIMS(input)[0];
if (CudaNdarray_HOST_DIMS(input)[2] != CudaNdarray_HOST_DIMS(input)[3]){
PyErr_Format(PyExc_ValueError,
"GpuCorrMM support only square images. Got %dx%d images\n",
CudaNdarray_HOST_DIMS(input)[2],
CudaNdarray_HOST_DIMS(input)[3]
);
return NULL;
}
if (kW != kH){
PyErr_Format(PyExc_ValueError,
"GpuCorrMM support only square kernel. Got %dx%d kernel\n",
kW, kH
);
return NULL;
}
if (CudaNdarray_HOST_DIMS(input)[1] != CudaNdarray_HOST_DIMS(weight)[1]){
if (CudaNdarray_HOST_DIMS(input)[1] != CudaNdarray_HOST_DIMS(weight)[1]){
PyErr_SetString(PyExc_ValueError,
PyErr_SetString(PyExc_ValueError,
"GpuCorrMM images and kernel must have the same stack size\n"
"GpuCorrMM images and kernel must have the same stack size\n"
...
@@ -126,18 +124,20 @@ CudaNdarray* corrMM(const CudaNdarray *input,
...
@@ -126,18 +124,20 @@ CudaNdarray* corrMM(const CudaNdarray *input,
}
}
long inputHeight = CudaNdarray_HOST_DIMS(input)[2];
long inputHeight = CudaNdarray_HOST_DIMS(input)[2];
long inputWidth = CudaNdarray_HOST_DIMS(input)[3];
long inputWidth = CudaNdarray_HOST_DIMS(input)[3];
long outputWidth = (inputWidth + 2*pad
ding
- kW) / dW + 1;
long outputWidth = (inputWidth + 2*pad
W
- kW) / dW + 1;
long outputHeight = (inputHeight + 2*pad
ding
- kH) / dH + 1;
long outputHeight = (inputHeight + 2*pad
H
- kH) / dH + 1;
// check output, size (batchSize, nOutputPlane,
// check output, size (batchSize, nOutputPlane,
// outputHeight, outputWidth);
// outputHeight, outputWidth);
if (batchSize != CudaNdarray_HOST_DIMS(output)[0] ||
if (batchSize != CudaNdarray_HOST_DIMS(output)[0] ||
nOutputPlane != CudaNdarray_HOST_DIMS(output)[1] ||
nOutputPlane != CudaNdarray_HOST_DIMS(output)[1] ||
outputHeight != CudaNdarray_HOST_DIMS(output)[2] ||
outputHeight != CudaNdarray_HOST_DIMS(output)[2] ||
outputWidth != CudaNdarray_HOST_DIMS(output)[3]){
outputWidth != CudaNdarray_HOST_DIMS(output)[3]){
PyErr_SetString(PyExc_ValueError,
PyErr_SetString(PyExc_ValueError,
"GpuCorrMM outputs parameter don't have the good shape\n"
"GpuCorrMM outputs parameter don't have the good shape");
);
printf("GpuCorrMM outputs parameter don't have the good shape %d %d %d %d, %d %d %d %d\n",
batchSize, nOutputPlane, outputHeight, outputWidth, CudaNdarray_HOST_DIMS(output)[0],
CudaNdarray_HOST_DIMS(output)[1], CudaNdarray_HOST_DIMS(output)[2],
CudaNdarray_HOST_DIMS(output)[3]);
return NULL;
return NULL;
}
}
// Create temporary columns
// Create temporary columns
...
@@ -158,7 +158,7 @@ CudaNdarray* corrMM(const CudaNdarray *input,
...
@@ -158,7 +158,7 @@ CudaNdarray* corrMM(const CudaNdarray *input,
// 1. Extract columns:
// 1. Extract columns:
im2col(
im2col(
input->devdata + elt*ip_stride,
input->devdata + elt*ip_stride,
nInputPlane, input
Width, inputHeight, kW, padding
, dW,
nInputPlane, input
Height, inputWidth, kH, kW, padH, padW, dH
, dW,
columns->devdata
columns->devdata
);
);
...
...
theano/sandbox/cuda/tests/test_conv_cuda_ndarray.py
浏览文件 @
85b8a90f
...
@@ -7,6 +7,7 @@ import unittest
...
@@ -7,6 +7,7 @@ import unittest
import
numpy
import
numpy
import
scipy
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
imported_scipy_convolve2d
=
False
imported_scipy_convolve2d
=
False
...
@@ -114,7 +115,8 @@ def py_conv_scipy(img, kern, mode, subsample):
...
@@ -114,7 +115,8 @@ def py_conv_scipy(img, kern, mode, subsample):
for
b
in
xrange
(
out
.
shape
[
0
]):
for
b
in
xrange
(
out
.
shape
[
0
]):
for
k
in
xrange
(
out
.
shape
[
1
]):
for
k
in
xrange
(
out
.
shape
[
1
]):
for
s
in
xrange
(
img
.
shape
[
1
]):
for
s
in
xrange
(
img
.
shape
[
1
]):
out
[
b
,
k
,
:,
:]
+=
convolve2d
(
img
[
b
,
s
,
:,
:],
#convolve2d or correlate
out
[
b
,
k
,
:,
:]
+=
scipy
.
signal
.
convolve2d
(
img
[
b
,
s
,
:,
:],
kern
[
k
,
s
,
:,
:],
kern
[
k
,
s
,
:,
:],
mode
)
mode
)
return
out
[:,
:,
::
subsample
[
0
],
::
subsample
[
1
]]
return
out
[:,
:,
::
subsample
[
0
],
::
subsample
[
1
]]
...
@@ -830,47 +832,47 @@ def test_gemm():
...
@@ -830,47 +832,47 @@ def test_gemm():
input: (batch size, channels, rows, columns)
input: (batch size, channels, rows, columns)
filters: (number of filters, channels, rows, columns)
filters: (number of filters, channels, rows, columns)
"""
"""
for
mode
in
[
'
valid'
,
'full
'
]:
for
mode
in
[
'
full'
,
'valid
'
]:
print
'Testing mode: '
+
mode
print
'Testing mode: '
+
mode
for
bs
in
range
(
1
,
5
):
for
bs
in
range
(
1
,
5
):
for
ch
in
range
(
1
,
4
):
for
ch
in
range
(
1
,
4
):
for
nf
in
range
(
1
,
4
):
for
nf
in
range
(
1
,
4
):
for
rImg
in
range
(
5
,
9
):
for
rImg
1
in
range
(
5
,
9
):
for
r
Flt
in
range
(
2
,
4
):
for
r
Img2
in
range
(
5
,
9
):
ishape
=
(
bs
,
ch
,
rImg
,
rImg
)
for
rFlt1
in
range
(
2
,
4
):
kshape
=
(
nf
,
ch
,
rFlt
,
rFlt
)
for
rFlt2
in
range
(
2
,
4
):
print
"ishape: "
,
ishape
ishape
=
(
bs
,
ch
,
rImg1
,
rImg2
)
print
"kshape: "
,
kshape
kshape
=
(
nf
,
ch
,
rFlt1
,
rFlt2
)
subsample
=
(
1
,
1
)
print
"ishape: "
,
ishape
print
"kshape: "
,
kshape
npy_img
=
theano
.
_asarray
(
numpy
.
random
.
rand
(
*
ishape
),
dtype
=
'float32'
)
subsample
=
(
1
,
1
)
npy_kern
=
theano
.
_asarray
(
numpy
.
random
.
rand
(
*
kshape
),
dtype
=
'float32'
)
npy_img
=
theano
.
_asarray
(
numpy
.
random
.
rand
(
*
ishape
),
dtype
=
'float32'
)
i
=
cuda_tensor4
(
)
npy_kern
=
theano
.
_asarray
(
numpy
.
random
.
rand
(
*
kshape
),
dtype
=
'float32'
)
k
=
cuda_tensor4
()
i
=
cuda_tensor4
()
t2
=
None
k
=
cuda_tensor4
()
t0
=
time
.
time
()
t2
=
None
cpuval
=
py_conv
(
npy_img
,
npy_kern
,
mode
,
subsample
)
t0
=
time
.
time
()
t1
=
time
.
time
(
)
cpuval
=
py_conv
(
npy_img
,
npy_kern
,
mode
,
subsample
)
op
=
theano
.
sandbox
.
cuda
.
blas
.
GpuCorrMM
(
border_mode
=
mode
)(
i
,
k
)
t1
=
time
.
time
(
)
f
=
theano
.
function
([
i
,
k
],
op
,
mode
=
theano_mode
)
op
=
theano
.
sandbox
.
cuda
.
blas
.
GpuCorrMM
(
border_mode
=
mode
)(
i
,
k
)
for
k
in
range
(
npy_kern
.
shape
[
0
]):
f
=
theano
.
function
([
i
,
k
],
op
,
mode
=
theano_mode
)
for
s
in
range
(
npy_kern
.
shape
[
1
]):
npy_kern
[
k
,
s
,:,:]
=
numpy
.
rot90
(
npy_kern
[
k
,
s
,:,:],
2
)
npy_kern
=
npy_kern
[:,:,::
-
1
,::
-
1
]
gpuval
=
f
(
npy_img
,
npy_kern
)
gpuval
=
f
(
npy_img
,
npy_kern
)
t2
=
time
.
time
()
t2
=
time
.
time
()
gpuval
=
numpy
.
asarray
(
gpuval
)
gpuval
=
numpy
.
asarray
(
gpuval
)
rval
=
numpy
.
allclose
(
cpuval
,
gpuval
,
rtol
=
1e-4
)
rval
=
numpy
.
allclose
(
cpuval
,
gpuval
,
rtol
=
1e-4
)
assert
(
rval
==
True
)
assert
(
rval
==
True
)
print
'Test Passed'
print
'Test Passed'
def
benchmark
():
def
benchmark
():
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论