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.0 kB

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

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