-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Add partial data propagation to enhance shape inference #26269
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
Changes from 50 commits
d345c26
cb52305
fb5a424
bd14235
e2c532f
f79f65b
dbe36a8
d310a26
3fa22a0
92c71be
f652eec
78ed993
035a66e
e3e26a0
6d478cb
9dc51aa
1ec4bf9
1391a15
21d5e33
e59216b
eb8be39
2616c6f
73e38ad
8bbebf4
de2ad68
24ed992
40c8d09
8b444c4
49f7063
4649a73
2043c3a
f6b3f63
89faeab
037c5cb
6bf5e97
114a0fb
2e263ae
dad69a5
a2d12dd
9abfc68
7754921
745e095
b959403
29ff80f
d08da8c
5f9f09a
9b35eee
2c59c09
58c1711
9af5e28
280ebf9
b431a1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include "add_op_data_propagation.h" | ||
|
Check warning on line 4 in onnxruntime/core/graph/data_propagation/add_op_data_propagation.cc
|
||
| #include "core/common/common.h" | ||
| #include "core/graph/node_arg.h" | ||
| #include "core/graph/onnx_protobuf.h" | ||
| #include "core/providers/common.h" | ||
|
|
||
| namespace onnxruntime { | ||
|
|
||
| Status AddOpDataPropagation::infer() { | ||
| // Get "A" input | ||
| const auto* input_0 = node_.InputDefs()[0]; | ||
| // Get "B" input | ||
| const auto* input_1 = node_.InputDefs()[1]; | ||
|
|
||
| // Return and do nothing if input doesn't exist | ||
| if (!input_0 || !input_1) { | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| if (input_0->GetInferredShapeScalarValue().has_value() && input_1->GetInferredShapeScalarValue().has_value()) { | ||
| output_def_.SetInferredShapeScalarValue( | ||
| input_0->GetInferredShapeScalarValue().value() + | ||
| input_1->GetInferredShapeScalarValue().value()); | ||
| } | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| } // namespace onnxruntime | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
|
||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "custom_data_propagation.h" | ||
|
Check warning on line 6 in onnxruntime/core/graph/data_propagation/add_op_data_propagation.h
|
||
| #include "core/graph/graph.h" | ||
|
|
||
| namespace onnxruntime { | ||
|
|
||
| /** | ||
| * @brief Class to infer the output scalar for 'Add' operator given the input is a scalar related to shape. | ||
| * | ||
| * | ||
| * For example: | ||
| * | ||
| * (input with the shape as float32[1, 3, 64, 64]) | ||
| * | | ||
| * v | ||
| * Shape (It saves [1, 3, 64, 64] in inferred_shape_values_ in output's node_arg | ||
| * | during Graph::SaveShapeValuesFromDataPropagation()) | ||
| * | | ||
| * | ______ | ||
| * | | | ||
| * v v | ||
| * Gather Gather (First 'Gather' saves 3 in inferred_scalar_value_ in output node_arg, and | ||
| * | | second 'Gather' saves 64 in inferred_scalar_value_ in output node_arg | ||
| * | | during GatherOpDataPropagation(), if the 'index' attributes | ||
| * | | are 1 and 2 respectively) | ||
| * \ / | ||
| * \ / | ||
| * | | | ||
| * v v | ||
| * Add (It gets 3 from inferred_scalar_value_ in input A's node_arg and 64 from inferred_scalar_value_ | ||
| * | in input B's node_arg, then performs add operation to get 67 and saves in inferred_scalar_value_ | ||
| * | in output's node_arg) | ||
| * v | ||
| * ... | ||
| */ | ||
| class AddOpDataPropagation : public CustomDataPropagationBase { | ||
| public: | ||
| AddOpDataPropagation(const Node& node, | ||
| NodeArg& output_def, | ||
| std::function<Status(const std::string&, TensorShapeVector&)> func, | ||
|
Check warning on line 44 in onnxruntime/core/graph/data_propagation/add_op_data_propagation.h
|
||
| const ONNX_NAMESPACE::TypeProto& output_from_onnx_op_data_propagation, | ||
| const logging::Logger& logger) noexcept | ||
| : CustomDataPropagationBase(node, output_def, func, output_from_onnx_op_data_propagation, logger) {} | ||
|
|
||
| Status infer() override; | ||
| }; | ||
|
|
||
| } // namespace onnxruntime | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
|
||
| // Licensed under the MIT License. | ||
|
|
||
| #include "custom_data_propagation.h" | ||
|
Check warning on line 4 in onnxruntime/core/graph/data_propagation/custom_data_propagation.cc
|
||
| #include "core/common/common.h" | ||
| #include "core/graph/graph.h" | ||
| #include "core/common/logging/logging.h" | ||
| #include "size_op_data_propagation.h" | ||
|
Check warning on line 8 in onnxruntime/core/graph/data_propagation/custom_data_propagation.cc
|
||
| #include "squeeze_op_data_propagation.h" | ||
|
Check warning on line 9 in onnxruntime/core/graph/data_propagation/custom_data_propagation.cc
|
||
| #include "unsqueeze_op_data_propagation.h" | ||
|
Check warning on line 10 in onnxruntime/core/graph/data_propagation/custom_data_propagation.cc
|
||
| #include "gather_op_data_propagation.h" | ||
|
Check warning on line 11 in onnxruntime/core/graph/data_propagation/custom_data_propagation.cc
|
||
| #include "add_op_data_propagation.h" | ||
|
Check warning on line 12 in onnxruntime/core/graph/data_propagation/custom_data_propagation.cc
|
||
| #include "sub_op_data_propagation.h" | ||
| #include "mul_op_data_propagation.h" | ||
| #include "div_op_data_propagation.h" | ||
| #include <onnx/onnx-ml.pb.h> | ||
|
|
||
| namespace onnxruntime { | ||
|
|
||
| std::unique_ptr<CustomDataPropagationBase> CreateCustomDataPropagation(const Node& node, | ||
| NodeArg& output_def, | ||
| std::function<Status(const std::string&, TensorShapeVector&)> func, | ||
| const ONNX_NAMESPACE::TypeProto& output_from_onnx_op_data_propagation, | ||
| const logging::Logger& logger) { | ||
| int dim_size = 0; | ||
| if (output_from_onnx_op_data_propagation.has_tensor_type() && | ||
| output_from_onnx_op_data_propagation.tensor_type().has_shape()) { | ||
| dim_size = output_from_onnx_op_data_propagation.tensor_type().shape().dim_size(); | ||
| } | ||
|
|
||
| if (node.OpType() == "Size") { | ||
| return std::make_unique<SizeOpDataPropagation>(node, output_def, std::move(func), output_from_onnx_op_data_propagation, logger); | ||
| } else if (node.OpType() == "Squeeze") { | ||
| return std::make_unique<SqueezeOpDataPropagation>(node, output_def, std::move(func), output_from_onnx_op_data_propagation, logger); | ||
| } else if (node.OpType() == "Unsqueeze") { | ||
| return std::make_unique<UnsqueezeOpDataPropagation>(node, output_def, std::move(func), output_from_onnx_op_data_propagation, logger); | ||
| } else if (dim_size == 0) { | ||
| if (node.OpType() == "Gather") { | ||
| return std::make_unique<GatherOpDataPropagation>(node, output_def, std::move(func), output_from_onnx_op_data_propagation, logger); | ||
| } else if (node.OpType() == "Add") { | ||
| return std::make_unique<AddOpDataPropagation>(node, output_def, std::move(func), output_from_onnx_op_data_propagation, logger); | ||
| } else if (node.OpType() == "Sub") { | ||
| return std::make_unique<SubOpDataPropagation>(node, output_def, std::move(func), output_from_onnx_op_data_propagation, logger); | ||
| } else if (node.OpType() == "Mul") { | ||
| return std::make_unique<MulOpDataPropagation>(node, output_def, std::move(func), output_from_onnx_op_data_propagation, logger); | ||
| } else if (node.OpType() == "Div") { | ||
| return std::make_unique<DivOpDataPropagation>(node, output_def, std::move(func), output_from_onnx_op_data_propagation, logger); | ||
| } | ||
| } | ||
| return nullptr; | ||
| } | ||
|
|
||
| } // namespace onnxruntime | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
|
||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "core/common/common.h" | ||
| #include "core/graph/graph.h" | ||
| #include "core/common/logging/logging.h" | ||
| #include <onnx/onnx-ml.pb.h> | ||
|
|
||
| namespace onnxruntime { | ||
|
|
||
| /** | ||
| * @class CustomDataPropagation | ||
| * Custom data propagation for the operator to help enhance shape inference. | ||
| * | ||
| * Calling infer() can infer the output values for the specific operator given the input is shape values | ||
| * and saves the output values in output node_arg for other operators to use later. | ||
| * The purpose of this class is to make shape values being correctly inferred and propogated through the graph. | ||
| */ | ||
| class CustomDataPropagationBase { | ||
| public: | ||
| ORT_DISALLOW_COPY(CustomDataPropagationBase); | ||
| virtual ~CustomDataPropagationBase() = default; | ||
| virtual Status infer() = 0; | ||
|
|
||
| protected: | ||
| CustomDataPropagationBase(const Node& node, | ||
| NodeArg& output_def, | ||
| std::function<Status(const std::string&, TensorShapeVector&)> func, | ||
| const ONNX_NAMESPACE::TypeProto& output_from_onnx_op_data_propagation, | ||
| const logging::Logger& logger) noexcept | ||
| : node_(node), | ||
| output_def_(output_def), | ||
| get_initialized_input_values_func_(std::move(func)), | ||
| output_from_onnx_op_data_propagation_(output_from_onnx_op_data_propagation), | ||
| logger_(logger) {} | ||
|
|
||
| const Node& node_; | ||
| NodeArg& output_def_; | ||
| std::function<Status(const std::string&, TensorShapeVector&)> get_initialized_input_values_func_; | ||
| const ONNX_NAMESPACE::TypeProto& output_from_onnx_op_data_propagation_; | ||
| const logging::Logger& logger_; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Create custom data propagation for the operator. | ||
| * | ||
| * For certain operators (e.g., Size, Squeeze, Unsqueeze), ONNX's | ||
| * PartialDataPropagationFunction() does not always produce complete or accurate | ||
| * inferred shape values. | ||
| * | ||
| * In particular: | ||
| * - Scalar inputs and outputs are not handled correctly. | ||
| * - Some operators require additional logic that is not covered by the default function, | ||
| e.g. PartialDataPropagationFunction. | ||
| * | ||
| * Therefore, for these cases, we perform custom data propagation to ensure | ||
| * correct and complete inference. | ||
| * | ||
| * @param node The ORT's node | ||
| * @param output_def The node's output NodeArg to save the inferred shape values if needed | ||
| * @param func Helper function to get the input value if it's a initializer | ||
| * @param output_from_onnx_op_data_propagation The result from executing ONNX operator's data propagation | ||
| * @param logger The reference to a logger | ||
| * @return std::unique_ptr<CustomDataPropagation> Returns a CustomDataPropagation object if available | ||
| */ | ||
| std::unique_ptr<CustomDataPropagationBase> CreateCustomDataPropagation( | ||
| const Node& node, | ||
| NodeArg& output_def, | ||
| std::function<Status(const std::string&, TensorShapeVector&)> func, | ||
| const ONNX_NAMESPACE::TypeProto& output_from_onnx_op_data_propagation, | ||
| const logging::Logger& logger); | ||
|
|
||
| } // namespace onnxruntime | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
|
||
| // Licensed under the MIT License. | ||
|
|
||
| #include "div_op_data_propagation.h" | ||
| #include "core/common/common.h" | ||
| #include "core/graph/node_arg.h" | ||
| #include "core/graph/onnx_protobuf.h" | ||
| #include "core/providers/common.h" | ||
|
|
||
| namespace onnxruntime { | ||
|
|
||
| Status DivOpDataPropagation::infer() { | ||
| // Get "A" input | ||
| const auto* input_0 = node_.InputDefs()[0]; | ||
| // Get "B" input | ||
| const auto* input_1 = node_.InputDefs()[1]; | ||
|
|
||
| // Return and do nothing if input doesn't exist | ||
| if (!input_0 || !input_1) { | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| if (input_0->GetInferredShapeScalarValue().has_value() && input_1->GetInferredShapeScalarValue().has_value()) { | ||
| output_def_.SetInferredShapeScalarValue( | ||
| input_0->GetInferredShapeScalarValue().value() / | ||
| input_1->GetInferredShapeScalarValue().value()); | ||
| } | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| } // namespace onnxruntime | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
|
||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "custom_data_propagation.h" | ||
| #include "core/graph/graph.h" | ||
|
|
||
| namespace onnxruntime { | ||
|
|
||
| /** | ||
| * @brief Class to infer the output scalar for 'Div' operator given the input is a scalar related to shape. | ||
| * | ||
| * | ||
| * For example: | ||
| * | ||
| * (input with the shape as float32[1, 3, 64, 64]) | ||
| * | | ||
| * v | ||
| * Shape (It saves [1, 3, 64, 64] in inferred_shape_values_ in output's node_arg | ||
| * | during graph::SaveShapeValuesFromDataPropagation()) | ||
| * | | ||
| * | ______ | ||
| * | | | ||
| * v v | ||
| * Gather Gather (First 'Gather' saves 64 in inferred_scalar_value_ in output node_arg, and | ||
| * | | second 'Gather' saves 1 in inferred_scalar_value_ in output node_arg | ||
| * | | during GatherOpDataPropagation(), if the 'index' attributes | ||
| * | | are 2 and 0 respectively) | ||
| * \ / | ||
| * \ / | ||
| * | | | ||
| * v v | ||
| * Div (It gets 64 from inferred_scalar_value_ in input A's node_arg and 1 from inferred_scalar_value_ | ||
| * | in input B's node_arg, then performs div operation to get 64 and saves in inferred_scalar_value_ | ||
| * | in output's node_arg) | ||
| * v | ||
| * ... | ||
| */ | ||
| class DivOpDataPropagation : public CustomDataPropagationBase { | ||
| public: | ||
| DivOpDataPropagation(const Node& node, | ||
| NodeArg& output_def, | ||
| std::function<Status(const std::string&, TensorShapeVector&)> func, | ||
| const ONNX_NAMESPACE::TypeProto& output_from_onnx_op_data_propagation, | ||
| const logging::Logger& logger) noexcept | ||
| : CustomDataPropagationBase(node, output_def, func, output_from_onnx_op_data_propagation, logger) {} | ||
|
|
||
| Status infer() override; | ||
| }; | ||
|
|
||
| } // namespace onnxruntime | ||
Uh oh!
There was an error while loading. Please reload this page.