|
|
|
@ -108,39 +108,51 @@ class SoftmaxWithCrossEntropyOp : public framework::OperatorWithKernel {
|
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
|
|
|
|
|
|
void InferShape(framework::InferShapeContext* ctx) const override {
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("Logits"),
|
|
|
|
|
"Input(Logits) should be not null.");
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("Label"), "Input(Label) should be not null.");
|
|
|
|
|
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasOutput("Softmax"),
|
|
|
|
|
"Output(Softmax) should be not null.");
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasOutput("Loss"), "Output(Loss) should be not null.");
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
ctx->HasInput("Logits"), true,
|
|
|
|
|
platform::errors::InvalidArgument("Input(Logits) should be not null."));
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
ctx->HasInput("Label"), true,
|
|
|
|
|
platform::errors::InvalidArgument("Input(Label) should be not null."));
|
|
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_EQ(ctx->HasOutput("Softmax"), true,
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"Output(Softmax) should be not null."));
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
ctx->HasOutput("Loss"), true,
|
|
|
|
|
platform::errors::InvalidArgument("Output(Loss) should be not null."));
|
|
|
|
|
|
|
|
|
|
auto axis = ctx->Attrs().Get<int>("axis");
|
|
|
|
|
auto logits_dims = ctx->GetInputDim("Logits");
|
|
|
|
|
auto labels_dims = ctx->GetInputDim("Label");
|
|
|
|
|
auto logits_rank = logits_dims.size();
|
|
|
|
|
PADDLE_ENFORCE(axis >= -logits_rank && axis < logits_rank,
|
|
|
|
|
"Attr(axis) value should be in range [-R, R-1], "
|
|
|
|
|
"R is the rank of Input(Logits).");
|
|
|
|
|
PADDLE_ENFORCE_GE(axis, -logits_rank,
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"Attr(axis) value should be in range [-R, R-1], "
|
|
|
|
|
"R is the rank of Input(Logits)."));
|
|
|
|
|
PADDLE_ENFORCE_LT(axis, logits_rank,
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"Attr(axis) value should be in range [-R, R-1], "
|
|
|
|
|
"R is the rank of Input(Logits)."));
|
|
|
|
|
|
|
|
|
|
axis = CanonicalAxis(axis, logits_rank);
|
|
|
|
|
for (int i = 0; i < logits_rank; i++) {
|
|
|
|
|
if (i != axis) {
|
|
|
|
|
if (ctx->IsRuntime() || (logits_dims[i] > 0 && labels_dims[i] > 0)) {
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
logits_dims[i], labels_dims[i],
|
|
|
|
|
"Input(Logits) and Input(Label) should in same shape in "
|
|
|
|
|
"dimensions except axis.");
|
|
|
|
|
PADDLE_ENFORCE_EQ(logits_dims[i], labels_dims[i],
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"Input(Logits) and Input(Label) should in "
|
|
|
|
|
"same shape in dimensions except axis."));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto numeric_stable_mode = ctx->Attrs().Get<bool>("numeric_stable_mode");
|
|
|
|
|
if (axis != logits_rank - 1) {
|
|
|
|
|
PADDLE_ENFORCE(
|
|
|
|
|
numeric_stable_mode,
|
|
|
|
|
"Attr(axis) can only be -1 when not in numeric_stable_mode.");
|
|
|
|
|
PADDLE_ENFORCE_EQ(numeric_stable_mode, true,
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"Attr(axis) can only be -1 "
|
|
|
|
|
"when not in numeric_stable_mode."));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool soft_label = ctx->Attrs().Get<bool>("soft_label");
|
|
|
|
@ -148,14 +160,18 @@ class SoftmaxWithCrossEntropyOp : public framework::OperatorWithKernel {
|
|
|
|
|
if (ctx->IsRuntime() ||
|
|
|
|
|
(logits_dims[axis] > 0 && labels_dims[axis] > 0)) {
|
|
|
|
|
PADDLE_ENFORCE_EQ(logits_dims[axis], labels_dims[axis],
|
|
|
|
|
"If Attr(soft_label) == true, the axis dimension of "
|
|
|
|
|
"Input(X) and Input(Label) should be equal.");
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"If Attr(soft_label) == true, "
|
|
|
|
|
"the axis dimension of "
|
|
|
|
|
"Input(X) and Input(Label) should be equal."));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (ctx->IsRuntime() || labels_dims[axis] > 0) {
|
|
|
|
|
PADDLE_ENFORCE_EQ(labels_dims[axis], 1UL,
|
|
|
|
|
"If Attr(soft_label) == false, the axis dimension of "
|
|
|
|
|
"Input(Label) should be 1.");
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
labels_dims[axis], 1UL,
|
|
|
|
|
platform::errors::InvalidArgument("If Attr(soft_label) == false, "
|
|
|
|
|
"the axis dimension of "
|
|
|
|
|
"Input(Label) should be 1."));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -182,21 +198,31 @@ class SoftmaxWithCrossEntropyOpGrad : public framework::OperatorWithKernel {
|
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
|
|
|
|
|
|
void InferShape(framework::InferShapeContext* ctx) const override {
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Loss")),
|
|
|
|
|
"Input(Loss@Grad) should not be null.");
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("Softmax"),
|
|
|
|
|
"Input(Softmax) should be not null.");
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("Label"), "Input(Label) should be not null.");
|
|
|
|
|
PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("Logits")),
|
|
|
|
|
"Output(Logits@Grad) should be not null.");
|
|
|
|
|
PADDLE_ENFORCE_EQ(ctx->HasInput(framework::GradVarName("Loss")), true,
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"Input(Loss@Grad) should not be null."));
|
|
|
|
|
PADDLE_ENFORCE_EQ(ctx->HasInput("Softmax"), true,
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"Input(Softmax) should be not null."));
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
ctx->HasInput("Label"), true,
|
|
|
|
|
platform::errors::InvalidArgument("Input(Label) should be not null."));
|
|
|
|
|
PADDLE_ENFORCE_EQ(ctx->HasOutput(framework::GradVarName("Logits")), true,
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"Output(Logits@Grad) should be not null."));
|
|
|
|
|
|
|
|
|
|
auto axis = ctx->Attrs().Get<int>("axis");
|
|
|
|
|
auto softmax_dims = ctx->GetInputDim("Softmax");
|
|
|
|
|
auto labels_dims = ctx->GetInputDim("Label");
|
|
|
|
|
auto softmax_rank = softmax_dims.size();
|
|
|
|
|
PADDLE_ENFORCE(axis >= -softmax_rank && axis < softmax_rank,
|
|
|
|
|
"Attr(axis) value should be in range [-R, R-1], "
|
|
|
|
|
"R is the rank of Input(Logits).");
|
|
|
|
|
PADDLE_ENFORCE_GE(axis, -softmax_rank,
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"Attr(axis) value should be in range [-R, R-1], "
|
|
|
|
|
"R is the rank of Input(Logits)."));
|
|
|
|
|
PADDLE_ENFORCE_LT(axis, softmax_rank,
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"Attr(axis) value should be in range [-R, R-1], "
|
|
|
|
|
"R is the rank of Input(Logits)."));
|
|
|
|
|
|
|
|
|
|
axis = CanonicalAxis(axis, softmax_rank);
|
|
|
|
|
for (int i = 0; i < softmax_rank; i++) {
|
|
|
|
@ -204,8 +230,9 @@ class SoftmaxWithCrossEntropyOpGrad : public framework::OperatorWithKernel {
|
|
|
|
|
if (ctx->IsRuntime() || (softmax_dims[i] > 0 && labels_dims[i] > 0)) {
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
softmax_dims[i], labels_dims[i],
|
|
|
|
|
"Input(Logits) and Input(Label) should in same shape in "
|
|
|
|
|
"dimensions except axis.");
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"Input(Logits) and Input(Label) should in same shape in "
|
|
|
|
|
"dimensions except axis."));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -215,14 +242,18 @@ class SoftmaxWithCrossEntropyOpGrad : public framework::OperatorWithKernel {
|
|
|
|
|
if (ctx->IsRuntime() ||
|
|
|
|
|
(softmax_dims[axis] > 0 && labels_dims[axis] > 0)) {
|
|
|
|
|
PADDLE_ENFORCE_EQ(softmax_dims[axis], labels_dims[axis],
|
|
|
|
|
"If Attr(soft_label) == true, the axis dimension of "
|
|
|
|
|
"Input(X) and Input(Label) should be equal.");
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"If Attr(soft_label) == true, "
|
|
|
|
|
"the axis dimension of "
|
|
|
|
|
"Input(X) and Input(Label) should be equal."));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (ctx->IsRuntime() || labels_dims[axis] > 0) {
|
|
|
|
|
PADDLE_ENFORCE_EQ(labels_dims[axis], 1UL,
|
|
|
|
|
"If Attr(soft_label) == false, the axis dimension of "
|
|
|
|
|
"Input(Label) should be 1.");
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
labels_dims[axis], 1UL,
|
|
|
|
|
platform::errors::InvalidArgument("If Attr(soft_label) == false, "
|
|
|
|
|
"the axis dimension of "
|
|
|
|
|
"Input(Label) should be 1."));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|