Skip to content

Commit

Permalink
add im2col for transposed conv path (pytorch#445)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: pytorch#445

add im2col for transposed convolution and related unit test and benchmarking.

Reviewed By: dskhudia

Differential Revision: D24265053

fbshipit-source-id: ddf1c16c4292fc36ff50ad7ab37e713bf7f1fd92
  • Loading branch information
YazhiGao authored and facebook-github-bot committed Oct 20, 2020
1 parent 4096024 commit 23cb1db
Show file tree
Hide file tree
Showing 6 changed files with 721 additions and 307 deletions.
57 changes: 52 additions & 5 deletions bench/ConvUnifiedBenchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,49 @@ using namespace fbgemm;
// clang-format off
// 1D conv shapes
vector<conv_param_t<1>> shapes_1d = {
// MB, IC, OC, IW, G, KW, stride_w, pad_w_left, pad_w_right
// MB, IC, OC, IW, G, KW, stride_w, pad_w_left, pad_w_right,
// (dilation, output_padding_w, tranpose)
// regular
conv_param_t<1>(1, 600, 100, {1}, 1, {3}, {1}, {2, 2}),
conv_param_t<1>(1, 600, 100, {2}, 1, {3}, {1}, {2, 2}),
conv_param_t<1>(1, 600, 100, {3}, 1, {3}, {1}, {2, 2}),
conv_param_t<1>(1, 200, 162, {1}, 1, {3}, {1}, {2, 2}),
conv_param_t<1>(1, 600, 100, {4}, 1, {3}, {1}, {2, 2})
conv_param_t<1>(1, 600, 100, {4}, 1, {3}, {1}, {2, 2}),

// deconv
conv_param_t<1>(1, 384, 192, {74}, 1, {16}, {8}, {4, 4}, {1}, {0}, true),
conv_param_t<1>(1, 192, 96, {74}, 1, {8}, {4}, {2, 2}, {1}, {0}, true),
conv_param_t<1>(1, 96, 48, {74}, 1, {4}, {2}, {1, 1}, {1}, {0}, true),
conv_param_t<1>(1, 96, 48, {74}, 1, {4}, {2}, {1, 1}, {1}, {1}, true),
};

// 2D conv shapes
vector<conv_param_t<2>> shapes_2d = {
// MB, IC, OC, IH, IW, G, KH, KW, stride_h, stride_w,
// pad_h_top, pad_w_left, pad_h_bottom, pad_w_right
// pad_h_top, pad_w_left, pad_h_bottom, pad_w_right,
// (dilation_h, dilation_w, output_padding_h, output_padding_w, tranpose)
// 2D convolutions
// regular
conv_param_t<>(1, 128, 128, {56, 56}, 1, {3, 3},
{1, 1}, {1, 1, 1, 1}),
conv_param_t<>(1, 128, 128, {56, 56}, 1, {3, 3},
{1, 1}, {1, 1, 1, 1}, {1, 1}, {0}, true),
conv_param_t<>(1, 128, 128, {56, 56}, 1, {3, 3},
{1, 1}, {1, 1, 1, 1}, {1, 1}, {1}, true),
// regular with dilation
conv_param_t<>(1, 128, 128, {56, 56}, 1, {3, 3},
{1, 1}, {1, 1, 1, 1}, {2, 2}),
conv_param_t<>(1, 128, 128, {56, 56}, 1, {3, 3},
{1, 1}, {1, 1, 1, 1}, {2, 2}, {0}, true),
conv_param_t<>(1, 128, 128, {56, 56}, 1, {3, 3},
{1, 1}, {1, 1, 1, 1}, {2, 2}, {1}, true),
// groupwise
conv_param_t<>(1, 128, 128, {56, 56}, 32, {3, 3},
{1, 1}, {1, 1, 1, 1}),
conv_param_t<>(1, 128, 128, {56, 56}, 32, {3, 3},
{1, 1}, {1, 1, 1, 1}, {1, 1}, {0}, true),
conv_param_t<>(1, 128, 128, {56, 56}, 32, {3, 3},
{1, 1}, {1, 1, 1, 1}, {1, 1}, {0}, true),
// DW
conv_param_t<>(1, 272, 272, {47, 125}, 272, {3, 3},
{1, 1}, {1, 1, 1, 1}),
Expand All @@ -64,14 +84,23 @@ vector<conv_param_t<2>> shapes_2d = {
vector<conv_param_t<3>> shapes_3d = {
// MB, IC, OC, {IT, IH, IW}, G, {KT, KH, KW}, {stride_t, stride_h,
// stride_w},
// {pad_prev, pad_h_top, pad_w_left, pad_next, pad_h_bottom, pad_w_right}
// {pad_prev, pad_h_top, pad_w_left, pad_next, pad_h_bottom, pad_w_right},
// ({dilation_t, dilation_h, dilation_w},
// {output_padding_t, output_padding_h, output_padding_w}, tranpose)
// Regular
conv_param_t<3>(1, 64, 64, {8, 14, 14}, 1, {3, 3, 3},
{1, 1, 1}, {1, 1, 1, 1, 1, 1}),

conv_param_t<3>(1, 64, 64, {8, 14, 14}, 1, {3, 3, 3},
{1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1}, {0, 0, 0}, true),
conv_param_t<3>(1, 64, 64, {8, 14, 14}, 1, {3, 3, 3},
{1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1}, {1, 1, 1}, true),
//With dilations
conv_param_t<3>(1, 64, 64, {8, 14, 14}, 1, {3, 3, 3},
{1, 1, 1}, {1, 1, 1, 1, 1, 1}, {2, 2, 2}),
conv_param_t<3>(1, 64, 64, {8, 14, 14}, 1, {3, 3, 3},
{1, 1, 1}, {1, 1, 1, 1, 1, 1}, {2, 2, 2}, {0, 0, 0}, true),
conv_param_t<3>(1, 64, 64, {8, 14, 14}, 1, {3, 3, 3},
{1, 1, 1}, {1, 1, 1, 1, 1, 1}, {2, 2, 2}, {1, 1, 1}, true),

// Groupwise
conv_param_t<3>(32, 192, 192, {2, 28, 28}, 96, {3, 3, 3},
Expand Down Expand Up @@ -103,6 +132,12 @@ vector<conv_param_t<3>> shapes_3d = {
{1, 2, 2}, {1, 1, 1, 1, 1, 1}),
conv_param_t<3>(32, 256, 256, {1, 7, 7}, 64, {3, 3, 3},
{1, 1, 1}, {1, 1, 1, 1, 1, 1}),
conv_param_t<3>(32, 128, 128, {2, 28, 28}, 32, {3, 3, 3},
{2, 2, 2}, {1, 1, 1, 1, 1, 1}, {1, 1, 1}, {0, 0, 0}, true),
conv_param_t<3>(32, 128, 128, {1, 14, 14}, 32, {3, 3, 3},
{1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1}, {0, 0, 0}, true),
conv_param_t<3>(32, 128, 128, {1, 14, 14}, 32, {3, 3, 3},
{1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1}, {1, 1, 1}, true),

// Depthwise
conv_param_t<3>(1, 64, 64, {8, 14, 14}, 64, {3, 3, 3},
Expand Down Expand Up @@ -166,6 +201,14 @@ void performance_test(const vector<conv_param_t<SPATIAL_DIM>>& shapes) {
header += "dilation_h, ";
}
header += "dilation_w, ";
if (SPATIAL_DIM == 3) {
header += "output_padding_t, ";
}
if (SPATIAL_DIM > 1) {
header += "output_padding_h, ";
}
header += "output_padding_w, ";
header += "transposed, ";

header += "Type, M, N, K, ";

Expand Down Expand Up @@ -373,6 +416,10 @@ void performance_test(const vector<conv_param_t<SPATIAL_DIM>>& shapes) {
for (int i = 0; i < SPATIAL_DIM; ++i) {
cout << conv_p.dilation[i] << ", ";
}
for (int i = 0; i < SPATIAL_DIM; ++i) {
cout << conv_p.output_pad[i] << ", ";
}
cout << conv_p.transposed;
cout << setw(13) << runType << ", " << setw(5) << fixed << setw(5)
<< setw(6) << MDim << ", " << setw(6) << NDim << ", " << setw(6)
<< KDim << ", ";
Expand Down
7 changes: 4 additions & 3 deletions src/Fbgemm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ void fbgemmPacked(

template <int SPATIAL_DIM>
bool fbgemmOptimizedGConv(const conv_param_t<SPATIAL_DIM>& conv_p) {

if (SPATIAL_DIM == 1) return false;
if (SPATIAL_DIM == 1)
return false;

int C_per_G = conv_p.IC / conv_p.G;
int K_per_G = conv_p.OC / conv_p.G;
Expand Down Expand Up @@ -247,7 +247,8 @@ bool fbgemmOptimizedGConv(const conv_param_t<SPATIAL_DIM>& conv_p) {
std::all_of(
conv_p.stride.begin() + SPATIAL_DIM - 2,
conv_p.stride.end(),
std::bind(areEqual, std::placeholders::_1, 2)));
std::bind(areEqual, std::placeholders::_1, 2))) &&
!conv_p.transposed;
}

template FBGEMM_API bool fbgemmOptimizedGConv(const conv_param_t<1>& conv_p);
Expand Down
13 changes: 8 additions & 5 deletions src/FbgemmConv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ bool takeDepthWiseFastPath(const conv_param_t<SPATIAL_DIM>& conv_p) {
conv_p.K.begin(),
conv_p.K.end(),
[](int i) { return i == 3 || i == 5; }) &&
std::all_of(conv_p.dilation.begin(), conv_p.dilation.end(), [](int i) {
return i == 1;
});
std::all_of(
conv_p.dilation.begin(),
conv_p.dilation.end(),
[](int i) { return i == 1; }) &&
!conv_p.transposed;

// Check pads result in same input and output spatial dim
for (int i = 0; i < SPATIAL_DIM; ++i) {
Expand All @@ -56,12 +58,13 @@ bool takePointWiseFastPath(const conv_param_t<SPATIAL_DIM>& conv_p) {
SPATIAL_DIM &&
std::accumulate(conv_p.dilation.begin(), conv_p.dilation.end(), 0) ==
SPATIAL_DIM &&
std::accumulate(conv_p.pad.begin(), conv_p.pad.end(), 0) == 0;
std::accumulate(conv_p.pad.begin(), conv_p.pad.end(), 0) == 0 &&
!conv_p.transposed;
}

template <int SPATIAL_DIM>
bool take1DFastPath(const conv_param_t<SPATIAL_DIM>& conv_p) {
return false;
return false && !conv_p.transposed;
}

template <int SPATIAL_DIM, typename ACC_T>
Expand Down
Loading

0 comments on commit 23cb1db

Please sign in to comment.