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

Proper fix for backward passes bwd/wrw for CK group conv 3d #2619

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/solver/conv_hip_implicit_gemm_3d_grouped_bwd_xdlops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ struct CKArgs
Do = ProblemInterpreter::GetOutputDepthDo(problem);
Z = ProblemInterpreter::GetFilterDepthZ(problem);

// On a backward pass, out is in and in is out and this is silly
output = {G, N, C, Di, Hi, Wi};
input = {G, N, K, Do, Ho, Wo};
input = {G, N, C, Di, Hi, Wi};
output = {G, N, K, Do, Ho, Wo};
weight = {G, K, C, Z, Y, X};

// miopen strides to CK strides
auto miopen_in_strides = problem.GetIn().GetStrides();
auto miopen_out_strides = problem.GetOut().GetStrides();
// On a backward pass, problem.GetIn() means y(or out),
// and problem.GetOut means x(or in)
auto miopen_in_strides = problem.GetOut().GetStrides();
auto miopen_out_strides = problem.GetIn().GetStrides();
auto miopen_wei_strides = problem.GetWeights().GetStrides();
miopen_in_strides.insert(miopen_in_strides.begin(), C);
miopen_out_strides.insert(miopen_out_strides.begin(), K);
Expand Down Expand Up @@ -123,6 +124,29 @@ struct CKArgs
template <typename ConvPtr>
auto MakeArgPtr(const ConvPtr& conv_ptr, ConstData_t in, ConstData_t w, Data_t out) const
{

#if 0 // Leaving for debugging needs
std::cout << "y ptr = " << in << std::endl;
std::cout << "w ptr = " << w << std::endl;
std::cout << "x ptr = " << out << std::endl;

auto print_vec = [](const char* name, const auto& vec) {
std::cout << name << " = [ ";
for(const auto& v : vec)
{
std::cout << v << ", ";
}
std::cout << "]\n";
};
#define PRINT_VEC(x) print_vec(#x, x);

PRINT_VEC(output);
PRINT_VEC(out_strides);
PRINT_VEC(input);
PRINT_VEC(in_strides);
PRINT_VEC(weight);
PRINT_VEC(wei_strides);
#endif
Comment on lines +128 to +149
Copy link
Contributor

Choose a reason for hiding this comment

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

So far we have never had such code in MIOpen, if we intend on keeping this, we should move the macro to the proper header file. and add it as a higher log level.

return conv_ptr->MakeArgumentPointer(in,
w,
{},
Expand Down
33 changes: 28 additions & 5 deletions src/solver/conv_hip_implicit_gemm_3d_grouped_wrw_xdlops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ struct CKArgs
Do = ProblemInterpreter::GetOutputDepthDo(problem);
Z = ProblemInterpreter::GetFilterDepthZ(problem);

// On a backward pass, out is in and in is out and this is silly
output = {G, N, C, Di, Hi, Wi};
input = {G, N, K, Do, Ho, Wo};
input = {G, N, C, Di, Hi, Wi};
output = {G, N, K, Do, Ho, Wo};
weight = {G, K, C, Z, Y, X};

// miopen strides to CK strides
auto miopen_in_strides = problem.GetIn().GetStrides();
auto miopen_out_strides = problem.GetOut().GetStrides();
// On a backward pass, problem.GetIn() means y(or out),
// and problem.GetOut means x(or in)
Comment on lines +92 to +93
Copy link
Contributor

Choose a reason for hiding this comment

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

[Notice] For backward pass, x (or dx) tensor contains output data (non-const buffer), y (dy) tensor contains input data (the buffer allowed to be const).

auto miopen_in_strides = problem.GetOut().GetStrides();
auto miopen_out_strides = problem.GetIn().GetStrides();
auto miopen_wei_strides = problem.GetWeights().GetStrides();
miopen_in_strides.insert(miopen_in_strides.begin(), C);
miopen_out_strides.insert(miopen_out_strides.begin(), K);
Expand Down Expand Up @@ -120,6 +121,28 @@ struct CKArgs
template <typename ConvPtr>
auto MakeArgPtr(const ConvPtr& conv_ptr, ConstData_t x, Data_t dw, ConstData_t dy) const
{
#if 0 // Leaving for debugging needs
std::cout << "y ptr = " << dy << std::endl;
std::cout << "w ptr = " << dw << std::endl;
std::cout << "x ptr = " << x << std::endl;

auto print_vec = [](const char* name, const auto& vec) {
std::cout << name << " = [ ";
for(const auto& v : vec)
{
std::cout << v << ", ";
}
std::cout << "]\n";
};
#define PRINT_VEC(x) print_vec(#x, x);

PRINT_VEC(output);
PRINT_VEC(out_strides);
PRINT_VEC(input);
PRINT_VEC(in_strides);
PRINT_VEC(weight);
PRINT_VEC(wei_strides);
#endif
Comment on lines +124 to +145
Copy link
Contributor

Choose a reason for hiding this comment

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

[same as above]

return conv_ptr->MakeArgumentPointer(x,
dw,
dy,
Expand Down
3 changes: 2 additions & 1 deletion test/gtest/group_conv3d_bwd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ void SolverBwd(const miopen::TensorDescriptor& inputDesc,
const auto tensors =
miopen::ConvBwdTensors{outputDesc, output, wDesc, weight, inputDesc, input};

// order for bwd data pass is dy, w, dx
const auto problem = miopen::conv::ProblemDescription{
inputDesc, wDesc, outputDesc, convDesc, miopen::conv::Direction::BackwardData};
outputDesc, wDesc, inputDesc, convDesc, miopen::conv::Direction::BackwardData};
Copy link
Contributor

Choose a reason for hiding this comment

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

[Notice] The test (incorrectly) uses notions of "input" and "output", which is not good for convolutions. This is legacy that we should get rid of in the future, I guess.

Copy link
Contributor

Choose a reason for hiding this comment

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

Lets make that the "near future"

auto ctx = miopen::ExecutionContext{};

ctx.SetStream(&handle);
Expand Down
8 changes: 4 additions & 4 deletions test/gtest/group_conv3d_bwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ std::vector<Conv3DTestCase> ConvTestConfigs()
{1, 1, 1, 1, 4, 4, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{1, 1, 1, 8, 8, 8, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, 1, miopenConvolution},
{1, 1, 1, 8, 8, 8, 1, 2, 2, 2, 0, 0, 0, 2, 2, 2, 1, 1, 1, miopenConvolution},
{1, 64, 32, 28, 28, 28, 32, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{32, 128, 32, 28, 28, 28, 32, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{1, 64, 32, 28, 28, 28, 16, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{16, 128, 16, 28, 28, 28, 32, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{16, 128, 16, 28, 28, 28, 16, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{8, 128, 8, 28, 28, 28, 8, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{4, 128, 4, 28, 28, 28, 4, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{4, 128, 8, 28, 28, 28, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{4, 128, 4, 28, 28, 28, 8, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{2, 128, 2, 28, 28, 28, 2, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution}};
}

Expand Down
3 changes: 2 additions & 1 deletion test/gtest/group_conv3d_wrw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ void SolverWrw(const miopen::TensorDescriptor& inputDesc,
const auto tensors =
miopen::ConvWrwTensors{outputDesc, output, inputDesc, input, wDesc, weight};

// order for bwd weights pass is dy, dw, x
const auto problem = miopen::conv::ProblemDescription{
inputDesc, wDesc, outputDesc, convDesc, miopen::conv::Direction::BackwardWeights};
outputDesc, wDesc, inputDesc, convDesc, miopen::conv::Direction::BackwardWeights};
auto ctx = miopen::ExecutionContext{};

ctx.SetStream(&handle);
Expand Down
14 changes: 7 additions & 7 deletions test/gtest/group_conv3d_wrw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ std::vector<Conv3DTestCase> ConvTestConfigs()
{1, 1, 1, 1, 4, 4, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{1, 1, 1, 8, 8, 8, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, 1, miopenConvolution},
{1, 1, 1, 8, 8, 8, 1, 2, 2, 2, 0, 0, 0, 2, 2, 2, 1, 1, 1, miopenConvolution},
{1, 64, 32, 28, 28, 28, 32, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{2, 128, 32, 28, 28, 28, 32, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{32, 128, 32, 28, 28, 28, 32, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{8, 64, 32, 28, 28, 28, 32, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{16, 64, 32, 28, 28, 28, 32, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{1, 64, 5, 28, 28, 28, 9, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{2, 128, 4, 28, 28, 28, 10, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{16, 128, 32, 28, 28, 28, 32, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{8, 64, 32, 28, 28, 28, 16, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{16, 64, 16, 28, 28, 28, 32, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{3, 48, 48, 28, 28, 28, 48, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{3, 48, 39, 28, 28, 28, 39, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{5, 120, 60, 28, 28, 28, 60, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution}};
{3, 48, 39, 28, 28, 28, 12, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution},
{5, 120, 10, 28, 28, 28, 30, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, miopenConvolution}};
}

inline int SetTensorLayout(miopen::TensorDescriptor& desc)
Expand Down