|
|
@ -25,16 +25,29 @@ class CosSimOp : public framework::OperatorWithKernel {
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
protected:
|
|
|
|
void InferShape(const framework::InferShapeContext &ctx) const override {
|
|
|
|
void InferShape(const framework::InferShapeContext &ctx) const override {
|
|
|
|
|
|
|
|
// notnull check
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"), "Input(X) must not be null.");
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"), "Input(X) must not be null.");
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Y"), "Input(Y) must not be null.");
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Y"), "Input(Y) must not be null.");
|
|
|
|
PADDLE_ENFORCE_EQ(ctx.Input<Tensor>("X")->dims(),
|
|
|
|
|
|
|
|
ctx.Input<Tensor>("Y")->dims(),
|
|
|
|
// shape check
|
|
|
|
"Dimensions of Input(X) and Input(Y) must be the same.");
|
|
|
|
auto x_dims = ctx.Input<Tensor>("X")->dims();
|
|
|
|
|
|
|
|
auto y_dims = ctx.Input<Tensor>("Y")->dims();
|
|
|
|
auto dims = ctx.Input<Tensor>("X")->dims();
|
|
|
|
PADDLE_ENFORCE_EQ(framework::arity(x_dims), framework::arity(y_dims),
|
|
|
|
ctx.Output<Tensor>("Out")->Resize({dims[0], 1});
|
|
|
|
"Ranks of Input(X) and Input(Y) must be equal.");
|
|
|
|
ctx.Output<Tensor>("XNorm")->Resize({dims[0], 1});
|
|
|
|
PADDLE_ENFORCE_GE(framework::arity(x_dims), 2,
|
|
|
|
ctx.Output<Tensor>("YNorm")->Resize({dims[0], 1});
|
|
|
|
"Rank of Input(X) must not be less than 2.");
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
|
|
|
framework::slice_ddim(x_dims, 1, framework::arity(x_dims)),
|
|
|
|
|
|
|
|
framework::slice_ddim(y_dims, 1, framework::arity(y_dims)),
|
|
|
|
|
|
|
|
"All dimensions except 1st of Input(X) and Input(Y) must be equal.");
|
|
|
|
|
|
|
|
PADDLE_ENFORCE(x_dims[0] == y_dims[0] || y_dims[0] == 1,
|
|
|
|
|
|
|
|
"1st dimension of Input(Y) must be equal to Input(X) or "
|
|
|
|
|
|
|
|
"just 1 (which will be broadcasted to match Input(X)).");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// resize tensor
|
|
|
|
|
|
|
|
ctx.Output<Tensor>("Out")->Resize({x_dims[0], 1});
|
|
|
|
|
|
|
|
ctx.Output<Tensor>("XNorm")->Resize({x_dims[0], 1});
|
|
|
|
|
|
|
|
ctx.Output<Tensor>("YNorm")->Resize({y_dims[0], 1});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -42,8 +55,8 @@ class CosSimOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
CosSimOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
|
|
|
CosSimOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
|
|
|
: OpProtoAndCheckerMaker(proto, op_checker) {
|
|
|
|
: OpProtoAndCheckerMaker(proto, op_checker) {
|
|
|
|
AddInput("X", "The first input of cos_sim op.");
|
|
|
|
AddInput("X", "The 1st input of cos_sim op.");
|
|
|
|
AddInput("Y", "The second input of cos_sim op.");
|
|
|
|
AddInput("Y", "The 2nd input of cos_sim op.");
|
|
|
|
AddOutput("Out", "The output of cos_sim op.");
|
|
|
|
AddOutput("Out", "The output of cos_sim op.");
|
|
|
|
AddOutput("XNorm", "Row norm of the first input.").AsIntermediate();
|
|
|
|
AddOutput("XNorm", "Row norm of the first input.").AsIntermediate();
|
|
|
|
AddOutput("YNorm", "Row norm of the second input.").AsIntermediate();
|
|
|
|
AddOutput("YNorm", "Row norm of the second input.").AsIntermediate();
|
|
|
@ -51,7 +64,12 @@ class CosSimOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
AddComment(R"DOC(
|
|
|
|
AddComment(R"DOC(
|
|
|
|
Cosine Similarity Operator.
|
|
|
|
Cosine Similarity Operator.
|
|
|
|
|
|
|
|
|
|
|
|
The equation is: Out = X^T * Y / (sqrt(X^T * X) * sqrt(Y^T * Y))
|
|
|
|
The equation is: Out = X^T * Y / (sqrt(X^T * X) * sqrt(Y^T * Y)).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Input(X) and Input(Y) must have the same shape, except that the 1st dimension
|
|
|
|
|
|
|
|
of Input(Y) could be just 1 (different from Input(X)), which will be
|
|
|
|
|
|
|
|
broadcasted to match the shape of Input(X) before computing their cosine
|
|
|
|
|
|
|
|
similarity.
|
|
|
|
)DOC");
|
|
|
|
)DOC");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -62,32 +80,47 @@ class CosSimOpGrad : public framework::OperatorWithKernel {
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
protected:
|
|
|
|
void InferShape(const framework::InferShapeContext &ctx) const override {
|
|
|
|
void InferShape(const framework::InferShapeContext &ctx) const override {
|
|
|
|
|
|
|
|
// notnull check
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"), "Input(X) must not be null.");
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"), "Input(X) must not be null.");
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Y"), "Input(Y) must not be null.");
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Y"), "Input(Y) must not be null.");
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("XNorm"),
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("XNorm"),
|
|
|
|
"Input(XNorm) must not be null.");
|
|
|
|
"Input(XNorm) must not be null.");
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("YNorm"),
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("YNorm"),
|
|
|
|
"Input(YNorm) must not be null.");
|
|
|
|
"Input(YNorm) must not be null.");
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Out"),
|
|
|
|
|
|
|
|
"Input(Out) must not be null.");
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar(framework::GradVarName("Out")),
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar(framework::GradVarName("Out")),
|
|
|
|
"Input(Out@GRAD) must not be null.");
|
|
|
|
"Input(Out@GRAD) must not be null.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// shape check
|
|
|
|
auto x_dims = ctx.Input<Tensor>("X")->dims();
|
|
|
|
auto x_dims = ctx.Input<Tensor>("X")->dims();
|
|
|
|
auto y_dims = ctx.Input<Tensor>("Y")->dims();
|
|
|
|
auto y_dims = ctx.Input<Tensor>("Y")->dims();
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_GE(framework::arity(x_dims), framework::arity(y_dims),
|
|
|
|
|
|
|
|
"Ranks of Input(X) and Input(Y) must be equal.");
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_GE(framework::arity(x_dims), 2,
|
|
|
|
|
|
|
|
"Rank of Input(X) must not be less than 2.");
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
|
|
|
framework::slice_ddim(x_dims, 1, framework::arity(x_dims)),
|
|
|
|
|
|
|
|
framework::slice_ddim(y_dims, 1, framework::arity(y_dims)),
|
|
|
|
|
|
|
|
"All dimensions except 1st of Input(X) and Input(Y) must be equal.");
|
|
|
|
|
|
|
|
PADDLE_ENFORCE(x_dims[0] == y_dims[0] || y_dims[0] == 1,
|
|
|
|
|
|
|
|
"1st dimension of Input(Y) must be equal to Input(X) or "
|
|
|
|
|
|
|
|
"just 1 (which will be broadcasted to match Input(X)).");
|
|
|
|
auto xnorm_dims = ctx.Input<Tensor>("XNorm")->dims();
|
|
|
|
auto xnorm_dims = ctx.Input<Tensor>("XNorm")->dims();
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_EQ(xnorm_dims, framework::make_ddim({x_dims[0], 1}),
|
|
|
|
|
|
|
|
"Shape of Input(XNorm) must be [X.Dim(0), 1].");
|
|
|
|
auto ynorm_dims = ctx.Input<Tensor>("YNorm")->dims();
|
|
|
|
auto ynorm_dims = ctx.Input<Tensor>("YNorm")->dims();
|
|
|
|
auto out_dims = ctx.Input<Tensor>(framework::GradVarName("Out"))->dims();
|
|
|
|
PADDLE_ENFORCE_EQ(ynorm_dims, framework::make_ddim({y_dims[0], 1}),
|
|
|
|
PADDLE_ENFORCE_EQ(x_dims, y_dims,
|
|
|
|
"Shape of Input(YNorm) must be [Y.Dim(0), 1].");
|
|
|
|
"Dimensions of Input(X) and Input(Y) must be the same.");
|
|
|
|
auto out_dims = ctx.Input<Tensor>("Out")->dims();
|
|
|
|
PADDLE_ENFORCE_EQ(xnorm_dims[0], x_dims[0],
|
|
|
|
PADDLE_ENFORCE_EQ(out_dims, framework::make_ddim({x_dims[0], 1}),
|
|
|
|
"1st dimension of XNorm must equal that of Input(X).");
|
|
|
|
"Shape of Input(Out) must be [X.Dim(0), 1].");
|
|
|
|
PADDLE_ENFORCE_EQ(xnorm_dims[1], 1, "2st dimension of XNorm must be one.");
|
|
|
|
auto out_grad_dims =
|
|
|
|
PADDLE_ENFORCE_EQ(ynorm_dims[0], y_dims[0],
|
|
|
|
ctx.Input<Tensor>(framework::GradVarName("Out"))->dims();
|
|
|
|
"1st dimension of YNorm must equal that of Input(Y).");
|
|
|
|
PADDLE_ENFORCE_EQ(out_grad_dims, framework::make_ddim({x_dims[0], 1}),
|
|
|
|
PADDLE_ENFORCE_EQ(ynorm_dims[1], 1, "2st dimension of YNorm must be one.");
|
|
|
|
"Shape of Input(Out@Grad) must be [X.Dim(0), 1].");
|
|
|
|
PADDLE_ENFORCE_EQ(out_dims[0], x_dims[0],
|
|
|
|
|
|
|
|
"1st dimension of Out@GRAD must equal that of Input(X)");
|
|
|
|
// resize tensor
|
|
|
|
PADDLE_ENFORCE_EQ(out_dims[1], 1, "1st dimension of Out@GRAD must be one.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto *x_grad = ctx.Output<Tensor>(framework::GradVarName("X"));
|
|
|
|
auto *x_grad = ctx.Output<Tensor>(framework::GradVarName("X"));
|
|
|
|
auto *y_grad = ctx.Output<Tensor>(framework::GradVarName("Y"));
|
|
|
|
auto *y_grad = ctx.Output<Tensor>(framework::GradVarName("Y"));
|
|
|
|
if (x_grad) x_grad->Resize(x_dims);
|
|
|
|
if (x_grad) x_grad->Resize(x_dims);
|
|
|
|