-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add pre-training transform to convert BatchNorm to BatchNormInternal #7539
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
Merged
SherlockNoMad
merged 6 commits into
microsoft:master
from
pranav-prakash:batchnorm_grad
May 10, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e2ba74f
Add transformer for BatchNorm -> BN Internal
pranav-prakash 8142bbb
Add test for BN replacement transformer
pranav-prakash 8dddf56
Resolve comments
pranav-prakash f2b0844
Merge master
pranav-prakash 772dc3f
Resolve comments
pranav-prakash abc6b35
Revert removal of InsertMaxpoolOutput in gradient_graph_builder
pranav-prakash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
orttraining/orttraining/core/optimizer/batchnorm_replacement.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include "orttraining/core/optimizer/batchnorm_replacement.h" | ||
|
|
||
| #include "core/common/logging/logging.h" | ||
| #include "core/optimizer/rewrite_rule.h" | ||
| #include "core/optimizer/utils.h" | ||
| #include "core/graph/graph.h" | ||
| #include "core/graph/graph_utils.h" | ||
|
|
||
| namespace onnxruntime { | ||
|
|
||
| Status BatchNormReplacement::Apply(Graph& graph, Node& bn_node, RewriteRuleEffect& rule_effect, const logging::Logger&) const { | ||
| const auto& bn_inputs = bn_node.MutableInputDefs(); | ||
| auto& bn_outputs = bn_node.MutableOutputDefs(); | ||
| const NodeArg* scale_input_def = bn_inputs[1]; | ||
| auto scale_input_def_type_proto = scale_input_def->TypeAsProto(); | ||
|
|
||
| if (bn_outputs.size() == 1) { | ||
pranav-prakash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| NodeArg& running_mean_def = graph.GetOrCreateNodeArg(graph.GenerateNodeArgName("running_mean_def"), scale_input_def_type_proto); | ||
| NodeArg& running_var_def = graph.GetOrCreateNodeArg(graph.GenerateNodeArgName("running_var_def"), scale_input_def_type_proto); | ||
| bn_outputs.push_back(&running_mean_def); | ||
| bn_outputs.push_back(&running_var_def); | ||
| } | ||
|
|
||
| if (bn_outputs.size() == 3) { | ||
| NodeArg& saved_mean_def = graph.GetOrCreateNodeArg(graph.GenerateNodeArgName("saved_mean_def"), scale_input_def_type_proto); | ||
| NodeArg& saved_inv_std = graph.GetOrCreateNodeArg(graph.GenerateNodeArgName("saved_inv_std"), scale_input_def_type_proto); | ||
| bn_outputs.push_back(&saved_inv_std); | ||
| bn_outputs.push_back(&saved_mean_def); | ||
| } | ||
|
|
||
| // check Batch Normalization node has 5 output node args for training mode | ||
| ORT_ENFORCE(bn_node.OutputDefs().size() == 5); | ||
|
|
||
| Node& batchnorm_internal_node = graph.AddNode(graph.GenerateNodeName(bn_node.Name() + "_BatchNormInternal"), | ||
| "BatchNormInternal", | ||
| "BatchNormalization with saved mean/inv_std_dev", | ||
| bn_inputs, | ||
| bn_outputs, | ||
| &bn_node.GetAttributes(), | ||
| kMSDomain); | ||
| batchnorm_internal_node.AddAttribute("training_mode", static_cast<int64_t>(1)); | ||
| // Assign provider to this new node. Provider should be same as the provider for old node. | ||
| batchnorm_internal_node.SetExecutionProviderType(bn_node.GetExecutionProviderType()); | ||
| graph_utils::FinalizeNodeFusion(graph, batchnorm_internal_node, bn_node); | ||
| rule_effect = RewriteRuleEffect::kRemovedCurrentNode; | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| bool BatchNormReplacement::SatisfyCondition(const Graph&, const Node&, const logging::Logger&) const { | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace onnxruntime | ||
30 changes: 30 additions & 0 deletions
30
orttraining/orttraining/core/optimizer/batchnorm_replacement.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "core/optimizer/rewrite_rule.h" | ||
|
|
||
| namespace onnxruntime { | ||
|
|
||
| /** | ||
| @Class BatchNorm Replacement | ||
|
|
||
| Rewrite rule that replaces BatchNorm with BatchNormInternal, that has additional outputs | ||
| for saved_mean and saved_std_dev | ||
| */ | ||
| class BatchNormReplacement : public RewriteRule { | ||
| public: | ||
| BatchNormReplacement() noexcept : RewriteRule("BatchNormReplacement") {} | ||
|
|
||
| std::vector<std::string> TargetOpTypes() const noexcept override { | ||
| return {"BatchNormalization"}; | ||
| } | ||
|
|
||
| private: | ||
| bool SatisfyCondition(const Graph& graph, const Node& node, const logging::Logger& logger) const override; | ||
|
|
||
| Status Apply(Graph& graph, Node& node, RewriteRuleEffect& rule_effect, const logging::Logger& logger) const override; | ||
| }; | ||
|
|
||
| } // namespace onnxruntime |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.