Merged
fix(framework): Fix thread safety and resource management issues. #3198
黄俊杰Add on May 12
fix(framework): Fix thread safety and resource management issues. #3198
Merged
Merged fromjunjieHuang/pyptoreviewintocann/pyptomaster
Total: 13 files changed+159-83
| @@ -401,7 +401,8 @@ public: | |||
| 401 | static const char* MSG = "DIWEFVN"; | 401 | static const char* MSG = "DIWEFVN"; |
| 402 | auto now = std::chrono::system_clock::now(); | 402 | auto now = std::chrono::system_clock::now(); |
| 403 | auto time = std::chrono::system_clock::to_time_t(now); | 403 | auto time = std::chrono::system_clock::to_time_t(now); |
| 404 | - auto tm = *std::localtime(&time); | 404 | + std::tm tm {}; |
| 405 | + (void)localtime_r(&time, &tm); | ||
| 405 | 406 | ||
| 406 | // Format timestamp | 407 | // Format timestamp |
| 407 | char timeBuf[128]; | 408 | char timeBuf[128]; |
| @@ -381,6 +381,7 @@ enum class VerifyErrorCategory : uint32_t { | |||
| 381 | enum class VerifyEnableScene : uint32_t { | 381 | enum class VerifyEnableScene : uint32_t { |
| 382 | VERIFY_NOT_ENABLE = 0xB0001U, | 382 | VERIFY_NOT_ENABLE = 0xB0001U, |
| 383 | VERIFY_LOAD_CALC_OPS_FAILED = 0xB0002U, | 383 | VERIFY_LOAD_CALC_OPS_FAILED = 0xB0002U, |
| 384 | + TOLERANCE_MISMATCH = 0xB0003, | ||
| 384 | }; | 385 | }; |
| 385 | 386 | ||
| 386 | enum class ControlFlowScene : uint32_t { | 387 | enum class ControlFlowScene : uint32_t { |
| @@ -402,6 +403,7 @@ enum class ControlFlowScene : uint32_t { | |||
| 402 | MIX_GLOBAL_TENSOR_WAIT_TIMEOUT = 0xB1010U, | 403 | MIX_GLOBAL_TENSOR_WAIT_TIMEOUT = 0xB1010U, |
| 403 | MIX_SPLIT_PARALLEL_LIMIT_EXCEEDED = 0xB1011U, | 404 | MIX_SPLIT_PARALLEL_LIMIT_EXCEEDED = 0xB1011U, |
| 404 | INTERPRETER_SYNC_SIM_WAIT_TIMEOUT = 0xB1012U, | 405 | INTERPRETER_SYNC_SIM_WAIT_TIMEOUT = 0xB1012U, |
| 406 | + FUNC_RAW_TENSOR_NULL = 0xB1013U, | ||
| 405 | }; | 407 | }; |
| 406 | 408 | ||
| 407 | enum class ExecuteOperationScene : uint32_t { | 409 | enum class ExecuteOperationScene : uint32_t { |
| @@ -14,12 +14,15 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "interface/inner/hash_buffer.h" | 16 | #include "interface/inner/hash_buffer.h" |
| 17 | +#include "tilefwk/error.h" | ||
| 17 | 18 | ||
| 18 | namespace npu::tile_fwk { | 19 | namespace npu::tile_fwk { |
| 19 | 20 | ||
| 20 | template <> | 21 | template <> |
| 21 | uint64_t HashBuffer::Get<uint64_t>(int index) const | 22 | uint64_t HashBuffer::Get<uint64_t>(int index) const |
| 22 | { | 23 | { |
| 24 | + FE_ASSERT(index >= 0 && static_cast<size_t>(index + 1) < this->size()) | ||
| 25 | + << "HashBuffer::Get index out of range: index=" << index << ", size=" << this->size(); | ||
| 23 | uint64_t l = this->at(index); | 26 | uint64_t l = this->at(index); |
| 24 | uint64_t h = this->at(index + 1); | 27 | uint64_t h = this->at(index + 1); |
| 25 | return l + (h << 32); // h takes high 32 bits | 28 | return l + (h << 32); // h takes high 32 bits |
| @@ -28,6 +31,8 @@ uint64_t HashBuffer::Get<uint64_t>(int index) const | |||
| 28 | template <> | 31 | template <> |
| 29 | int64_t HashBuffer::Get<int64_t>(int index) const | 32 | int64_t HashBuffer::Get<int64_t>(int index) const |
| 30 | { | 33 | { |
| 34 | + FE_ASSERT(index >= 0 && static_cast<size_t>(index + 1) < this->size()) | ||
| 35 | + << "HashBuffer::Get index out of range: index=" << index << ", size=" << this->size(); | ||
| 31 | int64_t l = this->at(index); | 36 | int64_t l = this->at(index); |
| 32 | int64_t h = this->at(index + 1); | 37 | int64_t h = this->at(index + 1); |
| 33 | return l + (h << 32); // h takes high 32 bits | 38 | return l + (h << 32); // h takes high 32 bits |
| @@ -561,6 +561,7 @@ void FlowVerifier::VerifyPass(Function* func, int passIndex, const std::string& | |||
| 561 | } | 561 | } |
| 562 | 562 | ||
| 563 | std::vector<double> tolerance = config::GetVerifyOption<std::vector<double>>(KEY_PASS_VERIFY_ERROR_TOL); | 563 | std::vector<double> tolerance = config::GetVerifyOption<std::vector<double>>(KEY_PASS_VERIFY_ERROR_TOL); |
| 564 | + ASSERT(VerifyEnableScene::TOLERANCE_MISMATCH, tolerance.size() == 2) << "Expected tolerance size: 2, actual tolerance size: " << tolerance.size(); | ||
| 564 | float rtol = static_cast<float>(tolerance[0]); | 565 | float rtol = static_cast<float>(tolerance[0]); |
| 565 | float atol = static_cast<float>(tolerance[1]); | 566 | float atol = static_cast<float>(tolerance[1]); |
| 566 | 567 | ||
| @@ -141,9 +141,21 @@ FunctionInterpreter::FunctionInterpreter() | |||
| 141 | dumpPath = dumpPath + "/" + "verify_" + MakeVerifyRunTimestampTag() + "/"; | 141 | dumpPath = dumpPath + "/" + "verify_" + MakeVerifyRunTimestampTag() + "/"; |
| 142 | CreateMultiLevelDir(dumpPath); | 142 | CreateMultiLevelDir(dumpPath); |
| 143 | interpreter::SetLogFilePath(dumpPath + "interpreter.log"); | 143 | interpreter::SetLogFilePath(dumpPath + "interpreter.log"); |
| 144 | - execOpResultFile = fopen((dumpPath + "verify_graph_data_metainfo.csv").c_str(), "w"); | 144 | + const std::string opResultFilePath = dumpPath + "verify_graph_data_metainfo.csv"; |
| 145 | - execProgrameResultFile = fopen((dumpPath + "verify_graph_result_brief.csv").c_str(), "w"); | 145 | + execOpResultFile = fopen(opResultFilePath.c_str(), "w"); |
| 146 | - execDumpErrorFile = fopen((dumpPath + "verify_graph_result_brief.log").c_str(), "w"); | 146 | + if (execOpResultFile == nullptr) { |
| 147 | + INTERPRETER_LOGE(OpDumpScene::DUMP_OPEN_FILE_FAILED, "open file %s failed", opResultFilePath.c_str()); | ||
| 148 | + } | ||
| 149 | + const std::string programeResultFilePath = dumpPath + "verify_graph_result_brief.csv"; | ||
| 150 | + execProgrameResultFile = fopen(programeResultFilePath.c_str(), "w"); | ||
| 151 | + if (execProgrameResultFile == nullptr) { | ||
| 152 | + INTERPRETER_LOGE(OpDumpScene::DUMP_OPEN_FILE_FAILED, "open file %s failed", programeResultFilePath.c_str()); | ||
| 153 | + } | ||
| 154 | + const std::string dumpErrorFilePath = dumpPath + "verify_graph_result_brief.log"; | ||
| 155 | + execDumpErrorFile = fopen(dumpErrorFilePath.c_str(), "w"); | ||
| 156 | + if (execDumpErrorFile == nullptr) { | ||
| 157 | + INTERPRETER_LOGE(OpDumpScene::DUMP_OPEN_FILE_FAILED, "open file %s failed", dumpErrorFilePath.c_str()); | ||
| 158 | + } | ||
| 147 | auto opCsvHeader = MakeOpInfoCsvHeader(); | 159 | auto opCsvHeader = MakeOpInfoCsvHeader(); |
| 148 | auto programeCsvHeader = MakeProgrameInfoCsvHeader(); | 160 | auto programeCsvHeader = MakeProgrameInfoCsvHeader(); |
| 149 | WriteCsvRow(opCsvHeader, opInfoRowNum, execOpResultFile); | 161 | WriteCsvRow(opCsvHeader, opInfoRowNum, execOpResultFile); |
| @@ -351,6 +363,10 @@ void FunctionInterpreter::DumpTensorBinary( | |||
| 351 | return; | 363 | return; |
| 352 | } | 364 | } |
| 353 | FILE* fdata = fopen(dumpTensorFilePath.c_str(), "wb"); | 365 | FILE* fdata = fopen(dumpTensorFilePath.c_str(), "wb"); |
| 366 | + if (fdata == nullptr) { | ||
| 367 | + INTERPRETER_LOGE(OpDumpScene::DUMP_OPEN_FILE_FAILED, "Failed to open file: %s", dumpTensorFilePath.c_str()); | ||
| 368 | + return; | ||
| 369 | + } | ||
| 354 | DumpBinary(validShape, stride, offset, fdata, dataView->GetData()->data(), BytesOf(dataView->GetDataType())); | 370 | DumpBinary(validShape, stride, offset, fdata, dataView->GetData()->data(), BytesOf(dataView->GetDataType())); |
| 355 | fclose(fdata); | 371 | fclose(fdata); |
| 356 | } | 372 | } |
| @@ -366,6 +382,10 @@ std::shared_ptr<LogicalTensorData> FunctionInterpreter::LoadTensorBinary( | |||
| 366 | return nullptr; | 382 | return nullptr; |
| 367 | } | 383 | } |
| 368 | FILE* fdata = fopen(filepath.c_str(), "rb"); | 384 | FILE* fdata = fopen(filepath.c_str(), "rb"); |
| 385 | + if (fdata == nullptr) { | ||
| 386 | + INTERPRETER_LOGE(OpDumpScene::DUMP_OPEN_FILE_FAILED, "Failed to open file: %s", filepath.c_str()); | ||
| 387 | + return nullptr; | ||
| 388 | + } | ||
| 369 | auto data = std::make_shared<RawTensorData>(static_cast<DataType>(tensor->Datatype()), shape); | 389 | auto data = std::make_shared<RawTensorData>(static_cast<DataType>(tensor->Datatype()), shape); |
| 370 | if (fread(data->data(), 1, data->size(), fdata) != data->size()) { | 390 | if (fread(data->data(), 1, data->size(), fdata) != data->size()) { |
| 371 | fclose(fdata); | 391 | fclose(fdata); |
| @@ -392,6 +412,10 @@ void FunctionInterpreter::DumpTensorList( | |||
| 392 | 412 | ||
| 393 | std::string dumpTensorFilePath = GetDumpFilePath(execDumpDir, dumpTensorDirName, dumpTensorFileName); | 413 | std::string dumpTensorFilePath = GetDumpFilePath(execDumpDir, dumpTensorDirName, dumpTensorFileName); |
| 394 | FILE* dumpTensorFile = fopen(dumpTensorFilePath.c_str(), "w"); | 414 | FILE* dumpTensorFile = fopen(dumpTensorFilePath.c_str(), "w"); |
| 415 | + if (dumpTensorFile == nullptr) { | ||
| 416 | + INTERPRETER_LOGE(OpDumpScene::DUMP_OPEN_FILE_FAILED, "Failed to open file: %s", dumpTensorFilePath.c_str()); | ||
| 417 | + return; | ||
| 418 | + } | ||
| 395 | fprintf(dumpTensorFile, R"HTML( | 419 | fprintf(dumpTensorFile, R"HTML( |
| 396 | <html> | 420 | <html> |
| 397 | <head> | 421 | <head> |
| @@ -594,6 +618,10 @@ void FunctionInterpreter::DumpPassTensorDiff( | |||
| 594 | 618 | ||
| 595 | std::string dumpStyleFilePath = execDumpDir + "/entry_" + std::to_string(captureIndex) + ".css"; | 619 | std::string dumpStyleFilePath = execDumpDir + "/entry_" + std::to_string(captureIndex) + ".css"; |
| 596 | execDumpStyleFile = fopen(dumpStyleFilePath.c_str(), "w"); | 620 | execDumpStyleFile = fopen(dumpStyleFilePath.c_str(), "w"); |
| 621 | + if (execDumpStyleFile == nullptr) { | ||
| 622 | + INTERPRETER_LOGE(OpDumpScene::DUMP_OPEN_FILE_FAILED, "Failed to open file: %s", dumpStyleFilePath.c_str()); | ||
| 623 | + return; | ||
| 624 | + } | ||
| 597 | for (size_t idx = 0; idx < captureGolden->GetFrameList().size(); idx++) { | 625 | for (size_t idx = 0; idx < captureGolden->GetFrameList().size(); idx++) { |
| 598 | auto frameExecution = captureExecution->GetFrameList()[idx]; | 626 | auto frameExecution = captureExecution->GetFrameList()[idx]; |
| 599 | auto frameGolden = captureGolden->GetFrameList()[idx]; | 627 | auto frameGolden = captureGolden->GetFrameList()[idx]; |
| @@ -639,16 +667,24 @@ void FunctionInterpreter::DumpBegin() | |||
| 639 | std::string styleFilePath = dumpPath + "/verifier.css"; | 667 | std::string styleFilePath = dumpPath + "/verifier.css"; |
| 640 | if (GetFileSize(styleFilePath) == 0) { | 668 | if (GetFileSize(styleFilePath) == 0) { |
| 641 | FILE* fcss = fopen(styleFilePath.c_str(), "w"); | 669 | FILE* fcss = fopen(styleFilePath.c_str(), "w"); |
| 642 | - for (int i = 0; i < MAX_IDENT_LEVEL; i++) { | 670 | + if (fcss == nullptr) { |
| 643 | - fprintf(fcss, ".indent_%d { margin-left: %dpx; }\n", i, i * 50); // 50 is left margin | 671 | + INTERPRETER_LOGE(OpDumpScene::DUMP_OPEN_FILE_FAILED, "Failed to open file: %s", styleFilePath.c_str()); |
| 672 | + } else { | ||
| 673 | + for (int i = 0; i < MAX_IDENT_LEVEL; i++) { | ||
| 674 | + fprintf(fcss, ".indent_%d { margin-left: %dpx; }\n", i, i * 50); // 50 is left margin | ||
| 675 | + } | ||
| 676 | + fprintf(fcss, ".table_head { width: 10%%; }\n"); | ||
| 677 | + fprintf(fcss, ".tensor_data { vertical-align: top; }\n"); | ||
| 678 | + fclose(fcss); | ||
| 644 | } | 679 | } |
| 645 | - fprintf(fcss, ".table_head { width: 10%%; }\n"); | ||
| 646 | - fprintf(fcss, ".tensor_data { vertical-align: top; }\n"); | ||
| 647 | - fclose(fcss); | ||
| 648 | } | 680 | } |
| 649 | 681 | ||
| 650 | std::string dumpFilePath = execDumpDir + "/entry_" + std::to_string(captureIndex) + ".html"; | 682 | std::string dumpFilePath = execDumpDir + "/entry_" + std::to_string(captureIndex) + ".html"; |
| 651 | execDumpFile = fopen(dumpFilePath.c_str(), "w"); | 683 | execDumpFile = fopen(dumpFilePath.c_str(), "w"); |
| 684 | + if (execDumpFile == nullptr) { | ||
| 685 | + INTERPRETER_LOGE(OpDumpScene::DUMP_OPEN_FILE_FAILED, "Failed to open file: %s", dumpFilePath.c_str()); | ||
| 686 | + return; | ||
| 687 | + } | ||
| 652 | fprintf( | 688 | fprintf( |
| 653 | execDumpFile, R"HTML( | 689 | execDumpFile, R"HTML( |
| 654 | <html> | 690 | <html> |
| @@ -292,11 +292,12 @@ struct FunctionFrame { | |||
| 292 | } | 292 | } |
| 293 | 293 | ||
| 294 | auto raw = inplaceTensor ? inplaceTensor->GetRawTensor() : tensor->GetRawTensor(); | 294 | auto raw = inplaceTensor ? inplaceTensor->GetRawTensor() : tensor->GetRawTensor(); |
| 295 | + ASSERT(ControlFlowScene::FUNC_RAW_TENSOR_NULL, raw != nullptr) << "raw is nullptr."; | ||
| 295 | bool isSpilled = false; | 296 | bool isSpilled = false; |
| 296 | 297 | ||
| 297 | - std::string spillRawMaigc = "1056964608"; | 298 | + const std::string spillRawMagic = "1056964608"; |
| 298 | std::string rawMagic = std::to_string(raw->GetRawMagic()); | 299 | std::string rawMagic = std::to_string(raw->GetRawMagic()); |
| 299 | - if (rawMagic.find(spillRawMaigc) != std::string::npos) { | 300 | + if (rawMagic.find(spillRawMagic) != std::string::npos) { |
| 300 | if (spillRawTensorDict.count(tensor)) { | 301 | if (spillRawTensorDict.count(tensor)) { |
| 301 | raw = spillRawTensorDict[tensor]; | 302 | raw = spillRawTensorDict[tensor]; |
| 302 | } else { | 303 | } else { |
| @@ -585,9 +586,15 @@ struct FunctionInterpreter { | |||
| 585 | 586 | ||
| 586 | ~FunctionInterpreter() | 587 | ~FunctionInterpreter() |
| 587 | { | 588 | { |
| 588 | - fclose(execOpResultFile); | 589 | + if (execOpResultFile != nullptr) { |
| 589 | - fclose(execProgrameResultFile); | 590 | + fclose(execOpResultFile); |
| 590 | - fclose(execDumpErrorFile); | 591 | + } |
| 592 | + if (execProgrameResultFile != nullptr) { | ||
| 593 | + fclose(execProgrameResultFile); | ||
| 594 | + } | ||
| 595 | + if (execDumpErrorFile != nullptr) { | ||
| 596 | + fclose(execDumpErrorFile); | ||
| 597 | + } | ||
| 591 | } | 598 | } |
| 592 | 599 | ||
| 593 | Function* entry_; | 600 | Function* entry_; |
| @@ -1744,6 +1751,10 @@ public: | |||
| 1744 | 1751 | ||
| 1745 | void WriteCsvRow(std::vector<std::string>& row, int& rowNum, FILE* file) | 1752 | void WriteCsvRow(std::vector<std::string>& row, int& rowNum, FILE* file) |
| 1746 | { | 1753 | { |
| 1754 | + if (file == nullptr) { | ||
| 1755 | + INTERPRETER_LOGE(OpDumpScene::DUMP_OPEN_FILE_FAILED, "File is nullptr."); | ||
| 1756 | + return; | ||
| 1757 | + } | ||
| 1747 | if (rowNum > 0) { | 1758 | if (rowNum > 0) { |
| 1748 | row[0] = std::to_string(rowNum); | 1759 | row[0] = std::to_string(rowNum); |
| 1749 | } | 1760 | } |
| @@ -17,12 +17,15 @@ | |||
| 17 | 17 | ||
| 18 | namespace npu::tile_fwk { | 18 | namespace npu::tile_fwk { |
| 19 | 19 | ||
| 20 | +constexpr size_t LOGICAL_TENSOR_DATA_HEAD_MAX_DIM = 5; | ||
| 21 | +constexpr size_t LOGICAL_TENSOR_DATA_HEAD_PADDING_SIZE = 3; | ||
| 22 | + | ||
| 20 | struct LogicalTensorDataHead { | 23 | struct LogicalTensorDataHead { |
| 21 | uint32_t version{0}; | 24 | uint32_t version{0}; |
| 22 | uint32_t dataType{0}; | 25 | uint32_t dataType{0}; |
| 23 | uint32_t dimension{0}; | 26 | uint32_t dimension{0}; |
| 24 | - int64_t shape[0x5] = {0}; | 27 | + int64_t shape[LOGICAL_TENSOR_DATA_HEAD_MAX_DIM] = {0}; |
| 25 | - uint32_t padding[0x3] = {0}; | 28 | + uint32_t padding[LOGICAL_TENSOR_DATA_HEAD_PADDING_SIZE] = {0}; |
| 26 | }; | 29 | }; |
| 27 | 30 | ||
| 28 | std::string LogicalTensorData::DumpRange( | 31 | std::string LogicalTensorData::DumpRange( |
| @@ -79,8 +82,10 @@ std::string LogicalTensorData::DumpCoord(int row) const | |||
| 79 | int coordDim = row; | 82 | int coordDim = row; |
| 80 | for (size_t k = 0; k < GetShape().size() - 1; k++) { | 83 | for (size_t k = 0; k < GetShape().size() - 1; k++) { |
| 81 | int dim = GetShape().size() - 2 - k; | 84 | int dim = GetShape().size() - 2 - k; |
| 82 | - coord[dim] = coordDim % GetShape()[dim]; | 85 | + int dimLength = GetShape()[dim]; |
| 83 | - coordDim /= GetShape()[dim]; | 86 | + ASSERT(ExecuteOperationScene::INVALID_TENSOR_SHAPE, dimLength != 0) << "Shape dimension " << dim << " must not be zero"; |
| 87 | + coord[dim] = coordDim % dimLength; | ||
| 88 | + coordDim /= dimLength; | ||
| 84 | } | 89 | } |
| 85 | for (size_t k = 0; k < coord.size(); k++) { | 90 | for (size_t k = 0; k < coord.size(); k++) { |
| 86 | if (k != 0) { | 91 | if (k != 0) { |
| @@ -99,6 +104,9 @@ std::string LogicalTensorData::DumpData(int indent, const std::vector<ElementDum | |||
| 99 | oss << DumpType() << " {\n"; | 104 | oss << DumpType() << " {\n"; |
| 100 | int rowSize = 1; | 105 | int rowSize = 1; |
| 101 | for (size_t k = 0; k < GetShape().size() - 1; k++) { | 106 | for (size_t k = 0; k < GetShape().size() - 1; k++) { |
| 107 | + ASSERT(ExecuteOperationScene::INVALID_TENSOR_SHAPE, | ||
| 108 | + GetShape()[k] > 0 && rowSize <= INT_MAX / GetShape()[k]) | ||
| 109 | + << "Tensor shape multiplication overflow at dimension " << k; | ||
| 102 | rowSize *= GetShape()[k]; | 110 | rowSize *= GetShape()[k]; |
| 103 | } | 111 | } |
| 104 | int colSize = GetShape().back(); | 112 | int colSize = GetShape().back(); |
| @@ -132,70 +140,74 @@ std::string LogicalTensorData::DumpData(int indent, const std::vector<ElementDum | |||
| 132 | void LogicalTensorData::Save(const std::string& filepath) const | 140 | void LogicalTensorData::Save(const std::string& filepath) const |
| 133 | { | 141 | { |
| 134 | FILE* fdata = fopen(filepath.c_str(), "wb"); | 142 | FILE* fdata = fopen(filepath.c_str(), "wb"); |
| 135 | - LogicalTensorDataHead head; | 143 | + if (fdata != nullptr) { |
| 136 | - head.dataType = GetDataType(); | 144 | + LogicalTensorDataHead head; |
| 137 | - head.dimension = GetShape().size(); | 145 | + head.dataType = GetDataType(); |
| 138 | - for (size_t k = 0; k < GetShape().size(); k++) { | 146 | + head.dimension = GetShape().size(); |
| 139 | - head.shape[k] = GetShape()[k]; | 147 | + for (size_t k = 0; k < GetShape().size(); k++) { |
| 140 | - } | 148 | + head.shape[k] = GetShape()[k]; |
| 141 | - fwrite(&head, sizeof(head), 1, fdata); | 149 | + } |
| 150 | + fwrite(&head, sizeof(head), 1, fdata); | ||
| 142 | 151 | ||
| 143 | - int rowSize = GetShape().back(); | 152 | + int rowSize = GetShape().back(); |
| 144 | - int totalSize = GetSize(); | 153 | + int totalSize = GetSize(); |
| 145 | 154 | ||
| 146 | - switch (GetDataType()) { | 155 | + switch (GetDataType()) { |
| 147 | - case DT_INT8: | 156 | + case DT_INT8: |
| 148 | - HandleSave<int8_t>(fdata, totalSize, rowSize); | 157 | + HandleSave<int8_t>(fdata, totalSize, rowSize); |
| 149 | - break; | 158 | + break; |
| 150 | - case DT_INT16: | 159 | + case DT_INT16: |
| 151 | - HandleSave<int16_t>(fdata, totalSize, rowSize); | 160 | + HandleSave<int16_t>(fdata, totalSize, rowSize); |
| 152 | - break; | 161 | + break; |
| 153 | - case DT_INT32: | 162 | + case DT_INT32: |
| 154 | - HandleSave<int32_t>(fdata, totalSize, rowSize); | 163 | + HandleSave<int32_t>(fdata, totalSize, rowSize); |
| 155 | - break; | 164 | + break; |
| 156 | - case DT_INT64: | 165 | + case DT_INT64: |
| 157 | - HandleSave<int64_t>(fdata, totalSize, rowSize); | 166 | + HandleSave<int64_t>(fdata, totalSize, rowSize); |
| 158 | - break; | 167 | + break; |
| 159 | - case DT_FP16: | 168 | + case DT_FP16: |
| 160 | - HandleSave<npu::tile_fwk::float16>(fdata, totalSize, rowSize); | 169 | + HandleSave<npu::tile_fwk::float16>(fdata, totalSize, rowSize); |
| 161 | - break; | 170 | + break; |
| 162 | - case DT_FP32: | 171 | + case DT_FP32: |
| 163 | - HandleSave<float>(fdata, totalSize, rowSize); | 172 | + HandleSave<float>(fdata, totalSize, rowSize); |
| 164 | - break; | 173 | + break; |
| 165 | - case DT_BF16: | 174 | + case DT_BF16: |
| 166 | - HandleSave<npu::tile_fwk::bfloat16>(fdata, totalSize, rowSize); | 175 | + HandleSave<npu::tile_fwk::bfloat16>(fdata, totalSize, rowSize); |
| 167 | - break; | 176 | + break; |
| 168 | - case DT_UINT8: | 177 | + case DT_UINT8: |
| 169 | - HandleSave<uint8_t>(fdata, totalSize, rowSize); | 178 | + HandleSave<uint8_t>(fdata, totalSize, rowSize); |
| 170 | - break; | 179 | + break; |
| 171 | - case DT_UINT16: | 180 | + case DT_UINT16: |
| 172 | - HandleSave<uint16_t>(fdata, totalSize, rowSize); | 181 | + HandleSave<uint16_t>(fdata, totalSize, rowSize); |
| 173 | - break; | 182 | + break; |
| 174 | - case DT_UINT32: | 183 | + case DT_UINT32: |
| 175 | - HandleSave<uint32_t>(fdata, totalSize, rowSize); | 184 | + HandleSave<uint32_t>(fdata, totalSize, rowSize); |
| 176 | - break; | 185 | + break; |
| 177 | - case DT_UINT64: | 186 | + case DT_UINT64: |
| 178 | - HandleSave<uint64_t>(fdata, totalSize, rowSize); | 187 | + HandleSave<uint64_t>(fdata, totalSize, rowSize); |
| 179 | - break; | 188 | + break; |
| 180 | - case DT_DOUBLE: | 189 | + case DT_DOUBLE: |
| 181 | - HandleSave<double>(fdata, totalSize, rowSize); | 190 | + HandleSave<double>(fdata, totalSize, rowSize); |
| 182 | - break; | 191 | + break; |
| 183 | - case DT_BOOL: | 192 | + case DT_BOOL: |
| 184 | - HandleSave<bool>(fdata, totalSize, rowSize); | 193 | + HandleSave<bool>(fdata, totalSize, rowSize); |
| 185 | - break; | 194 | + break; |
| 186 | - default: | 195 | + default: |
| 187 | - ASSERT(false); | 196 | + ASSERT(false); |
| 188 | - break; | 197 | + break; |
| 198 | + } | ||
| 199 | + fclose(fdata); | ||
| 189 | } | 200 | } |
| 190 | - fclose(fdata); | ||
| 191 | } | 201 | } |
| 192 | 202 | ||
| 193 | void LogicalTensorData::SaveFile(const char* filepath) const { return Save(filepath); } | 203 | void LogicalTensorData::SaveFile(const char* filepath) const { return Save(filepath); } |
| 194 | 204 | ||
| 195 | std::string LogicalTensorData::ToString(const PrintOptions* options) const | 205 | std::string LogicalTensorData::ToString(const PrintOptions* options) const |
| 196 | { | 206 | { |
| 207 | + constexpr size_t MAX_AXES_DIM = 8; | ||
| 208 | + constexpr int EDGE_ITEMS_MULTIPLIER = 2; | ||
| 197 | std::stringstream os; | 209 | std::stringstream os; |
| 198 | - int64_t axes[0x8] = {0}; // max dim is 8 | 210 | + int64_t axes[MAX_AXES_DIM] = {0}; |
| 199 | 211 | ||
| 200 | if (options == nullptr) { | 212 | if (options == nullptr) { |
| 201 | options = &config::GetPrintOptions(); | 213 | options = &config::GetPrintOptions(); |
| @@ -252,7 +264,7 @@ std::string LogicalTensorData::ToString(const PrintOptions* options) const | |||
| 252 | printImpl = [&](int dim) { | 264 | printImpl = [&](int dim) { |
| 253 | os << "["; | 265 | os << "["; |
| 254 | if (dim == ndim - 1) { | 266 | if (dim == ndim - 1) { |
| 255 | - if (shape[dim] > 0x2 * edgeItems) { | 267 | + if (shape[dim] > EDGE_ITEMS_MULTIPLIER * edgeItems) { |
| 256 | print1d(dim, 0, edgeItems); | 268 | print1d(dim, 0, edgeItems); |
| 257 | os << " ..."; | 269 | os << " ..."; |
| 258 | print1d(dim, shape[dim] - edgeItems, shape[dim]); | 270 | print1d(dim, shape[dim] - edgeItems, shape[dim]); |
| @@ -260,7 +272,7 @@ std::string LogicalTensorData::ToString(const PrintOptions* options) const | |||
| 260 | print1d(dim, 0, shape[dim]); | 272 | print1d(dim, 0, shape[dim]); |
| 261 | } | 273 | } |
| 262 | } else { | 274 | } else { |
| 263 | - if (shape[dim] > 0x2 * edgeItems) { | 275 | + if (shape[dim] > EDGE_ITEMS_MULTIPLIER * edgeItems) { |
| 264 | printnd(dim, 0, edgeItems); | 276 | printnd(dim, 0, edgeItems); |
| 265 | os << "\n"; | 277 | os << "\n"; |
| 266 | repeat(' ', dim + 1); | 278 | repeat(' ', dim + 1); |
| @@ -48,7 +48,7 @@ Json RawTensor::DumpJson() const | |||
| 48 | result["actual_rawmagic"] = actualRawmagic; | 48 | result["actual_rawmagic"] = actualRawmagic; |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | - if (symbol != "") { | 51 | + if (!symbol.empty()) { |
| 52 | result["symbol"] = symbol; | 52 | result["symbol"] = symbol; |
| 53 | } | 53 | } |
| 54 | return result; | 54 | return result; |
| @@ -58,16 +58,18 @@ bool TensorPtrComparator::operator()( | |||
| 58 | // Insert function: Adds a tensor to the map, removing any matching tensors first | 58 | // Insert function: Adds a tensor to the map, removing any matching tensors first |
| 59 | void TensorMap::Insert(std::shared_ptr<LogicalTensor> tobject, bool checkOverlap) | 59 | void TensorMap::Insert(std::shared_ptr<LogicalTensor> tobject, bool checkOverlap) |
| 60 | { | 60 | { |
| 61 | + FE_ASSERT(tobject != nullptr) << "tobject is nullptr."; | ||
| 61 | std::vector<int> tensorShape = tensor2shape(tobject); | 62 | std::vector<int> tensorShape = tensor2shape(tobject); |
| 62 | 63 | ||
| 63 | if (checkOverlap) { | 64 | if (checkOverlap) { |
| 64 | auto match = Find(tobject); | 65 | auto match = Find(tobject); |
| 65 | if (!match.empty()) { | 66 | if (!match.empty()) { |
| 66 | - FE_LOGI("Tensor %d is full coverd in function %s", tobject->magic, belongTo.GetRawName().c_str()); | 67 | + FE_LOGI("Tensor %d is full covered in function %s", tobject->magic, belongTo.GetRawName().c_str()); |
| 67 | return; | 68 | return; |
| 68 | } | 69 | } |
| 69 | } | 70 | } |
| 70 | 71 | ||
| 72 | + FE_ASSERT(tobject->tensor != nullptr) << "tobject->tensor is nullptr."; | ||
| 71 | int rawmagic = tobject->tensor->rawmagic; | 73 | int rawmagic = tobject->tensor->rawmagic; |
| 72 | // Check if the tensor with the same rawmagic exists | 74 | // Check if the tensor with the same rawmagic exists |
| 73 | auto& tensorList = tensorMap_[rawmagic]; | 75 | auto& tensorList = tensorMap_[rawmagic]; |
| @@ -485,10 +485,16 @@ inline bool IsPtoDataDumpEnabled() | |||
| 485 | } | 485 | } |
| 486 | 486 | ||
| 487 | // 向上取整除法 | 487 | // 向上取整除法 |
| 488 | -inline int CeilDiv(int a, int b) { return (a + (b - 1)) / b; } | 488 | +inline int CeilDiv(int a, int b) { |
| 489 | + ASSERT(b != 0) << "Param(b) must not be zero."; | ||
| 490 | + return a / b + (a % b != 0 ? 1 : 0); | ||
| 491 | +} | ||
| 489 | 492 | ||
| 490 | // 向下取整除法 | 493 | // 向下取整除法 |
| 491 | -inline int FloorDiv(int a, int b) { return a / b; } | 494 | +inline int FloorDiv(int a, int b) { |
| 495 | + ASSERT(b != 0) << "Param(b) must not be zero."; | ||
| 496 | + return a / b; | ||
| 497 | +} | ||
| 492 | 498 | ||
| 493 | // 求最小值 | 499 | // 求最小值 |
| 494 | inline int Min(int a, int b) { return (a < b) ? a : b; } | 500 | inline int Min(int a, int b) { return (a < b) ? a : b; } |
| @@ -108,7 +108,7 @@ int Error::operator=(ErrorMessage& msg) | |||
| 108 | msg_ = msg.Message(); | 108 | msg_ = msg.Message(); |
| 109 | what_.Reset(); | 109 | what_.Reset(); |
| 110 | if (std::uncaught_exceptions() == 0) { | 110 | if (std::uncaught_exceptions() == 0) { |
| 111 | - PYPTO_LOGE("%s", DiagnosticWithBacktrace().c_str()); | 111 | + PYPTO_LOGE_FULL("%s", DiagnosticWithBacktrace().c_str()); |
| 112 | (void)ErrorManager::Instance().GetFirstErrorMessage(msg_); | 112 | (void)ErrorManager::Instance().GetFirstErrorMessage(msg_); |
| 113 | throw *this; | 113 | throw *this; |
| 114 | } | 114 | } |
| @@ -161,7 +161,7 @@ bool ReadBytesFromFile(const std::string& fPath, std::vector<char>& buffer) | |||
| 161 | 161 | ||
| 162 | std::ifstream ifStream(realPath.c_str(), std::ios::binary | std::ios::ate); | 162 | std::ifstream ifStream(realPath.c_str(), std::ios::binary | std::ios::ate); |
| 163 | if (!ifStream.is_open()) { | 163 | if (!ifStream.is_open()) { |
| 164 | - PYPTO_LOGW("read file %s failed.", fPath.c_str()); | 164 | + PYPTO_LOGW("Read file %s failed.", fPath.c_str()); |
| 165 | return false; | 165 | return false; |
| 166 | } | 166 | } |
| 167 | try { | 167 | try { |
| @@ -411,7 +411,7 @@ std::string GetCurRunningPath() | |||
| 411 | char buffer[size] = {}; | 411 | char buffer[size] = {}; |
| 412 | std::string cwd = getcwd(buffer, size); | 412 | std::string cwd = getcwd(buffer, size); |
| 413 | if (cwd.empty()) { | 413 | if (cwd.empty()) { |
| 414 | - PYPTO_LOGW("failed to call getcwd()"); | 414 | + PYPTO_LOGW("Failed to call getcwd()"); |
| 415 | return ""; | 415 | return ""; |
| 416 | } | 416 | } |
| 417 | return cwd; | 417 | return cwd; |
| @@ -421,7 +421,7 @@ void RemoveOldestDirs(const std::string& path, const std::string& prefix, int le | |||
| 421 | { | 421 | { |
| 422 | DIR* dir = opendir(path.c_str()); | 422 | DIR* dir = opendir(path.c_str()); |
| 423 | if (dir == nullptr) { | 423 | if (dir == nullptr) { |
| 424 | - PYPTO_LOGW("failed to opendir: %s", path.c_str()); | 424 | + PYPTO_LOGW("Failed to opendir: %s", path.c_str()); |
| 425 | return; | 425 | return; |
| 426 | } | 426 | } |
| 427 | 427 | ||
| @@ -22,7 +22,7 @@ | |||
| 22 | 22 | ||
| 23 | namespace npu::tile_fwk { | 23 | namespace npu::tile_fwk { |
| 24 | constexpr uint64_t MAIN_KEY_MASK = 0xFFFFFFFFFFFFF; | 24 | constexpr uint64_t MAIN_KEY_MASK = 0xFFFFFFFFFFFFF; |
| 25 | -constexpr uint64_t SUB_KEY_OFFSET = 52; | 25 | +constexpr uint64_t SUB_KEY_OFFSET = 52UL; |
| 26 | constexpr uint64_t SUB_KEY_MASK = 0xFFF0000000000000; | 26 | constexpr uint64_t SUB_KEY_MASK = 0xFFF0000000000000; |
| 27 | 27 | ||
| 28 | class OpInfoManager { | 28 | class OpInfoManager { |
如果 file 是空,这里要报错吧。另外这个 function.h 里面为啥会写 Csv 文件?