• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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 
17 #ifndef MINDSPORE_LITE_TOOLS_OPTIMIZER_FUSION_SCALE_SCALE_FUSION_H_
18 #define MINDSPORE_LITE_TOOLS_OPTIMIZER_FUSION_SCALE_SCALE_FUSION_H_
19 
20 #include <vector>
21 #include <string>
22 #include "tools/optimizer/common/pattern_process_pass_extends.h"
23 
24 namespace mindspore::opt {
25 class ScaleScaleFusion : public LitePatternProcessPass {
26  public:
27   explicit ScaleScaleFusion(bool multigraph = true, const std::string &name = "ScaleScaleFusion")
LitePatternProcessPass(name,multigraph)28       : LitePatternProcessPass(name, multigraph) {}
29   ~ScaleScaleFusion() override = default;
30   const BaseRef DefinePattern() const override;
31   const AnfNodePtr Process(const FuncGraphPtr &, const AnfNodePtr &, const EquivPtr &) const override;
32 
33  private:
34   bool CheckScaleNode(const CNodePtr &scale_cnode) const;
35   int GetInputParamsAndTensors(const CNodePtr &up_scale_cnode, const CNodePtr &down_scale_cnode) const;
36   ParameterPtr GenerateNewWeightNode(const FuncGraphPtr &func_graph, const std::string &name) const;
37   ParameterPtr GenerateNewBiasNode(const FuncGraphPtr &func_graph, const std::string &name) const;
38   tensor::TensorPtr GetMultiplyResultTensorInfo(const tensor::TensorPtr &left_tensor,
39                                                 const tensor::TensorPtr &right_tensor) const;
40 
41  private:
42   mutable std::vector<int64_t> scale_input_shape_;
43   mutable std::vector<int64_t> expand_shape_;
44   mutable tensor::TensorPtr up_weight_tensor_;
45   mutable tensor::TensorPtr up_bias_tensor_;
46   mutable tensor::TensorPtr down_weight_tensor_;
47   mutable tensor::TensorPtr down_bias_tensor_;
48   mutable size_t up_scale_axis_;
49   mutable size_t down_scale_axis_;
50 };
51 }  // namespace mindspore::opt
52 #endif  // MINDSPORE_LITE_TOOLS_OPTIMIZER_FUSION_SCALE_SCALE_FUSION_H_
53