• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-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_SRC_RUNTIME_DELEGATE_NPU_PASS_NPU_PASS_UTILS_H_
18 #define MINDSPORE_LITE_SRC_RUNTIME_DELEGATE_NPU_PASS_NPU_PASS_UTILS_H_
19 #include <vector>
20 #include <set>
21 #include <string>
22 #include <unordered_map>
23 #include "src/delegate/npu/op/npu_op.h"
24 #include "src/delegate/npu/op/transpose_npu.h"
25 
26 namespace mindspore {
27 extern std::unordered_map<schema::PrimitiveType, std::set<int>> nodes2const_index;
28 class NPUPassUtils {
29  public:
30   static NPUOp *CreateNchw2NhwcOp(const std::vector<mindspore::MSTensor> &in_tensors,
31                                   const std::vector<mindspore::MSTensor> &out_tensors, const std::string &name);
32 
33   static NPUOp *CreateNhwc2NchwOp(const std::vector<mindspore::MSTensor> &in_tensors,
34                                   const std::vector<mindspore::MSTensor> &out_tensors, const std::string &name);
35 
36   static void UpdateOp(NPUOp *op, const std::vector<NPUOp *> &in_ops, const std::vector<NPUOp *> &out_ops,
37                        const std::vector<mindspore::MSTensor> &in_tensors,
38                        const std::vector<mindspore::MSTensor> &out_tensors);
39 
40   static void UpdateNH2NCTransNodePreOp(NPUOp *pre_op, NPUOp *trans_op, NPUOp *op);
41 
42   static void UpdateNC2NHTransNodePreOp(NPUOp *pre_op, const std::vector<NPUOp *> &trans_ops,
43                                         const std::vector<NPUOp *> &ops);
44 
45   static void UpdateNH2NCTransNodePostOp(NPUOp *trans_op, NPUOp *post_op);
46 
47   static void UpdateNC2NHTransNodePostOp(NPUOp *op, NPUOp *trans_op, NPUOp *post_op);
48 
49   static void UpdateNC2NHPostOpInTensors(NPUOp *op, NPUOp *trans_op, NPUOp *post_op);
50 
51   static bool IsNhwc2Nchw(NPUOp *op);
52 
53   static bool IsNchw2Nhwc(NPUOp *op);
54   static NPUOp *OpInputFromOp(NPUOp *op, mindspore::MSTensor in_tensor);
55   static std::vector<mindspore::MSTensor> GetNonConstInputs(NPUOp *op);
56   static bool Scale4dCase(NPUOp *op);
57   static void AssistDataNHWC2NCHW(int *data, size_t unit_size);
58   static int MaskDataNHWC2NCHW(int mask);
59 };
60 }  // namespace mindspore
61 #endif  // MINDSPORE_LITE_SRC_RUNTIME_DELEGATE_NPU_PASS_NPU_PASS_UTILS_H_
62