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.

acl_graph_parser_util.h 5.5 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13. #ifndef ACL_GRAPH_PARSE_UTIL_
  14. #define ACL_GRAPH_PARSE_UTIL_
  15. #include <map>
  16. #include <string>
  17. #include <google/protobuf/text_format.h>
  18. #include <sstream>
  19. #include "framework/omg/parser/parser_types.h"
  20. #include "register/register_error_codes.h"
  21. #include "graph/utils/graph_utils.h"
  22. namespace ge {
  23. using google::protobuf::Message;
  24. class AclGrphParseUtil {
  25. public:
  26. AclGrphParseUtil() {}
  27. virtual ~AclGrphParseUtil() {}
  28. domi::Status LoadOpsProtoLib();
  29. void SaveCustomCaffeProtoPath();
  30. domi::Status AclParserInitialize(const std::map<std::string, std::string> &options);
  31. domi::Status SetDefaultOutputNode(ge::Graph &graph);
  32. private:
  33. bool parser_initialized = false;
  34. domi::Status GetOutputLeaf(NodePtr node, std::vector<std::pair<ge::NodePtr, int32_t>> &output_nodes_info);
  35. void GetOutputNodesNameAndIndex(std::vector<std::pair<ge::NodePtr, int32_t>> &output_nodes_info,
  36. std::vector<std::string> &output_nodes_name);
  37. };
  38. namespace parser {
  39. ///
  40. /// @ingroup: domi_common
  41. /// @brief: get length of file
  42. /// @param [in] input_file: path of file
  43. /// @return long: File length. If the file length fails to be obtained, the value -1 is returned.
  44. ///
  45. extern long GetFileLength(const std::string &input_file);
  46. ///
  47. /// @ingroup domi_common
  48. /// @brief Absolute path for obtaining files.
  49. /// @param [in] path of input file
  50. /// @param [out] Absolute path of a file. If the absolute path cannot be obtained, an empty string is returned
  51. ///
  52. std::string RealPath(const char *path);
  53. ///
  54. /// @ingroup domi_common
  55. /// @brief Obtains the absolute time (timestamp) of the current system.
  56. /// @return Timestamp, in microseconds (US)
  57. ///
  58. ///
  59. uint64_t GetCurrentTimestamp();
  60. ///
  61. /// @ingroup domi_common
  62. /// @brief Reads all data from a binary file.
  63. /// @param [in] file_name path of file
  64. /// @param [out] buffer Output memory address, which needs to be released by the caller.
  65. /// @param [out] length Output memory size
  66. /// @return false fail
  67. /// @return true success
  68. ///
  69. bool ReadBytesFromBinaryFile(const char *file_name, char **buffer, int &length);
  70. ///
  71. /// @ingroup domi_common
  72. /// @brief proto file in bianary format
  73. /// @param [in] file path of proto file
  74. /// @param [out] proto memory for storing the proto file
  75. /// @return true success
  76. /// @return false fail
  77. ///
  78. bool ReadProtoFromBinaryFile(const char *file, Message *proto);
  79. ///
  80. /// @ingroup domi_common
  81. /// @brief Reads the proto structure from an array.
  82. /// @param [in] data proto data to be read
  83. /// @param [in] size proto data size
  84. /// @param [out] proto Memory for storing the proto file
  85. /// @return true success
  86. /// @return false fail
  87. ///
  88. bool ReadProtoFromArray(const void *data, int size, Message *proto);
  89. ///
  90. /// @ingroup domi_proto
  91. /// @brief Reads the proto file in the text format.
  92. /// @param [in] file path of proto file
  93. /// @param [out] message Memory for storing the proto file
  94. /// @return true success
  95. /// @return false fail
  96. ///
  97. bool ReadProtoFromText(const char *file, google::protobuf::Message *message);
  98. bool ReadProtoFromMem(const char *data, int size, google::protobuf::Message *message);
  99. ///
  100. /// @brief get the Original Type of FrameworkOp
  101. /// @param [in] node
  102. /// @param [out] type
  103. /// @return Status
  104. ///
  105. domi::Status GetOriginalType(const ge::NodePtr &node, string &type);
  106. ///
  107. /// @ingroup domi_common
  108. /// @brief Check whether the file path meets the whitelist verification requirements.
  109. /// @param [in] filePath file path
  110. /// @param [out] result
  111. ///
  112. bool ValidateStr(const std::string &filePath, const std::string &mode);
  113. ///
  114. /// @ingroup domi_common
  115. /// @brief Obtains the current time string.
  116. /// @return Time character string in the format: %Y%m%d%H%M%S, eg: 20171011083555
  117. ///
  118. std::string CurrentTimeInStr();
  119. } // namespace parser
  120. } // namespace ge
  121. /*lint --emacro((773),GE_TIMESTAMP_START)*/
  122. /*lint -esym(773,GE_TIMESTAMP_START)*/
  123. #define PARSER_TIMESTAMP_START(stage) uint64_t startUsec_##stage = ge::parser::GetCurrentTimestamp()
  124. #define PARSER_TIMESTAMP_END(stage, stage_name) \
  125. do { \
  126. uint64_t endUsec_##stage = ge::parser::GetCurrentTimestamp(); \
  127. GELOGI("[GEPERFTRACE] The time cost of %s is [%lu] micro second.", (stage_name), \
  128. (endUsec_##stage - startUsec_##stage)); \
  129. } while (0);
  130. #define PARSER_TIMESTAMP_EVENT_END(stage, stage_name) \
  131. do { \
  132. uint64_t endUsec_##stage = ge::parser::GetCurrentTimestamp(); \
  133. GEEVENT("[GEPERFTRACE] The time cost of %s is [%lu] micro second.", (stage_name), \
  134. (endUsec_##stage - startUsec_##stage)); \
  135. } while (0);
  136. #endif // ACL_GRAPH_PARSE_UTIL_

Ascend CANN Parser(简称parser)配合TF_Adapter、 ATC工具、IR构图等使用,开发者通过以上工具,借助parser能方便地将第三方框架的算法表示转换成Ascend IR,充分利用昇腾AI处理器卓越的运算能力