From: @xsmq Reviewed-by: @lilongfei15,@kingxian Signed-off-by: @lilongfei15tags/v1.2.0-rc1^0
| @@ -34,7 +34,12 @@ void CreateOutputsOfUpdateGrad(const FuncGraphPtr &graph, const CNodePtr &bn_gra | |||
| MS_EXCEPTION_IF_NULL(graph); | |||
| MS_EXCEPTION_IF_NULL(bn_grad_node); | |||
| auto bn_grad_inputs = bn_grad_node->inputs(); | |||
| CheckCNodeInputSize(bn_grad_node, kBNGradInputTensorNum); | |||
| if (AnfAlgo::CheckPrimitiveType(bn_grad_node, prim::kPrimBatchNormGrad)) { | |||
| CheckCNodeInputSize(bn_grad_node, kBNGradInputTensorNum); | |||
| } else { | |||
| CheckCNodeInputSize(bn_grad_node, kSyncBNGradInputTensorNum); | |||
| } | |||
| std::vector<AnfNodePtr> bn_update_grad_inputs = { | |||
| NewValueNode(std::make_shared<Primitive>(kBNTrainingUpdateGradOpName)), bn_grad_inputs[1], bn_grad_inputs[2], | |||
| bn_grad_inputs[4], bn_grad_inputs[5]}; | |||
| @@ -57,7 +62,11 @@ void CreateOutputsOfReduceGrad(const FuncGraphPtr &graph, const CNodePtr &bn_gra | |||
| MS_EXCEPTION_IF_NULL(graph); | |||
| MS_EXCEPTION_IF_NULL(bn_grad_node); | |||
| auto bn_grad_inputs = bn_grad_node->inputs(); | |||
| CheckCNodeInputSize(bn_grad_node, kBNGradInputTensorNum); | |||
| if (AnfAlgo::CheckPrimitiveType(bn_grad_node, prim::kPrimBatchNormGrad)) { | |||
| CheckCNodeInputSize(bn_grad_node, kBNGradInputTensorNum); | |||
| } else { | |||
| CheckCNodeInputSize(bn_grad_node, kSyncBNGradInputTensorNum); | |||
| } | |||
| if (bn_update_grad_outputs.size() != kBNTrainingUpdateGradOutputNum) { | |||
| MS_LOG(EXCEPTION) << "bn_update_grad_outputs has wrong size"; | |||
| @@ -1,81 +0,0 @@ | |||
| /** | |||
| * Copyright 2021 Huawei Technologies Co., Ltd | |||
| * | |||
| * Licensed under the Apache License, Version 2.0 (the "License"); | |||
| * you may not use this file except in compliance with the License. | |||
| * You may obtain a copy of the License at | |||
| * | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * Unless required by applicable law or agreed to in writing, software | |||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| * See the License for the specific language governing permissions and | |||
| * limitations under the License. | |||
| */ | |||
| #include "backend/optimizer/ascend/mindir/bn_grad_unify_mindir.h" | |||
| #include <vector> | |||
| #include <memory> | |||
| #include "utils/utils.h" | |||
| #include "utils/ms_context.h" | |||
| #include "backend/optimizer/common/helper.h" | |||
| #include "runtime/device/kernel_info.h" | |||
| #include "backend/session/anf_runtime_algorithm.h" | |||
| #include "utils/trace_base.h" | |||
| namespace mindspore { | |||
| namespace opt { | |||
| namespace { | |||
| constexpr auto kAttrUnifyIRPassed = "unifyir_passed"; | |||
| AnfNodePtr CreateNewBatchNormGrad(const FuncGraphPtr &graph, const CNodePtr &bn_grad_node) { | |||
| MS_EXCEPTION_IF_NULL(graph); | |||
| MS_EXCEPTION_IF_NULL(bn_grad_node); | |||
| size_t kBNGradInputNum = 6; | |||
| const auto &bn_grad_node_inputs = bn_grad_node->inputs(); | |||
| CheckCNodeInputSize(bn_grad_node, kBNGradInputNum); | |||
| std::vector<AnfNodePtr> bn_grad_inputs = {NewValueNode(std::make_shared<Primitive>(kBatchNormGradOpName)), | |||
| bn_grad_node_inputs[1], | |||
| bn_grad_node_inputs[2], | |||
| bn_grad_node_inputs[3], | |||
| bn_grad_node_inputs[4], | |||
| bn_grad_node_inputs[5]}; | |||
| auto new_bn_grad = graph->NewCNode(bn_grad_inputs); | |||
| MS_EXCEPTION_IF_NULL(new_bn_grad); | |||
| new_bn_grad->set_scope(bn_grad_node->scope()); | |||
| auto types = {AnfAlgo::GetOutputInferDataType(bn_grad_node, 0), AnfAlgo::GetOutputInferDataType(bn_grad_node, 1), | |||
| AnfAlgo::GetOutputInferDataType(bn_grad_node, 2), | |||
| AnfAlgo::GetPrevNodeOutputInferDataType(bn_grad_node, 3), | |||
| AnfAlgo::GetPrevNodeOutputInferDataType(bn_grad_node, 4)}; | |||
| auto shapes = {AnfAlgo::GetOutputInferShape(bn_grad_node, 0), AnfAlgo::GetOutputInferShape(bn_grad_node, 1), | |||
| AnfAlgo::GetOutputInferShape(bn_grad_node, 2), AnfAlgo::GetPrevNodeOutputInferShape(bn_grad_node, 3), | |||
| AnfAlgo::GetPrevNodeOutputInferShape(bn_grad_node, 4)}; | |||
| AnfAlgo::SetOutputInferTypeAndShape(types, shapes, new_bn_grad.get()); | |||
| AnfAlgo::CopyNodeAttrs(bn_grad_node, new_bn_grad); | |||
| AnfAlgo::SetNodeAttr(kAttrUnifyIRPassed, MakeValue(true), new_bn_grad); | |||
| return new_bn_grad; | |||
| } | |||
| } // namespace | |||
| const BaseRef BatchNormGradUnifyMindIR::DefinePattern() const { | |||
| VarPtr Xs = std::make_shared<SeqVar>(); | |||
| auto prim = std::make_shared<Primitive>(kBatchNormGradOpName); | |||
| return VectorRef({prim, Xs}); | |||
| } | |||
| const AnfNodePtr BatchNormGradUnifyMindIR::Process(const FuncGraphPtr &func_graph, const AnfNodePtr &node, | |||
| const EquivPtr &) const { | |||
| MS_EXCEPTION_IF_NULL(node); | |||
| MS_EXCEPTION_IF_NULL(func_graph); | |||
| auto cnode = node->cast<CNodePtr>(); | |||
| MS_EXCEPTION_IF_NULL(cnode); | |||
| if (AnfAlgo::HasNodeAttr(kAttrUnifyIRPassed, cnode)) { | |||
| return nullptr; | |||
| } | |||
| return CreateNewBatchNormGrad(func_graph, cnode); | |||
| } | |||
| } // namespace opt | |||
| } // namespace mindspore | |||
| @@ -1,33 +0,0 @@ | |||
| /** | |||
| * Copyright 2021 Huawei Technologies Co., Ltd | |||
| * | |||
| * Licensed under the Apache License, Version 2.0 (the "License"); | |||
| * you may not use this file except in compliance with the License. | |||
| * You may obtain a copy of the License at | |||
| * | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * Unless required by applicable law or agreed to in writing, software | |||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| * See the License for the specific language governing permissions and | |||
| * limitations under the License. | |||
| */ | |||
| #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_MINDIR_BN_GRAD_UNIFY_MINDIR_H_ | |||
| #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_MINDIR_BN_GRAD_UNIFY_MINDIR_H_ | |||
| #include "backend/optimizer/common/optimizer.h" | |||
| #include "backend/optimizer/common/helper.h" | |||
| namespace mindspore { | |||
| namespace opt { | |||
| class BatchNormGradUnifyMindIR : public PatternProcessPass { | |||
| public: | |||
| explicit BatchNormGradUnifyMindIR(bool multigraph = true) : PatternProcessPass("bn_grad_unify_mindir", multigraph) {} | |||
| ~BatchNormGradUnifyMindIR() override = default; | |||
| const BaseRef DefinePattern() const override; | |||
| const AnfNodePtr Process(const FuncGraphPtr &, const AnfNodePtr &, const EquivPtr &) const override; | |||
| }; | |||
| } // namespace opt | |||
| } // namespace mindspore | |||
| #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_MINDIR_BN_GRAD_UNIFY_MINDIR_H_ | |||
| @@ -56,7 +56,8 @@ constexpr size_t kBN1OutputNum = 2; | |||
| constexpr size_t kBN2OutputNum = 3; | |||
| constexpr size_t kBN3OutputNum = 1; | |||
| constexpr size_t kBNGradInputTensorNum = 5; | |||
| constexpr size_t kBNGradInputTensorNum = 6; | |||
| constexpr size_t kSyncBNGradInputTensorNum = 5; | |||
| constexpr size_t kBNGradOutputNum = 3; | |||
| constexpr size_t kBNGrad1OutputNum = 3; | |||
| @@ -42,7 +42,6 @@ | |||
| #include "backend/optimizer/ascend/mindir/sparse_softmax_cross_entropy_with_logits_unify_mindir.h" | |||
| #include "backend/optimizer/ascend/mindir/slice_grad_unify_mindir.h" | |||
| #include "backend/optimizer/ascend/mindir/avg_pool_grad_unify_mindir.h" | |||
| #include "backend/optimizer/ascend/mindir/bn_grad_unify_mindir.h" | |||
| #include "runtime/device/kernel_adjust.h" | |||
| #include "runtime/device/ascend/ascend_stream_assign.h" | |||
| #include "backend/session/anf_runtime_algorithm.h" | |||
| @@ -249,7 +248,6 @@ void AscendSession::UnifyMindIR(const KernelGraphPtr &graph) { | |||
| } | |||
| unify_mindir_pm->AddPass(std::make_shared<opt::DropoutUnifyMindIR1>()); | |||
| unify_mindir_pm->AddPass(std::make_shared<opt::DropoutGradUnifyMindIR>()); | |||
| unify_mindir_pm->AddPass(std::make_shared<opt::BatchNormGradUnifyMindIR>()); | |||
| optimizer->AddPassManager(unify_mindir_pm); | |||
| (void)optimizer->Optimize(graph); | |||
| @@ -113,8 +113,8 @@ def _partial_init(cls_or_self, **kwargs): | |||
| Examples: | |||
| >>> class Foo: | |||
| ... def __init__(self, a, b, answer): | |||
| ... pass | |||
| >>> def __init__(self, a, b, answer): | |||
| >>> pass | |||
| >>> Foo.partial_init = classmethod(_partial_init) | |||
| >>> foo_builder = Foo.partial_init(a=3, b=4).partial_init(answer=42) | |||
| >>> foo_instance1 = foo_builder() | |||
| @@ -15,7 +15,6 @@ | |||
| */ | |||
| #include "backend/optimizer/ascend/ir_fission/batch_norm_grad_infer_fission.h" | |||
| #include "backend/optimizer/ascend/mindir/bn_grad_unify_mindir.h" | |||
| #include "common/backend_common_test.h" | |||
| #include "common/py_func_graph_fetcher.h" | |||
| @@ -43,7 +42,6 @@ TEST_F(TestHWBatchNormGradInferFission, test_batch_norm_grad_infer_fission) { | |||
| auto optimizer = std::make_shared<opt::GraphOptimizer>(); | |||
| auto pm = std::make_shared<opt::PassManager>(); | |||
| pm->AddPass(std::make_shared<opt::BatchNormGradUnifyMindIR>()); | |||
| pm->AddPass(std::make_shared<opt::BatchNormGradInferFission>()); | |||
| optimizer->AddPassManager(pm); | |||
| FuncGraphPtr new_graph = optimizer->Optimize(kg); | |||
| @@ -65,7 +63,6 @@ TEST_F(TestHWBatchNormGradInferFission, test_batch_norm_grad_infer_no_fission1) | |||
| auto optimizer = std::make_shared<opt::GraphOptimizer>(); | |||
| auto pm = std::make_shared<opt::PassManager>(); | |||
| pm->AddPass(std::make_shared<opt::BatchNormGradUnifyMindIR>()); | |||
| pm->AddPass(std::make_shared<opt::BatchNormGradInferFission>()); | |||
| optimizer->AddPassManager(pm); | |||
| FuncGraphPtr new_graph = optimizer->Optimize(kg); | |||
| @@ -85,7 +82,6 @@ TEST_F(TestHWBatchNormGradInferFission, test_batch_norm_grad_infer_no_fission2) | |||
| auto optimizer = std::make_shared<opt::GraphOptimizer>(); | |||
| auto pm = std::make_shared<opt::PassManager>(); | |||
| pm->AddPass(std::make_shared<opt::BatchNormGradUnifyMindIR>()); | |||
| pm->AddPass(std::make_shared<opt::BatchNormGradInferFission>()); | |||
| optimizer->AddPassManager(pm); | |||
| FuncGraphPtr new_graph = optimizer->Optimize(kg); | |||