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.

option_parser.cc 6.8 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * Copyright 2020 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 "core/util/option_parser.h"
  17. #include <vector>
  18. #include <string>
  19. #include <cstring>
  20. #include <iostream>
  21. #include <iomanip>
  22. #include "mindspore/ccsrc/utils/log_adapter.h"
  23. namespace mindspore {
  24. namespace serving {
  25. bool StartWith(const std::string &str, const std::string &expected) {
  26. return expected.empty() ||
  27. (str.size() >= expected.size() && memcmp(str.data(), expected.data(), expected.size()) == 0);
  28. }
  29. bool RemovePrefix(std::string *str, const std::string &prefix) {
  30. if (!StartWith(*str, prefix)) return false;
  31. str->replace(str->begin(), str->begin() + prefix.size(), "");
  32. return true;
  33. }
  34. bool Option::ParseInt32(std::string *arg) {
  35. if (RemovePrefix(arg, "--") && RemovePrefix(arg, name_) && RemovePrefix(arg, "=")) {
  36. char extra;
  37. int32_t parsed_value;
  38. if (sscanf(arg->data(), "%d%c", &parsed_value, &extra) != 1) {
  39. std::cout << "Parse " << name_ << "Error for option " << *arg << std::endl;
  40. return false;
  41. } else {
  42. *int32_default_ = parsed_value;
  43. }
  44. return true;
  45. }
  46. return false;
  47. }
  48. bool Option::ParseBool(std::string *arg) {
  49. if (RemovePrefix(arg, "--") && RemovePrefix(arg, name_) && RemovePrefix(arg, "=")) {
  50. if (*arg == "true") {
  51. *bool_default_ = true;
  52. } else if (*arg == "false") {
  53. *bool_default_ = false;
  54. } else {
  55. std::cout << "Parse " << name_ << " Error for option " << *arg << std::endl;
  56. return false;
  57. }
  58. return true;
  59. }
  60. return false;
  61. }
  62. bool Option::ParseString(std::string *arg) {
  63. if (RemovePrefix(arg, "--") && RemovePrefix(arg, name_) && RemovePrefix(arg, "=")) {
  64. *string_default_ = *arg;
  65. return true;
  66. }
  67. return false;
  68. }
  69. bool Option::ParseFloat(std::string *arg) {
  70. if (RemovePrefix(arg, "--") && RemovePrefix(arg, name_) && RemovePrefix(arg, "=")) {
  71. char extra;
  72. float parsed_value;
  73. if (sscanf(arg->data(), "%f%c", &parsed_value, &extra) != 1) {
  74. std::cout << "Parse " << name_ << "Error for option " << *arg << std::endl;
  75. return false;
  76. } else {
  77. *float_default_ = parsed_value;
  78. }
  79. return true;
  80. }
  81. return false;
  82. }
  83. Option::Option(const std::string &name, int32_t *default_point, const std::string &usage)
  84. : name_(name),
  85. type_(MS_TYPE_INT32),
  86. int32_default_(default_point),
  87. bool_default_(nullptr),
  88. string_default_(nullptr),
  89. float_default_(nullptr),
  90. usage_(usage) {}
  91. Option::Option(const std::string &name, bool *default_point, const std::string &usage)
  92. : name_(name),
  93. type_(MS_TYPE_BOOL),
  94. int32_default_(nullptr),
  95. bool_default_(default_point),
  96. string_default_(nullptr),
  97. float_default_(nullptr),
  98. usage_(usage) {}
  99. Option::Option(const std::string &name, std::string *default_point, const std::string &usage)
  100. : name_(name),
  101. type_(MS_TYPE_STRING),
  102. int32_default_(nullptr),
  103. bool_default_(nullptr),
  104. string_default_(default_point),
  105. float_default_(nullptr),
  106. usage_(usage) {}
  107. Option::Option(const std::string &name, float *default_point, const std::string &usage)
  108. : name_(name),
  109. type_(MS_TYPE_FLOAT),
  110. int32_default_(nullptr),
  111. bool_default_(nullptr),
  112. string_default_(nullptr),
  113. float_default_(default_point),
  114. usage_(usage) {}
  115. bool Option::Parse(std::string *arg) {
  116. bool result = false;
  117. switch (type_) {
  118. case MS_TYPE_BOOL:
  119. result = ParseBool(arg);
  120. break;
  121. case MS_TYPE_FLOAT:
  122. result = ParseFloat(arg);
  123. break;
  124. case MS_TYPE_INT32:
  125. result = ParseInt32(arg);
  126. break;
  127. case MS_TYPE_STRING:
  128. result = ParseString(arg);
  129. break;
  130. default:
  131. break;
  132. }
  133. return result;
  134. }
  135. std::shared_ptr<Options> Options::inst_ = nullptr;
  136. Options &Options::Instance() {
  137. static Options instance;
  138. return instance;
  139. }
  140. Options::Options() : args_(nullptr) { CreateOptions(); }
  141. void Options::CreateOptions() {
  142. args_ = std::make_shared<Arguments>();
  143. std::vector<Option> options = {
  144. Option("port", &args_->grpc_port, "Port to listen on for gRPC API, default is 5500"),
  145. Option("model_name", &args_->model_name, "model name "),
  146. Option("model_path", &args_->model_path, "the path of the model files"),
  147. Option("device_id", &args_->device_id, "the device id, default is 0"),
  148. };
  149. options_ = options;
  150. }
  151. bool Options::CheckOptions() {
  152. if (args_->model_name == "" || args_->model_path == "") {
  153. std::cout << "model_path and model_name should not be null" << std::endl;
  154. return false;
  155. }
  156. if (args_->device_type != "Ascend") {
  157. std::cout << "device_type only support Ascend right now" << std::endl;
  158. return false;
  159. }
  160. return true;
  161. }
  162. bool Options::ParseCommandLine(int argc, char **argv) {
  163. if (argc < 2 || (strcmp(argv[1], "--help") == 0)) {
  164. Usage();
  165. return false;
  166. }
  167. std::vector<std::string> unkown_options;
  168. for (int i = 1; i < argc; ++i) {
  169. bool found = false;
  170. for (auto &option : options_) {
  171. std::string arg = argv[i];
  172. if (option.Parse(&arg)) {
  173. found = true;
  174. break;
  175. }
  176. }
  177. if (found == false) {
  178. unkown_options.push_back(argv[i]);
  179. }
  180. }
  181. if (!unkown_options.empty()) {
  182. std::cout << "unkown options:" << std::endl;
  183. for (const auto &option : unkown_options) {
  184. std::cout << option << std::endl;
  185. }
  186. }
  187. bool valid = (unkown_options.empty() && CheckOptions());
  188. if (!valid) {
  189. Usage();
  190. }
  191. return valid;
  192. }
  193. void Options::Usage() {
  194. std::cout << "USAGE: mindspore-serving [options]" << std::endl;
  195. for (const auto &option : options_) {
  196. std::string type;
  197. switch (option.type_) {
  198. case Option::MS_TYPE_BOOL:
  199. type = "bool";
  200. break;
  201. case Option::MS_TYPE_FLOAT:
  202. type = "float";
  203. break;
  204. case Option::MS_TYPE_INT32:
  205. type = "int32";
  206. break;
  207. case Option::MS_TYPE_STRING:
  208. type = "string";
  209. break;
  210. default:
  211. break;
  212. }
  213. std::cout << "--" << std::setw(30) << std::left << option.name_ << std::setw(10) << std::left << type
  214. << option.usage_ << std::endl;
  215. }
  216. }
  217. } // namespace serving
  218. } // namespace mindspore