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.

log.cc 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <iostream>
  17. #include <string.h>
  18. #include <stdarg.h>
  19. #include "easy_graph/infra/log.h"
  20. EG_NS_BEGIN
  21. namespace {
  22. struct ConsoleState {
  23. bool isColorful() const {
  24. return colorful;
  25. }
  26. protected:
  27. ConsoleState(bool c) : colorful(c) {}
  28. private:
  29. bool colorful;
  30. };
  31. ////////////////////////////////////////////////////////
  32. #define DEF_COLOR_STATE(STATE, COLOR) \
  33. struct STATE : ConsoleState { \
  34. STATE(bool colorful) : ConsoleState(colorful) {} \
  35. }; \
  36. std::ostream &operator<<(std::ostream &os, const STATE &state) { \
  37. if (state.isColorful()) \
  38. os << COLOR; \
  39. return os; \
  40. }
  41. ////////////////////////////////////////////////////////
  42. #define __RED "\033[1;31m"
  43. #define __GREEN "\033[1;32m"
  44. #define __YELLOW "\033[1;33m"
  45. #define __BLUE "\033[1;34m"
  46. #define __MAGENTA "\033[1;35m"
  47. #define __CYAN "\033[1;36m"
  48. #define __WHITE "\033[0m"
  49. DEF_COLOR_STATE(DebugState, __BLUE)
  50. DEF_COLOR_STATE(InfoState, __CYAN)
  51. DEF_COLOR_STATE(NormalState, __WHITE)
  52. DEF_COLOR_STATE(SuccState, __GREEN)
  53. DEF_COLOR_STATE(WarnState, __YELLOW)
  54. DEF_COLOR_STATE(FailState, __RED)
  55. ///////////////////////////////////////////////////////
  56. struct StdoutListener;
  57. StdoutListener *inst = nullptr;
  58. struct StdoutListener {
  59. static StdoutListener &getInstance() {
  60. if (inst == 0) {
  61. inst = new StdoutListener(true);
  62. }
  63. return *inst;
  64. }
  65. void println(const uint8_t level, const char *msg) {
  66. #define ON_LEVEL(level, state) \
  67. case level: \
  68. doPrint(state, msg); \
  69. break;
  70. switch (level) {
  71. ON_LEVEL(EG_FATAL_LEVEL, fail)
  72. ON_LEVEL(EG_ERR_LEVEL, fail)
  73. ON_LEVEL(EG_WARN_LEVEL, warn)
  74. ON_LEVEL(EG_SUCC_LEVEL, succ)
  75. ON_LEVEL(EG_INFO_LEVEL, info)
  76. ON_LEVEL(EG_DEBUG_LEVEL, debug)
  77. default:
  78. doPrint(normal, msg);
  79. }
  80. }
  81. private:
  82. StdoutListener(bool colorful)
  83. : succ(colorful), fail(colorful), normal(colorful), info(colorful), warn(colorful), debug(colorful) {}
  84. template<typename STATE>
  85. void doPrint(const STATE &state, const char *msg) {
  86. std::cout << state << msg << normal << std::endl;
  87. }
  88. private:
  89. SuccState succ;
  90. FailState fail;
  91. NormalState normal;
  92. InfoState info;
  93. WarnState warn;
  94. DebugState debug;
  95. };
  96. const char *getBaseName(const char *absPath) {
  97. if (absPath == 0)
  98. return "";
  99. const char *p = absPath + strlen(absPath);
  100. while ((p != absPath) && (*(p - 1) != '/')) {
  101. p--;
  102. }
  103. return p;
  104. }
  105. } // namespace
  106. void eg_log(int level, const char *levelstr, const char *file, unsigned int line, const char *fmt, ...) {
  107. const int FMT_BUFF_SIZE = 1024;
  108. char fmt_buff[FMT_BUFF_SIZE] = {0};
  109. va_list valist;
  110. va_start(valist, fmt);
  111. vsnprintf(fmt_buff, FMT_BUFF_SIZE, fmt, valist);
  112. va_end(valist);
  113. char buff[1280] = {0};
  114. sprintf(buff, "[%s]: %s:%u: %s", levelstr, getBaseName(file), line, fmt_buff);
  115. StdoutListener::getInstance().println(level, buff);
  116. }
  117. EG_NS_END

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