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.

string_util.h 4.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Copyright 2019-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. #ifndef INC_FRAMEWORK_COMMON_STRING_UTIL_H_
  17. #define INC_FRAMEWORK_COMMON_STRING_UTIL_H_
  18. #include <securec.h>
  19. #include <algorithm>
  20. #include <functional>
  21. #include <sstream>
  22. #include <string>
  23. #include <vector>
  24. namespace ge {
  25. class StringUtils {
  26. public:
  27. static std::string &Ltrim(std::string &s) {
  28. #if __cplusplus >= 201103L
  29. (void)s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) { return !std::isspace(c); }));
  30. #else
  31. (void)s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
  32. #endif
  33. return s;
  34. }
  35. static std::string &Rtrim(std::string &s) {
  36. #if __cplusplus >= 201103L
  37. (void)s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) { return !std::isspace(c); }));
  38. #else
  39. (void)s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
  40. #endif
  41. return s;
  42. }
  43. ///
  44. /// @ingroup domi_common
  45. /// @brief delete spaces at the beginning and end of a string
  46. /// @param [in] string to be trimmed
  47. /// @return string after trim
  48. ///
  49. static std::string &Trim(std::string &s) { return Ltrim(Rtrim(s)); }
  50. ///
  51. /// @ingroup domi_common
  52. /// @brief string splitting
  53. /// @param [in] str string to be trimmed
  54. /// @param [in] delim separator
  55. /// @return string array after segmentation
  56. ///
  57. static std::vector<std::string> Split(const std::string &str, char delim) {
  58. std::vector<std::string> elems;
  59. if (str.empty()) {
  60. elems.emplace_back("");
  61. return elems;
  62. }
  63. std::stringstream ss(str);
  64. std::string item;
  65. while (getline(ss, item, delim)) {
  66. elems.push_back(item);
  67. }
  68. auto str_size = str.size();
  69. if (str_size > 0 && str[str_size - 1] == delim) {
  70. elems.emplace_back("");
  71. }
  72. return elems;
  73. }
  74. ///
  75. /// @ingroup domi_common
  76. /// @brief obtain the file name
  77. /// @param [in] s path name
  78. /// @return file name
  79. ///
  80. static std::string GetFileName(std::string &s) {
  81. if (s.empty()) {
  82. return "";
  83. }
  84. std::vector<std::string> files = StringUtils::Split(s, '/');
  85. return files.empty() ? "" : files[files.size() - 1];
  86. }
  87. ///
  88. /// @ingroup domi_common
  89. /// @brief full replacement
  90. /// @link
  91. /// @param [in] str str string to be replaced
  92. /// @param [in] old_value old Characters Before Replacement
  93. /// @param [in] new_value new Characters Before Replacement
  94. /// @return string after replacement
  95. ///
  96. static std::string ReplaceAll(std::string str, const std::string &old_value, const std::string &new_value) {
  97. std::string::size_type cur_pos = 0;
  98. std::string::size_type old_length = old_value.length();
  99. std::string::size_type new_length = new_value.length();
  100. // cycle replace
  101. for (; cur_pos != std::string::npos; cur_pos += new_length) {
  102. if ((cur_pos = str.find(old_value, cur_pos)) != std::string::npos) {
  103. (void)str.replace(cur_pos, old_length, new_value);
  104. } else {
  105. break;
  106. }
  107. }
  108. return str;
  109. }
  110. ///
  111. /// @ingroup domi_common
  112. /// @brief checks whether a character string starts with a character string (prefix)
  113. /// @link
  114. /// @param [in] str string to be compared
  115. /// @param [in] str_x prefix
  116. /// @return if the value is a prefix, true is returned. Otherwise, false is returned
  117. ///
  118. static bool StartWith(const std::string &str, const std::string str_x) {
  119. return ((str.size() >= str_x.size()) && (str.compare(0, str_x.size(), str_x) == 0));
  120. }
  121. ///
  122. /// @ingroup domi_common
  123. /// @brief format string
  124. /// @link
  125. /// @param [in] format specifies the character string format
  126. /// @param [in] ... format Filling Content
  127. /// @return formatted string
  128. ///
  129. static std::string FormatString(const char *format, ...) {
  130. const uint32_t MAX_BUFFER_LEN = 1024; // the stack memory plint check result must be less than 1024
  131. va_list args;
  132. va_start(args, format);
  133. char buffer[MAX_BUFFER_LEN] = {0};
  134. int32_t ret = vsnprintf_s(buffer, MAX_BUFFER_LEN, MAX_BUFFER_LEN - 1, format, args);
  135. va_end(args);
  136. return ret > 0 ? buffer : "";
  137. }
  138. };
  139. } // namespace ge
  140. #endif // INC_FRAMEWORK_COMMON_STRING_UTIL_H_

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示