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

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

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