1From aa1ba4e66099334f25fa83d7c27c92900b735b4e Mon Sep 17 00:00:00 2001 2From: qinzheng4 <qinzheng4@huawei.com> 3Date: Thu, 28 Dec 2023 22:23:50 +0800 4Subject: [PATCH] 0006-remove-lite-expression-fix-double-loadso 5 6--- 7 include/api/graph.h | 15 - 8 include/api/model.h | 14 - 9 include/api/net.h | 142 ------- 10 .../cpu/kernel/nnacl/infer/reshape_infer.c | 2 +- 11 mindspore/lite/BUILD.gn | 48 +-- 12 mindspore/lite/src/CMakeLists.txt | 32 +- 13 .../common/ops/populate/custom_populate.cc | 9 +- 14 mindspore/lite/src/expression/cfg.h | 68 ---- 15 mindspore/lite/src/expression/export.cc | 76 ---- 16 mindspore/lite/src/expression/export.h | 52 --- 17 mindspore/lite/src/expression/expr.cc | 98 ----- 18 mindspore/lite/src/expression/expr.h | 70 ---- 19 mindspore/lite/src/expression/import.cc | 180 --------- 20 mindspore/lite/src/expression/import.h | 61 --- 21 mindspore/lite/src/expression/net.cc | 268 ------------- 22 mindspore/lite/src/expression/net.h | 114 ------ 23 mindspore/lite/src/expression/node.cc | 271 ------------- 24 mindspore/lite/src/expression/node.h | 156 -------- 25 mindspore/lite/src/expression/ops.cc | 66 ---- 26 mindspore/lite/src/expression/ops.h | 69 ---- 27 .../lite/src/expression/ops/activation.cc | 133 ------- 28 .../lite/src/expression/ops/activation.h | 44 --- 29 mindspore/lite/src/expression/ops/adam.cc | 142 ------- 30 mindspore/lite/src/expression/ops/adam.h | 46 --- 31 mindspore/lite/src/expression/ops/addn.cc | 42 -- 32 mindspore/lite/src/expression/ops/addn.h | 34 -- 33 .../lite/src/expression/ops/arithmetic.cc | 223 ----------- 34 .../lite/src/expression/ops/arithmetic.h | 75 ---- 35 .../src/expression/ops/arithmetic_self.cc | 72 ---- 36 .../lite/src/expression/ops/arithmetic_self.h | 46 --- 37 mindspore/lite/src/expression/ops/assign.cc | 60 --- 38 mindspore/lite/src/expression/ops/assign.h | 35 -- 39 .../lite/src/expression/ops/batchnorm.cc | 135 ------- 40 mindspore/lite/src/expression/ops/batchnorm.h | 43 -- 41 mindspore/lite/src/expression/ops/biasadd.cc | 93 ----- 42 mindspore/lite/src/expression/ops/biasadd.h | 44 --- 43 mindspore/lite/src/expression/ops/conv.cc | 241 ------------ 44 mindspore/lite/src/expression/ops/conv.h | 58 --- 45 mindspore/lite/src/expression/ops/dense.cc | 151 ------- 46 mindspore/lite/src/expression/ops/dense.h | 44 --- 47 mindspore/lite/src/expression/ops/depend.cc | 43 -- 48 mindspore/lite/src/expression/ops/dropout.cc | 91 ----- 49 mindspore/lite/src/expression/ops/dropout.h | 42 -- 50 mindspore/lite/src/expression/ops/flatten.cc | 71 ---- 51 mindspore/lite/src/expression/ops/flatten.h | 36 -- 52 mindspore/lite/src/expression/ops/pooling.cc | 215 ---------- 53 mindspore/lite/src/expression/ops/pooling.h | 74 ---- 54 mindspore/lite/src/expression/ops/reduce.cc | 126 ------ 55 mindspore/lite/src/expression/ops/reduce.h | 42 -- 56 mindspore/lite/src/expression/ops/reshape.cc | 74 ---- 57 mindspore/lite/src/expression/ops/reshape.h | 37 -- 58 mindspore/lite/src/expression/ops/softmax.cc | 119 ------ 59 mindspore/lite/src/expression/ops/softmax.h | 39 -- 60 .../lite/src/expression/ops/softmaxCE.cc | 93 ----- 61 mindspore/lite/src/expression/ops/softmaxCE.h | 47 --- 62 mindspore/lite/src/expression/ops/tile.cc | 62 --- 63 mindspore/lite/src/expression/ops/tile.h | 40 -- 64 .../lite/src/expression/ops/transpose.cc | 88 ----- 65 mindspore/lite/src/expression/ops/transpose.h | 59 --- 66 mindspore/lite/src/expression/ops_utils.cc | 275 ------------- 67 mindspore/lite/src/expression/ops_utils.h | 69 ---- 68 mindspore/lite/src/expression/param.cc | 70 ---- 69 mindspore/lite/src/expression/param.h | 60 --- 70 mindspore/lite/src/expression/sequential.cc | 30 -- 71 mindspore/lite/src/expression/sequential.h | 32 -- 72 .../lite/src/litert/cxx_api/expression/net.cc | 145 ------- 73 .../src/litert/cxx_api/expression/net_impl.cc | 220 ----------- 74 .../src/litert/cxx_api/expression/net_impl.h | 95 ----- 75 .../litert/cxx_api/expression/node_impl.cc | 50 --- 76 .../src/litert/cxx_api/expression/node_impl.h | 71 ---- 77 .../lite/src/litert/cxx_api/graph/graph.cc | 9 - 78 .../lite/src/litert/cxx_api/graph/net_data.cc | 21 - 79 .../lite/src/litert/cxx_api/graph/net_data.h | 35 -- 80 .../lite/src/litert/cxx_api/model/model.cc | 1 - 81 .../src/litert/cxx_api/model/model_impl.cc | 8 - 82 .../src/litert/cxx_api/model/model_impl.h | 4 - 83 .../lite/src/litert/cxx_api/serialization.cc | 39 +- 84 .../lite/src/litert/cxx_api/train/model.cc | 1 - 85 .../src/litert/cxx_api/train/model_build.cc | 28 -- 86 .../litert/cxx_api/train/model_build_impl.cc | 28 -- 87 .../src/litert/cxx_api/train/model_impl.cc | 1 - 88 mindspore/lite/src/train/graph_fusion.cc | 7 + 89 .../fusion/remove_redundant_tensor.cc | 89 +++++ 90 .../fusion/remove_redundant_tensor.h} | 25 +- 91 mindspore/lite/src/train/train_session.cc | 90 ++--- 92 mindspore/lite/src/train/train_session.h | 3 - 93 .../test/config_level0/models_ms_train.cfg | 2 - 94 .../lite/tools/benchmark_train/CMakeLists.txt | 7 - 95 .../lite/tools/benchmark_train/net_runner.cc | 371 ------------------ 96 .../lite/tools/benchmark_train/net_runner.h | 81 ---- 97 .../lite/tools/benchmark_train/net_train.cc | 1 - 98 91 files changed, 162 insertions(+), 6776 deletions(-) 99 delete mode 100644 include/api/net.h 100 delete mode 100644 mindspore/lite/src/expression/cfg.h 101 delete mode 100644 mindspore/lite/src/expression/export.cc 102 delete mode 100644 mindspore/lite/src/expression/export.h 103 delete mode 100644 mindspore/lite/src/expression/expr.cc 104 delete mode 100644 mindspore/lite/src/expression/expr.h 105 delete mode 100644 mindspore/lite/src/expression/import.cc 106 delete mode 100644 mindspore/lite/src/expression/import.h 107 delete mode 100644 mindspore/lite/src/expression/net.cc 108 delete mode 100644 mindspore/lite/src/expression/net.h 109 delete mode 100644 mindspore/lite/src/expression/node.cc 110 delete mode 100644 mindspore/lite/src/expression/node.h 111 delete mode 100644 mindspore/lite/src/expression/ops.cc 112 delete mode 100644 mindspore/lite/src/expression/ops.h 113 delete mode 100644 mindspore/lite/src/expression/ops/activation.cc 114 delete mode 100644 mindspore/lite/src/expression/ops/activation.h 115 delete mode 100644 mindspore/lite/src/expression/ops/adam.cc 116 delete mode 100644 mindspore/lite/src/expression/ops/adam.h 117 delete mode 100644 mindspore/lite/src/expression/ops/addn.cc 118 delete mode 100644 mindspore/lite/src/expression/ops/addn.h 119 delete mode 100644 mindspore/lite/src/expression/ops/arithmetic.cc 120 delete mode 100644 mindspore/lite/src/expression/ops/arithmetic.h 121 delete mode 100644 mindspore/lite/src/expression/ops/arithmetic_self.cc 122 delete mode 100644 mindspore/lite/src/expression/ops/arithmetic_self.h 123 delete mode 100644 mindspore/lite/src/expression/ops/assign.cc 124 delete mode 100644 mindspore/lite/src/expression/ops/assign.h 125 delete mode 100644 mindspore/lite/src/expression/ops/batchnorm.cc 126 delete mode 100644 mindspore/lite/src/expression/ops/batchnorm.h 127 delete mode 100644 mindspore/lite/src/expression/ops/biasadd.cc 128 delete mode 100644 mindspore/lite/src/expression/ops/biasadd.h 129 delete mode 100644 mindspore/lite/src/expression/ops/conv.cc 130 delete mode 100644 mindspore/lite/src/expression/ops/conv.h 131 delete mode 100644 mindspore/lite/src/expression/ops/dense.cc 132 delete mode 100644 mindspore/lite/src/expression/ops/dense.h 133 delete mode 100644 mindspore/lite/src/expression/ops/depend.cc 134 delete mode 100644 mindspore/lite/src/expression/ops/dropout.cc 135 delete mode 100644 mindspore/lite/src/expression/ops/dropout.h 136 delete mode 100644 mindspore/lite/src/expression/ops/flatten.cc 137 delete mode 100644 mindspore/lite/src/expression/ops/flatten.h 138 delete mode 100644 mindspore/lite/src/expression/ops/pooling.cc 139 delete mode 100644 mindspore/lite/src/expression/ops/pooling.h 140 delete mode 100644 mindspore/lite/src/expression/ops/reduce.cc 141 delete mode 100644 mindspore/lite/src/expression/ops/reduce.h 142 delete mode 100644 mindspore/lite/src/expression/ops/reshape.cc 143 delete mode 100644 mindspore/lite/src/expression/ops/reshape.h 144 delete mode 100644 mindspore/lite/src/expression/ops/softmax.cc 145 delete mode 100644 mindspore/lite/src/expression/ops/softmax.h 146 delete mode 100644 mindspore/lite/src/expression/ops/softmaxCE.cc 147 delete mode 100644 mindspore/lite/src/expression/ops/softmaxCE.h 148 delete mode 100644 mindspore/lite/src/expression/ops/tile.cc 149 delete mode 100644 mindspore/lite/src/expression/ops/tile.h 150 delete mode 100644 mindspore/lite/src/expression/ops/transpose.cc 151 delete mode 100644 mindspore/lite/src/expression/ops/transpose.h 152 delete mode 100644 mindspore/lite/src/expression/ops_utils.cc 153 delete mode 100644 mindspore/lite/src/expression/ops_utils.h 154 delete mode 100644 mindspore/lite/src/expression/param.cc 155 delete mode 100644 mindspore/lite/src/expression/param.h 156 delete mode 100644 mindspore/lite/src/expression/sequential.cc 157 delete mode 100644 mindspore/lite/src/expression/sequential.h 158 delete mode 100644 mindspore/lite/src/litert/cxx_api/expression/net.cc 159 delete mode 100644 mindspore/lite/src/litert/cxx_api/expression/net_impl.cc 160 delete mode 100644 mindspore/lite/src/litert/cxx_api/expression/net_impl.h 161 delete mode 100644 mindspore/lite/src/litert/cxx_api/expression/node_impl.cc 162 delete mode 100644 mindspore/lite/src/litert/cxx_api/expression/node_impl.h 163 delete mode 100644 mindspore/lite/src/litert/cxx_api/graph/net_data.cc 164 delete mode 100644 mindspore/lite/src/litert/cxx_api/graph/net_data.h 165 create mode 100644 mindspore/lite/src/train/optimizer/fusion/remove_redundant_tensor.cc 166 rename mindspore/lite/src/{expression/ops/depend.h => train/optimizer/fusion/remove_redundant_tensor.h} (55%) 167 delete mode 100644 mindspore/lite/tools/benchmark_train/net_runner.cc 168 delete mode 100644 mindspore/lite/tools/benchmark_train/net_runner.h 169 170diff --git a/include/api/graph.h b/include/api/graph.h 171index 05548890..f25a6217 100644 172--- a/include/api/graph.h 173+++ b/include/api/graph.h 174@@ -24,38 +24,23 @@ 175 #include "include/api/types.h" 176 177 namespace mindspore { 178-class NetData; 179-class Net; 180- 181 class MS_API Graph { 182 public: 183 class GraphData; 184- enum Type : uint32_t { 185- kExpressionGraph = 0, ///< graph as expression - can auto grad 186- kExecutableGraph = 1, ///< graph is loaded as is 187- kUnknownTypeGraph = 0xffffffff 188- }; 189 Graph(); 190 explicit Graph(const std::shared_ptr<GraphData> &graph_data); 191 explicit Graph(std::shared_ptr<GraphData> &&graph_data); 192 explicit Graph(std::nullptr_t); 193 ~Graph(); 194- explicit Graph(Type executable); 195- explicit Graph(Net *net); 196 197 enum ModelType ModelType() const; 198 bool operator==(std::nullptr_t) const; 199 bool operator!=(std::nullptr_t) const; 200- bool IsExecutable() { return graph_type_ == kExecutableGraph; } 201 202 private: 203 friend class GraphCell; 204 friend class ModelImpl; 205- friend class NetImpl; 206- friend class Model; 207 std::shared_ptr<GraphData> graph_data_; 208- std::shared_ptr<NetData> net_data_; 209- Type graph_type_ = kExecutableGraph; 210 }; 211 } // namespace mindspore 212 #endif // MINDSPORE_INCLUDE_API_GRAPH_H 213diff --git a/include/api/model.h b/include/api/model.h 214index 4c4359f4..64c52bb1 100644 215--- a/include/api/model.h 216+++ b/include/api/model.h 217@@ -33,9 +33,6 @@ 218 namespace mindspore { 219 class ModelImpl; 220 class Metrics; 221-class Net; 222-class Node; 223-class Expr; 224 225 namespace dataset { 226 class Dataset; 227@@ -112,17 +109,6 @@ class MS_API Model { 228 Status Build(GraphCell graph, const std::shared_ptr<Context> &model_context = nullptr, 229 const std::shared_ptr<TrainCfg> &train_cfg = nullptr); 230 231- /// \brief Build train model 232- /// 233- /// \param[in] graph A forward network 234- /// \param[in] optimizer An optimizer node 235- /// \param[in] inputs Inputs expression for the trained network (ex: input, label ) 236- /// \param[in] model_context A context used to store options during execution. 237- /// \param[in] train_cfg A config used by training 238- /// \return Status 239- Status Build(GraphCell graph, Node *optimizer, std::vector<Expr *> inputs, 240- const std::shared_ptr<Context> &model_context, const std::shared_ptr<TrainCfg> &train_cfg); 241- 242 /// \brief Build a Transfer Learning model where the backbone weights are fixed and the head weights are trainable 243 /// 244 /// \param[in] backbone The static, non-learnable part of the graph 245diff --git a/include/api/net.h b/include/api/net.h 246deleted file mode 100644 247index 61990ae0..00000000 248--- a/include/api/net.h 249+++ /dev/null 250@@ -1,142 +0,0 @@ 251-/** 252- * Copyright 2022-2023 Huawei Technologies Co., Ltd 253- * 254- * Licensed under the Apache License, Version 2.0 (the "License"); 255- * you may not use this file except in compliance with the License. 256- * You may obtain a copy of the License at 257- * 258- * http://www.apache.org/licenses/LICENSE-2.0 259- * 260- * Unless required by applicable law or agreed to in writing, software 261- * distributed under the License is distributed on an "AS IS" BASIS, 262- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 263- * See the License for the specific language governing permissions and 264- * limitations under the License. 265- */ 266- 267-#ifndef MINDSPORE_INCLUDE_API_NET_H 268-#define MINDSPORE_INCLUDE_API_NET_H 269- 270-#include <memory> 271-#include <vector> 272-#include <unordered_set> 273-#include <string> 274-#include "include/api/types.h" 275-#include "include/api/data_type.h" 276-#include "include/api/cfg.h" 277- 278-namespace mindspore { 279-/// \brief Register node or sub network 280-#define REG(_name) Register(_name, #_name) 281- 282-class Expr; 283-class NodeImpl; 284-class NetImpl; 285-class NodeSet; 286-class Graph; 287-class NetData; 288- 289-class MS_API NetBase { 290- public: 291- NetBase() = default; 292- virtual std::vector<Expr *> operator()(const std::vector<Expr *> &inputs) = 0; 293- virtual uint32_t type() = 0; 294-}; 295- 296-class MS_API Node : public NetBase { 297- public: 298- Node(); 299- virtual ~Node(); 300- /// \brief Create output expression from node 301- 302- /// \param[in] name Name of input (like "labels" etc.) 303- /// 304- /// \return Expression 305- Expr *Create(std::string name); 306- /// \brief Run node on inputs. This operator is used in Net::construct() 307- /// 308- /// \param[in] inputs Inputs expression for the node. 309- /// \return Output node expression vector 310- std::vector<Expr *> operator()(const std::vector<Expr *> &inputs) override; 311- uint32_t type() final; 312- 313- private: 314- friend NodeImpl; 315- std::shared_ptr<NodeImpl> impl_ = nullptr; 316-}; 317- 318-class MS_API Net : public NetBase, public std::enable_shared_from_this<Net> { 319- public: 320- Net(); 321- virtual ~Net(); 322- explicit Net(std::string name); 323- explicit Net(const Graph &g); 324- /// \brief Define the relation between network inputs and outputs 325- /// 326- /// \param[in] inputs expression vector 327- /// 328- /// \return expression vector 329- 330- virtual std::vector<Expr *> construct(const std::vector<Expr *> &inputs); 331- /// \brief Addition operation 332- /// 333- /// \param[in] inputs Two elements to add 334- /// 335- /// \return expression vector (single element) 336- 337- /// \brief Execution operator. Connect inputs to outputs via user defined construct 338- /// 339- /// \return expression vector 340- 341- std::vector<Expr *> operator()(const std::vector<Expr *> &inputs); 342- void Register(Net *net, std::string &&name); 343- void Register(Node *node, std::string &&name); 344- /// \brief Find the trainable params for the trained network 345- /// 346- /// \return NodeSet for all trainable nodes 347- std::shared_ptr<NodeSet> trainable_params(); 348- virtual void Add(NetBase *element); 349- /// \brief Input shape 350- /// 351- /// \param[in] idx input index 352- /// 353- /// \return Specific input shape vector 354- const std::vector<int> InputShape(int idx); 355- /// \brief Output shape 356- /// 357- /// \param[in] idx Output index 358- /// 359- /// \return Specific output shape vector 360- const std::vector<int> OutputShape(int idx); 361- uint32_t type() final; 362- 363- private: 364- friend NetImpl; 365- friend NetData; 366- std::shared_ptr<NetImpl> impl_; 367-}; 368- 369-class MS_API SoftMaxCrossEntropyCfg { 370- public: 371- std::string reduction = "mean"; /**< Specifies reduction mode. The optional values are "none", "mean", "sum" */ 372-}; 373- 374-class MS_API AdamConfig { 375- public: 376- float learning_rate_ = 1e-3; 377- float beta1_ = 0.9; 378- float beta2_ = 0.999; 379- float eps_ = 1e-08; 380- bool use_nesterov_ = false; 381-}; 382- 383-namespace NN { 384-MS_API Net *NetWithLoss(Net *net, Node *loss); 385-MS_API Graph *GraphWithLoss(Graph *g, Node *loss); 386-MS_API Node *Adam(std::shared_ptr<NodeSet> learn, const AdamConfig &cfg); 387-MS_API Node *SoftmaxCrossEntropy(const SoftMaxCrossEntropyCfg &cfg); 388-MS_API std::unique_ptr<Node> Input(std::vector<int> dims, DataType data_type = DataType::kNumberTypeFloat32, 389- int fmt = NHWC); 390-}; // namespace NN 391-} // namespace mindspore 392-#endif // MINDSPORE_INCLUDE_API_NET_H 393diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/infer/reshape_infer.c b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/infer/reshape_infer.c 394index 3c192df7..37aaa410 100644 395--- a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/infer/reshape_infer.c 396+++ b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/infer/reshape_infer.c 397@@ -182,7 +182,7 @@ int ReshapeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC 398 size_t out_shape_size = 0; 399 if (inputs_size == 2) { 400 const TensorC *shape_tensor = inputs[1]; 401- if (GetElementNum(input) == 1 && input->shape_size_ == 0) { 402+ if (GetElementNum(input) == 1) { 403 if (shape_tensor->data_ == NULL || (shape_tensor->shape_size_ == 1 && shape_tensor->shape_[0] == 0)) { 404 SetShapeArray(output, out_shape, out_shape_size); 405 return NNACL_OK; 406diff --git a/mindspore/lite/BUILD.gn b/mindspore/lite/BUILD.gn 407index 9318d54e..d7fe4f55 100644 408--- a/mindspore/lite/BUILD.gn 409+++ b/mindspore/lite/BUILD.gn 410@@ -99,7 +99,6 @@ cxx_api_sources = [ 411 "src/litert/cxx_api/model/model_group_impl.cc", 412 "src/litert/cxx_api/model/model_impl.cc", 413 "src/litert/cxx_api/graph/graph.cc", 414- "src/litert/cxx_api/graph/net_data.cc", 415 "src/litert/cxx_api/tensor/tensor_impl.cc", 416 ] 417 418@@ -532,50 +531,6 @@ ohos_shared_library("mindspore_ndk") { 419 } 420 421 # Train library 422-expression_cxx_api_sources = [ 423- "src/litert/cxx_api/expression/net.cc", 424- "src/litert/cxx_api/expression/net_impl.cc", 425- "src/litert/cxx_api/expression/node_impl.cc", 426-] 427- 428-expression_op_sources = [ 429- "src/expression/ops/activation.cc", 430- "src/expression/ops/adam.cc", 431- "src/expression/ops/addn.cc", 432- "src/expression/ops/arithmetic.cc", 433- "src/expression/ops/arithmetic_self.cc", 434- "src/expression/ops/assign.cc", 435- "src/expression/ops/batchnorm.cc", 436- "src/expression/ops/biasadd.cc", 437- "src/expression/ops/conv.cc", 438- "src/expression/ops/dense.cc", 439- "src/expression/ops/depend.cc", 440- "src/expression/ops/dropout.cc", 441- "src/expression/ops/flatten.cc", 442- "src/expression/ops/pooling.cc", 443- "src/expression/ops/reduce.cc", 444- "src/expression/ops/reshape.cc", 445- "src/expression/ops/softmax.cc", 446- "src/expression/ops/softmaxCE.cc", 447- "src/expression/ops/tile.cc", 448- "src/expression/ops/transpose.cc", 449-] 450- 451-all_expression_sources = [ 452- "src/expression/export.cc", 453- "src/expression/expr.cc", 454- "src/expression/import.cc", 455- "src/expression/net.cc", 456- "src/expression/node.cc", 457- "src/expression/ops.cc", 458- "src/expression/ops_utils.cc", 459- "src/expression/param.cc", 460- "src/expression/sequential.cc", 461-] 462- 463-all_expression_sources += expression_cxx_api_sources 464-all_expression_sources += expression_op_sources 465- 466 all_train_sources = [ 467 # ${API_TRAIN_SRC} is empty. 468 # ${TRAIN_SRC_WITH_MD} is empty. 469@@ -604,6 +559,7 @@ all_train_sources = [ 470 "src/train/optimizer/fusion/gru_fusion_pass.cc", 471 "src/train/optimizer/fusion/matmul_add_fusion_pass.cc", 472 "src/train/optimizer/fusion/matmul_matmul_add_fusion_pass.cc", 473+ "src/train/optimizer/fusion/remove_redundant_tensor.cc", 474 "src/common/storage.cc", 475 "tools/converter/optimizer.cc", 476 "tools/converter/legacy_optimizer/fusion/fusion_pass.cc", 477@@ -616,8 +572,6 @@ all_train_sources = [ 478 "tools/converter/legacy_optimizer/graph/subgraph_node_pass.cc", 479 ] 480 481-all_train_sources += all_expression_sources 482- 483 fp16_train_kernel_sources = [ 484 "src/litert/kernel/cpu/fp16_grad/activation_fp16_grad.cc", 485 "src/litert/kernel/cpu/fp16_grad/arithmetic_fp16_grad.cc", 486diff --git a/mindspore/lite/src/CMakeLists.txt b/mindspore/lite/src/CMakeLists.txt 487index 469bcb6b..47033473 100644 488--- a/mindspore/lite/src/CMakeLists.txt 489+++ b/mindspore/lite/src/CMakeLists.txt 490@@ -289,33 +289,9 @@ if(MSLITE_MINDDATA_IMPLEMENT STREQUAL "full") 491 ) 492 endif() 493 494- 495-file(GLOB CXX_API_EXPRESSION 496- ${CMAKE_CURRENT_SOURCE_DIR}/litert/cxx_api/expression/*.cc 497- ) 498- 499-file(GLOB EXPRESSION_OPS 500- ${CMAKE_CURRENT_SOURCE_DIR}/expression/ops/*.cc 501- ) 502- 503-set(EXPRESSION_SRC 504- ${CXX_API_EXPRESSION} 505- ${CMAKE_CURRENT_SOURCE_DIR}/expression/export.cc 506- ${CMAKE_CURRENT_SOURCE_DIR}/expression/expr.cc 507- ${CMAKE_CURRENT_SOURCE_DIR}/expression/import.cc 508- ${CMAKE_CURRENT_SOURCE_DIR}/expression/net.cc 509- ${CMAKE_CURRENT_SOURCE_DIR}/expression/node.cc 510- ${CMAKE_CURRENT_SOURCE_DIR}/expression/ops.cc 511- ${CMAKE_CURRENT_SOURCE_DIR}/expression/ops_utils.cc 512- ${CMAKE_CURRENT_SOURCE_DIR}/expression/param.cc 513- ${CMAKE_CURRENT_SOURCE_DIR}/expression/sequential.cc 514- ${EXPRESSION_OPS} 515- ) 516- 517 set(TRAIN_SRC 518 ${API_TRAIN_SRC} 519 ${TRAIN_SRC_WITH_MD} 520- ${EXPRESSION_SRC} 521 ${CMAKE_CURRENT_SOURCE_DIR}/litert/cxx_api/metrics/accuracy.cc 522 ${CMAKE_CURRENT_SOURCE_DIR}/litert/cxx_api/train/model_build.cc 523 ${CMAKE_CURRENT_SOURCE_DIR}/litert/cxx_api/train/model_build_impl.cc 524@@ -340,6 +316,7 @@ set(TRAIN_SRC 525 ${CMAKE_CURRENT_SOURCE_DIR}/train/optimizer/fusion/matmul_add_fusion_pass.cc 526 ${CMAKE_CURRENT_SOURCE_DIR}/train/optimizer/fusion/matmul_matmul_add_fusion_pass.cc 527 ${CMAKE_CURRENT_SOURCE_DIR}/train/optimizer/fusion/reshape_gather_reshape_fusion_pass.cc 528+ ${CMAKE_CURRENT_SOURCE_DIR}/train/optimizer/fusion/remove_redundant_tensor.cc 529 ${TOOLS_DIR}/converter/optimizer.cc 530 ${TOOLS_DIR}/converter/legacy_optimizer/fusion/fusion_pass.cc 531 ${TOOLS_DIR}/converter/legacy_optimizer/fusion/fusion_pattern.cc 532@@ -399,13 +376,6 @@ if(NOT MSLITE_ENABLE_COREML) 533 ${CMAKE_CURRENT_SOURCE_DIR}/litert/delegate/coreml/stub/coreml_delegate_stub.cc) 534 endif() 535 536-if(MSVC) 537- set(LITE_SRC 538- ${LITE_SRC} 539- ${EXPRESSION_SRC} 540- ) 541-endif() 542- 543 add_subdirectory(litert/kernel/cpu) 544 add_subdirectory(common) 545 546diff --git a/mindspore/lite/src/common/ops/populate/custom_populate.cc b/mindspore/lite/src/common/ops/populate/custom_populate.cc 547index 6c490130..9933f6ab 100644 548--- a/mindspore/lite/src/common/ops/populate/custom_populate.cc 549+++ b/mindspore/lite/src/common/ops/populate/custom_populate.cc 550@@ -23,6 +23,7 @@ 551 #include "nnacl/custom_is_inf_parameter.h" 552 #include "nnacl/custom_tensor_scatter_max_parameter.h" 553 #include "nnacl/custom_gather_d_grad_v2_parameter.h" 554+#include "nnacl/scatter_nd_parameter.h" 555 using mindspore::schema::PrimitiveType_Custom; 556 557 namespace mindspore { 558@@ -108,13 +109,13 @@ OpParameter *CreateCustomIsInfParameter() { 559 } 560 561 OpParameter *CreateCustomTensorScatterMaxParameter() { 562- auto *param = static_cast<CustomTensorScatterMaxParameter *>(malloc(sizeof(CustomTensorScatterMaxParameter))); 563+ auto *param = static_cast<ScatterNDParameter *>(malloc(sizeof(ScatterNDParameter))); 564 if (param == nullptr) { 565- MS_LOG(ERROR) << "malloc CustomTensorScatterMaxParameter failed."; 566+ MS_LOG(ERROR) << "malloc ScatterNDParameter failed."; 567 return nullptr; 568 } 569- memset(param, 0, sizeof(CustomTensorScatterMaxParameter)); 570- param->op_parameter_.type_ = PrimType_Inner_CustomTensorScatterMax; 571+ memset(param, 0, sizeof(ScatterNDParameter)); 572+ param->op_parameter.type_ = PrimType_Inner_CustomTensorScatterMax; 573 return reinterpret_cast<OpParameter *>(param); 574 } 575 576diff --git a/mindspore/lite/src/expression/cfg.h b/mindspore/lite/src/expression/cfg.h 577deleted file mode 100644 578index e590d2b7..00000000 579--- a/mindspore/lite/src/expression/cfg.h 580+++ /dev/null 581@@ -1,68 +0,0 @@ 582-/** 583- * Copyright 2022 Huawei Technologies Co., Ltd 584- * 585- * Licensed under the Apache License, Version 2.0 (the "License"); 586- * you may not use this file except in compliance with the License. 587- * You may obtain a copy of the License at 588- * 589- * http://www.apache.org/licenses/LICENSE-2.0 590- * 591- * Unless required by applicable law or agreed to in writing, software 592- * distributed under the License is distributed on an "AS IS" BASIS, 593- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 594- * See the License for the specific language governing permissions and 595- * limitations under the License. 596- */ 597- 598-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_CFG_H_ 599-#define MINDSPORE_LITE_SRC_EXPRESSION_CFG_H_ 600- 601-#include <vector> 602-#include <string> 603- 604-namespace mindspore { 605-namespace lite { 606-class ConvConfig { 607- public: 608- ConvConfig() = default; 609- int in_channel_ = 3; /**< The channel number of the input of the Conv2d layer */ 610- int out_channel_ = 3; /**< The channel number of the output tensor of the Conv2d layer */ 611- std::vector<int64_t> kernel_size_ = {3, 3}; /**< Specifies the height and width of the 2D convolution kernel. */ 612- std::vector<int64_t> stride_ = {1, 1}; /**< The movement stride of the 2D convolution kernel */ 613- std::vector<int64_t> padding_ = {0, 0, 0, 0}; /**< The top, bottom, left, and right padding input */ 614- std::vector<int64_t> dilation_ = {1, 1}; /**< diletion height and width*/ 615- int group_ = 1; // < Splits filter into groups, `in_channels` and `out_channels` must be 616- // divisible by `group`. If the group is equal to `in_channels` and `out_channels`, 617- // this 2D convolution layer also can be called 2D depthwise convolution layer */ 618- bool has_bias = false; /** < Whether the Conv2d layer has a bias parameter */ 619- std::string weight_init_ = 620- "normal"; /**< Initialization method of weight parameter ("normal","uniform", "ones", "zeros") */ 621- std::string pad_mode_ = "same"; /**< Specifies padding mode. The optional values are "same", "valid", "pad" */ 622- 623- private: 624- std::string bias_init_ = "zeros"; 625- std::string data_format; 626-}; 627- 628-class DenseConfig { 629- public: 630- int in_channels_; /**< The number of channels in the input space */ 631- int out_channels_; /**< The number of channels in the output space */ 632- bool has_bias_ = false; /** Specifies whether the layer uses a bias vector **/ 633- private: 634- std::string weight_init_ = "normal"; 635- std::string bias_init_ = "zeros"; 636- std::string activation_ = "none"; 637-}; 638- 639-class PoolingConfig { 640- public: 641- PoolingConfig() = default; 642- std::vector<int64_t> kernel_size_ = {1, 1}; /**< Specifies the height and width of the 2D kernel. */ 643- std::vector<int64_t> stride_ = {1, 1}; /**< The movement stride of the 2D kernel */ 644- std::string pad_mode_ = "same"; /**< Specifies padding mode. The optional values are "same", "valid" */ 645-}; 646-} // namespace lite 647-} // namespace mindspore 648- 649-#endif // MINDSPORE_LITE_SRC_EXPRESSION_CFG_H_ 650diff --git a/mindspore/lite/src/expression/export.cc b/mindspore/lite/src/expression/export.cc 651deleted file mode 100644 652index a86c54a6..00000000 653--- a/mindspore/lite/src/expression/export.cc 654+++ /dev/null 655@@ -1,76 +0,0 @@ 656-/** 657- * Copyright 2022 Huawei Technologies Co., Ltd 658- * 659- * Licensed under the Apache License, Version 2.0 (the "License"); 660- * you may not use this file except in compliance with the License. 661- * You may obtain a copy of the License at 662- * 663- * http://www.apache.org/licenses/LICENSE-2.0 664- * 665- * Unless required by applicable law or agreed to in writing, software 666- * distributed under the License is distributed on an "AS IS" BASIS, 667- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 668- * See the License for the specific language governing permissions and 669- * limitations under the License. 670- */ 671- 672-#include <utility> 673-#include "src/expression/export.h" 674-#include "src/expression/ops.h" 675-#include "src/common/utils.h" 676-#include "nnacl/conv_parameter.h" 677-#include "include/errorcode.h" 678- 679-namespace mindspore { 680-namespace lite { 681-constexpr static int kFmkVal = 3; 682- 683-int ExportSession::Init(const std::string model_name, std::string version) { 684- meta_graph_ = new (std::nothrow) mindspore::schema::MetaGraphT(); 685- if (meta_graph_ == nullptr) { 686- MS_LOG(ERROR) << "cannot allocate meta_graph"; 687- return RET_ERROR; 688- } 689- meta_graph_->fmkType = kFmkVal; 690- meta_graph_->name = model_name; 691- meta_graph_->version = version; 692- return RET_OK; 693-} 694- 695-bool ExportSession::IsToDependOnly(EXPR *expr) { 696- auto itr = outmap_.find(expr); 697- if (itr != outmap_.end() && !itr->second.empty()) { 698- for (auto expr : itr->second) { 699- auto node = expr->node(); 700- if (node->primitive() != schema::PrimitiveType_Depend) return false; 701- } 702- return true; 703- } 704- return false; 705-} 706- 707-int ExportSession::SetInputOutput(const std::vector<EXPR *> &inputs, const std::vector<EXPR *> &outputs) { 708- for (auto &in : inputs) { 709- auto id = GetOutput(in); 710- meta_graph_->inputIndex.push_back(id); 711- } 712- for (auto &out : outputs) { 713- auto id = GetOutput(out); 714- meta_graph_->outputIndex.push_back(id); 715- } 716- auto sub_graph = std::make_unique<mindspore::schema::SubGraphT>(); 717- if (sub_graph == nullptr) { 718- MS_LOG(ERROR) << "cannot allocate SubGraphT"; 719- return RET_ERROR; 720- } 721- auto model_name = meta_graph_->name; 722- sub_graph->name = model_name + "_subgraph"; 723- sub_graph->inputIndices = meta_graph_->inputIndex; 724- sub_graph->outputIndices = meta_graph_->outputIndex; 725- for (size_t i = 0; i < meta_graph_->nodes.size(); i++) sub_graph->nodeIndices.push_back(i); 726- for (size_t i = 0; i < meta_graph_->allTensors.size(); i++) sub_graph->tensorIndices.push_back(i); 727- meta_graph_->subGraph.emplace_back(std::move(sub_graph)); 728- return RET_OK; 729-} 730-} // namespace lite 731-} // namespace mindspore 732diff --git a/mindspore/lite/src/expression/export.h b/mindspore/lite/src/expression/export.h 733deleted file mode 100644 734index 8009e080..00000000 735--- a/mindspore/lite/src/expression/export.h 736+++ /dev/null 737@@ -1,52 +0,0 @@ 738-/** 739- * Copyright 2022 Huawei Technologies Co., Ltd 740- * 741- * Licensed under the Apache License, Version 2.0 (the "License"); 742- * you may not use this file except in compliance with the License. 743- * You may obtain a copy of the License at 744- * 745- * http://www.apache.org/licenses/LICENSE-2.0 746- * 747- * Unless required by applicable law or agreed to in writing, software 748- * distributed under the License is distributed on an "AS IS" BASIS, 749- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 750- * See the License for the specific language governing permissions and 751- * limitations under the License. 752- */ 753- 754-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_EXPORT_H_ 755-#define MINDSPORE_LITE_SRC_EXPRESSION_EXPORT_H_ 756- 757-#include <vector> 758-#include <memory> 759-#include <unordered_map> 760-#include <string> 761-#include <list> 762-#include <map> 763-#include <iostream> 764-#include "src/expression/expr.h" 765- 766-namespace mindspore { 767-namespace schema { 768-struct MetaGraphT; 769-} 770-namespace lite { 771-class ExportSession { 772- public: 773- explicit ExportSession(std::map<EXPR *, std::list<EXPR *>> &outmap) : outmap_(outmap) {} 774- int Init(const std::string model_name, std::string version); 775- void UpdateOutput(EXPR *expr, int id) { output_tensors_[expr] = id; } 776- int GetOutput(EXPR *expr) { return output_tensors_.at(expr); } 777- schema::MetaGraphT *&meta_graph() { return meta_graph_; } 778- int SetInputOutput(const std::vector<EXPR *> &inputs, const std::vector<EXPR *> &outputs); 779- bool IsToDependOnly(EXPR *expr); 780- 781- private: 782- schema::MetaGraphT *meta_graph_{nullptr}; 783- std::unordered_map<EXPR *, int> output_tensors_; // output tensors per EXPR 784- std::map<EXPR *, std::list<EXPR *>> &outmap_; 785-}; 786-} // namespace lite 787-} // namespace mindspore 788- 789-#endif // MINDSPORE_LITE_SRC_EXPRESSION_EXPORT_H_ 790diff --git a/mindspore/lite/src/expression/expr.cc b/mindspore/lite/src/expression/expr.cc 791deleted file mode 100644 792index b27853d2..00000000 793--- a/mindspore/lite/src/expression/expr.cc 794+++ /dev/null 795@@ -1,98 +0,0 @@ 796-/** 797- * Copyright 2022 Huawei Technologies Co., Ltd 798- * 799- * Licensed under the Apache License, Version 2.0 (the "License"); 800- * you may not use this file except in compliance with the License. 801- * You may obtain a copy of the License at 802- * 803- * http://www.apache.org/licenses/LICENSE-2.0 804- * 805- * Unless required by applicable law or agreed to in writing, software 806- * distributed under the License is distributed on an "AS IS" BASIS, 807- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 808- * See the License for the specific language governing permissions and 809- * limitations under the License. 810- */ 811- 812-#include <string> 813-#include <iostream> 814-#include "src/expression/expr.h" 815-#include "src/expression/node.h" 816- 817-namespace mindspore { 818-namespace lite { 819-std::string EXPR::name() { return node_->name(); } 820-void EXPR::Travers(std::function<bool(EXPR *e, EXPR *itr)> cb) { 821- if (!visited) { 822- visited = true; 823- for (auto &itr : params_) { 824- if (cb(this, itr)) { 825- itr->Travers(cb); 826- } 827- } 828- } 829-} 830- 831-void EXPR::Replace(EXPR **old, EXPR **n, std::vector<Node *> *to_delete) { 832- if (!visited) { 833- visited = true; 834- for (auto &itr : params_) 835- if (itr == *old) { 836- to_delete->push_back(itr->node()); 837- itr = *n; 838- } 839- for (auto &itr : params_) itr->Replace(old, n, to_delete); 840- } 841-} 842- 843-void EXPR::Replace(const std::vector<EXPR *> &vec, std::vector<EXPR *> *old, std::vector<EXPR *> *n) { 844- std::vector<Node *> to_delete; 845- for (auto &e : vec) { 846- for (std::size_t i = 0; i < old->size(); i++) { 847- e->Replace(&old->at(i), &n->at(i), &to_delete); 848- } 849- } 850- for (auto &itr : to_delete) delete itr; 851- for (auto e : vec) e->Clear(); 852-} 853- 854-void EXPR::Clear() { 855- EXPR *item = this; 856- if (visited == false) return; 857- visited = false; 858- while (item->params_.size() == 1) { 859- item = item->params_.front(); 860- if (item->visited == false) return; 861- item->visited = false; 862- } 863- for (auto &itr : item->params_) itr->Clear(); 864-} 865- 866-void EXPR::Clear(std::vector<EXPR *> vec) { 867- for (auto e : vec) e->Clear(); 868-} 869- 870-void EXPR::CreateOutputMap(std::vector<EXPR *> vec, std::map<EXPR *, std::list<EXPR *>> *outmap) { 871- for (auto e : vec) { 872- e->Travers([&](EXPR *e, EXPR *itr) { 873- (*outmap)[itr].push_back(e); 874- return true; 875- }); 876- } 877- Clear(vec); 878-} 879- 880-void EXPR::PrintDot(std::vector<EXPR *> vec) { 881- std::cout << "digraph \"expr\" { " << std::endl; 882- for (auto e : vec) { 883- e->Travers([](EXPR *e, EXPR *itr) { 884- std::cout << "\"" << itr->node_->name() << "\"->" 885- << "\"" << e->node_->name() << "\"" << std::endl; 886- return true; 887- }); 888- } 889- std::cout << "}" << std::endl; 890- Clear(vec); 891-} 892-} // namespace lite 893-} // namespace mindspore 894diff --git a/mindspore/lite/src/expression/expr.h b/mindspore/lite/src/expression/expr.h 895deleted file mode 100644 896index 8c76befd..00000000 897--- a/mindspore/lite/src/expression/expr.h 898+++ /dev/null 899@@ -1,70 +0,0 @@ 900-/** 901- * Copyright 2022 Huawei Technologies Co., Ltd 902- * 903- * Licensed under the Apache License, Version 2.0 (the "License"); 904- * you may not use this file except in compliance with the License. 905- * You may obtain a copy of the License at 906- * 907- * http://www.apache.org/licenses/LICENSE-2.0 908- * 909- * Unless required by applicable law or agreed to in writing, software 910- * distributed under the License is distributed on an "AS IS" BASIS, 911- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 912- * See the License for the specific language governing permissions and 913- * limitations under the License. 914- */ 915- 916-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_EXPR_H_ 917-#define MINDSPORE_LITE_SRC_EXPRESSION_EXPR_H_ 918- 919-#include <vector> 920-#include <list> 921-#include <memory> 922-#include <map> 923-#include <functional> 924-#include <string> 925-#include "include/api/format.h" 926-#include "mindapi/base/type_id.h" 927- 928-namespace mindspore { 929-namespace lite { 930-class Node; 931- 932-class EXPR { 933- public: 934- explicit EXPR(Node *node) : node_(node) { SetSize(1); } 935- static void PrintDot(std::vector<EXPR *> vec); 936- static void Replace(const std::vector<EXPR *> &vec, std::vector<EXPR *> *old, std::vector<EXPR *> *n); 937- static void CreateOutputMap(std::vector<EXPR *> vec, std::map<EXPR *, std::list<EXPR *>> *outmap); 938- static void Clear(std::vector<EXPR *> vec); 939- void Travers(std::function<bool(EXPR *e, EXPR *itr)> cb); 940- std::string name(); 941- EXPR *GetInput(int idx) { return params_.at(idx); } 942- void set_node(Node *node) { node_ = node; } 943- Node *node() { return node_; } 944- bool visited = false; 945- void set_params(std::vector<EXPR *> params) { params_ = params; } 946- void set_params(int idx, EXPR *expr) { params_[idx] = expr; } 947- void add_params(EXPR *e) { params_.push_back(e); } 948- std::vector<EXPR *> ¶ms() { return params_; } 949- EXPR *params(int i) { return params_[i]; } 950- void SetSize(int n) { params_.resize(n); } 951- void SetDims(std::vector<int> dims) { dims_ = dims; } 952- std::vector<int> &dims() { return dims_; } 953- void set_format(int fmt) { format_ = fmt; } 954- int format() { return format_; } 955- void set_data_type(TypeId data_type) { data_type_ = data_type; } 956- TypeId data_type() { return data_type_; } 957- 958- private: 959- void Replace(EXPR **old, EXPR **n, std::vector<Node *> *to_delete); 960- std::vector<EXPR *> params_; 961- Node *node_{nullptr}; 962- void Clear(); 963- std::vector<int> dims_; 964- int format_ = NHWC; 965- TypeId data_type_ = kNumberTypeFloat32; 966-}; 967-} // namespace lite 968-} // namespace mindspore 969-#endif // MINDSPORE_LITE_SRC_EXPRESSION_EXPR_H_ 970diff --git a/mindspore/lite/src/expression/import.cc b/mindspore/lite/src/expression/import.cc 971deleted file mode 100644 972index b3c20839..00000000 973--- a/mindspore/lite/src/expression/import.cc 974+++ /dev/null 975@@ -1,180 +0,0 @@ 976-/** 977- * Copyright 2022 Huawei Technologies Co., Ltd 978- * 979- * Licensed under the Apache License, Version 2.0 (the "License"); 980- * you may not use this file except in compliance with the License. 981- * You may obtain a copy of the License at 982- * 983- * http://www.apache.org/licenses/LICENSE-2.0 984- * 985- * Unless required by applicable law or agreed to in writing, software 986- * distributed under the License is distributed on an "AS IS" BASIS, 987- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 988- * See the License for the specific language governing permissions and 989- * limitations under the License. 990- */ 991- 992-#include <vector> 993-#include "src/expression/import.h" 994-#include "common/ops/populate/populate_register.h" 995-#include "src/expression/ops.h" 996-#include "src/expression/ops/activation.h" 997-#include "src/expression/ops/batchnorm.h" 998-#include "src/expression/ops/biasadd.h" 999-#include "src/expression/ops/conv.h" 1000-#include "src/expression/ops/dense.h" 1001-#include "src/expression/ops/pooling.h" 1002-#include "src/expression/ops/reshape.h" 1003-#include "src/expression/ops/transpose.h" 1004- 1005-namespace mindspore { 1006-namespace lite { 1007-std::unordered_map<mindspore::schema::PrimitiveType, import_func> ImportReg::import_map_; 1008- 1009-import_func ImportReg::GetImportFunc(mindspore::schema::PrimitiveType type) { 1010- auto f = import_map_.find(type); 1011- if (f == import_map_.end()) { 1012- return nullptr; 1013- } 1014- return f->second; 1015-} 1016- 1017-OpParameter *Import::GetAttr(const schema::Primitive *prim) { 1018- auto parameter_gen = PopulateRegistry::GetInstance()->GetParameterCreator(prim->value_type(), SCHEMA_CUR); 1019- if (parameter_gen == nullptr) { 1020- MS_LOG(ERROR) << "PopulateParameter return nullptr, type: " << schema::EnumNamePrimitiveType(prim->value_type()); 1021- return nullptr; 1022- } 1023- auto parameter = parameter_gen(prim); 1024- if (parameter == nullptr) { 1025- MS_LOG(ERROR) << "parameter is nullptr."; 1026- return nullptr; 1027- } 1028- return parameter; 1029-} 1030- 1031-std::unique_ptr<Node> Import::CreateNode(const schema::CNode *cnode) { 1032- auto param = GetAttr(cnode->primitive()); 1033- auto type = cnode->primitive()->value_type(); 1034- auto fn = ImportReg::GetImportFunc(type); 1035- if (fn == nullptr) { 1036- MS_LOG(ERROR) << "Cannot find importer for " << schema::EnumNamePrimitiveType(type); 1037- return nullptr; 1038- } 1039- auto node = fn(); 1040- if (node == nullptr) { 1041- MS_LOG(ERROR) << "Cannot allocate node" << cnode->name()->str(); 1042- return nullptr; 1043- } 1044- node->SetOpParam(param); 1045- node->set_name(cnode->name()->str()); 1046- node->set_primitive(type); 1047- return std::unique_ptr<Node>(node); 1048-} 1049- 1050-Net *Import::ImportMs(std::string file_name) { 1051- std::ifstream infile; 1052- infile.open(file_name, std::ios::binary | std::ios::in); 1053- if (!infile.good()) { 1054- MS_LOG(ERROR) << "cannot read " << file_name << std::endl; 1055- return nullptr; 1056- } 1057- infile.seekg(0, std::ios::end); 1058- int length = infile.tellg(); 1059- infile.seekg(0, std::ios::beg); 1060- auto data_ptr = std::make_unique<int8_t[]>(length); 1061- auto *data = data_ptr.get(); 1062- infile.read(reinterpret_cast<char *>(data), length); 1063- infile.close(); 1064- flatbuffers::Verifier verifier = flatbuffers::Verifier(reinterpret_cast<const uint8_t *>(data), length); 1065- bool res = schema::VerifyMetaGraphBuffer(verifier); 1066- if (res != true) { 1067- MS_LOG(ERROR) << "fault file: " << file_name << "(" << length << ")\n"; 1068- return nullptr; 1069- } else { 1070- MS_LOG(INFO) << "verify pass file: " << file_name << "(" << length << ")\n"; 1071- } 1072- buffer_ = data_ptr.get(); 1073- auto metaGraph = schema::GetMetaGraph(data_ptr.release()); 1074- return ImportMs(metaGraph); 1075-} 1076- 1077-Net *Import::ImportMs(const schema::MetaGraph *metaGraph) { 1078- if (metaGraph == nullptr) { 1079- MS_LOG(ERROR) << "null input"; 1080- return nullptr; 1081- } 1082- std::string NetName = "Network"; 1083- if (metaGraph->name() != nullptr) NetName = metaGraph->name()->str(); 1084- auto net = std::make_unique<Net>(NetName); 1085- std::unordered_map<int, EXPR *> outputs; 1086- // save inputs 1087- for (size_t i = 0; i < metaGraph->inputIndex()->size(); i++) { 1088- auto tensor_id = metaGraph->inputIndex()->Get(i); 1089- const schema::Tensor *tensor = metaGraph->allTensors()->Get(tensor_id); 1090- auto input = new (std::nothrow) InputM(tensor); 1091- if (input == nullptr) { 1092- MS_LOG(ERROR) << "Cannot allocate input"; 1093- return nullptr; 1094- } 1095- auto e = input->expr(); 1096- outputs[tensor_id] = e; 1097- net->PushInput(e); 1098- } 1099- for (size_t i = 0; i < metaGraph->nodes()->size(); i++) { 1100- auto Cnode = metaGraph->nodes()->Get(i); 1101- std::vector<EXPR *> param_tensors; 1102- for (size_t j = 0; j < Cnode->inputIndex()->size(); j++) { 1103- int tensor_id = Cnode->inputIndex()->Get(j); 1104- const schema::Tensor *tensor = metaGraph->allTensors()->Get(tensor_id); 1105- auto iter = outputs.find(tensor_id); 1106- if (iter == outputs.end()) { 1107- // create value node if not exist 1108- if (tensor->nodeType() != NodeType::NodeType_CNode) { 1109- auto valnode = new (std::nothrow) InputM(tensor); 1110- if (valnode == nullptr) { 1111- MS_LOG(ERROR) << "Cannot allocate valnode"; 1112- return nullptr; 1113- } 1114- outputs[tensor_id] = valnode->expr(); 1115- param_tensors.push_back(valnode->expr()); 1116- net->PushOp(valnode); 1117- } else { 1118- MS_LOG(ERROR) << "did not found input tensor " << tensor_id; 1119- return nullptr; 1120- } 1121- } else { 1122- param_tensors.push_back(iter->second); 1123- } 1124- } 1125- // create expression from node // 1126- auto node = CreateNode(Cnode); 1127- if (node != nullptr) { 1128- node->SetOutputs(Cnode->outputIndex()->size()); 1129- std::vector<EXPR *> e = (*node)(param_tensors); 1130- for (size_t j = 0; j < Cnode->outputIndex()->size(); j++) { 1131- int tensor_id = Cnode->outputIndex()->Get(j); 1132- outputs[tensor_id] = e.at(j); 1133- } 1134- } else { 1135- MS_LOG(ERROR) << "failed to create node " << Cnode->name(); 1136- return nullptr; 1137- } 1138- auto node_ptr = node.release(); 1139- net->PushOp(node_ptr); 1140- node_ptr->SetLearn(); 1141- } 1142- for (size_t i = 0; i < metaGraph->outputIndex()->size(); i++) { 1143- auto tensor_id = metaGraph->outputIndex()->Get(i); 1144- auto iter = outputs.find(tensor_id); 1145- if (iter == outputs.end()) { 1146- MS_LOG(ERROR) << "could not find source for tensor " << tensor_id; 1147- return nullptr; 1148- } else { 1149- net->PushOutput(iter->second); 1150- } 1151- } 1152- return net.release(); 1153-} 1154-} // namespace lite 1155-} // namespace mindspore 1156diff --git a/mindspore/lite/src/expression/import.h b/mindspore/lite/src/expression/import.h 1157deleted file mode 100644 1158index 3d4f301e..00000000 1159--- a/mindspore/lite/src/expression/import.h 1160+++ /dev/null 1161@@ -1,61 +0,0 @@ 1162-/** 1163- * Copyright 2022 Huawei Technologies Co., Ltd 1164- * 1165- * Licensed under the Apache License, Version 2.0 (the "License"); 1166- * you may not use this file except in compliance with the License. 1167- * You may obtain a copy of the License at 1168- * 1169- * http://www.apache.org/licenses/LICENSE-2.0 1170- * 1171- * Unless required by applicable law or agreed to in writing, software 1172- * distributed under the License is distributed on an "AS IS" BASIS, 1173- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1174- * See the License for the specific language governing permissions and 1175- * limitations under the License. 1176- */ 1177- 1178-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_IMPORT_H_ 1179-#define MINDSPORE_LITE_SRC_EXPRESSION_IMPORT_H_ 1180- 1181-#include <string> 1182-#include <unordered_map> 1183-#include <memory> 1184-#include "nnacl/op_base.h" 1185-#include "src/expression/net.h" 1186- 1187-namespace mindspore { 1188-namespace lite { 1189-using import_func = std::function<Node *()>; 1190- 1191-template <typename T> 1192-Node *ReturnNode() { 1193- return new (std::nothrow) T(); 1194-} 1195- 1196-class ImportReg { 1197- public: 1198- explicit ImportReg(mindspore::schema::PrimitiveType type, import_func func) { import_map_[type] = func; } 1199- static import_func GetImportFunc(mindspore::schema::PrimitiveType type); 1200- 1201- private: 1202- static std::unordered_map<mindspore::schema::PrimitiveType, import_func> import_map_; 1203-}; 1204- 1205-class Import { 1206- private: 1207- int8_t *buffer_ = nullptr; 1208- OpParameter *GetAttr(const schema::Primitive *prim); 1209- std::unique_ptr<Node> CreateNode(const schema::CNode *cnode); 1210- 1211- public: 1212- Net *ImportMs(const schema::MetaGraph *meta_graph); 1213- Net *ImportMs(std::string file_name); 1214- ~Import() { 1215- delete[] buffer_; 1216- buffer_ = nullptr; 1217- } 1218-}; 1219-} // namespace lite 1220-} // namespace mindspore 1221- 1222-#endif // MINDSPORE_LITE_SRC_EXPRESSION_IMPORT_H_ 1223diff --git a/mindspore/lite/src/expression/net.cc b/mindspore/lite/src/expression/net.cc 1224deleted file mode 100644 1225index 560f8cf6..00000000 1226--- a/mindspore/lite/src/expression/net.cc 1227+++ /dev/null 1228@@ -1,268 +0,0 @@ 1229-/** 1230- * Copyright 2022 Huawei Technologies Co., Ltd 1231- * 1232- * Licensed under the Apache License, Version 2.0 (the "License"); 1233- * you may not use this file except in compliance with the License. 1234- * You may obtain a copy of the License at 1235- * 1236- * http://www.apache.org/licenses/LICENSE-2.0 1237- * 1238- * Unless required by applicable law or agreed to in writing, software 1239- * distributed under the License is distributed on an "AS IS" BASIS, 1240- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1241- * See the License for the specific language governing permissions and 1242- * limitations under the License. 1243- */ 1244- 1245-#include "src/expression/net.h" 1246-#include <vector> 1247-#include "src/litert/cxx_api/expression/net_impl.h" 1248-#include "src/expression/ops.h" 1249-#include "src/expression/export.h" 1250-#include "src/expression/ops/addn.h" 1251-#include "src/expression/ops/arithmetic.h" 1252-#include "src/common/storage.h" 1253-#include "tools/common/meta_graph_serializer.h" 1254-namespace mindspore { 1255-namespace lite { 1256-void Net::update_name(std::string name) { 1257- if (!this->name().empty()) 1258- Node::update_name(name); 1259- else 1260- set_name(name); 1261- for (auto &itr : ops_) { 1262- itr->update_name(name); 1263- } 1264-} 1265- 1266-std::vector<EXPR *> Net::operator()(const std::initializer_list<EXPR *> &&inputs) { 1267- std::vector<EXPR *> vec = inputs; 1268- std::vector<EXPR *> x; 1269- if (impl_ == nullptr) { 1270- x = construct(inputs); 1271- } else { 1272- x = impl_->construct(vec); 1273- } 1274- return x; 1275-} 1276- 1277-std::vector<EXPR *> Net::operator()(const std::vector<EXPR *> &inputs) { 1278- std::vector<EXPR *> x; 1279- if (impl_ == nullptr) { 1280- x = construct(inputs); 1281- } else { 1282- x = impl_->construct(inputs); 1283- } 1284- input_ = inputs; 1285- output_ = x; 1286- real_output_ = x; 1287- return x; 1288-} 1289- 1290-std::vector<EXPR *> Net::construct(const std::vector<EXPR *> &inputs) { 1291- if (!output_.empty()) { 1292- if (input_.size() != inputs.size()) { 1293- MS_LOG(ERROR) << "input size mismatch, should be " << input_.size() << " got " << inputs.size(); 1294- return {}; 1295- } 1296- auto in_ptr = inputs; 1297- EXPR::Replace(output_, &input_, &in_ptr); 1298- } else { 1299- MS_LOG(ERROR) << "no network construction function"; 1300- } 1301- return output_; 1302-} 1303- 1304-void Net::TopoSortUtil(Node *node, std::stack<Node *> *stack) { 1305- visited_.insert(node); 1306- for (size_t i = 0; i < node->OutputsNum(); i++) { 1307- auto expr = node->expr(i); 1308- auto itr = outmap_.find(expr); 1309- if (itr != outmap_.end()) { 1310- for (auto &e : itr->second) 1311- if (visited_.find(e->node()) == visited_.end()) { 1312- TopoSortUtil(e->node(), stack); 1313- } 1314- } 1315- } 1316- stack->push(node); 1317-} 1318- 1319-std::vector<Node *> Net::Sort() { 1320- std::stack<Node *> stack; 1321- outmap_.clear(); 1322- EXPR::CreateOutputMap(output_, &outmap_); 1323- for (auto &itr : outmap_) { 1324- EXPR *e = itr.first; 1325- if (visited_.find(e->node()) == visited_.end()) { 1326- TopoSortUtil(e->node(), &stack); 1327- } 1328- } 1329- std::vector<Node *> res; 1330- while (stack.empty() == false) { 1331- res.push_back(stack.top()); 1332- stack.pop(); 1333- } 1334- visited_.clear(); 1335- return res; 1336-} 1337- 1338-std::unique_ptr<schema::MetaGraphT> Net::MakeMs() { 1339- auto nodes = Sort(); 1340- auto s = new (std::nothrow) ExportSession(outmap_); 1341- if (s == nullptr) { 1342- MS_LOG(ERROR) << "Cannot allocate export session"; 1343- return nullptr; 1344- } 1345- session_.reset(s); 1346- session_->Init(name(), Version()); 1347- for (auto node : nodes) { 1348- auto res = node->MakeEntry(session_.get()); 1349- if (res != RET_OK) { 1350- MS_LOG(ERROR) << "failed in MakeEntry: " << node->name(); 1351- return nullptr; 1352- } 1353- } 1354- session_->SetInputOutput(input_, real_output_); 1355- auto res = session_->meta_graph(); 1356- return std::unique_ptr<schema::MetaGraphT>(res); 1357-} 1358- 1359-std::unique_ptr<schema::MetaGraphT> Net::MakeMs(const std::string file_name) { 1360- auto graph = MakeMs(); 1361- Save(*graph, file_name); 1362- return graph; 1363-} 1364- 1365-std::set<Node *> Net::trainable_params() { 1366- std::set<Node *> res; 1367- for (auto &node : ops_) { 1368- res.merge(node->trainable_params()); 1369- } 1370- return res; 1371-} 1372- 1373-int Net::BuildGrad(Node *optimizer) { 1374- std::set<Node *> learn = optimizer->trainable_params(); 1375- auto NetOrder = Sort(); 1376- optimizer_.reset(optimizer); 1377- optimizer->AddNetOutput(&output_); 1378- std::map<std::pair<EXPR *, EXPR *>, EXPR *> backprop; 1379- for (auto itr = NetOrder.rbegin(); itr != NetOrder.rend(); itr++) { 1380- Node *node = *itr; 1381- EXPR *yt = nullptr; 1382- if (node->primitive() == schema::PrimitiveType_NONE) continue; 1383- if (outmap_.find(node->expr()) == outmap_.end() || outmap_[node->expr()].size() == 0) { 1384- yt = node->expr(); 1385- } else { 1386- std::vector<EXPR *> add_params; 1387- for (auto &output : outmap_[node->expr()]) { 1388- auto link = std::make_pair(node->expr(), output); 1389- auto grad = backprop[link]; 1390- add_params.push_back(grad); 1391- } 1392- if (add_params.size() == 1) { 1393- yt = add_params.front(); 1394- } else { 1395- auto addn = new (std::nothrow) AddN(0); 1396- if (addn == nullptr) { 1397- MS_LOG(ERROR) << "Cannot allocate add operator"; 1398- return RET_ERROR; 1399- } 1400- PushOp(addn); 1401- addn->update_name(name()); 1402- yt = (*addn)(add_params).front(); 1403- } 1404- } 1405- auto inGrads = node->Grad(yt); 1406- for (size_t i = 0; i < node->inputs().size(); i++) { 1407- EXPR *inGrad{nullptr}; 1408- if (i < inGrads.size()) { 1409- inGrad = inGrads[i]; 1410- } else { 1411- inGrad = nullptr; 1412- } 1413- auto input = node->input(i); 1414- if (learn.find(input->node()) != learn.end()) { 1415- auto opt = optimizer->Clone(inGrad, input); 1416- if (opt.size() == 0) { 1417- MS_LOG(ERROR) << "failed to create optimizer"; 1418- return RET_ERROR; 1419- } 1420- if (inGrad == nullptr) { 1421- MS_LOG(ERROR) << "illegal null value for grad"; 1422- return RET_ERROR; 1423- } 1424- if (opt.size() == 0) { 1425- MS_LOG(ERROR) << "optimizer for " << input->node()->name() << " failure"; 1426- return RET_ERROR; 1427- } 1428- auto opt_op = opt.at(0)->node(); 1429- PushOp(opt_op); 1430- opt_op->update_name(node->name()); 1431- output_.push_back(opt.at(0)); 1432- } 1433- auto link = std::make_pair(input, node->expr()); 1434- backprop[link] = inGrad; 1435- } 1436- } 1437- return RET_OK; 1438-} 1439- 1440-std::vector<EXPR *> Net::add(const std::vector<EXPR *> &input) { 1441- auto _add = NN::Add(); 1442- _add->set_name(name() + "/" + _add->name()); 1443- ops_.push_back(_add); 1444- return (*_add)(input); 1445-} 1446- 1447-Net *Net::TrainNet(Node *optimizer, Node *loss_fn, const std::vector<EXPR *> &inputs) { 1448- auto net = new (std::nothrow) NetWithLoss(this, loss_fn); 1449- if (net == nullptr) { 1450- MS_LOG(ERROR) << "Cannot allocate loss network"; 1451- return nullptr; 1452- } 1453- return net->TrainNet(optimizer, inputs); 1454-} 1455- 1456-Net *Net::TrainNet(Node *optimizer, const std::vector<EXPR *> &inputs) { 1457- auto x = (*this)(inputs); 1458- auto res = BuildGrad(optimizer); 1459- if (res != RET_OK) { 1460- MS_LOG(ERROR) << "Build gradient network failed"; 1461- return nullptr; 1462- } 1463- real_output_ = x; 1464- return this; 1465-} 1466- 1467-int Net::Save(const schema::MetaGraphT &graph, std::string file_name) { return Storage::Save(graph, file_name); } 1468- 1469-const std::vector<int> Net::OutputShape(int idx) { 1470- if (static_cast<size_t>(idx) >= real_output_.size()) { 1471- MS_LOG(ERROR) << "index (" << idx << ") exceed output size (" << real_output_.size() << ")"; 1472- return {}; 1473- } 1474- return real_output_.at(idx)->dims(); 1475-} 1476- 1477-const std::vector<int> Net::InputShape(int idx) { 1478- if (static_cast<size_t>(idx) >= input_.size()) { 1479- MS_LOG(ERROR) << "index (" << idx << ") exceed input size (" << input_.size() << ")"; 1480- return {}; 1481- } 1482- return input_.at(idx)->dims(); 1483-} 1484- 1485-Net::~Net() { 1486- if (impl_ != nullptr) { 1487- impl_->erase_net(); 1488- auto pnet = impl_->pnet(); 1489- if (pnet != nullptr) { 1490- impl_->set_pnet(nullptr); 1491- } 1492- } 1493- impl_ = nullptr; 1494-} 1495-} // namespace lite 1496-} // namespace mindspore 1497diff --git a/mindspore/lite/src/expression/net.h b/mindspore/lite/src/expression/net.h 1498deleted file mode 100644 1499index f5525173..00000000 1500--- a/mindspore/lite/src/expression/net.h 1501+++ /dev/null 1502@@ -1,114 +0,0 @@ 1503-/** 1504- * Copyright 2022 Huawei Technologies Co., Ltd 1505- * 1506- * Licensed under the Apache License, Version 2.0 (the "License"); 1507- * you may not use this file except in compliance with the License. 1508- * You may obtain a copy of the License at 1509- * 1510- * http://www.apache.org/licenses/LICENSE-2.0 1511- * 1512- * Unless required by applicable law or agreed to in writing, software 1513- * distributed under the License is distributed on an "AS IS" BASIS, 1514- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1515- * See the License for the specific language governing permissions and 1516- * limitations under the License. 1517- */ 1518- 1519-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_NET_H_ 1520-#define MINDSPORE_LITE_SRC_EXPRESSION_NET_H_ 1521-#include <stack> 1522-#include <memory> 1523-#include <set> 1524-#include <map> 1525-#include <utility> 1526-#include <string> 1527-#include <unordered_set> 1528-#include <list> 1529-#include <vector> 1530-#include "src/expression/node.h" 1531-#include "inner/model_generated.h" 1532- 1533-namespace mindspore { 1534-class Net; 1535-class NetImpl; 1536-namespace lite { 1537-#define REG(_name) Register(_name, #_name) 1538- 1539-class ExportSession; 1540- 1541-class Net : public Node { 1542- public: 1543- Net() = default; 1544- virtual ~Net(); 1545- explicit Net(std::string name) : Node(name) {} 1546- std::vector<EXPR *> construct(const std::vector<EXPR *> &inputs) override; 1547- std::vector<EXPR *> operator()(const std::vector<EXPR *> &inputs) override; 1548- std::vector<EXPR *> operator()(const std::initializer_list<EXPR *> &&inputs) override; 1549- void update_name(std::string name) override; 1550- Net *TrainNet(Node *optimizer, Node *loss_fn, const std::vector<EXPR *> &inputs); 1551- Net *TrainNet(Node *optimizer, const std::vector<EXPR *> &inputs); 1552- void PrintDot() { EXPR::PrintDot(output_); } 1553- 1554- void PushOutput(EXPR *e) { output_.push_back(e); } 1555- void PushInput(EXPR *e) { input_.push_back(e); } 1556- void SetRealOutput() { real_output_ = output_; } 1557- std::set<Node *> trainable_params() override; 1558- std::vector<Node *> Sort(); 1559- int BuildGrad(Node *optimizer); 1560- int BuildGrad(Node *optimizer, std::set<Node *> learnable); 1561- std::unique_ptr<schema::MetaGraphT> MakeMs(); 1562- std::unique_ptr<schema::MetaGraphT> MakeMs(std::string file_name); 1563- schema::MetaGraph *meta_graph() { return meta_graph_; } 1564- int Save(const schema::MetaGraphT &graph, const std::string filename); 1565- void set_impl(std::shared_ptr<mindspore::NetImpl> impl) { impl_ = impl; } 1566- const std::vector<int> InputShape(int idx); 1567- const std::vector<int> OutputShape(int idx); 1568- 1569- protected: 1570- std::vector<EXPR *> add(const std::vector<EXPR *> &input); 1571- void Register(Node *node, std::string &&name) { 1572- if (node != nullptr) { 1573- PushOp(node); 1574- node->update_name(name); 1575- } 1576- } 1577- 1578- private: 1579- friend mindspore::Net; 1580- std::unordered_set<Node *> visited_; 1581- std::map<EXPR *, std::list<EXPR *>> outmap_; // outputs per expression 1582- std::map<EXPR *, std::list<EXPR *>> inmap_; // inputs per expression 1583- std::vector<EXPR *> output_; // network output expression 1584- std::vector<EXPR *> real_output_; // network output for export 1585- std::vector<EXPR *> input_; // network input expression 1586- schema::MetaGraph *meta_graph_; // imported meta_graph 1587- std::unique_ptr<ExportSession> session_; // export session 1588- std::unique_ptr<Node> optimizer_; 1589- void TopoSortUtil(Node *v, std::stack<Node *> *stack); 1590- void CreateOutputMap(std::vector<EXPR *> vec, std::map<Node *, std::list<Node *>> *outmap); 1591- std::shared_ptr<mindspore::NetImpl> impl_; 1592-}; 1593- 1594-class NetWithLoss : public Net { 1595- public: 1596- NetWithLoss(Net *net, Node *loss) : net_(net), loss_fn_(loss) { 1597- REG(net_); 1598- REG(loss_fn_); 1599- loss_fn_->set_name("_loss_fn"); 1600- } 1601- std::vector<EXPR *> construct(const std::vector<EXPR *> &inputs) { 1602- auto input = inputs[0]; 1603- auto label = inputs[1]; 1604- auto x = (*net_)({input}); 1605- x = (*loss_fn_)({x[0], label}); 1606- return {x.front()}; 1607- } 1608- 1609- private: 1610- Net *net_{nullptr}; 1611- Node *loss_fn_{nullptr}; 1612-}; 1613-} // namespace lite 1614-} // namespace mindspore 1615- 1616-#endif // MINDSPORE_LITE_SRC_EXPRESSION_NET_H_ 1617diff --git a/mindspore/lite/src/expression/node.cc b/mindspore/lite/src/expression/node.cc 1618deleted file mode 100644 1619index 022f15d4..00000000 1620--- a/mindspore/lite/src/expression/node.cc 1621+++ /dev/null 1622@@ -1,271 +0,0 @@ 1623-/** 1624- * Copyright 2022 Huawei Technologies Co., Ltd 1625- * 1626- * Licensed under the Apache License, Version 2.0 (the "License"); 1627- * you may not use this file except in compliance with the License. 1628- * You may obtain a copy of the License at 1629- * 1630- * http://www.apache.org/licenses/LICENSE-2.0 1631- * 1632- * Unless required by applicable law or agreed to in writing, software 1633- * distributed under the License is distributed on an "AS IS" BASIS, 1634- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1635- * See the License for the specific language governing permissions and 1636- * limitations under the License. 1637- */ 1638- 1639-#include <algorithm> 1640-#include <utility> 1641-#include <functional> 1642-#include "src/expression/node.h" 1643-#include "src/expression/ops.h" 1644-#include "src/expression/export.h" 1645-#include "src/litert/infer_manager.h" 1646-#include "src/common/utils.h" 1647-#include "src/litert/cxx_api/expression/net_impl.h" 1648- 1649-namespace mindspore { 1650-namespace lite { 1651-int Node::name_id; 1652- 1653-std::vector<EXPR *> Node::construct(const std::vector<EXPR *> &inputs) { 1654- if (inputs.size() >= expr()->params().size()) { 1655- expr()->set_params(inputs); 1656- } else { 1657- for (std::size_t i = 0; i < inputs.size(); i++) { 1658- expr()->set_params(i, inputs[i]); 1659- } 1660- } 1661- auto ret = InferShape(); 1662- if (ret != RET_OK) { 1663- MS_LOG(ERROR) << "error infershape for node " << name(); 1664- return {}; 1665- } 1666- std::vector<EXPR *> res(expr_.size()); 1667- (void)std::transform(expr_.begin(), expr_.end(), res.begin(), [](const EXPR &e) { return const_cast<EXPR *>(&e); }); 1668- return res; 1669-} 1670- 1671-std::vector<EXPR *> Node::Grad(EXPR *expr) { 1672- MS_LOG(ERROR) << name() << " (" << schema::EnumNamePrimitiveType(primitive()) << ") does not have grad defined"; 1673- return {}; 1674-} 1675- 1676-int Node::CreateTensorFromExpr(const std::vector<EXPR *> &expr, std::vector<Tensor *> *tensors, bool is_input) { 1677- MS_ASSERT(tensors != nullptr); 1678- int ret = RET_OK; 1679- for (auto e : expr) { 1680- // Tensor -> TensorC 1681- if (is_input && e->node()->primitive() == schema::PrimitiveType_Depend) { 1682- continue; 1683- } 1684- auto type = (e->node()->primitive() != schema::PrimitiveType_NONE) ? Category::VAR : Category::CONST_TENSOR; 1685- auto t = std::make_unique<Tensor>(e->data_type(), e->dims(), (mindspore::Format)e->format(), type); 1686- if (t == nullptr) { 1687- ret = RET_NULL_PTR; 1688- break; 1689- } 1690- // copy data if any 1691- if (type == Category::CONST_TENSOR) { 1692- void *dst = t->MutableData(); 1693- if (dst == nullptr) { 1694- ret = RET_NULL_PTR; 1695- break; 1696- } 1697- if (e->node()->data() && (e->node()->data()->data().size() > 0)) { 1698- uint8_t *src = e->node()->data()->data().data(); 1699- memcpy(dst, src, t->Size()); 1700- } 1701- } 1702- tensors->push_back(t.release()); 1703- } 1704- return ret; 1705-} 1706- 1707-void Node::FreeAllTensors(std::vector<Tensor *> *tensors) { 1708- MS_ASSERT(tensors != nullptr); 1709- for (auto &t : *tensors) { 1710- delete t; 1711- } 1712- tensors->clear(); 1713-} 1714- 1715-int Node::InferShape() { 1716- auto ret = RET_OK; 1717- std::vector<Tensor *> in_tensors; 1718- std::vector<Tensor *> out_tensors; 1719- // build in \ out tensors 1720- ret = CreateTensorFromExpr(expr()->params(), &in_tensors, true); 1721- if (ret != RET_OK) { 1722- MS_LOG(ERROR) << "Failed in create in tensors"; 1723- FreeAllTensors(&in_tensors); 1724- return RET_ERROR; 1725- } 1726- std::vector<EXPR *> expr(expr_.size()); 1727- (void)std::transform(expr_.begin(), expr_.end(), expr.begin(), [](const EXPR &e) { return const_cast<EXPR *>(&e); }); 1728- ret = CreateTensorFromExpr(expr, &out_tensors); 1729- if (ret != RET_OK) { 1730- MS_LOG(ERROR) << "Failed in create out tensors"; 1731- FreeAllTensors(&in_tensors); 1732- FreeAllTensors(&out_tensors); 1733- return RET_ERROR; 1734- } 1735- // Do infer Shape 1736- ret = KernelInferShape(in_tensors, out_tensors, OpParam()); 1737- if (ret != RET_OK) { 1738- MS_LOG(ERROR) << "failed in infer shape for " << name(); 1739- FreeAllTensors(&in_tensors); 1740- FreeAllTensors(&out_tensors); 1741- return RET_ERROR; 1742- } 1743- // copy infer shape into expr 1744- for (uint32_t i = 0; i < expr_.size(); i++) { 1745- auto e = &expr_.at(i); 1746- auto o = out_tensors.at(i); 1747- e->set_format((o->format())); 1748- e->set_data_type(o->data_type()); 1749- e->SetDims(o->shape()); 1750- } 1751- // cleanup 1752- FreeAllTensors(&in_tensors); 1753- FreeAllTensors(&out_tensors); 1754- 1755- return ret; 1756-} 1757- 1758-EXPR *Node::CreateWeights(std::vector<int> dims, TypeId data_type, int format, Param::Mode mode, std::string name) { 1759- auto weights = new (std::nothrow) InputM(dims); 1760- if (weights == nullptr) { 1761- MS_LOG(ERROR) << "Cannot allocate weights"; 1762- return nullptr; 1763- } 1764- weights->set_name(this->name() + "/" + name); 1765- int size = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<int>()); 1766- weights->data()->SetSize(size); 1767- weights->data()->Fill(mode); 1768- PushOp(weights); 1769- return weights->expr(); 1770-} 1771- 1772-Node *Node::CreateConstTensor(int index, std::vector<int> dims, TypeId data_type, int format, std::string name, 1773- const void *data) { 1774- auto tensor = NN::Input(dims, data_type, format); 1775- int elem_size = DataTypeSize(data_type); 1776- tensor->set_name(this->name() + "/" + name); 1777- int size = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<int>()) * elem_size; 1778- tensor->data()->SetSize(size); 1779- tensor->data()->Copy(reinterpret_cast<const uint8_t *>(data), size); 1780- expr()->set_params(index, tensor->expr()); 1781- PushOp(tensor); 1782- return tensor; 1783-} 1784- 1785-int Node::MakeEntry(ExportSession *session) { 1786- std::vector<uint32_t> input_idx; 1787- std::vector<uint32_t> output_idx; 1788- std::vector<uint8_t> empty; 1789- if (primitive() == schema::PrimitiveType_Depend) return RET_OK; 1790- // create node input 1791- size_t inputs = InputsNum(); 1792- for (size_t i = 0; i < inputs; i++) { 1793- EXPR *ex = expr()->GetInput(i); 1794- if (ex->node()->primitive() == schema::PrimitiveType_Depend) continue; 1795- uint32_t id = session->GetOutput(ex); 1796- input_idx.push_back(id); 1797- } 1798- size_t outputs = OutputsNum(); 1799- size_t last_id = session->meta_graph()->allTensors.size(); 1800- int type = (primitive() == schema::PrimitiveType_NONE) ? static_cast<int>(NodeType_ValueNode) : static_cast<int>(NodeType_CNode); 1801- auto data = (type == static_cast<int>(NodeType_ValueNode)) ? this->data()->data() : empty; 1802- if (data.empty()) type = NodeType_CNode; // input is Cnode !!? 1803- int idx = 0; 1804- for (size_t i = 0; i < outputs; i++) { 1805- if (session->IsToDependOnly(expr(i))) continue; 1806- output_idx.push_back(last_id + idx); 1807- session->UpdateOutput(expr(i), last_id + idx); 1808- auto odims = dims(i); 1809- auto data_type = expr(i)->data_type(); 1810- auto format = expr(i)->format(); 1811- std::string footer = (i > 0) ? ("-" + std::to_string(i)) : ""; 1812- auto otensor = CreateTensor(name() + footer, type, data_type, odims, format, data); 1813- std::cout << "tensor -" << last_id + idx << ": " << name() + footer << std::endl; 1814- idx++; 1815- session->meta_graph()->allTensors.emplace_back(std::move(otensor)); 1816- } 1817- if (primitive() != schema::PrimitiveType_NONE) { 1818- if (output_idx.size() == 0) { 1819- return RET_OK; 1820- } 1821- auto cnode = CreateCNode(input_idx, output_idx); 1822- 1823- auto ret = UnPopulate(cnode); 1824- if (ret != RET_OK) { 1825- MS_LOG(ERROR) << "failed to populate cnode"; 1826- return RET_ERROR; 1827- } 1828- session->meta_graph()->nodes.emplace_back(std::move(cnode)); 1829- } 1830- 1831- return RET_OK; 1832-} 1833- 1834-std::unique_ptr<schema::CNodeT> Node::CreateCNode(std::vector<uint32_t> inputIndex, std::vector<uint32_t> outputIndex) { 1835- auto cnode = std::make_unique<schema::CNodeT>(); 1836- cnode->primitive = std::make_unique<schema::PrimitiveT>(); 1837- cnode->primitive->value.type = primitive(); 1838- cnode->name = name(); 1839- cnode->inputIndex = inputIndex; 1840- cnode->outputIndex = outputIndex; 1841- return cnode; 1842-} 1843- 1844-int Node::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 1845- MS_LOG(ERROR) << "Node " << schema::EnumNamePrimitiveType(primitive()) << " cannot be exported"; 1846- return RET_ERROR; 1847-} 1848- 1849-std::unique_ptr<mindspore::schema::TensorT> Node::CreateTensor(std::string name, int type, int data_type, 1850- const std::vector<int32_t> dims, int format, 1851- const std::vector<uint8_t> &data) { 1852- auto tensorT = std::make_unique<mindspore::schema::TensorT>(); 1853- tensorT->nodeType = type; 1854- tensorT->dims = dims; 1855- tensorT->format = static_cast<schema::Format>(format); 1856- tensorT->name = name; 1857- tensorT->refCount = 0; 1858- tensorT->offset = 0; 1859- tensorT->dataType = data_type; 1860- tensorT->data = data; 1861- tensorT->enableHuffmanCode = false; 1862- if (tensorT->nodeType == mindspore::lite::NodeType_ValueNode) { 1863- tensorT->data = data; 1864- } 1865- return tensorT; 1866-} 1867- 1868-int Node::SetOutputs(int num) { 1869- EXPR e(this); 1870- e.SetSize(0); 1871- for (auto i = expr_.size(); i < static_cast<size_t>(num); i++) { 1872- expr_.emplace_back(e); 1873- } 1874- return RET_OK; 1875-} 1876- 1877-Node::~Node() { 1878- for (auto &op : ops_) { 1879- delete op; 1880- } 1881- ops_.clear(); 1882- if (impl_ != nullptr) { 1883- impl_->set_node(nullptr); 1884- auto pnode = impl_->pnode(); 1885- if (pnode != nullptr) { 1886- impl_->set_pnode(nullptr); 1887- delete pnode; 1888- } 1889- } 1890- impl_ = nullptr; 1891-} 1892-} // namespace lite 1893-} // namespace mindspore 1894diff --git a/mindspore/lite/src/expression/node.h b/mindspore/lite/src/expression/node.h 1895deleted file mode 100644 1896index b6a820e0..00000000 1897--- a/mindspore/lite/src/expression/node.h 1898+++ /dev/null 1899@@ -1,156 +0,0 @@ 1900-/** 1901- * Copyright 2022 Huawei Technologies Co., Ltd 1902- * 1903- * Licensed under the Apache License, Version 2.0 (the "License"); 1904- * you may not use this file except in compliance with the License. 1905- * You may obtain a copy of the License at 1906- * 1907- * http://www.apache.org/licenses/LICENSE-2.0 1908- * 1909- * Unless required by applicable law or agreed to in writing, software 1910- * distributed under the License is distributed on an "AS IS" BASIS, 1911- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1912- * See the License for the specific language governing permissions and 1913- * limitations under the License. 1914- */ 1915- 1916-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_NODE_H_ 1917-#define MINDSPORE_LITE_SRC_EXPRESSION_NODE_H_ 1918- 1919-#include <stdlib.h> 1920-#include <vector> 1921-#include <string> 1922-#include <iostream> 1923-#include <memory> 1924-#include <set> 1925-#include "src/expression/export.h" 1926-#include "inner/model_generated.h" 1927-#include "src/expression/param.h" 1928-#include "src/expression/expr.h" 1929-#include "src/tensor.h" 1930-#include "nnacl/op_base.h" 1931- 1932-namespace mindspore { 1933-class NodeImpl; 1934-namespace schema { 1935-struct TensorT; 1936-struct CNodeT; 1937-} // namespace schema 1938- 1939-namespace lite { 1940-class Node { 1941- public: 1942- const std::string kGradName = "Gradients"; 1943- explicit Node(const std::string name) : opParam_(nullptr), name_(name) { expr_.emplace_back(this); } 1944- virtual ~Node(); 1945- Node() : Node("") {} 1946- explicit Node(Node *node) : Node(*node) {} 1947- EXPR *create(std::string name) { 1948- name_ = name; 1949- return &expr_[0]; 1950- } 1951- virtual std::vector<EXPR *> operator()(const std::vector<EXPR *> &inputs) { 1952- auto x = construct(inputs); 1953- return x; 1954- } 1955- virtual std::vector<EXPR *> operator()(const std::initializer_list<EXPR *> &&inputs) { 1956- std::vector<EXPR *> vec = inputs; 1957- auto x = construct(vec); 1958- return x; 1959- } 1960- virtual std::vector<EXPR *> operator()(const std::initializer_list<EXPR *> &inputs) { 1961- std::vector<EXPR *> vec = inputs; 1962- auto x = construct(vec); 1963- return x; 1964- } 1965- void set_primitive(schema::PrimitiveType primitive) { 1966- primitive_ = primitive; 1967- if (OpParam() != nullptr) opParam_->type_ = primitive_; 1968- } 1969- schema::PrimitiveType primitive() { return primitive_; } 1970- virtual std::vector<EXPR *> construct(const std::vector<EXPR *> &inputs); 1971- std::string name() { return name_; } 1972- void set_name(std::string name) { name_ = name; } 1973- virtual void update_name(std::string name) { set_name(name + "/" + name_); } 1974- size_t Load(std::string file_name, size_t offset = 0) { return offset; } 1975- OpParameter *OpParam() const { return opParam_.get(); } 1976- virtual void Add(Node *node) {} 1977- virtual std::vector<EXPR *> Clone(EXPR *grad, EXPR *weight) { return {}; } 1978- void SetOpParam(std::shared_ptr<OpParameter> opParam) { opParam_ = opParam; } 1979- void SetOpParam(void *opParam) { opParam_.reset(reinterpret_cast<OpParameter *>(opParam), free); } 1980- static std::string UniqueName(const std::string &name) { return name + "-" + std::to_string(name_id++); } 1981- static std::string UniqueName(std::string &&name) { return name + "-" + std::to_string(name_id++); } 1982- template <typename T> 1983- int CloneOpParam(std::shared_ptr<OpParameter> opParam) { 1984- auto t = reinterpret_cast<T *>(opParam.get()); 1985- auto obj = new (std::nothrow) T(*t); // copy content 1986- if (obj == nullptr) { 1987- MS_LOG(ERROR) << "Cannot allocate obj"; 1988- return RET_ERROR; 1989- } 1990- opParam_.reset(reinterpret_cast<OpParameter *>(obj)); 1991- return RET_OK; 1992- } 1993- template <typename T> 1994- int CloneOpParam(OpParameter *opParam) { 1995- auto t = reinterpret_cast<T *>(opParam); 1996- auto obj = new (std::nothrow) T(*t); // copy content 1997- if (obj == nullptr) { 1998- MS_LOG(ERROR) << "Cannot allocate obj"; 1999- return RET_ERROR; 2000- } 2001- opParam_.reset(reinterpret_cast<OpParameter *>(obj)); 2002- return RET_OK; 2003- } 2004- virtual Param *weight() { return nullptr; } 2005- EXPR *expr(int i) { return &expr_[i]; } 2006- EXPR *expr() { return expr(0); } 2007- std::vector<EXPR *> inputs() { return expr()[0].params(); } 2008- size_t InputsNum() { return expr()[0].params().size(); } 2009- size_t OutputsNum() { return expr_.size(); } 2010- EXPR *input(int idx) { return expr()[0].params().at(idx); } 2011- EXPR *output(int idx) { return expr(idx); } 2012- EXPR *CreateWeights(std::vector<int> dims, TypeId data_type, int format, Param::Mode mode, std::string name); 2013- Node *CreateConstTensor(int index, std::vector<int> dims, TypeId data_type, int format, std::string name, 2014- const void *data); 2015- virtual std::vector<EXPR *> Grad(EXPR *expr); 2016- virtual Param *data() { return nullptr; } 2017- bool IsLearn(Node *node) { return learnable_.find(node) != learnable_.end(); } 2018- virtual void SetLearn() {} 2019- virtual std::set<Node *> trainable_params() { return learnable_; } 2020- std::vector<int> &dims() { return expr()->dims(); } 2021- std::vector<int> &dims(int i) { return expr(i)->dims(); } 2022- // export 2023- int MakeEntry(ExportSession *session); 2024- void PushOp(Node *n) { ops_.push_back(n); } 2025- virtual void AddNetOutput(std::vector<EXPR *> *output) {} 2026- int SetOutputs(int num); 2027- std::shared_ptr<OpParameter> opParam_; 2028- void set_impl(std::shared_ptr<NodeImpl> impl) { impl_ = impl; } 2029- 2030- protected: 2031- std::vector<EXPR> expr_; // hold outputs 2032- std::vector<Node *> ops_; // all nodes or subnets 2033- int InferShape(); 2034- void AddLearn(Node *node) { learnable_.insert(node); } 2035- void AssignLearn(std::set<Node *> &&learn) { learnable_ = learn; } 2036- 2037- std::unique_ptr<schema::CNodeT> CreateCNode(std::vector<uint32_t> inputIndex, std::vector<uint32_t> outputIndex); 2038- virtual int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode); 2039- std::unique_ptr<schema::TensorT> CreateTensor(std::string name, int type, int data_type, 2040- const std::vector<int32_t> dims, int format, 2041- const std::vector<uint8_t> &data); 2042- 2043- private: 2044- int CreateTensorFromExpr(const std::vector<EXPR *> &expr, std::vector<Tensor *> *tensors, bool is_input = false); 2045- void FreeAllTensors(std::vector<Tensor *> *tensors); 2046- static int name_id; 2047- std::set<Node *> learnable_; // set of nodes with learnable parameters 2048- std::string name_; 2049- schema::PrimitiveType primitive_; 2050- std::shared_ptr<NodeImpl> impl_; 2051-}; 2052-} // namespace lite 2053-} // namespace mindspore 2054- 2055-#endif // MINDSPORE_LITE_SRC_EXPRESSION_NODE_H_ 2056diff --git a/mindspore/lite/src/expression/ops.cc b/mindspore/lite/src/expression/ops.cc 2057deleted file mode 100644 2058index 629fa0ae..00000000 2059--- a/mindspore/lite/src/expression/ops.cc 2060+++ /dev/null 2061@@ -1,66 +0,0 @@ 2062- 2063-/** 2064- * Copyright 2022 Huawei Technologies Co., Ltd 2065- * 2066- * Licensed under the Apache License, Version 2.0 (the "License"); 2067- * you may not use this file except in compliance with the License. 2068- * You may obtain a copy of the License at 2069- * 2070- * http://www.apache.org/licenses/LICENSE-2.0 2071- * 2072- * Unless required by applicable law or agreed to in writing, software 2073- * distributed under the License is distributed on an "AS IS" BASIS, 2074- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2075- * See the License for the specific language governing permissions and 2076- * limitations under the License. 2077- */ 2078- 2079-#include <numeric> 2080-#include <algorithm> 2081-#include "src/expression/ops.h" 2082-#include "src/expression/ops_utils.h" 2083-#include "src/expression/param.h" 2084-#include "include/api/cfg.h" 2085-#include "src/expression/sequential.h" 2086- 2087-namespace mindspore { 2088-namespace lite { 2089-void InputM::SetUp(const std::vector<int> &dims, TypeId data_type, int fmt) { 2090- expr()->SetSize(C0NUM); 2091- expr()->SetDims(dims); 2092- expr()->set_data_type(data_type); 2093- expr()->set_format(fmt); 2094- set_primitive(schema::PrimitiveType_NONE); 2095-} 2096- 2097-InputM::InputM(const std::vector<int> &dims, TypeId data_type, int fmt) : Node() { SetUp(dims, data_type, fmt); } 2098- 2099-InputM::InputM(const schema::Tensor *tensor) : Node() { 2100- std::vector<int> dims(tensor->dims()->size()); 2101- (void)std::transform(tensor->dims()->begin(), tensor->dims()->end(), dims.begin(), [](int32_t x) { return x; }); 2102- SetUp(dims, static_cast<TypeId>(tensor->dataType()), tensor->format()); 2103- if (tensor->name()) set_name(tensor->name()->str()); 2104- if (tensor->data() != nullptr) data_.Copy(tensor->data()->data(), tensor->data()->size()); 2105-} 2106- 2107-namespace NN { 2108-Node *Input(const std::vector<int> &dims, TypeId data_type, int fmt) { 2109- auto i = new (std::nothrow) InputM(dims, data_type, fmt); 2110- if (i == nullptr) { 2111- MS_LOG(ERROR) << "Cannot allocate input expression "; 2112- return nullptr; 2113- } 2114- return i; 2115-} 2116- 2117-Net *Sequential() { 2118- auto s = new (std::nothrow) mindspore::lite::Sequential(); 2119- if (s == nullptr) { 2120- MS_LOG(ERROR) << "Cannot allocate sequential expression"; 2121- return nullptr; 2122- } 2123- return s; 2124-} 2125-}; // namespace NN 2126-} // namespace lite 2127-} // namespace mindspore 2128diff --git a/mindspore/lite/src/expression/ops.h b/mindspore/lite/src/expression/ops.h 2129deleted file mode 100644 2130index 96a006f4..00000000 2131--- a/mindspore/lite/src/expression/ops.h 2132+++ /dev/null 2133@@ -1,69 +0,0 @@ 2134-/** 2135- * Copyright 2022 Huawei Technologies Co., Ltd 2136- * 2137- * Licensed under the Apache License, Version 2.0 (the "License"); 2138- * you may not use this file except in compliance with the License. 2139- * You may obtain a copy of the License at 2140- * 2141- * http://www.apache.org/licenses/LICENSE-2.0 2142- * 2143- * Unless required by applicable law or agreed to in writing, software 2144- * distributed under the License is distributed on an "AS IS" BASIS, 2145- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2146- * See the License for the specific language governing permissions and 2147- * limitations under the License. 2148- */ 2149- 2150-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_H_ 2151-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_H_ 2152- 2153-#include <vector> 2154-#include <string> 2155-#include <set> 2156-#include "include/api/net.h" 2157-#include "src/expression/cfg.h" 2158-#include "src/expression/net.h" 2159-#include "inner/model_generated.h" 2160- 2161-namespace mindspore { 2162-namespace lite { 2163-class InputM : public Node { 2164- public: 2165- explicit InputM(const schema::Tensor *tensor); 2166- explicit InputM(const std::vector<int> &dims, TypeId data_type = kNumberTypeFloat32, int fmt = NHWC); 2167- Param *data() override { return &data_; } 2168- 2169- private: 2170- void SetUp(const std::vector<int> &dims, TypeId data_type, int fmt); 2171- Param data_; 2172-}; 2173-namespace NN { 2174-Node *Conv2D(const ConvConfig &cfg); 2175-Node *Relu(); 2176-Node *Dense(const DenseConfig &cfg); 2177-Node *Flatten(); 2178-Node *Input(const std::vector<int> &dims, TypeId data_type = kNumberTypeFloat32, int fmt = NHWC); 2179-Node *Add(); 2180-Node *Sub(); 2181-Node *Div(); 2182-Node *Mul(); 2183-Node *Neg(); 2184-Node *SoftmaxCrossEntropy(); 2185-Net *Sequential(); 2186-Node *Adam(std::set<Node *> &&learn, const AdamConfig &cfg); 2187- 2188-Node *Softmax(int axis = -1); 2189-Node *BatchNorm2D(int outp, float momentum = 0.1, float epsilon = 1e-5f); 2190-Node *Sigmoid(); 2191-Node *DropOut(float ration = 0.5); 2192-Node *ReLU6(); 2193-Node *Reshape(const std::vector<int> &shape); 2194-Node *ReduceMean(bool keep_dims, const std::vector<int> &dims); 2195-Node *ReduceSum(bool keep_dims, const std::vector<int> &dims); 2196-Node *Tile(const std::vector<int> &multiples); 2197-Node *MaxPool2D(const PoolingConfig &cfg); 2198-Node *AvgPool2D(const PoolingConfig &cfg); 2199-} // namespace NN 2200-} // namespace lite 2201-} // namespace mindspore 2202-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_H_ 2203diff --git a/mindspore/lite/src/expression/ops/activation.cc b/mindspore/lite/src/expression/ops/activation.cc 2204deleted file mode 100644 2205index 3429a003..00000000 2206--- a/mindspore/lite/src/expression/ops/activation.cc 2207+++ /dev/null 2208@@ -1,133 +0,0 @@ 2209-/** 2210- * Copyright 2022 Huawei Technologies Co., Ltd 2211- * 2212- * Licensed under the Apache License, Version 2.0 (the "License"); 2213- * you may not use this file except in compliance with the License. 2214- * You may obtain a copy of the License at 2215- * 2216- * http://www.apache.org/licenses/LICENSE-2.0 2217- * 2218- * Unless required by applicable law or agreed to in writing, software 2219- * distributed under the License is distributed on an "AS IS" BASIS, 2220- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2221- * See the License for the specific language governing permissions and 2222- * limitations under the License. 2223- */ 2224-#include "src/expression/ops/activation.h" 2225-#include "nnacl/fp32/activation_fp32.h" 2226-#include "src/expression/import.h" 2227-#include "src/expression/ops.h" 2228-#include "src/litert/cxx_api/expression/node_impl.h" 2229- 2230-namespace mindspore { 2231-namespace lite { 2232-ActM::ActM(schema::ActivationType type) : Node() { 2233- auto op_param = malloc(sizeof(ActivationParameter)); 2234- if (op_param == nullptr) { 2235- MS_LOG(ERROR) << " cannot allocate ActivationParameter"; 2236- return; 2237- } 2238- SetOpParam(op_param); 2239- set_primitive(schema::PrimitiveType_Activation); 2240- ActivationParameter *act_param = reinterpret_cast<ActivationParameter *>(opParam_.get()); 2241- act_param->type_ = type; 2242- act_param->alpha_ = 0.f; 2243- act_param->min_val_ = 0.f; 2244- act_param->max_val_ = 0.f; 2245-} 2246- 2247-std::vector<EXPR *> ActM::Grad(EXPR *yt) { 2248- auto actGrad = new (std::nothrow) ActGradM(this); 2249- if (actGrad == nullptr) { 2250- MS_LOG(ERROR) << "Cannot allocate activation grad node"; 2251- return {}; 2252- } 2253- PushOp(actGrad); 2254- auto param = reinterpret_cast<ActivationParameter *>(actGrad->OpParam()); 2255- EXPR *ag = nullptr; 2256- actGrad->expr()->SetSize(C2NUM); 2257- if ((param->type_ == schema::ActivationType_SIGMOID) || (param->type_ == schema::ActivationType_TANH)) { 2258- ag = (*actGrad)({output(0), yt}).front(); 2259- } else if ((param->type_ == schema::ActivationType_HSWISH) || (param->type_ == schema::ActivationType_HSIGMOID) || 2260- (param->type_ == schema::ActivationType_RELU6)) { 2261- ag = (*actGrad)({yt, input(0)}).front(); 2262- } else if (param->type_ == schema::ActivationType_GELU) { 2263- actGrad->expr()->SetSize(C3NUM); 2264- ag = (*actGrad)({yt, input(0), output(0)}).front(); 2265- } else { 2266- ag = (*actGrad)({yt, output(0)}).front(); 2267- } 2268- std::vector<EXPR *> res = {ag}; 2269- return res; 2270-} 2271- 2272-int ActM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 2273- auto act_param = reinterpret_cast<const ActivationParameter *>(OpParam()); 2274- auto prim = new (std::nothrow) schema::ActivationT; 2275- if (prim == nullptr) { 2276- MS_LOG(ERROR) << "Cannot allocate activation primitive"; 2277- return RET_ERROR; 2278- } 2279- prim->activation_type = static_cast<decltype(prim->activation_type)>(act_param->type_); 2280- prim->alpha = act_param->alpha_; 2281- prim->min_val = act_param->min_val_; 2282- prim->max_val = act_param->max_val_; 2283- cnode->primitive->value.value = prim; 2284- return RET_OK; 2285-} 2286- 2287-static ImportReg reg(schema::PrimitiveType_Activation, ReturnNode<ActM>); 2288- 2289-ActGradM::ActGradM(Node *node) { 2290- CloneOpParam<ActivationParameter>(node->OpParam()); 2291- set_primitive(schema::PrimitiveType_ActivationGrad); 2292- set_name(node->name() + "/" + kGradName + "/actGrad"); 2293-} 2294- 2295-std::vector<EXPR *> ActGradM::Grad(EXPR *yt) { return {}; } 2296- 2297-int ActGradM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 2298- auto act_param = reinterpret_cast<const ActivationParameter *>(OpParam()); 2299- auto prim = new (std::nothrow) schema::ActivationGradT; 2300- if (prim == nullptr) { 2301- MS_LOG(ERROR) << "Cannot allocate activation grad primitive"; 2302- return RET_ERROR; 2303- } 2304- prim->activation_type = static_cast<decltype(prim->activation_type)>(act_param->type_); 2305- prim->alpha = act_param->alpha_; 2306- cnode->primitive->value.value = prim; 2307- return RET_OK; 2308-} 2309- 2310-static ImportReg regGrad(schema::PrimitiveType_ActivationGrad, ReturnNode<ActGradM>); 2311-namespace NN { 2312-Node *ReLU6() { 2313- auto r = new (std::nothrow) ActM(schema::ActivationType_RELU6); 2314- if (r == nullptr) { 2315- MS_LOG(ERROR) << "Cannot allocate relu6"; 2316- return nullptr; 2317- } 2318- r->set_name(Node::UniqueName("ReLU6")); 2319- return r; 2320-} 2321-Node *Sigmoid() { 2322- auto s = new (std::nothrow) ActM(schema::ActivationType_SIGMOID); 2323- if (s == nullptr) { 2324- MS_LOG(ERROR) << "Cannot allocate sigmoid"; 2325- return nullptr; 2326- } 2327- s->set_name(Node::UniqueName("Sigmoid")); 2328- return s; 2329-} 2330-Node *Relu() { 2331- auto r = new (std::nothrow) ActM(schema::ActivationType_RELU); 2332- if (r == nullptr) { 2333- MS_LOG(ERROR) << "Cannot allocate relu"; 2334- return nullptr; 2335- } 2336- r->set_name(r->UniqueName("Relu")); 2337- return r; 2338-} 2339-} // namespace NN 2340-} // namespace lite 2341-} // namespace mindspore 2342diff --git a/mindspore/lite/src/expression/ops/activation.h b/mindspore/lite/src/expression/ops/activation.h 2343deleted file mode 100644 2344index 14271c09..00000000 2345--- a/mindspore/lite/src/expression/ops/activation.h 2346+++ /dev/null 2347@@ -1,44 +0,0 @@ 2348-/** 2349- * Copyright 2022 Huawei Technologies Co., Ltd 2350- * 2351- * Licensed under the Apache License, Version 2.0 (the "License"); 2352- * you may not use this file except in compliance with the License. 2353- * You may obtain a copy of the License at 2354- * 2355- * http://www.apache.org/licenses/LICENSE-2.0 2356- * 2357- * Unless required by applicable law or agreed to in writing, software 2358- * distributed under the License is distributed on an "AS IS" BASIS, 2359- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2360- * See the License for the specific language governing permissions and 2361- * limitations under the License. 2362- */ 2363- 2364-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_ACTIVATION_H_ 2365-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_ACTIVATION_H_ 2366- 2367-#include <vector> 2368-#include <memory> 2369-#include "src/expression/net.h" 2370-#include "inner/model_generated.h" 2371- 2372-namespace mindspore { 2373-namespace lite { 2374-class ActM : public Node { 2375- public: 2376- ActM() = default; 2377- explicit ActM(schema::ActivationType type); 2378- std::vector<EXPR *> Grad(EXPR *expr) override; 2379- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 2380-}; 2381- 2382-class ActGradM : public Node { 2383- public: 2384- ActGradM() : Node() {} // for Import 2385- explicit ActGradM(Node *act); // for Grad 2386- std::vector<EXPR *> Grad(EXPR *expr) override; 2387- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 2388-}; 2389-} // namespace lite 2390-} // namespace mindspore 2391-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_ACTIVATION_H_ 2392diff --git a/mindspore/lite/src/expression/ops/adam.cc b/mindspore/lite/src/expression/ops/adam.cc 2393deleted file mode 100644 2394index 7fcfdc74..00000000 2395--- a/mindspore/lite/src/expression/ops/adam.cc 2396+++ /dev/null 2397@@ -1,142 +0,0 @@ 2398-/** 2399- * Copyright 2022 Huawei Technologies Co., Ltd 2400- * 2401- * Licensed under the Apache License, Version 2.0 (the "License"); 2402- * you may not use this file except in compliance with the License. 2403- * You may obtain a copy of the License at 2404- * 2405- * http://www.apache.org/licenses/LICENSE-2.0 2406- * 2407- * Unless required by applicable law or agreed to in writing, software 2408- * distributed under the License is distributed on an "AS IS" BASIS, 2409- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2410- * See the License for the specific language governing permissions and 2411- * limitations under the License. 2412- */ 2413- 2414-#include "src/expression/ops/adam.h" 2415-#include <memory> 2416-#include <set> 2417-#include <utility> 2418-#include "src/expression/ops.h" 2419-#include "src/expression/ops/assign.h" 2420-#include "src/expression/ops/arithmetic.h" 2421-#include "nnacl/fp32_grad/optimizer.h" 2422-#include "include/api/net.h" 2423-#include "src/litert/cxx_api/expression/node_impl.h" 2424- 2425-namespace mindspore { 2426-namespace NN { 2427-Node *Adam(std::shared_ptr<NodeSet> learn, const AdamConfig &cfg) { 2428- auto lite_node = lite::NN::Adam(std::move(learn->set_), cfg); 2429- return NodeImpl::Connect(lite_node); 2430-} 2431-} // namespace NN 2432- 2433-namespace lite { 2434-std::vector<EXPR *> AdamM::Clone(EXPR *grad, EXPR *weight) { 2435- auto adam = new (std::nothrow) AdamM(); 2436- if (adam == nullptr) { 2437- MS_LOG(ERROR) << "Cannot allocate adam"; 2438- return {}; 2439- } 2440- adam->set_name("optimizer-Adam"); 2441- adam->CloneOpParam<AdamParameter>(OpParam()); 2442- adam->update_name(weight->node()->name()); 2443- adam->set_primitive(primitive()); 2444- adam->expr()->SetSize(C10NUM); 2445- // setup weight and momentum 2446- adam->expr()->set_params(C0NUM, weight); 2447- auto dims = grad->dims(); 2448- auto m = adam->CreateWeights(dims, kNumberTypeFloat32, KHWC, Param::ZEROS, "m"); 2449- adam->expr()->set_params(C1NUM, m); 2450- auto v = adam->CreateWeights(dims, kNumberTypeFloat32, KHWC, Param::ZEROS, "v"); 2451- adam->expr()->set_params(C2NUM, v); 2452- // copy parameters 2453- for (int i = C3NUM; i < C9NUM; i++) { 2454- adam->expr()->set_params(i, this->input(i)); 2455- } 2456- adam->expr()->set_params(C9NUM, grad); 2457- return (*adam)(adam->inputs()); 2458-} 2459- 2460-AdamM::AdamM(std::set<Node *> &&learn, const AdamConfig &cfg) { 2461- auto op_param = reinterpret_cast<AdamParameter *>(malloc(sizeof(AdamParameter))); 2462- if (op_param == nullptr) { 2463- MS_LOG(ERROR) << " cannot allocate ActivationParameter"; 2464- return; 2465- } 2466- AssignLearn(std::move(learn)); 2467- memset(op_param, 0, sizeof(AdamParameter)); 2468- op_param->use_nesterov_ = cfg.use_nesterov_; 2469- SetOpParam(op_param); 2470- set_primitive(schema::PrimitiveType_Adam); 2471- set_name("optimizer-Adam"); 2472- // Adam Network 2473- expr()->SetSize(C10NUM); 2474- auto assign1 = new (std::nothrow) AssignM(0); 2475- if (assign1 == nullptr) { 2476- MS_LOG(ERROR) << "Cannot allocate assign"; 2477- return; 2478- } 2479- PushOp(assign1); 2480- auto assign2 = new (std::nothrow) AssignM(0); 2481- if (assign2 == nullptr) { 2482- MS_LOG(ERROR) << "Cannot allocate assign"; 2483- return; 2484- } 2485- PushOp(assign2); 2486- auto mul1 = NN::Mul(); 2487- if (mul1 == nullptr) { 2488- MS_LOG(ERROR) << "Cannot allocate mul"; 2489- return; 2490- } 2491- PushOp(mul1); 2492- auto mul2 = NN::Mul(); 2493- if (mul2 == nullptr) { 2494- MS_LOG(ERROR) << "Cannot allocate mul"; 2495- return; 2496- } 2497- PushOp(mul2); 2498- auto tmp = 1.0f; 2499- mul1->CreateConstTensor(C0NUM, {1}, kNumberTypeFloat32, KHWC, "beta1-power", &tmp); 2500- mul1->CreateConstTensor(C1NUM, {1}, kNumberTypeFloat32, KHWC, "beta1-data", &cfg.beta1_); 2501- auto o1 = (*mul1)({}); 2502- assign1_ = (*assign1)({mul1->input(0), o1.front()}).front(); 2503- mul2->CreateConstTensor(C0NUM, {1}, kNumberTypeFloat32, KHWC, "beta2-power", &tmp); 2504- mul2->CreateConstTensor(C1NUM, {1}, kNumberTypeFloat32, KHWC, "beta2-data", &cfg.beta2_); 2505- auto o2 = (*mul2)({}); 2506- assign2_ = (*assign2)({mul2->input(0), o2.front()}).front(); 2507- expr()->set_params(C3NUM, o1.front()); 2508- expr()->set_params(C4NUM, o2.front()); 2509- CreateConstTensor(C5NUM, {1}, kNumberTypeFloat32, KHWC, "learning-rate", &cfg.learning_rate_); 2510- CreateConstTensor(C6NUM, {1}, kNumberTypeFloat32, KHWC, "beta1", &cfg.beta1_); 2511- CreateConstTensor(C7NUM, {1}, kNumberTypeFloat32, KHWC, "beta2", &cfg.beta2_); 2512- CreateConstTensor(C8NUM, {1}, kNumberTypeFloat32, KHWC, "epsilon", &cfg.eps_); 2513-} 2514- 2515-int AdamM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 2516- auto param = reinterpret_cast<const AdamParameter *>(OpParam()); 2517- auto prim = new (std::nothrow) schema::AdamT; 2518- if (prim == nullptr) { 2519- MS_LOG(ERROR) << "Cannot allocate " << cnode->name; 2520- return RET_ERROR; 2521- } 2522- prim->use_nesterov = param->use_nesterov_; 2523- prim->use_locking = false; 2524- cnode->primitive->value.value = prim; 2525- return RET_OK; 2526-} 2527- 2528-namespace NN { 2529-Node *Adam(std::set<Node *> &&learn, const AdamConfig &cfg) { 2530- auto a = new (std::nothrow) AdamM(std::move(learn), cfg); 2531- if (a == nullptr) { 2532- MS_LOG(ERROR) << "Cannot allocate adam"; 2533- return nullptr; 2534- } 2535- return a; 2536-} 2537-} // namespace NN 2538-} // namespace lite 2539-} // namespace mindspore 2540diff --git a/mindspore/lite/src/expression/ops/adam.h b/mindspore/lite/src/expression/ops/adam.h 2541deleted file mode 100644 2542index 58e44def..00000000 2543--- a/mindspore/lite/src/expression/ops/adam.h 2544+++ /dev/null 2545@@ -1,46 +0,0 @@ 2546-/** 2547- * Copyright 2022 Huawei Technologies Co., Ltd 2548- * 2549- * Licensed under the Apache License, Version 2.0 (the "License"); 2550- * you may not use this file except in compliance with the License. 2551- * You may obtain a copy of the License at 2552- * 2553- * http://www.apache.org/licenses/LICENSE-2.0 2554- * 2555- * Unless required by applicable law or agreed to in writing, software 2556- * distributed under the License is distributed on an "AS IS" BASIS, 2557- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2558- * See the License for the specific language governing permissions and 2559- * limitations under the License. 2560- */ 2561- 2562-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_ADAM_H_ 2563-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_ADAM_H_ 2564- 2565-#include <vector> 2566-#include <set> 2567-#include <memory> 2568-#include "include/api/net.h" 2569-#include "src/expression/net.h" 2570-#include "inner/model_generated.h" 2571- 2572-namespace mindspore { 2573-namespace lite { 2574-class AdamM : public Node { 2575- public: 2576- AdamM() = default; 2577- AdamM(std::set<Node *> &&learn, const AdamConfig &cfg); 2578- std::vector<EXPR *> Clone(EXPR *grad, EXPR *weight) override; 2579- void AddNetOutput(std::vector<EXPR *> *output) override { 2580- output->push_back(assign1_); 2581- output->push_back(assign2_); 2582- } 2583- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 2584- 2585- private: 2586- EXPR *assign1_{nullptr}; 2587- EXPR *assign2_{nullptr}; 2588-}; 2589-} // namespace lite 2590-} // namespace mindspore 2591-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_ADAM_H_ 2592diff --git a/mindspore/lite/src/expression/ops/addn.cc b/mindspore/lite/src/expression/ops/addn.cc 2593deleted file mode 100644 2594index bd614f1d..00000000 2595--- a/mindspore/lite/src/expression/ops/addn.cc 2596+++ /dev/null 2597@@ -1,42 +0,0 @@ 2598-/** 2599- * Copyright 2022 Huawei Technologies Co., Ltd 2600- * 2601- * Licensed under the Apache License, Version 2.0 (the "License"); 2602- * you may not use this file except in compliance with the License. 2603- * You may obtain a copy of the License at 2604- * 2605- * http://www.apache.org/licenses/LICENSE-2.0 2606- * 2607- * Unless required by applicable law or agreed to in writing, software 2608- * distributed under the License is distributed on an "AS IS" BASIS, 2609- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2610- * See the License for the specific language governing permissions and 2611- * limitations under the License. 2612- */ 2613- 2614-#include "src/expression/ops/addn.h" 2615- 2616-namespace mindspore { 2617-namespace lite { 2618-AddN::AddN(int dummy) { 2619- auto op_param = calloc(1, sizeof(OpParameter)); 2620- if (op_param == nullptr) { 2621- MS_LOG(ERROR) << "Cannot allocate parameter "; 2622- return; 2623- } 2624- set_name(UniqueName("addN")); 2625- SetOpParam(op_param); 2626- set_primitive(schema::PrimitiveType_AddN); 2627-} 2628- 2629-int AddN::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 2630- auto prim = new (std::nothrow) schema::AddNT; 2631- if (prim == nullptr) { 2632- MS_LOG(ERROR) << "Cannot allocate primitive"; 2633- return RET_ERROR; 2634- } 2635- cnode->primitive->value.value = prim; 2636- return RET_OK; 2637-} 2638-} // namespace lite 2639-} // namespace mindspore 2640diff --git a/mindspore/lite/src/expression/ops/addn.h b/mindspore/lite/src/expression/ops/addn.h 2641deleted file mode 100644 2642index 3ed96319..00000000 2643--- a/mindspore/lite/src/expression/ops/addn.h 2644+++ /dev/null 2645@@ -1,34 +0,0 @@ 2646-/** 2647- * Copyright 2022 Huawei Technologies Co., Ltd 2648- * 2649- * Licensed under the Apache License, Version 2.0 (the "License"); 2650- * you may not use this file except in compliance with the License. 2651- * You may obtain a copy of the License at 2652- * 2653- * http://www.apache.org/licenses/LICENSE-2.0 2654- * 2655- * Unless required by applicable law or agreed to in writing, software 2656- * distributed under the License is distributed on an "AS IS" BASIS, 2657- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2658- * See the License for the specific language governing permissions and 2659- * limitations under the License. 2660- */ 2661- 2662-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_ADDN_H_ 2663-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_ADDN_H_ 2664- 2665-#include <memory> 2666-#include "src/expression/node.h" 2667-#include "inner/model_generated.h" 2668- 2669-namespace mindspore { 2670-namespace lite { 2671-class AddN : public Node { 2672- public: 2673- explicit AddN(int dummy); 2674- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 2675-}; 2676-} // namespace lite 2677-} // namespace mindspore 2678- 2679-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_ADDN_H_ 2680diff --git a/mindspore/lite/src/expression/ops/arithmetic.cc b/mindspore/lite/src/expression/ops/arithmetic.cc 2681deleted file mode 100644 2682index 89af36d6..00000000 2683--- a/mindspore/lite/src/expression/ops/arithmetic.cc 2684+++ /dev/null 2685@@ -1,223 +0,0 @@ 2686-/** 2687- * Copyright 2022 Huawei Technologies Co., Ltd 2688- * 2689- * Licensed under the Apache License, Version 2.0 (the "License"); 2690- * you may not use this file except in compliance with the License. 2691- * You may obtain a copy of the License at 2692- * 2693- * http://www.apache.org/licenses/LICENSE-2.0 2694- * 2695- * Unless required by applicable law or agreed to in writing, software 2696- * distributed under the License is distributed on an "AS IS" BASIS, 2697- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2698- * See the License for the specific language governing permissions and 2699- * limitations under the License. 2700- */ 2701- 2702-#include "src/expression/ops/arithmetic.h" 2703-#include <memory> 2704-#include "src/expression/ops/reduce.h" 2705-#include "src/expression/ops/reshape.h" 2706-#include "src/expression/ops_utils.h" 2707-#include "src/expression/ops/arithmetic_self.h" 2708-#include "src/expression/ops.h" 2709-#include "nnacl/arithmetic_parameter.h" 2710-#include "src/expression/import.h" 2711-#include "src/litert/cxx_api/expression/node_impl.h" 2712- 2713-namespace mindspore { 2714-namespace lite { 2715-// Common Arithmetic Functionality 2716-ArithmeticM::ArithmeticM(schema::PrimitiveType type) : Node() { 2717- auto op_param = malloc(sizeof(ArithmeticParameter)); 2718- if (op_param == nullptr) { 2719- MS_LOG(ERROR) << " cannot allocate ActivationParameter"; 2720- return; 2721- } 2722- SetOpParam(op_param); 2723- expr()->SetSize(C2NUM); 2724- set_primitive(type); 2725-} 2726- 2727-std::vector<EXPR *> ArithmeticM::binop_grad_common(EXPR *x, EXPR *y, EXPR *dx, EXPR *dy) { 2728- auto shape_of_x = x->dims(); 2729- auto shape_of_y = y->dims(); 2730- auto reduce_dx = dx; 2731- auto reduce_dy = dy; 2732- auto rx = (BroadcastGradientArgs(shape_of_x, shape_of_y))(); 2733- if (rx[0].size()) { 2734- auto reduce_sum = NN::ReduceSum(false, rx[0]); 2735- PushOp(reduce_sum); 2736- reduce_dx = (*reduce_sum)({reduce_dx}).front(); 2737- auto reshape = NN::Reshape(shape_of_x); 2738- PushOp(reshape); 2739- reduce_dx = (*reshape)({reduce_dx}).front(); 2740- } 2741- if (rx[1].size()) { 2742- auto reduce_sum = NN::ReduceSum(false, rx[1]); 2743- PushOp(reduce_sum); 2744- reduce_dy = (*reduce_sum)({reduce_dy}).front(); 2745- auto reshape = NN::Reshape(shape_of_y); 2746- PushOp(reshape); 2747- reduce_dy = (*reshape)({reduce_dy}).front(); 2748- } 2749- std::vector<EXPR *> out = {reduce_dx, reduce_dy}; 2750- return out; 2751-} 2752- 2753-// Add Op 2754-AddM::AddM(int dummy) : ArithmeticM(schema::PrimitiveType_AddFusion) { set_name(UniqueName("Add")); } 2755- 2756-std::vector<EXPR *> AddM::Grad(EXPR *yt) { return binop_grad_common(input(0), input(1), yt, yt); } 2757- 2758-int AddM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 2759- auto prim = new (std::nothrow) schema::AddFusionT; 2760- if (prim == nullptr) { 2761- MS_LOG(ERROR) << "Cannot allocate prim"; 2762- return RET_ERROR; 2763- } 2764- prim->activation_type = schema::ActivationType_NO_ACTIVATION; 2765- cnode->primitive->value.value = prim; 2766- return RET_OK; 2767-} 2768- 2769-static ImportReg AddReg(schema::PrimitiveType_AddFusion, ReturnNode<AddM>); 2770- 2771-// Div op 2772-DivM::DivM(int dummy) : ArithmeticM(schema::PrimitiveType_RealDiv) { set_name(UniqueName("RealDiv")); } 2773-std::vector<EXPR *> DivM::Grad(EXPR *yt) { 2774- auto x = input(0); 2775- auto y = input(1); 2776- auto o = output(0); 2777- auto div_op = NN::Div(); 2778- if (div_op == nullptr) { 2779- MS_LOG(ERROR) << "Cannot allocate div_op"; 2780- return {}; 2781- } 2782- PushOp(div_op); 2783- auto neg_op = NN::Neg(); 2784- if (neg_op == nullptr) { 2785- MS_LOG(ERROR) << "Cannot allocate neg_op"; 2786- return {}; 2787- } 2788- PushOp(neg_op); 2789- auto mul_op = NN::Mul(); 2790- if (mul_op == nullptr) { 2791- MS_LOG(ERROR) << "Cannot allocate mul_op"; 2792- return {}; 2793- } 2794- PushOp(mul_op); 2795- auto bc_x = (*div_op)({yt, y}).front(); 2796- auto bc_y = (*neg_op)((*mul_op)({bc_x, o})).front(); 2797- return binop_grad_common(x, y, bc_x, bc_y); 2798-} 2799-int DivM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 2800- auto prim = new (std::nothrow) schema::RealDivT; 2801- if (prim == nullptr) { 2802- MS_LOG(ERROR) << "Cannot allocate primitive"; 2803- return RET_ERROR; 2804- } 2805- cnode->primitive->value.value = prim; 2806- return RET_OK; 2807-} 2808-static ImportReg DivReg(schema::PrimitiveType_DivFusion, ReturnNode<DivM>); 2809- 2810-// Mul op 2811-MulM::MulM(int dummy) : ArithmeticM(schema::PrimitiveType_MulFusion) { set_name(UniqueName("Mul")); } 2812- 2813-std::vector<EXPR *> MulM::Grad(EXPR *yt) { 2814- auto mul_dx = NN::Mul(); 2815- if (mul_dx == nullptr) { 2816- MS_LOG(ERROR) << "Cannot allocate mul dx"; 2817- return {}; 2818- } 2819- PushOp(mul_dx); 2820- auto mul_dy = NN::Mul(); 2821- if (mul_dy == nullptr) { 2822- MS_LOG(ERROR) << "Cannot allocate mul_dy"; 2823- return {}; 2824- } 2825- PushOp(mul_dy); 2826- auto x = input(0); 2827- auto y = input(1); 2828- auto bc_dx = (*mul_dx)({y, yt}).front(); 2829- auto bc_dy = (*mul_dy)({x, yt}).front(); 2830- return binop_grad_common(x, y, bc_dx, bc_dy); 2831-} 2832- 2833-int MulM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 2834- auto prim = new (std::nothrow) schema::MulFusionT; 2835- if (prim == nullptr) { 2836- MS_LOG(ERROR) << "Cannot allocate prim"; 2837- return RET_ERROR; 2838- } 2839- prim->activation_type = schema::ActivationType_NO_ACTIVATION; 2840- cnode->primitive->value.value = prim; 2841- return RET_OK; 2842-} 2843-static ImportReg MulReg(schema::PrimitiveType_MulFusion, ReturnNode<MulM>); 2844- 2845-// Sub op 2846-SubM::SubM(int dummy) : ArithmeticM(schema::PrimitiveType_SubFusion) { set_name(UniqueName("Sub")); } 2847- 2848-std::vector<EXPR *> SubM::Grad(EXPR *yt) { 2849- auto x = input(0); 2850- auto y = input(1); 2851- auto neg = NN::Neg(); 2852- if (neg == nullptr) { 2853- MS_LOG(ERROR) << "Cannot allocate neg"; 2854- return {}; 2855- } 2856- PushOp(neg); 2857- auto neg_grad = (*neg)({yt}).front(); 2858- return binop_grad_common(x, y, yt, neg_grad); 2859-} 2860-int SubM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 2861- auto prim = new (std::nothrow) schema::SubFusionT; 2862- if (prim == nullptr) { 2863- MS_LOG(ERROR) << "Cannot allocate prim"; 2864- return RET_ERROR; 2865- } 2866- prim->activation_type = schema::ActivationType_NO_ACTIVATION; 2867- cnode->primitive->value.value = prim; 2868- return RET_OK; 2869-} 2870-static ImportReg SubReg(schema::PrimitiveType_SubFusion, ReturnNode<SubM>); 2871- 2872-namespace NN { 2873-Node *Add() { 2874- auto a = new (std::nothrow) AddM(0); 2875- if (a == nullptr) { 2876- MS_LOG(ERROR) << "Cannot allocate a"; 2877- return nullptr; 2878- } 2879- return a; 2880-} 2881-Node *Sub() { 2882- auto a = new (std::nothrow) SubM(0); 2883- if (a == nullptr) { 2884- MS_LOG(ERROR) << "Cannot allocate a"; 2885- return nullptr; 2886- } 2887- return a; 2888-} 2889- 2890-Node *Mul() { 2891- auto a = new (std::nothrow) MulM(0); 2892- if (a == nullptr) { 2893- MS_LOG(ERROR) << "Cannot allocate a"; 2894- return nullptr; 2895- } 2896- return a; 2897-} 2898-Node *Div() { 2899- auto a = new (std::nothrow) DivM(0); 2900- if (a == nullptr) { 2901- MS_LOG(ERROR) << "Cannot allocate a"; 2902- return nullptr; 2903- } 2904- return a; 2905-} 2906-} // namespace NN 2907-} // namespace lite 2908-} // namespace mindspore 2909diff --git a/mindspore/lite/src/expression/ops/arithmetic.h b/mindspore/lite/src/expression/ops/arithmetic.h 2910deleted file mode 100644 2911index b1509245..00000000 2912--- a/mindspore/lite/src/expression/ops/arithmetic.h 2913+++ /dev/null 2914@@ -1,75 +0,0 @@ 2915-/** 2916- * Copyright 2022 Huawei Technologies Co., Ltd 2917- * 2918- * Licensed under the Apache License, Version 2.0 (the "License"); 2919- * you may not use this file except in compliance with the License. 2920- * You may obtain a copy of the License at 2921- * 2922- * http://www.apache.org/licenses/LICENSE-2.0 2923- * 2924- * Unless required by applicable law or agreed to in writing, software 2925- * distributed under the License is distributed on an "AS IS" BASIS, 2926- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2927- * See the License for the specific language governing permissions and 2928- * limitations under the License. 2929- */ 2930- 2931-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_ARITHMETIC_H_ 2932-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_ARITHMETIC_H_ 2933- 2934-#include <vector> 2935-#include <memory> 2936-#include "src/expression/net.h" 2937-#include "inner/model_generated.h" 2938- 2939-namespace mindspore { 2940-namespace lite { 2941-class ArithmeticM : public Node { 2942- public: 2943- ArithmeticM() = default; 2944- explicit ArithmeticM(schema::PrimitiveType type); 2945- 2946- protected: 2947- std::vector<EXPR *> binop_grad_common(EXPR *x, EXPR *y, EXPR *dx, EXPR *dy); 2948-}; 2949- 2950-class AddM : public ArithmeticM { 2951- public: 2952- AddM() = default; 2953- explicit AddM(int dummy); 2954- std::vector<EXPR *> Grad(EXPR *expr) override; 2955- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 2956-}; 2957- 2958-class DivM : public ArithmeticM { 2959- public: 2960- DivM() = default; 2961- explicit DivM(int dummy); 2962- std::vector<EXPR *> Grad(EXPR *expr) override; 2963- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 2964-}; 2965- 2966-class MulM : public ArithmeticM { 2967- public: 2968- MulM() = default; 2969- explicit MulM(int dummy); 2970- std::vector<EXPR *> Grad(EXPR *expr) override; 2971- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 2972-}; 2973- 2974-class SubM : public ArithmeticM { 2975- public: 2976- SubM() = default; 2977- explicit SubM(int dummy); 2978- std::vector<EXPR *> Grad(EXPR *expr) override; 2979- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 2980-}; 2981-namespace NN { 2982-Node *Add(); 2983-Node *Sub(); 2984-Node *Mul(); 2985-Node *Div(); 2986-} // namespace NN 2987-} // namespace lite 2988-} // namespace mindspore 2989-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_ARITHMETIC_H_ 2990diff --git a/mindspore/lite/src/expression/ops/arithmetic_self.cc b/mindspore/lite/src/expression/ops/arithmetic_self.cc 2991deleted file mode 100644 2992index e5a84f75..00000000 2993--- a/mindspore/lite/src/expression/ops/arithmetic_self.cc 2994+++ /dev/null 2995@@ -1,72 +0,0 @@ 2996-/** 2997- * Copyright 2022 Huawei Technologies Co., Ltd 2998- * 2999- * Licensed under the Apache License, Version 2.0 (the "License"); 3000- * you may not use this file except in compliance with the License. 3001- * You may obtain a copy of the License at 3002- * 3003- * http://www.apache.org/licenses/LICENSE-2.0 3004- * 3005- * Unless required by applicable law or agreed to in writing, software 3006- * distributed under the License is distributed on an "AS IS" BASIS, 3007- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 3008- * See the License for the specific language governing permissions and 3009- * limitations under the License. 3010- */ 3011- 3012-#include "src/expression/ops/arithmetic_self.h" 3013-#include <memory> 3014-#include "src/expression/ops_utils.h" 3015-#include "src/expression/ops.h" 3016-#include "nnacl/arithmetic_self_parameter.h" 3017-#include "src/expression/import.h" 3018- 3019-namespace mindspore { 3020-namespace lite { 3021-// Common Arithmetic Self Functionality 3022-ArithmeticSelfM::ArithmeticSelfM(schema::PrimitiveType type) : Node() { 3023- auto op_param = malloc(sizeof(ArithmeticSelfParameter)); 3024- if (op_param == nullptr) { 3025- MS_LOG(ERROR) << " cannot allocate ArithmeticSelfParameter"; 3026- return; 3027- } 3028- memset(op_param, 0, sizeof(ArithmeticSelfParameter)); 3029- SetOpParam(op_param); 3030- expr()->SetSize(C1NUM); 3031- set_primitive(type); 3032-} 3033- 3034-// NEG OP 3035-NegM::NegM(int dummy) : ArithmeticSelfM(schema::PrimitiveType_NegGrad) { set_name(UniqueName("Neg")); } 3036- 3037-std::vector<EXPR *> NegM::Grad(EXPR *yt) { 3038- auto grad_neg = new (std::nothrow) NegM(0); 3039- if (grad_neg == nullptr) { 3040- MS_LOG(ERROR) << "Cannot allocate neg gradient"; 3041- return {}; 3042- } 3043- return (*grad_neg)({yt}); 3044-} 3045- 3046-int NegM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 3047- auto prim = new (std::nothrow) schema::NegT; 3048- if (prim == nullptr) { 3049- MS_LOG(ERROR) << "Cannot allocate primitive"; 3050- return RET_ERROR; 3051- } 3052- cnode->primitive->value.value = prim; 3053- return RET_OK; 3054-} 3055- 3056-namespace NN { 3057-Node *Neg() { 3058- auto a = new (std::nothrow) NegM(0); 3059- if (a == nullptr) { 3060- MS_LOG(ERROR) << "Cannot allocate neg node"; 3061- return nullptr; 3062- } 3063- return a; 3064-} 3065-} // namespace NN 3066-} // namespace lite 3067-} // namespace mindspore 3068diff --git a/mindspore/lite/src/expression/ops/arithmetic_self.h b/mindspore/lite/src/expression/ops/arithmetic_self.h 3069deleted file mode 100644 3070index e64ba024..00000000 3071--- a/mindspore/lite/src/expression/ops/arithmetic_self.h 3072+++ /dev/null 3073@@ -1,46 +0,0 @@ 3074-/** 3075- * Copyright 2022 Huawei Technologies Co., Ltd 3076- * 3077- * Licensed under the Apache License, Version 2.0 (the "License"); 3078- * you may not use this file except in compliance with the License. 3079- * You may obtain a copy of the License at 3080- * 3081- * http://www.apache.org/licenses/LICENSE-2.0 3082- * 3083- * Unless required by applicable law or agreed to in writing, software 3084- * distributed under the License is distributed on an "AS IS" BASIS, 3085- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 3086- * See the License for the specific language governing permissions and 3087- * limitations under the License. 3088- */ 3089- 3090-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_ARITHMETIC_SELF_H_ 3091-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_ARITHMETIC_SELF_H_ 3092- 3093-#include <vector> 3094-#include <memory> 3095-#include "src/expression/net.h" 3096-#include "inner/model_generated.h" 3097- 3098-namespace mindspore { 3099-namespace lite { 3100-class ArithmeticSelfM : public Node { 3101- public: 3102- explicit ArithmeticSelfM(schema::PrimitiveType type); 3103- 3104- protected: 3105-}; 3106- 3107-class NegM : public ArithmeticSelfM { 3108- public: 3109- explicit NegM(int dummy); 3110- std::vector<EXPR *> Grad(EXPR *expr) override; 3111- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 3112-}; 3113- 3114-namespace NN { 3115-Node *Neg(); 3116-} 3117-} // namespace lite 3118-} // namespace mindspore 3119-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_ARITHMETIC_SELF_H_ 3120diff --git a/mindspore/lite/src/expression/ops/assign.cc b/mindspore/lite/src/expression/ops/assign.cc 3121deleted file mode 100644 3122index acf10950..00000000 3123--- a/mindspore/lite/src/expression/ops/assign.cc 3124+++ /dev/null 3125@@ -1,60 +0,0 @@ 3126-/** 3127- * Copyright 2022 Huawei Technologies Co., Ltd 3128- * 3129- * Licensed under the Apache License, Version 2.0 (the "License"); 3130- * you may not use this file except in compliance with the License. 3131- * You may obtain a copy of the License at 3132- * 3133- * http://www.apache.org/licenses/LICENSE-2.0 3134- * 3135- * Unless required by applicable law or agreed to in writing, software 3136- * distributed under the License is distributed on an "AS IS" BASIS, 3137- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 3138- * See the License for the specific language governing permissions and 3139- * limitations under the License. 3140- */ 3141- 3142-#include "src/expression/ops/assign.h" 3143-#include <memory> 3144-#include "nnacl/reshape_parameter.h" 3145-#include "src/expression/import.h" 3146- 3147-namespace mindspore { 3148-namespace lite { 3149-AssignM::AssignM(int dummy) { 3150- auto op_param = calloc(1, sizeof(OpParameter)); 3151- if (op_param == nullptr) { 3152- MS_LOG(ERROR) << " cannot allocate ReshapeParameter"; 3153- return; 3154- } 3155- expr()->SetSize(C2NUM); 3156- SetOpParam(op_param); 3157- set_primitive(schema::PrimitiveType_Assign); 3158- set_name(UniqueName("Assign")); 3159-} 3160- 3161-int AssignM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 3162- auto prim = new (std::nothrow) schema::AssignT; 3163- if (prim == nullptr) { 3164- MS_LOG(ERROR) << "Cannot allocate primitive"; 3165- return RET_ERROR; 3166- } 3167- cnode->primitive->value.value = prim; 3168- return RET_OK; 3169-} 3170- 3171-static ImportReg reg(schema::PrimitiveType_Reshape, ReturnNode<AssignM>); 3172- 3173-namespace NN { 3174-Node *Assign() { 3175- auto node = new (std::nothrow) AssignM(0); 3176- if (node == nullptr) { 3177- MS_LOG(ERROR) << "Cannot allocate node"; 3178- return nullptr; 3179- } 3180- node->set_name(Node::UniqueName("Assign")); 3181- return node; 3182-} 3183-} // namespace NN 3184-} // namespace lite 3185-} // namespace mindspore 3186diff --git a/mindspore/lite/src/expression/ops/assign.h b/mindspore/lite/src/expression/ops/assign.h 3187deleted file mode 100644 3188index 0dfd2c67..00000000 3189--- a/mindspore/lite/src/expression/ops/assign.h 3190+++ /dev/null 3191@@ -1,35 +0,0 @@ 3192-/** 3193- * Copyright 2022 Huawei Technologies Co., Ltd 3194- * 3195- * Licensed under the Apache License, Version 2.0 (the "License"); 3196- * you may not use this file except in compliance with the License. 3197- * You may obtain a copy of the License at 3198- * 3199- * http://www.apache.org/licenses/LICENSE-2.0 3200- * 3201- * Unless required by applicable law or agreed to in writing, software 3202- * distributed under the License is distributed on an "AS IS" BASIS, 3203- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 3204- * See the License for the specific language governing permissions and 3205- * limitations under the License. 3206- */ 3207- 3208-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_ASSIGN_H_ 3209-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_ASSIGN_H_ 3210- 3211-#include <vector> 3212-#include <memory> 3213-#include "src/expression/net.h" 3214-#include "inner/model_generated.h" 3215- 3216-namespace mindspore { 3217-namespace lite { 3218-class AssignM : public Node { 3219- public: 3220- AssignM() = default; 3221- explicit AssignM(int dummy); 3222- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 3223-}; 3224-} // namespace lite 3225-} // namespace mindspore 3226-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_ASSIGN_H_ 3227diff --git a/mindspore/lite/src/expression/ops/batchnorm.cc b/mindspore/lite/src/expression/ops/batchnorm.cc 3228deleted file mode 100644 3229index ec2bc7b5..00000000 3230--- a/mindspore/lite/src/expression/ops/batchnorm.cc 3231+++ /dev/null 3232@@ -1,135 +0,0 @@ 3233-/** 3234- * Copyright 2022 Huawei Technologies Co., Ltd 3235- * 3236- * Licensed under the Apache License, Version 2.0 (the "License"); 3237- * you may not use this file except in compliance with the License. 3238- * You may obtain a copy of the License at 3239- * 3240- * http://www.apache.org/licenses/LICENSE-2.0 3241- * 3242- * Unless required by applicable law or agreed to in writing, software 3243- * distributed under the License is distributed on an "AS IS" BASIS, 3244- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 3245- * See the License for the specific language governing permissions and 3246- * limitations under the License. 3247- */ 3248- 3249-#include "src/expression/ops/batchnorm.h" 3250-#include <memory> 3251-#include "nnacl/batchnorm_parameter.h" 3252-#include "nnacl/fp32_grad/batch_norm_grad.h" 3253-#include "src/expression/import.h" 3254-#include "src/expression/ops.h" 3255-#include "src/litert/cxx_api/expression/node_impl.h" 3256- 3257-namespace mindspore { 3258-namespace lite { 3259-BatchNorm2dM::BatchNorm2dM(int outp, float momentum, float epsilon) { 3260- constexpr int bn_inputs = 5; 3261- constexpr int bn_outputs = 5; 3262- 3263- auto op_param = calloc(1, sizeof(BatchNormParameter)); 3264- if (op_param == nullptr) { 3265- MS_LOG(ERROR) << " cannot allocate BatchNormParameter"; 3266- return; 3267- } 3268- expr()->SetSize(bn_inputs); 3269- set_name(UniqueName("BatchNorm2D")); 3270- auto bn_param = reinterpret_cast<BatchNormParameter *>(op_param); 3271- bn_param->channel_ = outp; 3272- bn_param->momentum_ = momentum; 3273- bn_param->epsilon_ = epsilon; 3274- SetOpParam(op_param); 3275- set_primitive(schema::PrimitiveType_FusedBatchNorm); 3276- std::vector<int> dims = {outp}; 3277- auto scale = Node::CreateWeights(dims, kNumberTypeFloat32, KHWC, Param::Mode::ONES, "scale"); 3278- expr()->set_params(C1NUM, scale); 3279- auto offset = Node::CreateWeights(dims, kNumberTypeFloat32, KHWC, Param::Mode::ZEROS, "offset"); 3280- expr()->set_params(C2NUM, offset); 3281- auto mean = Node::CreateWeights(dims, kNumberTypeFloat32, KHWC, Param::Mode::ZEROS, "mean"); 3282- expr()->set_params(C3NUM, mean); 3283- auto var = Node::CreateWeights(dims, kNumberTypeFloat32, KHWC, Param::Mode::ONES, "var"); 3284- expr()->set_params(C4NUM, var); 3285- SetOutputs(bn_outputs); 3286- SetLearn(); 3287-} 3288- 3289-BatchNorm2dGradM::BatchNorm2dGradM(BatchNorm2dM *bn_node) : Node() { 3290- auto op_param = calloc(1, sizeof(BNGradParameter)); 3291- if (op_param == nullptr) { 3292- MS_LOG(ERROR) << " cannot allocate BNGradParameter"; 3293- return; 3294- } 3295- expr()->SetSize(C6NUM); 3296- set_name(bn_node->name() + "/" + kGradName + "/bnGrad"); 3297- auto bn_grad_param = reinterpret_cast<BNGradParameter *>(op_param); 3298- auto bn_param = reinterpret_cast<BatchNormParameter *>(bn_node->OpParam()); 3299- bn_param->is_training_ = true; 3300- bn_grad_param->epsilon_ = bn_param->epsilon_; 3301- bn_grad_param->is_training_ = true; 3302- SetOpParam(op_param); 3303- set_primitive(schema::PrimitiveType_BatchNormGrad); 3304- EXPR e(this); 3305- e.SetSize(0); 3306- // Dgamma 3307- expr_.emplace_back(e); 3308- // Doffset 3309- expr_.emplace_back(e); 3310-} 3311- 3312-std::vector<EXPR *> BatchNorm2dM::Grad(EXPR *yt) { 3313- auto bn_grad_node = new (std::nothrow) BatchNorm2dGradM(this); 3314- if (bn_grad_node == nullptr) { 3315- MS_LOG(ERROR) << "Cannot allocate batchnorm grad"; 3316- return {}; 3317- } 3318- PushOp(bn_grad_node); 3319- auto bn_grad = (*bn_grad_node)({yt, input(0), output(1), output(3), output(4), output(2)}); 3320- return bn_grad; 3321-} 3322- 3323-int BatchNorm2dM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 3324- auto bn_param = reinterpret_cast<const BatchNormParameter *>(OpParam()); 3325- auto prim = new (std::nothrow) schema::FusedBatchNormT; 3326- if (prim == nullptr) { 3327- MS_LOG(ERROR) << "Cannot allocate primitive"; 3328- return RET_ERROR; 3329- } 3330- prim->epsilon = bn_param->epsilon_; 3331- prim->momentum = bn_param->momentum_; 3332- prim->mode = (bn_param->is_training_ == false) ? 0 : 1; 3333- cnode->primitive->value.value = prim; 3334- return RET_OK; 3335-} 3336- 3337-void BatchNorm2dM::SetLearn() { 3338- AddLearn(input(C1NUM)->node()); 3339- AddLearn(input(C2NUM)->node()); 3340-} 3341- 3342-int BatchNorm2dGradM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 3343- auto param = reinterpret_cast<const BNGradParameter *>(OpParam()); 3344- auto prim = new (std::nothrow) schema::BatchNormGradT; 3345- if (prim == nullptr) { 3346- MS_LOG(ERROR) << "Cannot allocate primitive"; 3347- return RET_ERROR; 3348- } 3349- prim->epsilon = param->epsilon_; 3350- prim->is_training = param->is_training_; 3351- cnode->primitive->value.value = prim; 3352- return RET_OK; 3353-} 3354- 3355-static ImportReg reg(schema::PrimitiveType_FusedBatchNorm, ReturnNode<BatchNorm2dM>); 3356-namespace NN { 3357-Node *BatchNorm2D(int outp, float momentum, float epsilon) { 3358- auto node = new (std::nothrow) BatchNorm2dM(outp, momentum, epsilon); 3359- if (node == nullptr) { 3360- MS_LOG(ERROR) << "Cannot allocate node"; 3361- return nullptr; 3362- } 3363- return node; 3364-} 3365-} // namespace NN 3366-} // namespace lite 3367-} // namespace mindspore 3368diff --git a/mindspore/lite/src/expression/ops/batchnorm.h b/mindspore/lite/src/expression/ops/batchnorm.h 3369deleted file mode 100644 3370index 2891f35a..00000000 3371--- a/mindspore/lite/src/expression/ops/batchnorm.h 3372+++ /dev/null 3373@@ -1,43 +0,0 @@ 3374-/** 3375- * Copyright 2022 Huawei Technologies Co., Ltd 3376- * 3377- * Licensed under the Apache License, Version 2.0 (the "License"); 3378- * you may not use this file except in compliance with the License. 3379- * You may obtain a copy of the License at 3380- * 3381- * http://www.apache.org/licenses/LICENSE-2.0 3382- * 3383- * Unless required by applicable law or agreed to in writing, software 3384- * distributed under the License is distributed on an "AS IS" BASIS, 3385- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 3386- * See the License for the specific language governing permissions and 3387- * limitations under the License. 3388- */ 3389- 3390-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_BATCHNORM_H_ 3391-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_BATCHNORM_H_ 3392- 3393-#include <vector> 3394-#include <memory> 3395-#include "src/expression/net.h" 3396-#include "inner/model_generated.h" 3397- 3398-namespace mindspore { 3399-namespace lite { 3400-class BatchNorm2dM : public Node { 3401- public: 3402- BatchNorm2dM() = default; 3403- BatchNorm2dM(int outp, float momentum, float epsilon); 3404- std::vector<EXPR *> Grad(EXPR *expr) override; 3405- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 3406- void SetLearn() override; 3407-}; 3408- 3409-class BatchNorm2dGradM : public Node { 3410- public: 3411- explicit BatchNorm2dGradM(BatchNorm2dM *bn_node); 3412- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 3413-}; 3414-} // namespace lite 3415-} // namespace mindspore 3416-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_BATCHNORM_H_ 3417diff --git a/mindspore/lite/src/expression/ops/biasadd.cc b/mindspore/lite/src/expression/ops/biasadd.cc 3418deleted file mode 100644 3419index c6088584..00000000 3420--- a/mindspore/lite/src/expression/ops/biasadd.cc 3421+++ /dev/null 3422@@ -1,93 +0,0 @@ 3423-/** 3424- * Copyright 2022 Huawei Technologies Co., Ltd 3425- * 3426- * Licensed under the Apache License, Version 2.0 (the "License"); 3427- * you may not use this file except in compliance with the License. 3428- * You may obtain a copy of the License at 3429- * 3430- * http://www.apache.org/licenses/LICENSE-2.0 3431- * 3432- * Unless required by applicable law or agreed to in writing, software 3433- * distributed under the License is distributed on an "AS IS" BASIS, 3434- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 3435- * See the License for the specific language governing permissions and 3436- * limitations under the License. 3437- */ 3438- 3439-#include "src/expression/ops/biasadd.h" 3440-#include "src/expression/ops/transpose.h" 3441-#include "nnacl/arithmetic_parameter.h" 3442-#include "src/expression/import.h" 3443- 3444-namespace mindspore { 3445-namespace lite { 3446-BiasAddM::BiasAddM(Format data_format) { 3447- auto op_param = calloc(1, sizeof(ArithmeticParameter)); 3448- if (op_param == nullptr) { 3449- MS_LOG(ERROR) << " cannot allocate ConvParameter"; 3450- return; 3451- } 3452- auto bias_param = reinterpret_cast<ArithmeticParameter *>(op_param); 3453- SetOpParam(bias_param); 3454- set_primitive(schema::PrimitiveType_BiasAdd); 3455-} 3456- 3457-std::vector<EXPR *> BiasAddM::construct(const std::vector<EXPR *> &inputs) { 3458- auto x = Node::construct(inputs); 3459- AddLearn(inputs.at(C1NUM)->node()); 3460- return x; 3461-} 3462- 3463-void BiasAddM::SetLearn() { AddLearn(input(C1NUM)->node()); } 3464- 3465-std::vector<EXPR *> BiasAddM::Grad(EXPR *yt) { 3466- auto in = yt; 3467- if (yt->format() != NHWC && yt->dims().size() == C4NUM) { 3468- in = TransposeM::TransposeCHW2HWC(yt); 3469- in->node()->set_name(kGradName + "/" + name() + "/" + in->node()->name()); 3470- PushOp(in->node()); 3471- } 3472- auto grad_node = new (std::nothrow) BiasAddGradM(*this); 3473- if (grad_node == nullptr) { 3474- MS_LOG(ERROR) << "Cannon allocate Bias Grad"; 3475- return {}; 3476- } 3477- PushOp(grad_node); 3478- auto bias_grad = (*grad_node)({in}); 3479- return {in, bias_grad.front()}; 3480-} 3481-int BiasAddM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 3482- auto prim = new (std::nothrow) schema::BiasAddT; 3483- if (prim == nullptr) { 3484- MS_LOG(ERROR) << "cannot allocate prim"; 3485- return RET_ERROR; 3486- } 3487- prim->format = static_cast<schema::Format>(KHWC); 3488- cnode->primitive->value.value = prim; 3489- return RET_OK; 3490-} 3491- 3492-BiasAddGradM::BiasAddGradM(const BiasAddM &bias) { 3493- auto op_param = calloc(1, sizeof(OpParameter)); 3494- if (op_param == nullptr) { 3495- MS_LOG(ERROR) << "Cannot allocate op_param"; 3496- } 3497- SetOpParam(op_param); 3498- set_primitive(schema::PrimitiveType_BiasAddGrad); 3499-} 3500- 3501-int BiasAddGradM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 3502- auto prim = new (std::nothrow) schema::BiasAddGradT; 3503- if (prim == nullptr) { 3504- MS_LOG(ERROR) << "Cannot allocate primitive"; 3505- return RET_ERROR; 3506- } 3507- cnode->primitive->value.value = prim; 3508- return RET_OK; 3509-} 3510- 3511-static ImportReg reg(schema::PrimitiveType_BiasAdd, ReturnNode<BiasAddM>); 3512- 3513-namespace NN {} 3514-} // namespace lite 3515-} // namespace mindspore 3516diff --git a/mindspore/lite/src/expression/ops/biasadd.h b/mindspore/lite/src/expression/ops/biasadd.h 3517deleted file mode 100644 3518index bb23e0ff..00000000 3519--- a/mindspore/lite/src/expression/ops/biasadd.h 3520+++ /dev/null 3521@@ -1,44 +0,0 @@ 3522-/** 3523- * Copyright 2022 Huawei Technologies Co., Ltd 3524- * 3525- * Licensed under the Apache License, Version 2.0 (the "License"); 3526- * you may not use this file except in compliance with the License. 3527- * You may obtain a copy of the License at 3528- * 3529- * http://www.apache.org/licenses/LICENSE-2.0 3530- * 3531- * Unless required by applicable law or agreed to in writing, software 3532- * distributed under the License is distributed on an "AS IS" BASIS, 3533- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 3534- * See the License for the specific language governing permissions and 3535- * limitations under the License. 3536- */ 3537- 3538-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_BIASADD_H_ 3539-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_BIASADD_H_ 3540- 3541-#include <vector> 3542-#include <memory> 3543-#include "src/expression/net.h" 3544-#include "inner/model_generated.h" 3545- 3546-namespace mindspore { 3547-namespace lite { 3548-class BiasAddM : public Node { 3549- public: 3550- BiasAddM() = default; 3551- explicit BiasAddM(Format data_format); 3552- std::vector<EXPR *> construct(const std::vector<EXPR *> &inputs) override; 3553- std::vector<EXPR *> Grad(EXPR *yt) override; 3554- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 3555- void SetLearn() override; 3556-}; 3557- 3558-class BiasAddGradM : public Node { 3559- public: 3560- explicit BiasAddGradM(const BiasAddM &bias); 3561- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 3562-}; 3563-} // namespace lite 3564-} // namespace mindspore 3565-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_BIASADD_H_ 3566diff --git a/mindspore/lite/src/expression/ops/conv.cc b/mindspore/lite/src/expression/ops/conv.cc 3567deleted file mode 100644 3568index 669fd2b9..00000000 3569--- a/mindspore/lite/src/expression/ops/conv.cc 3570+++ /dev/null 3571@@ -1,241 +0,0 @@ 3572-/** 3573- * Copyright 2022 Huawei Technologies Co., Ltd 3574- * 3575- * Licensed under the Apache License, Version 2.0 (the "License"); 3576- * you may not use this file except in compliance with the License. 3577- * You may obtain a copy of the License at 3578- * 3579- * http://www.apache.org/licenses/LICENSE-2.0 3580- * 3581- * Unless required by applicable law or agreed to in writing, software 3582- * distributed under the License is distributed on an "AS IS" BASIS, 3583- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 3584- * See the License for the specific language governing permissions and 3585- * limitations under the License. 3586- */ 3587- 3588-#include "src/expression/ops/conv.h" 3589-#include <memory> 3590-#include "src/expression/ops/biasadd.h" 3591-#include "src/expression/ops/depend.h" 3592-#include "src/expression/ops/transpose.h" 3593-#include "nnacl/conv_parameter.h" 3594-#include "inner/model_generated.h" 3595-#include "src/expression/import.h" 3596-#include "src/expression/ops.h" 3597-#include "src/litert/cxx_api/expression/node_impl.h" 3598- 3599-namespace mindspore { 3600-namespace lite { 3601-ConvM::ConvM(const ConvConfig &cfg) : Node() { 3602- auto op_param = calloc(1, sizeof(ConvParameter)); 3603- if (op_param == nullptr) { 3604- MS_LOG(ERROR) << " cannot allocate ConvParameter"; 3605- return; 3606- } 3607- SetOpParam(op_param); 3608- ConvParameter *conv_param = reinterpret_cast<ConvParameter *>(OpParam()); 3609- conv_param->input_channel_ = cfg.in_channel_; 3610- conv_param->output_channel_ = cfg.out_channel_; 3611- conv_param->kernel_h_ = cfg.kernel_size_[0]; 3612- conv_param->kernel_w_ = cfg.kernel_size_[1]; 3613- conv_param->stride_h_ = cfg.stride_[0]; 3614- conv_param->stride_w_ = cfg.stride_[1]; 3615- auto pad_mode = GetMode(cfg.pad_mode_); 3616- if (pad_mode == -1) { 3617- MS_LOG(ERROR) << "bad pad mode"; 3618- return; 3619- } 3620- conv_param->pad_mode_ = static_cast<PadType>(pad_mode); 3621- conv_param->pad_u_ = cfg.padding_[C0NUM]; 3622- conv_param->pad_d_ = cfg.padding_[C1NUM]; 3623- conv_param->pad_l_ = cfg.padding_[C2NUM]; 3624- conv_param->pad_r_ = cfg.padding_[C3NUM]; 3625- conv_param->dilation_h_ = cfg.dilation_[C0NUM]; 3626- conv_param->dilation_w_ = cfg.dilation_[C1NUM]; 3627- conv_param->group_ = cfg.group_; 3628- conv_param->out_format_ = NHWC; 3629- conv_param->act_type_ = ActType_No; 3630- expr()->SetSize(C2NUM); 3631- set_primitive(schema::PrimitiveType_Conv2DFusion); 3632- set_name(UniqueName("Conv")); 3633- Param::Mode mode = Param::String2Enum(cfg.weight_init_); 3634- std::vector<int> dims = {conv_param->output_channel_, conv_param->kernel_h_, conv_param->kernel_w_, 3635- conv_param->input_channel_ / conv_param->group_}; 3636- auto w = CreateWeights(dims, kNumberTypeFloat32, KHWC, mode, "weights"); 3637- expr()->set_params(C1NUM, w); 3638- if (cfg.has_bias) { 3639- bias_ = new (std::nothrow) BiasAddM(KHWC); 3640- if (bias_ == nullptr) { 3641- MS_LOG(ERROR) << "Cannot allocate bias"; 3642- return; 3643- } 3644- bias_->update_name(name()); 3645- std::vector<int> dim_bias = {conv_param->output_channel_}; 3646- wbias_ = CreateWeights(dim_bias, kNumberTypeFloat32, KHWC, Param::Mode::ZEROS, "weights"); 3647- AddLearn(wbias_->node()); 3648- PushOp(bias_); 3649- } 3650- SetLearn(); 3651-} 3652- 3653-std::vector<EXPR *> ConvM::construct(const std::vector<EXPR *> &inputs) { 3654- auto in = inputs; 3655- auto x = in.front(); 3656- if (x->format() != NHWC && x->dims().size() == C4NUM) { 3657- x = TransposeM::TransposeCHW2HWC(x); 3658- x->node()->set_name(name() + "/" + x->node()->name()); 3659- PushOp(x->node()); 3660- in.at(0) = x; 3661- } 3662- auto y = Node::construct(in); 3663- if (bias_ != nullptr) { 3664- y = (*bias_)({y.front(), wbias_}); 3665- } 3666- return y; 3667-} 3668- 3669-void ConvM::SetLearn() { AddLearn(input(C1NUM)->node()); } 3670- 3671-int ConvM::GetMode(std::string mode) { 3672- const std::vector<std::string> list = {"pad", "same", "valid"}; 3673- auto itr = std::find(list.begin(), list.end(), mode); 3674- if (itr == list.end()) { 3675- MS_LOG(ERROR) << "illegal mode" << mode; 3676- return -1; 3677- } 3678- return std::distance(list.begin(), itr); 3679-} 3680- 3681-std::vector<EXPR *> ConvM::Grad(EXPR *yt) { 3682- // Generate Input Grad 3683- EXPR *in = yt; 3684- if (yt->format() != NHWC && yt->dims().size() == C4NUM) { 3685- in = TransposeM::TransposeCHW2HWC(yt); 3686- in->node()->set_name(kGradName + "/" + name() + "/" + in->node()->name()); 3687- PushOp(in->node()); 3688- } 3689- auto inGrad = new (std::nothrow) ConvInputGradM(this); 3690- if (inGrad == nullptr) { 3691- MS_LOG(ERROR) << "Cannot allocate convolution input grad"; 3692- return {}; 3693- } 3694- PushOp(inGrad); 3695- auto ig = (*inGrad)({in, input(1), inGrad->input(2)}); 3696- // Execution Control Flow ! 3697- auto depend = NN::Depend(); 3698- if (depend == nullptr) { 3699- MS_LOG(ERROR) << "Cannot allocate depend"; 3700- return {}; 3701- } 3702- PushOp(depend); 3703- depend->update_name(name()); 3704- auto de = (*depend)({inGrad->expr()}); 3705- // Generate Filter Grad 3706- auto filterGrad = new (std::nothrow) ConvFilterGradM(this); 3707- if (filterGrad == nullptr) { 3708- MS_LOG(ERROR) << "Cannot allocate convolution filter grad"; 3709- return {}; 3710- } 3711- PushOp(filterGrad); 3712- filterGrad->update_name(name()); 3713- auto fg = (*filterGrad)({in, input(0), filterGrad->input(2), de[0]}); 3714- std::vector<EXPR *> res = {ig[0], fg[0]}; 3715- return res; 3716-} 3717- 3718-int ConvM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 3719- auto conv_param = reinterpret_cast<const ConvParameter *>(OpParam()); 3720- auto prim = new (std::nothrow) schema::Conv2DFusionT; 3721- if (prim == nullptr) { 3722- MS_LOG(ERROR) << "Cannot allocate primitive"; 3723- return RET_ERROR; 3724- } 3725- prim->activation_type = static_cast<schema::ActivationType>(conv_param->act_type_); 3726- prim->format = static_cast<schema::Format>(conv_param->out_format_); 3727- prim->stride = {conv_param->stride_h_, conv_param->stride_w_}; 3728- prim->kernel_size = {conv_param->kernel_h_, conv_param->kernel_w_}; 3729- prim->dilation = {conv_param->dilation_h_, conv_param->dilation_w_}; 3730- prim->out_channel = conv_param->output_channel_; 3731- prim->in_channel = conv_param->input_channel_; 3732- prim->group = conv_param->group_; 3733- prim->pad_mode = static_cast<schema::PadMode>(conv_param->pad_mode_); 3734- prim->pad_list = {conv_param->pad_u_, conv_param->pad_d_, conv_param->pad_l_, conv_param->pad_r_}; 3735- prim->mode = 1; 3736- cnode->primitive->value.value = prim; 3737- return RET_OK; 3738-} 3739- 3740-ConvInputGradM::ConvInputGradM(ConvM *conv_node) : Node() { 3741- CloneOpParam<ConvParameter>(conv_node->OpParam()); 3742- set_primitive(schema::PrimitiveType_Conv2DBackpropInputFusion); 3743- set_name(kGradName + "/conv2DBackpropInput"); 3744- expr()->SetSize(C3NUM); 3745- auto const x = conv_node->input(0); 3746- CreateConstTensor(C2NUM, {static_cast<int32_t>(x->dims().size())}, kNumberTypeInt32, KHWC, "shape", x->dims().data()); 3747-} 3748- 3749-int ConvInputGradM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 3750- auto conv_param = reinterpret_cast<const ConvParameter *>(OpParam()); 3751- auto prim = new (std::nothrow) schema::Conv2DBackpropInputFusionT; 3752- if (prim == nullptr) { 3753- MS_LOG(ERROR) << "Cannot allocate primitive"; 3754- return RET_ERROR; 3755- } 3756- prim->activation_type = static_cast<schema::ActivationType>(conv_param->act_type_); 3757- prim->format = static_cast<schema::Format>(conv_param->out_format_); 3758- prim->stride = {conv_param->stride_h_, conv_param->stride_w_}; 3759- prim->kernel_size = {conv_param->kernel_h_, conv_param->kernel_w_}; 3760- prim->dilation = {conv_param->dilation_h_, conv_param->dilation_w_}; 3761- prim->out_channel = conv_param->output_channel_; 3762- prim->in_channel = conv_param->input_channel_; 3763- prim->group = conv_param->group_; 3764- prim->pad_mode = static_cast<schema::PadMode>(conv_param->pad_mode_); 3765- prim->pad_list = {conv_param->pad_u_, conv_param->pad_d_, conv_param->pad_l_, conv_param->pad_r_}; 3766- cnode->primitive->value.value = prim; 3767- return RET_OK; 3768-} 3769- 3770-ConvFilterGradM::ConvFilterGradM(ConvM *conv_node) : Node() { 3771- CloneOpParam<ConvParameter>(conv_node->OpParam()); 3772- set_primitive(schema::PrimitiveType_Conv2DBackpropFilterFusion); 3773- set_name(kGradName + "/conv2DBackpropFilter"); 3774- expr()->SetSize(C4NUM); 3775- auto w = conv_node->input(1); 3776- CreateConstTensor(C2NUM, {static_cast<int32_t>(w->dims().size())}, kNumberTypeInt32, KHWC, "shape", w->dims().data()); 3777-} 3778-int ConvFilterGradM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 3779- auto conv_param = reinterpret_cast<const ConvParameter *>(OpParam()); 3780- auto prim = new (std::nothrow) schema::Conv2DBackpropFilterFusionT; 3781- if (prim == nullptr) { 3782- MS_LOG(ERROR) << "Cannot allocate primitive"; 3783- return RET_ERROR; 3784- } 3785- prim->activation_type = static_cast<schema::ActivationType>(conv_param->act_type_); 3786- prim->format = static_cast<schema::Format>(conv_param->out_format_); 3787- prim->stride = {conv_param->stride_h_, conv_param->stride_w_}; 3788- prim->kernel_size = {conv_param->kernel_h_, conv_param->kernel_w_}; 3789- prim->dilation = {conv_param->dilation_h_, conv_param->dilation_w_}; 3790- prim->out_channel = conv_param->output_channel_; 3791- prim->in_channel = conv_param->input_channel_; 3792- prim->group = conv_param->group_; 3793- prim->pad_mode = static_cast<schema::PadMode>(conv_param->pad_mode_); 3794- prim->pad_list = {conv_param->pad_u_, conv_param->pad_d_, conv_param->pad_l_, conv_param->pad_r_}; 3795- cnode->primitive->value.value = prim; 3796- return RET_OK; 3797-} 3798- 3799-static ImportReg reg(schema::PrimitiveType_Conv2DFusion, ReturnNode<ConvM>); 3800- 3801-namespace NN { 3802-Node *Conv2D(const ConvConfig &cfg) { 3803- auto c = new (std::nothrow) ConvM(cfg); 3804- if (c == nullptr) { 3805- MS_LOG(ERROR) << "Cannot allocate Convolution object"; 3806- return nullptr; 3807- } 3808- return c; 3809-} 3810-} // namespace NN 3811-} // namespace lite 3812-} // namespace mindspore 3813diff --git a/mindspore/lite/src/expression/ops/conv.h b/mindspore/lite/src/expression/ops/conv.h 3814deleted file mode 100644 3815index 32fc6632..00000000 3816--- a/mindspore/lite/src/expression/ops/conv.h 3817+++ /dev/null 3818@@ -1,58 +0,0 @@ 3819-/** 3820- * Copyright 2022 Huawei Technologies Co., Ltd 3821- * 3822- * Licensed under the Apache License, Version 2.0 (the "License"); 3823- * you may not use this file except in compliance with the License. 3824- * You may obtain a copy of the License at 3825- * 3826- * http://www.apache.org/licenses/LICENSE-2.0 3827- * 3828- * Unless required by applicable law or agreed to in writing, software 3829- * distributed under the License is distributed on an "AS IS" BASIS, 3830- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 3831- * See the License for the specific language governing permissions and 3832- * limitations under the License. 3833- */ 3834- 3835-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_CONV_H_ 3836-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_CONV_H_ 3837- 3838-#include <vector> 3839-#include <memory> 3840-#include <string> 3841-#include "src/expression/cfg.h" 3842-#include "src/expression/node.h" 3843- 3844-namespace mindspore { 3845-namespace lite { 3846-class ConvM : public Node { 3847- public: 3848- ConvM() = default; 3849- explicit ConvM(const ConvConfig &cfg); 3850- std::vector<EXPR *> construct(const std::vector<EXPR *> &inputs) override; 3851- Param *weight() override { return input(1)->node()->data(); } 3852- std::vector<EXPR *> Grad(EXPR *expr) override; 3853- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 3854- void SetLearn() override; 3855- 3856- private: 3857- int GetMode(std::string mode); 3858- Node *bias_ = nullptr; 3859- EXPR *wbias_ = nullptr; 3860-}; 3861- 3862-class ConvInputGradM : public Node { 3863- public: 3864- explicit ConvInputGradM(ConvM *conv_node); 3865- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 3866-}; 3867- 3868-class ConvFilterGradM : public Node { 3869- public: 3870- explicit ConvFilterGradM(ConvM *conv_node); 3871- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 3872-}; 3873-} // namespace lite 3874-} // namespace mindspore 3875- 3876-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_CONV_H_ 3877diff --git a/mindspore/lite/src/expression/ops/dense.cc b/mindspore/lite/src/expression/ops/dense.cc 3878deleted file mode 100644 3879index 9d1df46f..00000000 3880--- a/mindspore/lite/src/expression/ops/dense.cc 3881+++ /dev/null 3882@@ -1,151 +0,0 @@ 3883-/** 3884- * Copyright 2022 Huawei Technologies Co., Ltd 3885- * 3886- * Licensed under the Apache License, Version 2.0 (the "License"); 3887- * you may not use this file except in compliance with the License. 3888- * You may obtain a copy of the License at 3889- * 3890- * http://www.apache.org/licenses/LICENSE-2.0 3891- * 3892- * Unless required by applicable law or agreed to in writing, software 3893- * distributed under the License is distributed on an "AS IS" BASIS, 3894- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 3895- * See the License for the specific language governing permissions and 3896- * limitations under the License. 3897- */ 3898- 3899-#include "src/expression/ops/dense.h" 3900-#include <memory> 3901-#include "include/api/cfg.h" 3902-#include "src/expression/ops/biasadd.h" 3903-#include "src/expression/ops/depend.h" 3904-#include "src/expression/ops.h" 3905-#include "nnacl/matmul_parameter.h" 3906-#include "src/expression/import.h" 3907-#include "inner/model_generated.h" 3908-#include "src/litert/cxx_api/expression/node_impl.h" 3909- 3910-namespace mindspore { 3911-namespace lite { 3912-DenseM::DenseM(const DenseConfig &cfg) : Node() { 3913- auto op_param = calloc(1, sizeof(MatMulParameter)); 3914- if (op_param == nullptr) { 3915- MS_LOG(ERROR) << " cannot allocate MatMulParameter"; 3916- return; 3917- } 3918- set_name(UniqueName("Dense")); 3919- SetOpParam(op_param); 3920- expr()->SetSize(C2NUM); 3921- set_primitive(schema::PrimitiveType_MatMulFusion); 3922- auto param = reinterpret_cast<MatMulParameter *>(opParam_.get()); 3923- param->row_ = cfg.out_channels_; 3924- param->col_ = cfg.in_channels_; 3925- param->a_transpose_ = false; 3926- param->b_transpose_ = true; 3927- std::vector<int> dims = {param->row_, param->col_}; 3928- auto w = Node::CreateWeights(dims, kNumberTypeFloat32, KHWC, Param::Mode::NORMAL, "weights"); 3929- expr()->set_params(C1NUM, w); 3930- if (cfg.has_bias_) { 3931- wbias_ = CreateWeights({cfg.out_channels_}, kNumberTypeFloat32, KHWC, Param::Mode::ZEROS, "bias_weights"); 3932- bias_ = new (std::nothrow) BiasAddM(KHWC); 3933- if (bias_ == nullptr) { 3934- MS_LOG(ERROR) << "Cannot allocate bias"; 3935- return; 3936- } 3937- bias_->update_name(name()); 3938- AddLearn(wbias_->node()); 3939- PushOp(bias_); 3940- } 3941- SetLearn(); 3942-} 3943- 3944-std::vector<EXPR *> DenseM::construct(const std::vector<EXPR *> &inputs) { 3945- auto x = Node::construct(inputs); 3946- if (bias_ != nullptr) { 3947- x = (*bias_)({x.front(), wbias_}); 3948- } 3949- return x; 3950-} 3951- 3952-std::vector<EXPR *> DenseM::Grad(EXPR *yt) { 3953- auto src_param = reinterpret_cast<MatMulParameter *>(opParam_.get()); 3954- bool ta = src_param->a_transpose_; 3955- bool tb = src_param->b_transpose_; 3956- 3957- // dx grad op 3958- auto dxGrad = new (std::nothrow) DenseM(); 3959- if (dxGrad == nullptr) { 3960- MS_LOG(ERROR) << "Cannot allocate dxGrad "; 3961- return {}; 3962- } 3963- PushOp(dxGrad); 3964- dxGrad->CloneOpParam<MatMulParameter>(opParam_); 3965- dxGrad->set_primitive(schema::PrimitiveType_MatMulFusion); 3966- auto dxGradParam = reinterpret_cast<MatMulParameter *>(dxGrad->OpParam()); 3967- dxGradParam->a_transpose_ = (ta && tb); 3968- dxGradParam->b_transpose_ = (ta || !tb); 3969- dxGrad->set_name(name() + kGradName + "/dxGrad"); 3970- EXPR *dx = nullptr; 3971- if (ta) { 3972- dx = (*dxGrad)({input(1), yt}).front(); 3973- } else { 3974- dx = (*dxGrad)({yt, input(1)}).front(); 3975- } 3976- // Control execution flow 3977- auto depend = NN::Depend(); 3978- if (depend == nullptr) { 3979- MS_LOG(ERROR) << "Cannot allocate depend "; 3980- return {}; 3981- } 3982- PushOp(depend); 3983- auto de = (*depend)({dxGrad->expr()}).front(); 3984- 3985- // dw grad op 3986- auto dwGrad = new (std::nothrow) DenseM(); 3987- if (dwGrad == nullptr) { 3988- MS_LOG(ERROR) << "Cannot allocate dwGrad "; 3989- return {}; 3990- } 3991- PushOp(dwGrad); 3992- dwGrad->CloneOpParam<MatMulParameter>(opParam_); 3993- dwGrad->set_primitive(schema::PrimitiveType_MatMulFusion); 3994- auto dwGradParam = reinterpret_cast<MatMulParameter *>(dwGrad->OpParam()); 3995- dwGradParam->a_transpose_ = (!ta || tb); 3996- dwGradParam->b_transpose_ = ta && tb; 3997- dwGrad->set_name(name() + kGradName + "/dwGrad"); 3998- EXPR *dw = nullptr; 3999- if (tb) { 4000- dw = (*dwGrad)({yt, input(0), de}).front(); 4001- } else { 4002- dw = (*dwGrad)({input(0), yt, de}).front(); 4003- } 4004- return {dx, dw}; 4005-} 4006-int DenseM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 4007- auto dense_param = reinterpret_cast<const MatMulParameter *>(OpParam()); 4008- auto prim = new (std::nothrow) schema::MatMulFusionT; 4009- if (prim == nullptr) { 4010- MS_LOG(ERROR) << "Cannot allocate primitive"; 4011- return RET_ERROR; 4012- } 4013- prim->transpose_a = dense_param->a_transpose_; 4014- prim->transpose_b = dense_param->b_transpose_; 4015- cnode->primitive->value.value = prim; 4016- return RET_OK; 4017-} 4018- 4019-void DenseM::SetLearn() { AddLearn(input(C1NUM)->node()); } 4020- 4021-static ImportReg reg(schema::PrimitiveType_MatMulFusion, ReturnNode<DenseM>); 4022- 4023-namespace NN { 4024-Node *Dense(const DenseConfig &cfg) { 4025- auto l = new (std::nothrow) DenseM(cfg); 4026- if (l == nullptr) { 4027- MS_LOG(ERROR) << "Cannot allocate Dense object"; 4028- } 4029- return l; 4030-} 4031-} // namespace NN 4032-} // namespace lite 4033-} // namespace mindspore 4034diff --git a/mindspore/lite/src/expression/ops/dense.h b/mindspore/lite/src/expression/ops/dense.h 4035deleted file mode 100644 4036index 10734336..00000000 4037--- a/mindspore/lite/src/expression/ops/dense.h 4038+++ /dev/null 4039@@ -1,44 +0,0 @@ 4040-/** 4041- * Copyright 2022 Huawei Technologies Co., Ltd 4042- * 4043- * Licensed under the Apache License, Version 2.0 (the "License"); 4044- * you may not use this file except in compliance with the License. 4045- * You may obtain a copy of the License at 4046- * 4047- * http://www.apache.org/licenses/LICENSE-2.0 4048- * 4049- * Unless required by applicable law or agreed to in writing, software 4050- * distributed under the License is distributed on an "AS IS" BASIS, 4051- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 4052- * See the License for the specific language governing permissions and 4053- * limitations under the License. 4054- */ 4055- 4056-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_DENSE_H_ 4057-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_DENSE_H_ 4058- 4059-#include <vector> 4060-#include <memory> 4061-#include "src/expression/node.h" 4062-#include "src/expression/cfg.h" 4063-#include "inner/model_generated.h" 4064- 4065-namespace mindspore { 4066-namespace lite { 4067-class DenseM : public Node { 4068- public: 4069- DenseM() = default; 4070- explicit DenseM(const DenseConfig &cfg); 4071- std::vector<EXPR *> construct(const std::vector<EXPR *> &inputs) override; 4072- std::vector<EXPR *> Grad(EXPR *expr) override; 4073- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 4074- void SetLearn() override; 4075- 4076- private: 4077- Param *weight() override { return input(1)->node()->data(); } 4078- Node *bias_{nullptr}; 4079- EXPR *wbias_{nullptr}; 4080-}; 4081-} // namespace lite 4082-} // namespace mindspore 4083-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_DENSE_H_ 4084diff --git a/mindspore/lite/src/expression/ops/depend.cc b/mindspore/lite/src/expression/ops/depend.cc 4085deleted file mode 100644 4086index c6aee153..00000000 4087--- a/mindspore/lite/src/expression/ops/depend.cc 4088+++ /dev/null 4089@@ -1,43 +0,0 @@ 4090-/** 4091- * Copyright 2022 Huawei Technologies Co., Ltd 4092- * 4093- * Licensed under the Apache License, Version 2.0 (the "License"); 4094- * you may not use this file except in compliance with the License. 4095- * You may obtain a copy of the License at 4096- * 4097- * http://www.apache.org/licenses/LICENSE-2.0 4098- * 4099- * Unless required by applicable law or agreed to in writing, software 4100- * distributed under the License is distributed on an "AS IS" BASIS, 4101- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 4102- * See the License for the specific language governing permissions and 4103- * limitations under the License. 4104- */ 4105- 4106-#include "src/expression/ops/depend.h" 4107-#include "inner/model_generated.h" 4108- 4109-namespace mindspore { 4110-namespace lite { 4111-DependM::DependM() : Node() { 4112- auto param = calloc(1, sizeof(OpParameter)); 4113- if (param == nullptr) { 4114- MS_LOG(ERROR) << "Cannot allocate parameter"; 4115- return; 4116- } 4117- SetOpParam(param); 4118- set_primitive(schema::PrimitiveType_Depend); 4119- set_name(UniqueName("Depend")); 4120-} 4121-namespace NN { 4122-Node *Depend() { 4123- auto d = new (std::nothrow) DependM(); 4124- if (d == nullptr) { 4125- MS_LOG(ERROR) << "Cannot allocate depend object"; 4126- return nullptr; 4127- } 4128- return d; 4129-} 4130-} // namespace NN 4131-} // namespace lite 4132-} // namespace mindspore 4133diff --git a/mindspore/lite/src/expression/ops/dropout.cc b/mindspore/lite/src/expression/ops/dropout.cc 4134deleted file mode 100644 4135index e49bf07a..00000000 4136--- a/mindspore/lite/src/expression/ops/dropout.cc 4137+++ /dev/null 4138@@ -1,91 +0,0 @@ 4139-/** 4140- * Copyright 2022 Huawei Technologies Co., Ltd 4141- * 4142- * Licensed under the Apache License, Version 2.0 (the "License"); 4143- * you may not use this file except in compliance with the License. 4144- * You may obtain a copy of the License at 4145- * 4146- * http://www.apache.org/licenses/LICENSE-2.0 4147- * 4148- * Unless required by applicable law or agreed to in writing, software 4149- * distributed under the License is distributed on an "AS IS" BASIS, 4150- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 4151- * See the License for the specific language governing permissions and 4152- * limitations under the License. 4153- */ 4154- 4155-#include "src/expression/ops/dropout.h" 4156-#include <vector> 4157-#include "nnacl/fp32_grad/dropout_parameter.h" 4158-#include "inner/model_generated.h" 4159-#include "src/expression/import.h" 4160-#include "src/expression/ops.h" 4161-#include "src/litert/cxx_api/expression/node_impl.h" 4162- 4163-namespace mindspore { 4164-namespace lite { 4165-DropOutM::DropOutM(float ratio) { 4166- auto param = reinterpret_cast<DropoutParameter *>(calloc(1, sizeof(DropoutParameter))); 4167- if (param == nullptr) { 4168- MS_LOG(ERROR) << "Cannot allocate parameter"; 4169- return; 4170- } 4171- param->ratio_ = ratio; 4172- SetOpParam(param); 4173- set_primitive(schema::PrimitiveType_Dropout); 4174- set_name(UniqueName("DropOut")); 4175-} 4176- 4177-std::vector<EXPR *> DropOutM::Grad(EXPR *yt) { 4178- auto inGrad = new (std::nothrow) DropOutGradM(this); 4179- if (inGrad == nullptr) { 4180- MS_LOG(ERROR) << "Cannot allocate drop grad"; 4181- return {}; 4182- } 4183- return (*inGrad)({yt, expr()}); 4184-} 4185- 4186-int DropOutM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 4187- auto param = reinterpret_cast<const DropoutParameter *>(OpParam()); 4188- auto prim = new (std::nothrow) schema::DropoutT; 4189- if (prim == nullptr) { 4190- MS_LOG(ERROR) << "Cannot allocate primitive"; 4191- return RET_ERROR; 4192- } 4193- prim->keep_prob = param->ratio_; 4194- cnode->primitive->value.value = prim; 4195- return RET_OK; 4196-} 4197- 4198-DropOutGradM::DropOutGradM(DropOutM *node) { 4199- CloneOpParam<DropoutParameter>(node->OpParam()); 4200- set_primitive(schema::PrimitiveType_DropoutGrad); 4201- set_name(kGradName + "/DropOutGrad"); 4202-} 4203- 4204-int DropOutGradM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 4205- auto param = reinterpret_cast<const DropoutParameter *>(OpParam()); 4206- auto prim = new (std::nothrow) schema::DropoutGradT; 4207- if (prim == nullptr) { 4208- MS_LOG(ERROR) << "Cannot allocate primitive"; 4209- return RET_ERROR; 4210- } 4211- prim->keep_prob = param->ratio_; 4212- cnode->primitive->value.value = prim; 4213- return RET_OK; 4214-} 4215- 4216-static ImportReg reg(schema::PrimitiveType_Dropout, ReturnNode<DropOutM>); 4217- 4218-namespace NN { 4219-Node *DropOut(float ratio) { 4220- auto node = new (std::nothrow) DropOutM(ratio); 4221- if (node == nullptr) { 4222- MS_LOG(ERROR) << "Cannot allocate dropout node"; 4223- return nullptr; 4224- } 4225- return node; 4226-} 4227-} // namespace NN 4228-} // namespace lite 4229-} // namespace mindspore 4230diff --git a/mindspore/lite/src/expression/ops/dropout.h b/mindspore/lite/src/expression/ops/dropout.h 4231deleted file mode 100644 4232index dce87f18..00000000 4233--- a/mindspore/lite/src/expression/ops/dropout.h 4234+++ /dev/null 4235@@ -1,42 +0,0 @@ 4236-/** 4237- * Copyright 2022 Huawei Technologies Co., Ltd 4238- * 4239- * Licensed under the Apache License, Version 2.0 (the "License"); 4240- * you may not use this file except in compliance with the License. 4241- * You may obtain a copy of the License at 4242- * 4243- * http://www.apache.org/licenses/LICENSE-2.0 4244- * 4245- * Unless required by applicable law or agreed to in writing, software 4246- * distributed under the License is distributed on an "AS IS" BASIS, 4247- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 4248- * See the License for the specific language governing permissions and 4249- * limitations under the License. 4250- */ 4251- 4252-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_DROPOUT_H_ 4253-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_DROPOUT_H_ 4254- 4255-#include <vector> 4256-#include <memory> 4257-#include "src/expression/node.h" 4258- 4259-namespace mindspore { 4260-namespace lite { 4261-class DropOutM : public Node { 4262- public: 4263- DropOutM() = default; 4264- explicit DropOutM(float ratio); 4265- std::vector<EXPR *> Grad(EXPR *expr) override; 4266- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 4267-}; 4268- 4269-class DropOutGradM : public Node { 4270- public: 4271- explicit DropOutGradM(DropOutM *node); 4272- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 4273-}; 4274-} // namespace lite 4275-} // namespace mindspore 4276- 4277-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_DROPOUT_H_ 4278diff --git a/mindspore/lite/src/expression/ops/flatten.cc b/mindspore/lite/src/expression/ops/flatten.cc 4279deleted file mode 100644 4280index 76564186..00000000 4281--- a/mindspore/lite/src/expression/ops/flatten.cc 4282+++ /dev/null 4283@@ -1,71 +0,0 @@ 4284-/** 4285- * Copyright 2022 Huawei Technologies Co., Ltd 4286- * 4287- * Licensed under the Apache License, Version 2.0 (the "License"); 4288- * you may not use this file except in compliance with the License. 4289- * You may obtain a copy of the License at 4290- * 4291- * http://www.apache.org/licenses/LICENSE-2.0 4292- * 4293- * Unless required by applicable law or agreed to in writing, software 4294- * distributed under the License is distributed on an "AS IS" BASIS, 4295- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 4296- * See the License for the specific language governing permissions and 4297- * limitations under the License. 4298- */ 4299- 4300-#include "src/expression/ops/flatten.h" 4301-#include <vector> 4302-#include "inner/model_generated.h" 4303-#include "src/expression/import.h" 4304-#include "src/expression/ops.h" 4305- 4306-#include "src/litert/cxx_api/expression/node_impl.h" 4307-#include "nnacl/op_base.h" 4308- 4309-namespace mindspore { 4310-namespace lite { 4311-FlattenM::FlattenM(int dummy) { 4312- auto param = reinterpret_cast<OpParameter *>(calloc(C1NUM, sizeof(OpParameter))); 4313- if (param == nullptr) { 4314- MS_LOG(ERROR) << "Cannot allocate parameter"; 4315- return; 4316- } 4317- SetOpParam(param); 4318- set_primitive(schema::PrimitiveType_Flatten); 4319- set_name(UniqueName("Flatten")); 4320-} 4321- 4322-std::vector<EXPR *> FlattenM::construct(const std::vector<EXPR *> &inputs) { 4323- auto in = inputs; 4324- auto y = Node::construct(in); 4325- return y; 4326-} 4327- 4328-std::vector<EXPR *> FlattenM::Grad(EXPR *yt) { 4329- auto shape_of_x = input(0)->dims(); 4330- auto reshape = NN::Reshape(shape_of_x); 4331- PushOp(reshape); 4332- return (*reshape)({yt}); 4333-} 4334- 4335-int FlattenM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 4336- auto prim = new (std::nothrow) schema::DropoutT; 4337- if (prim == nullptr) { 4338- MS_LOG(ERROR) << "Cannot allocate primitive"; 4339- return RET_ERROR; 4340- } 4341- cnode->primitive->value.value = prim; 4342- return RET_OK; 4343-} 4344- 4345-static ImportReg reg(schema::PrimitiveType_Flatten, ReturnNode<FlattenM>); 4346- 4347-namespace NN { 4348-Node *Flatten() { 4349- auto node = new (std::nothrow) FlattenM(0); 4350- return node; 4351-} 4352-} // namespace NN 4353-} // namespace lite 4354-} // namespace mindspore 4355diff --git a/mindspore/lite/src/expression/ops/flatten.h b/mindspore/lite/src/expression/ops/flatten.h 4356deleted file mode 100644 4357index 0be7d5bb..00000000 4358--- a/mindspore/lite/src/expression/ops/flatten.h 4359+++ /dev/null 4360@@ -1,36 +0,0 @@ 4361-/** 4362- * Copyright 2022 Huawei Technologies Co., Ltd 4363- * 4364- * Licensed under the Apache License, Version 2.0 (the "License"); 4365- * you may not use this file except in compliance with the License. 4366- * You may obtain a copy of the License at 4367- * 4368- * http://www.apache.org/licenses/LICENSE-2.0 4369- * 4370- * Unless required by applicable law or agreed to in writing, software 4371- * distributed under the License is distributed on an "AS IS" BASIS, 4372- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 4373- * See the License for the specific language governing permissions and 4374- * limitations under the License. 4375- */ 4376- 4377-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_FLATTEN_H_ 4378-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_FLATTEN_H_ 4379- 4380-#include <memory> 4381-#include <vector> 4382-#include "src/expression/node.h" 4383- 4384-namespace mindspore { 4385-namespace lite { 4386-class FlattenM : public Node { 4387- public: 4388- FlattenM() = default; 4389- explicit FlattenM(int dummy); 4390- std::vector<EXPR *> Grad(EXPR *expr) override; 4391- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 4392- std::vector<EXPR *> construct(const std::vector<EXPR *> &inputs) override; 4393-}; 4394-} // namespace lite 4395-} // namespace mindspore 4396-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_FLATTEN_H_ 4397diff --git a/mindspore/lite/src/expression/ops/pooling.cc b/mindspore/lite/src/expression/ops/pooling.cc 4398deleted file mode 100644 4399index 6efbc863..00000000 4400--- a/mindspore/lite/src/expression/ops/pooling.cc 4401+++ /dev/null 4402@@ -1,215 +0,0 @@ 4403-/** 4404- * Copyright 2022 Huawei Technologies Co., Ltd 4405- * 4406- * Licensed under the Apache License, Version 2.0 (the "License"); 4407- * you may not use this file except in compliance with the License. 4408- * You may obtain a copy of the License at 4409- * 4410- * http://www.apache.org/licenses/LICENSE-2.0 4411- * 4412- * Unless required by applicable law or agreed to in writing, software 4413- * distributed under the License is distributed on an "AS IS" BASIS, 4414- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 4415- * See the License for the specific language governing permissions and 4416- * limitations under the License. 4417- */ 4418- 4419-#include "src/expression/ops/pooling.h" 4420-#include "src/expression/ops.h" 4421-#include "src/expression/import.h" 4422-#include "src/litert/cxx_api/expression/node_impl.h" 4423-#include "src/expression/ops/transpose.h" 4424- 4425-namespace mindspore { 4426-namespace lite { 4427-PoolingM::PoolingM(const PoolingConfig &cfg) : Node() { 4428- auto op_param = calloc(1, sizeof(PoolingParameter)); 4429- if (op_param == nullptr) { 4430- MS_LOG(ERROR) << " cannot allocate PoolingParameter"; 4431- return; 4432- } 4433- SetOpParam(op_param); 4434- PoolingParameter *pool_param = reinterpret_cast<PoolingParameter *>(OpParam()); 4435- 4436- pool_param->window_h_ = cfg.kernel_size_[0]; 4437- pool_param->window_w_ = cfg.kernel_size_[1]; 4438- pool_param->stride_h_ = cfg.stride_[0]; 4439- pool_param->stride_w_ = cfg.stride_[1]; 4440- auto pad_mode = GetMode(cfg.pad_mode_); 4441- if (pad_mode == -1) { 4442- MS_LOG(ERROR) << "bad pad mode"; 4443- return; 4444- } 4445- pool_param->pad_mode_ = static_cast<PadType>(pad_mode + Pad_pad); 4446- pool_param->round_type_ = RoundType_Floor; 4447- pool_param->act_type_ = ActType_No; 4448-} 4449- 4450-std::vector<EXPR *> PoolingM::construct(const std::vector<EXPR *> &inputs) { 4451- auto in = inputs; 4452- auto x = in.front(); 4453- if (x->format() != NHWC && x->dims().size() == C4NUM) { 4454- x = TransposeM::TransposeCHW2HWC(x); 4455- x->node()->set_name(name() + "/" + x->node()->name()); 4456- PushOp(x->node()); 4457- in.at(0) = x; 4458- } 4459- auto y = Node::construct(in); 4460- return y; 4461-} 4462- 4463-int PoolingM::GetMode(std::string mode) { 4464- const std::vector<std::string> list = {"same", "valid"}; 4465- auto itr = std::find(list.begin(), list.end(), mode); 4466- if (itr == list.end()) { 4467- MS_LOG(ERROR) << "illegal mode" << mode; 4468- return -1; 4469- } 4470- return std::distance(list.begin(), itr); 4471-} 4472- 4473-void PoolingM::UpdateRoundMode(const PoolingParameter *param, schema::RoundMode *round_mode) { 4474- switch (param->round_type_) { 4475- case RoundType_Floor: 4476- *round_mode = schema::RoundMode_FLOOR; 4477- break; 4478- case RoundType_Ceil: 4479- *round_mode = schema::RoundMode_CEIL; 4480- break; 4481- default: 4482- *round_mode = schema::RoundMode_FLOOR; 4483- break; 4484- } 4485-} 4486- 4487-template <typename T> 4488-int PoolingM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 4489- auto param = reinterpret_cast<const PoolingParameter *>(OpParam()); 4490- auto prim = new (std::nothrow) T; 4491- if (prim == nullptr) { 4492- MS_LOG(ERROR) << "Cannot allocate primitive"; 4493- return RET_ERROR; 4494- } 4495- prim->kernel_size = {param->window_h_, param->window_w_}; 4496- prim->strides = {param->stride_h_, param->stride_w_}; 4497- prim->pad = {param->pad_u_, param->pad_d_, param->pad_l_, param->pad_r_}; 4498- prim->pad_mode = static_cast<schema::PadMode>(param->pad_mode_); 4499- UpdateRoundMode(param, &prim->round_mode); 4500- prim->global = param->global_; 4501- prim->activation_type = schema::ActivationType_NO_ACTIVATION; 4502- cnode->primitive->value.value = prim; 4503- return RET_OK; 4504-} 4505- 4506-template <typename T> 4507-int PoolingM::UnPopulateGrad(const std::unique_ptr<schema::CNodeT> &cnode) { 4508- auto param = reinterpret_cast<const PoolingParameter *>(OpParam()); 4509- auto prim = new (std::nothrow) T; 4510- if (prim == nullptr) { 4511- MS_LOG(ERROR) << "Cannot allocate primitive"; 4512- return RET_ERROR; 4513- } 4514- prim->kernel_size = {param->window_h_, param->window_w_}; 4515- prim->strides = {param->stride_h_, param->stride_w_}; 4516- prim->pad_mode = static_cast<schema::PadMode>(param->pad_mode_); 4517- cnode->primitive->value.value = prim; 4518- return RET_OK; 4519-} 4520- 4521-// Max pooling Definition 4522-MaxPoolM::MaxPoolM(const PoolingConfig &cfg) : PoolingM(cfg) { 4523- auto param = reinterpret_cast<PoolingParameter *>(OpParam()); 4524- param->pool_mode_ = PoolMode_MaxPool; 4525- set_primitive(schema::PrimitiveType_MaxPoolFusion); 4526- set_name(UniqueName("MaxPool")); 4527-} 4528- 4529-int MaxPoolM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 4530- return PoolingM::UnPopulate<schema::MaxPoolFusionT>(cnode); 4531-} 4532- 4533-std::vector<EXPR *> MaxPoolM::Grad(EXPR *yt) { 4534- auto in = yt; 4535- if (yt->format() != NHWC && yt->dims().size() == C4NUM) { 4536- in = TransposeM::TransposeCHW2HWC(yt); 4537- in->node()->set_name(kGradName + "/" + name() + "/" + in->node()->name()); 4538- PushOp(in->node()); 4539- } 4540- auto pool_grad = new (std::nothrow) MaxPoolGradM(this); 4541- PushOp(pool_grad); 4542- return (*pool_grad)({input(0), output(0), in}); 4543-} 4544- 4545-static ImportReg maxPoolReg(schema::PrimitiveType_MaxPoolFusion, ReturnNode<MaxPoolM>); 4546- 4547-// Avg pooling Definition 4548-AvgPoolM::AvgPoolM(const PoolingConfig &cfg) : PoolingM(cfg) { 4549- auto param = reinterpret_cast<PoolingParameter *>(OpParam()); 4550- param->pool_mode_ = PoolMode_AvgPool; 4551- set_primitive(schema::PrimitiveType_AvgPoolFusion); 4552- set_name(UniqueName("AvgPool")); 4553-} 4554- 4555-int AvgPoolM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 4556- return PoolingM::UnPopulate<schema::AvgPoolFusionT>(cnode); 4557-} 4558- 4559-std::vector<EXPR *> AvgPoolM::Grad(EXPR *yt) { 4560- auto in = yt; 4561- if (yt->format() != NHWC && yt->dims().size() == C4NUM) { 4562- in = TransposeM::TransposeCHW2HWC(yt); 4563- in->node()->set_name(kGradName + "/" + name() + "/" + in->node()->name()); 4564- PushOp(in->node()); 4565- } 4566- auto pool_grad = new (std::nothrow) AvgPoolGradM(this); 4567- PushOp(pool_grad); 4568- return (*pool_grad)({input(0), output(0), in}); 4569-} 4570- 4571-static ImportReg avgPoolReg(schema::PrimitiveType_AvgPoolFusion, ReturnNode<AvgPoolM>); 4572- 4573-// Max Pool Grad Definition 4574-MaxPoolGradM::MaxPoolGradM(MaxPoolM *node) { 4575- Node(); 4576- CloneOpParam<PoolingParameter>(node->OpParam()); 4577- set_primitive(schema::PrimitiveType_MaxPoolGrad); 4578- set_name(kGradName + "/" + node->name() + "/MaxPoolGrad"); 4579-} 4580- 4581-int MaxPoolGradM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 4582- return PoolingM::UnPopulateGrad<schema::MaxPoolGradT>(cnode); 4583-} 4584- 4585-// Avg Pool Grad Definition 4586-AvgPoolGradM::AvgPoolGradM(AvgPoolM *node) { 4587- Node(); 4588- CloneOpParam<PoolingParameter>(node->OpParam()); 4589- set_primitive(schema::PrimitiveType_AvgPoolGrad); 4590- set_name(kGradName + "/" + node->name() + "/AvgPoolGrad"); 4591-} 4592- 4593-int AvgPoolGradM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 4594- return PoolingM::UnPopulateGrad<schema::AvgPoolGradT>(cnode); 4595-} 4596- 4597-namespace NN { 4598-Node *MaxPool2D(const PoolingConfig &cfg) { 4599- auto c = new (std::nothrow) MaxPoolM(cfg); 4600- if (c == nullptr) { 4601- MS_LOG(ERROR) << "Cannot allocate max pool object"; 4602- return nullptr; 4603- } 4604- return c; 4605-} 4606- 4607-Node *AvgPool2D(const PoolingConfig &cfg) { 4608- auto c = new (std::nothrow) AvgPoolM(cfg); 4609- if (c == nullptr) { 4610- MS_LOG(ERROR) << "Cannot allocate average pool object"; 4611- return nullptr; 4612- } 4613- return c; 4614-} 4615-} // namespace NN 4616-} // namespace lite 4617-} // namespace mindspore 4618diff --git a/mindspore/lite/src/expression/ops/pooling.h b/mindspore/lite/src/expression/ops/pooling.h 4619deleted file mode 100644 4620index 881996a3..00000000 4621--- a/mindspore/lite/src/expression/ops/pooling.h 4622+++ /dev/null 4623@@ -1,74 +0,0 @@ 4624-/** 4625- * Copyright 2022 Huawei Technologies Co., Ltd 4626- * 4627- * Licensed under the Apache License, Version 2.0 (the "License"); 4628- * you may not use this file except in compliance with the License. 4629- * You may obtain a copy of the License at 4630- * 4631- * http://www.apache.org/licenses/LICENSE-2.0 4632- * 4633- * Unless required by applicable law or agreed to in writing, software 4634- * distributed under the License is distributed on an "AS IS" BASIS, 4635- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 4636- * See the License for the specific language governing permissions and 4637- * limitations under the License. 4638- */ 4639- 4640-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_POOLING_H_ 4641-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_POOLING_H_ 4642- 4643-#include <vector> 4644-#include <string> 4645-#include <memory> 4646-#include "src/expression/node.h" 4647-#include "inner/model_generated.h" 4648-#include "src/expression/cfg.h" 4649-#include "nnacl/pooling_parameter.h" 4650- 4651-namespace mindspore { 4652-namespace lite { 4653-class PoolingM : public Node { 4654- public: 4655- PoolingM() = default; 4656- explicit PoolingM(const PoolingConfig &cfg); 4657- template <typename T> 4658- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode); 4659- template <typename T> 4660- int UnPopulateGrad(const std::unique_ptr<schema::CNodeT> &cnode); 4661- std::vector<EXPR *> construct(const std::vector<EXPR *> &inputs); 4662- 4663- private: 4664- void UpdateRoundMode(const PoolingParameter *param, enum schema::RoundMode *round_mode); 4665- int GetMode(std::string mode); 4666-}; 4667- 4668-class MaxPoolM : public PoolingM { 4669- public: 4670- MaxPoolM() = default; 4671- explicit MaxPoolM(const PoolingConfig &cfg); 4672- std::vector<EXPR *> Grad(EXPR *expr) override; 4673- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 4674-}; 4675- 4676-class AvgPoolM : public PoolingM { 4677- public: 4678- AvgPoolM() = default; 4679- explicit AvgPoolM(const PoolingConfig &cfg); 4680- std::vector<EXPR *> Grad(EXPR *expr) override; 4681- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 4682-}; 4683- 4684-class MaxPoolGradM : public PoolingM { 4685- public: 4686- explicit MaxPoolGradM(MaxPoolM *node); 4687- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 4688-}; 4689- 4690-class AvgPoolGradM : public PoolingM { 4691- public: 4692- explicit AvgPoolGradM(AvgPoolM *node); 4693- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 4694-}; 4695-} // namespace lite 4696-} // namespace mindspore 4697-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_POOLING_H_ 4698diff --git a/mindspore/lite/src/expression/ops/reduce.cc b/mindspore/lite/src/expression/ops/reduce.cc 4699deleted file mode 100644 4700index 055d2026..00000000 4701--- a/mindspore/lite/src/expression/ops/reduce.cc 4702+++ /dev/null 4703@@ -1,126 +0,0 @@ 4704-/** 4705- * Copyright 2022 Huawei Technologies Co., Ltd 4706- * 4707- * Licensed under the Apache License, Version 2.0 (the "License"); 4708- * you may not use this file except in compliance with the License. 4709- * You may obtain a copy of the License at 4710- * 4711- * http://www.apache.org/licenses/LICENSE-2.0 4712- * 4713- * Unless required by applicable law or agreed to in writing, software 4714- * distributed under the License is distributed on an "AS IS" BASIS, 4715- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 4716- * See the License for the specific language governing permissions and 4717- * limitations under the License. 4718- */ 4719- 4720-#include "src/expression/ops/reduce.h" 4721-#include <functional> 4722-#include "src/expression/ops/tile.h" 4723-#include "src/expression/ops/reshape.h" 4724-#include "src/expression/ops/arithmetic.h" 4725-#include "src/expression/ops.h" 4726-#include "src/expression/ops_utils.h" 4727-#include "src/expression/import.h" 4728-#include "nnacl/reduce_parameter.h" 4729-#include "src/litert/cxx_api/expression/node_impl.h" 4730- 4731-namespace mindspore { 4732-namespace lite { 4733-ReduceM::ReduceM(schema::ReduceMode mode, bool keep_dims, const std::vector<int> &axis) : Node() { 4734- expr()->SetSize(C2NUM); 4735- ReduceParameter *param = reinterpret_cast<ReduceParameter *>(calloc(1, sizeof(ReduceParameter))); 4736- if (param == nullptr) { 4737- MS_LOG(ERROR) << "Cannot allocate parameter"; 4738- return; 4739- } 4740- param->mode_ = mode; 4741- param->keep_dims_ = keep_dims; 4742- param->reduce_to_end_ = false; 4743- param->coeff = 1.f; 4744- SetOpParam(param); 4745- set_name(UniqueName("Reduce")); 4746- set_primitive(schema::PrimitiveType_ReduceFusion); 4747- Node::CreateConstTensor(C1NUM, {static_cast<int32_t>(axis.size())}, kNumberTypeInt32, KHWC, "axis", axis.data()); 4748-} 4749- 4750-int ReduceM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 4751- auto reduce_param = reinterpret_cast<const ReduceParameter *>(OpParam()); 4752- auto prim = new (std::nothrow) schema::ReduceFusionT; 4753- if (prim == nullptr) { 4754- MS_LOG(ERROR) << "Cannot allocate primitive"; 4755- return RET_ERROR; 4756- } 4757- prim->keep_dims = reduce_param->keep_dims_; 4758- prim->mode = static_cast<schema::ReduceMode>(reduce_param->mode_); 4759- prim->coeff = reduce_param->coeff; 4760- prim->reduce_to_end = reduce_param->reduce_to_end_; 4761- cnode->primitive->value.value = prim; 4762- return RET_OK; 4763-} 4764- 4765-std::vector<EXPR *> ReduceM::Grad(EXPR *yt) { 4766- auto shape_of_x = input(0)->dims(); 4767- std::vector<int> shape_of_axis; 4768- 4769- auto data = input(1)->node()->data()->data().data(); 4770- int size = input(1)->dims().at(0); 4771- auto int_data = reinterpret_cast<int *>(data); 4772- for (int i = 0; i < size; i++) { 4773- shape_of_axis.push_back(int_data[i]); 4774- } 4775- 4776- // assume no dynamic shape 4777- ShapeReduce reduce_shape; 4778- auto output_shape_kept_dims = ShapeReduce()(shape_of_x, shape_of_axis); 4779- auto tile_scaling = VectorDiv()(shape_of_x, output_shape_kept_dims); 4780- auto reshape = NN::Reshape(output_shape_kept_dims); 4781- PushOp(reshape); 4782- reshape->set_name(name() + "/reshape"); 4783- auto g = (*reshape)({yt}).front(); 4784- auto tile = NN::Tile(tile_scaling); 4785- PushOp(tile); 4786- tile->set_name(name() + "/tile"); 4787- auto sum_grad = (*tile)({g}).front(); 4788- auto reduce_param = reinterpret_cast<const ReduceParameter *>(OpParam()); 4789- if (reduce_param->mode_ == schema::ReduceMode_ReduceSum) { 4790- return {sum_grad}; 4791- } else if (reduce_param->mode_ == schema::ReduceMode_ReduceMean) { 4792- auto shape_of_y = output(0)->dims(); 4793- auto shape_x_mul = std::accumulate(shape_of_x.begin(), shape_of_x.end(), 1, std::multiplies<int>()); 4794- auto shape_y_mul = std::accumulate(shape_of_y.begin(), shape_of_y.end(), 1, std::multiplies<int>()); 4795- auto div_shape = static_cast<float>(shape_x_mul) / static_cast<float>(shape_y_mul); 4796- auto div_op = NN::Div(); 4797- PushOp(div_op); 4798- auto d = div_op->CreateConstTensor(C1NUM, {1}, kNumberTypeFloat32, KHWC, "div_shape", &div_shape); 4799- auto dx = (*div_op)({sum_grad, d->expr()}); 4800- return dx; 4801- } else { 4802- return {}; 4803- } 4804-} 4805- 4806-static ImportReg reg(schema::PrimitiveType_ReduceFusion, ReturnNode<ReduceM>); 4807- 4808-namespace NN { 4809-Node *ReduceSum(bool keep_dims, const std::vector<int> &axis) { 4810- auto node = new (std::nothrow) ReduceM(schema::ReduceMode_ReduceSum, keep_dims, axis); 4811- if (node == nullptr) { 4812- MS_LOG(ERROR) << "Cannot allocate reduce sum node"; 4813- return nullptr; 4814- } 4815- node->set_name(Node::UniqueName("ReduceSum")); 4816- return node; 4817-} 4818-Node *ReduceMean(bool keep_dims, const std::vector<int> &axis) { 4819- auto node = new (std::nothrow) ReduceM(schema::ReduceMode_ReduceMean, keep_dims, axis); 4820- if (node == nullptr) { 4821- MS_LOG(ERROR) << "Cannot allocate reduce mean node"; 4822- return nullptr; 4823- } 4824- node->set_name(Node::UniqueName("ReduceMean")); 4825- return node; 4826-} 4827-} // namespace NN 4828-} // namespace lite 4829-} // namespace mindspore 4830diff --git a/mindspore/lite/src/expression/ops/reduce.h b/mindspore/lite/src/expression/ops/reduce.h 4831deleted file mode 100644 4832index 1ca0e921..00000000 4833--- a/mindspore/lite/src/expression/ops/reduce.h 4834+++ /dev/null 4835@@ -1,42 +0,0 @@ 4836-/** 4837- * Copyright 2022 Huawei Technologies Co., Ltd 4838- * 4839- * Licensed under the Apache License, Version 2.0 (the "License"); 4840- * you may not use this file except in compliance with the License. 4841- * You may obtain a copy of the License at 4842- * 4843- * http://www.apache.org/licenses/LICENSE-2.0 4844- * 4845- * Unless required by applicable law or agreed to in writing, software 4846- * distributed under the License is distributed on an "AS IS" BASIS, 4847- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 4848- * See the License for the specific language governing permissions and 4849- * limitations under the License. 4850- */ 4851- 4852-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_REDUCE_H_ 4853-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_REDUCE_H_ 4854- 4855-#include <vector> 4856-#include <memory> 4857-#include "src/expression/node.h" 4858-#include "inner/model_generated.h" 4859- 4860-namespace mindspore { 4861-namespace lite { 4862-class ReduceM : public Node { 4863- public: 4864- ReduceM() = default; 4865- ReduceM(schema::ReduceMode mode, bool keep_dims, const std::vector<int> &axis); 4866- Param *weight() override { return input(1)->node()->data(); } 4867- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 4868- std::vector<EXPR *> Grad(EXPR *expr) override; 4869-}; 4870- 4871-namespace NN { 4872-Node *ReduceMean(bool keep_dims, const std::vector<int> &axis); 4873-Node *ReduceSum(bool keep_dims, const std::vector<int> &axis); 4874-} // namespace NN 4875-} // namespace lite 4876-} // namespace mindspore 4877-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_REDUCE_H_ 4878diff --git a/mindspore/lite/src/expression/ops/reshape.cc b/mindspore/lite/src/expression/ops/reshape.cc 4879deleted file mode 100644 4880index d6cc0433..00000000 4881--- a/mindspore/lite/src/expression/ops/reshape.cc 4882+++ /dev/null 4883@@ -1,74 +0,0 @@ 4884-/** 4885- * Copyright 2022 Huawei Technologies Co., Ltd 4886- * 4887- * Licensed under the Apache License, Version 2.0 (the "License"); 4888- * you may not use this file except in compliance with the License. 4889- * You may obtain a copy of the License at 4890- * 4891- * http://www.apache.org/licenses/LICENSE-2.0 4892- * 4893- * Unless required by applicable law or agreed to in writing, software 4894- * distributed under the License is distributed on an "AS IS" BASIS, 4895- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 4896- * See the License for the specific language governing permissions and 4897- * limitations under the License. 4898- */ 4899- 4900-#include "src/expression/ops/reshape.h" 4901-#include "src/expression/ops.h" 4902-#include "nnacl/reshape_parameter.h" 4903-#include "src/expression/import.h" 4904- 4905-namespace mindspore { 4906-namespace lite { 4907-ReshapeM::ReshapeM(const std::vector<int> &shape) : Node() { 4908- auto op_param = calloc(1, sizeof(ReshapeParameter)); 4909- if (op_param == nullptr) { 4910- MS_LOG(ERROR) << " cannot allocate ReshapeParameter"; 4911- return; 4912- } 4913- set_name(UniqueName("Reshape")); 4914- expr()->SetSize(C2NUM); 4915- SetOpParam(op_param); 4916- set_primitive(schema::PrimitiveType_Reshape); 4917- 4918- ReshapeParameter *reshape_param = reinterpret_cast<ReshapeParameter *>(opParam_.get()); 4919- reshape_param->shape_dim_ = shape.size(); 4920- for (int i = 0; i < reshape_param->shape_dim_; i++) { 4921- reshape_param->shape_[i] = shape.at(i); 4922- } 4923- Node::CreateConstTensor(C1NUM, {static_cast<int32_t>(shape.size())}, kNumberTypeInt32, KHWC, "shape", shape.data()); 4924-} 4925- 4926-int ReshapeM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 4927- auto prim = new (std::nothrow) schema::ReshapeT; 4928- if (prim == nullptr) { 4929- MS_LOG(ERROR) << "Cannot allocate primitive"; 4930- return RET_ERROR; 4931- } 4932- cnode->primitive->value.value = prim; 4933- return RET_OK; 4934-} 4935- 4936-std::vector<EXPR *> ReshapeM::Grad(EXPR *yt) { 4937- auto shape_of_x = input(0)->dims(); 4938- auto reshape = NN::Reshape(shape_of_x); 4939- PushOp(reshape); 4940- return (*reshape)({yt}); 4941-} 4942- 4943-static ImportReg reg(schema::PrimitiveType_Reshape, ReturnNode<ReshapeM>); 4944- 4945-namespace NN { 4946-Node *Reshape(const std::vector<int> &shape) { 4947- auto node = new (std::nothrow) ReshapeM(shape); 4948- if (node == nullptr) { 4949- MS_LOG(ERROR) << "Cannot allocate reshape node"; 4950- return nullptr; 4951- } 4952- node->set_name(Node::UniqueName("Reshape")); 4953- return node; 4954-} 4955-} // namespace NN 4956-} // namespace lite 4957-} // namespace mindspore 4958diff --git a/mindspore/lite/src/expression/ops/reshape.h b/mindspore/lite/src/expression/ops/reshape.h 4959deleted file mode 100644 4960index a7c15377..00000000 4961--- a/mindspore/lite/src/expression/ops/reshape.h 4962+++ /dev/null 4963@@ -1,37 +0,0 @@ 4964-/** 4965- * Copyright 2022 Huawei Technologies Co., Ltd 4966- * 4967- * Licensed under the Apache License, Version 2.0 (the "License"); 4968- * you may not use this file except in compliance with the License. 4969- * You may obtain a copy of the License at 4970- * 4971- * http://www.apache.org/licenses/LICENSE-2.0 4972- * 4973- * Unless required by applicable law or agreed to in writing, software 4974- * distributed under the License is distributed on an "AS IS" BASIS, 4975- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 4976- * See the License for the specific language governing permissions and 4977- * limitations under the License. 4978- */ 4979- 4980-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_RESHAPE_H_ 4981-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_RESHAPE_H_ 4982- 4983-#include <vector> 4984-#include <memory> 4985- 4986-#include "src/expression/node.h" 4987-#include "inner/model_generated.h" 4988- 4989-namespace mindspore { 4990-namespace lite { 4991-class ReshapeM : public Node { 4992- public: 4993- ReshapeM() = default; 4994- explicit ReshapeM(const std::vector<int> &shape); 4995- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 4996- std::vector<EXPR *> Grad(EXPR *expr) override; 4997-}; 4998-} // namespace lite 4999-} // namespace mindspore 5000-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_RESHAPE_H_ 5001diff --git a/mindspore/lite/src/expression/ops/softmax.cc b/mindspore/lite/src/expression/ops/softmax.cc 5002deleted file mode 100644 5003index 29ab5ee1..00000000 5004--- a/mindspore/lite/src/expression/ops/softmax.cc 5005+++ /dev/null 5006@@ -1,119 +0,0 @@ 5007-/** 5008- * Copyright 2022 Huawei Technologies Co., Ltd 5009- * 5010- * Licensed under the Apache License, Version 2.0 (the "License"); 5011- * you may not use this file except in compliance with the License. 5012- * You may obtain a copy of the License at 5013- * 5014- * http://www.apache.org/licenses/LICENSE-2.0 5015- * 5016- * Unless required by applicable law or agreed to in writing, software 5017- * distributed under the License is distributed on an "AS IS" BASIS, 5018- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 5019- * See the License for the specific language governing permissions and 5020- * limitations under the License. 5021- */ 5022- 5023-#include "src/expression/ops/softmax.h" 5024-#include "nnacl/softmax_parameter.h" 5025-#include "inner/model_generated.h" 5026-#include "src/expression/import.h" 5027-#include "src/expression/ops/reshape.h" 5028-#include "src/expression/ops/reduce.h" 5029-#include "src/expression/ops/arithmetic.h" 5030-#include "src/expression/ops/transpose.h" 5031-#include "src/litert/cxx_api/expression/node_impl.h" 5032-#include "src/expression/ops.h" 5033- 5034-namespace mindspore { 5035-namespace lite { 5036-SoftmaxM::SoftmaxM(int axis) { 5037- auto param = reinterpret_cast<SoftmaxParameter *>(calloc(1, sizeof(SoftmaxParameter))); 5038- if (param == nullptr) { 5039- MS_LOG(ERROR) << "Cannot allocate parameter"; 5040- return; 5041- } 5042- param->axis_ = axis; 5043- SetOpParam(param); 5044- set_primitive(schema::PrimitiveType_Softmax); 5045- set_name(UniqueName("Softmax")); 5046-} 5047- 5048-std::vector<int> SoftmaxM::getTransposeAxis(const std::vector<int> &shape, int axis) { 5049- int rank = shape.size(); 5050- if (axis < 0) { 5051- axis += rank; 5052- } 5053- std::vector<int> reverse_axis(rank); 5054- std::iota(reverse_axis.begin(), reverse_axis.end(), 0); 5055- reverse_axis.at(axis) = rank - 1; 5056- reverse_axis.at(rank - 1) = axis; 5057- return reverse_axis; 5058-} 5059- 5060-std::vector<EXPR *> SoftmaxM::Grad(EXPR *yt) { 5061- auto x = input(0); 5062- auto out = output(0); 5063- auto shape_of_x = x->dims(); 5064- auto param = reinterpret_cast<const SoftmaxParameter *>(OpParam()); 5065- auto reverse_axis = getTransposeAxis(shape_of_x, param->axis_); 5066- 5067- auto transpose_out = NN::Transpose(reverse_axis); 5068- transpose_out->set_name(kGradName + "/" + name() + "/" + transpose_out->name() + "/out/"); 5069- PushOp(transpose_out); 5070- auto y_trn = (*transpose_out)({out}).front(); 5071- 5072- auto transpose_dout = NN::Transpose(reverse_axis); 5073- transpose_dout->set_name(kGradName + "/" + name() + "/" + transpose_dout->name() + "/dout/"); 5074- PushOp(transpose_dout); 5075- auto yt_trn = (*transpose_dout)({yt}).front(); 5076- 5077- auto mul0 = NN::Mul(); 5078- mul0->set_name(kGradName + "/" + name() + "/" + mul0->name() + "0"); 5079- PushOp(mul0); 5080- auto tmp0 = (*mul0)({y_trn, yt_trn}).front(); 5081- 5082- auto sum_func = NN::ReduceSum(true, {-1}); 5083- sum_func->set_name(kGradName + "/" + name() + "/" + sum_func->name()); 5084- PushOp(sum_func); 5085- auto tmp1 = (*sum_func)({tmp0}).front(); 5086- 5087- auto sub = NN::Sub(); 5088- sub->set_name(kGradName + "/" + name() + "/" + sub->name()); 5089- PushOp(sub); 5090- auto tmp2 = (*sub)({yt_trn, tmp1}).front(); 5091- 5092- auto mul1 = NN::Mul(); 5093- mul1->set_name(kGradName + "/" + name() + "/" + mul1->name() + "1"); 5094- PushOp(mul1); 5095- auto tmp3 = (*mul1)({y_trn, tmp2}); 5096- 5097- auto transpose_dx = NN::Transpose(reverse_axis); 5098- transpose_dx->set_name(kGradName + "/" + name() + "/" + transpose_dx->name() + "/dx"); 5099- PushOp(transpose_dx); 5100- auto dx = (*transpose_dx)({tmp3}); 5101- return dx; 5102-} 5103- 5104-int SoftmaxM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 5105- auto param = reinterpret_cast<const SoftmaxParameter *>(OpParam()); 5106- auto prim = new (std::nothrow) schema::SoftmaxT; 5107- if (prim == nullptr) { 5108- MS_LOG(ERROR) << "Cannot allocate primitive"; 5109- return RET_ERROR; 5110- } 5111- prim->axis.push_back(param->axis_); 5112- cnode->primitive->value.value = prim; 5113- return RET_OK; 5114-} 5115- 5116-static ImportReg reg(schema::PrimitiveType_Softmax, ReturnNode<SoftmaxM>); 5117- 5118-namespace NN { 5119-Node *Softmax(int axis) { 5120- auto node = new (std::nothrow) SoftmaxM(axis); 5121- return node; 5122-} 5123-} // namespace NN 5124-} // namespace lite 5125-} // namespace mindspore 5126diff --git a/mindspore/lite/src/expression/ops/softmax.h b/mindspore/lite/src/expression/ops/softmax.h 5127deleted file mode 100644 5128index 7347984e..00000000 5129--- a/mindspore/lite/src/expression/ops/softmax.h 5130+++ /dev/null 5131@@ -1,39 +0,0 @@ 5132-/** 5133- * Copyright 2022 Huawei Technologies Co., Ltd 5134- * 5135- * Licensed under the Apache License, Version 2.0 (the "License"); 5136- * you may not use this file except in compliance with the License. 5137- * You may obtain a copy of the License at 5138- * 5139- * http://www.apache.org/licenses/LICENSE-2.0 5140- * 5141- * Unless required by applicable law or agreed to in writing, software 5142- * distributed under the License is distributed on an "AS IS" BASIS, 5143- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 5144- * See the License for the specific language governing permissions and 5145- * limitations under the License. 5146- */ 5147- 5148-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_SOFTMAX_H_ 5149-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_SOFTMAX_H_ 5150- 5151-#include <vector> 5152-#include <memory> 5153-#include "src/expression/node.h" 5154- 5155-namespace mindspore { 5156-namespace lite { 5157-class SoftmaxM : public Node { 5158- public: 5159- SoftmaxM() = default; 5160- explicit SoftmaxM(int axis); 5161- std::vector<EXPR *> Grad(EXPR *expr) override; 5162- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 5163- 5164- private: 5165- std::vector<int> getTransposeAxis(const std::vector<int> &shape, int axis); 5166-}; 5167-} // namespace lite 5168-} // namespace mindspore 5169- 5170-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_SOFTMAX_H_ 5171diff --git a/mindspore/lite/src/expression/ops/softmaxCE.cc b/mindspore/lite/src/expression/ops/softmaxCE.cc 5172deleted file mode 100644 5173index f4ffce87..00000000 5174--- a/mindspore/lite/src/expression/ops/softmaxCE.cc 5175+++ /dev/null 5176@@ -1,93 +0,0 @@ 5177-/** 5178- * Copyright 2022 Huawei Technologies Co., Ltd 5179- * 5180- * Licensed under the Apache License, Version 2.0 (the "License"); 5181- * you may not use this file except in compliance with the License. 5182- * You may obtain a copy of the License at 5183- * 5184- * http://www.apache.org/licenses/LICENSE-2.0 5185- * 5186- * Unless required by applicable law or agreed to in writing, software 5187- * distributed under the License is distributed on an "AS IS" BASIS, 5188- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 5189- * See the License for the specific language governing permissions and 5190- * limitations under the License. 5191- */ 5192- 5193-#include "src/expression/ops/softmaxCE.h" 5194-#include "include/api/net.h" 5195-#include "src/litert/cxx_api/expression/node_impl.h" 5196-#include "src/expression/ops/reduce.h" 5197-namespace mindspore { 5198-namespace NN { 5199-Node *SoftmaxCrossEntropy(const SoftMaxCrossEntropyCfg &cfg) { 5200- auto lite_node = lite::NN::SoftmaxCrossEntropy(cfg); 5201- return NodeImpl::Connect(lite_node); 5202-} 5203-} // namespace NN 5204- 5205-namespace lite { 5206-SoftmaxCrossEntropyM::SoftmaxCrossEntropyM() { 5207- auto param = calloc(1, sizeof(OpParameter)); 5208- if (param == nullptr) { 5209- MS_LOG(ERROR) << "Cannot allocate parameter"; 5210- return; 5211- } 5212- expr()->SetSize(C2NUM); 5213- SetOpParam(param); 5214- set_name("SoftmaxCrossEntropy"); 5215- set_primitive(schema::PrimitiveType_SoftmaxCrossEntropyWithLogits); 5216- EXPR e(this); 5217- e.SetSize(0); 5218- expr_.emplace_back(e); 5219-} 5220- 5221-Node *SoftmaxCrossEntropyM::GetReductionNode(const std::string &mode, const std::vector<int> &axis) { 5222- if (mode == "mean") { 5223- return NN::ReduceMean(false, axis); 5224- } else if (mode == "sum") { 5225- return NN::ReduceSum(false, axis); 5226- } else { 5227- return nullptr; 5228- } 5229-} 5230- 5231-SoftmaxCrossEntropyM::SoftmaxCrossEntropyM(const SoftMaxCrossEntropyCfg &cfg) : SoftmaxCrossEntropyM() { 5232- std::vector<int> axis = {0}; 5233- reduce_ = GetReductionNode(cfg.reduction, axis); 5234- if (reduce_ != nullptr) { 5235- PushOp(reduce_); 5236- } 5237-} 5238- 5239-std::vector<EXPR *> SoftmaxCrossEntropyM::construct(const std::vector<EXPR *> &inputs) { 5240- auto y = Node::construct(inputs); 5241- if (reduce_ != nullptr) { 5242- y = (*reduce_)({y.front()}); 5243- } 5244- return y; 5245-} 5246- 5247-std::vector<EXPR *> SoftmaxCrossEntropyM::Grad(EXPR *expr) { return {this->expr(1)}; } 5248- 5249-int SoftmaxCrossEntropyM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 5250- auto prim = new (std::nothrow) schema::SoftmaxCrossEntropyWithLogitsT; 5251- if (prim == nullptr) { 5252- MS_LOG(ERROR) << "Cannot allocate primitive"; 5253- return RET_ERROR; 5254- } 5255- cnode->primitive->value.value = prim; 5256- return RET_OK; 5257-} 5258- 5259-namespace NN { 5260-Node *SoftmaxCrossEntropy(const SoftMaxCrossEntropyCfg &cfg) { 5261- auto s = new (std::nothrow) SoftmaxCrossEntropyM(cfg); 5262- if (s == nullptr) { 5263- MS_LOG(ERROR) << "Cannot allocate softmax node"; 5264- } 5265- return s; 5266-} 5267-} // namespace NN 5268-} // namespace lite 5269-} // namespace mindspore 5270diff --git a/mindspore/lite/src/expression/ops/softmaxCE.h b/mindspore/lite/src/expression/ops/softmaxCE.h 5271deleted file mode 100644 5272index 1c0c516d..00000000 5273--- a/mindspore/lite/src/expression/ops/softmaxCE.h 5274+++ /dev/null 5275@@ -1,47 +0,0 @@ 5276-/** 5277- * Copyright 2022 Huawei Technologies Co., Ltd 5278- * 5279- * Licensed under the Apache License, Version 2.0 (the "License"); 5280- * you may not use this file except in compliance with the License. 5281- * You may obtain a copy of the License at 5282- * 5283- * http://www.apache.org/licenses/LICENSE-2.0 5284- * 5285- * Unless required by applicable law or agreed to in writing, software 5286- * distributed under the License is distributed on an "AS IS" BASIS, 5287- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 5288- * See the License for the specific language governing permissions and 5289- * limitations under the License. 5290- */ 5291- 5292-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_SOFTMAXCE_H_ 5293-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_SOFTMAXCE_H_ 5294- 5295-#include <vector> 5296-#include <memory> 5297-#include <string> 5298-#include "src/expression/node.h" 5299-#include "inner/model_generated.h" 5300-#include "include/api/net.h" 5301- 5302-namespace mindspore { 5303-namespace lite { 5304-class SoftmaxCrossEntropyM : public Node { 5305- public: 5306- SoftmaxCrossEntropyM(); 5307- explicit SoftmaxCrossEntropyM(const SoftMaxCrossEntropyCfg &cfg); 5308- std::vector<EXPR *> Grad(EXPR *expr) override; 5309- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 5310- std::vector<EXPR *> construct(const std::vector<EXPR *> &inputs) override; 5311- 5312- private: 5313- Node *GetReductionNode(const std::string &mode, const std::vector<int> &axis); 5314- Node *reduce_ = nullptr; 5315-}; 5316- 5317-namespace NN { 5318-Node *SoftmaxCrossEntropy(const SoftMaxCrossEntropyCfg &cfg); 5319-} // namespace NN 5320-} // namespace lite 5321-} // namespace mindspore 5322-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_SOFTMAXCE_H_ 5323diff --git a/mindspore/lite/src/expression/ops/tile.cc b/mindspore/lite/src/expression/ops/tile.cc 5324deleted file mode 100644 5325index 4008da12..00000000 5326--- a/mindspore/lite/src/expression/ops/tile.cc 5327+++ /dev/null 5328@@ -1,62 +0,0 @@ 5329-/** 5330- * Copyright 2022 Huawei Technologies Co., Ltd 5331- * 5332- * Licensed under the Apache License, Version 2.0 (the "License"); 5333- * you may not use this file except in compliance with the License. 5334- * You may obtain a copy of the License at 5335- * 5336- * http://www.apache.org/licenses/LICENSE-2.0 5337- * 5338- * Unless required by applicable law or agreed to in writing, software 5339- * distributed under the License is distributed on an "AS IS" BASIS, 5340- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 5341- * See the License for the specific language governing permissions and 5342- * limitations under the License. 5343- */ 5344- 5345-#include "src/expression/ops/tile.h" 5346-#include <memory> 5347-#include "src/expression/ops.h" 5348-#include "nnacl/base/tile_base.h" 5349- 5350-namespace mindspore { 5351-namespace lite { 5352-TileM::TileM(const std::vector<int> &multiples) : Node() { 5353- expr()->SetSize(C2NUM); 5354- TileParameter *param = reinterpret_cast<TileParameter *>(calloc(1, sizeof(TileParameter))); 5355- if (param == nullptr) { 5356- MS_LOG(ERROR) << " cannot allocate ConvParameter"; 5357- return; 5358- } 5359- SetOpParam(param); 5360- set_name(UniqueName("Tile")); 5361- set_primitive(schema::PrimitiveType_TileFusion); 5362- Node::CreateConstTensor(C1NUM, {static_cast<int32_t>(multiples.size())}, kNumberTypeInt32, KHWC, "axis", 5363- multiples.data()); 5364-} 5365- 5366-int TileM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 5367- auto tile_param = reinterpret_cast<const TileParameter *>(OpParam()); 5368- auto prim = new (std::nothrow) schema::TileFusionT; 5369- if (prim == nullptr) { 5370- MS_LOG(ERROR) << "Cannot allocate primitive"; 5371- return RET_ERROR; 5372- } 5373- for (size_t i = 0; i < tile_param->dims_size_; i++) { 5374- prim->dims.push_back(tile_param->dims_[i]); 5375- } 5376- cnode->primitive->value.value = prim; 5377- return RET_OK; 5378-} 5379- 5380-namespace NN { 5381-Node *Tile(const std::vector<int> &multiples) { 5382- auto node = new (std::nothrow) TileM(multiples); 5383- if (node == nullptr) { 5384- MS_LOG(ERROR) << "Cannot allocate tile node"; 5385- } 5386- return node; 5387-} 5388-} // namespace NN 5389-} // namespace lite 5390-} // namespace mindspore 5391diff --git a/mindspore/lite/src/expression/ops/tile.h b/mindspore/lite/src/expression/ops/tile.h 5392deleted file mode 100644 5393index 0d793c0e..00000000 5394--- a/mindspore/lite/src/expression/ops/tile.h 5395+++ /dev/null 5396@@ -1,40 +0,0 @@ 5397-/** 5398- * Copyright 2022 Huawei Technologies Co., Ltd 5399- * 5400- * Licensed under the Apache License, Version 2.0 (the "License"); 5401- * you may not use this file except in compliance with the License. 5402- * You may obtain a copy of the License at 5403- * 5404- * http://www.apache.org/licenses/LICENSE-2.0 5405- * 5406- * Unless required by applicable law or agreed to in writing, software 5407- * distributed under the License is distributed on an "AS IS" BASIS, 5408- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 5409- * See the License for the specific language governing permissions and 5410- * limitations under the License. 5411- */ 5412- 5413-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_TILE_H_ 5414-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_TILE_H_ 5415- 5416-#include <vector> 5417-#include <memory> 5418-#include "src/expression/node.h" 5419-#include "inner/model_generated.h" 5420- 5421-namespace mindspore { 5422-namespace lite { 5423-class TileM : public Node { 5424- public: 5425- TileM() = default; 5426- explicit TileM(const std::vector<int> &multiples); 5427- Param *weight() override { return input(1)->node()->data(); } 5428- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 5429-}; 5430- 5431-namespace NN { 5432-Node *Tile(const std::vector<int> &multiples); 5433-} 5434-} // namespace lite 5435-} // namespace mindspore 5436-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_TILE_H_ 5437diff --git a/mindspore/lite/src/expression/ops/transpose.cc b/mindspore/lite/src/expression/ops/transpose.cc 5438deleted file mode 100644 5439index fbe2b14d..00000000 5440--- a/mindspore/lite/src/expression/ops/transpose.cc 5441+++ /dev/null 5442@@ -1,88 +0,0 @@ 5443-/** 5444- * Copyright 2022 Huawei Technologies Co., Ltd 5445- * 5446- * Licensed under the Apache License, Version 2.0 (the "License"); 5447- * you may not use this file except in compliance with the License. 5448- * You may obtain a copy of the License at 5449- * 5450- * http://www.apache.org/licenses/LICENSE-2.0 5451- * 5452- * Unless required by applicable law or agreed to in writing, software 5453- * distributed under the License is distributed on an "AS IS" BASIS, 5454- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 5455- * See the License for the specific language governing permissions and 5456- * limitations under the License. 5457- */ 5458- 5459-#include "src/expression/ops/transpose.h" 5460-#include <memory> 5461-#include "nnacl/transpose_parameter.h" 5462-#include "inner/model_generated.h" 5463-#include "src/expression/import.h" 5464- 5465-namespace mindspore { 5466-namespace lite { 5467-TransposeM::TransposeM(const std::vector<int> &vector) { 5468- auto param = calloc(1, sizeof(TransposeParameter)); 5469- if (param == nullptr) { 5470- MS_LOG(ERROR) << "Cannot allocate transpose parameter"; 5471- return; 5472- } 5473- SetOpParam(param); 5474- expr()->SetSize(C2NUM); 5475- set_primitive(schema::PrimitiveType_Transpose); 5476- std::vector<int> dims = {static_cast<int>(vector.size())}; 5477- set_name(UniqueName("Transpose")); 5478- CreateConstTensor(C1NUM, dims, kNumberTypeInt32, KHWC, "axis", vector.data()); 5479-} 5480- 5481-std::vector<int> TransposeM::Invert(const std::vector<int> &vector) { 5482- std::vector<int> res; 5483- for (size_t i = 0; i < vector.size(); i++) { 5484- int idx = static_cast<int>(i); 5485- auto val = std::find_if(vector.begin(), vector.end(), [idx](int x) { return (x == idx) ? true : false; }); 5486- if (val == vector.end()) { 5487- MS_LOG(ERROR) << "Wrong index for " << idx; 5488- return {}; 5489- } 5490- res.push_back(std::distance(vector.begin(), val)); 5491- } 5492- return res; 5493-} 5494- 5495-std::vector<EXPR *> TransposeM::Grad(EXPR *yt) { 5496- auto tensor = input(1)->node(); 5497- auto data = tensor->data(); 5498- auto vec = data->Extract<int>(); 5499- auto invert = Invert(vec); 5500- auto tran = new (std::nothrow) TransposeM(invert); 5501- if (tran == nullptr) { 5502- MS_LOG(ERROR) << "Cannot allocate transpose grad"; 5503- return {}; 5504- } 5505- tran->set_name(kGradName + "/" + name() + "/" + tran->name()); 5506- PushOp(tran); 5507- auto grad = (*tran)({yt}); 5508- return grad; 5509-} 5510- 5511-int TransposeM::UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) { 5512- auto prim = new (std::nothrow) schema::TransposeT; 5513- if (prim == nullptr) { 5514- MS_LOG(ERROR) << "Cannot allocate primitive"; 5515- return RET_ERROR; 5516- } 5517- cnode->primitive->value.value = prim; 5518- return RET_OK; 5519-} 5520- 5521-static ImportReg reg(schema::PrimitiveType_Transpose, ReturnNode<TransposeM>); 5522- 5523-namespace NN { 5524-Node *Transpose(const std::vector<int> &permute) { 5525- auto node = new (std::nothrow) TransposeM(permute); 5526- return node; 5527-} 5528-} // namespace NN 5529-} // namespace lite 5530-} // namespace mindspore 5531diff --git a/mindspore/lite/src/expression/ops/transpose.h b/mindspore/lite/src/expression/ops/transpose.h 5532deleted file mode 100644 5533index d4c9a1c1..00000000 5534--- a/mindspore/lite/src/expression/ops/transpose.h 5535+++ /dev/null 5536@@ -1,59 +0,0 @@ 5537-/** 5538- * Copyright 2022 Huawei Technologies Co., Ltd 5539- * 5540- * Licensed under the Apache License, Version 2.0 (the "License"); 5541- * you may not use this file except in compliance with the License. 5542- * You may obtain a copy of the License at 5543- * 5544- * http://www.apache.org/licenses/LICENSE-2.0 5545- * 5546- * Unless required by applicable law or agreed to in writing, software 5547- * distributed under the License is distributed on an "AS IS" BASIS, 5548- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 5549- * See the License for the specific language governing permissions and 5550- * limitations under the License. 5551- */ 5552- 5553-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_TRANSPOSE_H_ 5554-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_TRANSPOSE_H_ 5555- 5556-#include <vector> 5557-#include <memory> 5558-#include "src/expression/node.h" 5559-#include "inner/model_generated.h" 5560- 5561-namespace mindspore { 5562-namespace lite { 5563-class TransposeM : public Node { 5564- public: 5565- TransposeM() = default; 5566- explicit TransposeM(const std::vector<int> &vector); 5567- static EXPR *TransposeCHW2HWC(EXPR *in) { 5568- std::vector<int> res = {0, 2, 3, 1}; 5569- auto trans = new (std::nothrow) TransposeM(res); 5570- if (trans == nullptr) { 5571- return nullptr; 5572- } 5573- return (*trans)({in}).front(); 5574- } 5575- static EXPR *TransposeHWC2CHW(EXPR *in) { 5576- std::vector<int> res = {0, 3, 1, 2}; 5577- auto trans = new (std::nothrow) TransposeM(res); 5578- if (trans == nullptr) { 5579- return nullptr; 5580- } 5581- return (*trans)({in}).front(); 5582- } 5583- int UnPopulate(const std::unique_ptr<schema::CNodeT> &cnode) override; 5584- std::vector<EXPR *> Grad(EXPR *yt) override; 5585- 5586- private: 5587- std::vector<int> Invert(const std::vector<int> &vec); 5588-}; 5589- 5590-namespace NN { 5591-Node *Transpose(const std::vector<int> &permute); 5592-} // namespace NN 5593-} // namespace lite 5594-} // namespace mindspore 5595-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_TRANSPOSE_H_ 5596diff --git a/mindspore/lite/src/expression/ops_utils.cc b/mindspore/lite/src/expression/ops_utils.cc 5597deleted file mode 100644 5598index e63b903d..00000000 5599--- a/mindspore/lite/src/expression/ops_utils.cc 5600+++ /dev/null 5601@@ -1,275 +0,0 @@ 5602-/** 5603- * Copyright 2022 Huawei Technologies Co., Ltd 5604- * 5605- * Licensed under the Apache License, Version 2.0 (the "License"); 5606- * you may not use this file except in compliance with the License. 5607- * You may obtain a copy of the License at 5608- * 5609- * http://www.apache.org/licenses/LICENSE-2.0 5610- * 5611- * Unless required by applicable law or agreed to in writing, software 5612- * distributed under the License is distributed on an "AS IS" BASIS, 5613- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 5614- * See the License for the specific language governing permissions and 5615- * limitations under the License. 5616- */ 5617- 5618-#include "src/expression/ops_utils.h" 5619-#include <set> 5620-#include <algorithm> 5621- 5622-namespace mindspore { 5623-namespace lite { 5624-enum class State { 5625- SAME, 5626- X_ONE, 5627- Y_ONE, 5628-}; 5629- 5630-bool CompareShape(const std::vector<int> &x_shape, const std::vector<int> &y_shape) { 5631- if (x_shape.size() != y_shape.size()) { 5632- return false; 5633- } 5634- 5635- for (size_t i = 0; i < x_shape.size(); ++i) { 5636- if (x_shape.at(i) != y_shape.at(i)) { 5637- return false; 5638- } 5639- } 5640- 5641- return true; 5642-} 5643- 5644-void ComputeReduceIndex(const std::vector<int> &reverse_x, const std::vector<int> &reverse_y, 5645- std::vector<int> *grad_x_reduce_idx, std::vector<int> *grad_y_reduce_idy) { 5646- MS_ASSERT(grad_x_reduce_idx != nullptr); 5647- MS_ASSERT(grad_y_reduce_idy != nullptr); 5648- const size_t n = reverse_x.size(); 5649- if (reverse_y.size() < n) { 5650- MS_LOG_ERROR << "The size of reverse_y is less than the size of reverse_x."; 5651- } 5652- for (size_t i = 0; i < n; ++i) { 5653- State curr = State::SAME; 5654- const int x_i = reverse_x[i]; 5655- const int y_i = reverse_y[i]; 5656- const int reduce_idx = (n - 1 - i); 5657- if (x_i == y_i) { 5658- curr = State::SAME; 5659- } else if (x_i == 1) { 5660- grad_x_reduce_idx->push_back(reduce_idx); 5661- curr = State::X_ONE; 5662- } else if (y_i == 1) { 5663- grad_y_reduce_idy->push_back(reduce_idx); 5664- curr = State::Y_ONE; 5665- } else { 5666- MS_LOG_ERROR << "not compatible shape input for BroadcastGradientArgs"; 5667- } 5668- if (curr == State::SAME && x_i == 1) { 5669- grad_x_reduce_idx->push_back(reduce_idx); 5670- grad_y_reduce_idy->push_back(reduce_idx); 5671- continue; 5672- } 5673- } 5674- 5675- std::reverse(grad_x_reduce_idx->begin(), grad_x_reduce_idx->end()); 5676- std::reverse(grad_y_reduce_idy->begin(), grad_y_reduce_idy->end()); 5677-} 5678- 5679-std::vector<std::vector<int>> BroadcastGradientArgs::operator()() { 5680- std::vector<std::vector<int>> input_dim(kInNum); 5681- input_dim[0] = dim0_; 5682- input_dim[1] = dim1_; 5683- auto same_shape = CompareShape(dim0_, dim1_); 5684- if (same_shape) { 5685- return {{}, {}}; 5686- } 5687- 5688- std::vector<int> reverse_x; 5689- std::vector<int> reverse_y; 5690- 5691- (void)std::transform(dim0_.rbegin(), dim0_.rend(), std::back_inserter(reverse_x), [](const int &v) { return v; }); 5692- (void)std::transform(dim1_.rbegin(), dim1_.rend(), std::back_inserter(reverse_y), [](const int &v) { return v; }); 5693- 5694- if (reverse_x.size() > reverse_y.size()) { 5695- reverse_y.resize(reverse_x.size(), 1); 5696- } else { 5697- reverse_x.resize(reverse_y.size(), 1); 5698- } 5699- 5700- std::vector<int> grad_x_reduce_idx; 5701- std::vector<int> grad_y_reduce_idy; 5702- ComputeReduceIndex(reverse_x, reverse_y, &grad_x_reduce_idx, &grad_y_reduce_idy); 5703- return {grad_x_reduce_idx, grad_y_reduce_idy}; 5704-} 5705- 5706-void DynamicBroadcastGradientArgs::AddElementToGradReduceIdx(std::vector<std::vector<int>> *grad_reduce_idx, 5707- std::vector<bool> current_is_one, bool none_is_one, 5708- const size_t largest_rank, size_t j) { 5709- for (size_t i = 0; i < kInNum; ++i) { 5710- if (current_is_one[i] && !none_is_one) { 5711- (void)(*grad_reduce_idx)[i].emplace_back(largest_rank - 1 - j); 5712- } 5713- } 5714-} 5715- 5716-void DynamicBroadcastGradientArgs::UpdatePreIsOne(std::vector<bool> *prev_is_one, std::vector<bool> current_is_one) { 5717- for (size_t i = 0; i < kInNum; ++i) { 5718- (*prev_is_one)[i] = current_is_one[i]; 5719- } 5720-} 5721- 5722-std::vector<std::vector<int>> DynamicBroadcastGradientArgs::GetGradientIndices( 5723- const std::vector<std::vector<int>> &reverse_shape, const size_t largest_rank) { 5724- std::vector<std::vector<int>> grad_reduce_idx(kInNum); 5725- // indices of j-th component of each input. 5726- std::vector<bool> prev_is_one(kInNum); 5727- std::vector<bool> current_is_one(kInNum); 5728- for (size_t i = 0; i < kInNum; ++i) { 5729- prev_is_one[i] = false; 5730- current_is_one[i] = false; 5731- } 5732- 5733- bool set_one = false; 5734- for (size_t j = 0; j < largest_rank; ++j) { 5735- int output_dim = -1; 5736- bool output_dim_set = false; 5737- bool none_is_one = true; 5738- // Find which indices are 1. 5739- for (size_t i = 0; i < kInNum; ++i) { 5740- if (reverse_shape[i][j] == 1) { 5741- current_is_one[i] = true; 5742- none_is_one = false; 5743- } else { 5744- current_is_one[i] = false; 5745- if (!output_dim_set || reverse_shape[i][j] == static_cast<int>(output_dim)) { 5746- output_dim = reverse_shape[i][j]; 5747- output_dim_set = true; 5748- } else { 5749- std::cout << "Input[0] and input[1] Cannot broadcast!"; 5750- } 5751- } 5752- } 5753- // All dimensions are 1. 5754- if (!output_dim_set) { 5755- for (size_t i = 0; i < kInNum; ++i) { 5756- (void)grad_reduce_idx[i].emplace_back(largest_rank - 1 - j); 5757- } 5758- continue; 5759- } else if (std::equal(current_is_one.begin(), current_is_one.end(), prev_is_one.begin()) && set_one) { 5760- AddElementToGradReduceIdx(&grad_reduce_idx, current_is_one, none_is_one, largest_rank, j); 5761- } else { 5762- AddElementToGradReduceIdx(&grad_reduce_idx, current_is_one, none_is_one, largest_rank, j); 5763- } 5764- set_one = true; 5765- UpdatePreIsOne(&prev_is_one, current_is_one); 5766- } 5767- return grad_reduce_idx; 5768-} 5769- 5770-std::vector<std::vector<int>> DynamicBroadcastGradientArgs::CalculateOutput(const std::vector<std::vector<int>> &x) { 5771- std::vector<std::vector<int>> grad_reduce_idx(kInNum); 5772- bool all_equal = true; 5773- size_t largest_rank = 0; 5774- for (size_t i = 0; i < kInNum; ++i) { 5775- if (x[i] != x[0]) { 5776- all_equal = false; 5777- } 5778- if (x[i].size() > largest_rank) { 5779- largest_rank = x[i].size(); 5780- } 5781- } 5782- if (all_equal) { 5783- return grad_reduce_idx; 5784- } 5785- 5786- // Reverse input the shapes 5787- std::vector<std::vector<int>> reverse_shape(kInNum); 5788- for (size_t i = 0; i < kInNum; ++i) { 5789- reverse_shape[i] = x[i]; 5790- std::reverse(reverse_shape[i].begin(), reverse_shape[i].end()); 5791- } 5792- 5793- // 1-extend and align all vectors. 5794- for (size_t i = 0; i < kInNum; ++i) { 5795- if (reverse_shape[i].size() < largest_rank) { 5796- reverse_shape[i].resize(largest_rank, 1); 5797- } 5798- } 5799- grad_reduce_idx = GetGradientIndices(reverse_shape, largest_rank); 5800- return grad_reduce_idx; 5801-} 5802- 5803-std::vector<std::vector<int>> DynamicBroadcastGradientArgs::SetOutputValue( 5804- const std::vector<std::vector<int>> &grad_reduce_idx, const std::vector<std::vector<int>> &input_dim) { 5805- std::vector<std::vector<int>> output(kInNum); 5806- for (size_t index = 0; index < kInNum; ++index) { 5807- auto idx_num = grad_reduce_idx[index].size(); 5808- for (size_t k = 0; k < idx_num; ++k) { 5809- output[index].push_back(grad_reduce_idx[index][idx_num - 1 - k]); 5810- } 5811- if (idx_num == 0) { 5812- auto input_num = input_dim[index].size(); 5813- for (size_t k = 0; k < input_num; ++k) { 5814- output[index].push_back(k); 5815- } 5816- } 5817- } 5818- return output; 5819-} 5820- 5821-std::vector<std::vector<int>> DynamicBroadcastGradientArgs::operator()() { 5822- std::vector<std::vector<int>> input_dim(kInNum); 5823- input_dim[0] = dim0_; 5824- input_dim[1] = dim1_; 5825- auto grad_reduce_idx = CalculateOutput(input_dim); 5826- auto output = SetOutputValue(grad_reduce_idx, input_dim); 5827- return output; 5828-} 5829- 5830-std::vector<int> VectorDiv::operator()(const std::vector<int> &x, const std::vector<int> &d) { 5831- if (d.size() != x.size()) { 5832- MS_LOG(ERROR) << "x and divider must have same size"; 5833- return {}; 5834- } 5835- std::vector<int> res; 5836- for (size_t i = 0; i < d.size(); i++) { 5837- auto x_value = x.at(i); 5838- auto d_value = d.at(i); 5839- if (d_value == 0) { 5840- MS_LOG(ERROR) << "Divisor is zero"; 5841- return {}; 5842- } 5843- if ((x_value % d_value) != 0) { 5844- MS_LOG(ERROR) << "x and d and not dividable"; 5845- } 5846- auto r = x_value / d_value; 5847- res.push_back(r); 5848- } 5849- return res; 5850-} 5851- 5852-std::vector<int> ShapeReduce::operator()(const std::vector<int> &x_shape, const std::vector<int> &axis) { 5853- int x_rank = x_shape.size(); 5854- std::set<int> axis_set; 5855- 5856- auto min = -x_rank; 5857- auto max = x_rank - 1; 5858- for (auto &elem : axis) { 5859- if (elem > max || elem < min) { 5860- MS_LOG(ERROR) << "illegal axis value"; 5861- return {}; 5862- } 5863- axis_set.insert(elem); 5864- } 5865- std::vector<int> res; 5866- for (int i = 0; i < x_rank; i++) { 5867- if (axis_set.count(i) || axis_set.count(i - x_rank)) { 5868- res.push_back(1); 5869- } else { 5870- res.push_back(x_shape.at(i)); 5871- } 5872- } 5873- return res; 5874-} 5875-} // namespace lite 5876-} // namespace mindspore 5877diff --git a/mindspore/lite/src/expression/ops_utils.h b/mindspore/lite/src/expression/ops_utils.h 5878deleted file mode 100644 5879index 6c62de11..00000000 5880--- a/mindspore/lite/src/expression/ops_utils.h 5881+++ /dev/null 5882@@ -1,69 +0,0 @@ 5883-/** 5884- * Copyright 2022 Huawei Technologies Co., Ltd 5885- * 5886- * Licensed under the Apache License, Version 2.0 (the "License"); 5887- * you may not use this file except in compliance with the License. 5888- * You may obtain a copy of the License at 5889- * 5890- * http://www.apache.org/licenses/LICENSE-2.0 5891- * 5892- * Unless required by applicable law or agreed to in writing, software 5893- * distributed under the License is distributed on an "AS IS" BASIS, 5894- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 5895- * See the License for the specific language governing permissions and 5896- * limitations under the License. 5897- */ 5898- 5899-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_UTILS_H_ 5900-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_UTILS_H_ 5901- 5902-#include "include/api/cfg.h" 5903-#include "src/expression/net.h" 5904-#include "vector" 5905- 5906-namespace mindspore { 5907-namespace lite { 5908-class BroadcastGradientArgs { 5909- public: 5910- BroadcastGradientArgs(const std::vector<int> &dim0, const std::vector<int> &dim1) : dim0_(dim0), dim1_(dim1) {} 5911- std::vector<std::vector<int>> operator()(); 5912- 5913- private: 5914- static const int kInNum = 2; 5915- const std::vector<int> &dim0_; 5916- const std::vector<int> &dim1_; 5917-}; 5918- 5919-class DynamicBroadcastGradientArgs { 5920- public: 5921- DynamicBroadcastGradientArgs(const std::vector<int> &dim0, const std::vector<int> &dim1) : dim0_(dim0), dim1_(dim1) {} 5922- std::vector<std::vector<int>> operator()(); 5923- 5924- private: 5925- void AddElementToGradReduceIdx(std::vector<std::vector<int>> *grad_reduce_idx, std::vector<bool> current_is_one, 5926- bool none_is_one, const size_t largest_rank, size_t j); 5927- void UpdatePreIsOne(std::vector<bool> *prev_is_one, std::vector<bool> current_is_one); 5928- std::vector<std::vector<int>> GetGradientIndices(const std::vector<std::vector<int>> &reverse_shape, 5929- const size_t largest_rank); 5930- std::vector<std::vector<int>> CalculateOutput(const std::vector<std::vector<int>> &x); 5931- std::vector<std::vector<int>> SetOutputValue(const std::vector<std::vector<int>> &grad_reduce_idx, 5932- const std::vector<std::vector<int>> &input_dim); 5933- static const int kInNum = 2; 5934- const std::vector<int> &dim0_; 5935- const std::vector<int> &dim1_; 5936-}; 5937- 5938-class VectorDiv { 5939- public: 5940- VectorDiv() {} 5941- std::vector<int> operator()(const std::vector<int> &x, const std::vector<int> &d); 5942-}; 5943- 5944-class ShapeReduce { 5945- public: 5946- ShapeReduce() {} 5947- std::vector<int> operator()(const std::vector<int> &x_shape, const std::vector<int> &axis); 5948-}; 5949-} // namespace lite 5950-} // namespace mindspore 5951-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_UTILS_H_ 5952diff --git a/mindspore/lite/src/expression/param.cc b/mindspore/lite/src/expression/param.cc 5953deleted file mode 100644 5954index 284cf141..00000000 5955--- a/mindspore/lite/src/expression/param.cc 5956+++ /dev/null 5957@@ -1,70 +0,0 @@ 5958-/** 5959- * Copyright 2022 Huawei Technologies Co., Ltd 5960- * 5961- * Licensed under the Apache License, Version 2.0 (the "License"); 5962- * you may not use this file except in compliance with the License. 5963- * You may obtain a copy of the License at 5964- * 5965- * http://www.apache.org/licenses/LICENSE-2.0 5966- * 5967- * Unless required by applicable law or agreed to in writing, software 5968- * distributed under the License is distributed on an "AS IS" BASIS, 5969- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 5970- * See the License for the specific language governing permissions and 5971- * limitations under the License. 5972- */ 5973- 5974-#include "src/expression/param.h" 5975-#include <random> 5976-#include <algorithm> 5977-#include <string> 5978-#include "include/errorcode.h" 5979- 5980-using mindspore::lite::RET_ERROR; 5981-using mindspore::lite::RET_OK; 5982- 5983-constexpr float kZero = 0.0f; 5984-constexpr float kOne = 1.0f; 5985- 5986-namespace mindspore { 5987-namespace lite { 5988-int Param::Fill(Mode mode) { 5989- std::default_random_engine engine{static_cast<unsigned int>(0)}; 5990- std::vector<float> data(size_); 5991- switch (mode) { 5992- case NORMAL: { 5993- constexpr float scale = 0.01; 5994- std::normal_distribution<float> n{0, 1}; 5995- std::generate_n(data.begin(), size_, [&]() { return n(engine); }); 5996- (void)std::transform(data.begin(), data.end(), data.begin(), [=](float x) { return x * scale; }); 5997- break; 5998- } 5999- case UNIFORM: { 6000- constexpr float scale = 0.07; 6001- std::uniform_real_distribution<float> u{-1.0, 1.0}; 6002- std::generate_n(data.begin(), size_, [&]() { return u(engine) * scale; }); 6003- break; 6004- } 6005- case ZEROS: 6006- std::fill_n(data.begin(), size_, kZero); 6007- break; 6008- case ONES: 6009- std::fill_n(data.begin(), size_, kOne); 6010- break; 6011- case NOT_SUPPORTED: 6012- return RET_ERROR; 6013- } 6014- Copy(data); 6015- return RET_OK; 6016-} 6017- 6018-Param::Mode Param::String2Enum(std::string mode) { 6019- (void)std::transform(mode.begin(), mode.end(), mode.begin(), ::tolower); 6020- if (mode == "normal") return NORMAL; 6021- if (mode == "uniform") return UNIFORM; 6022- if (mode == "ones") return ONES; 6023- if (mode == "zeors") return ZEROS; 6024- return NOT_SUPPORTED; 6025-} 6026-} // namespace lite 6027-} // namespace mindspore 6028diff --git a/mindspore/lite/src/expression/param.h b/mindspore/lite/src/expression/param.h 6029deleted file mode 100644 6030index 201e69fc..00000000 6031--- a/mindspore/lite/src/expression/param.h 6032+++ /dev/null 6033@@ -1,60 +0,0 @@ 6034-/** 6035- * Copyright 2022 Huawei Technologies Co., Ltd 6036- * 6037- * Licensed under the Apache License, Version 2.0 (the "License"); 6038- * you may not use this file except in compliance with the License. 6039- * You may obtain a copy of the License at 6040- * 6041- * http://www.apache.org/licenses/LICENSE-2.0 6042- * 6043- * Unless required by applicable law or agreed to in writing, software 6044- * distributed under the License is distributed on an "AS IS" BASIS, 6045- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 6046- * See the License for the specific language governing permissions and 6047- * limitations under the License. 6048- */ 6049- 6050-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_PARAM_H_ 6051-#define MINDSPORE_LITE_SRC_EXPRESSION_PARAM_H_ 6052- 6053-#include <vector> 6054-#include <iostream> 6055-#include <fstream> 6056-#include <string> 6057- 6058-namespace mindspore { 6059-namespace lite { 6060-class Param { 6061- public: 6062- enum Mode { NORMAL, UNIFORM, ONES, ZEROS, NOT_SUPPORTED }; 6063- int Fill(Mode type); 6064- static Mode String2Enum(std::string); 6065- std::vector<uint8_t> &data() { return data_; } 6066- size_t Load(std::string file_name, size_t offset = 0) { return data_.size() * sizeof(float); } 6067- size_t Load(std::ifstream &s, int offset = 0) { return data_.size() * sizeof(float); } 6068- void SetSize(size_t size) { size_ = size; } 6069- template <typename T> 6070- void Copy(const T *data, size_t size) { 6071- auto cast_data = reinterpret_cast<const uint8_t *>(data); 6072- data_ = decltype(data_)(cast_data, cast_data + size * sizeof(T) / sizeof(uint8_t)); 6073- } 6074- template <typename T> 6075- void Copy(const std::vector<T> data) { 6076- Copy<T>(data.data(), data.size()); 6077- } 6078- 6079- template <typename T> 6080- std::vector<T> Extract() { 6081- T *num = reinterpret_cast<T *>(data_.data()); 6082- std::vector<T> res(num, num + data_.size() / sizeof(T)); 6083- return res; 6084- } 6085- 6086- private: 6087- size_t size_; 6088- std::vector<uint8_t> data_; 6089-}; 6090-} // namespace lite 6091-} // namespace mindspore 6092- 6093-#endif // MINDSPORE_LITE_SRC_EXPRESSION_PARAM_H_ 6094diff --git a/mindspore/lite/src/expression/sequential.cc b/mindspore/lite/src/expression/sequential.cc 6095deleted file mode 100644 6096index 5f3a8a76..00000000 6097--- a/mindspore/lite/src/expression/sequential.cc 6098+++ /dev/null 6099@@ -1,30 +0,0 @@ 6100-/** 6101- * Copyright 2022 Huawei Technologies Co., Ltd 6102- * 6103- * Licensed under the Apache License, Version 2.0 (the "License"); 6104- * you may not use this file except in compliance with the License. 6105- * You may obtain a copy of the License at 6106- * 6107- * http://www.apache.org/licenses/LICENSE-2.0 6108- * 6109- * Unless required by applicable law or agreed to in writing, software 6110- * distributed under the License is distributed on an "AS IS" BASIS, 6111- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 6112- * See the License for the specific language governing permissions and 6113- * limitations under the License. 6114- */ 6115-#include "src/expression/sequential.h" 6116- 6117-namespace mindspore { 6118-namespace lite { 6119-void Sequential::Add(Node *node) { PushOp(node); } 6120- 6121-std::vector<EXPR *> Sequential::construct(const std::vector<EXPR *> &inputs) { 6122- auto x = inputs; 6123- for (auto &node : ops_) { 6124- x = (*node)({x.front()}); 6125- } 6126- return x; 6127-} 6128-} // namespace lite 6129-} // namespace mindspore 6130diff --git a/mindspore/lite/src/expression/sequential.h b/mindspore/lite/src/expression/sequential.h 6131deleted file mode 100644 6132index 9b1a69e5..00000000 6133--- a/mindspore/lite/src/expression/sequential.h 6134+++ /dev/null 6135@@ -1,32 +0,0 @@ 6136-/** 6137- * Copyright 2022 Huawei Technologies Co., Ltd 6138- * 6139- * Licensed under the Apache License, Version 2.0 (the "License"); 6140- * you may not use this file except in compliance with the License. 6141- * You may obtain a copy of the License at 6142- * 6143- * http://www.apache.org/licenses/LICENSE-2.0 6144- * 6145- * Unless required by applicable law or agreed to in writing, software 6146- * distributed under the License is distributed on an "AS IS" BASIS, 6147- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 6148- * See the License for the specific language governing permissions and 6149- * limitations under the License. 6150- */ 6151- 6152-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_SEQUENTIAL_H_ 6153-#define MINDSPORE_LITE_SRC_EXPRESSION_SEQUENTIAL_H_ 6154-#include <vector> 6155-#include "src/expression/net.h" 6156- 6157-namespace mindspore { 6158-namespace lite { 6159-class Sequential : public Net { 6160- public: 6161- std::vector<EXPR *> construct(const std::vector<EXPR *> &inputs) override; 6162- void Add(Node *node) override; 6163-}; 6164-} // namespace lite 6165-} // namespace mindspore 6166- 6167-#endif // MINDSPORE_LITE_SRC_EXPRESSION_SEQUENTIAL_H_ 6168diff --git a/mindspore/lite/src/litert/cxx_api/expression/net.cc b/mindspore/lite/src/litert/cxx_api/expression/net.cc 6169deleted file mode 100644 6170index fd590e94..00000000 6171--- a/mindspore/lite/src/litert/cxx_api/expression/net.cc 6172+++ /dev/null 6173@@ -1,145 +0,0 @@ 6174-/** 6175- * Copyright 2022 Huawei Technologies Co., Ltd 6176- * 6177- * Licensed under the Apache License, Version 2.0 (the "License"); 6178- * you may not use this file except in compliance with the License. 6179- * You may obtain a copy of the License at 6180- * 6181- * http://www.apache.org/licenses/LICENSE-2.0 6182- * 6183- * Unless required by applicable law or agreed to in writing, software 6184- * distributed under the License is distributed on an "AS IS" BASIS, 6185- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 6186- * See the License for the specific language governing permissions and 6187- * limitations under the License. 6188- */ 6189- 6190-#include "include/api/net.h" 6191-#include "include/api/status.h" 6192-#include "src/litert/cxx_api/expression/node_impl.h" 6193-#include "src/litert/cxx_api/expression/net_impl.h" 6194-#include "src/expression/ops.h" 6195-#include "src/expression/cfg.h" 6196- 6197-namespace mindspore { 6198-uint32_t Node::type() { return kNodeType; } 6199- 6200-std::vector<Expr *> Node::operator()(const std::vector<Expr *> &inputs) { 6201- auto in = Expr::convert(inputs); 6202- if (impl_ == nullptr) { 6203- MS_LOG(ERROR) << "empty implementation"; 6204- return {}; 6205- } 6206- if (impl_->node() == nullptr) { 6207- MS_LOG(ERROR) << "expression node is not attached"; 6208- return {}; 6209- } 6210- auto out = impl_->node()->construct(in); 6211- return Expr::convert(out); 6212-} 6213- 6214-Expr *Node::Create(std::string name) { 6215- auto expr = impl_->node()->create(name); 6216- return reinterpret_cast<Expr *>(expr); 6217-} 6218- 6219-Node::Node() { 6220- auto impl = std::make_shared<NodeImpl>(); 6221- impl_ = impl; 6222- impl_->set_pnode(this); 6223-} 6224- 6225-Node::~Node() { 6226- impl_->set_pnode(nullptr); 6227- auto node = impl_->node(); 6228- if (node != nullptr) { 6229- impl_->set_node(nullptr); 6230- delete node; 6231- } 6232-} 6233- 6234-Net::Net(std::string name) { 6235- auto impl = std::make_shared<NetImpl>(); 6236- if (impl == nullptr) { 6237- MS_LOG(ERROR) << "Cannot allocate network implementation"; 6238- return; 6239- } 6240- impl_ = impl; 6241- impl_->set_pnet(std::shared_ptr<Net>(this)); 6242- auto netl = new (std::nothrow) lite::Net(name); 6243- if (netl == nullptr) { 6244- MS_LOG(ERROR) << "Cannot allocate network lite"; 6245- return; 6246- } 6247- netl->set_impl(impl); 6248- impl_->set_net(netl); 6249-} 6250- 6251-Net::Net() : Net("") {} 6252- 6253-Net::Net(const Graph &g) { 6254- auto net = NetImpl::GetNet(g); 6255- impl_ = net->impl_; 6256-} 6257- 6258-void Net::Add(NetBase *element) { MS_LOG(WARNING) << "Only sequential can add element"; } 6259- 6260-uint32_t Net::type() { return kNetType; } 6261- 6262-std::vector<Expr *> Net::construct(const std::vector<Expr *> &inputs) { 6263- auto in = Expr::convert(inputs); 6264- auto out = impl_->net()->construct(in); 6265- return Expr::convert(out); 6266-} 6267- 6268-std::vector<Expr *> Net::operator()(const std::vector<Expr *> &inputs) { 6269- auto in = Expr::convert(inputs); 6270- auto x = construct(inputs); 6271- impl_->net()->input_ = in; 6272- auto out = Expr::convert(x); 6273- impl_->net()->output_ = out; 6274- impl_->net()->real_output_ = out; 6275- return x; 6276-} 6277-void Net::Register(Net *net, std::string &&name) { 6278- if (net != nullptr) { 6279- auto net_lite = net->impl_->net(); 6280- impl_->net()->Register(net_lite, std::move(name)); 6281- } 6282-} 6283- 6284-void Net::Register(Node *node, std::string &&name) { 6285- if (node != nullptr) { 6286- auto impl = NodeImpl::GetImpl(node); 6287- if (impl == nullptr) { 6288- MS_LOG(ERROR) << "missing implementation"; 6289- return; 6290- } 6291- auto node_lite = impl->node(); 6292- impl_->net()->Register(node_lite, std::move(name)); 6293- } 6294-} 6295- 6296-std::shared_ptr<NodeSet> Net::trainable_params() { 6297- auto node_set = std::make_shared<NodeSet>(); 6298- if (node_set == nullptr) { 6299- MS_LOG(ERROR) << "new NodeSet failed."; 6300- return nullptr; 6301- } 6302- node_set->set_ = impl_->net()->trainable_params(); 6303- return node_set; 6304-} 6305- 6306-const std::vector<int> Net::InputShape(int idx) { return impl_->InputShape(idx); } 6307-const std::vector<int> Net::OutputShape(int idx) { return impl_->OutputShape(idx); } 6308- 6309-Net::~Net() { 6310- if (impl_ != nullptr) { 6311- if ((impl_->pnet() == nullptr) || (impl_->pnet() == this)) { 6312- impl_->set_pnet(nullptr); 6313- impl_->set_net(nullptr); 6314- impl_.reset(); 6315- } 6316- } 6317-} 6318-} // namespace mindspore 6319diff --git a/mindspore/lite/src/litert/cxx_api/expression/net_impl.cc b/mindspore/lite/src/litert/cxx_api/expression/net_impl.cc 6320deleted file mode 100644 6321index 5487d8c7..00000000 6322--- a/mindspore/lite/src/litert/cxx_api/expression/net_impl.cc 6323+++ /dev/null 6324@@ -1,220 +0,0 @@ 6325-/** 6326- * Copyright 2022 Huawei Technologies Co., Ltd 6327- * 6328- * Licensed under the Apache License, Version 2.0 (the "License"); 6329- * you may not use this file except in compliance with the License. 6330- * You may obtain a copy of the License at 6331- * 6332- * http://www.apache.org/licenses/LICENSE-2.0 6333- * 6334- * Unless required by applicable law or agreed to in writing, software 6335- * distributed under the License is distributed on an "AS IS" BASIS, 6336- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 6337- * See the License for the specific language governing permissions and 6338- * limitations under the License. 6339- */ 6340- 6341-#include "src/litert/cxx_api/expression/net_impl.h" 6342-#include <vector> 6343-#include <utility> 6344-#include "include/api/serialization.h" 6345-#include "src/expression/import.h" 6346-#include "src/expression/ops.h" 6347-#include "src/litert/cxx_api/model/model_impl.h" 6348- 6349-namespace { 6350-constexpr size_t kFlatbuffersBuilderInitSize = 1024; 6351-}; 6352- 6353-namespace mindspore { 6354-Sequential::Sequential() {} 6355- 6356-lite::Node *Sequential::GetNode(NetBase *element) { 6357- lite::Node *lite_node = nullptr; 6358- switch (element->type()) { 6359- case kNodeType: { 6360- Node *node = reinterpret_cast<Node *>(element); 6361- auto impl = NodeImpl::GetImpl(node); 6362- if (impl == nullptr) { 6363- MS_LOG(ERROR) << "cannot find node implement"; 6364- return nullptr; 6365- } 6366- lite_node = impl->node(); 6367- break; 6368- } 6369- case kNetType: { 6370- auto net = reinterpret_cast<Net *>(element); 6371- auto impl = NetImpl::GetImpl(net); 6372- if (impl == nullptr) { 6373- MS_LOG(ERROR) << "cannot find node implement"; 6374- return nullptr; 6375- } 6376- lite_node = impl->net(); 6377- break; 6378- } 6379- } 6380- return lite_node; 6381-} 6382- 6383-void Sequential::Add(NetBase *element) { 6384- lite::Node *node = GetNode(element); 6385- auto impl = NetImpl::GetImpl(this); 6386- if (impl == nullptr) { 6387- MS_LOG(ERROR) << "No implementation"; 6388- return; 6389- } 6390- impl->net()->Add(node); 6391-} 6392- 6393-NetWithLoss::NetWithLoss(Net *net, Node *loss) : net_(net), loss_fn_(loss) { 6394- REG(net_); 6395- Register(loss_fn_, "_loss_fn"); 6396-} 6397- 6398-std::vector<Expr *> NetWithLoss::construct(const std::vector<Expr *> &inputs) { 6399- if (inputs.size() != C2NUM) { 6400- MS_LOG(ERROR) << "need 2 inputs for loss"; 6401- return {}; 6402- } 6403- auto input = inputs[FIRST_INPUT]; 6404- auto label = inputs[SECOND_INPUT]; 6405- auto x = (*net_)({input}); 6406- x = (*loss_fn_)({x[FIRST_INPUT], label}); 6407- return x; 6408-} 6409- 6410-NetImpl::NetImpl(std::shared_ptr<Net> p) { pnet_ = p; } 6411- 6412-NetImpl::NetImpl(Graph *g) { pnet_ = g->net_data_->net(); } 6413- 6414-std::vector<lite::EXPR *> NetImpl::construct(const std::vector<lite::EXPR *> &inputs) { 6415- auto in = Expr::convert(inputs); 6416- auto out = pnet_->construct(in); 6417- return Expr::convert(out); 6418-} 6419- 6420-Net *NetImpl::Connect(std::shared_ptr<Net> net, lite::Net *lnet) { 6421- auto impl = GetImpl(net.get()); 6422- if (impl == nullptr) { 6423- MS_LOG(ERROR) << "missing implementation"; 6424- return nullptr; 6425- } 6426- impl->set_pnet(net); 6427- lnet->set_impl(impl); 6428- impl->set_net(lnet); 6429- return net.get(); 6430-} 6431- 6432-Status NetImpl::Import(const char *model_buf, Graph *graph) { 6433- auto mg = schema::GetMetaGraph(model_buf); 6434- auto net = new (std::nothrow) Net(); 6435- if (net == nullptr) { 6436- MS_LOG(ERROR) << "Cannot allocate network"; 6437- return kLiteMemoryFailed; 6438- } 6439- lite::Import import; 6440- auto lite_net = import.ImportMs(mg); 6441- if (lite_net == nullptr) { 6442- MS_LOG(ERROR) << "failed to import net"; 6443- return kLiteMemoryFailed; 6444- } 6445- lite_net->SetRealOutput(); 6446- Connect(net->shared_from_this(), lite_net); 6447- *graph = Graph(net); 6448- return kSuccess; 6449-} 6450- 6451-Status NetImpl::TrainNet(Node *optimizer, const std::vector<Expr *> &inputs) { 6452- auto impl = NodeImpl::GetImpl(optimizer); 6453- if (impl == nullptr) { 6454- MS_LOG(ERROR) << "missing implementation "; 6455- return kLiteNullptr; 6456- } 6457- auto opt = impl->node(); 6458- auto in = Expr::convert(inputs); 6459- auto ret_net = net()->TrainNet(opt, in); 6460- if (ret_net == nullptr) { 6461- MS_LOG(ERROR) << "failed to train network"; 6462- return kLiteNullptr; 6463- } 6464- return kSuccess; 6465-} 6466- 6467-std::unique_ptr<Graph> NetImpl::MakeMs() { 6468- auto mgraph = std::make_unique<Graph>(Graph::Type::kExecutableGraph); 6469- if (mgraph == nullptr) { 6470- MS_LOG(ERROR) << "Cannot allocate graph"; 6471- return nullptr; 6472- } 6473- auto trained_graph = net()->MakeMs(); 6474- if (trained_graph == nullptr) { 6475- MS_LOG(ERROR) << "cannot create flat buffer"; 6476- return nullptr; 6477- } 6478- flatbuffers::FlatBufferBuilder builder(kFlatbuffersBuilderInitSize); 6479- auto offset = schema::MetaGraph::Pack(builder, trained_graph.get()); 6480- builder.Finish(offset); 6481- schema::FinishMetaGraphBuffer(builder, offset); 6482- auto buffer = builder.GetBufferPointer(); 6483- size_t size = builder.GetSize(); 6484- auto status = Serialization::Load(buffer, size, mindspore::kMindIR, mgraph.get()); 6485- if (status != kSuccess) { 6486- MS_LOG(ERROR) << "failed to load flatbuffer to graph"; 6487- return nullptr; 6488- } 6489- return mgraph; 6490-} 6491- 6492-const std::vector<int> NetImpl::InputShape(int idx) { return net_->InputShape(idx); } 6493- 6494-const std::vector<int> NetImpl::OutputShape(int idx) { return net_->OutputShape(idx); } 6495- 6496-void NetImpl::ReplaceNet(Graph *g, std::shared_ptr<Net> n) { g->net_data_->net().swap(n); } 6497- 6498-ExpressionLoader expression_registrator = CreateExpressionLoader(NetImpl::Import); 6499- 6500-namespace NN { 6501-Net *Sequential() { 6502- auto net = new (std::nothrow) mindspore::Sequential(); 6503- if (net == nullptr) { 6504- MS_LOG(ERROR) << "Cannot allocate "; 6505- return nullptr; 6506- } 6507- auto netl = lite::NN::Sequential(); 6508- return NetImpl::Connect(net->shared_from_this(), netl); 6509-} 6510- 6511-Net *NetWithLoss(Net *net, Node *loss) { 6512- auto loss_net = new (std::nothrow) mindspore::NetWithLoss(net, loss); 6513- if (net == nullptr) { 6514- MS_LOG(ERROR) << "Cannot allocate loss net"; 6515- return nullptr; 6516- } 6517- return loss_net; 6518-} 6519- 6520-Graph *GraphWithLoss(Graph *graph, Node *loss) { 6521- auto net = NetImpl::GetNet(*graph); 6522- if (net == nullptr) { 6523- MS_LOG(ERROR) << "Cannot allocate network"; 6524- return nullptr; 6525- } 6526- auto loss_net = NetWithLoss(net.get(), loss); 6527- if (loss_net == nullptr) { 6528- MS_LOG(ERROR) << "Cannot allocate network"; 6529- return nullptr; 6530- } 6531- NetImpl::ReplaceNet(graph, loss_net->shared_from_this()); 6532- return graph; 6533-} 6534- 6535-Net *NetWithLoss(Graph *g, Node *loss) { 6536- auto net = new (std::nothrow) Net(*g); 6537- if (net == nullptr) { 6538- MS_LOG(ERROR) << "Cannot allocate net"; 6539- return nullptr; 6540- } 6541- return NetWithLoss(net, loss); 6542-} 6543-} // namespace NN 6544-} // namespace mindspore 6545diff --git a/mindspore/lite/src/litert/cxx_api/expression/net_impl.h b/mindspore/lite/src/litert/cxx_api/expression/net_impl.h 6546deleted file mode 100644 6547index 682ba0b2..00000000 6548--- a/mindspore/lite/src/litert/cxx_api/expression/net_impl.h 6549+++ /dev/null 6550@@ -1,95 +0,0 @@ 6551-/** 6552- * Copyright 2022 Huawei Technologies Co., Ltd 6553- * 6554- * Licensed under the Apache License, Version 2.0 (the "License"); 6555- * you may not use this file except in compliance with the License. 6556- * You may obtain a copy of the License at 6557- * 6558- * http://www.apache.org/licenses/LICENSE-2.0 6559- * 6560- * Unless required by applicable law or agreed to in writing, software 6561- * distributed under the License is distributed on an "AS IS" BASIS, 6562- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 6563- * See the License for the specific language governing permissions and 6564- * limitations under the License. 6565- */ 6566- 6567-#ifndef MINDSPORE_LITE_SRC_RUNTIME_CXX_API_EXPRESSION_NET_IMPL_H_ 6568-#define MINDSPORE_LITE_SRC_RUNTIME_CXX_API_EXPRESSION_NET_IMPL_H_ 6569- 6570-#include <algorithm> 6571-#include <set> 6572-#include <memory> 6573-#include <vector> 6574-#include <utility> 6575-#include "include/api/cfg.h" 6576-#include "include/api/data_type.h" 6577-#include "include/api/graph.h" 6578-#include "include/api/status.h" 6579-#include "include/api/net.h" 6580-#include "src/litert/cxx_api/expression/node_impl.h" 6581-#include "src/litert/cxx_api/graph/net_data.h" 6582-#include "src/expression/net.h" 6583-#include "src/expression/ops.h" 6584- 6585-namespace mindspore { 6586-constexpr uint32_t kNodeType = 1; 6587-constexpr uint32_t kNetType = 2; 6588-class Sequential : public Net { 6589- public: 6590- Sequential(); 6591- void Add(NetBase *n) override; 6592- 6593- private: 6594- std::vector<NetBase *> ops_; 6595- lite::Node *GetNode(NetBase *element); 6596-}; 6597- 6598-class NetWithLoss : public Net { 6599- public: 6600- NetWithLoss(Net *net, Node *loss); 6601- std::vector<Expr *> construct(const std::vector<Expr *> &inputs) override; 6602- 6603- private: 6604- Net *net_{nullptr}; 6605- Node *loss_fn_{nullptr}; 6606-}; 6607- 6608-class MS_API NetImpl { 6609- public: 6610- virtual ~NetImpl() {} 6611- explicit NetImpl(std::shared_ptr<Net> p); 6612- explicit NetImpl(Graph *g); 6613- NetImpl() = default; 6614- void set_net(lite::Net *net) { 6615- if (net_ != nullptr) { 6616- net_->set_impl(nullptr); 6617- delete net_; 6618- } 6619- net_ = net; 6620- } 6621- void erase_net() { net_ = nullptr; } 6622- void set_pnet(std::shared_ptr<Net> net) { pnet_ = net; } 6623- Net *pnet() { return pnet_.get(); } 6624- lite::Net *net() { return net_; } 6625- 6626- std::vector<lite::EXPR *> construct(const std::vector<lite::EXPR *> &inputs); 6627- static std::shared_ptr<mindspore::NetImpl> &GetImpl(Net *net) { return net->impl_; } 6628- static Net *Connect(std::shared_ptr<Net> net, lite::Net *lnet); 6629- static std::shared_ptr<Net> &GetNet(const Graph &g) { return g.net_data_->net(); } 6630- static void SetNet(Graph *g, std::shared_ptr<Net> n) { g->net_data_->set_net(n); } 6631- static void ReplaceNet(Graph *g, std::shared_ptr<Net> n); 6632- static Status Import(const char *model_buf, Graph *graph); 6633- Status TrainNet(Node *optimizer, const std::vector<Expr *> &inputs); 6634- const std::vector<int> InputShape(int idx); 6635- const std::vector<int> OutputShape(int idx); 6636- std::unique_ptr<Graph> MakeMs(); 6637- void Release() { pnet_.reset(); } 6638- 6639- private: 6640- std::shared_ptr<Net> pnet_; 6641- lite::Net *net_ = nullptr; 6642-}; 6643-} // namespace mindspore 6644- 6645-#endif // MINDSPORE_LITE_SRC_RUNTIME_CXX_API_EXPRESSION_NET_IMPL_H_ 6646diff --git a/mindspore/lite/src/litert/cxx_api/expression/node_impl.cc b/mindspore/lite/src/litert/cxx_api/expression/node_impl.cc 6647deleted file mode 100644 6648index df10a7d0..00000000 6649--- a/mindspore/lite/src/litert/cxx_api/expression/node_impl.cc 6650+++ /dev/null 6651@@ -1,50 +0,0 @@ 6652-/** 6653- * Copyright 2022 Huawei Technologies Co., Ltd 6654- * 6655- * Licensed under the Apache License, Version 2.0 (the "License"); 6656- * you may not use this file except in compliance with the License. 6657- * You may obtain a copy of the License at 6658- * 6659- * http://www.apache.org/licenses/LICENSE-2.0 6660- * 6661- * Unless required by applicable law or agreed to in writing, software 6662- * distributed under the License is distributed on an "AS IS" BASIS, 6663- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 6664- * See the License for the specific language governing permissions and 6665- * limitations under the License. 6666- */ 6667- 6668-#include "src/litert/cxx_api/expression/node_impl.h" 6669-#include <vector> 6670-#include "include/api/net.h" 6671-#include "src/expression/ops.h" 6672- 6673-namespace mindspore { 6674-Node *NodeImpl::Connect(lite::Node *lnode) { 6675- auto node = std::make_unique<Node>(); 6676- if (node == nullptr) { 6677- MS_LOG(ERROR) << "Cannot allocate node"; 6678- return nullptr; 6679- } 6680- if (lnode == nullptr) { 6681- MS_LOG(ERROR) << "lite node is null"; 6682- return nullptr; 6683- } 6684- auto pnode = node.release(); 6685- auto impl = GetImpl(pnode); 6686- if (impl == nullptr) { 6687- MS_LOG(ERROR) << "missing implementation"; 6688- return nullptr; 6689- } 6690- impl->set_node(lnode); 6691- lnode->set_impl(impl); 6692- return pnode; 6693-} 6694-namespace NN { 6695-std::unique_ptr<Node> Input(std::vector<int> dims, DataType data_type, int fmt) { 6696- auto type = static_cast<TypeId>(data_type); 6697- auto lite_node = lite::NN::Input(dims, type, fmt); 6698- return std::unique_ptr<Node>(NodeImpl::Connect(lite_node)); 6699-} 6700-} // namespace NN 6701-} // namespace mindspore 6702diff --git a/mindspore/lite/src/litert/cxx_api/expression/node_impl.h b/mindspore/lite/src/litert/cxx_api/expression/node_impl.h 6703deleted file mode 100644 6704index a1ff4530..00000000 6705--- a/mindspore/lite/src/litert/cxx_api/expression/node_impl.h 6706+++ /dev/null 6707@@ -1,71 +0,0 @@ 6708-/** 6709- * Copyright 2022 Huawei Technologies Co., Ltd 6710- * 6711- * Licensed under the Apache License, Version 2.0 (the "License"); 6712- * you may not use this file except in compliance with the License. 6713- * You may obtain a copy of the License at 6714- * 6715- * http://www.apache.org/licenses/LICENSE-2.0 6716- * 6717- * Unless required by applicable law or agreed to in writing, software 6718- * distributed under the License is distributed on an "AS IS" BASIS, 6719- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 6720- * See the License for the specific language governing permissions and 6721- * limitations under the License. 6722- */ 6723- 6724-#ifndef MINDSPORE_LITE_SRC_RUNTIME_CXX_API_EXPRESSION_NODE_IMPL_H_ 6725-#define MINDSPORE_LITE_SRC_RUNTIME_CXX_API_EXPRESSION_NODE_IMPL_H_ 6726- 6727-#include <algorithm> 6728-#include <set> 6729-#include <memory> 6730-#include <vector> 6731-#include "include/api/net.h" 6732-#include "include/api/cfg.h" 6733-#include "include/api/data_type.h" 6734-#include "src/expression/node.h" 6735-#include "src/expression/expr.h" 6736- 6737-namespace mindspore { 6738-using lite::EXPR; 6739-class NodeSet { 6740- public: 6741- std::set<lite::Node *> set_; 6742-}; 6743- 6744-class Expr : public EXPR { 6745- public: 6746- static std::vector<EXPR *> convert(const std::vector<Expr *> &input) { 6747- std::vector<EXPR *> vec(input.size()); 6748- (void)std::transform(input.begin(), input.end(), vec.begin(), [](Expr *e) { return reinterpret_cast<EXPR *>(e); }); 6749- return vec; 6750- } 6751- static std::vector<Expr *> convert(const std::vector<EXPR *> &input) { 6752- std::vector<Expr *> vec(input.size()); 6753- (void)std::transform(input.begin(), input.end(), vec.begin(), [](EXPR *e) { return reinterpret_cast<Expr *>(e); }); 6754- return vec; 6755- } 6756-}; 6757- 6758-class MS_API NodeImpl { 6759- public: 6760- std::vector<Expr *> operator()(const std::vector<Expr *> &inputs) { 6761- auto in = Expr::convert(inputs); 6762- auto out = (*node_)(in); 6763- return Expr::convert(out); 6764- } 6765- lite::Node *node() { return node_; } 6766- void set_node(lite::Node *node) { node_ = node; } 6767- void set_pnode(Node *node) { pnode_ = node; } 6768- Node *pnode() { return pnode_; } 6769- static Node *Connect(lite::Node *lnode); 6770- static std::shared_ptr<NodeImpl> &GetImpl(Node *node) { return node->impl_; } 6771- 6772- private: 6773- Node *pnode_{nullptr}; 6774- lite::Node *node_{nullptr}; 6775-}; 6776-} // namespace mindspore 6777- 6778-#endif // MINDSPORE_LITE_SRC_RUNTIME_CXX_API_EXPRESSION_NODE_IMPL_H_ 6779diff --git a/mindspore/lite/src/litert/cxx_api/graph/graph.cc b/mindspore/lite/src/litert/cxx_api/graph/graph.cc 6780index 35912580..5c1567cc 100644 6781--- a/mindspore/lite/src/litert/cxx_api/graph/graph.cc 6782+++ b/mindspore/lite/src/litert/cxx_api/graph/graph.cc 6783@@ -16,9 +16,7 @@ 6784 6785 #include "include/api/graph.h" 6786 #include "include/api/cell.h" 6787-#include "include/api/net.h" 6788 #include "src/litert/cxx_api/graph/graph_data.h" 6789-#include "src/litert/cxx_api/graph/net_data.h" 6790 6791 namespace mindspore { 6792 Graph::Graph() : graph_data_(nullptr) {} 6793@@ -27,15 +25,8 @@ Graph::Graph(const std::shared_ptr<GraphData> &graph_data) : graph_data_(graph_d 6794 6795 Graph::Graph(std::shared_ptr<GraphData> &&graph_data) : graph_data_(graph_data) {} 6796 6797-Graph::Graph(Graph::Type type) : graph_type_(type) {} 6798- 6799 Graph::~Graph() {} 6800 6801-Graph::Graph(Net *net) : graph_type_(kExpressionGraph) { 6802- auto shared = std::make_shared<NetData>(net->shared_from_this()); 6803- net_data_ = shared; 6804-} 6805- 6806 Graph::Graph(std::nullptr_t) : graph_data_(nullptr) {} 6807 6808 bool Graph::operator==(std::nullptr_t) const { return graph_data_ == nullptr; } 6809diff --git a/mindspore/lite/src/litert/cxx_api/graph/net_data.cc b/mindspore/lite/src/litert/cxx_api/graph/net_data.cc 6810deleted file mode 100644 6811index 2de4d875..00000000 6812--- a/mindspore/lite/src/litert/cxx_api/graph/net_data.cc 6813+++ /dev/null 6814@@ -1,21 +0,0 @@ 6815-/** 6816- * Copyright 2022 Huawei Technologies Co., Ltd 6817- * 6818- * Licensed under the Apache License, Version 2.0 (the "License"); 6819- * you may not use this file except in compliance with the License. 6820- * You may obtain a copy of the License at 6821- * 6822- * http://www.apache.org/licenses/LICENSE-2.0 6823- * 6824- * Unless required by applicable law or agreed to in writing, software 6825- * distributed under the License is distributed on an "AS IS" BASIS, 6826- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 6827- * See the License for the specific language governing permissions and 6828- * limitations under the License. 6829- */ 6830-#include "src/litert/cxx_api/graph/net_data.h" 6831-#include "src/litert/cxx_api/expression/net_impl.h" 6832- 6833-namespace mindspore { 6834-NetData::~NetData() { net_->impl_->Release(); } 6835-} // namespace mindspore 6836diff --git a/mindspore/lite/src/litert/cxx_api/graph/net_data.h b/mindspore/lite/src/litert/cxx_api/graph/net_data.h 6837deleted file mode 100644 6838index 15393d49..00000000 6839--- a/mindspore/lite/src/litert/cxx_api/graph/net_data.h 6840+++ /dev/null 6841@@ -1,35 +0,0 @@ 6842-/** 6843- * Copyright 2022 Huawei Technologies Co., Ltd 6844- * 6845- * Licensed under the Apache License, Version 2.0 (the "License"); 6846- * you may not use this file except in compliance with the License. 6847- * You may obtain a copy of the License at 6848- * 6849- * http://www.apache.org/licenses/LICENSE-2.0 6850- * 6851- * Unless required by applicable law or agreed to in writing, software 6852- * distributed under the License is distributed on an "AS IS" BASIS, 6853- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 6854- * See the License for the specific language governing permissions and 6855- * limitations under the License. 6856- */ 6857- 6858-#ifndef MINDSPORE_LITE_SRC_RUNTIME_CXX_API_GRAPH_NET_DATA_H_ 6859-#define MINDSPORE_LITE_SRC_RUNTIME_CXX_API_GRAPH_NET_DATA_H_ 6860- 6861-#include <memory> 6862-#include "include/api/net.h" 6863- 6864-namespace mindspore { 6865-class NetData { 6866- public: 6867- explicit NetData(const std::shared_ptr<Net> &net) : net_(net) {} 6868- virtual ~NetData(); 6869- void set_net(std::shared_ptr<Net> net) { net_ = net; } 6870- std::shared_ptr<Net> &net() { return net_; } 6871- 6872- private: 6873- std::shared_ptr<Net> net_; 6874-}; 6875-} // namespace mindspore 6876-#endif // MINDSPORE_LITE_SRC_RUNTIME_CXX_API_GRAPH_NET_DATA_H_ 6877diff --git a/mindspore/lite/src/litert/cxx_api/model/model.cc b/mindspore/lite/src/litert/cxx_api/model/model.cc 6878index 081b4dfd..7bbaca5c 100644 6879--- a/mindspore/lite/src/litert/cxx_api/model/model.cc 6880+++ b/mindspore/lite/src/litert/cxx_api/model/model.cc 6881@@ -30,7 +30,6 @@ 6882 #if defined(ENABLE_PRE_INFERENCE) && defined(__linux__) && !defined(Debug) 6883 #include "src/common/thread_utils.h" 6884 #endif 6885-#include "src/litert/cxx_api/expression/net_impl.h" 6886 #include "src/litert/cxx_api/callback/callback_adapter.h" 6887 #include "src/litert/cxx_api/callback/callback_impl.h" 6888 #include "src/litert/cxx_api/model/model_impl.h" 6889diff --git a/mindspore/lite/src/litert/cxx_api/model/model_impl.cc b/mindspore/lite/src/litert/cxx_api/model/model_impl.cc 6890index 77ee95ab..78b1ca67 100644 6891--- a/mindspore/lite/src/litert/cxx_api/model/model_impl.cc 6892+++ b/mindspore/lite/src/litert/cxx_api/model/model_impl.cc 6893@@ -74,14 +74,6 @@ CreateTrainSessionProto *CreateTrainSessionCallbackHolder(CreateTrainSessionProt 6894 return proto_; 6895 } 6896 6897-ExpressionLoader CreateExpressionLoader(const ExpressionLoader &loader) { 6898- static ExpressionLoader loader_ = nullptr; 6899- if (loader != nullptr) { 6900- loader_ = loader; 6901- } 6902- return loader_; 6903-} 6904- 6905 #if defined(ENABLE_PRE_INFERENCE) && defined(__linux__) && !defined(Debug) 6906 Status ModelImpl::BuildAndRun(const void *model_data, size_t data_size, ModelType model_type, 6907 const std::shared_ptr<Context> &model_context) { 6908diff --git a/mindspore/lite/src/litert/cxx_api/model/model_impl.h b/mindspore/lite/src/litert/cxx_api/model/model_impl.h 6909index 19433cce..8e11ee55 100644 6910--- a/mindspore/lite/src/litert/cxx_api/model/model_impl.h 6911+++ b/mindspore/lite/src/litert/cxx_api/model/model_impl.h 6912@@ -49,9 +49,6 @@ typedef std::shared_ptr<lite::LiteSession>(CreateTrainSessionProto)(std::shared_ 6913 const std::shared_ptr<lite::InnerContext> &context); 6914 MS_API CreateTrainSessionProto *CreateTrainSessionCallbackHolder(CreateTrainSessionProto *proto = nullptr); 6915 6916-using ExpressionLoader = std::function<Status(const char *, Graph *)>; 6917-MS_API ExpressionLoader CreateExpressionLoader(const ExpressionLoader &loader = nullptr); 6918- 6919 namespace session { 6920 class Metrics; 6921 class TrainLoopCallBack; 6922@@ -106,7 +103,6 @@ class ModelImpl { 6923 6924 static bool CheckModelSupport(const std::string &device_type, ModelType model_type); 6925 bool IsTrainModel(); 6926- std::unique_ptr<Graph> BuildTrain(Node *optimizer, std::vector<Expr *> inputs); 6927 Status SetupVirtualBatch(int virtual_batch_multiplier, float lr, float momentum); 6928 Status SetLearningRate(float learning_rate); 6929 float GetLearningRate(); 6930diff --git a/mindspore/lite/src/litert/cxx_api/serialization.cc b/mindspore/lite/src/litert/cxx_api/serialization.cc 6931index 08dfaf61..1bc33a69 100644 6932--- a/mindspore/lite/src/litert/cxx_api/serialization.cc 6933+++ b/mindspore/lite/src/litert/cxx_api/serialization.cc 6934@@ -20,7 +20,6 @@ 6935 #include "include/api/graph.h" 6936 #include "include/api/types.h" 6937 #include "include/model.h" 6938-#include "src/litert/cxx_api/expression/net_impl.h" 6939 #include "src/litert/cxx_api/graph/graph_data.h" 6940 #include "src/litert/cxx_api/model/model_impl.h" 6941 #include "src/litert/cxx_api/converters.h" 6942@@ -28,8 +27,6 @@ 6943 #include "src/litert/lite_session.h" 6944 6945 namespace mindspore { 6946-std::function<int(void *)> ExpressionCallback; 6947- 6948 Key::Key(const char *dec_key, size_t key_len) { 6949 len = 0; 6950 if (key_len >= max_key_len) { 6951@@ -121,31 +118,19 @@ Status Serialization::Load(const std::vector<char> &file, ModelType model_type, 6952 MS_LOG(ERROR) << "Read model file failed"; 6953 return kLiteNullptr; 6954 } 6955- if (graph->IsExecutable()) { 6956- auto model = 6957- std::shared_ptr<lite::Model>(lite::ImportFromBuffer(static_cast<const char *>(model_buf), model_size, true)); 6958- if (model == nullptr) { 6959- MS_LOG(ERROR) << "New model failed."; 6960- return kLiteNullptr; 6961- } 6962- auto graph_data = std::shared_ptr<Graph::GraphData>(new (std::nothrow) Graph::GraphData(model)); 6963- if (graph_data == nullptr) { 6964- MS_LOG(ERROR) << "New graph data failed."; 6965- return kLiteMemoryFailed; 6966- } 6967- *graph = Graph(graph_data); 6968- return kSuccess; 6969- } else { 6970- auto loader = CreateExpressionLoader(); 6971- if (loader == nullptr) { 6972- MS_LOG(ERROR) << "Unsupported Feature."; 6973- delete[] model_buf; 6974- return kLiteError; 6975- } 6976- (void)loader(model_buf, graph); 6977- delete[] model_buf; 6978- return kSuccess; 6979+ auto model = 6980+ std::shared_ptr<lite::Model>(lite::ImportFromBuffer(static_cast<const char *>(model_buf), model_size, true)); 6981+ if (model == nullptr) { 6982+ MS_LOG(ERROR) << "New model failed."; 6983+ return kLiteNullptr; 6984 } 6985+ auto graph_data = std::shared_ptr<Graph::GraphData>(new (std::nothrow) Graph::GraphData(model)); 6986+ if (graph_data == nullptr) { 6987+ MS_LOG(ERROR) << "New graph data failed."; 6988+ return kLiteMemoryFailed; 6989+ } 6990+ *graph = Graph(graph_data); 6991+ return kSuccess; 6992 } 6993 6994 Status Serialization::Load(const std::vector<std::vector<char>> &files, ModelType model_type, 6995diff --git a/mindspore/lite/src/litert/cxx_api/train/model.cc b/mindspore/lite/src/litert/cxx_api/train/model.cc 6996index 40525d9d..2ac44ada 100644 6997--- a/mindspore/lite/src/litert/cxx_api/train/model.cc 6998+++ b/mindspore/lite/src/litert/cxx_api/train/model.cc 6999@@ -15,7 +15,6 @@ 7000 */ 7001 7002 #include "include/api/model.h" 7003-#include "include/api/net.h" 7004 #include "include/api/callback/callback.h" 7005 #include "include/api/dual_abi_helper.h" 7006 #include "src/litert/cxx_api/model/model_impl.h" 7007diff --git a/mindspore/lite/src/litert/cxx_api/train/model_build.cc b/mindspore/lite/src/litert/cxx_api/train/model_build.cc 7008index c2f0161b..6ec79777 100644 7009--- a/mindspore/lite/src/litert/cxx_api/train/model_build.cc 7010+++ b/mindspore/lite/src/litert/cxx_api/train/model_build.cc 7011@@ -18,34 +18,6 @@ 7012 #include "src/common/log_adapter.h" 7013 #include "src/litert/cxx_api/model/model_impl.h" 7014 namespace mindspore { 7015-Status Model::Build(GraphCell lossGraphCell, Node *optimizer, std::vector<Expr *> inputs, 7016- const std::shared_ptr<Context> &model_context, const std::shared_ptr<TrainCfg> &train_cfg) { 7017- std::stringstream err_msg; 7018- if (impl_ == nullptr) { 7019- impl_ = std::make_shared<ModelImpl>(); 7020- if (impl_ == nullptr) { 7021- MS_LOG(ERROR) << "Model implement is null."; 7022- return kLiteFileError; 7023- } 7024- } 7025- auto lossGraph = lossGraphCell.GetGraph(); 7026- if (lossGraph == nullptr) { 7027- err_msg << "Invalid null graph"; 7028- MS_LOG(ERROR) << err_msg.str(); 7029- return Status(kLiteNullptr, err_msg.str()); 7030- } 7031- impl_->SetContext(model_context); 7032- impl_->SetConfig(train_cfg); 7033- impl_->SetGraph(lossGraph); 7034- auto graph = impl_->BuildTrain(optimizer, inputs); 7035- auto status = Build(GraphCell(*graph), model_context, train_cfg); 7036- if (status != mindspore::kSuccess) { 7037- MS_LOG(ERROR) << "Error " << status << " during model build"; 7038- return status; 7039- } 7040- return kSuccess; // status 7041-} 7042- 7043 Status Model::BuildTransferLearning(GraphCell backbone, GraphCell head, const std::shared_ptr<Context> &context, 7044 const std::shared_ptr<TrainCfg> &train_cfg) { 7045 std::stringstream err_msg; 7046diff --git a/mindspore/lite/src/litert/cxx_api/train/model_build_impl.cc b/mindspore/lite/src/litert/cxx_api/train/model_build_impl.cc 7047index 28328944..ef561708 100644 7048--- a/mindspore/lite/src/litert/cxx_api/train/model_build_impl.cc 7049+++ b/mindspore/lite/src/litert/cxx_api/train/model_build_impl.cc 7050@@ -18,35 +18,7 @@ 7051 #include "include/train/train_cfg.h" 7052 #include "src/litert/cxx_api/converters.h" 7053 #include "src/train/transfer_session.h" 7054-#include "src/litert/cxx_api/expression/node_impl.h" 7055-#include "src/litert/cxx_api/expression/net_impl.h" 7056 namespace mindspore { 7057-std::unique_ptr<Graph> ModelImpl::BuildTrain(Node *optimizer, std::vector<Expr *> inputs) { 7058- auto opt_impl = NodeImpl::GetImpl(optimizer); 7059- if (opt_impl == nullptr) { 7060- MS_LOG(ERROR) << "missing optimizer node implementation"; 7061- return nullptr; 7062- } 7063- auto opt = opt_impl->node(); 7064- auto in = Expr::convert(inputs); 7065- auto net_impl = NetImpl::GetImpl(graph_->net_data_->net().get()); 7066- if (net_impl == nullptr) { 7067- MS_LOG(ERROR) << "missing net implementation"; 7068- return nullptr; 7069- } 7070- auto trained_net = net_impl->net()->TrainNet(opt, in); 7071- if (trained_net == nullptr) { 7072- MS_LOG(ERROR) << "failed to train network"; 7073- return nullptr; 7074- } 7075- auto mgraph = net_impl->MakeMs(); 7076- if (mgraph == nullptr) { 7077- MS_LOG(ERROR) << "failed to create graph"; 7078- return nullptr; 7079- } 7080- return mgraph; 7081-} 7082- 7083 Status ModelImpl::BuildTransferLearning(const std::shared_ptr<Graph> &backbone, const std::shared_ptr<Graph> &head) { 7084 const auto b_graph_data = backbone->graph_data_; 7085 const auto h_graph_data = head->graph_data_; 7086diff --git a/mindspore/lite/src/litert/cxx_api/train/model_impl.cc b/mindspore/lite/src/litert/cxx_api/train/model_impl.cc 7087index e2fe7a82..726b0585 100644 7088--- a/mindspore/lite/src/litert/cxx_api/train/model_impl.cc 7089+++ b/mindspore/lite/src/litert/cxx_api/train/model_impl.cc 7090@@ -20,7 +20,6 @@ 7091 #include "include/api/serialization.h" 7092 #include "include/api/callback/callback.h" 7093 #include "include/api/metrics/metrics.h" 7094-#include "src/litert/cxx_api/expression/net_impl.h" 7095 #include "src/litert/cxx_api/converters.h" 7096 #include "src/litert/cxx_api/metrics/metrics_adapter.h" 7097 #include "src/litert/cxx_api/metrics/metrics_impl.h" 7098diff --git a/mindspore/lite/src/train/graph_fusion.cc b/mindspore/lite/src/train/graph_fusion.cc 7099index 7982f818..980b2baa 100644 7100--- a/mindspore/lite/src/train/graph_fusion.cc 7101+++ b/mindspore/lite/src/train/graph_fusion.cc 7102@@ -27,6 +27,7 @@ 7103 #include "tools/converter/legacy_optimizer/graph/subgraph_node_pass.h" 7104 #include "src/train/optimizer/fusion/matmul_add_fusion_pass.h" 7105 #include "src/train/optimizer/fusion/matmul_matmul_add_fusion_pass.h" 7106+#include "src/train/optimizer/fusion/remove_redundant_tensor.h" 7107 7108 namespace mindspore { 7109 namespace lite { 7110@@ -64,6 +65,12 @@ STATUS GraphFusion::Run(schema::MetaGraphT *graph) { 7111 MS_LOG(ERROR) << "graph fusion failed."; 7112 return RET_ERROR; 7113 } 7114+ auto opt_tensor = new (std::nothrow) RemoveRedundantTensor(); 7115+ MS_CHECK_TRUE_MSG(opt_tensor != nullptr, RET_NULL_PTR, "Create RemoveRedundantTensor failed."); 7116+ if (opt_tensor->Run(graph) != RET_OK) { 7117+ MS_LOG(ERROR) << "Do RemoveRedundantTensor failed."; 7118+ return RET_ERROR; 7119+ } 7120 return RET_OK; 7121 } 7122 } // namespace lite 7123diff --git a/mindspore/lite/src/train/optimizer/fusion/remove_redundant_tensor.cc b/mindspore/lite/src/train/optimizer/fusion/remove_redundant_tensor.cc 7124new file mode 100644 7125index 00000000..e74e78e2 7126--- /dev/null 7127+++ b/mindspore/lite/src/train/optimizer/fusion/remove_redundant_tensor.cc 7128@@ -0,0 +1,89 @@ 7129+/** 7130+ * Copyright 2023 Huawei Technologies Co., Ltd 7131+ * 7132+ * Licensed under the Apache License, Version 2.0 (the "License"); 7133+ * you may not use this file except in compliance with the License. 7134+ * You may obtain a copy of the License at 7135+ * 7136+ * http://www.apache.org/licenses/LICENSE-2.0 7137+ * 7138+ * Unless required by applicable law or agreed to in writing, software 7139+ * distributed under the License is distributed on an "AS IS" BASIS, 7140+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 7141+ * See the License for the specific language governing permissions and 7142+ * limitations under the License. 7143+ */ 7144+ 7145+#include "src/train/optimizer/fusion/remove_redundant_tensor.h" 7146+#include <map> 7147+#include "src/common/log_adapter.h" 7148+#include "nnacl/op_base.h" 7149+ 7150+namespace mindspore { 7151+namespace lite { 7152+STATUS RemoveRedundantTensor::Run(schema::MetaGraphT *graph) { 7153+ if (graph == nullptr) { 7154+ MS_LOG(ERROR) << "The graph is a nullptr."; 7155+ return RET_NULL_PTR; 7156+ } 7157+ std::map<uint32_t, uint32_t> index_map; 7158+ uint32_t index = 0; 7159+ auto graph_input_index = graph->inputIndex; 7160+ graph->inputIndex.clear(); 7161+ for (auto input_index : graph_input_index) { 7162+ if (index_map.find(input_index) == index_map.end()) { 7163+ index_map[input_index] = index; 7164+ ++index; 7165+ } 7166+ graph->inputIndex.push_back(index_map[input_index]); 7167+ } 7168+ for (auto &node : graph->nodes) { 7169+ auto node_in_index = node->inputIndex; 7170+ node->inputIndex.clear(); 7171+ for (auto in_index : node_in_index) { 7172+ if (index_map.find(in_index) == index_map.end()) { 7173+ index_map[in_index] = index; 7174+ ++index; 7175+ } 7176+ node->inputIndex.push_back(index_map[in_index]); 7177+ } 7178+ auto node_out_index = node->outputIndex; 7179+ node->outputIndex.clear(); 7180+ for (auto out_index : node_out_index) { 7181+ if (index_map.find(out_index) == index_map.end()) { 7182+ index_map[out_index] = index; 7183+ ++index; 7184+ } 7185+ node->outputIndex.push_back(index_map[out_index]); 7186+ } 7187+ } 7188+ auto graph_output_index = graph->outputIndex; 7189+ graph->outputIndex.clear(); 7190+ for (auto output_index : graph_output_index) { 7191+ if (index_map.find(output_index) == index_map.end()) { 7192+ index_map[output_index] = index; 7193+ ++index; 7194+ } 7195+ graph->outputIndex.push_back(index_map[output_index]); 7196+ } 7197+ std::vector<std::unique_ptr<mindspore::schema::TensorT>> old_tensors; 7198+ old_tensors.swap(graph->allTensors); 7199+ graph->allTensors.resize(index_map.size()); 7200+ for (size_t i = 0; i < old_tensors.size(); ++i) { 7201+ if (index_map.find(i) == index_map.end()) { 7202+ continue; 7203+ } 7204+ graph->allTensors[index_map[i]].swap(old_tensors[i]); 7205+ } 7206+ if (!graph->subGraph.empty()) { 7207+ graph->subGraph[0]->inputIndices = graph->inputIndex; 7208+ graph->subGraph[0]->outputIndices = graph->outputIndex; 7209+ graph->subGraph[0]->tensorIndices = {}; 7210+ for (uint32_t i = 0; i < index; ++i) { 7211+ graph->subGraph[0]->tensorIndices.push_back(i); 7212+ } 7213+ } 7214+ return RET_OK; 7215+} 7216+} // namespace lite 7217+} // namespace mindspore 7218diff --git a/mindspore/lite/src/expression/ops/depend.h b/mindspore/lite/src/train/optimizer/fusion/remove_redundant_tensor.h 7219similarity index 55% 7220rename from mindspore/lite/src/expression/ops/depend.h 7221rename to mindspore/lite/src/train/optimizer/fusion/remove_redundant_tensor.h 7222index 0995e664..8da58dc5 100644 7223--- a/mindspore/lite/src/expression/ops/depend.h 7224+++ b/mindspore/lite/src/train/optimizer/fusion/remove_redundant_tensor.h 7225@@ -1,5 +1,5 @@ 7226 /** 7227- * Copyright 2022 Huawei Technologies Co., Ltd 7228+ * Copyright 2023 Huawei Technologies Co., Ltd 7229 * 7230 * Licensed under the Apache License, Version 2.0 (the "License"); 7231 * you may not use this file except in compliance with the License. 7232@@ -14,24 +14,23 @@ 7233 * limitations under the License. 7234 */ 7235 7236-#ifndef MINDSPORE_LITE_SRC_EXPRESSION_OPS_DEPEND_H_ 7237-#define MINDSPORE_LITE_SRC_EXPRESSION_OPS_DEPEND_H_ 7238+#ifndef MINDSPORE_LITE_TOOLS_CONVERTER_LEGACY_OPTIMIZER_FUSION_REMOVE_REDUNDANT_TENSOR_H_ 7239+#define MINDSPORE_LITE_TOOLS_CONVERTER_LEGACY_OPTIMIZER_FUSION_REMOVE_REDUNDANT_TENSOR_H_ 7240 7241-#include <vector> 7242-#include "src/expression/node.h" 7243-#include "src/expression/ops.h" 7244-#include "inner/model_generated.h" 7245+#include "include/errorcode.h" 7246+#include "schema/inner/model_generated.h" 7247 7248 namespace mindspore { 7249 namespace lite { 7250-class DependM : public Node { 7251+class RemoveRedundantTensor { 7252 public: 7253- DependM(); 7254-}; 7255+ RemoveRedundantTensor() = default; 7256+ 7257+ ~RemoveRedundantTensor() = default; 7258 7259-namespace NN { 7260-Node *Depend(); 7261+ STATUS Run(schema::MetaGraphT *graph); 7262 }; 7263 } // namespace lite 7264 } // namespace mindspore 7265-#endif // MINDSPORE_LITE_SRC_EXPRESSION_OPS_DEPEND_H_ 7266+ 7267+#endif // MINDSPORE_LITE_TOOLS_CONVERTER_LEGACY_OPTIMIZER_FUSION_REMOVE_REDUNDANT_TENSOR_H_ 7268diff --git a/mindspore/lite/src/train/train_session.cc b/mindspore/lite/src/train/train_session.cc 7269index c123cba8..fa7625cb 100644 7270--- a/mindspore/lite/src/train/train_session.cc 7271+++ b/mindspore/lite/src/train/train_session.cc 7272@@ -1248,38 +1248,53 @@ int TrainSession::FindExportKernels(std::vector<kernel::KernelExec *> *export_ke 7273 } 7274 7275 template <typename DestType> 7276-int TrainSession::ExportByDifferentType(DestType destination, ModelType model_type, QuantizationType quant_type, 7277- bool orig_train_state, std::vector<std::string> output_tensor_name) { 7278+int TrainSession::ExportInner(DestType destination, ModelType model_type, QuantizationType quant_type, 7279+ FormatType format, std::vector<std::string> out_put_tensor_name) { 7280+ if constexpr (std::is_same_v<DestType, const std::string &>) { 7281+ MS_CHECK_FALSE_MSG(destination.empty(), RET_ERROR, "File name cannot be empty"); 7282+ struct stat path_type; 7283+ if (stat(destination.c_str(), &path_type) == RET_OK) { 7284+ if (path_type.st_mode & S_IFDIR) { 7285+ MS_LOG(ERROR) << "Destination must be path, now is a directory"; 7286+ return RET_ERROR; 7287+ } 7288+ } 7289+ } else if constexpr (std::is_same_v<DestType, Buffer *>) { 7290+ MS_CHECK_FALSE_MSG(destination == nullptr, RET_ERROR, "model buffer cannot be nullptr"); 7291+ } else { 7292+ MS_LOG(ERROR) << "Unsupported destination."; 7293+ return RET_ERROR; 7294+ } 7295+ MS_CHECK_FALSE_MSG(model_type > mindspore::lite::MT_INFERENCE || model_type < mindspore::lite::MT_TRAIN, RET_ERROR, 7296+ "Export model type parameter error"); 7297+ MS_CHECK_FALSE_MSG(quant_type < mindspore::lite::QT_DEFAULT || quant_type > mindspore::lite::QT_WEIGHT, RET_ERROR, 7298+ "Export quant type parameter error"); 7299+ MS_CHECK_FALSE_MSG(format != FT_FLATBUFFERS, RET_ERROR, "File name cannot be empty"); 7300+ 7301+ bool orig_train_state = IsTrain(); 7302 TrainExport texport(destination); 7303 int status = texport.ExportInit(model_.get()->graph_.name_, model_.get()->graph_.version_); 7304 TRAIN_SESSION_CHECK_FALSE_MSG(status != RET_OK, status, "Fail to init export"); 7305- if (!output_tensor_name.empty() && model_type == MT_INFERENCE) { 7306+ 7307+ if (!out_put_tensor_name.empty() && model_type == MT_INFERENCE) { 7308 std::vector<kernel::KernelExec *> export_kernels = {}; 7309- status = FindExportKernels(&export_kernels, output_tensor_name, const_fold_kernels_); 7310+ status = FindExportKernels(&export_kernels, out_put_tensor_name, const_fold_kernels_); 7311 TRAIN_SESSION_CHECK_FALSE_MSG(status != RET_OK, status, "FindExportKernels failed."); 7312 status = 7313- texport.ExportNet(export_kernels, tensors_, const_output_tensors_, output_tensor_name, model_.get(), quant_type); 7314+ texport.ExportNet(export_kernels, tensors_, const_output_tensors_, out_put_tensor_name, model_.get(), quant_type); 7315 } else { 7316- if (!output_tensor_name.empty() && model_type == MT_TRAIN) { 7317- MS_LOG(WARNING) << "Train model does not support to export selected output tensor, and all of the train kernels " 7318- "tensors will be exported"; 7319- } 7320 if ((!model_buff_changed_) && (quant_type == QT_NONE) && (model_type == MT_TRAIN) && 7321 std::all_of(model_->graph_.all_nodes_.begin(), model_->graph_.all_nodes_.end(), [](const LiteGraph::Node *n) { 7322 return n->quant_type_ == schema::QuantType::QuantType_QUANT_NONE; 7323 })) { 7324 status = texport.SaveModel(model_.get(), destination); 7325- TRAIN_SESSION_CHECK_FALSE_MSG(status != RET_OK, status, "Failed to save model"); 7326- if (orig_train_state) { 7327- status = Train(); 7328- TRAIN_SESSION_CHECK_FALSE_MSG(status != RET_OK, status, "Train failed."); 7329- } 7330+ if (orig_train_state) Train(); 7331 return status; 7332 } else { 7333 if (quant_type == QT_NONE) { 7334 status = texport.ExportNet( 7335- (model_type == MT_TRAIN) ? train_kernels_ : const_fold_kernels_, tensors_, const_output_tensors_, 7336- (model_type == MT_TRAIN) ? train_output_tensor_names_ : eval_output_tensor_names_, model_.get(), quant_type); 7337+ (model_type == MT_TRAIN) ? train_kernels_ : const_fold_kernels_, tensors_, const_output_tensors_, 7338+ (model_type == MT_TRAIN) ? train_output_tensor_names_ : eval_output_tensor_names_, model_.get(), quant_type); 7339 } else { 7340 status = texport.ExportNet((model_type == MT_TRAIN) ? train_kernels_ : inference_kernels_, tensors_, {}, 7341 (model_type == MT_TRAIN) ? train_output_tensor_names_ : eval_output_tensor_names_, 7342@@ -1288,6 +1303,7 @@ int TrainSession::ExportByDifferentType(DestType destination, ModelType model_ty 7343 } 7344 } 7345 TRAIN_SESSION_CHECK_FALSE_MSG(status != RET_OK, status, "Fail to export Network."); 7346+ 7347 if (model_type == MT_INFERENCE) { 7348 status = texport.TrainModelDrop(); 7349 TRAIN_SESSION_CHECK_FALSE_MSG(status != RET_OK, status, "TrainModelDrop failed."); 7350@@ -1304,46 +1320,8 @@ int TrainSession::ExportByDifferentType(DestType destination, ModelType model_ty 7351 status = texport.SaveToBuffer(); 7352 TRAIN_SESSION_CHECK_FALSE_MSG(status != RET_OK, status, "fail to save to model buffer."); 7353 } 7354- return RET_OK; 7355-} 7356- 7357-template <typename DestType> 7358-int TrainSession::ExportInner(DestType destination, ModelType model_type, QuantizationType quant_type, 7359- FormatType format, std::vector<std::string> out_put_tensor_name) { 7360- if constexpr (std::is_same_v<DestType, const std::string &>) { 7361- MS_CHECK_FALSE_MSG(destination.empty(), RET_ERROR, "File name cannot be empty"); 7362- struct stat path_type; 7363- if (stat(destination.c_str(), &path_type) == RET_OK) { 7364- if (path_type.st_mode & S_IFDIR) { 7365- MS_LOG(ERROR) << "Destination must be path, now is a directory"; 7366- return RET_ERROR; 7367- } 7368- } 7369- } else if constexpr (std::is_same_v<DestType, Buffer *>) { 7370- MS_CHECK_FALSE_MSG(destination == nullptr, RET_ERROR, "model buffer cannot be nullptr"); 7371- } else { 7372- MS_LOG(ERROR) << "Unsupported destination."; 7373- return RET_ERROR; 7374- } 7375- MS_CHECK_FALSE_MSG(model_type > mindspore::lite::MT_INFERENCE || model_type < mindspore::lite::MT_TRAIN, RET_ERROR, 7376- "Export model type parameter error"); 7377- MS_CHECK_FALSE_MSG(quant_type < mindspore::lite::QT_DEFAULT || quant_type > mindspore::lite::QT_WEIGHT, RET_ERROR, 7378- "Export quant type parameter error"); 7379- MS_CHECK_FALSE_MSG(format != FT_FLATBUFFERS, RET_ERROR, "File name cannot be empty"); 7380- 7381- bool orig_train_state = IsTrain(); 7382- int status = Eval(); 7383- TRAIN_SESSION_CHECK_FALSE_MSG(status != RET_OK, status, "Eval failed"); 7384- status = ExportByDifferentType<DestType>(destination, model_type, quant_type, orig_train_state, out_put_tensor_name); 7385- if (status != RET_OK) { 7386- MS_LOG(ERROR) << "Fail to export by different type"; 7387- return status; 7388- } 7389- if (orig_train_state) { 7390- status = Train(); 7391- TRAIN_SESSION_CHECK_FALSE_MSG(status != RET_OK, status, "Train failed"); 7392- } 7393- return RET_OK; 7394+ if (orig_train_state) Train(); 7395+ return status; 7396 } 7397 7398 int TrainSession::Export(const std::string &file_name, ModelType model_type, QuantizationType quant_type, 7399diff --git a/mindspore/lite/src/train/train_session.h b/mindspore/lite/src/train/train_session.h 7400index 0bd14b21..0a65f64e 100644 7401--- a/mindspore/lite/src/train/train_session.h 7402+++ b/mindspore/lite/src/train/train_session.h 7403@@ -175,9 +175,6 @@ class TrainSession : virtual public lite::LiteSession { 7404 const std::unordered_map<lite::Tensor *, size_t> &offset_map, 7405 std::unordered_map<lite::Tensor *, int> *ref_count, uint32_t input_idx); 7406 template <typename DestType> 7407- int ExportByDifferentType(DestType destination, ModelType model_type, QuantizationType quant_type, 7408- bool orig_train_state, std::vector<std::string> output_tensor_name = {}); 7409- template <typename DestType> 7410 int ExportInner(DestType destination, ModelType model_type, QuantizationType quant_type, FormatType, 7411 std::vector<std::string> out_put_tensor_name = {}); 7412 std::map<Tensor *, Tensor *> restored_origin_tensors_; 7413diff --git a/mindspore/lite/test/config_level0/models_ms_train.cfg b/mindspore/lite/test/config_level0/models_ms_train.cfg 7414index 645a31c4..7a6b9702 100644 7415--- a/mindspore/lite/test/config_level0/models_ms_train.cfg 7416+++ b/mindspore/lite/test/config_level0/models_ms_train.cfg 7417@@ -51,7 +51,5 @@ vae 7418 unified_api code_example 7419 train_lenet code_example 7420 train_lenet_java code_example 7421-lenet expression 7422-mobilenetv2 expression noarm32 7423 # LAST 7424 #test_resize inputShapes 16,10,10,1:16,10,10,1 0.5 7425diff --git a/mindspore/lite/tools/benchmark_train/CMakeLists.txt b/mindspore/lite/tools/benchmark_train/CMakeLists.txt 7426index 1b9fc347..3c92af7f 100644 7427--- a/mindspore/lite/tools/benchmark_train/CMakeLists.txt 7428+++ b/mindspore/lite/tools/benchmark_train/CMakeLists.txt 7429@@ -17,13 +17,6 @@ set(TEST_SRC 7430 # add static securec link library 7431 include(${TOP_DIR}/cmake/dependency_securec.cmake) 7432 7433-if(MSLITE_MINDDATA_IMPLEMENT STREQUAL "full") 7434- set(TEST_SRC 7435- ${TEST_SRC} 7436- ${CMAKE_CURRENT_SOURCE_DIR}/net_runner.cc 7437- ) 7438-endif() 7439- 7440 add_executable(benchmark_train 7441 ${TEST_SRC} 7442 ${COMMON_SRC}) 7443diff --git a/mindspore/lite/tools/benchmark_train/net_runner.cc b/mindspore/lite/tools/benchmark_train/net_runner.cc 7444deleted file mode 100644 7445index edf3e964..00000000 7446--- a/mindspore/lite/tools/benchmark_train/net_runner.cc 7447+++ /dev/null 7448@@ -1,371 +0,0 @@ 7449-/** 7450- * Copyright 2022 Huawei Technologies Co., Ltd 7451- * 7452- * Licensed under the Apache License, Version 2.0 (the "License"); 7453- * you may not use this file except in compliance with the License. 7454- * You may obtain a copy of the License at 7455- * 7456- * http://www.apache.org/licenses/LICENSE-2.0 7457- * 7458- * Unless required by applicable law or agreed to in writing, software 7459- * distributed under the License is distributed on an "AS IS" BASIS, 7460- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 7461- * See the License for the specific language governing permissions and 7462- * limitations under the License. 7463- */ 7464- 7465-#include "tools/benchmark_train/net_runner.h" 7466-#include "tools/benchmark_train/net_train_base.h" 7467-#include <getopt.h> 7468-#include <malloc.h> 7469-#include <cmath> 7470-#include <cstdio> 7471-#include <cstring> 7472-#include <iostream> 7473-#include <fstream> 7474-#include <utility> 7475-#include <chrono> 7476-#include "include/api/types.h" 7477-#include "include/api/context.h" 7478-#include "include/api/serialization.h" 7479-#include "include/api/callback/loss_monitor.h" 7480-#include "include/api/metrics/accuracy.h" 7481-#include "include/api/callback/ckpt_saver.h" 7482-#include "include/api/callback/train_accuracy.h" 7483-#include "include/api/callback/lr_scheduler.h" 7484-#include "include/dataset/datasets.h" 7485-#include "include/dataset/vision_lite.h" 7486-#include "include/dataset/transforms.h" 7487-#include "include/api/cfg.h" 7488-#include "include/api/net.h" 7489- 7490-using mindspore::AccuracyMetrics; 7491-using mindspore::Model; 7492-using mindspore::TrainAccuracy; 7493-using mindspore::TrainCallBack; 7494-using mindspore::TrainCallBackData; 7495-using mindspore::dataset::Dataset; 7496-using mindspore::dataset::Mnist; 7497-using mindspore::dataset::SequentialSampler; 7498-using mindspore::dataset::TensorOperation; 7499-using mindspore::dataset::transforms::TypeCast; 7500-using mindspore::dataset::vision::Normalize; 7501-using mindspore::dataset::vision::Resize; 7502- 7503-constexpr int kNCHWCDim = 2; 7504-constexpr int kPrintTimes = 100; 7505-constexpr float kBetta1 = 0.9f; 7506-constexpr float kBetta2 = 0.999f; 7507- 7508-class Rescaler : public mindspore::TrainCallBack { 7509- public: 7510- explicit Rescaler(float scale) : scale_(scale) { 7511- if (scale_ == 0) { 7512- scale_ = 1.0; 7513- } 7514- } 7515- ~Rescaler() override = default; 7516- void StepBegin(const mindspore::TrainCallBackData &cb_data) override { 7517- auto inputs = cb_data.model_->GetInputs(); 7518- auto *input_data = reinterpret_cast<float *>(inputs.at(0).MutableData()); 7519- for (int k = 0; k < inputs.at(0).ElementNum(); k++) input_data[k] /= scale_; 7520- } 7521- 7522- private: 7523- float scale_ = 1.0; 7524-}; 7525- 7526-/* This is an example of a user defined Callback to measure memory and latency of execution */ 7527-class Measurement : public mindspore::TrainCallBack { 7528- public: 7529- explicit Measurement(unsigned int epochs) 7530- : time_avg_(std::chrono::duration<double, std::milli>(0)), epochs_(epochs) {} 7531- ~Measurement() override = default; 7532- void EpochBegin(const mindspore::TrainCallBackData &cb_data) override { 7533- start_time_ = std::chrono::high_resolution_clock::now(); 7534- } 7535- mindspore::CallbackRetValue EpochEnd(const mindspore::TrainCallBackData &cb_data) override { 7536- end_time_ = std::chrono::high_resolution_clock::now(); 7537- auto time = std::chrono::duration<double, std::milli>(end_time_ - start_time_); 7538- time_avg_ += time; 7539- return mindspore::kContinue; 7540- } 7541- void End(const mindspore::TrainCallBackData &cb_data) override { 7542- if (epochs_ > 0) { 7543- std::cout << "AvgRunTime: " << time_avg_.count() / epochs_ << " ms" << std::endl; 7544- } 7545- 7546- struct mallinfo info = mallinfo(); 7547- std::cout << "Total allocation: " << info.arena + info.hblkhd << std::endl; 7548- } 7549- 7550- private: 7551- std::chrono::time_point<std::chrono::high_resolution_clock> start_time_; 7552- std::chrono::time_point<std::chrono::high_resolution_clock> end_time_; 7553- std::chrono::duration<double, std::milli> time_avg_; 7554- unsigned int epochs_; 7555-}; 7556- 7557-NetRunner::~NetRunner() { 7558- if (model_ != nullptr) { 7559- delete model_; 7560- } 7561- if (graph_ != nullptr) { 7562- delete graph_; 7563- } 7564-} 7565- 7566-mindspore::Status NetRunner::InitAndFigureInputs() { 7567- auto context = std::make_shared<mindspore::Context>(); 7568- auto cpu_context = std::make_shared<mindspore::CPUDeviceInfo>(); 7569- cpu_context->SetEnableFP16(enable_fp16_); 7570- context->MutableDeviceInfo().push_back(cpu_context); 7571- 7572- graph_ = new (std::nothrow) mindspore::Graph(mindspore::Graph::Type::kExpressionGraph); 7573- if (graph_ == nullptr) { 7574- std::cout << "Cannot allocate graph" << std::endl; 7575- return mindspore::kLiteMemoryFailed; 7576- } 7577- auto status = mindspore::Serialization::Load(ms_file_, mindspore::kMindIR, graph_); 7578- if (status != mindspore::kSuccess) { 7579- std::cout << "Error " << status << " during serialization of graph " << ms_file_; 7580- return status; 7581- } 7582- auto net = std::make_unique<mindspore::Net>(*graph_); 7583- auto input_shape = net->InputShape(0); 7584- auto label_shape = net->OutputShape(0); 7585- auto inputM = mindspore::NN::Input(input_shape); 7586- auto labelM = mindspore::NN::Input(label_shape); 7587- auto label = labelM->Create("label"); 7588- auto input = inputM->Create("input"); 7589- 7590- auto cfg = std::make_shared<mindspore::TrainCfg>(); 7591- if (enable_fp16_) { 7592- cfg.get()->optimization_level_ = mindspore::kO2; 7593- } 7594- 7595- model_ = new (std::nothrow) mindspore::Model(); 7596- if (model_ == nullptr) { 7597- std::cout << "model allocation failed" << std::endl; 7598- return mindspore::kLiteMemoryFailed; 7599- } 7600- mindspore::SoftMaxCrossEntropyCfg softmax_ce_cfg; 7601- softmax_ce_cfg.reduction = "none"; 7602- auto netWithLoss = mindspore::NN::GraphWithLoss(graph_, mindspore::NN::SoftmaxCrossEntropy(softmax_ce_cfg)); 7603- mindspore::AdamConfig AdamCfg; 7604- AdamCfg.beta1_ = kBetta1; 7605- AdamCfg.beta2_ = kBetta2; 7606- AdamCfg.eps_ = 1e-8; 7607- AdamCfg.learning_rate_ = 1e-2; 7608- auto optimizer = mindspore::NN::Adam(net->trainable_params(), AdamCfg); 7609- status = model_->Build(mindspore::GraphCell(*netWithLoss), optimizer, {input, label}, context, cfg); 7610- if (status != mindspore::kSuccess) { 7611- std::cout << "Error " << status << " during build of model " << ms_file_ << std::endl; 7612- return status; 7613- } 7614- delete graph_; 7615- graph_ = nullptr; 7616- auto inputs = model_->GetInputs(); 7617- if (inputs.size() < 1) { 7618- return mindspore::kLiteError; 7619- } 7620- auto nhwc_input_dims = inputs.at(0).Shape(); 7621- batch_size_ = nhwc_input_dims.at(0); 7622- h_ = nhwc_input_dims.at(1); 7623- w_ = nhwc_input_dims.at(kNCHWCDim); 7624- return mindspore::kSuccess; 7625-} 7626- 7627-int NetRunner::CompareOutput(const std::vector<mindspore::MSTensor> &outputs) { 7628- std::cout << "================ Comparing Forward Output data ================" << std::endl; 7629- float total_bias = 0; 7630- int total_size = 0; 7631- bool has_error = false; 7632- int i = 1; 7633- for (auto &tensor : outputs) { 7634- std::cout << "output is tensor " << tensor.Name() << "\n"; 7635- auto output = tensor.Data(); 7636- size_t size; 7637- std::string output_file = flags_->data_file_ + std::to_string(i) + ".bin"; 7638- auto bin_buf = std::unique_ptr<float[]>(mindspore::lite::NetTrainBase::ReadFileBuf(output_file.c_str(), &size)); 7639- if (bin_buf == nullptr) { 7640- MS_LOG(ERROR) << "ReadFile return nullptr"; 7641- std::cout << "ReadFile return nullptr" << std::endl; 7642- return mindspore::kLiteNullptr; 7643- } 7644- if (size != tensor.DataSize()) { 7645- MS_LOG(ERROR) << "Output buffer and output file differ by size. Tensor size: " << tensor.DataSize() 7646- << ", read size: " << size; 7647- std::cout << "Output buffer and output file differ by size. Tensor size: " << tensor.DataSize() 7648- << ", read size: " << size << std::endl; 7649- return mindspore::kLiteError; 7650- } 7651- float bias = mindspore::lite::NetTrainBase::CompareData<float>(bin_buf.get(), tensor.ElementNum(), 7652- reinterpret_cast<const float *>(output.get())); 7653- if (bias >= 0) { 7654- total_bias += bias; 7655- total_size++; 7656- } else { 7657- has_error = true; 7658- break; 7659- } 7660- i++; 7661- } 7662- 7663- if (!has_error) { 7664- float mean_bias; 7665- if (total_size != 0) { 7666- mean_bias = total_bias / total_size * kPrintTimes; 7667- } else { 7668- mean_bias = 0; 7669- } 7670- 7671- std::cout << "Mean bias of all nodes/tensors: " << mean_bias << "%" 7672- << " threshold is:" << this->flags_->accuracy_threshold_ << std::endl; 7673- std::cout << "=======================================================" << std::endl << std::endl; 7674- 7675- if (mean_bias > this->flags_->accuracy_threshold_) { 7676- MS_LOG(INFO) << "Mean bias of all nodes/tensors is too big: " << mean_bias << "%"; 7677- std::cout << "Mean bias of all nodes/tensors is too big: " << mean_bias << "%" << std::endl; 7678- return mindspore::kLiteError; 7679- } else { 7680- return mindspore::kSuccess; 7681- } 7682- } else { 7683- MS_LOG(ERROR) << "Error in CompareData"; 7684- std::cout << "Error in CompareData" << std::endl; 7685- std::cout << "=======================================================" << std::endl << std::endl; 7686- return mindspore::kSuccess; 7687- } 7688-} 7689- 7690-void NetRunner::CheckSum(const mindspore::MSTensor &tensor, std::string node_type, int id, std::string in_out) { 7691- constexpr int kPrintLen = 4; 7692- int tensor_size = tensor.ElementNum(); 7693- const void *data = tensor.Data().get(); 7694- const float *fdata = reinterpret_cast<const float *>(data); 7695- mindspore::DataType type = tensor.DataType(); 7696- std::cout << node_type << " " << in_out << id << std::endl; 7697- std::cout << "tensor name: " << tensor.Name() << std::endl; 7698- if ((tensor_size) == 0 || (data == nullptr)) { 7699- std::cout << "Empty tensor" << std::endl; 7700- return; 7701- } 7702- switch (type) { 7703- case mindspore::DataType::kNumberTypeFloat32: 7704- std::cout << "sum=" << mindspore::lite::TensorSum<float>(data, tensor_size) << std::endl; 7705- std::cout << "data: "; 7706- for (int i = 0; i <= kPrintLen && i < tensor_size; i++) { 7707- std::cout << static_cast<float>(fdata[i]) << ", "; 7708- } 7709- std::cout << std::endl; 7710- break; 7711- case mindspore::DataType::kNumberTypeInt32: 7712- std::cout << "sum=" << mindspore::lite::TensorSum<int>(data, tensor_size) << std::endl; 7713- break; 7714- default: 7715- std::cout << "unsupported type:" << static_cast<int>(type) << std::endl; 7716- break; 7717- } 7718-} 7719- 7720-int NetRunner::InitCallbackParameter() { 7721- // after callback 7722- after_call_back_ = [&](const std::vector<mindspore::MSTensor> &after_inputs, 7723- const std::vector<mindspore::MSTensor> &after_outputs, 7724- const mindspore::MSCallBackParam &call_param) { 7725- if (after_inputs.empty()) { 7726- MS_LOG(INFO) << "The num of after inputs is empty"; 7727- } 7728- if (after_outputs.empty()) { 7729- MS_LOG(INFO) << "The num of after outputs is empty"; 7730- } 7731- if (flags_->layer_checksum_) { 7732- for (size_t i = 0; i < after_inputs.size(); i++) { 7733- CheckSum(after_inputs.at(i), call_param.node_type, i, "in"); 7734- } 7735- for (size_t i = 0; i < after_outputs.size(); i++) { 7736- CheckSum(after_outputs.at(i), call_param.node_type, i, "out"); 7737- } 7738- std::cout << std::endl; 7739- } 7740- return true; 7741- }; 7742- return false; 7743-} 7744- 7745-int NetRunner::RunOnce() { 7746- auto inputs = model_->GetInputs(); 7747- std::vector<mindspore::MSTensor> output; 7748- auto status = LoadInput(&inputs); 7749- if (status != mindspore::kSuccess) { 7750- std::cout << "cannot load data"; 7751- return status; 7752- } 7753- model_->SetTrainMode(true); 7754- model_->RunStep(nullptr, nullptr); 7755- model_->SetTrainMode(false); 7756- model_->Predict(inputs, &output, nullptr, nullptr); 7757- return CompareOutput(output); 7758-} 7759- 7760-int NetRunner::LoadInput(std::vector<mindspore::MSTensor> *ms_inputs) { 7761- auto status = ReadInputFile(ms_inputs); 7762- if (status != mindspore::kSuccess) { 7763- std::cout << "Read Input File error, " << status << std::endl; 7764- MS_LOG(ERROR) << "Read Input File error, " << status; 7765- return status; 7766- } 7767- return mindspore::kSuccess; 7768-} 7769- 7770-int NetRunner::ReadInputFile(std::vector<mindspore::MSTensor> *ms_inputs) { 7771- if (ms_inputs->empty()) { 7772- std::cout << "no inputs to input" << std::endl; 7773- return mindspore::kLiteError; 7774- } 7775- for (size_t i = 0; i < ms_inputs->size(); i++) { 7776- auto cur_tensor = ms_inputs->at(i); 7777- if (cur_tensor == nullptr) { 7778- std::cout << "empty tensor " << i << std::endl; 7779- MS_LOG(ERROR) << "empty tensor " << i; 7780- } 7781- size_t size; 7782- std::string file_name = flags_->in_data_file_ + std::to_string(i + 1) + ".bin"; 7783- auto bin_buf = std::unique_ptr<float[]>(mindspore::lite::NetTrainBase::ReadFileBuf(file_name.c_str(), &size)); 7784- if (bin_buf == nullptr) { 7785- MS_LOG(ERROR) << "ReadFile return nullptr"; 7786- std::cout << "ReadFile return nullptr" << std::endl; 7787- return mindspore::kLiteNullptr; 7788- } 7789- auto tensor_data_size = cur_tensor.DataSize(); 7790- if (size != tensor_data_size) { 7791- std::cout << "Input binary file size error, required: " << tensor_data_size << ", in fact: " << size 7792- << " ,file_name: " << file_name.c_str() << std::endl; 7793- MS_LOG(ERROR) << "Input binary file size error, required: " << tensor_data_size << ", in fact: " << size 7794- << " ,file_name: " << file_name.c_str(); 7795- return mindspore::kLiteError; 7796- } 7797- auto input_data = cur_tensor.MutableData(); 7798- memcpy(input_data, bin_buf.get(), tensor_data_size); 7799- } 7800- return mindspore::kSuccess; 7801-} 7802- 7803-int NetRunner::Main() { 7804- ms_file_ = flags_->model_file_; 7805- InitCallbackParameter(); 7806- auto status = InitAndFigureInputs(); 7807- if (status != mindspore::kSuccess) { 7808- std::cout << "failed to initialize network" << std::endl; 7809- return status.StatusCode(); 7810- } 7811- return RunOnce(); 7812-} 7813- 7814-int CallBack(mindspore::lite::NetTrainFlags *flags) { 7815- NetRunner nr(flags); 7816- return nr.Main(); 7817-} 7818- 7819-int init = mindspore::lite::NetTrainBase::SetNr(CallBack); 7820diff --git a/mindspore/lite/tools/benchmark_train/net_runner.h b/mindspore/lite/tools/benchmark_train/net_runner.h 7821deleted file mode 100644 7822index 243b94ef..00000000 7823--- a/mindspore/lite/tools/benchmark_train/net_runner.h 7824+++ /dev/null 7825@@ -1,81 +0,0 @@ 7826-/** 7827- * Copyright 2022 Huawei Technologies Co., Ltd 7828- * 7829- * Licensed under the Apache License, Version 2.0 (the "License"); 7830- * you may not use this file except in compliance with the License. 7831- * You may obtain a copy of the License at 7832- * 7833- * http://www.apache.org/licenses/LICENSE-2.0 7834- * 7835- * Unless required by applicable law or agreed to in writing, software 7836- * distributed under the License is distributed on an "AS IS" BASIS, 7837- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 7838- * See the License for the specific language governing permissions and 7839- * limitations under the License. 7840- */ 7841- 7842-#ifndef MINDSPORE_LITE_TOOLS_BENCHMARK_TRAIN_NET_RUNNER_H_ 7843-#define MINDSPORE_LITE_TOOLS_BENCHMARK_TRAIN_NET_RUNNER_H_ 7844- 7845-#include <tuple> 7846-#include <iomanip> 7847-#include <map> 7848-#include <vector> 7849-#include <memory> 7850-#include <string> 7851-#include "include/api/model.h" 7852-#include "include/api/graph.h" 7853-#include "include/api/types.h" 7854-#include "include/api/status.h" 7855-#include "include/api/metrics/accuracy.h" 7856-#include "include/dataset/datasets.h" 7857- 7858-using mindspore::AccuracyMetrics; 7859-using mindspore::dataset::Dataset; 7860- 7861-namespace mindspore::lite { 7862-class NetTrainFlags; 7863-} 7864- 7865-class NetRunner { 7866- public: 7867- int Main(); 7868- explicit NetRunner(mindspore::lite::NetTrainFlags *flags) : flags_(flags) {} 7869- bool ReadArgs(int argc, int8_t *argv[]); 7870- virtual ~NetRunner(); 7871- 7872- private: 7873- void Usage(); 7874- mindspore::Status InitAndFigureInputs(); 7875- void CheckSum(const mindspore::MSTensor &tensor, std::string node_type, int id, std::string in_out); 7876- int InitCallbackParameter(); 7877- int TrainLoop(); 7878- float CalculateAccuracy(int max_tests = 0); 7879- float GetLoss() const; 7880- int RunOnce(); 7881- int CompareOutput(const std::vector<mindspore::MSTensor> &outputs); 7882- int LoadInput(std::vector<mindspore::MSTensor> *ms_inputs); 7883- int ReadInputFile(std::vector<mindspore::MSTensor> *ms_inputs); 7884- 7885- mindspore::Model *model_ = nullptr; 7886- mindspore::Graph *graph_ = nullptr; 7887- 7888- std::shared_ptr<Dataset> train_ds_; 7889- std::shared_ptr<Dataset> test_ds_; 7890- std::shared_ptr<AccuracyMetrics> acc_metrics_; 7891- 7892- std::string ms_file_ = ""; 7893- std::string data_dir_ = ""; 7894- unsigned int epochs_ = 10; 7895- bool verbose_ = false; 7896- bool enable_fp16_ = false; 7897- int virtual_batch_ = -1; 7898- int save_checkpoint_ = 0; 7899- int batch_size_ = 32; 7900- int h_ = 32; 7901- int w_ = 32; 7902- mindspore::lite::NetTrainFlags *flags_{nullptr}; 7903- mindspore::MSKernelCallBack after_call_back_; 7904-}; 7905- 7906-#endif // MINDSPORE_LITE_TOOLS_BENCHMARK_TRAIN_NET_RUNNER_H_ 7907diff --git a/mindspore/lite/tools/benchmark_train/net_train.cc b/mindspore/lite/tools/benchmark_train/net_train.cc 7908index 514bba53..dd7b22a9 100644 7909--- a/mindspore/lite/tools/benchmark_train/net_train.cc 7910+++ b/mindspore/lite/tools/benchmark_train/net_train.cc 7911@@ -24,7 +24,6 @@ 7912 #ifdef ENABLE_NEON 7913 #include <arm_neon.h> 7914 #endif 7915-#include "tools/benchmark_train/net_runner.h" 7916 #include "src/common/common.h" 7917 #include "include/api/serialization.h" 7918 #include "securec/include/securec.h" 7919-- 79202.25.1 7921 7922