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

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

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