From 88c3704257e18dbee022390acbb89e4b0b7226d4 Mon Sep 17 00:00:00 2001 From: Alberto Magni <49027342+alberto-magni@users.noreply.github.com> Date: Thu, 12 Nov 2020 20:18:54 +0000 Subject: [PATCH] Add shape inference for additional ops This commit adds shape inference support for the following ops: SoftmaxCrossEntropy SoftmaxCrossEntropyLossGrad SoftmaxCrossEntropyGrad LayerNormalizationGrad Motivation and Context --- .../core/graph/training_op_defs.cc | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/orttraining/orttraining/core/graph/training_op_defs.cc b/orttraining/orttraining/core/graph/training_op_defs.cc index abd8a7aa9b..6130afb64c 100644 --- a/orttraining/orttraining/core/graph/training_op_defs.cc +++ b/orttraining/orttraining/core/graph/training_op_defs.cc @@ -871,6 +871,34 @@ Example 4: "T", {"tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bfloat16)"}, "Constrain to float, float16 and double tensors.") + .TypeAndShapeInferenceFunction([](InferenceContext& ctx) { + propagateElemTypeFromInputToOutput(ctx, 0, 0); + std::string reduction = getAttribute(ctx, "reduction", "mean"); + if (reduction.compare("none") == 0) { + if (hasInputShape(ctx, 1)) { + // If no reduction is performed the shape of the loss looks + // like the shape of the labels, without the onehot dimension. + + TensorShapeProto loss_shape; + const TensorShapeProto& label_shape = ctx.getInputType(1)->tensor_type().shape(); + + for (int i = 0; i != label_shape.dim_size() - 1; ++i) { + *loss_shape.add_dim() = label_shape.dim(i); + } + + *ctx.getOutputType(0)->mutable_tensor_type()->mutable_shape() = + loss_shape; + } + + } else { + updateOutputShape(ctx, 0, {}); + } + + if (ctx.getNumOutputs() == 2) { + propagateElemTypeFromInputToOutput(ctx, 0, 1); + propagateShapeFromInputToOutput(ctx, 0, 1); + } + }) .SetDoc(R"DOC(SoftmaxCrossEntropy)DOC"); ONNX_CONTRIB_OPERATOR_SCHEMA(SoftmaxCrossEntropyGrad) @@ -888,6 +916,10 @@ Example 4: "T", {"tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bfloat16)"}, "Constrain to float, float16 and double tensors.") + .TypeAndShapeInferenceFunction([](InferenceContext& ctx) { + propagateElemTypeFromInputToOutput(ctx, 1, 0); + propagateShapeFromInputToOutput(ctx, 1, 0); + }) .SetDoc(R"DOC(SoftmaxCrossEntropyGrad)DOC"); ONNX_CONTRIB_OPERATOR_SCHEMA(HorovodAllReduce) @@ -1062,6 +1094,10 @@ Example 4: .TypeConstraint("Tind", {"tensor(int32)", "tensor(int64)"}, "Constrain indices to integer types") + .TypeAndShapeInferenceFunction([](InferenceContext& ctx) { + propagateElemTypeFromInputToOutput(ctx, 1, 0); + propagateShapeFromInputToOutput(ctx, 1, 0); + }) .SetDoc(R"DOC(SoftmaxCrossEntropyLossGrad)DOC"); ONNX_CONTRIB_OPERATOR_SCHEMA(BiasDropout) @@ -1652,7 +1688,16 @@ Example 4: .TypeConstraint( "U", {"tensor(float)", "tensor(bfloat16)"}, - "Constrain mean and inv_std_var to float tensors."); + "Constrain mean and inv_std_var to float tensors.") + .TypeAndShapeInferenceFunction([](ONNX_NAMESPACE::InferenceContext& ctx) { + propagateElemTypeFromInputToOutput(ctx, 1, 0); + propagateShapeFromInputToOutput(ctx, 1, 0); + propagateElemTypeFromInputToOutput(ctx, 2, 1); + propagateShapeFromInputToOutput(ctx, 2, 1); + // The bias tensor has the same shape of the scale tensor. + propagateElemTypeFromInputToOutput(ctx, 2, 2); + propagateShapeFromInputToOutput(ctx, 2, 2); + }); ONNX_CONTRIB_OPERATOR_SCHEMA(SimplifiedLayerNormalizationGrad) .SetDomain(kMSDomain)