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

Fix for swin_transformer(matmul+transpose+reshape) #35740

Merged
merged 1 commit into from
Sep 15, 2021
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
18 changes: 18 additions & 0 deletions paddle/fluid/operators/matmul_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,24 @@ class MatMulOp : public framework::OperatorWithKernel {
platform::errors::InvalidArgument("reshape_out supported rank is 3, "
"received %d",
reshape_out_size));

auto it = std::find(reshape_out.begin(), reshape_out.end(), -1);

// if "-1" is present then one of reshape dims must be infered
if (it != reshape_out.end()) {
int index = std::distance(reshape_out.begin(), it);

auto ddim_out_vec = framework::vectorize(ddim_out);

int ddim_out_product =
std::accumulate(ddim_out_vec.begin(), ddim_out_vec.end(), 1,
std::multiplies<int>());
int reshape_out_product = std::accumulate(
reshape_out.begin(), reshape_out.end(), -1, std::multiplies<int>());

reshape_out[index] = ddim_out_product / reshape_out_product;
}
Comment on lines +661 to +674
Copy link
Contributor

@tsocha tsocha Sep 16, 2021

Choose a reason for hiding this comment

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

You modify a reshape_out dim which is already pointed by iterator.
There is no need to calculate an index via std::distance, just use 'it'.

I wonder if it is worth to check shapes compatibility:
'''
ddim_out_product % reshape_out_product == 0
'''


framework::DDim shape_out =
ddim_out.transpose(transpose_out).reshape(reshape_out);
context->SetOutputDim("Out", shape_out);
Expand Down
2 changes: 2 additions & 0 deletions paddle/fluid/platform/mkldnn_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ inline MKLDNNMemoryFormat MKLDNNFormatForSize(size_t dims_size,
} else if (data_format == MKLDNNMemoryFormat::nhwc) {
return MKLDNNMemoryFormat::ndhwc;
}
} else if (dims_size == 6) {
return MKLDNNMemoryFormat::abcdef;
}
return data_format;
}
Expand Down