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

coder.cc 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * Copyright 2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "coder/coder.h"
  17. #include <getopt.h>
  18. #include <iomanip>
  19. #include <string>
  20. #include <vector>
  21. #include <map>
  22. #include "schema/inner/model_generated.h"
  23. #include "tools/common/flag_parser.h"
  24. #include "coder/session.h"
  25. #include "coder/context.h"
  26. #include "utils/dir_utils.h"
  27. #include "securec/include/securec.h"
  28. #include "src/common/file_utils.h"
  29. #include "src/common/utils.h"
  30. #include "coder/coder_config.h"
  31. namespace mindspore::lite::micro {
  32. class CoderFlags : public virtual FlagParser {
  33. public:
  34. CoderFlags() {
  35. AddFlag(&CoderFlags::is_weight_file_, "isWeightFile", "whether generating weight .net file, true| false", false);
  36. AddFlag(&CoderFlags::model_path_, "modelPath", "Input model path", "");
  37. AddFlag(&CoderFlags::code_path_, "codePath", "Input code path", ".");
  38. AddFlag(&CoderFlags::code_module_name_, "moduleName", "Input code module name", "");
  39. AddFlag(&CoderFlags::target_, "target", "generateed code target, x86| ARM32M| ARM32A| ARM64", "x86");
  40. AddFlag(&CoderFlags::code_mode_, "codeMode", "generated code mode, Normal | Android ", "Normal");
  41. AddFlag(&CoderFlags::debug_mode_, "debugMode", "dump perlayer's time cost and tensor, true | false", false);
  42. }
  43. ~CoderFlags() override = default;
  44. public:
  45. std::string model_path_;
  46. bool is_weight_file_{false};
  47. std::string code_module_name_;
  48. std::string code_path_;
  49. std::string code_mode_;
  50. bool debug_mode_{false};
  51. std::string target_;
  52. };
  53. int Coder::Run(const std::string &model_path) {
  54. session_ = CreateCoderSession();
  55. if (session_ == nullptr) {
  56. MS_LOG(ERROR) << "new session failed while running";
  57. return RET_ERROR;
  58. }
  59. STATUS status = session_->Init(model_path);
  60. if (status != RET_OK) {
  61. MS_LOG(ERROR) << "Init session failed.";
  62. return RET_ERROR;
  63. }
  64. status = session_->Build();
  65. if (status != RET_OK) {
  66. MS_LOG(ERROR) << "Set Input resize shapes error";
  67. return status;
  68. }
  69. status = session_->Run();
  70. if (status != RET_OK) {
  71. MS_LOG(ERROR) << "Generate Code Files error. " << status;
  72. return status;
  73. }
  74. status = session_->GenerateCode();
  75. if (status != RET_OK) {
  76. MS_LOG(ERROR) << "Generate Code Files error " << status;
  77. }
  78. return status;
  79. }
  80. int Coder::Init(const CoderFlags &flags) const {
  81. static const std::map<std::string, Target> kTargetMap = {
  82. {"x86", kX86}, {"ARM32M", kARM32M}, {"ARM32A", kARM32A}, {"ARM64", kARM64}, {"All", kAllTargets}};
  83. static const std::map<std::string, CodeMode> kCodeModeMap = {{"Normal", Code_Normal}, {"Android", Code_Android}};
  84. Configurator *config = Configurator::GetInstance();
  85. std::vector<std::function<bool()>> parsers;
  86. parsers.emplace_back([flags, config]() -> bool {
  87. config->set_is_weight_file(flags.is_weight_file_);
  88. return true;
  89. });
  90. parsers.emplace_back([&flags, config]() -> bool {
  91. auto target_item = kTargetMap.find(flags.target_);
  92. MS_CHECK_TRUE_RET_BOOL(target_item != kTargetMap.end(), "unsupported target: " + flags.target_);
  93. config->set_target(target_item->second);
  94. return true;
  95. });
  96. parsers.emplace_back([&flags, config]() -> bool {
  97. auto code_item = kCodeModeMap.find(flags.code_mode_);
  98. MS_CHECK_TRUE_RET_BOOL(code_item != kCodeModeMap.end(), "unsupported code mode: " + flags.code_mode_);
  99. config->set_code_mode(code_item->second);
  100. return true;
  101. });
  102. parsers.emplace_back([&flags, config]() -> bool {
  103. config->set_debug_mode(flags.debug_mode_);
  104. return true;
  105. });
  106. parsers.emplace_back([&flags, config]() -> bool {
  107. if (!FileExists(flags.model_path_)) {
  108. MS_LOG(ERROR) << "code_gen model_path " << flags.model_path_ << " is not valid";
  109. return false;
  110. }
  111. if (flags.code_module_name_.empty() || isdigit(flags.code_module_name_.at(0))) {
  112. MS_LOG(ERROR) << "code_gen code module name " << flags.code_module_name_
  113. << " not valid: it must be given and the first char could not be number";
  114. return false;
  115. }
  116. config->set_module_name(flags.code_module_name_);
  117. return true;
  118. });
  119. parsers.emplace_back([&flags, config]() -> bool {
  120. const std::string slash = std::string(kSlash);
  121. if (!flags.code_path_.empty() && !DirExists(flags.code_path_)) {
  122. MS_LOG(ERROR) << "code_gen code path " << flags.code_path_ << " is not valid";
  123. return false;
  124. }
  125. config->set_code_path(flags.code_path_);
  126. if (flags.code_path_.empty()) {
  127. std::string path = ".." + slash + config->module_name();
  128. config->set_code_path(path);
  129. } else {
  130. if (flags.code_path_.substr(flags.code_path_.size() - 1, 1) != slash) {
  131. std::string path = flags.code_path_ + slash + config->module_name();
  132. config->set_code_path(path);
  133. } else {
  134. std::string path = flags.code_path_ + config->module_name();
  135. config->set_code_path(path);
  136. }
  137. }
  138. return InitProjDirs(flags.code_path_, config->module_name()) != RET_ERROR;
  139. });
  140. if (!std::all_of(parsers.begin(), parsers.end(), [](auto &parser) -> bool { return parser(); })) {
  141. if (!flags.help) {
  142. std::cerr << flags.Usage() << std::endl;
  143. return 0;
  144. }
  145. return RET_ERROR;
  146. }
  147. auto print_parameter = [](auto name, auto value) {
  148. MS_LOG(INFO) << std::setw(20) << std::left << name << "= " << value;
  149. };
  150. print_parameter("modelPath", flags.model_path_);
  151. print_parameter("target", config->target());
  152. print_parameter("codePath", config->code_path());
  153. print_parameter("codeMode", config->code_mode());
  154. print_parameter("codeModuleName", config->module_name());
  155. print_parameter("isWeightFile", config->is_weight_file());
  156. print_parameter("debugMode", config->debug_mode());
  157. return RET_OK;
  158. }
  159. int RunCoder(int argc, const char **argv) {
  160. CoderFlags flags;
  161. Option<std::string> err = flags.ParseFlags(argc, argv, false, false);
  162. if (err.IsSome()) {
  163. std::cerr << err.Get() << std::endl;
  164. std::cerr << flags.Usage() << std::endl;
  165. return RET_ERROR;
  166. }
  167. if (flags.help) {
  168. std::cerr << flags.Usage() << std::endl;
  169. return RET_OK;
  170. }
  171. Coder code_gen;
  172. STATUS status = code_gen.Init(flags);
  173. if (status != RET_OK) {
  174. MS_LOG(ERROR) << "Coder init Error : " << status;
  175. return status;
  176. }
  177. status = code_gen.Run(flags.model_path_);
  178. if (status != RET_OK) {
  179. MS_LOG(ERROR) << "Run Coder Error : " << status;
  180. return status;
  181. }
  182. MS_LOG(INFO) << "end of Coder";
  183. return RET_OK;
  184. }
  185. } // namespace mindspore::lite::micro