Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 3D Convolution operator implemented by GEMM. #4709

Merged
merged 19 commits into from
Nov 6, 2017

Conversation

chengduoZH
Copy link
Contributor

@chengduoZH chengduoZH commented Oct 11, 2017

fix #4710

@chengduoZH
Copy link
Contributor Author

chengduoZH commented Oct 11, 2017

conv

  • conv2d
    • gemm
      • Conv2DOp, Conv2DOpGrad, Conv2DOpMaker, GemmConv2DKernel
    • cudnn
      • Conv2DOp, Conv2DOpGrad, CudnnConv2DOpMaker, CudnnConvOp2DKernel
  • conv3d
    • gemm
      • Conv3DOp, Conv3DOpGrad, Conv3DOpMaker, GemmConv3DKernel
    • cudnn
      • Conv3DOp, Conv3DOpGrad, CudnnConv3DOpMaker, CudnnConvOp3DKernel

In convolution operations, Conv2DOpGrad and Conv3DOpGrad are completely the same. Conv2DOp and Conv3DOp, Conv2DOpMaker and CudnnConv2DOpMaker, Conv3DOpMaker and CudnnConv3DOpMaker are respectively similar.
I think it is necessary to write a base class or write in one class, respectively.
This problem is also encountered in pooling operations and deconv.

@chengduoZH chengduoZH force-pushed the Add_conv3d_gemm_op branch 5 times, most recently from 18f8136 to c51adfa Compare October 11, 2017 10:48
Copy link
Contributor

@typhoonzero typhoonzero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In convolution operations, Conv2DOpGrad and Conv3DOpGrad are completely the same. Conv2DOp and Conv3DOp, Conv2DOpMaker and CudnnConv2DOpMaker, Conv3DOpMaker and CudnnConv3DOpMaker are respectively similar.
I think it is necessary to write a base class or write in one class, respectively.
This problem is also encountered in pooling operations and deconv.

So are you going to add a base class in this PR or next one?

Need to pull/rebase from develop branch.

@@ -0,0 +1,117 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里缩进下,保持和第一行一致。和其他的文件保持一致。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Conv3DOpMaker::Conv3DOpMaker(framework::OpProto* proto,
framework::OpAttrChecker* op_checker)
: OpProtoAndCheckerMaker(proto, op_checker) {
AddInput(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这部分的确和2d很类似,可以用一个base class做一些代码复用。

@@ -0,0 +1,22 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License缩进同上

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


output_group_channels = output_channels / self.groups
input_group_channels = input_channels / self.groups
for batchid in xrange(batch_size):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c++和python代码都实现一次计算感觉比较冗余,python的unitest相当于认定python的conv实现一定是对的,然后来检测c++实现,但我们不一定都能保证python的实现都是对的(比如unitest部分某次commit更新错了,c++也有同样的错误,这样unitest虽然过了但不能保证计算是对的)。

我理解unitest中,需要有使用其他权威计算程序,比如matlab计算一组结果作为验证数据(包含边界条件的数据),同时验证python实现和c++实现,或者只验证c++实现

Copy link
Contributor Author

@chengduoZH chengduoZH Oct 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python的unitest相当于认定python的conv实现一定是对的,然后来检测c++实现,但我们不一定都能保证python的实现都是对的

有道理。我觉得当前应该把python代码的写更直观易懂一些,如果某些包(如 numpy)中已经存在这些操作,最好直接调用。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convolution在sci里的实现貌似只有1d的。放一个matlab算出来的结果作为test case会保险点。个人感觉,比如实现了一个add function,unitest必然是test下1+1=2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c++和python代码都实现一次计算感觉比较冗余,python的unitest相当于认定python的conv实现一定是对的,然后来检测c++实现,但我们不一定都能保证python的实现都是对的(比如unitest部分某次commit更新错了,c++也有同样的错误,这样unitest虽然过了但不能保证计算是对的)。

恰巧看到这个comment。我也遇到类似的问题。CRF 的计算逻辑比较特殊化,单测我先用比较粗暴的策略写了一下。写的时候体会到,效果就是:python 写一遍,C++ 写一遍。到底哪个是对的呢?很可能都是错的。因为会直接照着python的逻辑抄到c++,或者反过来。

后面会再仔细想办法换策略来单测。python 写一遍,c++ 写一遍实在不是个好办法。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

卷积跟CRF可能还不完全一样,卷积这个例子,Python中用的是direct方式实现的,C++中一般用的是gemm或其他算法实现的;所以,相当于用一种算法实现检查另一种算法实现,单元测试这么写并没有什么问题。

Copy link
Contributor

@lcy-seso lcy-seso Oct 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,卷积这样比较常规的运算情况会好一些。CRF 有些特殊。

后面会再仔细想办法换策略来单测。python 写一遍,c++ 写一遍实在不是个好办法。

这个是说我自己这样测CRF的前向,不是个明智的做法。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可不可以用老paddle的输出做一下对比?不过这样的前提是老paddle的输出得是正确的。

@chengduoZH chengduoZH force-pushed the Add_conv3d_gemm_op branch 3 times, most recently from 588b1b1 to 8c7edb5 Compare October 12, 2017 09:53
@chengduoZH chengduoZH force-pushed the Add_conv3d_gemm_op branch 5 times, most recently from c85db7e to 4a556b3 Compare October 12, 2017 15:50

class TestCase1(TestConv3dOp):
def init_test_case(self):
# self.groups = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these commented lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

no_grad_set=set(['Input']))

def init_test_case(self):
# self.groups = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these commented lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

for k in range(sub_out_c):
out[:, g * sub_out_c + k, d, i, j] = \
np.sum(input_pad_masked * f_sub[k, :, :, :, :],
axis=(1, 2, 3,4))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a space between 3, and 4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@chengduoZH chengduoZH force-pushed the Add_conv3d_gemm_op branch 2 times, most recently from 0155eb1 to 4f99b39 Compare October 18, 2017 12:20
@chengduoZH
Copy link
Contributor Author

I think we can write conv2d and conv3d together.

typhoonzero
typhoonzero previously approved these changes Oct 27, 2017
Copy link
Contributor

@typhoonzero typhoonzero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM++

I approve this for now so it won't block following work, we can do the refine later.

@typhoonzero
Copy link
Contributor

Need to pull/rebase from develop to resolve the conflict.

@chengduoZH chengduoZH force-pushed the Add_conv3d_gemm_op branch 2 times, most recently from e0f8a14 to 3b568c5 Compare October 30, 2017 09:47
typhoonzero
typhoonzero previously approved these changes Oct 30, 2017
Copy link
Contributor

@typhoonzero typhoonzero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM++

@qingqing01 qingqing01 changed the title Add conv3d_gemm_op Add 3D Convolution operator implemented by GEMM. Nov 6, 2017
@chengduoZH chengduoZH merged commit f78731c into PaddlePaddle:develop Nov 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add conv3d_gemm_op
5 participants