You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

arithmetic_simplify.h 2.6 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_ARITHMETIC_SIMPLIFY_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_ARITHMETIC_SIMPLIFY_H_
  18. #include <algorithm>
  19. #include <memory>
  20. #include <vector>
  21. #include "frontend/optimizer/irpass.h"
  22. #include "frontend/optimizer/irpass/prim_eliminate.h"
  23. #include "frontend/optimizer/optimizer_caller.h"
  24. #include "frontend/optimizer/anf_visitor.h"
  25. #include "ir/pattern_matcher.h"
  26. namespace mindspore {
  27. namespace opt {
  28. namespace irpass {
  29. // grad = AllReduce(grad) / worker_number
  30. // grad = grad + weight * decy
  31. // ->
  32. // grad = grad + weight * decy
  33. // grad = AllReduce(grad) / worker_number
  34. // {prim::kPrimAddN, {prim::kPrimMakeTuple, {prim::kPrimMul, {prim::kPrimAllReduce, X}, Y}, Z}} ->
  35. // {prim::kPrimMul, {prim::kPrimAllReduce, {prim::kPrimAddN,{prim::kPrimMakeTuple, Z, X}}}, Y}
  36. class AdjustAllReduceMulAdd : public OptimizerCaller {
  37. public:
  38. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override;
  39. void ProcessDependEdge(const FuncGraphPtr &fg, const AnfNodePtr &addn_maketuple, const AnfNodePtr &new_node);
  40. private:
  41. AnfNodePtr mul_cnode_{nullptr};
  42. };
  43. class ArithmeticSimplify : public OptimizerCaller {
  44. public:
  45. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override;
  46. };
  47. // Arithmetic Simplifications should be done after step_parallel.
  48. // eg: Mul(0, weight) where weight is a parameter will be simplified to a constant tensor
  49. // with shape(weight), but after step_parallel, shape of weight may be changed, so the
  50. // shape of the constant tensor should also be changed. So this pass is seperated from
  51. // ArithmeticSimplify and deferred until step_parallel.
  52. class ArithmeticSimplify2 : public OptimizerCaller {
  53. public:
  54. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override;
  55. };
  56. } // namespace irpass
  57. } // namespace opt
  58. } // namespace mindspore
  59. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_ARITHMETIC_SIMPLIFY_H_