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 5.5 kB

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

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