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.

base64.h 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 GE_COMMON_BASE64_H_
  17. #define GE_COMMON_BASE64_H_
  18. #include <algorithm>
  19. #include <string>
  20. #include "debug/ge_log.h"
  21. #include "ge_error_codes.h"
  22. namespace ge {
  23. namespace {
  24. const char *kBase64Chars =
  25. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  26. "abcdefghijklmnopqrstuvwxyz"
  27. "0123456789+/";
  28. const char kEqualSymbol = '=';
  29. const size_t kBase64CharsNum = 64;
  30. const size_t kThreeByteOneGroup = 3;
  31. const size_t kFourByteOneGroup = 4;
  32. } // namespace
  33. namespace base64 {
  34. static inline bool IsBase64Char(const char &c) { return (isalnum(c) || (c == '+') || (c == '/')); }
  35. static std::string EncodeToBase64(const std::string &raw_data) {
  36. size_t encode_length = raw_data.size() / kThreeByteOneGroup * kFourByteOneGroup;
  37. encode_length += raw_data.size() % kThreeByteOneGroup == 0 ? 0 : kFourByteOneGroup;
  38. size_t raw_data_index = 0;
  39. size_t encode_data_index = 0;
  40. std::string encode_data;
  41. encode_data.resize(encode_length);
  42. for (; raw_data_index + kThreeByteOneGroup <= raw_data.size(); raw_data_index += kThreeByteOneGroup) {
  43. auto char_1 = static_cast<uint8_t>(raw_data[raw_data_index]);
  44. auto char_2 = static_cast<uint8_t>(raw_data[raw_data_index + 1]);
  45. auto char_3 = static_cast<uint8_t>(raw_data[raw_data_index + 2]);
  46. encode_data[encode_data_index++] = kBase64Chars[char_1 >> 2u];
  47. encode_data[encode_data_index++] = kBase64Chars[((char_1 << 4u) & 0x30) | (char_2 >> 4u)];
  48. encode_data[encode_data_index++] = kBase64Chars[((char_2 << 2u) & 0x3c) | (char_3 >> 6u)];
  49. encode_data[encode_data_index++] = kBase64Chars[char_3 & 0x3f];
  50. }
  51. if (raw_data_index < raw_data.size()) {
  52. auto tail = raw_data.size() - raw_data_index;
  53. auto char_1 = static_cast<uint8_t>(raw_data[raw_data_index]);
  54. if (tail == 1) {
  55. encode_data[encode_data_index++] = kBase64Chars[char_1 >> 2u];
  56. encode_data[encode_data_index++] = kBase64Chars[(char_1 << 4u) & 0x30];
  57. encode_data[encode_data_index++] = kEqualSymbol;
  58. encode_data[encode_data_index++] = kEqualSymbol;
  59. } else {
  60. auto char_2 = static_cast<uint8_t>(raw_data[raw_data_index + 1]);
  61. encode_data[encode_data_index++] = kBase64Chars[char_1 >> 2u];
  62. encode_data[encode_data_index++] = kBase64Chars[((char_1 << 4u) & 0x30) | (char_2 >> 4u)];
  63. encode_data[encode_data_index++] = kBase64Chars[(char_2 << 2u) & 0x3c];
  64. encode_data[encode_data_index++] = kEqualSymbol;
  65. }
  66. }
  67. return encode_data;
  68. }
  69. #pragma GCC diagnostic push
  70. #pragma GCC diagnostic ignored "-Wunused-function"
  71. static Status DecodeFromBase64(const std::string &base64_data, std::string &decode_data) {
  72. if (base64_data.size() % kFourByteOneGroup != 0) {
  73. GELOGE(PARAM_INVALID, "base64 data size must can be divided by 4, but given data size is %zu", base64_data.size());
  74. return PARAM_INVALID;
  75. }
  76. decode_data.clear();
  77. size_t base64_data_len = base64_data.size();
  78. uint8_t byte_4[kFourByteOneGroup];
  79. auto FindCharInBase64Chars = [&](const char &raw_char) -> uint8_t {
  80. auto char_pos = std::find(kBase64Chars, kBase64Chars + kBase64CharsNum, raw_char);
  81. return static_cast<uint8_t>(std::distance(kBase64Chars, char_pos)) & 0xff;
  82. };
  83. for (std::size_t input_data_index = 0; input_data_index < base64_data_len; input_data_index += 4) {
  84. for (size_t i = 0; i < kFourByteOneGroup; ++i) {
  85. if (base64_data[input_data_index + i] == kEqualSymbol && input_data_index >= base64_data_len - 4 && i > 1) {
  86. byte_4[i] = kBase64CharsNum;
  87. } else if (IsBase64Char(base64_data[input_data_index + i])) {
  88. byte_4[i] = FindCharInBase64Chars(base64_data[input_data_index + i]);
  89. } else {
  90. GELOGE(PARAM_INVALID, "given base64 data is illegal");
  91. return PARAM_INVALID;
  92. }
  93. }
  94. decode_data += static_cast<char>((byte_4[0] << 2u) + ((byte_4[1] & 0x30) >> 4u));
  95. if (byte_4[2] >= kBase64CharsNum) {
  96. break;
  97. } else if (byte_4[3] >= kBase64CharsNum) {
  98. decode_data += static_cast<char>(((byte_4[1] & 0x0f) << 4u) + ((byte_4[2] & 0x3c) >> 2u));
  99. break;
  100. }
  101. decode_data += static_cast<char>(((byte_4[1] & 0x0f) << 4u) + ((byte_4[2] & 0x3c) >> 2u));
  102. decode_data += static_cast<char>(((byte_4[2] & 0x03) << 6u) + byte_4[3]);
  103. }
  104. return SUCCESS;
  105. }
  106. #pragma GCC diagnostic pop
  107. } // namespace base64
  108. } // namespace ge
  109. #endif // GE_COMMON_BASE64_H_

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