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

[Phi] move infershape for mv #39954

Merged
merged 2 commits into from
Mar 5, 2022
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
36 changes: 8 additions & 28 deletions paddle/fluid/operators/mv_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ limitations under the License. */
#include <utility>
#include <vector>

#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/binary.h"

namespace paddle {
namespace operators {
Expand All @@ -42,33 +45,6 @@ class MVOp : public framework::OperatorWithKernel {
using framework::OperatorWithKernel::OperatorWithKernel;

protected:
void InferShape(framework::InferShapeContext *context) const override {
OP_INOUT_CHECK(context->HasInput("X"), "Input", "X", "mv");
OP_INOUT_CHECK(context->HasInput("Vec"), "Input", "Vec", "mv");
OP_INOUT_CHECK(context->HasOutput("Out"), "Output", "Out", "mv");

auto dim_x = context->GetInputDim("X");
auto dim_vec = context->GetInputDim("Vec");
PADDLE_ENFORCE_EQ(
dim_x.size(), 2,
platform::errors::InvalidArgument(
"The rank of input X should be 2, but is %d", dim_x.size()));
PADDLE_ENFORCE_EQ(
dim_vec.size(), 1,
platform::errors::InvalidArgument(
"The rank of input Vec should be 1, but is %d", dim_vec.size()));
PADDLE_ENFORCE_EQ(dim_x[1], dim_vec[0],
platform::errors::InvalidArgument(
"X's second dimension is expected to be equal to "
"Vec's first dimension"
"but recieved X'shape = [%s], Vec's shape = [%s]",
dim_x, dim_vec));

framework::DDim dim_out = phi::make_ddim({dim_x[0]});

context->SetOutputDim("Out", dim_out);
context->ShareLoD("X", /*->*/ "Out");
}
};

template <typename T>
Expand Down Expand Up @@ -118,7 +94,11 @@ class MVOpGrad : public framework::OperatorWithKernel {
namespace ops = paddle::operators;
namespace plat = paddle::platform;

DELCARE_INFER_SHAPE_FUNCTOR(mv, MvInferShapeFunctor,
PT_INFER_META(phi::MvInferMeta));

REGISTER_OPERATOR(mv, ops::MVOp, ops::MVOpMaker,
ops::MVOpGradMaker<paddle::framework::OpDesc>,
ops::MVOpGradMaker<paddle::imperative::OpBase>);
ops::MVOpGradMaker<paddle::imperative::OpBase>,
MvInferShapeFunctor);
REGISTER_OPERATOR(mv_grad, ops::MVOpGrad);
30 changes: 30 additions & 0 deletions paddle/phi/infermeta/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,34 @@ void GatherTreeMeta(const MetaTensor& ids,
out->set_dims(ids_dims);
}

void MvInferMeta(const MetaTensor& x, const MetaTensor& vec, MetaTensor* out) {
auto dim_x = x.dims();
auto dim_vec = vec.dims();
PADDLE_ENFORCE_EQ(
dim_x.size(),
2,
phi::errors::InvalidArgument("The rank of input X should be 2, but is %d",
dim_x.size()));
PADDLE_ENFORCE_EQ(
dim_vec.size(),
1,
phi::errors::InvalidArgument(
"The rank of input Vec should be 1, but is %d", dim_vec.size()));
PADDLE_ENFORCE_EQ(dim_x[1],
dim_vec[0],
phi::errors::InvalidArgument(
"X's second dimension is expected to be equal to "
"Vec's first dimension"
"but recieved X'shape = [%s], Vec's shape = [%s]",
dim_x,
dim_vec));

auto dim_out = phi::make_ddim({dim_x[0]});

out->set_dims(dim_out);
out->set_dtype(x.dtype());
out->set_layout(x.layout());
out->share_lod(x);
}

} // namespace phi
3 changes: 3 additions & 0 deletions paddle/phi/infermeta/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,7 @@ void GatherNdInferMeta(const MetaTensor& x,
void GatherTreeMeta(const MetaTensor& ids,
const MetaTensor& parents,
MetaTensor* out);

void MvInferMeta(const MetaTensor& x, const MetaTensor& vec, MetaTensor* out);

} // namespace phi
5 changes: 0 additions & 5 deletions paddle/phi/ops/compat/mv_sig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

namespace phi {

KernelSignature MvOpArgumentMapping(const ArgumentMappingContext& ctx) {
return KernelSignature("mv", {"X", "Vec"}, {}, {"Out"});
}

KernelSignature MvGradOpArgumentMapping(const ArgumentMappingContext& ctx) {
return KernelSignature("mv_grad",
{"X", "Vec", GradVarName("Out")},
Expand All @@ -29,5 +25,4 @@ KernelSignature MvGradOpArgumentMapping(const ArgumentMappingContext& ctx) {

} // namespace phi

PD_REGISTER_ARG_MAPPING_FN(mv, phi::MvOpArgumentMapping);
PD_REGISTER_ARG_MAPPING_FN(mv_grad, phi::MvGradOpArgumentMapping);