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.

dump_data_builder.h 2.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Copyright 2021 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 MINDSPORE_CCSRC_DEBUG_DUMP_DATA_BUILDER_H_
  17. #define MINDSPORE_CCSRC_DEBUG_DUMP_DATA_BUILDER_H_
  18. #include <vector>
  19. #include <string>
  20. #include <iostream>
  21. #include "utils/log_adapter.h"
  22. #ifdef ENABLE_D
  23. #include "proto/dump_data.pb.h"
  24. #include "toolchain/adx_datadump_callback.h"
  25. using Adx::DumpChunk;
  26. #endif
  27. // This class is for building dump data receiving from adx server. Tensor Data for each kernel will be divided in pieces
  28. // and each piece would be wrapped into DumpChunk struct. This class provides function to merge dump chunks and
  29. // construct dump data object.
  30. class DumpDataBuilder {
  31. public:
  32. DumpDataBuilder() {}
  33. ~DumpDataBuilder() = default;
  34. #ifdef ENABLE_D
  35. bool CopyDumpChunk(const DumpChunk *dump_chunk) {
  36. try {
  37. uint32_t buf_sz = dump_chunk->bufLen;
  38. std::string buffer_str(reinterpret_cast<const char *>(dump_chunk->dataBuf), buf_sz);
  39. chunk_list_.push_back(buffer_str);
  40. total_sz_ += buf_sz;
  41. } catch (std::bad_alloc &err) {
  42. MS_LOG(ERROR) << "Failed to allocate memory for " << dump_chunk->fileName << ", reason: " << err.what();
  43. return false;
  44. }
  45. return true;
  46. }
  47. bool ConstructDumpData(debugger::dump::DumpData *dump_data_proto, std::vector<char> *data_ptr) {
  48. if (chunk_list_.empty()) {
  49. return false;
  50. }
  51. // merge several chunks into one piece.
  52. std::string dump_proto_str;
  53. dump_proto_str.reserve(total_sz_);
  54. for (auto item : chunk_list_) {
  55. dump_proto_str += item;
  56. }
  57. chunk_list_.clear();
  58. const int8_t header_len_offset = 8;
  59. uint64_t header_len = *reinterpret_cast<const uint64_t *>(dump_proto_str.c_str());
  60. std::string header = dump_proto_str.substr(header_len_offset, header_len);
  61. if (!(*dump_data_proto).ParseFromString(header)) {
  62. MS_LOG(ERROR) << "Failed to parse dump proto file.";
  63. return false;
  64. }
  65. auto data_sz = total_sz_ - header_len_offset - header_len;
  66. data_ptr->resize(data_sz);
  67. auto ret = memcpy_s(data_ptr->data(), data_sz, dump_proto_str.c_str() + header_len_offset + header_len, data_sz);
  68. if (ret != 0) {
  69. MS_LOG(ERROR) << "Failed to get data from Adx";
  70. return false;
  71. }
  72. return true;
  73. }
  74. #endif
  75. private:
  76. std::vector<std::string> chunk_list_;
  77. uint64_t total_sz_{0};
  78. };
  79. #endif // MINDSPORE_CCSRC_DEBUG_DUMP_DATA_BUILDER_H_