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.

pb2json.cc 8.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // File: pb2json.h
  17. // Description: This imply file for protobuf message and json interconversion
  18. #include "common/convert/pb2json.h"
  19. #include <set>
  20. #include <string>
  21. #include "securec.h"
  22. #include "framework/common/fmk_types.h"
  23. #include "framework/common/debug/ge_log.h"
  24. using std::set;
  25. using std::string;
  26. namespace ge {
  27. namespace {
  28. const int kSignificantDigits = 10;
  29. }
  30. // JSON parses non utf8 character throwing exceptions, so some fields need to be shielded through black fields
  31. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void Pb2Json::Message2Json(const ProtobufMsg &message,
  32. const set<string> &black_fields, Json &json,
  33. bool enum2str) {
  34. auto descriptor = message.GetDescriptor();
  35. auto reflection = message.GetReflection();
  36. if (descriptor == nullptr || reflection == nullptr) {
  37. return;
  38. }
  39. auto count = descriptor->field_count();
  40. for (auto i = 0; i < count; ++i) {
  41. const auto field = descriptor->field(i);
  42. if (field == nullptr) {
  43. return;
  44. }
  45. // Do not display weight data
  46. if (black_fields.find(field->name()) != black_fields.end()) {
  47. continue;
  48. }
  49. if (field->is_repeated()) {
  50. if (reflection->FieldSize(message, field) > 0) {
  51. RepeatedMessage2Json(message, field, reflection, black_fields, json[field->name()], enum2str);
  52. }
  53. continue;
  54. }
  55. if (!reflection->HasField(message, field)) {
  56. continue;
  57. }
  58. OneField2Json(message, field, reflection, black_fields, json, enum2str);
  59. }
  60. }
  61. void Pb2Json::OneField2Json(const ProtobufMsg &message, const ProtobufFieldDescriptor *field,
  62. const ProtobufReflection *reflection, const set<string> &black_fields, Json &json,
  63. bool enum2str) {
  64. switch (field->type()) {
  65. case ProtobufFieldDescriptor::TYPE_MESSAGE: {
  66. const ProtobufMsg &tmp_message = reflection->GetMessage(message, field);
  67. if (0 != tmp_message.ByteSize()) {
  68. Message2Json(tmp_message, black_fields, json[field->name()], enum2str);
  69. }
  70. break;
  71. }
  72. case ProtobufFieldDescriptor::TYPE_BOOL:
  73. json[field->name()] = reflection->GetBool(message, field);
  74. break;
  75. case ProtobufFieldDescriptor::TYPE_ENUM: {
  76. auto *enum_value_desc = reflection->GetEnum(message, field);
  77. Enum2Json(enum_value_desc, field, enum2str, json);
  78. break;
  79. }
  80. case ProtobufFieldDescriptor::TYPE_INT32:
  81. case ProtobufFieldDescriptor::TYPE_SINT32:
  82. case ProtobufFieldDescriptor::TYPE_SFIXED32:
  83. json[field->name()] = reflection->GetInt32(message, field);
  84. break;
  85. case ProtobufFieldDescriptor::TYPE_UINT32:
  86. case ProtobufFieldDescriptor::TYPE_FIXED32:
  87. json[field->name()] = reflection->GetUInt32(message, field);
  88. break;
  89. case ProtobufFieldDescriptor::TYPE_INT64:
  90. case ProtobufFieldDescriptor::TYPE_SINT64:
  91. case ProtobufFieldDescriptor::TYPE_SFIXED64:
  92. json[field->name()] = reflection->GetInt64(message, field);
  93. break;
  94. case ProtobufFieldDescriptor::TYPE_UINT64:
  95. case ProtobufFieldDescriptor::TYPE_FIXED64:
  96. json[field->name()] = reflection->GetUInt64(message, field);
  97. break;
  98. case ProtobufFieldDescriptor::TYPE_FLOAT:
  99. char str[kSignificantDigits];
  100. if (sprintf_s(str, kSignificantDigits, "%g", reflection->GetFloat(message, field)) != -1) {
  101. json[field->name()] = str;
  102. } else {
  103. json[field->name()] = reflection->GetFloat(message, field);
  104. }
  105. break;
  106. case ProtobufFieldDescriptor::TYPE_STRING:
  107. json[field->name()] = reflection->GetString(message, field);
  108. break;
  109. case ProtobufFieldDescriptor::TYPE_BYTES: {
  110. string field_name = field->name();
  111. string type_bytes = reflection->GetString(message, field);
  112. json[field_name] = TypeBytes2String(field_name, type_bytes);
  113. break;
  114. }
  115. default:
  116. break;
  117. }
  118. }
  119. string Pb2Json::TypeBytes2String(string &field_name, string &type_bytes) {
  120. if (field_name != "offset") {
  121. return type_bytes;
  122. }
  123. string result = "";
  124. for (char temp_value : type_bytes) {
  125. uint8_t *value = 0;
  126. value = reinterpret_cast<uint8_t *>(&temp_value);
  127. char str[kSignificantDigits];
  128. if (sprintf_s(str, kSignificantDigits, "%d", *value) == -1) {
  129. GELOGW("Convert bytes to string fail, filed name:%s", field_name.c_str());
  130. continue;
  131. }
  132. result += str;
  133. }
  134. return result;
  135. }
  136. void Pb2Json::RepeatedMessage2Json(const ProtobufMsg &message, const ProtobufFieldDescriptor *field,
  137. const ProtobufReflection *reflection, const set<string> &black_fields, Json &json,
  138. bool enum2str) {
  139. if ((field == nullptr) || (reflection == nullptr)) {
  140. Message2Json(message, black_fields, json, enum2str);
  141. return;
  142. }
  143. for (auto i = 0; i < reflection->FieldSize(message, field); ++i) {
  144. Json tmp_json;
  145. switch (field->type()) {
  146. case ProtobufFieldDescriptor::TYPE_MESSAGE: {
  147. const ProtobufMsg &tmp_message = reflection->GetRepeatedMessage(message, field, i);
  148. if (0 != tmp_message.ByteSize()) {
  149. Message2Json(tmp_message, black_fields, tmp_json, enum2str);
  150. }
  151. } break;
  152. case ProtobufFieldDescriptor::TYPE_BOOL:
  153. tmp_json = reflection->GetRepeatedBool(message, field, i);
  154. break;
  155. case ProtobufFieldDescriptor::TYPE_ENUM: {
  156. auto *enum_value_desc = reflection->GetRepeatedEnum(message, field, i);
  157. RepeatedEnum2Json(enum_value_desc, enum2str, tmp_json);
  158. } break;
  159. case ProtobufFieldDescriptor::TYPE_INT32:
  160. case ProtobufFieldDescriptor::TYPE_SINT32:
  161. case ProtobufFieldDescriptor::TYPE_SFIXED32:
  162. tmp_json = reflection->GetRepeatedInt32(message, field, i);
  163. break;
  164. case ProtobufFieldDescriptor::TYPE_UINT32:
  165. case ProtobufFieldDescriptor::TYPE_FIXED32:
  166. tmp_json = reflection->GetRepeatedUInt32(message, field, i);
  167. break;
  168. case ProtobufFieldDescriptor::TYPE_INT64:
  169. case ProtobufFieldDescriptor::TYPE_SINT64:
  170. case ProtobufFieldDescriptor::TYPE_SFIXED64:
  171. tmp_json = reflection->GetRepeatedInt64(message, field, i);
  172. break;
  173. case ProtobufFieldDescriptor::TYPE_UINT64:
  174. case ProtobufFieldDescriptor::TYPE_FIXED64:
  175. tmp_json = reflection->GetRepeatedUInt64(message, field, i);
  176. break;
  177. case ProtobufFieldDescriptor::TYPE_FLOAT:
  178. tmp_json = reflection->GetRepeatedFloat(message, field, i);
  179. break;
  180. case ProtobufFieldDescriptor::TYPE_STRING:
  181. case ProtobufFieldDescriptor::TYPE_BYTES:
  182. tmp_json = reflection->GetRepeatedString(message, field, i);
  183. break;
  184. default:
  185. break;
  186. }
  187. json += tmp_json;
  188. }
  189. }
  190. void Pb2Json::Enum2Json(const ProtobufEnumValueDescriptor *enum_value_desc, const ProtobufFieldDescriptor *field,
  191. bool enum2str, Json &json) {
  192. if (enum_value_desc != nullptr) {
  193. if (field == nullptr) {
  194. return;
  195. }
  196. if (enum2str) {
  197. json[field->name()] = enum_value_desc->name();
  198. } else {
  199. json[field->name()] = enum_value_desc->number();
  200. }
  201. }
  202. }
  203. void Pb2Json::RepeatedEnum2Json(const ProtobufEnumValueDescriptor *enum_value_desc, bool enum2str, Json &json) {
  204. if (enum_value_desc != nullptr) {
  205. if (enum2str) {
  206. json = enum_value_desc->name();
  207. } else {
  208. json = enum_value_desc->number();
  209. }
  210. }
  211. }
  212. } // namespace ge

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