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.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <iomanip>
  18. #include <string>
  19. #include <vector>
  20. #include <map>
  21. #include "schema/inner/model_generated.h"
  22. #include "tools/common/flag_parser.h"
  23. #include "coder/session.h"
  24. #include "coder/context.h"
  25. #include "utils/dir_utils.h"
  26. #include "securec/include/securec.h"
  27. #include "src/common/file_utils.h"
  28. #include "src/common/utils.h"
  29. #include "coder/config.h"
  30. #include "coder/generator/component/component.h"
  31. namespace mindspore::lite::micro {
  32. class CoderFlags : public virtual FlagParser {
  33. public:
  34. CoderFlags() {
  35. AddFlag(&CoderFlags::model_path_, "modelPath", "Input model path", "");
  36. AddFlag(&CoderFlags::code_path_, "codePath", "Input code path", ".");
  37. AddFlag(&CoderFlags::target_, "target", "generated code target, x86| ARM32M| ARM32A| ARM64", "x86");
  38. AddFlag(&CoderFlags::code_mode_, "codeMode", "generated code mode, Inference | Train", "Inference");
  39. AddFlag(&CoderFlags::support_parallel_, "supportParallel", "whether support parallel launch, true | false", false);
  40. AddFlag(&CoderFlags::debug_mode_, "debugMode", "dump the tensors data for debugging, true | false", false);
  41. }
  42. ~CoderFlags() override = default;
  43. std::string model_path_;
  44. bool support_parallel_{false};
  45. std::string code_path_;
  46. std::string code_mode_;
  47. bool debug_mode_{false};
  48. std::string target_;
  49. };
  50. int Coder::Run(const std::string &model_path) {
  51. session_ = CreateCoderSession();
  52. if (session_ == nullptr) {
  53. MS_LOG(ERROR) << "new session failed while running!";
  54. return RET_ERROR;
  55. }
  56. STATUS status = session_->Init(model_path);
  57. if (status != RET_OK) {
  58. MS_LOG(ERROR) << "Init session failed!";
  59. return RET_ERROR;
  60. }
  61. status = session_->Build();
  62. if (status != RET_OK) {
  63. MS_LOG(ERROR) << "Compile graph failed!";
  64. return status;
  65. }
  66. status = session_->Run();
  67. if (status != RET_OK) {
  68. MS_LOG(ERROR) << "Generate Code Files error!" << status;
  69. return status;
  70. }
  71. status = session_->GenerateCode();
  72. if (status != RET_OK) {
  73. MS_LOG(ERROR) << "Generate Code Files error!" << status;
  74. }
  75. return status;
  76. }
  77. int Configurator::ParseProjDir(std::string model_path) {
  78. // split model_path to get model file name
  79. proj_dir_ = model_path;
  80. size_t found = proj_dir_.find_last_of("/\\");
  81. if (found != std::string::npos) {
  82. proj_dir_ = proj_dir_.substr(found + 1);
  83. }
  84. found = proj_dir_.find(".ms");
  85. if (found != std::string::npos) {
  86. proj_dir_ = proj_dir_.substr(0, found);
  87. } else {
  88. MS_LOG(ERROR) << "model file's name must be end with \".ms\".";
  89. return RET_ERROR;
  90. }
  91. if (proj_dir_.size() == 0) {
  92. proj_dir_ = "net";
  93. MS_LOG(WARNING) << "parse model's name failed, use \"net\" instead.";
  94. }
  95. return RET_OK;
  96. }
  97. int Coder::Init(const CoderFlags &flags) const {
  98. static const std::map<std::string, Target> kTargetMap = {
  99. {"x86", kX86}, {"ARM32M", kARM32M}, {"ARM32A", kARM32A}, {"ARM64", kARM64}, {"All", kAllTargets}};
  100. static const std::map<std::string, CodeMode> kCodeModeMap = {{"Inference", Inference}, {"Train", Train}};
  101. Configurator *config = Configurator::GetInstance();
  102. std::vector<std::function<bool()>> parsers;
  103. parsers.emplace_back([&flags, config]() -> bool {
  104. if (!FileExists(flags.model_path_)) {
  105. MS_LOG(ERROR) << "model_path \"" << flags.model_path_ << "\" is not valid";
  106. return false;
  107. }
  108. if (config->ParseProjDir(flags.model_path_) != RET_OK) {
  109. return false;
  110. }
  111. return true;
  112. });
  113. parsers.emplace_back([&flags, config]() -> bool {
  114. auto target_item = kTargetMap.find(flags.target_);
  115. MS_CHECK_TRUE_RET_BOOL(target_item != kTargetMap.end(), "unsupported target: " + flags.target_);
  116. config->set_target(target_item->second);
  117. return true;
  118. });
  119. parsers.emplace_back([&flags, config]() -> bool {
  120. auto code_item = kCodeModeMap.find(flags.code_mode_);
  121. MS_CHECK_TRUE_RET_BOOL(code_item != kCodeModeMap.end(), "unsupported code mode: " + flags.code_mode_);
  122. config->set_code_mode(code_item->second);
  123. return true;
  124. });
  125. parsers.emplace_back([&flags, config]() -> bool {
  126. if (flags.support_parallel_ && config->target() == kARM32M) {
  127. MS_LOG(ERROR) << "arm32M cannot support parallel.";
  128. return false;
  129. }
  130. config->set_support_parallel(flags.support_parallel_);
  131. return true;
  132. });
  133. parsers.emplace_back([&flags, config]() -> bool {
  134. config->set_debug_mode(flags.debug_mode_);
  135. return true;
  136. });
  137. parsers.emplace_back([&flags, config]() -> bool {
  138. const std::string slash = std::string(kSlash);
  139. if (!flags.code_path_.empty() && !DirExists(flags.code_path_)) {
  140. MS_LOG(ERROR) << "code_gen code path " << flags.code_path_ << " is not valid";
  141. return false;
  142. }
  143. config->set_code_path(flags.code_path_);
  144. if (flags.code_path_.empty()) {
  145. std::string path = ".." + slash + config->proj_dir();
  146. config->set_code_path(path);
  147. } else {
  148. if (flags.code_path_.substr(flags.code_path_.size() - 1, 1) != slash) {
  149. std::string path = flags.code_path_ + slash + config->proj_dir();
  150. config->set_code_path(path);
  151. } else {
  152. std::string path = flags.code_path_ + config->proj_dir();
  153. config->set_code_path(path);
  154. }
  155. }
  156. return InitProjDirs(flags.code_path_, config->proj_dir()) != RET_ERROR;
  157. });
  158. if (!std::all_of(parsers.begin(), parsers.end(), [](auto &parser) -> bool { return parser(); })) {
  159. if (!flags.help) {
  160. std::cerr << flags.Usage() << std::endl;
  161. return 0;
  162. }
  163. return RET_ERROR;
  164. }
  165. auto print_parameter = [](auto name, auto value) {
  166. MS_LOG(INFO) << std::setw(20) << std::left << name << "= " << value;
  167. };
  168. print_parameter("modelPath", flags.model_path_);
  169. print_parameter("projectName", config->proj_dir());
  170. print_parameter("target", config->target());
  171. print_parameter("codePath", config->code_path());
  172. print_parameter("codeMode", config->code_mode());
  173. print_parameter("debugMode", config->debug_mode());
  174. return RET_OK;
  175. }
  176. int RunCoder(int argc, const char **argv) {
  177. CoderFlags flags;
  178. Option<std::string> err = flags.ParseFlags(argc, argv, false, false);
  179. if (err.IsSome()) {
  180. std::cerr << err.Get() << std::endl;
  181. std::cerr << flags.Usage() << std::endl;
  182. return RET_ERROR;
  183. }
  184. if (flags.help) {
  185. std::cerr << flags.Usage() << std::endl;
  186. return RET_OK;
  187. }
  188. Coder code_gen;
  189. STATUS status = code_gen.Init(flags);
  190. if (status != RET_OK) {
  191. MS_LOG(ERROR) << "Coder init Error";
  192. return status;
  193. }
  194. status = code_gen.Run(flags.model_path_);
  195. if (status != RET_OK) {
  196. MS_LOG(ERROR) << "Coder Run Error.";
  197. return status;
  198. }
  199. MS_LOG(INFO) << "end of Coder";
  200. return RET_OK;
  201. }
  202. } // namespace mindspore::lite::micro