Merged
refactor(utils): Refactor file_utils.h #4130
lidongshengAdd on Jun 17
refactor(utils): Refactor file_utils.h #4130
Merged
Merged fromtsungl4/pypto-openrefactor/file_utils1intocann/pyptomaster
Total: 64 files changed+769-1003
| @@ -114,7 +114,6 @@ frontend@pypto: | |||
| 114 | - framework/include/tilefwk/config.h | 114 | - framework/include/tilefwk/config.h |
| 115 | - framework/include/tilefwk/data_type.h | 115 | - framework/include/tilefwk/data_type.h |
| 116 | - framework/include/tilefwk/element.h | 116 | - framework/include/tilefwk/element.h |
| 117 | - - framework/include/tilefwk/file.h | ||
| 118 | - framework/include/tilefwk/function.h | 117 | - framework/include/tilefwk/function.h |
| 119 | - framework/include/tilefwk/lazy.h | 118 | - framework/include/tilefwk/lazy.h |
| 120 | - framework/include/tilefwk/symbolic_scalar.h | 119 | - framework/include/tilefwk/symbolic_scalar.h |
| @@ -136,8 +135,6 @@ frontend@pypto: | |||
| 136 | - framework/src/interface/utils/common.h | 135 | - framework/src/interface/utils/common.h |
| 137 | - framework/src/interface/utils/error.cpp | 136 | - framework/src/interface/utils/error.cpp |
| 138 | - framework/src/interface/utils/error.h | 137 | - framework/src/interface/utils/error.h |
| 139 | - - framework/src/interface/utils/file_utils.cpp | ||
| 140 | - - framework/src/interface/utils/file_utils.h | ||
| 141 | - framework/src/interface/utils/id_gen.h | 138 | - framework/src/interface/utils/id_gen.h |
| 142 | - framework/src/interface/utils/operator_tracer.cpp | 139 | - framework/src/interface/utils/operator_tracer.cpp |
| 143 | - framework/src/interface/utils/operator_tracer.h | 140 | - framework/src/interface/utils/operator_tracer.h |
| @@ -22,7 +22,8 @@ | |||
| 22 | "framework/src/platform/" | 22 | "framework/src/platform/" |
| 23 | ], | 23 | ], |
| 24 | "Area": [ | 24 | "Area": [ |
| 25 | - "framework/src/cann_host_runtime/" | 25 | + "framework/src/cann_host_runtime/", |
| 26 | + "framework/src/utils" | ||
| 26 | ], | 27 | ], |
| 27 | "Include": [ | 28 | "Include": [ |
| 28 | "framework/include/" | 29 | "framework/include/" |
| @@ -80,7 +81,8 @@ | |||
| 80 | ], | 81 | ], |
| 81 | "Area": [ | 82 | "Area": [ |
| 82 | "framework/src/interface/", | 83 | "framework/src/interface/", |
| 83 | - "framework/src/passes/" | 84 | + "framework/src/passes/", |
| 85 | + "framework/src/utils" | ||
| 84 | ], | 86 | ], |
| 85 | "Include": [ | 87 | "Include": [ |
| 86 | "framework/include/" | 88 | "framework/include/" |
| @@ -99,7 +101,8 @@ | |||
| 99 | "Area": [ | 101 | "Area": [ |
| 100 | "framework/src/interface/", | 102 | "framework/src/interface/", |
| 101 | "framework/src/passes/", | 103 | "framework/src/passes/", |
| 102 | - "framework/src/codegen/" | 104 | + "framework/src/codegen/", |
| 105 | + "framework/src/utils" | ||
| 103 | ], | 106 | ], |
| 104 | "Include": [ | 107 | "Include": [ |
| 105 | "framework/include/" | 108 | "framework/include/" |
| @@ -121,7 +124,8 @@ | |||
| 121 | "framework/src/interface/", | 124 | "framework/src/interface/", |
| 122 | "framework/src/passes/", | 125 | "framework/src/passes/", |
| 123 | "framework/src/machine/", | 126 | "framework/src/machine/", |
| 124 | - "framework/src/cost_model/simulation" | 127 | + "framework/src/cost_model/simulation/", |
| 128 | + "framework/src/utils" | ||
| 125 | ], | 129 | ], |
| 126 | "Include": [ | 130 | "Include": [ |
| 127 | "framework/include/" | 131 | "framework/include/" |
| @@ -139,7 +143,8 @@ | |||
| 139 | ], | 143 | ], |
| 140 | "Area": [ | 144 | "Area": [ |
| 141 | "framework/src/interface/", | 145 | "framework/src/interface/", |
| 142 | - "framework/src/passes/" | 146 | + "framework/src/passes/", |
| 147 | + "framework/src/utils" | ||
| 143 | ], | 148 | ], |
| 144 | "Include": [ | 149 | "Include": [ |
| 145 | "framework/include/" | 150 | "framework/include/" |
| @@ -160,7 +165,8 @@ | |||
| 160 | "framework/src/interface/", | 165 | "framework/src/interface/", |
| 161 | "framework/src/passes/", | 166 | "framework/src/passes/", |
| 162 | "framework/src/codegen/", | 167 | "framework/src/codegen/", |
| 163 | - "framework/src/cost_model/simulation/" | 168 | + "framework/src/cost_model/simulation/", |
| 169 | + "framework/src/utils" | ||
| 164 | ], | 170 | ], |
| 165 | "Include": [ | 171 | "Include": [ |
| 166 | "framework/include/" | 172 | "framework/include/" |
| @@ -16,7 +16,7 @@ | |||
| 16 | #pragma once | 16 | #pragma once |
| 17 | 17 | ||
| 18 | #include <cstdint> | 18 | #include <cstdint> |
| 19 | -#include "file.h" | 19 | +#include <string> |
| 20 | 20 | ||
| 21 | namespace npu { | 21 | namespace npu { |
| 22 | namespace tile_fwk { | 22 | namespace tile_fwk { |
| @@ -1,60 +0,0 @@ | |||
| 1 | -/** | ||
| 2 | - * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | - * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | - * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | - * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | - * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | - * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | - */ | ||
| 10 | - | ||
| 11 | -/*! | ||
| 12 | - * \file file.h | ||
| 13 | - * \brief | ||
| 14 | - */ | ||
| 15 | - | ||
| 16 | -#pragma once | ||
| 17 | -#include <string> | ||
| 18 | -#include <limits.h> | ||
| 19 | -#include <dlfcn.h> | ||
| 20 | -#include <sys/stat.h> | ||
| 21 | -#include "pypto_fwk_log.h" | ||
| 22 | - | ||
| 23 | -namespace npu { | ||
| 24 | -namespace tile_fwk { | ||
| 25 | -inline std::string RealPath(const std::string& path) | ||
| 26 | -{ | ||
| 27 | - std::string res; | ||
| 28 | - if (path.empty()) { | ||
| 29 | - return res; | ||
| 30 | - } | ||
| 31 | - if (path.size() >= PATH_MAX) { | ||
| 32 | - FE_LOGI("File path %s is too long.", path.c_str()); | ||
| 33 | - return ""; | ||
| 34 | - } | ||
| 35 | - char resolvedPath[PATH_MAX] = {0x00}; | ||
| 36 | - if (realpath(path.c_str(), resolvedPath) == nullptr) { | ||
| 37 | - return res; | ||
| 38 | - } | ||
| 39 | - return std::string(resolvedPath); | ||
| 40 | -} | ||
| 41 | - | ||
| 42 | -inline bool FileExist(const std::string& filePath) { return !RealPath(filePath).empty(); } | ||
| 43 | - | ||
| 44 | -inline bool IsPathExist(const std::string& path) | ||
| 45 | -{ | ||
| 46 | - if (path.empty()) { | ||
| 47 | - return false; | ||
| 48 | - } | ||
| 49 | - struct stat buffer; | ||
| 50 | - return (stat(path.c_str(), &buffer) == 0); | ||
| 51 | -} | ||
| 52 | - | ||
| 53 | -inline std::string TrimLine(const std::string& s) | ||
| 54 | -{ | ||
| 55 | - size_t start = s.find_first_not_of(" \t\n\r"); | ||
| 56 | - size_t end = s.find_last_not_of(" \t\n\r"); | ||
| 57 | - return (start == std::string::npos) ? "" : s.substr(start, end - start + 1); | ||
| 58 | -} | ||
| 59 | -} // namespace tile_fwk | ||
| 60 | -} // namespace npu | ||
| @@ -22,6 +22,7 @@ target_link_libraries(tile_fwk_cann_host_runtime | |||
| 22 | dl | 22 | dl |
| 23 | c_sec | 23 | c_sec |
| 24 | tile_fwk_utils | 24 | tile_fwk_utils |
| 25 | + json | ||
| 25 | ) | 26 | ) |
| 26 | target_compile_definitions(tile_fwk_cann_host_runtime | 27 | target_compile_definitions(tile_fwk_cann_host_runtime |
| 27 | PRIVATE | 28 | PRIVATE |
| @@ -14,9 +14,11 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "tilefwk/cann_host_runtime.h" | 16 | #include "tilefwk/cann_host_runtime.h" |
| 17 | +#include "utils/file_utils.h" | ||
| 17 | #include "tilefwk/pypto_fwk_log.h" | 18 | #include "tilefwk/pypto_fwk_log.h" |
| 18 | #include <cstdlib> | 19 | #include <cstdlib> |
| 19 | #include <cstring> | 20 | #include <cstring> |
| 21 | +#include <dlfcn.h> | ||
| 20 | 22 | ||
| 21 | namespace npu { | 23 | namespace npu { |
| 22 | namespace tile_fwk { | 24 | namespace tile_fwk { |
| @@ -18,7 +18,7 @@ | |||
| 18 | #include "codegen_cce.h" | 18 | #include "codegen_cce.h" |
| 19 | #include "interface/configs/config_manager.h" | 19 | #include "interface/configs/config_manager.h" |
| 20 | #include "interface/program/program.h" | 20 | #include "interface/program/program.h" |
| 21 | -#include "interface/utils/file_utils.h" | 21 | +#include "utils/file_utils.h" |
| 22 | 22 | ||
| 23 | namespace npu::tile_fwk { | 23 | namespace npu::tile_fwk { |
| 24 | void CodeGenCCE::PrepareOutputPath() | 24 | void CodeGenCCE::PrepareOutputPath() |
| @@ -33,7 +33,7 @@ void CodeGenCCE::PrepareDefaultOutputPath() | |||
| 33 | if (ctx.IsCCEPathEmpty()) { | 33 | if (ctx.IsCCEPathEmpty()) { |
| 34 | ctx.cceDir = config::GetEmitPath("kernel_aicore"); | 34 | ctx.cceDir = config::GetEmitPath("kernel_aicore"); |
| 35 | }; | 35 | }; |
| 36 | - CreateMultiLevelDir(ctx.cceDir); | 36 | + CreateDir(ctx.cceDir, true); |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | std::map<int, int> GenRealizeIdMap(const SubfuncParam& subFuncParam) | 39 | std::map<int, int> GenRealizeIdMap(const SubfuncParam& subFuncParam) |
| @@ -20,7 +20,7 @@ | |||
| 20 | 20 | ||
| 21 | #include "codegen/utils/parallel_execute.h" | 21 | #include "codegen/utils/parallel_execute.h" |
| 22 | #include "codegen_op_npu.h" | 22 | #include "codegen_op_npu.h" |
| 23 | -#include "interface/utils/file_utils.h" | 23 | +#include "utils/file_utils.h" |
| 24 | #include "interface/tensor/logical_tensor.h" | 24 | #include "interface/tensor/logical_tensor.h" |
| 25 | #include "interface/function/function.h" | 25 | #include "interface/function/function.h" |
| 26 | #include "interface/compiler_monitor/monitor_stage_scope.h" | 26 | #include "interface/compiler_monitor/monitor_stage_scope.h" |
| @@ -358,7 +358,7 @@ bool CodeGenNPU::IsNeedDumpCode(const std::string& inputFile) const | |||
| 358 | return true; | 358 | return true; |
| 359 | } | 359 | } |
| 360 | // not force dump | 360 | // not force dump |
| 361 | - if (FileExist(inputFile)) { | 361 | + if (IsPathExist(inputFile)) { |
| 362 | return false; | 362 | return false; |
| 363 | } | 363 | } |
| 364 | return true; | 364 | return true; |
| @@ -469,7 +469,7 @@ void CodeGenNPU::CompileCode(const std::string& compileCmd) const | |||
| 469 | 469 | ||
| 470 | std::string GetIncludePathByLib() | 470 | std::string GetIncludePathByLib() |
| 471 | { | 471 | { |
| 472 | - std::string libPath = GetCurrentSharedLibPath(); | 472 | + std::string libPath = GetPyptoLibPath(); |
| 473 | if (libPath.empty()) { | 473 | if (libPath.empty()) { |
| 474 | return ""; | 474 | return ""; |
| 475 | } | 475 | } |
| @@ -17,7 +17,7 @@ | |||
| 17 | 17 | ||
| 18 | #include <iostream> | 18 | #include <iostream> |
| 19 | 19 | ||
| 20 | -#include "interface/utils/file_utils.h" | 20 | +#include "utils/file_utils.h" |
| 21 | #include "cost_model/simulation/tools/ParseArgs.h" | 21 | #include "cost_model/simulation/tools/ParseArgs.h" |
| 22 | #include "cost_model/simulation/base/ModelTop.h" | 22 | #include "cost_model/simulation/base/ModelTop.h" |
| 23 | #include "tilefwk/error_code.h" | 23 | #include "tilefwk/error_code.h" |
| @@ -25,11 +25,10 @@ | |||
| 25 | #include "cost_model/simulation/arch/PipeFactory.h" | 25 | #include "cost_model/simulation/arch/PipeFactory.h" |
| 26 | #include "cost_model/simulation/pv/PvModelFactory.h" | 26 | #include "cost_model/simulation/pv/PvModelFactory.h" |
| 27 | #include "tilefwk/error_code.h" | 27 | #include "tilefwk/error_code.h" |
| 28 | -#include "interface/utils/file_utils.h" | 28 | +#include "utils/file_utils.h" |
| 29 | #include "interface/utils/common.h" | 29 | #include "interface/utils/common.h" |
| 30 | #include "tilefwk/pypto_fwk_log.h" | 30 | #include "tilefwk/pypto_fwk_log.h" |
| 31 | #include "tilefwk/error.h" | 31 | #include "tilefwk/error.h" |
| 32 | -#include "tilefwk/file.h" | ||
| 33 | 32 | ||
| 34 | namespace { | 33 | namespace { |
| 35 | 34 | ||
| @@ -693,10 +692,10 @@ void SimSys::OutputLogForPipeSwimLane(std::string prefix) | |||
| 693 | osPipeSwim.close(); | 692 | osPipeSwim.close(); |
| 694 | 693 | ||
| 695 | SIMULATION_LOGW("Pipe SwimLane Graph Generated (PNG & HTML): %s", pipeDetailPath.c_str()); | 694 | SIMULATION_LOGW("Pipe SwimLane Graph Generated (PNG & HTML): %s", pipeDetailPath.c_str()); |
| 696 | - std::string drawScriptPath = GetCurrentSharedLibPath() + "/scripts/draw_pipe_swim_lane.py"; | 695 | + std::string drawScriptPath = GetPyptoLibPath() + "/scripts/draw_pipe_swim_lane.py"; |
| 697 | - CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::FileExist(drawScriptPath)) | 696 | + CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::IsPathExist(drawScriptPath)) |
| 698 | << "draw_pipe_swim_lane.py does not exist. drawScriptPath: " << drawScriptPath; | 697 | << "draw_pipe_swim_lane.py does not exist. drawScriptPath: " << drawScriptPath; |
| 699 | - CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::FileExist(pipeDetailPath)) | 698 | + CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::IsPathExist(pipeDetailPath)) |
| 700 | << "pipe.swim.json does not exist. pipeDetailPath: " << pipeDetailPath; | 699 | << "pipe.swim.json does not exist. pipeDetailPath: " << pipeDetailPath; |
| 701 | std::string cmd = "python3 " + drawScriptPath + " " + pipeDetailPath; | 700 | std::string cmd = "python3 " + drawScriptPath + " " + pipeDetailPath; |
| 702 | auto args = SplitString(cmd); | 701 | auto args = SplitString(cmd); |
| @@ -728,10 +727,10 @@ void SimSys::OutputLogForSwimLane(std::string prefix) | |||
| 728 | return; | 727 | return; |
| 729 | } | 728 | } |
| 730 | SIMULATION_LOGW("SwimLane Graph Generated (PNG): %s", outSwimPath.c_str()); | 729 | SIMULATION_LOGW("SwimLane Graph Generated (PNG): %s", outSwimPath.c_str()); |
| 731 | - std::string drawScriptPath = GetCurrentSharedLibPath() + "/scripts/print_swim_lane.py"; | 730 | + std::string drawScriptPath = GetPyptoLibPath() + "/scripts/print_swim_lane.py"; |
| 732 | - CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::FileExist(drawScriptPath)) | 731 | + CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::IsPathExist(drawScriptPath)) |
| 733 | << "draw_pipe_swim_lane.py does not exist. drawScriptPath: " << drawScriptPath; | 732 | << "draw_pipe_swim_lane.py does not exist. drawScriptPath: " << drawScriptPath; |
| 734 | - CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::FileExist(outSwimPath)) | 733 | + CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::IsPathExist(outSwimPath)) |
| 735 | << "swim.json does not exist. outSwimPath: " << outSwimPath; | 734 | << "swim.json does not exist. outSwimPath: " << outSwimPath; |
| 736 | std::string cmd = "python3 " + drawScriptPath + " " + outSwimPath + " -t"; | 735 | std::string cmd = "python3 " + drawScriptPath + " " + outSwimPath + " -t"; |
| 737 | auto args1 = SplitString(cmd); | 736 | auto args1 = SplitString(cmd); |
| @@ -740,7 +739,7 @@ void SimSys::OutputLogForSwimLane(std::string prefix) | |||
| 740 | SIMULATION_LOGE(CostModel::ExternalErrorScene::PYTHON_CMD_ERROR, "draw swim lane cmd error."); | 739 | SIMULATION_LOGE(CostModel::ExternalErrorScene::PYTHON_CMD_ERROR, "draw swim lane cmd error."); |
| 741 | } | 740 | } |
| 742 | 741 | ||
| 743 | - std::string mergeScriptPath = GetCurrentSharedLibPath() + "/scripts/draw_swim_lane.py"; | 742 | + std::string mergeScriptPath = GetPyptoLibPath() + "/scripts/draw_swim_lane.py"; |
| 744 | auto devicePtr = std::dynamic_pointer_cast<DeviceMachine>(machineGroup[int(MachineType::DEVICE)][0]); | 743 | auto devicePtr = std::dynamic_pointer_cast<DeviceMachine>(machineGroup[int(MachineType::DEVICE)][0]); |
| 745 | SIMULATION_LOGW("devicePtr->config.submitTopo: %d", devicePtr->config.submitTopo); | 744 | SIMULATION_LOGW("devicePtr->config.submitTopo: %d", devicePtr->config.submitTopo); |
| 746 | std::string topo_txt_path = outdir + "/../" + "dyn_topo.txt"; | 745 | std::string topo_txt_path = outdir + "/../" + "dyn_topo.txt"; |
| @@ -749,20 +748,20 @@ void SimSys::OutputLogForSwimLane(std::string prefix) | |||
| 749 | SIMULATION_LOGI("program_json_path: %s", program_json_path.c_str()); | 748 | SIMULATION_LOGI("program_json_path: %s", program_json_path.c_str()); |
| 750 | std::string label_type = "--label_type=1 --time_convert_denominator=1800"; // default 1.8GHz | 749 | std::string label_type = "--label_type=1 --time_convert_denominator=1800"; // default 1.8GHz |
| 751 | SIMULATION_LOGI("label_type: %s", label_type.c_str()); | 750 | SIMULATION_LOGI("label_type: %s", label_type.c_str()); |
| 752 | - CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::FileExist(mergeScriptPath)) | 751 | + CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::IsPathExist(mergeScriptPath)) |
| 753 | << "draw_pipe_swim_lane.py does not exist. drawScriptPath: " << mergeScriptPath; | 752 | << "draw_pipe_swim_lane.py does not exist. drawScriptPath: " << mergeScriptPath; |
| 754 | if (devicePtr->config.submitTopo) { | 753 | if (devicePtr->config.submitTopo) { |
| 755 | CHECK( | 754 | CHECK( |
| 756 | - static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::FileExist(topo_txt_path)) | 755 | + static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::IsPathExist(topo_txt_path)) |
| 757 | << "dyn_topo.txt does not exist. topo_txt_path: " << topo_txt_path; | 756 | << "dyn_topo.txt does not exist. topo_txt_path: " << topo_txt_path; |
| 758 | CHECK( | 757 | CHECK( |
| 759 | static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), | 758 | static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), |
| 760 | - npu::tile_fwk::FileExist(program_json_path)) | 759 | + npu::tile_fwk::IsPathExist(program_json_path)) |
| 761 | << "program.json does not exist. program_json_path: " << program_json_path; | 760 | << "program.json does not exist. program_json_path: " << program_json_path; |
| 762 | cmd = "python3 " + mergeScriptPath + " " + outSwimPath + " " + topo_txt_path + " " + program_json_path + " " + | 761 | cmd = "python3 " + mergeScriptPath + " " + outSwimPath + " " + topo_txt_path + " " + program_json_path + " " + |
| 763 | label_type; | 762 | label_type; |
| 764 | } else { | 763 | } else { |
| 765 | - CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::FileExist(topoOutFile)) | 764 | + CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::IsPathExist(topoOutFile)) |
| 766 | << "topo.json does not exist. topoOutFile: " << topoOutFile; | 765 | << "topo.json does not exist. topoOutFile: " << topoOutFile; |
| 767 | SIMULATION_LOGW("devicePtr->config.submitTopo: %d", devicePtr->config.submitTopo); | 766 | SIMULATION_LOGW("devicePtr->config.submitTopo: %d", devicePtr->config.submitTopo); |
| 768 | cmd = "python3 " + mergeScriptPath + " " + outSwimPath + " " + topoOutFile; | 767 | cmd = "python3 " + mergeScriptPath + " " + outSwimPath + " " + topoOutFile; |
| @@ -19,7 +19,7 @@ | |||
| 19 | #include <fstream> | 19 | #include <fstream> |
| 20 | #include <set> | 20 | #include <set> |
| 21 | 21 | ||
| 22 | -#include "interface/utils/file_utils.h" | 22 | +#include "utils/file_utils.h" |
| 23 | #include "tilefwk/pypto_fwk_log.h" | 23 | #include "tilefwk/pypto_fwk_log.h" |
| 24 | 24 | ||
| 25 | namespace CostModel { | 25 | namespace CostModel { |
| @@ -23,7 +23,7 @@ | |||
| 23 | #include <regex> | 23 | #include <regex> |
| 24 | #include <dlfcn.h> | 24 | #include <dlfcn.h> |
| 25 | #include <fstream> | 25 | #include <fstream> |
| 26 | -#include "interface/utils/file_utils.h" | 26 | +#include "utils/file_utils.h" |
| 27 | #include "cost_model/simulation/pv/PvModel.h" | 27 | #include "cost_model/simulation/pv/PvModel.h" |
| 28 | #include "cost_model/simulation/common/CommonTools.h" | 28 | #include "cost_model/simulation/common/CommonTools.h" |
| 29 | #include "codegen/npu/cloudnpu/codegen_cloudnpu.h" | 29 | #include "codegen/npu/cloudnpu/codegen_cloudnpu.h" |
| @@ -33,7 +33,6 @@ | |||
| 33 | #include "tilefwk/platform.h" | 33 | #include "tilefwk/platform.h" |
| 34 | #include "tilefwk/pypto_fwk_log.h" | 34 | #include "tilefwk/pypto_fwk_log.h" |
| 35 | #include "tilefwk/error.h" | 35 | #include "tilefwk/error.h" |
| 36 | -#include "tilefwk/file.h" | ||
| 37 | #include "tilefwk/error_code.h" | 36 | #include "tilefwk/error_code.h" |
| 38 | 37 | ||
| 39 | constexpr int INVALID_ARG_INDEX = 0xFFFFFFFF; | 38 | constexpr int INVALID_ARG_INDEX = 0xFFFFFFFF; |
| @@ -208,7 +207,7 @@ public: | |||
| 208 | { | 207 | { |
| 209 | dir_ = npu::tile_fwk::config::LogTopFolder() + "/PvModelOutput"; | 208 | dir_ = npu::tile_fwk::config::LogTopFolder() + "/PvModelOutput"; |
| 210 | if (npu::tile_fwk::IsPathExist(dir_)) { | 209 | if (npu::tile_fwk::IsPathExist(dir_)) { |
| 211 | - npu::tile_fwk::DeleteDir(dir_); | 210 | + npu::tile_fwk::DeleteDir(dir_, true); |
| 212 | } | 211 | } |
| 213 | npu::tile_fwk::CreateDir(dir_); | 212 | npu::tile_fwk::CreateDir(dir_); |
| 214 | } | 213 | } |
| @@ -321,7 +320,7 @@ public: | |||
| 321 | binPath = srcPath.substr(0, srcPath.length() - Len3) + "bin"; | 320 | binPath = srcPath.substr(0, srcPath.length() - Len3) + "bin"; |
| 322 | constexpr int cmdLen = 2048; | 321 | constexpr int cmdLen = 2048; |
| 323 | char cmd[cmdLen]; | 322 | char cmd[cmdLen]; |
| 324 | - CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::FileExist(objPath)) | 323 | + CHECK(static_cast<unsigned>(CostModel::ExternalErrorScene::INVALID_PATH), npu::tile_fwk::IsPathExist(objPath)) |
| 325 | << "obj file does not exist. objPath: " << objPath; | 324 | << "obj file does not exist. objPath: " << objPath; |
| 326 | int ret = snprintf_s( | 325 | int ret = snprintf_s( |
| 327 | cmd, sizeof(cmd), sizeof(cmd) - 1, "llvm-objcopy -O binary -j .text %s %s", objPath.c_str(), | 326 | cmd, sizeof(cmd), sizeof(cmd) - 1, "llvm-objcopy -O binary -j .text %s %s", objPath.c_str(), |
| @@ -19,7 +19,7 @@ | |||
| 19 | #include <fstream> | 19 | #include <fstream> |
| 20 | #include "tilefwk/pypto_fwk_log.h" | 20 | #include "tilefwk/pypto_fwk_log.h" |
| 21 | #include "interface/utils/common.h" | 21 | #include "interface/utils/common.h" |
| 22 | -#include "interface/utils/file_utils.h" | 22 | +#include "utils/file_utils.h" |
| 23 | #include "tilefwk/error_code.h" | 23 | #include "tilefwk/error_code.h" |
| 24 | #include "topo_processor.h" | 24 | #include "topo_processor.h" |
| 25 | #include "securec.h" | 25 | #include "securec.h" |
| @@ -99,10 +99,10 @@ std::vector<uint8_t> LoadBinData(const std::string& binPath) | |||
| 99 | { | 99 | { |
| 100 | std::vector<uint8_t> text; | 100 | std::vector<uint8_t> text; |
| 101 | 101 | ||
| 102 | - uint32_t fileSize = GetFileSize(binPath); | 102 | + uint64_t fileSize = GetFileSize(binPath); |
| 103 | std::vector<char> buf(fileSize); | 103 | std::vector<char> buf(fileSize); |
| 104 | std::ifstream file(binPath); | 104 | std::ifstream file(binPath); |
| 105 | - file.read(buf.data(), fileSize); | 105 | + file.read(buf.data(), static_cast<std::streamsize>(fileSize)); |
| 106 | 106 | ||
| 107 | auto elfHeader = reinterpret_cast<Elf64_Ehdr*>(buf.data()); | 107 | auto elfHeader = reinterpret_cast<Elf64_Ehdr*>(buf.data()); |
| 108 | if (elfHeader->e_ident[EI_MAG0] != ELFMAG0 || elfHeader->e_ident[EI_MAG1] != ELFMAG1 || | 108 | if (elfHeader->e_ident[EI_MAG0] != ELFMAG0 || elfHeader->e_ident[EI_MAG1] != ELFMAG1 || |
| @@ -24,7 +24,7 @@ | |||
| 24 | 24 | ||
| 25 | #include "tilefwk/pypto_fwk_log.h" | 25 | #include "tilefwk/pypto_fwk_log.h" |
| 26 | #include "interface/utils/common.h" | 26 | #include "interface/utils/common.h" |
| 27 | -#include "interface/utils/file_utils.h" | 27 | +#include "utils/file_utils.h" |
| 28 | #include "tilefwk/comm_group_recorder.h" | 28 | #include "tilefwk/comm_group_recorder.h" |
| 29 | #include <unistd.h> | 29 | #include <unistd.h> |
| 30 | namespace npu::tile_fwk { | 30 | namespace npu::tile_fwk { |
| @@ -69,16 +69,19 @@ Status ConfigManager::Initialize() | |||
| 69 | /* 环境变量优先生效 */ | 69 | /* 环境变量优先生效 */ |
| 70 | std::string jsonFilePath = GetEnvVar(tilefwkConfigEnvName); | 70 | std::string jsonFilePath = GetEnvVar(tilefwkConfigEnvName); |
| 71 | if (jsonFilePath.empty()) { | 71 | if (jsonFilePath.empty()) { |
| 72 | - jsonFilePath = RealPath(GetCurrentSharedLibPath() + "/configs/tile_fwk_config.json"); | 72 | + jsonFilePath = RealPath(GetPyptoLibPath() + "/configs/tile_fwk_config.json"); |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | config::SetRunDataOption(KEY_PTO_CONFIG_FILE, jsonFilePath); | 75 | config::SetRunDataOption(KEY_PTO_CONFIG_FILE, jsonFilePath); |
| 76 | config::SetRunDataOption(KEY_RUNTYPE, "npu"); | 76 | config::SetRunDataOption(KEY_RUNTYPE, "npu"); |
| 77 | FE_LOGI("Start to parse op_json_file %s", jsonFilePath.c_str()); | 77 | FE_LOGI("Start to parse op_json_file %s", jsonFilePath.c_str()); |
| 78 | - if (!ReadJsonFile(jsonFilePath, json_)) { | 78 | + std::ifstream ifs(jsonFilePath); |
| 79 | - FE_LOGE(FeError::INVALID_FILE, "ReadJsonFile failed."); | 79 | + if (!ifs.is_open()) { |
| 80 | + FE_LOGE(FeError::INVALID_FILE, "Open file %s failed.", jsonFilePath.c_str()); | ||
| 80 | return FAILED; | 81 | return FAILED; |
| 81 | } | 82 | } |
| 83 | + ifs >> json_; | ||
| 84 | + ifs.close(); | ||
| 82 | 85 | ||
| 83 | #ifdef SRCPATH | 86 | #ifdef SRCPATH |
| 84 | constexpr const char* SRC_PATH = SRCPATH; | 87 | constexpr const char* SRC_PATH = SRCPATH; |
| @@ -145,7 +148,7 @@ static std::string CreateLogTopFolder() | |||
| 145 | bool ret = CreateDir(folderPath); | 148 | bool ret = CreateDir(folderPath); |
| 146 | CHECK(FeError::BAD_FD, ret) << "Failed to create dir: " << folderPath << ", ensure its parent dir exists."; | 149 | CHECK(FeError::BAD_FD, ret) << "Failed to create dir: " << folderPath << ", ensure its parent dir exists."; |
| 147 | 150 | ||
| 148 | - folderPath = folderPath + "/output_" + timestamp.str() + "_" + std::to_string(getpid()) + "_" + GetHostName(); | 151 | + folderPath = folderPath + "/output_" + timestamp.str() + "_" + std::to_string(getpid()) + "_" + GetHostName(); |
| 149 | ret = CreateDir(folderPath); | 152 | ret = CreateDir(folderPath); |
| 150 | FE_ASSERT(FeError::BAD_FD, ret) << "Failed to create dir: " << folderPath << ", ensure its parent dir exists."; | 153 | FE_ASSERT(FeError::BAD_FD, ret) << "Failed to create dir: " << folderPath << ", ensure its parent dir exists."; |
| 151 | config::SetRunDataOption(KEY_COMPUTE_GRAPH_PATH, RealPath(folderPath)); | 154 | config::SetRunDataOption(KEY_COMPUTE_GRAPH_PATH, RealPath(folderPath)); |
| @@ -368,12 +371,12 @@ void CreateRunDataDir() | |||
| 368 | std::string envStr = GetEnvVar(ENV_VAR_PYPTO_HOME); | 371 | std::string envStr = GetEnvVar(ENV_VAR_PYPTO_HOME); |
| 369 | std::string dir = envStr.empty() ? (GetEnvVar(ENV_VAR_HOME) + "/.pypto") : envStr; | 372 | std::string dir = envStr.empty() ? (GetEnvVar(ENV_VAR_HOME) + "/.pypto") : envStr; |
| 370 | g_config.rundataDir.path = dir + "/run"; | 373 | g_config.rundataDir.path = dir + "/run"; |
| 371 | - RemoveOldestDirs(g_config.rundataDir.path, PREFIX_RUNDATA, LIMIT_DIR_NUM_BEFORE_CREATE); | 374 | + RemoveOldDirectories(g_config.rundataDir.path, PREFIX_RUNDATA, LIMIT_DIR_NUM_BEFORE_CREATE); |
| 372 | auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); | 375 | auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); |
| 373 | std::stringstream timestamp; | 376 | std::stringstream timestamp; |
| 374 | timestamp << std::put_time(std::localtime(&time), "%Y%m%d%H%M%S"); | 377 | timestamp << std::put_time(std::localtime(&time), "%Y%m%d%H%M%S"); |
| 375 | g_config.rundataDir.dName = PREFIX_RUNDATA + timestamp.str(); | 378 | g_config.rundataDir.dName = PREFIX_RUNDATA + timestamp.str(); |
| 376 | - bool res = CreateMultiLevelDir(g_config.rundataDir.montage()); | 379 | + bool res = CreateDir(g_config.rundataDir.montage(), true); |
| 377 | FE_ASSERT(FeError::BAD_FD, res) << "Failed to create directory: " << g_config.rundataDir.montage(); | 380 | FE_ASSERT(FeError::BAD_FD, res) << "Failed to create directory: " << g_config.rundataDir.montage(); |
| 378 | } | 381 | } |
| 379 | 382 | ||
| @@ -387,7 +390,7 @@ void SetRunDataOption(const std::string& key, const std::string& value) | |||
| 387 | CreateRunDataDir(); | 390 | CreateRunDataDir(); |
| 388 | } | 391 | } |
| 389 | auto filename = g_config.rundataDir.montage() + "/rundata.json"; | 392 | auto filename = g_config.rundataDir.montage() + "/rundata.json"; |
| 390 | - SaveFileSafe(filename, reinterpret_cast<uint8_t*>(dumpValue.data()), dumpValue.size()); | 393 | + SaveFileSafe(filename, dumpValue); |
| 391 | } | 394 | } |
| 392 | 395 | ||
| 393 | void SetPrintOptions(int edgeItems, int precision, int threshold, int linewidth) | 396 | void SetPrintOptions(int edgeItems, int precision, int threshold, int linewidth) |
| @@ -21,7 +21,7 @@ | |||
| 21 | #include <unistd.h> | 21 | #include <unistd.h> |
| 22 | #include <type_traits> | 22 | #include <type_traits> |
| 23 | #include <set> | 23 | #include <set> |
| 24 | -#include "interface/utils/file_utils.h" | 24 | +#include "utils/file_utils.h" |
| 25 | #include "interface/configs/config_manager_ng.h" | 25 | #include "interface/configs/config_manager_ng.h" |
| 26 | #include "tilefwk/config.h" | 26 | #include "tilefwk/config.h" |
| 27 | #include "tilefwk/function.h" | 27 | #include "tilefwk/function.h" |
| @@ -27,7 +27,7 @@ | |||
| 27 | #include <nlohmann/json.hpp> | 27 | #include <nlohmann/json.hpp> |
| 28 | 28 | ||
| 29 | #include "interface/utils/common.h" | 29 | #include "interface/utils/common.h" |
| 30 | -#include "interface/utils/file_utils.h" | 30 | +#include "utils/file_utils.h" |
| 31 | #include "interface/utils/string_utils.h" | 31 | #include "interface/utils/string_utils.h" |
| 32 | 32 | ||
| 33 | #include "config_manager_ng.h" | 33 | #include "config_manager_ng.h" |
| @@ -476,7 +476,7 @@ struct ConfigManagerImpl { | |||
| 476 | } | 476 | } |
| 477 | 477 | ||
| 478 | private: | 478 | private: |
| 479 | - std::string GetConfDir() { return GetCurrentSharedLibPath() + "/configs/"; } | 479 | + std::string GetConfDir() { return GetPyptoLibPath() + "/configs/"; } |
| 480 | 480 | ||
| 481 | void LoadConf(const nlohmann::json& jData, const std::string& prefix) | 481 | void LoadConf(const nlohmann::json& jData, const std::string& prefix) |
| 482 | { | 482 | { |
| @@ -1,18 +0,0 @@ | |||
| 1 | -/** | ||
| 2 | - * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | - * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | - * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | - * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | - * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | - * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | - */ | ||
| 10 | - | ||
| 11 | -/*! | ||
| 12 | - * \file constants.h | ||
| 13 | - * \brief | ||
| 14 | - */ | ||
| 15 | - | ||
| 16 | -#pragma once | ||
| 17 | - | ||
| 18 | -constexpr int NOT_IN_SUBGRAPH = -1; | ||
| @@ -13,7 +13,7 @@ | |||
| 13 | 13 | ||
| 14 | #include "tilefwk/error.h" | 14 | #include "tilefwk/error.h" |
| 15 | #include "interface/interpreter/interpreter_log.h" | 15 | #include "interface/interpreter/interpreter_log.h" |
| 16 | -#include "interface/utils/file_utils.h" | 16 | +#include "utils/file_utils.h" |
| 17 | #include "tilefwk/error_code.h" | 17 | #include "tilefwk/error_code.h" |
| 18 | 18 | ||
| 19 | namespace npu::tile_fwk::calc { | 19 | namespace npu::tile_fwk::calc { |
| @@ -34,8 +34,8 @@ struct CalcOps* GetCalcOps() | |||
| 34 | return; | 34 | return; |
| 35 | } | 35 | } |
| 36 | #else | 36 | #else |
| 37 | - std::string path = GetCurrentSharedLibPath() + "/libtile_fwk_calculator.so"; | 37 | + std::string path = GetPyptoLibPath() + "/libtile_fwk_calculator.so"; |
| 38 | - if (!FileExist(path)) { | 38 | + if (!IsPathExist(path)) { |
| 39 | return; | 39 | return; |
| 40 | } | 40 | } |
| 41 | auto handle = dlopen(path.c_str(), RTLD_LAZY); | 41 | auto handle = dlopen(path.c_str(), RTLD_LAZY); |
| @@ -490,7 +490,7 @@ void FlowVerifier::VerifyTensorGraph( | |||
| 490 | } | 490 | } |
| 491 | 491 | ||
| 492 | auto tensorDir = config::LogTopFolder() + "/tensor"; | 492 | auto tensorDir = config::LogTopFolder() + "/tensor"; |
| 493 | - CreateMultiLevelDir(tensorDir); | 493 | + CreateDir(tensorDir, true); |
| 494 | 494 | ||
| 495 | try { | 495 | try { |
| 496 | controlFlowExecution_ = functionInterpreter_->RunForControlFlow( | 496 | controlFlowExecution_ = functionInterpreter_->RunForControlFlow( |
| @@ -17,7 +17,7 @@ | |||
| 17 | #include "interface/interpreter/flow_verifier.h" | 17 | #include "interface/interpreter/flow_verifier.h" |
| 18 | #include "interface/interpreter/interpreter_log.h" | 18 | #include "interface/interpreter/interpreter_log.h" |
| 19 | #include "interface/configs/config_manager.h" | 19 | #include "interface/configs/config_manager.h" |
| 20 | -#include "interface/utils/file_utils.h" | 20 | +#include "utils/file_utils.h" |
| 21 | 21 | ||
| 22 | #include <chrono> | 22 | #include <chrono> |
| 23 | #include <cstdio> | 23 | #include <cstdio> |
| @@ -138,7 +138,7 @@ FunctionInterpreter::FunctionInterpreter() | |||
| 138 | dumpPath = config::LogTopFolder(); | 138 | dumpPath = config::LogTopFolder(); |
| 139 | } | 139 | } |
| 140 | dumpPath = dumpPath + "/" + "verify_" + MakeVerifyRunTimestampTag() + "/"; | 140 | dumpPath = dumpPath + "/" + "verify_" + MakeVerifyRunTimestampTag() + "/"; |
| 141 | - CreateMultiLevelDir(dumpPath); | 141 | + CreateDir(dumpPath, true); |
| 142 | interpreter::SetLogFilePath(dumpPath + "interpreter.log"); | 142 | interpreter::SetLogFilePath(dumpPath + "interpreter.log"); |
| 143 | const std::string opResultFilePath = dumpPath + "verify_graph_data_metainfo.csv"; | 143 | const std::string opResultFilePath = dumpPath + "verify_graph_data_metainfo.csv"; |
| 144 | execOpResultFile = fopen(opResultFilePath.c_str(), "w"); | 144 | execOpResultFile = fopen(opResultFilePath.c_str(), "w"); |
| @@ -313,7 +313,7 @@ void FunctionInterpreter::DumpTensorBinary( | |||
| 313 | std::shared_ptr<LogicalTensorData> FunctionInterpreter::LoadTensorBinary( | 313 | std::shared_ptr<LogicalTensorData> FunctionInterpreter::LoadTensorBinary( |
| 314 | const std::shared_ptr<LogicalTensor>& tensor, const std::string filepath) | 314 | const std::shared_ptr<LogicalTensor>& tensor, const std::string filepath) |
| 315 | { | 315 | { |
| 316 | - if (!FileExist(filepath)) { | 316 | + if (!IsPathExist(filepath)) { |
| 317 | return nullptr; | 317 | return nullptr; |
| 318 | } | 318 | } |
| 319 | std::vector<int64_t> shape = tensor->GetShape(); | 319 | std::vector<int64_t> shape = tensor->GetShape(); |
| @@ -540,7 +540,7 @@ void FunctionInterpreter::DumpBegin() | |||
| 540 | { | 540 | { |
| 541 | frameCount = 0; | 541 | frameCount = 0; |
| 542 | execDumpDir = dumpPath + execDumpFuncKey; | 542 | execDumpDir = dumpPath + execDumpFuncKey; |
| 543 | - CreateMultiLevelDir(execDumpDir); | 543 | + CreateDir(execDumpDir, true); |
| 544 | 544 | ||
| 545 | if (execDumpLevel < EXEC_DUMP_LEVEL_OPERATION) | 545 | if (execDumpLevel < EXEC_DUMP_LEVEL_OPERATION) |
| 546 | return; | 546 | return; |
| @@ -23,7 +23,7 @@ | |||
| 23 | 23 | ||
| 24 | #include "interface/operation/attribute.h" | 24 | #include "interface/operation/attribute.h" |
| 25 | #include "interface/configs/config_manager.h" | 25 | #include "interface/configs/config_manager.h" |
| 26 | -#include "interface/utils/file_utils.h" | 26 | +#include "utils/file_utils.h" |
| 27 | #include "interface/tensor/symbolic_scalar_evaluate.h" | 27 | #include "interface/tensor/symbolic_scalar_evaluate.h" |
| 28 | #include "interface/inner/pre_def.h" | 28 | #include "interface/inner/pre_def.h" |
| 29 | #include "tilefwk/data_type.h" | 29 | #include "tilefwk/data_type.h" |
| @@ -18,23 +18,10 @@ | |||
| 18 | #include <cstdint> | 18 | #include <cstdint> |
| 19 | #include <string> | 19 | #include <string> |
| 20 | #include <unordered_map> | 20 | #include <unordered_map> |
| 21 | +#include "interface/utils/enum_flags.h" | ||
| 21 | 22 | ||
| 22 | namespace npu::tile_fwk { | 23 | namespace npu::tile_fwk { |
| 23 | 24 | ||
| 24 | -template <typename TypeData, typename TypeEnum> | ||
| 25 | -struct DescData { | ||
| 26 | - DescData() = default; | ||
| 27 | - DescData(TypeData v) : data_(v) {} | ||
| 28 | - | ||
| 29 | - void Add(TypeEnum enumValue) { data_ |= (1 << static_cast<int>(enumValue)); } | ||
| 30 | - bool Count(TypeEnum enumValue) const { return data_ & (1 << static_cast<int>(enumValue)); } | ||
| 31 | - DescData operator&(DescData<TypeData, TypeEnum> other) const { return DescData(data_ & other.data_); } | ||
| 32 | - DescData operator|(DescData<TypeData, TypeEnum> other) const { return DescData(data_ | other.data_); } | ||
| 33 | - | ||
| 34 | -private: | ||
| 35 | - TypeData data_{0}; | ||
| 36 | -}; | ||
| 37 | - | ||
| 38 | enum class RuntimeSlotKind : int { | 25 | enum class RuntimeSlotKind : int { |
| 39 | INNER, | 26 | INNER, |
| 40 | EXCLUSIVE_OUTCAST, | 27 | EXCLUSIVE_OUTCAST, |
| @@ -45,7 +32,7 @@ enum class RuntimeSlotKind : int { | |||
| 45 | ADDRESS_EXPRESSION, // For shared memory tensor address expression | 32 | ADDRESS_EXPRESSION, // For shared memory tensor address expression |
| 46 | }; | 33 | }; |
| 47 | 34 | ||
| 48 | -using RuntimeSlotKindSet = DescData<uint32_t, RuntimeSlotKind>; | 35 | +using RuntimeSlotKindSet = EnumFlags<RuntimeSlotKind>; |
| 49 | 36 | ||
| 50 | struct RuntimeSlotDesc { | 37 | struct RuntimeSlotDesc { |
| 51 | RuntimeSlotKind kind; | 38 | RuntimeSlotKind kind; |
| @@ -19,7 +19,7 @@ | |||
| 19 | #include <sstream> | 19 | #include <sstream> |
| 20 | #include <numeric> | 20 | #include <numeric> |
| 21 | #include <functional> | 21 | #include <functional> |
| 22 | -#include "interface/utils/file_utils.h" | 22 | +#include "utils/file_utils.h" |
| 23 | #include "interface/utils/common.h" | 23 | #include "interface/utils/common.h" |
| 24 | #include "tilefwk/pypto_fwk_log.h" | 24 | #include "tilefwk/pypto_fwk_log.h" |
| 25 | #include "symbolic_scalar_simplify.h" | 25 | #include "symbolic_scalar_simplify.h" |
| @@ -77,7 +77,7 @@ std::string CompileSourceCode(const std::string& sourceFilePath, const std::stri | |||
| 77 | { | 77 | { |
| 78 | std::string assembleFilePath = sourceFilePath + ".s"; | 78 | std::string assembleFilePath = sourceFilePath + ".s"; |
| 79 | std::string objectFilePath = sourceFilePath + "_t.o"; | 79 | std::string objectFilePath = sourceFilePath + "_t.o"; |
| 80 | - std::string includePath = GetCurrentSharedLibPath() + "/../include/tile_fwk"; | 80 | + std::string includePath = GetPyptoLibPath() + "/../include/tile_fwk"; |
| 81 | std::string macro = extraCflag.empty() ? "-D__DEVICE__" : ""; | 81 | std::string macro = extraCflag.empty() ? "-D__DEVICE__" : ""; |
| 82 | 82 | ||
| 83 | std::vector<std::string> argsGcc = {gcc, "-fPIC", "-fno-stack-protector", "-O2"}; | 83 | std::vector<std::string> argsGcc = {gcc, "-fPIC", "-fno-stack-protector", "-O2"}; |
| @@ -86,7 +86,7 @@ std::string CompileSourceCode(const std::string& sourceFilePath, const std::stri | |||
| 86 | if (!macro.empty()) { | 86 | if (!macro.empty()) { |
| 87 | argsGcc.push_back(macro); | 87 | argsGcc.push_back(macro); |
| 88 | } | 88 | } |
| 89 | - argsGcc.insert(argsGcc.end(), {"-I" + includePath, "-I" + GetCurrentSharedLibPath() + "/include/", | 89 | + argsGcc.insert(argsGcc.end(), {"-I" + includePath, "-I" + GetPyptoLibPath() + "/include/", |
| 90 | "-I" + includePath + "/tilefwk", "-S", sourceFilePath, "-o", assembleFilePath}); | 90 | "-I" + includePath + "/tilefwk", "-S", sourceFilePath, "-o", assembleFilePath}); |
| 91 | 91 | ||
| 92 | FE_LOGI("[RunCmd] %s", std::accumulate(argsGcc.begin(), argsGcc.end(), std::string(), | 92 | FE_LOGI("[RunCmd] %s", std::accumulate(argsGcc.begin(), argsGcc.end(), std::string(), |
| @@ -0,0 +1,85 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file enum_flags.h | ||
| 13 | + * \brief A robust bitset keyed by a scoped enum. | ||
| 14 | + */ | ||
| 15 | +#pragma once | ||
| 16 | + | ||
| 17 | +#include <cstdint> | ||
| 18 | +#include <type_traits> | ||
| 19 | + | ||
| 20 | +namespace npu::tile_fwk { | ||
| 21 | + | ||
| 22 | +// EnumFlags is a set of enumerators of E stored as a bitmask. | ||
| 23 | +// | ||
| 24 | +// Each enumerator e occupies bit static_cast<storage_type>(e). Enumerators whose | ||
| 25 | +// underlying value is negative or not less than the bit width of Storage are | ||
| 26 | +// mapped to a zero mask instead of triggering undefined behaviour, so the type | ||
| 27 | +// stays well-defined for any enumerator value. Storage must be an unsigned | ||
| 28 | +// integral type (defaults to uint32_t). | ||
| 29 | +template <typename E, typename Storage = std::uint32_t> | ||
| 30 | +class EnumFlags { | ||
| 31 | + static_assert(std::is_enum<E>::value, "EnumFlags: E must be an enum type"); | ||
| 32 | + static_assert(std::is_unsigned<Storage>::value && std::is_integral<Storage>::value, | ||
| 33 | + "EnumFlags: Storage must be an unsigned integral type"); | ||
| 34 | + | ||
| 35 | +public: | ||
| 36 | + using enum_type = E; | ||
| 37 | + using storage_type = Storage; | ||
| 38 | + | ||
| 39 | + constexpr EnumFlags() noexcept = default; | ||
| 40 | + constexpr explicit EnumFlags(storage_type bits) noexcept : bits_(bits) {} | ||
| 41 | + // Single-flag construction, e.g. EnumFlags<E>{E::A}. | ||
| 42 | + constexpr EnumFlags(E e) noexcept : bits_(Bit(e)) {} | ||
| 43 | + | ||
| 44 | + constexpr EnumFlags& Add(E e) noexcept { bits_ |= Bit(e); return *this; } | ||
| 45 | + constexpr EnumFlags& Remove(E e) noexcept { bits_ &= ~Bit(e); return *this; } | ||
| 46 | + constexpr bool Contains(E e) const noexcept { return (bits_ & Bit(e)) != storage_type{0}; } | ||
| 47 | + | ||
| 48 | + constexpr bool Empty() const noexcept { return bits_ == storage_type{0}; } | ||
| 49 | + constexpr bool Any() const noexcept { return bits_ != storage_type{0}; } | ||
| 50 | + constexpr bool Overlaps(EnumFlags other) const noexcept { return (bits_ & other.bits_) != storage_type{0}; } | ||
| 51 | + constexpr bool ContainsAll(EnumFlags other) const noexcept { return (bits_ & other.bits_) == other.bits_; } | ||
| 52 | + | ||
| 53 | + constexpr storage_type Value() const noexcept { return bits_; } | ||
| 54 | + | ||
| 55 | + constexpr EnumFlags operator|(EnumFlags rhs) const noexcept { return EnumFlags(bits_ | rhs.bits_, internal{}); } | ||
| 56 | + constexpr EnumFlags operator&(EnumFlags rhs) const noexcept { return EnumFlags(bits_ & rhs.bits_, internal{}); } | ||
| 57 | + constexpr EnumFlags operator^(EnumFlags rhs) const noexcept { return EnumFlags(bits_ ^ rhs.bits_, internal{}); } | ||
| 58 | + constexpr EnumFlags operator~() const noexcept { return EnumFlags(~bits_, internal{}); } | ||
| 59 | + constexpr EnumFlags& operator|=(EnumFlags rhs) noexcept { bits_ |= rhs.bits_; return *this; } | ||
| 60 | + constexpr EnumFlags& operator&=(EnumFlags rhs) noexcept { bits_ &= rhs.bits_; return *this; } | ||
| 61 | + constexpr EnumFlags& operator^=(EnumFlags rhs) noexcept { bits_ ^= rhs.bits_; return *this; } | ||
| 62 | + | ||
| 63 | + friend constexpr bool operator==(EnumFlags lhs, EnumFlags rhs) noexcept { return lhs.bits_ == rhs.bits_; } | ||
| 64 | + friend constexpr bool operator!=(EnumFlags lhs, EnumFlags rhs) noexcept { return lhs.bits_ != rhs.bits_; } | ||
| 65 | + | ||
| 66 | +private: | ||
| 67 | + struct internal {}; | ||
| 68 | + constexpr EnumFlags(storage_type bits, internal) noexcept : bits_(bits) {} | ||
| 69 | + | ||
| 70 | + using Underlying = typename std::underlying_type<E>::type; | ||
| 71 | + using UnsignedUnderlying = typename std::make_unsigned<Underlying>::type; | ||
| 72 | + | ||
| 73 | + static constexpr Storage Bit(E e) noexcept | ||
| 74 | + { | ||
| 75 | + // Cast through the unsigned underlying type so signed enums are handled, | ||
| 76 | + // then widen to uint64_t to test against the Storage bit width safely. | ||
| 77 | + const std::uint64_t raw = static_cast<std::uint64_t>(static_cast<UnsignedUnderlying>(static_cast<Underlying>(e))); | ||
| 78 | + constexpr std::uint64_t width = sizeof(Storage) * 8ULL; | ||
| 79 | + return (raw >= width) ? storage_type{0} : static_cast<storage_type>(storage_type{1} << raw); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + storage_type bits_{0}; | ||
| 83 | +}; | ||
| 84 | + | ||
| 85 | +} // namespace npu::tile_fwk | ||
| @@ -1,451 +0,0 @@ | |||
| 1 | -/** | ||
| 2 | - * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | - * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | - * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | - * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | - * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | - * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | - */ | ||
| 10 | - | ||
| 11 | -/*! | ||
| 12 | - * \file file_utils.cpp | ||
| 13 | - * \brief | ||
| 14 | - */ | ||
| 15 | - | ||
| 16 | -#include "interface/utils/file_utils.h" | ||
| 17 | -#include <fstream> | ||
| 18 | -#include <fcntl.h> | ||
| 19 | -#include <climits> | ||
| 20 | -#include <unistd.h> | ||
| 21 | -#include <sys/stat.h> | ||
| 22 | -#include <dirent.h> | ||
| 23 | -#include <dlfcn.h> | ||
| 24 | -#include <ftw.h> | ||
| 25 | -#include "tilefwk/pypto_fwk_log.h" | ||
| 26 | - | ||
| 27 | -namespace npu::tile_fwk { | ||
| 28 | -namespace { | ||
| 29 | -const int FILE_AUTHORITY = 0640; | ||
| 30 | -} | ||
| 31 | - | ||
| 32 | -bool GetFileSize(const std::string& fPath, uint32_t& fileSize) | ||
| 33 | -{ | ||
| 34 | - if (RealPath(fPath).empty()) { | ||
| 35 | - return false; | ||
| 36 | - } | ||
| 37 | - std::ifstream file(fPath, std::ios::binary | std::ios::ate); // 打开文件,定位到文件末尾 | ||
| 38 | - if (!file.is_open()) { | ||
| 39 | - return false; | ||
| 40 | - } | ||
| 41 | - fileSize = file.tellg(); | ||
| 42 | - file.close(); | ||
| 43 | - return true; | ||
| 44 | -} | ||
| 45 | - | ||
| 46 | -uint32_t GetFileSize(const std::string& fPath) | ||
| 47 | -{ | ||
| 48 | - uint32_t fileSize = 0; | ||
| 49 | - (void)GetFileSize(fPath, fileSize); | ||
| 50 | - return fileSize; | ||
| 51 | -} | ||
| 52 | - | ||
| 53 | -bool CreateDir(const std::string& directoryPath) | ||
| 54 | -{ | ||
| 55 | - int32_t ret = mkdir(directoryPath.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); // 755 | ||
| 56 | - if (ret != 0 && errno != EEXIST) { | ||
| 57 | - PYPTO_LOGW("Create dir[%s] failed, reason is %s", directoryPath.c_str(), strerror(errno)); | ||
| 58 | - return false; | ||
| 59 | - } | ||
| 60 | - return true; | ||
| 61 | -} | ||
| 62 | - | ||
| 63 | -bool JudgeEmptyAndCreateDir(char tmpDirPath[], const std::string& directoryPath) | ||
| 64 | -{ | ||
| 65 | - std::string realPath = RealPath(tmpDirPath); | ||
| 66 | - if (realPath.empty()) { | ||
| 67 | - int32_t ret = 0; | ||
| 68 | - ret = mkdir(tmpDirPath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); // 755 | ||
| 69 | - if (ret != 0 && errno != EEXIST) { | ||
| 70 | - PYPTO_LOGW("Create dir[%s] failed, reason is %s", directoryPath.c_str(), strerror(errno)); | ||
| 71 | - return false; | ||
| 72 | - } | ||
| 73 | - } | ||
| 74 | - return true; | ||
| 75 | -} | ||
| 76 | - | ||
| 77 | -bool CreateMultiLevelDir(const std::string& directoryPath) | ||
| 78 | -{ | ||
| 79 | - auto dirPathLen = directoryPath.length(); | ||
| 80 | - if (dirPathLen >= PATH_MAX) { | ||
| 81 | - PYPTO_LOGW("Path[%s] is too long, it must be less than %d", directoryPath.c_str(), PATH_MAX); | ||
| 82 | - return false; | ||
| 83 | - } | ||
| 84 | - char tmpDirPath[PATH_MAX] = {0}; | ||
| 85 | - int32_t ret; | ||
| 86 | - for (size_t i = 0; i < dirPathLen; ++i) { | ||
| 87 | - tmpDirPath[i] = directoryPath[i]; | ||
| 88 | - if ((tmpDirPath[i] == '\\') || (tmpDirPath[i] == '/')) { | ||
| 89 | - if (access(tmpDirPath, F_OK) == 0) { | ||
| 90 | - continue; | ||
| 91 | - } | ||
| 92 | - if (!JudgeEmptyAndCreateDir(tmpDirPath, directoryPath)) { | ||
| 93 | - return false; | ||
| 94 | - } | ||
| 95 | - } | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | - ret = mkdir(directoryPath.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); // 755 | ||
| 99 | - if (ret != 0 && errno != EEXIST) { | ||
| 100 | - PYPTO_LOGW("Create dir[%s] failed, reason is: %s", directoryPath.c_str(), strerror(errno)); | ||
| 101 | - return false; | ||
| 102 | - } | ||
| 103 | - | ||
| 104 | - PYPTO_LOGD("Create multi level dir [%s] successfully.", directoryPath.c_str()); | ||
| 105 | - return true; | ||
| 106 | -} | ||
| 107 | - | ||
| 108 | -void DeleteFile(const std::string& path) | ||
| 109 | -{ | ||
| 110 | - if (path.empty()) { | ||
| 111 | - PYPTO_LOGW("File name is empty."); | ||
| 112 | - return; | ||
| 113 | - } | ||
| 114 | - struct stat statBuf; | ||
| 115 | - if (lstat(path.c_str(), &statBuf) != 0) { | ||
| 116 | - PYPTO_LOGW("Stat file[%s] failed.", path.c_str()); | ||
| 117 | - return; | ||
| 118 | - } | ||
| 119 | - if (S_ISREG(statBuf.st_mode) == 0) { | ||
| 120 | - PYPTO_LOGW("[%s] is not a file.", path.c_str()); | ||
| 121 | - return; | ||
| 122 | - } | ||
| 123 | - int res = remove(path.c_str()); | ||
| 124 | - if (res != 0) { | ||
| 125 | - PYPTO_LOGW("Delete file[%s] failed.", path.c_str()); | ||
| 126 | - } | ||
| 127 | -} | ||
| 128 | - | ||
| 129 | -bool ReadJsonFile(const std::string& file, nlohmann::json& jsonObj) | ||
| 130 | -{ | ||
| 131 | - std::string path = RealPath(file); | ||
| 132 | - if (path.empty()) { | ||
| 133 | - PYPTO_LOGW("File path [%s] does not exist", file.c_str()); | ||
| 134 | - return false; | ||
| 135 | - } | ||
| 136 | - std::ifstream ifStream(path); | ||
| 137 | - try { | ||
| 138 | - if (!ifStream.is_open()) { | ||
| 139 | - PYPTO_LOGW("Open %s failed, file is already open", file.c_str()); | ||
| 140 | - return false; | ||
| 141 | - } | ||
| 142 | - | ||
| 143 | - ifStream >> jsonObj; | ||
| 144 | - ifStream.close(); | ||
| 145 | - } catch (const std::exception& e) { | ||
| 146 | - PYPTO_LOGW("Fail to convert file[%s] to Json. Exception message is .%s", path.c_str(), e.what()); | ||
| 147 | - ifStream.close(); | ||
| 148 | - return false; | ||
| 149 | - } | ||
| 150 | - | ||
| 151 | - return true; | ||
| 152 | -} | ||
| 153 | - | ||
| 154 | -bool ReadBytesFromFile(const std::string& fPath, std::vector<char>& buffer) | ||
| 155 | -{ | ||
| 156 | - std::string realPath = RealPath(fPath); | ||
| 157 | - if (realPath.empty()) { | ||
| 158 | - PYPTO_LOGW("Bin file path[%s] is not valid.", fPath.c_str()); | ||
| 159 | - return false; | ||
| 160 | - } | ||
| 161 | - | ||
| 162 | - std::ifstream ifStream(realPath.c_str(), std::ios::binary | std::ios::ate); | ||
| 163 | - if (!ifStream.is_open()) { | ||
| 164 | - PYPTO_LOGW("Read file %s failed.", fPath.c_str()); | ||
| 165 | - return false; | ||
| 166 | - } | ||
| 167 | - try { | ||
| 168 | - std::streamsize size = ifStream.tellg(); | ||
| 169 | - if (size <= 0 || size > INT_MAX) { | ||
| 170 | - ifStream.close(); | ||
| 171 | - PYPTO_LOGW("File size %ld is not within the range: (0, %d].", size, INT_MAX); | ||
| 172 | - return false; | ||
| 173 | - } | ||
| 174 | - | ||
| 175 | - ifStream.seekg(0, std::ios::beg); | ||
| 176 | - | ||
| 177 | - buffer.resize(size); | ||
| 178 | - ifStream.read(&buffer[0], size); | ||
| 179 | - PYPTO_LOGD("Release file(%s) handle.", realPath.c_str()); | ||
| 180 | - ifStream.close(); | ||
| 181 | - PYPTO_LOGD("Read size: %ld.", size); | ||
| 182 | - } catch (const std::ifstream::failure& e) { | ||
| 183 | - PYPTO_LOGW("Fail to read file %s. Exception: %s.", fPath.c_str(), e.what()); | ||
| 184 | - ifStream.close(); | ||
| 185 | - return false; | ||
| 186 | - } | ||
| 187 | - return true; | ||
| 188 | -} | ||
| 189 | - | ||
| 190 | -std::vector<std::string> GetFiles(const std::string& path, const std::string& ext) | ||
| 191 | -{ | ||
| 192 | - std::vector<std::string> files; | ||
| 193 | - DIR* dir = opendir(path.c_str()); | ||
| 194 | - if (dir == nullptr) { | ||
| 195 | - PYPTO_LOGW("Open directory [%s] failed", path.c_str()); | ||
| 196 | - return files; | ||
| 197 | - } | ||
| 198 | - | ||
| 199 | - struct dirent* ent; | ||
| 200 | - while ((ent = readdir(dir)) != nullptr) { | ||
| 201 | - std::string fileName = ent->d_name; | ||
| 202 | - if (fileName == "." || fileName == "..") { | ||
| 203 | - continue; | ||
| 204 | - } | ||
| 205 | - | ||
| 206 | - // 检查文件扩展名 | ||
| 207 | - if (!ext.empty()) { | ||
| 208 | - size_t pos = fileName.rfind('.'); | ||
| 209 | - if (pos == std::string::npos) { | ||
| 210 | - continue; // 没有扩展名 | ||
| 211 | - } | ||
| 212 | - std::string fileExt = fileName.substr(pos + 1); | ||
| 213 | - // 转换为小写进行比较 | ||
| 214 | - std::transform(fileExt.begin(), fileExt.end(), fileExt.begin(), ::tolower); | ||
| 215 | - std::string targetExt = ext; | ||
| 216 | - std::transform(targetExt.begin(), targetExt.end(), targetExt.begin(), ::tolower); | ||
| 217 | - if (fileExt != targetExt) { | ||
| 218 | - continue; | ||
| 219 | - } | ||
| 220 | - } | ||
| 221 | - | ||
| 222 | - files.push_back(fileName); | ||
| 223 | - } | ||
| 224 | - closedir(dir); | ||
| 225 | - | ||
| 226 | - std::sort(files.begin(), files.end()); | ||
| 227 | - return files; | ||
| 228 | -} | ||
| 229 | - | ||
| 230 | -void SaveFile(const std::string& fPath, const std::vector<uint8_t>& data) | ||
| 231 | -{ | ||
| 232 | - FILE* file = fopen(fPath.c_str(), "wb"); | ||
| 233 | - if (file == nullptr) { | ||
| 234 | - PYPTO_LOGW("Open file [%s] failed.", fPath.c_str()); | ||
| 235 | - return; | ||
| 236 | - } | ||
| 237 | - fwrite(data.data(), 1, data.size(), file); | ||
| 238 | - fclose(file); | ||
| 239 | -} | ||
| 240 | - | ||
| 241 | -bool SaveFile(const std::string& fPath, const uint8_t* data, size_t size) | ||
| 242 | -{ | ||
| 243 | - FILE* file = fopen(fPath.c_str(), "wb"); | ||
| 244 | - if (file == nullptr) { | ||
| 245 | - PYPTO_LOGW("Open file [%s] failed.", fPath.c_str()); | ||
| 246 | - return false; | ||
| 247 | - } | ||
| 248 | - fwrite(data, 1, size, file); | ||
| 249 | - fclose(file); | ||
| 250 | - return true; | ||
| 251 | -} | ||
| 252 | - | ||
| 253 | -void SaveFileSafe(const std::string& fPath, const uint8_t* data, size_t size) | ||
| 254 | -{ | ||
| 255 | - auto tmpfile = fPath + ".tmp"; | ||
| 256 | - if (SaveFile(tmpfile, data, size)) { | ||
| 257 | - Rename(tmpfile, fPath); | ||
| 258 | - } | ||
| 259 | -} | ||
| 260 | - | ||
| 261 | -void Rename(const std::string& oldPath, const std::string& newPath) | ||
| 262 | -{ | ||
| 263 | - if (rename(oldPath.c_str(), newPath.c_str()) != 0) { | ||
| 264 | - PYPTO_LOGW("Rename file %s to %s failed.", oldPath.c_str(), newPath.c_str()); | ||
| 265 | - } | ||
| 266 | -} | ||
| 267 | - | ||
| 268 | -bool DumpFile(const char* data, const size_t size, const std::string& fPath) | ||
| 269 | -{ | ||
| 270 | - // dump bin file | ||
| 271 | - std::ofstream outFile(fPath, std::ios::binary); | ||
| 272 | - if (!outFile) { | ||
| 273 | - PYPTO_LOGE("Failed open file %s.", fPath.c_str()); | ||
| 274 | - return false; | ||
| 275 | - } | ||
| 276 | - outFile.write(data, size); | ||
| 277 | - outFile.close(); | ||
| 278 | - PYPTO_LOGI("Bin file[%s] has been dumped.", fPath.c_str()); | ||
| 279 | - return true; | ||
| 280 | -} | ||
| 281 | - | ||
| 282 | -bool DumpFile(const std::vector<uint8_t>& data, const std::string& fPath) | ||
| 283 | -{ | ||
| 284 | - return DumpFile(reinterpret_cast<const char*>(data.data()), data.size(), fPath); | ||
| 285 | -} | ||
| 286 | - | ||
| 287 | -bool DumpFile(const std::string& text, const std::string& fPath) { return DumpFile(text.data(), text.size(), fPath); } | ||
| 288 | - | ||
| 289 | -std::vector<uint8_t> LoadFile(const std::string& fPath) | ||
| 290 | -{ | ||
| 291 | - std::vector<uint8_t> binary; | ||
| 292 | - std::string realPath = RealPath(fPath); | ||
| 293 | - if (realPath.empty()) { | ||
| 294 | - PYPTO_LOGW("Bin file path[%s] is not valid.", fPath.c_str()); | ||
| 295 | - return binary; | ||
| 296 | - } | ||
| 297 | - | ||
| 298 | - FILE* file = fopen(fPath.c_str(), "rb"); | ||
| 299 | - if (file != nullptr) { | ||
| 300 | - fseek(file, 0, SEEK_END); | ||
| 301 | - int size = ftell(file); | ||
| 302 | - binary.resize(size); | ||
| 303 | - fseek(file, 0, SEEK_SET); | ||
| 304 | - size_t readSize = fread(binary.data(), 1, size, file); | ||
| 305 | - if (readSize != static_cast<size_t>(size)) { | ||
| 306 | - binary.clear(); | ||
| 307 | - } | ||
| 308 | - fclose(file); | ||
| 309 | - } | ||
| 310 | - return binary; | ||
| 311 | -} | ||
| 312 | - | ||
| 313 | -static int RemoveFile(const char* path, const struct stat* sb, int flag, struct FTW* ftwbuf) | ||
| 314 | -{ | ||
| 315 | - (void)sb; | ||
| 316 | - (void)ftwbuf; | ||
| 317 | - if (flag == FTW_F) { | ||
| 318 | - return remove(path); | ||
| 319 | - } else if (flag == FTW_DP) { | ||
| 320 | - return rmdir(path); | ||
| 321 | - } | ||
| 322 | - return 0; | ||
| 323 | -} | ||
| 324 | - | ||
| 325 | -bool DeleteDir(const std::string& directoryPath) | ||
| 326 | -{ | ||
| 327 | - constexpr int limit = 64; | ||
| 328 | - int ret = nftw(directoryPath.c_str(), RemoveFile, limit, FTW_DEPTH | FTW_PHYS); | ||
| 329 | - if (ret != 0) { | ||
| 330 | - PYPTO_LOGW("Delete dir[%s] failed, reason is %d", directoryPath.c_str(), ret); | ||
| 331 | - return false; | ||
| 332 | - } | ||
| 333 | - return true; | ||
| 334 | -} | ||
| 335 | - | ||
| 336 | -bool FcntlLockFile(const int fd, const int type) | ||
| 337 | -{ | ||
| 338 | - struct flock lock; | ||
| 339 | - lock.l_whence = SEEK_SET; | ||
| 340 | - lock.l_start = 0; | ||
| 341 | - lock.l_len = 0; | ||
| 342 | - lock.l_type = type; | ||
| 343 | - | ||
| 344 | - // lock or unlock | ||
| 345 | - return fcntl(fd, F_SETLK, &lock) == 0; | ||
| 346 | -} | ||
| 347 | - | ||
| 348 | -FILE* LockAndOpenFile(const std::string& lockFilePath) | ||
| 349 | -{ | ||
| 350 | - FILE* fp = fopen(lockFilePath.c_str(), "a+"); | ||
| 351 | - if (fp == nullptr) { | ||
| 352 | - return nullptr; | ||
| 353 | - } | ||
| 354 | - (void)chmod(lockFilePath.c_str(), FILE_AUTHORITY); | ||
| 355 | - if (!FcntlLockFile(fileno(fp), F_WRLCK)) { | ||
| 356 | - PYPTO_LOGW("Fail to lock file: %s", lockFilePath.c_str()); | ||
| 357 | - fclose(fp); | ||
| 358 | - return nullptr; | ||
| 359 | - } | ||
| 360 | - PYPTO_LOGI("Lock file successfully. %s", lockFilePath.c_str()); | ||
| 361 | - return fp; | ||
| 362 | -} | ||
| 363 | - | ||
| 364 | -void UnlockAndCloseFile(FILE* fp) | ||
| 365 | -{ | ||
| 366 | - if (fp == nullptr) { | ||
| 367 | - return; | ||
| 368 | - } | ||
| 369 | - (void)FcntlLockFile(fileno(fp), F_UNLCK); | ||
| 370 | - fclose(fp); | ||
| 371 | - fp = nullptr; | ||
| 372 | -} | ||
| 373 | - | ||
| 374 | -bool CopyFile(const std::string& srcPath, const std::string& dstPath) | ||
| 375 | -{ | ||
| 376 | - std::ifstream src(srcPath, std::ios::binary); | ||
| 377 | - std::ofstream dst(dstPath, std::ios::binary); | ||
| 378 | - | ||
| 379 | - if (!src.is_open() || !dst.is_open()) { | ||
| 380 | - PYPTO_LOGW("Fail to open file: %s, %s", srcPath.c_str(), dstPath.c_str()); | ||
| 381 | - return false; | ||
| 382 | - } | ||
| 383 | - | ||
| 384 | - dst << src.rdbuf(); | ||
| 385 | - src.close(); | ||
| 386 | - dst.close(); | ||
| 387 | - return true; | ||
| 388 | -} | ||
| 389 | - | ||
| 390 | -std::string GetCurrentSharedLibPath() | ||
| 391 | -{ | ||
| 392 | - static std::string currentLibPath; | ||
| 393 | - if (!currentLibPath.empty()) { | ||
| 394 | - return currentLibPath; | ||
| 395 | - } | ||
| 396 | - | ||
| 397 | - Dl_info info; | ||
| 398 | - if (dladdr(reinterpret_cast<void*>(GetCurrentSharedLibPath), &info)) { | ||
| 399 | - currentLibPath = std::string(info.dli_fname); | ||
| 400 | - int32_t pos = currentLibPath.rfind('/'); | ||
| 401 | - if (pos >= 0) { | ||
| 402 | - currentLibPath = currentLibPath.substr(0, pos); | ||
| 403 | - } | ||
| 404 | - } | ||
| 405 | - return currentLibPath; | ||
| 406 | -} | ||
| 407 | - | ||
| 408 | -std::string GetCurRunningPath() | ||
| 409 | -{ | ||
| 410 | - constexpr size_t size = 1024; | ||
| 411 | - char buffer[size] = {}; | ||
| 412 | - std::string cwd = getcwd(buffer, size); | ||
| 413 | - if (cwd.empty()) { | ||
| 414 | - PYPTO_LOGW("Failed to call getcwd()"); | ||
| 415 | - return ""; | ||
| 416 | - } | ||
| 417 | - return cwd; | ||
| 418 | -} | ||
| 419 | - | ||
| 420 | -void RemoveOldestDirs(const std::string& path, const std::string& prefix, int left) | ||
| 421 | -{ | ||
| 422 | - DIR* dir = opendir(path.c_str()); | ||
| 423 | - if (dir == nullptr) { | ||
| 424 | - PYPTO_LOGW("Failed to opendir: %s", path.c_str()); | ||
| 425 | - return; | ||
| 426 | - } | ||
| 427 | - | ||
| 428 | - int32_t dirNum{0}; | ||
| 429 | - struct dirent* entry; | ||
| 430 | - std::map<long long, std::string, std::less<>> timeList; | ||
| 431 | - while ((entry = readdir(dir)) != nullptr) { | ||
| 432 | - if (strncmp(entry->d_name, prefix.c_str(), prefix.size()) != 0) { | ||
| 433 | - continue; | ||
| 434 | - } | ||
| 435 | - | ||
| 436 | - std::string dirName(entry->d_name); | ||
| 437 | - std::string fullPath = path + "/" + dirName; | ||
| 438 | - struct stat statBuf; | ||
| 439 | - if (stat(fullPath.c_str(), &statBuf) == 0 && S_ISDIR(statBuf.st_mode)) { | ||
| 440 | - ++dirNum; | ||
| 441 | - long long tmpTime = static_cast<long long>(statBuf.st_mtime); | ||
| 442 | - timeList[tmpTime] = fullPath; | ||
| 443 | - } | ||
| 444 | - } | ||
| 445 | - closedir(dir); | ||
| 446 | - | ||
| 447 | - for (auto it = timeList.begin(); dirNum > left && it != timeList.end(); --dirNum, ++it) { | ||
| 448 | - DeleteDir(it->second); | ||
| 449 | - } | ||
| 450 | -} | ||
| 451 | -} // namespace npu::tile_fwk | ||
| @@ -1,47 +0,0 @@ | |||
| 1 | -/** | ||
| 2 | - * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | - * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | - * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | - * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | - * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | - * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | - */ | ||
| 10 | - | ||
| 11 | -/*! | ||
| 12 | - * \file file_utils.h | ||
| 13 | - * \brief | ||
| 14 | - */ | ||
| 15 | - | ||
| 16 | -#pragma once | ||
| 17 | - | ||
| 18 | -#include <nlohmann/json.hpp> | ||
| 19 | -#include <string> | ||
| 20 | -#include "tilefwk/error_code.h" | ||
| 21 | -#include "tilefwk/file.h" | ||
| 22 | - | ||
| 23 | -namespace npu::tile_fwk { | ||
| 24 | -bool GetFileSize(const std::string& filePath, uint32_t& fileSize); | ||
| 25 | -uint32_t GetFileSize(const std::string& filePath); | ||
| 26 | -bool CreateDir(const std::string& directoryPath); | ||
| 27 | -bool DeleteDir(const std::string& directoryPath); | ||
| 28 | -bool CreateMultiLevelDir(const std::string& directoryPath); | ||
| 29 | -void DeleteFile(const std::string& path); | ||
| 30 | -bool ReadJsonFile(const std::string& file, nlohmann::json& jsonObj); | ||
| 31 | -bool ReadBytesFromFile(const std::string& filePath, std::vector<char>& buffer); | ||
| 32 | -std::vector<std::string> GetFiles(const std::string& path, const std::string& ext); | ||
| 33 | -void SaveFile(const std::string& filePath, const std::vector<uint8_t>& data); | ||
| 34 | -bool SaveFile(const std::string& filePath, const uint8_t* data, size_t size) __attribute__((warn_unused_result)); | ||
| 35 | -void SaveFileSafe(const std::string& filePath, const uint8_t* data, size_t size); | ||
| 36 | -bool DumpFile(const char* data, const size_t size, const std::string& filePath); | ||
| 37 | -bool DumpFile(const std::vector<uint8_t>& data, const std::string& filePath); | ||
| 38 | -bool DumpFile(const std::string& text, const std::string& filePath); | ||
| 39 | -void Rename(const std::string& oldPath, const std::string& newPath); | ||
| 40 | -std::vector<uint8_t> LoadFile(const std::string& filePath); | ||
| 41 | -FILE* LockAndOpenFile(const std::string& lockFilePath); | ||
| 42 | -void UnlockAndCloseFile(FILE* fp); | ||
| 43 | -bool CopyFile(const std::string& srcPath, const std::string& dstPath); | ||
| 44 | -std::string GetCurrentSharedLibPath(); | ||
| 45 | -std::string GetCurRunningPath(); | ||
| 46 | -void RemoveOldestDirs(const std::string& path, const std::string& prefix, int left); | ||
| 47 | -} // namespace npu::tile_fwk | ||
| @@ -57,7 +57,7 @@ void OpInfoManager::SetOpType(const std::string& opType) | |||
| 57 | 57 | ||
| 58 | const std::string& OpInfoManager::GetOpType() const { return opType_; } | 58 | const std::string& OpInfoManager::GetOpType() const { return opType_; } |
| 59 | 59 | ||
| 60 | -std::vector<char>& OpInfoManager::GetControlBuffer() { return controlBuffer_; } | 60 | +void OpInfoManager::SetControlBuffer(std::vector<uint8_t>&& buffer) { controlBuffer_ = std::move(buffer); } |
| 61 | 61 | ||
| 62 | std::vector<char>& OpInfoManager::GetCustomJson() { return customJson_; } | 62 | std::vector<char>& OpInfoManager::GetCustomJson() { return customJson_; } |
| 63 | 63 | ||
| @@ -37,7 +37,7 @@ public: | |||
| 37 | void SetOpType(const std::string& opType); | 37 | void SetOpType(const std::string& opType); |
| 38 | const std::string& GetOpType() const; | 38 | const std::string& GetOpType() const; |
| 39 | bool IsNotFabinCompile(); | 39 | bool IsNotFabinCompile(); |
| 40 | - std::vector<char>& GetControlBuffer(); | 40 | + void SetControlBuffer(std::vector<uint8_t>&& buffer); |
| 41 | std::vector<char>& GetCustomJson(); | 41 | std::vector<char>& GetCustomJson(); |
| 42 | std::string& GetCustomOpJsonPath(); | 42 | std::string& GetCustomOpJsonPath(); |
| 43 | std::string& GetOpFuncName(); | 43 | std::string& GetOpFuncName(); |
| @@ -49,7 +49,7 @@ private: | |||
| 49 | std::string opType_ = "tilefwk"; | 49 | std::string opType_ = "tilefwk"; |
| 50 | uint64_t opTilingKey_{0}; | 50 | uint64_t opTilingKey_{0}; |
| 51 | uint64_t subTilingKey_{0}; | 51 | uint64_t subTilingKey_{0}; |
| 52 | - std::vector<char> controlBuffer_ = {'0'}; | 52 | + std::vector<uint8_t> controlBuffer_ = {'0'}; |
| 53 | std::vector<char> customJson_ = {'0'}; | 53 | std::vector<char> customJson_ = {'0'}; |
| 54 | std::string controlFlowSoPath_; | 54 | std::string controlFlowSoPath_; |
| 55 | std::string funcName_; | 55 | std::string funcName_; |
| @@ -21,7 +21,7 @@ | |||
| 21 | #include <unistd.h> | 21 | #include <unistd.h> |
| 22 | #include <vector> | 22 | #include <vector> |
| 23 | #include "codegen/utils/parallel_execute.h" | 23 | #include "codegen/utils/parallel_execute.h" |
| 24 | -#include "interface/utils/file_utils.h" | 24 | +#include "utils/file_utils.h" |
| 25 | #include "interface/utils/op_info_manager.h" | 25 | #include "interface/utils/op_info_manager.h" |
| 26 | #include "machine/compile/gen_aicore_code.h" | 26 | #include "machine/compile/gen_aicore_code.h" |
| 27 | #include "machine/host/main_block.h" | 27 | #include "machine/host/main_block.h" |
| @@ -53,7 +53,7 @@ static int CompileCoreMachine( | |||
| 53 | config::GetRuntimeOption<int64_t>(CFG_VALID_SHAPE_OPTIMIZE) == 1)) ? "-D__ENABLE_MAIN_BLOCK" : ""; | 53 | config::GetRuntimeOption<int64_t>(CFG_VALID_SHAPE_OPTIMIZE) == 1)) ? "-D__ENABLE_MAIN_BLOCK" : ""; |
| 54 | std::string ccecCmd; | 54 | std::string ccecCmd; |
| 55 | ccecCmd.resize(CMD_SIZE_2K); | 55 | ccecCmd.resize(CMD_SIZE_2K); |
| 56 | - std::string includePath = GetCurrentSharedLibPath() + "/../include/tile_fwk"; | 56 | + std::string includePath = GetPyptoLibPath() + "/../include/tile_fwk"; |
| 57 | int ret = snprintf_s( | 57 | int ret = snprintf_s( |
| 58 | ccecCmd.data(), CMD_SIZE_2K, CMD_SIZE_2K - 1, | 58 | ccecCmd.data(), CMD_SIZE_2K, CMD_SIZE_2K - 1, |
| 59 | "%s -c -O3 -g -x cce -Wall -Werror -std=c++17 " | 59 | "%s -c -O3 -g -x cce -Wall -Werror -std=c++17 " |
| @@ -74,7 +74,7 @@ static int CompileCoreMachine( | |||
| 74 | "-I%s/include/ " "-o %s %s %s %s", | 74 | "-I%s/include/ " "-o %s %s %s %s", |
| 75 | BISHENG_PROGRAM_CMD, cc_opt.c_str(), std::to_string(tilingKey).c_str(), opType.c_str(), headFile.c_str(), | 75 | BISHENG_PROGRAM_CMD, cc_opt.c_str(), std::to_string(tilingKey).c_str(), opType.c_str(), headFile.c_str(), |
| 76 | hasSubFunc.c_str(), coreType.c_str(), includePath.c_str(), includePath.c_str(), | 76 | hasSubFunc.c_str(), coreType.c_str(), includePath.c_str(), includePath.c_str(), |
| 77 | - GetCurrentSharedLibPath().c_str(), GetCurrentSharedLibPath().c_str(), objFile.c_str(), | 77 | + GetPyptoLibPath().c_str(), GetPyptoLibPath().c_str(), objFile.c_str(), |
| 78 | aicoreSrcFile.c_str(), davArch.c_str(), enableMainBlock.c_str()); | 78 | aicoreSrcFile.c_str(), davArch.c_str(), enableMainBlock.c_str()); |
| 79 | if (ret < 0) { | 79 | if (ret < 0) { |
| 80 | MACHINE_LOGE(HostBackEndErr::COMPILE_AICORE_FAILED, "Compile aicore construct cmd failed."); | 80 | MACHINE_LOGE(HostBackEndErr::COMPILE_AICORE_FAILED, "Compile aicore construct cmd failed."); |
| @@ -17,7 +17,7 @@ | |||
| 17 | #include <vector> | 17 | #include <vector> |
| 18 | #include <nlohmann/json.hpp> | 18 | #include <nlohmann/json.hpp> |
| 19 | #include "interface/program/program.h" | 19 | #include "interface/program/program.h" |
| 20 | -#include "interface/utils/file_utils.h" | 20 | +#include "utils/file_utils.h" |
| 21 | #include "interface/utils/common.h" | 21 | #include "interface/utils/common.h" |
| 22 | #include "interface/configs/config_manager.h" | 22 | #include "interface/configs/config_manager.h" |
| 23 | #include "interface/utils/op_info_manager.h" | 23 | #include "interface/utils/op_info_manager.h" |
| @@ -45,7 +45,7 @@ std::string GetMachineCompilerPath() | |||
| 45 | } | 45 | } |
| 46 | const std::string compiler = | 46 | const std::string compiler = |
| 47 | homePath + "/toolkit/toolchain/hcc/bin/aarch64-target-linux-gnu-g++"; | 47 | homePath + "/toolkit/toolchain/hcc/bin/aarch64-target-linux-gnu-g++"; |
| 48 | - if (!npu::tile_fwk::FileExist(compiler)) { | 48 | + if (!npu::tile_fwk::IsPathExist(compiler)) { |
| 49 | MACHINE_LOGE(npu::tile_fwk::DevCommonErr::FILE_ERROR, | 49 | MACHINE_LOGE(npu::tile_fwk::DevCommonErr::FILE_ERROR, |
| 50 | "%s is set but aarch64 toolchain g++ was not found at \"%s\" (canonical path must exist).", | 50 | "%s is set but aarch64 toolchain g++ was not found at \"%s\" (canonical path must exist).", |
| 51 | kAscendHomeEnv, compiler.c_str()); | 51 | kAscendHomeEnv, compiler.c_str()); |
| @@ -75,8 +75,8 @@ void GenCustomOpInfo( | |||
| 75 | 75 | ||
| 76 | GenAicpuOpInfoJson(customOp, {costomInit, costomRun}); | 76 | GenAicpuOpInfoJson(customOp, {costomInit, costomRun}); |
| 77 | std::string fileName = controlAicpuPath + "/" + constrolSoName + ".json"; | 77 | std::string fileName = controlAicpuPath + "/" + constrolSoName + ".json"; |
| 78 | - if (!DumpFile(customOp.dump(DUMP_LEVEL_FOUR), fileName)) { | 78 | + if (!SaveFile(fileName, customOp.dump(DUMP_LEVEL_FOUR))) { |
| 79 | - MACHINE_LOGE(DevCommonErr::FILE_ERROR, "Contrust custom op json failed"); | 79 | + MACHINE_LOGE(DevCommonErr::FILE_ERROR, "Save custom op json failed"); |
| 80 | return; | 80 | return; |
| 81 | } | 81 | } |
| 82 | OpInfoManager::GetInstance().GetCustomOpJsonPath() = fileName; | 82 | OpInfoManager::GetInstance().GetCustomOpJsonPath() = fileName; |
| @@ -102,7 +102,7 @@ bool GenTilingFunc(const std::string& funcName, const std::string& controlAicpuP | |||
| 102 | oss << "}\n"; | 102 | oss << "}\n"; |
| 103 | oss << "}\n"; | 103 | oss << "}\n"; |
| 104 | std::string fileName = controlAicpuPath + "/control_flow_kernel.cpp"; | 104 | std::string fileName = controlAicpuPath + "/control_flow_kernel.cpp"; |
| 105 | - return DumpFile(oss.str(), fileName); | 105 | + return SaveFile(fileName, oss.str()); |
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | bool TieFwkAicpuPreCompile(std::string& preCompileO, std::string& controlAicpuPath) | 108 | bool TieFwkAicpuPreCompile(std::string& preCompileO, std::string& controlAicpuPath) |
| @@ -110,12 +110,12 @@ bool TieFwkAicpuPreCompile(std::string& preCompileO, std::string& controlAicpuPa | |||
| 110 | std::stringstream preCompileStream; | 110 | std::stringstream preCompileStream; |
| 111 | std::string ext = "cpp"; | 111 | std::string ext = "cpp"; |
| 112 | auto files = GetFiles(controlAicpuPath, ext); | 112 | auto files = GetFiles(controlAicpuPath, ext); |
| 113 | - std::string includePath = GetCurrentSharedLibPath() + "/../include/tilefwk"; | 113 | + std::string includePath = GetPyptoLibPath() + "/../include/tilefwk"; |
| 114 | for (auto file : files) { | 114 | for (auto file : files) { |
| 115 | std::string objFile = controlAicpuPath + file.substr(0, file.find(".")) + ".o"; | 115 | std::string objFile = controlAicpuPath + file.substr(0, file.find(".")) + ".o"; |
| 116 | std::string compileCmd = DeviceMahineCompiler + " -Wall -O2 -fPIC -c -std=gnu++17 -fno-common " + | 116 | std::string compileCmd = DeviceMahineCompiler + " -Wall -O2 -fPIC -c -std=gnu++17 -fno-common " + |
| 117 | controlAicpuPath + file + " -I" + includePath + " -I" + includePath + "/include/" + | 117 | controlAicpuPath + file + " -I" + includePath + " -I" + includePath + "/include/" + |
| 118 | - " -I" + GetCurrentSharedLibPath() + "/include/" + " -o " + objFile; | 118 | + " -I" + GetPyptoLibPath() + "/include/" + " -o " + objFile; |
| 119 | MACHINE_LOGD("PreCompileCmd is %s, file is %s.\n", compileCmd.c_str(), file.c_str()); | 119 | MACHINE_LOGD("PreCompileCmd is %s, file is %s.\n", compileCmd.c_str(), file.c_str()); |
| 120 | int ret = Checkinject(compileCmd.c_str(), compileCmd.size()); | 120 | int ret = Checkinject(compileCmd.c_str(), compileCmd.size()); |
| 121 | if (ret != 0) { | 121 | if (ret != 0) { |
| @@ -138,7 +138,7 @@ bool SharedAicpuCompile(const std::string& funcName, const std::string& aicpuDir | |||
| 138 | std::string cmdGccCompile = "LD_PRELOAD= " + DeviceMahineCompiler + | 138 | std::string cmdGccCompile = "LD_PRELOAD= " + DeviceMahineCompiler + |
| 139 | " -std=gnu++17 -fno-common -shared -fPIC -O2 -Wl,--no-warn-rwx-segments -o " + | 139 | " -std=gnu++17 -fno-common -shared -fPIC -O2 -Wl,--no-warn-rwx-segments -o " + |
| 140 | aicpuDirPath + "/lib" + funcName + "_control.so " + preCompileO + | 140 | aicpuDirPath + "/lib" + funcName + "_control.so " + preCompileO + |
| 141 | - " -Wl,--whole-archive " + GetCurrentSharedLibPath() + "/libpypto_ctrl_server.a" + | 141 | + " -Wl,--whole-archive " + GetPyptoLibPath() + "/libpypto_ctrl_server.a" + |
| 142 | " -Wl,--no-whole-archive"; | 142 | " -Wl,--no-whole-archive"; |
| 143 | int ret = Checkinject(cmdGccCompile.c_str(), cmdGccCompile.size()); | 143 | int ret = Checkinject(cmdGccCompile.c_str(), cmdGccCompile.size()); |
| 144 | if (ret != 0) { | 144 | if (ret != 0) { |
| @@ -153,7 +153,9 @@ bool SharedAicpuCompile(const std::string& funcName, const std::string& aicpuDir | |||
| 153 | std::string srcSoPath = aicpuDirPath + "/lib" + funcName + "_control.so"; | 153 | std::string srcSoPath = aicpuDirPath + "/lib" + funcName + "_control.so"; |
| 154 | std::string constrolSoName = "lib" + funcName + "_control"; | 154 | std::string constrolSoName = "lib" + funcName + "_control"; |
| 155 | GenCustomOpInfo(funcName, aicpuDirPath, constrolSoName); | 155 | GenCustomOpInfo(funcName, aicpuDirPath, constrolSoName); |
| 156 | - return ReadBytesFromFile(srcSoPath, OpInfoManager::GetInstance().GetControlBuffer()); | 156 | + bool succ = false; |
| 157 | + OpInfoManager::GetInstance().SetControlBuffer(ReadFile(srcSoPath, &succ)); | ||
| 158 | + return succ; | ||
| 157 | } | 159 | } |
| 158 | 160 | ||
| 159 | bool TileFwkAiCpuCompile(const std::string& funcName, const std::string& aicpuDirPath) | 161 | bool TileFwkAiCpuCompile(const std::string& funcName, const std::string& aicpuDirPath) |
| @@ -14,7 +14,7 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "machine/compile/gen_aicore_code.h" | 16 | #include "machine/compile/gen_aicore_code.h" |
| 17 | -#include "interface/utils/file_utils.h" | 17 | +#include "utils/file_utils.h" |
| 18 | 18 | ||
| 19 | namespace npu::tile_fwk { | 19 | namespace npu::tile_fwk { |
| 20 | namespace { | 20 | namespace { |
| @@ -39,7 +39,7 @@ bool GenAicoreSrcFile(const std::string& codeSrcPath, const std::string& funcHas | |||
| 39 | pos = newSrcCode.find(kKernelEntryStr, pos + kKernelEntryStrSize + 1); | 39 | pos = newSrcCode.find(kKernelEntryStr, pos + kKernelEntryStrSize + 1); |
| 40 | } | 40 | } |
| 41 | if (RealPath(codeSrcPath).empty()) { | 41 | if (RealPath(codeSrcPath).empty()) { |
| 42 | - DumpFile(kAicoreSrcCode, codeSrcPath); | 42 | + SaveFile(codeSrcPath, kAicoreSrcCode); |
| 43 | } | 43 | } |
| 44 | return !RealPath(codeSrcPath).empty(); | 44 | return !RealPath(codeSrcPath).empty(); |
| 45 | } | 45 | } |
| @@ -10,7 +10,7 @@ | |||
| 10 | 10 | ||
| 11 | /*! | 11 | /*! |
| 12 | * \file dump_device_topo.cpp | 12 | * \file dump_device_topo.cpp |
| 13 | - * \brief | 13 | + * \brief |
| 14 | */ | 14 | */ |
| 15 | #include "machine/device/dynamic/context/dump_device_topo.h" | 15 | #include "machine/device/dynamic/context/dump_device_topo.h" |
| 16 | 16 | ||
| @@ -20,7 +20,7 @@ | |||
| 20 | #include <string> | 20 | #include <string> |
| 21 | #include <vector> | 21 | #include <vector> |
| 22 | #include "interface/configs/config_manager.h" | 22 | #include "interface/configs/config_manager.h" |
| 23 | -#include "interface/utils/file_utils.h" | 23 | +#include "utils/file_utils.h" |
| 24 | #include "machine/utils/dynamic/dev_encode_function_stitch.h" | 24 | #include "machine/utils/dynamic/dev_encode_function_stitch.h" |
| 25 | #endif | 25 | #endif |
| 26 | 26 | ||
| @@ -22,7 +22,7 @@ | |||
| 22 | #include "interface/operation/operation.h" | 22 | #include "interface/operation/operation.h" |
| 23 | #include "interface/configs/config_manager.h" | 23 | #include "interface/configs/config_manager.h" |
| 24 | #include "interface/utils/common.h" | 24 | #include "interface/utils/common.h" |
| 25 | -#include "interface/utils/file_utils.h" | 25 | +#include "utils/file_utils.h" |
| 26 | #include "interface/utils/op_info_manager.h" | 26 | #include "interface/utils/op_info_manager.h" |
| 27 | #include "interface/compiler_monitor/monitor_manager.h" | 27 | #include "interface/compiler_monitor/monitor_manager.h" |
| 28 | #include "interface/compiler_monitor/monitor_stage_scope.h" | 28 | #include "interface/compiler_monitor/monitor_stage_scope.h" |
| @@ -865,7 +865,7 @@ static void SetDyndevProgBinary(Function* function, bool disableCtrlFlowCache) | |||
| 865 | if (config::GetPassDefaultConfig(npu::tile_fwk::KEY_PRINT_PROGRAM, false)) { | 865 | if (config::GetPassDefaultConfig(npu::tile_fwk::KEY_PRINT_PROGRAM, false)) { |
| 866 | devProg->DumpFile(config::LogTopFolder() + "/program.tifwkbintxt"); | 866 | devProg->DumpFile(config::LogTopFolder() + "/program.tifwkbintxt"); |
| 867 | std::string loopDirPath = config::LogTopFolder() + "/loop"; | 867 | std::string loopDirPath = config::LogTopFolder() + "/loop"; |
| 868 | - CreateMultiLevelDir(loopDirPath); | 868 | + CreateDir(loopDirPath, true); |
| 869 | for (size_t index = 0; index < dynAttrPtr->funcGroup.loopList.size(); index++) { | 869 | for (size_t index = 0; index < dynAttrPtr->funcGroup.loopList.size(); index++) { |
| 870 | Function* func = dynAttrPtr->funcGroup.loopList[index]; | 870 | Function* func = dynAttrPtr->funcGroup.loopList[index]; |
| 871 | func->DumpFile(loopDirPath + "/" + func->GetMagicName() + ".tifwkgr"); | 871 | func->DumpFile(loopDirPath + "/" + func->GetMagicName() + ".tifwkgr"); |
| @@ -962,11 +962,11 @@ static void EncodeOutcastProperty( | |||
| 962 | encodeDevAscendFunctionParam.outcastDescList.resize(slot->outcastSlot.size()); | 962 | encodeDevAscendFunctionParam.outcastDescList.resize(slot->outcastSlot.size()); |
| 963 | for (size_t outcastIndex = 0; outcastIndex < slot->outcastSlot.size(); outcastIndex++) { | 963 | for (size_t outcastIndex = 0; outcastIndex < slot->outcastSlot.size(); outcastIndex++) { |
| 964 | RuntimeSlotDesc& desc = encodeDevAscendFunctionParam.outcastDescList[outcastIndex]; | 964 | RuntimeSlotDesc& desc = encodeDevAscendFunctionParam.outcastDescList[outcastIndex]; |
| 965 | - if (outcastSlotKindSetList[outcastIndex].Count(RuntimeSlotKind::INPUT)) { | 965 | + if (outcastSlotKindSetList[outcastIndex].Contains(RuntimeSlotKind::INPUT)) { |
| 966 | desc.kind = RuntimeSlotKind::INPUT; | 966 | desc.kind = RuntimeSlotKind::INPUT; |
| 967 | - } else if (outcastSlotKindSetList[outcastIndex].Count(RuntimeSlotKind::OUTPUT)) { | 967 | + } else if (outcastSlotKindSetList[outcastIndex].Contains(RuntimeSlotKind::OUTPUT)) { |
| 968 | desc.kind = RuntimeSlotKind::OUTPUT; | 968 | desc.kind = RuntimeSlotKind::OUTPUT; |
| 969 | - } else if (outcastSlotKindSetList[outcastIndex].Count(RuntimeSlotKind::ASSEMBLE_OUTCAST)) { | 969 | + } else if (outcastSlotKindSetList[outcastIndex].Contains(RuntimeSlotKind::ASSEMBLE_OUTCAST)) { |
| 970 | desc.kind = RuntimeSlotKind::ASSEMBLE_OUTCAST; | 970 | desc.kind = RuntimeSlotKind::ASSEMBLE_OUTCAST; |
| 971 | } else { | 971 | } else { |
| 972 | int incastIndex = -1; | 972 | int incastIndex = -1; |
| @@ -1000,7 +1000,7 @@ static bool IsNeedDumpAicpuKernel(const std::string& inputFile) | |||
| 1000 | return true; | 1000 | return true; |
| 1001 | } | 1001 | } |
| 1002 | // not force dumprootTileDict | 1002 | // not force dumprootTileDict |
| 1003 | - if (npu::tile_fwk::FileExist(inputFile)) { | 1003 | + if (npu::tile_fwk::IsPathExist(inputFile)) { |
| 1004 | return false; | 1004 | return false; |
| 1005 | } | 1005 | } |
| 1006 | return true; | 1006 | return true; |
| @@ -1028,14 +1028,14 @@ static void CompileControlFlow( | |||
| 1028 | MACHINE_LOGD( | 1028 | MACHINE_LOGD( |
| 1029 | "Dumpath is %s, functionName %s, path is %s", aicpuDirPath.c_str(), funcName.c_str(), | 1029 | "Dumpath is %s, functionName %s, path is %s", aicpuDirPath.c_str(), funcName.c_str(), |
| 1030 | controlFlowCompilepath.c_str()); | 1030 | controlFlowCompilepath.c_str()); |
| 1031 | - if (!CreateMultiLevelDir(controlFlowCompilepath)) { | 1031 | + if (!CreateDir(controlFlowCompilepath, true)) { |
| 1032 | MACHINE_LOGE(DevCommonErr::FILE_ERROR, "Creat AicpuCompile dir not success\n"); | 1032 | MACHINE_LOGE(DevCommonErr::FILE_ERROR, "Creat AicpuCompile dir not success\n"); |
| 1033 | return; | 1033 | return; |
| 1034 | } | 1034 | } |
| 1035 | std::string controlFlowFileName = controlFlowCompilepath + "/controlFlow_dev" + funcName + ".h"; | 1035 | std::string controlFlowFileName = controlFlowCompilepath + "/controlFlow_dev" + funcName + ".h"; |
| 1036 | std::string expressFileName = controlFlowCompilepath + "/expression_0.h"; | 1036 | std::string expressFileName = controlFlowCompilepath + "/expression_0.h"; |
| 1037 | - if (!DumpFile(constrolFlow, controlFlowFileName) || !DumpFile(express, expressFileName)) { | 1037 | + if (!SaveFile(controlFlowFileName, constrolFlow) || !SaveFile(expressFileName, express)) { |
| 1038 | - MACHINE_LOGD("Dump controlFlow and express files failed\n"); | 1038 | + MACHINE_LOGD("Save controlFlow and express files failed\n"); |
| 1039 | return; | 1039 | return; |
| 1040 | } | 1040 | } |
| 1041 | #ifdef BUILD_WITH_CANN | 1041 | #ifdef BUILD_WITH_CANN |
| @@ -1128,7 +1128,7 @@ static void RunCompileControlFlowStage( | |||
| 1128 | 1128 | ||
| 1129 | const std::string funcHash = function->GetFunctionHash().Data(); | 1129 | const std::string funcHash = function->GetFunctionHash().Data(); |
| 1130 | const std::string arm64TargetToolPath = Arm64TargetTool("g++"); | 1130 | const std::string arm64TargetToolPath = Arm64TargetTool("g++"); |
| 1131 | - const bool hasArm64DevCompile = FileExist(arm64TargetToolPath); | 1131 | + const bool hasArm64DevCompile = IsPathExist(arm64TargetToolPath); |
| 1132 | const std::string controlFlowHostFilePath = aicpuDirPath + "/controlFlow_host_" + funcHash + ".cpp"; | 1132 | const std::string controlFlowHostFilePath = aicpuDirPath + "/controlFlow_host_" + funcHash + ".cpp"; |
| 1133 | 1133 | ||
| 1134 | const int hmStep = MonitorManager::Instance().AllocHostMachineStepIndex(); | 1134 | const int hmStep = MonitorManager::Instance().AllocHostMachineStepIndex(); |
| @@ -1301,7 +1301,7 @@ static void CompileDyndevFunction(Function* function, FunctionCache& cache, [[ma | |||
| 1301 | (hasAicoreKernelLink ? aicoreKernelStepCount : 0) + encodeStepCount); | 1301 | (hasAicoreKernelLink ? aicoreKernelStepCount : 0) + encodeStepCount); |
| 1302 | uint64_t tilingKey = OpInfoManager::GetInstance().GetOpTilingKey(); | 1302 | uint64_t tilingKey = OpInfoManager::GetInstance().GetOpTilingKey(); |
| 1303 | std::string aicpuDirPath = config::GetEmitPath("kernel_aicpu"); | 1303 | std::string aicpuDirPath = config::GetEmitPath("kernel_aicpu"); |
| 1304 | - npu::tile_fwk::CreateMultiLevelDir(aicpuDirPath); | 1304 | + npu::tile_fwk::CreateDir(aicpuDirPath, true); |
| 1305 | const std::string expName = "expression_" + std::to_string(tilingKey) + ".h"; | 1305 | const std::string expName = "expression_" + std::to_string(tilingKey) + ".h"; |
| 1306 | std::vector<std::string> exprSrcFiles; | 1306 | std::vector<std::string> exprSrcFiles; |
| 1307 | ValDependTensorMeta valDependTensorMeta; | 1307 | ValDependTensorMeta valDependTensorMeta; |
| @@ -1314,7 +1314,7 @@ static void CompileDyndevFunction(Function* function, FunctionCache& cache, [[ma | |||
| 1314 | 1314 | ||
| 1315 | std::string expressionFilePath = aicpuDirPath + "/" + expName; | 1315 | std::string expressionFilePath = aicpuDirPath + "/" + expName; |
| 1316 | if (IsNeedDumpAicpuKernel(expressionFilePath)) { | 1316 | if (IsNeedDumpAicpuKernel(expressionFilePath)) { |
| 1317 | - DumpFile(expressionSource, expressionFilePath); | 1317 | + SaveFile(expressionFilePath, expressionSource); |
| 1318 | } | 1318 | } |
| 1319 | 1319 | ||
| 1320 | RunCompileControlFlowStage(function, attr, aicpuDirPath, controlFlowSource, expressionSource, exprSrcFiles); | 1320 | RunCompileControlFlowStage(function, attr, aicpuDirPath, controlFlowSource, expressionSource, exprSrcFiles); |
| @@ -1335,7 +1335,7 @@ static void CompileDyndevFunction(Function* function, FunctionCache& cache, [[ma | |||
| 1335 | } | 1335 | } |
| 1336 | #endif | 1336 | #endif |
| 1337 | 1337 | ||
| 1338 | - attr->kernelBinary = LoadFile(kernelPath); | 1338 | + attr->kernelBinary = ReadFile(kernelPath); |
| 1339 | MACHINE_LOGD("KernelBinary size[%zu].", attr->kernelBinary.size()); | 1339 | MACHINE_LOGD("KernelBinary size[%zu].", attr->kernelBinary.size()); |
| 1340 | 1340 | ||
| 1341 | RunEncodeStage(function, attr, linker, encodeDevAscendFunctionParam, valDependTensorMeta.disableCtrlFlowCache); | 1341 | RunEncodeStage(function, attr, linker, encodeDevAscendFunctionParam, valDependTensorMeta.disableCtrlFlowCache); |
| @@ -1344,7 +1344,7 @@ static void CompileDyndevFunction(Function* function, FunctionCache& cache, [[ma | |||
| 1344 | MachineTask* GenCode(MachineTask* task, FunctionCache& cache) | 1344 | MachineTask* GenCode(MachineTask* task, FunctionCache& cache) |
| 1345 | { | 1345 | { |
| 1346 | npu::tile_fwk::CodeGenCtx codeGenCtx("", config::GetEmitPath("kernel_aicore")); | 1346 | npu::tile_fwk::CodeGenCtx codeGenCtx("", config::GetEmitPath("kernel_aicore")); |
| 1347 | - npu::tile_fwk::CreateMultiLevelDir(codeGenCtx.cceDir); | 1347 | + npu::tile_fwk::CreateDir(codeGenCtx.cceDir, true); |
| 1348 | npu::tile_fwk::CodeGen codeGen(codeGenCtx); | 1348 | npu::tile_fwk::CodeGen codeGen(codeGenCtx); |
| 1349 | auto function = task->GetFunction(); | 1349 | auto function = task->GetFunction(); |
| 1350 | /* each leafFunction inside is compiled to a standalone object file. | 1350 | /* each leafFunction inside is compiled to a standalone object file. |
| @@ -10,7 +10,7 @@ | |||
| 10 | 10 | ||
| 11 | /*! | 11 | /*! |
| 12 | * \file dump_host_topo.cpp | 12 | * \file dump_host_topo.cpp |
| 13 | - * \brief | 13 | + * \brief |
| 14 | */ | 14 | */ |
| 15 | #include "dump_host_topo.h" | 15 | #include "dump_host_topo.h" |
| 16 | 16 | ||
| @@ -24,7 +24,7 @@ | |||
| 24 | #include "interface/configs/config_manager.h" | 24 | #include "interface/configs/config_manager.h" |
| 25 | #include "interface/function/function.h" | 25 | #include "interface/function/function.h" |
| 26 | #include "interface/tensor/tensor_slot.h" | 26 | #include "interface/tensor/tensor_slot.h" |
| 27 | -#include "interface/utils/file_utils.h" | 27 | +#include "utils/file_utils.h" |
| 28 | #include "interface/utils/string_utils.h" | 28 | #include "interface/utils/string_utils.h" |
| 29 | #include "machine/utils/dynamic/dev_encode_function.h" | 29 | #include "machine/utils/dynamic/dev_encode_function.h" |
| 30 | #include "machine/utils/dynamic/dev_encode_operation.h" | 30 | #include "machine/utils/dynamic/dev_encode_operation.h" |
| @@ -26,7 +26,7 @@ | |||
| 26 | #include "interface/utils/op_info_manager.h" | 26 | #include "interface/utils/op_info_manager.h" |
| 27 | #include "interface/configs/config_manager.h" | 27 | #include "interface/configs/config_manager.h" |
| 28 | #include "interface/machine/host/host_machine.h" | 28 | #include "interface/machine/host/host_machine.h" |
| 29 | -#include "interface/utils/file_utils.h" | 29 | +#include "utils/file_utils.h" |
| 30 | #include "machine/runtime/runner/runtime_agent.h" | 30 | #include "machine/runtime/runner/runtime_agent.h" |
| 31 | #include "machine/runtime/runner/runtime_utils.h" | 31 | #include "machine/runtime/runner/runtime_utils.h" |
| 32 | #include "machine/runtime/context/stream_context.h" | 32 | #include "machine/runtime/context/stream_context.h" |
| @@ -320,9 +320,9 @@ int DeviceRunner::LaunchDynamicAiCpu(RtStream aicpuStream, uint32_t aicpuNum, De | |||
| 320 | 320 | ||
| 321 | void DeviceRunner::InitAiCpuSoBin(DeviceArgs& devArgs) | 321 | void DeviceRunner::InitAiCpuSoBin(DeviceArgs& devArgs) |
| 322 | { | 322 | { |
| 323 | - std::vector<char> buffer; | 323 | + std::string fileName = GetPyptoLibPath() + "/libtilefwk_backend_server.so"; |
| 324 | - std::string fileName = GetCurrentSharedLibPath() + "/libtilefwk_backend_server.so"; | 324 | + auto buffer = ReadFile(fileName); |
| 325 | - if (!ReadBytesFromFile(fileName, buffer)) { | 325 | + if (buffer.empty()) { |
| 326 | MACHINE_LOGE( | 326 | MACHINE_LOGE( |
| 327 | DevCommonErr::FILE_ERROR, "Read bin form tilefwk_backend_server.so failed, please check the so[%s]", | 327 | DevCommonErr::FILE_ERROR, "Read bin form tilefwk_backend_server.so failed, please check the so[%s]", |
| 328 | fileName.c_str()); | 328 | fileName.c_str()); |
| @@ -527,7 +527,7 @@ int DeviceRunner::DynamicRun(const KernelLaunchInfo &launchInfo, DeviceKernelArg | |||
| 527 | int DeviceRunner::Init() | 527 | int DeviceRunner::Init() |
| 528 | { | 528 | { |
| 529 | std::string builtInOpPath = config::LogTopFolder() + "/built_in"; | 529 | std::string builtInOpPath = config::LogTopFolder() + "/built_in"; |
| 530 | - CreateMultiLevelDir(builtInOpPath); | 530 | + CreateDir(builtInOpPath, true); |
| 531 | LoadAicpuOp::GetInstance().GenBuiltInOpInfo(builtInOpPath); | 531 | LoadAicpuOp::GetInstance().GenBuiltInOpInfo(builtInOpPath); |
| 532 | if (LoadAicpuOp::GetInstance().GetBuiltInOpBinHandle() != 0) { | 532 | if (LoadAicpuOp::GetInstance().GetBuiltInOpBinHandle() != 0) { |
| 533 | MACHINE_LOGE(DevCommonErr::GET_HANDLE_FAILED, "Get builtInOp Funchandle failed\n"); | 533 | MACHINE_LOGE(DevCommonErr::GET_HANDLE_FAILED, "Get builtInOp Funchandle failed\n"); |
| @@ -23,7 +23,7 @@ | |||
| 23 | #include "machine/runtime/runner/runtime_utils.h" | 23 | #include "machine/runtime/runner/runtime_utils.h" |
| 24 | #include "machine/runtime/launcher/device_launcher.h" | 24 | #include "machine/runtime/launcher/device_launcher.h" |
| 25 | #include "interface/machine/device/tilefwk/aicpu_common.h" | 25 | #include "interface/machine/device/tilefwk/aicpu_common.h" |
| 26 | -#include "interface/utils/file_utils.h" | 26 | +#include "utils/file_utils.h" |
| 27 | #include "interface/configs/config_manager.h" | 27 | #include "interface/configs/config_manager.h" |
| 28 | #include "machine/device/dynamic/device_utils.h" | 28 | #include "machine/device/dynamic/device_utils.h" |
| 29 | #include "machine/device/distributed/common.h" | 29 | #include "machine/device/distributed/common.h" |
| @@ -156,7 +156,7 @@ void DumpAicoreTaskExectInfo(const DeviceArgs& args, const std::vector<void*>& p | |||
| 156 | ConstructTaskInfo(i, rootTaskStatus, perfData, "AI-CPU"); | 156 | ConstructTaskInfo(i, rootTaskStatus, perfData, "AI-CPU"); |
| 157 | } | 157 | } |
| 158 | std::string jsonFilePath = npu::tile_fwk::config::LogTopFolder() + "/tilefwk_L1_prof_data.json"; | 158 | std::string jsonFilePath = npu::tile_fwk::config::LogTopFolder() + "/tilefwk_L1_prof_data.json"; |
| 159 | - if (!DumpFile(rootTaskStatus.dump(DUMP_LEVEL_FOUR), jsonFilePath)) { | 159 | + if (!SaveFile(jsonFilePath, rootTaskStatus.dump(DUMP_LEVEL_FOUR))) { |
| 160 | MACHINE_LOGW("Contrust custom op json failed"); | 160 | MACHINE_LOGW("Contrust custom op json failed"); |
| 161 | return; | 161 | return; |
| 162 | } | 162 | } |
| @@ -164,12 +164,12 @@ void DumpAicoreTaskExectInfo(const DeviceArgs& args, const std::vector<void*>& p | |||
| 164 | std::string topo_txt_path = npu::tile_fwk::config::LogTopFolder() + "/dyn_topo.txt"; | 164 | std::string topo_txt_path = npu::tile_fwk::config::LogTopFolder() + "/dyn_topo.txt"; |
| 165 | std::string program_json_path = npu::tile_fwk::config::LogTopFolder() + "/program.json"; | 165 | std::string program_json_path = npu::tile_fwk::config::LogTopFolder() + "/program.json"; |
| 166 | std::string mix_event_path = npu::tile_fwk::config::GetAbsoluteTopFolder() + "/mix_event_info.json"; | 166 | std::string mix_event_path = npu::tile_fwk::config::GetAbsoluteTopFolder() + "/mix_event_info.json"; |
| 167 | - std::string draw_swim_lane_py_path = GetCurrentSharedLibPath() + "/scripts/draw_swim_lane.py"; | 167 | + std::string draw_swim_lane_py_path = GetPyptoLibPath() + "/scripts/draw_swim_lane.py"; |
| 168 | npu::tile_fwk::config::SetRunDataOption( | 168 | npu::tile_fwk::config::SetRunDataOption( |
| 169 | KEY_SWIM_GRAPH_PATH, npu::tile_fwk::config::GetAbsoluteTopFolder() + "/merged_swimlane.json"); | 169 | KEY_SWIM_GRAPH_PATH, npu::tile_fwk::config::GetAbsoluteTopFolder() + "/merged_swimlane.json"); |
| 170 | uint64_t freq = (args.archInfo == ArchInfo::DAV_2201) ? FREQ_DAV_2201 : FREQ_DAV_3510; | 170 | uint64_t freq = (args.archInfo == ArchInfo::DAV_2201) ? FREQ_DAV_2201 : FREQ_DAV_3510; |
| 171 | 171 | ||
| 172 | - if (FileExist(program_json_path) && FileExist(topo_txt_path)) { | 172 | + if (IsPathExist(program_json_path) && IsPathExist(topo_txt_path)) { |
| 173 | MACHINE_LOGI("The files program.json and dyn_topo.txt exist. Start merging the swimlane."); | 173 | MACHINE_LOGI("The files program.json and dyn_topo.txt exist. Start merging the swimlane."); |
| 174 | std::string command = "python3 " + draw_swim_lane_py_path + " \"" + jsonFilePath + "\" \"" + topo_txt_path + | 174 | std::string command = "python3 " + draw_swim_lane_py_path + " \"" + jsonFilePath + "\" \"" + topo_txt_path + |
| 175 | "\" \"" + program_json_path + | 175 | "\" \"" + program_json_path + |
| @@ -227,17 +227,17 @@ inline void ConstructAicorePerfInfo(json& tasksArr, Metrics* aicoreMetric, const | |||
| 227 | AicoreMetric* aicoreMetricPref = &(aicoreMetric->aicoreDevTaskInfo[turnIdx]); | 227 | AicoreMetric* aicoreMetricPref = &(aicoreMetric->aicoreDevTaskInfo[turnIdx]); |
| 228 | for (uint64_t cnt = 0; cnt < aicoreMetricPref->cnt; cnt++) { | 228 | for (uint64_t cnt = 0; cnt < aicoreMetricPref->cnt; cnt++) { |
| 229 | AicoreDevTaskPerf perf = aicoreMetricPref->aicoreEveryDevTypeTimeStamp[cnt]; | 229 | AicoreDevTaskPerf perf = aicoreMetricPref->aicoreEveryDevTypeTimeStamp[cnt]; |
| 230 | - | 230 | + |
| 231 | curCycle = perf.aicoreDevTimeStamp; | 231 | curCycle = perf.aicoreDevTimeStamp; |
| 232 | if (curCycle == 0) { | 232 | if (curCycle == 0) { |
| 233 | continue; | 233 | continue; |
| 234 | } | 234 | } |
| 235 | - | 235 | + |
| 236 | uint32_t type = static_cast<uint32_t>(perf.type); | 236 | uint32_t type = static_cast<uint32_t>(perf.type); |
| 237 | if (type >= PERF_TRACE_CORE_MAX) { | 237 | if (type >= PERF_TRACE_CORE_MAX) { |
| 238 | continue; | 238 | continue; |
| 239 | } | 239 | } |
| 240 | - | 240 | + |
| 241 | json aicoreTaskType; | 241 | json aicoreTaskType; |
| 242 | std::string name = AicorePerfTraceName[type]; | 242 | std::string name = AicorePerfTraceName[type]; |
| 243 | name = name + "_" + std::to_string(turnIdx); | 243 | name = name + "_" + std::to_string(turnIdx); |
| @@ -363,13 +363,13 @@ void DumpAicpuPerfInfo(DeviceArgs& args, const std::vector<void*>& perfData, uin | |||
| 363 | 363 | ||
| 364 | std::string aicpuPerfilePath = | 364 | std::string aicpuPerfilePath = |
| 365 | config::LogTopFolder() + "/machine_trace_perf_data_" + std::to_string(g_last_round_num) + ".json"; | 365 | config::LogTopFolder() + "/machine_trace_perf_data_" + std::to_string(g_last_round_num) + ".json"; |
| 366 | - if (!DumpFile(aicpuPrefArray.dump(DUMP_LEVEL_FOUR), aicpuPerfilePath)) { | 366 | + if (!SaveFile(aicpuPerfilePath, aicpuPrefArray.dump(DUMP_LEVEL_FOUR))) { |
| 367 | MACHINE_LOGW("Contrust custom op json failed"); | 367 | MACHINE_LOGW("Contrust custom op json failed"); |
| 368 | return; | 368 | return; |
| 369 | } | 369 | } |
| 370 | 370 | ||
| 371 | // toolkit drawm aicpu preffto | 371 | // toolkit drawm aicpu preffto |
| 372 | - std::string scriptPath = GetCurrentSharedLibPath() + "/scripts/machine_perf_trace.py"; | 372 | + std::string scriptPath = GetPyptoLibPath() + "/scripts/machine_perf_trace.py"; |
| 373 | std::string cmd = "python3 " + scriptPath + " gen_perfetto " + aicpuPerfilePath + " " + | 373 | std::string cmd = "python3 " + scriptPath + " gen_perfetto " + aicpuPerfilePath + " " + |
| 374 | npu::tile_fwk::config::LogTopFolder() + "/machine_runtime_operator_trace_" + | 374 | npu::tile_fwk::config::LogTopFolder() + "/machine_runtime_operator_trace_" + |
| 375 | std::to_string(g_last_round_num) + ".json " + npu::tile_fwk::config::LogTopFolder() + | 375 | std::to_string(g_last_round_num) + ".json " + npu::tile_fwk::config::LogTopFolder() + |
| @@ -19,7 +19,7 @@ | |||
| 19 | #include <fstream> | 19 | #include <fstream> |
| 20 | #include <limits.h> | 20 | #include <limits.h> |
| 21 | #include "tilefwk/pypto_fwk_log.h" | 21 | #include "tilefwk/pypto_fwk_log.h" |
| 22 | -#include "interface/utils/file_utils.h" | 22 | +#include "utils/file_utils.h" |
| 23 | #include "interface/utils/op_info_manager.h" | 23 | #include "interface/utils/op_info_manager.h" |
| 24 | #include "machine/runtime/runner/runtime_utils.h" | 24 | #include "machine/runtime/runner/runtime_utils.h" |
| 25 | #include "machine/utils/machine_utils.h" | 25 | #include "machine/utils/machine_utils.h" |
| @@ -65,7 +65,7 @@ void LoadAicpuOp::GenBuiltInOpInfo(const std::string& jsonPath) | |||
| 65 | GenAicpuOpInfoJson(builtInOp, {pyptoInit, pyptoRun, pyptoNull}); | 65 | GenAicpuOpInfoJson(builtInOp, {pyptoInit, pyptoRun, pyptoNull}); |
| 66 | builtInOp.dump(DUMP_LEVEL_FOUR); | 66 | builtInOp.dump(DUMP_LEVEL_FOUR); |
| 67 | builtInOpJsonPath_ = jsonPath + "/pypto_op_info.json"; | 67 | builtInOpJsonPath_ = jsonPath + "/pypto_op_info.json"; |
| 68 | - if (!DumpFile(builtInOp.dump(DUMP_LEVEL_FOUR), builtInOpJsonPath_)) { | 68 | + if (!SaveFile(builtInOpJsonPath_, builtInOp.dump(DUMP_LEVEL_FOUR))) { |
| 69 | MACHINE_LOGE(DevCommonErr::FILE_ERROR, "Contrust custom op json failed"); | 69 | MACHINE_LOGE(DevCommonErr::FILE_ERROR, "Contrust custom op json failed"); |
| 70 | return; | 70 | return; |
| 71 | } | 71 | } |
| @@ -315,7 +315,7 @@ static bool ShouldDropBudget( | |||
| 315 | } | 315 | } |
| 316 | 316 | ||
| 317 | for (int slotIdx : slotInfo->outcastSlot[encoded.ioIndex]) { | 317 | for (int slotIdx : slotInfo->outcastSlot[encoded.ioIndex]) { |
| 318 | - if (runtimeSlotKindSetList[slotIdx].Count(RuntimeSlotKind::ADDRESS_EXPRESSION)) { | 318 | + if (runtimeSlotKindSetList[slotIdx].Contains(RuntimeSlotKind::ADDRESS_EXPRESSION)) { |
| 319 | return true; | 319 | return true; |
| 320 | } | 320 | } |
| 321 | } | 321 | } |
| @@ -326,9 +326,9 @@ static bool HasInputOutputOrAssembleDst( | |||
| 326 | const std::vector<int>& outcastSlots, const std::vector<npu::tile_fwk::RuntimeSlotKindSet>& runtimeSlotKindSetList) | 326 | const std::vector<int>& outcastSlots, const std::vector<npu::tile_fwk::RuntimeSlotKindSet>& runtimeSlotKindSetList) |
| 327 | { | 327 | { |
| 328 | for (int slotIdx : outcastSlots) { | 328 | for (int slotIdx : outcastSlots) { |
| 329 | - if (runtimeSlotKindSetList[slotIdx].Count(RuntimeSlotKind::INPUT) || | 329 | + if (runtimeSlotKindSetList[slotIdx].Contains(RuntimeSlotKind::INPUT) || |
| 330 | - runtimeSlotKindSetList[slotIdx].Count(RuntimeSlotKind::OUTPUT) || | 330 | + runtimeSlotKindSetList[slotIdx].Contains(RuntimeSlotKind::OUTPUT) || |
| 331 | - runtimeSlotKindSetList[slotIdx].Count(RuntimeSlotKind::ASSEMBLE_OUTCAST)) { | 331 | + runtimeSlotKindSetList[slotIdx].Contains(RuntimeSlotKind::ASSEMBLE_OUTCAST)) { |
| 332 | return true; | 332 | return true; |
| 333 | } | 333 | } |
| 334 | } | 334 | } |
| @@ -2676,9 +2676,9 @@ static std::vector<SlotInfo> MarkInputOutputAssembleSlots(DevAscendProgram& devP | |||
| 2676 | bool isAssembleOutcastSlot = false; | 2676 | bool isAssembleOutcastSlot = false; |
| 2677 | for (size_t j = 0; j < toSlotList.size(); j++) { | 2677 | for (size_t j = 0; j < toSlotList.size(); j++) { |
| 2678 | SlotInfo& slotInfo = slotInfoList[devFunc->At(toSlotList, j)]; | 2678 | SlotInfo& slotInfo = slotInfoList[devFunc->At(toSlotList, j)]; |
| 2679 | - if (slotInfo.kindSet.Count(RuntimeSlotKind::INPUT) || slotInfo.kindSet.Count(RuntimeSlotKind::OUTPUT)) { | 2679 | + if (slotInfo.kindSet.Contains(RuntimeSlotKind::INPUT) || slotInfo.kindSet.Contains(RuntimeSlotKind::OUTPUT)) { |
| 2680 | isInputOutputSlot = true; | 2680 | isInputOutputSlot = true; |
| 2681 | - } else if (slotInfo.kindSet.Count(RuntimeSlotKind::ASSEMBLE_OUTCAST)) { | 2681 | + } else if (slotInfo.kindSet.Contains(RuntimeSlotKind::ASSEMBLE_OUTCAST)) { |
| 2682 | isAssembleOutcastSlot = true; | 2682 | isAssembleOutcastSlot = true; |
| 2683 | } | 2683 | } |
| 2684 | } | 2684 | } |
| @@ -2710,8 +2710,8 @@ static bool IsInputOutputSlot(const std::vector<SlotInfo>& slotInfoList, DevAsce | |||
| 2710 | auto& toSlotList = func->GetOutcast(idx).toSlotList; | 2710 | auto& toSlotList = func->GetOutcast(idx).toSlotList; |
| 2711 | for (size_t j = 0; j < toSlotList.size(); j++) { | 2711 | for (size_t j = 0; j < toSlotList.size(); j++) { |
| 2712 | int slotIdx = func->At(toSlotList, j); | 2712 | int slotIdx = func->At(toSlotList, j); |
| 2713 | - if (slotInfoList[slotIdx].kindSet.Count(RuntimeSlotKind::INPUT) || | 2713 | + if (slotInfoList[slotIdx].kindSet.Contains(RuntimeSlotKind::INPUT) || |
| 2714 | - slotInfoList[slotIdx].kindSet.Count(RuntimeSlotKind::OUTPUT)) { | 2714 | + slotInfoList[slotIdx].kindSet.Contains(RuntimeSlotKind::OUTPUT)) { |
| 2715 | return true; | 2715 | return true; |
| 2716 | } | 2716 | } |
| 2717 | } | 2717 | } |
| @@ -2724,7 +2724,7 @@ static bool IsAssembleSlot(std::vector<SlotInfo>& slots, DevAscendFunction* func | |||
| 2724 | bool isAssemble = false; | 2724 | bool isAssemble = false; |
| 2725 | for (size_t j = 0; j < toSlotList.size(); j++) { | 2725 | for (size_t j = 0; j < toSlotList.size(); j++) { |
| 2726 | int slotIdx = func->At(toSlotList, j); | 2726 | int slotIdx = func->At(toSlotList, j); |
| 2727 | - if (slots[slotIdx].kindSet.Count(RuntimeSlotKind::ASSEMBLE_OUTCAST)) { | 2727 | + if (slots[slotIdx].kindSet.Contains(RuntimeSlotKind::ASSEMBLE_OUTCAST)) { |
| 2728 | isAssemble = true; | 2728 | isAssemble = true; |
| 2729 | break; | 2729 | break; |
| 2730 | } | 2730 | } |
| @@ -2851,7 +2851,7 @@ static std::pair<uint64_t, SymbolicScalar> ComputeAssembleOutcastMem(const std:: | |||
| 2851 | uint64_t maxStaticAssembleOutcastMem = | 2851 | uint64_t maxStaticAssembleOutcastMem = |
| 2852 | std::accumulate(slots.begin(), slots.end(), UINT64_C(0), [](uint64_t acc, const SlotInfo& slot) { | 2852 | std::accumulate(slots.begin(), slots.end(), UINT64_C(0), [](uint64_t acc, const SlotInfo& slot) { |
| 2853 | return std::max( | 2853 | return std::max( |
| 2854 | - acc, (slot.kindSet.Count(RuntimeSlotKind::ASSEMBLE_OUTCAST) ? slot.maxAssembleDstMemReq : 0)); | 2854 | + acc, (slot.kindSet.Contains(RuntimeSlotKind::ASSEMBLE_OUTCAST) ? slot.maxAssembleDstMemReq : 0)); |
| 2855 | }); | 2855 | }); |
| 2856 | 2856 | ||
| 2857 | SymbolicScalar maxDynamicAssembleOutcastMem = | 2857 | SymbolicScalar maxDynamicAssembleOutcastMem = |
| @@ -2993,10 +2993,10 @@ static TensorWorkspaceResult CalcTensorWorkspace( | |||
| 2993 | res.maxDynamicAssembleOutcastMem = maxDynamicAssembleOutcastMem; | 2993 | res.maxDynamicAssembleOutcastMem = maxDynamicAssembleOutcastMem; |
| 2994 | 2994 | ||
| 2995 | res.totalExclusiveOutcastSlot = std::count_if(slots.begin(), slots.end(), [](const SlotInfo& slot) { | 2995 | res.totalExclusiveOutcastSlot = std::count_if(slots.begin(), slots.end(), [](const SlotInfo& slot) { |
| 2996 | - return slot.kindSet.Count(RuntimeSlotKind::EXCLUSIVE_OUTCAST); | 2996 | + return slot.kindSet.Contains(RuntimeSlotKind::EXCLUSIVE_OUTCAST); |
| 2997 | }); | 2997 | }); |
| 2998 | res.totalAssembleOutcastSlot = std::count_if(slots.begin(), slots.end(), [](const SlotInfo& slot) { | 2998 | res.totalAssembleOutcastSlot = std::count_if(slots.begin(), slots.end(), [](const SlotInfo& slot) { |
| 2999 | - return slot.kindSet.Count(RuntimeSlotKind::ASSEMBLE_OUTCAST); | 2999 | + return slot.kindSet.Contains(RuntimeSlotKind::ASSEMBLE_OUTCAST); |
| 3000 | }); | 3000 | }); |
| 3001 | uint64_t dynamicCellMatchSlotNum = 0; | 3001 | uint64_t dynamicCellMatchSlotNum = 0; |
| 3002 | for (size_t i = 0; i < devProg.partialUpdateList.size(); ++i) { | 3002 | for (size_t i = 0; i < devProg.partialUpdateList.size(); ++i) { |
| @@ -18,7 +18,7 @@ | |||
| 18 | #include "tilefwk/tilefwk.h" | 18 | #include "tilefwk/tilefwk.h" |
| 19 | #include "interface/program/program.h" | 19 | #include "interface/program/program.h" |
| 20 | #include "interface/function/function.h" | 20 | #include "interface/function/function.h" |
| 21 | -#include "interface/utils/file_utils.h" | 21 | +#include "utils/file_utils.h" |
| 22 | #include "passes/pass_log/pass_log.h" | 22 | #include "passes/pass_log/pass_log.h" |
| 23 | 23 | ||
| 24 | #define MODULE_NAME "Pass" | 24 | #define MODULE_NAME "Pass" |
| @@ -52,7 +52,7 @@ Status Pass::CreateLogFolder(const std::string& topFolder, const std::string& st | |||
| 52 | } | 52 | } |
| 53 | if (!strategy.empty()) { | 53 | if (!strategy.empty()) { |
| 54 | passFolder_ = passFolder_ + "/" + kComputationGraphFolder + "/" + strategy; | 54 | passFolder_ = passFolder_ + "/" + kComputationGraphFolder + "/" + strategy; |
| 55 | - if (!CreateMultiLevelDir(passFolder_)) { | 55 | + if (!CreateDir(passFolder_, true)) { |
| 56 | APASS_LOG_ERROR_F(Elements::Function, "Failed to create strategy directory: [%s].", passFolder_.c_str()); | 56 | APASS_LOG_ERROR_F(Elements::Function, "Failed to create strategy directory: [%s].", passFolder_.c_str()); |
| 57 | return FAILED; | 57 | return FAILED; |
| 58 | } | 58 | } |
| @@ -204,7 +204,7 @@ Status Pass::CreateGraphFolder(Function& function) | |||
| 204 | graphFolder_ = graphFolder_ + '/' + kComputationGraphFolder + '/' + strategy_; | 204 | graphFolder_ = graphFolder_ + '/' + kComputationGraphFolder + '/' + strategy_; |
| 205 | } | 205 | } |
| 206 | graphFolder_ = graphFolder_ + '/' + function.GetMagicName(); | 206 | graphFolder_ = graphFolder_ + '/' + function.GetMagicName(); |
| 207 | - bool res = CreateMultiLevelDir(graphFolder_); | 207 | + bool res = CreateDir(graphFolder_, true); |
| 208 | if (res == false) { | 208 | if (res == false) { |
| 209 | APASS_LOG_ERROR_F(Elements::Function, "Failed to create directory: [%s].", graphFolder_.c_str()); | 209 | APASS_LOG_ERROR_F(Elements::Function, "Failed to create directory: [%s].", graphFolder_.c_str()); |
| 210 | return FAILED; | 210 | return FAILED; |
| @@ -20,7 +20,7 @@ | |||
| 20 | 20 | ||
| 21 | #include "interface/configs/config_manager.h" | 21 | #include "interface/configs/config_manager.h" |
| 22 | #include "interface/program/program.h" | 22 | #include "interface/program/program.h" |
| 23 | -#include "interface/utils/file_utils.h" | 23 | +#include "utils/file_utils.h" |
| 24 | #include "utils/host_log/log_manager.h" | 24 | #include "utils/host_log/log_manager.h" |
| 25 | 25 | ||
| 26 | #undef MODULE_NAME | 26 | #undef MODULE_NAME |
| @@ -48,7 +48,7 @@ void DeleteDirIfEmpty(const std::string& folder) | |||
| 48 | std::string ResolveExtractPassLogScriptPath() | 48 | std::string ResolveExtractPassLogScriptPath() |
| 49 | { | 49 | { |
| 50 | // Installed layout: <lib>/scripts/extract_pass_log.py | 50 | // Installed layout: <lib>/scripts/extract_pass_log.py |
| 51 | - const std::string installedScriptPath = GetCurrentSharedLibPath() + "/scripts/" + kExtractPassLogScriptName; | 51 | + const std::string installedScriptPath = GetPyptoLibPath() + "/scripts/" + kExtractPassLogScriptName; |
| 52 | if (IsFileAccessible(installedScriptPath)) { | 52 | if (IsFileAccessible(installedScriptPath)) { |
| 53 | return installedScriptPath; | 53 | return installedScriptPath; |
| 54 | } | 54 | } |
| @@ -124,7 +124,7 @@ void ExtractPassLogByFunction(const Function& function, const std::string& strat | |||
| 124 | if (scriptPath.empty()) { | 124 | if (scriptPath.empty()) { |
| 125 | APASS_LOG_WARN_F( | 125 | APASS_LOG_WARN_F( |
| 126 | Elements::Function, "%s not found under install(%s/scripts), or source(%s).", kExtractPassLogScriptName, | 126 | Elements::Function, "%s not found under install(%s/scripts), or source(%s).", kExtractPassLogScriptName, |
| 127 | - GetCurrentSharedLibPath().c_str(), kExtractPassLogScriptInSource); | 127 | + GetPyptoLibPath().c_str(), kExtractPassLogScriptInSource); |
| 128 | return; | 128 | return; |
| 129 | } | 129 | } |
| 130 | 130 | ||
| @@ -30,7 +30,7 @@ | |||
| 30 | #include "interface/function/function.h" | 30 | #include "interface/function/function.h" |
| 31 | #include "interface/tensor/logical_tensor.h" | 31 | #include "interface/tensor/logical_tensor.h" |
| 32 | #include "interface/tensor/raw_tensor.h" | 32 | #include "interface/tensor/raw_tensor.h" |
| 33 | -#include "interface/utils/file_utils.h" | 33 | +#include "utils/file_utils.h" |
| 34 | #include "interface/tensor/hypercube_overlap_checker.h" | 34 | #include "interface/tensor/hypercube_overlap_checker.h" |
| 35 | #include "passes/pass_log/pass_log.h" | 35 | #include "passes/pass_log/pass_log.h" |
| 36 | #include "passes/pass_utils/graph_utils.h" | 36 | #include "passes/pass_utils/graph_utils.h" |
| @@ -281,7 +281,7 @@ Status CalRedundantCopy(Function& function, json& report) | |||
| 281 | 281 | ||
| 282 | void WriteHealthReport(const json& report, const std::string& reportPath, const std::string& filename) | 282 | void WriteHealthReport(const json& report, const std::string& reportPath, const std::string& filename) |
| 283 | { | 283 | { |
| 284 | - if (!CreateMultiLevelDir(reportPath)) { | 284 | + if (!CreateDir(reportPath, true)) { |
| 285 | APASS_LOG_ERROR_F(Elements::Operation, "Failed to create directory for health report"); | 285 | APASS_LOG_ERROR_F(Elements::Operation, "Failed to create directory for health report"); |
| 286 | } | 286 | } |
| 287 | std::ofstream out(reportPath + "/" + filename); | 287 | std::ofstream out(reportPath + "/" + filename); |
| @@ -13,6 +13,9 @@ | |||
| 13 | * \brief | 13 | * \brief |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | +#include <dlfcn.h> | ||
| 17 | + | ||
| 18 | +#include "tilefwk/pypto_fwk_log.h" | ||
| 16 | #include "internal_parser.h" | 19 | #include "internal_parser.h" |
| 17 | 20 | ||
| 18 | namespace npu { | 21 | namespace npu { |
| @@ -22,23 +25,12 @@ const std::string paths = "PATHS"; | |||
| 22 | const std::string comma = ","; | 25 | const std::string comma = ","; |
| 23 | const std::string direction = "->"; | 26 | const std::string direction = "->"; |
| 24 | 27 | ||
| 25 | -// Thread-safe in C++11: static local initialization is guaranteed to be thread-safe | 28 | +// Trim leading/trailing whitespace (space, tab, CR, LF). Returns a new string. |
| 26 | -std::string GetCurSharedLibPath() | 29 | +std::string TrimLine(std::string_view s) |
| 27 | { | 30 | { |
| 28 | - static std::string curLibPath; | 31 | + size_t start = s.find_first_not_of(" \t\n\r"); |
| 29 | - if (!curLibPath.empty()) { | 32 | + size_t end = s.find_last_not_of(" \t\n\r"); |
| 30 | - return curLibPath; | 33 | + return (start == std::string_view::npos) ? std::string{} : std::string(s.substr(start, end - start + 1)); |
| 31 | - } | ||
| 32 | - | ||
| 33 | - Dl_info info; | ||
| 34 | - if (dladdr(reinterpret_cast<void*>(GetCurSharedLibPath), &info)) { | ||
| 35 | - curLibPath = std::string(info.dli_fname); | ||
| 36 | - auto pos = curLibPath.rfind('/'); | ||
| 37 | - if (pos != std::string::npos) { | ||
| 38 | - curLibPath = curLibPath.substr(0, pos); | ||
| 39 | - } | ||
| 40 | - } | ||
| 41 | - return curLibPath; | ||
| 42 | } | 34 | } |
| 43 | 35 | ||
| 44 | std::vector<std::string> SplitByDelimiter(const std::string& str, const std::string& delimiter) | 36 | std::vector<std::string> SplitByDelimiter(const std::string& str, const std::string& delimiter) |
| @@ -78,47 +70,47 @@ MemoryType StringToMemoryType(const std::string& memType) | |||
| 78 | } | 70 | } |
| 79 | 71 | ||
| 80 | bool InternalParser::LoadInternalInfo() | 72 | bool InternalParser::LoadInternalInfo() |
| 81 | -{ | 73 | + { |
| 82 | - std::string internalFile = RealPath(GetCurSharedLibPath() + iniFile); | 74 | + std::string internalFile = RealPath(GetPyptoLibPath() + iniFile); |
| 83 | - PLATFORM_LOGD("Try to obtain internal info from [%s].", internalFile.c_str()); | 75 | + PLATFORM_LOGD("Try to obtain internal info from [%s].", internalFile.c_str()); |
| 84 | - if (!IsPathExist(internalFile)) { | 76 | + if (!IsPathExist(internalFile)) { |
| 85 | - return false; | 77 | + return false; |
| 86 | - } | ||
| 87 | - std::ifstream file(internalFile); | ||
| 88 | - if (!file.is_open()) { | ||
| 89 | - return false; | ||
| 90 | - } | ||
| 91 | - std::string line; | ||
| 92 | - std::string trimLine; | ||
| 93 | - std::string section; | ||
| 94 | - std::string info; | ||
| 95 | - bool currentSoc = true; | ||
| 96 | - while (std::getline(file, line)) { | ||
| 97 | - trimLine = TrimLine(line); | ||
| 98 | - if (trimLine[0] == '#') { | ||
| 99 | - continue; | ||
| 100 | } | 78 | } |
| 101 | - if (trimLine.find("}") != std::string::npos) { | 79 | + std::ifstream file(internalFile); |
| 102 | - currentSoc = true; | 80 | + if (!file.is_open()) { |
| 103 | - } else if (trimLine.empty() || !currentSoc) { | 81 | + return false; |
| 104 | - continue; | ||
| 105 | } | 82 | } |
| 106 | - if (trimLine.find("]") != std::string::npos) { | 83 | + std::string line; |
| 107 | - data_[section] = info; | 84 | + std::string trimLine; |
| 108 | - info.clear(); | 85 | + std::string section; |
| 109 | - } else if (trimLine.find("{") != std::string::npos) { | 86 | + std::string info; |
| 110 | - if (TrimLine(trimLine.substr(0, trimLine.find(':'))) != archType_) { | 87 | + bool currentSoc = true; |
| 111 | - currentSoc = false; | 88 | + while (std::getline(file, line)) { |
| 89 | + trimLine = TrimLine(line); | ||
| 90 | + if (trimLine[0] == '#') { | ||
| 91 | + continue; | ||
| 92 | + } | ||
| 93 | + if (trimLine.find("}") != std::string::npos) { | ||
| 94 | + currentSoc = true; | ||
| 95 | + } else if (trimLine.empty() || !currentSoc) { | ||
| 96 | + continue; | ||
| 97 | + } | ||
| 98 | + if (trimLine.find("]") != std::string::npos) { | ||
| 99 | + data_[section] = info; | ||
| 100 | + info.clear(); | ||
| 101 | + } else if (trimLine.find("{") != std::string::npos) { | ||
| 102 | + if (TrimLine(trimLine.substr(0, trimLine.find(':'))) != archType_) { | ||
| 103 | + currentSoc = false; | ||
| 104 | + } | ||
| 105 | + } else if (trimLine.find("[") != std::string::npos) { | ||
| 106 | + section = TrimLine(trimLine.substr(0, trimLine.find(':'))); | ||
| 107 | + } else if (trimLine.find("}") == std::string::npos) { | ||
| 108 | + info += trimLine; | ||
| 112 | } | 109 | } |
| 113 | - } else if (trimLine.find("[") != std::string::npos) { | ||
| 114 | - section = TrimLine(trimLine.substr(0, trimLine.find(':'))); | ||
| 115 | - } else if (trimLine.find("}") == std::string::npos) { | ||
| 116 | - info += trimLine; | ||
| 117 | } | 110 | } |
| 118 | - } | 111 | + file.close(); |
| 119 | - file.close(); | 112 | + PLATFORM_LOGD("Obtained internal info successfully."); |
| 120 | - PLATFORM_LOGD("Obtained internal info successfully."); | 113 | + return true; |
| 121 | - return true; | ||
| 122 | } | 114 | } |
| 123 | 115 | ||
| 124 | bool InternalParser::GetDataPath(std::vector<std::pair<MemoryType, MemoryType>>& dataPath) | 116 | bool InternalParser::GetDataPath(std::vector<std::pair<MemoryType, MemoryType>>& dataPath) |
| @@ -20,7 +20,7 @@ | |||
| 20 | #include <limits> | 20 | #include <limits> |
| 21 | #include <fstream> | 21 | #include <fstream> |
| 22 | #include <sys/stat.h> | 22 | #include <sys/stat.h> |
| 23 | -#include "tilefwk/file.h" | 23 | +#include "utils/file_utils.h" |
| 24 | #include "tilefwk/data_type.h" | 24 | #include "tilefwk/data_type.h" |
| 25 | 25 | ||
| 26 | namespace npu { | 26 | namespace npu { |
| @@ -12,9 +12,11 @@ | |||
| 12 | * \file platform_parser.cpp | 12 | * \file platform_parser.cpp |
| 13 | * \brief | 13 | * \brief |
| 14 | */ | 14 | */ |
| 15 | +#include <fstream> | ||
| 15 | 16 | ||
| 16 | #include "platform_parser.h" | 17 | #include "platform_parser.h" |
| 17 | #include "tilefwk/error_code.h" | 18 | #include "tilefwk/error_code.h" |
| 19 | +#include "simulation_platform/simulation_platform.h" | ||
| 18 | 20 | ||
| 19 | namespace npu { | 21 | namespace npu { |
| 20 | namespace tile_fwk { | 22 | namespace tile_fwk { |
| @@ -16,14 +16,7 @@ | |||
| 16 | #define PLATFORM_PARSER_H_ | 16 | #define PLATFORM_PARSER_H_ |
| 17 | 17 | ||
| 18 | #include <map> | 18 | #include <map> |
| 19 | -#include <set> | ||
| 20 | -#include <vector> | ||
| 21 | -#include <fstream> | ||
| 22 | -#include <unordered_map> | ||
| 23 | -#include "tilefwk/file.h" | ||
| 24 | #include "tilefwk/platform.h" | 19 | #include "tilefwk/platform.h" |
| 25 | -#include "tilefwk/pypto_fwk_log.h" | ||
| 26 | -#include "simulation_platform/simulation_platform.h" | ||
| 27 | 20 | ||
| 28 | namespace npu { | 21 | namespace npu { |
| 29 | namespace tile_fwk { | 22 | namespace tile_fwk { |
| @@ -47,6 +40,8 @@ public: | |||
| 47 | ~CmdParser() = default; | 40 | ~CmdParser() = default; |
| 48 | bool GetStringVal(const std::string& column, const std::string& key, std::string& val) const override; | 41 | bool GetStringVal(const std::string& column, const std::string& key, std::string& val) const override; |
| 49 | }; | 42 | }; |
| 43 | + | ||
| 44 | +std::string TrimLine(std::string_view s); | ||
| 50 | } // namespace tile_fwk | 45 | } // namespace tile_fwk |
| 51 | } // namespace npu | 46 | } // namespace npu |
| 52 | #endif | 47 | #endif |
| @@ -52,23 +52,9 @@ std::string DPlatformToSocVersion(DPlatform platform) | |||
| 52 | } | 52 | } |
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | -std::string SimulationPlatform::GetCurrentSharedLibPath() | ||
| 56 | -{ | ||
| 57 | - std::string currentLibPath; | ||
| 58 | - Dl_info info; | ||
| 59 | - if (dladdr(reinterpret_cast<void*>(&GetCurrentSharedLibPath), &info)) { | ||
| 60 | - currentLibPath = std::string(info.dli_fname); | ||
| 61 | - int32_t pos = currentLibPath.rfind('/'); | ||
| 62 | - if (pos >= 0) { | ||
| 63 | - currentLibPath = currentLibPath.substr(0, pos); | ||
| 64 | - } | ||
| 65 | - } | ||
| 66 | - return currentLibPath; | ||
| 67 | -} | ||
| 68 | - | ||
| 69 | std::string SimulationPlatform::GetDevicePlatform() | 55 | std::string SimulationPlatform::GetDevicePlatform() |
| 70 | { | 56 | { |
| 71 | - const std::string configPath = RealPath(GetCurrentSharedLibPath() + PLATFORM_INFO_RELATIVE_PATH + FWK_CONFIG_RELATIVE_PATH); | 57 | + const std::string configPath = RealPath(GetPyptoLibPath() + PLATFORM_INFO_RELATIVE_PATH + FWK_CONFIG_RELATIVE_PATH); |
| 72 | if (configPath.empty()) { | 58 | if (configPath.empty()) { |
| 73 | PLATFORM_LOGW("Failed to open tile_fwk_config.json, use default platform."); | 59 | PLATFORM_LOGW("Failed to open tile_fwk_config.json, use default platform."); |
| 74 | return DEFAULT_SOC_VERSION; | 60 | return DEFAULT_SOC_VERSION; |
| @@ -100,7 +86,7 @@ bool SimulationPlatform::GetCostModelPlatformRealPath(const std::string& socVers | |||
| 100 | platformSocVersion = GetDevicePlatform(); | 86 | platformSocVersion = GetDevicePlatform(); |
| 101 | PLATFORM_LOGD("Config specified SoC version:%s.", platformSocVersion.c_str()); | 87 | PLATFORM_LOGD("Config specified SoC version:%s.", platformSocVersion.c_str()); |
| 102 | } | 88 | } |
| 103 | - realPath = RealPath(GetCurrentSharedLibPath() + PLATFORM_INFO_RELATIVE_PATH + platformSocVersion + INI_EXTENSION); | 89 | + realPath = RealPath(GetPyptoLibPath() + PLATFORM_INFO_RELATIVE_PATH + platformSocVersion + INI_EXTENSION); |
| 104 | if (realPath.empty()) { | 90 | if (realPath.empty()) { |
| 105 | return false; | 91 | return false; |
| 106 | } | 92 | } |
| @@ -19,7 +19,7 @@ | |||
| 19 | 19 | ||
| 20 | #include <string> | 20 | #include <string> |
| 21 | #include <cstdint> | 21 | #include <cstdint> |
| 22 | -#include "tilefwk/file.h" | 22 | +#include "utils/file_utils.h" |
| 23 | #include "tilefwk/pypto_fwk_log.h" | 23 | #include "tilefwk/pypto_fwk_log.h" |
| 24 | 24 | ||
| 25 | namespace npu { | 25 | namespace npu { |
| @@ -32,7 +32,6 @@ public: | |||
| 32 | bool GetCostModelPlatformRealPath(const std::string& socVersion, std::string& realPath); | 32 | bool GetCostModelPlatformRealPath(const std::string& socVersion, std::string& realPath); |
| 33 | 33 | ||
| 34 | private: | 34 | private: |
| 35 | - static std::string GetCurrentSharedLibPath(); | ||
| 36 | static std::string GetDevicePlatform(); | 35 | static std::string GetDevicePlatform(); |
| 37 | }; | 36 | }; |
| 38 | } // namespace tile_fwk | 37 | } // namespace tile_fwk |
| @@ -10,7 +10,8 @@ | |||
| 10 | 10 | ||
| 11 | file(GLOB_RECURSE _Utils_Srcs | 11 | file(GLOB_RECURSE _Utils_Srcs |
| 12 | host_log/*.cpp | 12 | host_log/*.cpp |
| 13 | - error_manager/*.cpp) | 13 | + error_manager/*.cpp |
| 14 | + file_utils.cpp) | ||
| 14 | 15 | ||
| 15 | add_library(tile_fwk_utils SHARED) | 16 | add_library(tile_fwk_utils SHARED) |
| 16 | target_sources(tile_fwk_utils PRIVATE ${_Utils_Srcs}) | 17 | target_sources(tile_fwk_utils PRIVATE ${_Utils_Srcs}) |
| @@ -0,0 +1,271 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file file_utils.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "file_utils.h" | ||
| 17 | + | ||
| 18 | +#include <cstring> | ||
| 19 | +#include <fstream> | ||
| 20 | +#include <vector> | ||
| 21 | +#include <algorithm> | ||
| 22 | +#include <fcntl.h> | ||
| 23 | +#include <climits> | ||
| 24 | +#include <unistd.h> | ||
| 25 | +#include <sys/stat.h> | ||
| 26 | +#include <dirent.h> | ||
| 27 | +#include <dlfcn.h> | ||
| 28 | +#include <ftw.h> | ||
| 29 | +#include <iostream> | ||
| 30 | + | ||
| 31 | +#include "securec.h" | ||
| 32 | + | ||
| 33 | +#include "tilefwk/pypto_fwk_log.h" | ||
| 34 | + | ||
| 35 | +namespace npu::tile_fwk { | ||
| 36 | +namespace { | ||
| 37 | +// 0755 for created directories, 0640 for lock files. | ||
| 38 | +constexpr mode_t DIR_MODE = 0755; | ||
| 39 | +constexpr mode_t FILE_MODE = 0640; | ||
| 40 | + | ||
| 41 | +int RemoveFile(const char* path, const struct stat* sb, int flag, struct FTW* ftwbuf) | ||
| 42 | +{ | ||
| 43 | + (void)sb; | ||
| 44 | + (void)ftwbuf; | ||
| 45 | + if (flag == FTW_F) { | ||
| 46 | + return remove(path); | ||
| 47 | + } else if (flag == FTW_DP) { | ||
| 48 | + return rmdir(path); | ||
| 49 | + } | ||
| 50 | + return 0; | ||
| 51 | +} | ||
| 52 | +} // namespace | ||
| 53 | + | ||
| 54 | +// do not use PYPTO_LOGX in it's used by log_manager | ||
| 55 | +bool CreateDir(const std::string& path, bool recursive) | ||
| 56 | +{ | ||
| 57 | + char fullpath[PATH_MAX]; | ||
| 58 | + if (strcpy_s(fullpath, PATH_MAX, path.c_str()) != 0) { | ||
| 59 | + std::cerr << "Create dir[" << path << "] failed, path is too long" << std::endl; | ||
| 60 | + return false; | ||
| 61 | + } | ||
| 62 | + if (recursive) { | ||
| 63 | + // Walk one '/' at a time, creating every non-empty prefix. Empty prefixes | ||
| 64 | + // come from leading '/' (e.g. "/a") or consecutive slashes (e.g. "a///b") | ||
| 65 | + // and are skipped, so "///" patterns are treated as a single separator. | ||
| 66 | + for (char* p = fullpath + 1; *p != '\0'; ++p) { | ||
| 67 | + if (*p != '/') { | ||
| 68 | + continue; | ||
| 69 | + } | ||
| 70 | + *p = '\0'; | ||
| 71 | + if (fullpath[0] != '\0' && mkdir(fullpath, DIR_MODE) != 0 && errno != EEXIST) { | ||
| 72 | + std::cerr << "Create dir[" << fullpath << "] failed, reason is " << errno << std::endl; | ||
| 73 | + return false; | ||
| 74 | + } | ||
| 75 | + *p = '/'; | ||
| 76 | + } | ||
| 77 | + } | ||
| 78 | + if (mkdir(fullpath, DIR_MODE) != 0 && errno != EEXIST) { | ||
| 79 | + std::cerr << "Create dir[" << fullpath << "] failed, reason is " << errno << std::endl; | ||
| 80 | + return false; | ||
| 81 | + } | ||
| 82 | + return true; | ||
| 83 | +} | ||
| 84 | + | ||
| 85 | +void DeleteDir(const std::string& path, bool recursive) | ||
| 86 | +{ | ||
| 87 | + if (recursive) { | ||
| 88 | + if (nftw(path.c_str(), RemoveFile, 0x10, FTW_DEPTH | FTW_PHYS)) { | ||
| 89 | + PYPTO_LOGW("Delete dir[%s] failed, reason is %d", path.c_str(), errno); | ||
| 90 | + } | ||
| 91 | + } else { | ||
| 92 | + if (rmdir(path.c_str()) != 0) { | ||
| 93 | + PYPTO_LOGW("Delete dir[%s] failed, reason is %d", path.c_str(), errno); | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +bool SaveFile(const std::string& path, const uint8_t* data, size_t size) | ||
| 99 | +{ | ||
| 100 | + std::ofstream of(path, std::ios::binary); | ||
| 101 | + if (!of.is_open()) { | ||
| 102 | + return false; | ||
| 103 | + } | ||
| 104 | + of.write((const char*)data, size); | ||
| 105 | + return of.good(); | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +void SaveFileSafe(const std::string& path, const uint8_t* data, size_t size) | ||
| 109 | +{ | ||
| 110 | + std::string tmpfile = std::string(path) + ".tmp"; | ||
| 111 | + if (SaveFile(tmpfile, data, size)) { | ||
| 112 | + Rename(tmpfile, path); | ||
| 113 | + } | ||
| 114 | +} | ||
| 115 | + | ||
| 116 | +void Rename(const std::string& src, const std::string& dst) | ||
| 117 | +{ | ||
| 118 | + if (rename(src.c_str(), dst.c_str()) != 0) { | ||
| 119 | + PYPTO_LOGW("Rename file %s to %s failed, reason is %d", src.c_str(), dst.c_str(), errno); | ||
| 120 | + } | ||
| 121 | +} | ||
| 122 | + | ||
| 123 | +std::vector<uint8_t> ReadFile(const std::string& path, bool *isSuccess) | ||
| 124 | +{ | ||
| 125 | + if (isSuccess != nullptr) { | ||
| 126 | + *isSuccess = false; | ||
| 127 | + } | ||
| 128 | + std::vector<uint8_t> binary; | ||
| 129 | + std::ifstream ifs(path, std::ios::binary); | ||
| 130 | + if (!ifs.is_open()) { | ||
| 131 | + PYPTO_LOGW("Open file %s failed.", path.c_str()); | ||
| 132 | + return binary; | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + ifs.seekg(0, std::ios::end); | ||
| 136 | + const std::streamoff size = ifs.tellg(); | ||
| 137 | + ifs.seekg(0, std::ios::beg); | ||
| 138 | + if (size < 0) { | ||
| 139 | + PYPTO_LOGW("Read file %s failed.", path.c_str()); | ||
| 140 | + return binary; | ||
| 141 | + } | ||
| 142 | + binary.resize(static_cast<size_t>(size)); | ||
| 143 | + ifs.read(reinterpret_cast<char*>(binary.data()), size); | ||
| 144 | + if (!ifs.good()) { | ||
| 145 | + PYPTO_LOGW("Read file %s failed.", path.c_str()); | ||
| 146 | + return binary; | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + if (isSuccess != nullptr) { | ||
| 150 | + *isSuccess = true; | ||
| 151 | + } | ||
| 152 | + return binary; | ||
| 153 | +} | ||
| 154 | + | ||
| 155 | +void Rename(const char* oldPath, const char* newPath) | ||
| 156 | +{ | ||
| 157 | + if (rename(oldPath, newPath) != 0) { | ||
| 158 | + PYPTO_LOGW("Rename file %s to %s failed, reason is %d", oldPath, newPath, errno); | ||
| 159 | + } | ||
| 160 | +} | ||
| 161 | + | ||
| 162 | +bool CopyFile(const std::string& src, const std::string& dst) | ||
| 163 | +{ | ||
| 164 | + std::ifstream ifs(src, std::ios::binary); | ||
| 165 | + std::ofstream ofs(dst, std::ios::binary | std::ios::trunc); | ||
| 166 | + if (!ifs.is_open() || !ofs.is_open()) { | ||
| 167 | + return false; | ||
| 168 | + } | ||
| 169 | + | ||
| 170 | + constexpr size_t kBufferSize = 16 * 1024; | ||
| 171 | + char buffer[kBufferSize]; | ||
| 172 | + while (ofs.good()) { | ||
| 173 | + ifs.read(buffer, kBufferSize); | ||
| 174 | + std::streamsize n = ifs.gcount(); | ||
| 175 | + if (n <= 0) { | ||
| 176 | + break; | ||
| 177 | + } | ||
| 178 | + ofs.write(buffer, n); | ||
| 179 | + } | ||
| 180 | + return ofs.good() && !ifs.bad(); | ||
| 181 | +} | ||
| 182 | + | ||
| 183 | +std::string GetCwd() | ||
| 184 | +{ | ||
| 185 | + char path[PATH_MAX]; | ||
| 186 | + auto cwd = getcwd(path, PATH_MAX); | ||
| 187 | + if (cwd == nullptr) { | ||
| 188 | + PYPTO_LOGW("Failed to call getcwd()"); | ||
| 189 | + return "."; | ||
| 190 | + } | ||
| 191 | + return cwd; | ||
| 192 | +} | ||
| 193 | + | ||
| 194 | +void RemoveOldDirectories(const std::string& path, const std::string& prefix, size_t kept) | ||
| 195 | +{ | ||
| 196 | + DIR* dir = opendir(path.c_str()); | ||
| 197 | + if (dir == nullptr) { | ||
| 198 | + PYPTO_LOGW("Failed to opendir: %s", path.c_str()); | ||
| 199 | + return; | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + struct dirent* entry; | ||
| 203 | + struct stat statBuf; | ||
| 204 | + std::vector<std::pair<std::string, int64_t>> dirList; | ||
| 205 | + while ((entry = readdir(dir)) != nullptr) { | ||
| 206 | + if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) { | ||
| 207 | + continue; | ||
| 208 | + } | ||
| 209 | + if (strncmp(entry->d_name, prefix.c_str(), prefix.size()) != 0) { | ||
| 210 | + continue; | ||
| 211 | + } | ||
| 212 | + std::string dirname = path + "/" + entry->d_name; | ||
| 213 | + if (stat(dirname.c_str(), &statBuf) == 0 && S_ISDIR(statBuf.st_mode)) { | ||
| 214 | + dirList.emplace_back(dirname, statBuf.st_mtime); | ||
| 215 | + } | ||
| 216 | + } | ||
| 217 | + | ||
| 218 | + closedir(dir); | ||
| 219 | + | ||
| 220 | + std::sort(dirList.begin(), dirList.end(), [](const auto& a, const auto& b) { return a.second < b.second; }); | ||
| 221 | + for (size_t i = kept; i < dirList.size(); ++i) { | ||
| 222 | + DeleteDir(dirList[i].first, true); | ||
| 223 | + } | ||
| 224 | +} | ||
| 225 | + | ||
| 226 | +// do not use PYPTO_LOGX in it's used by log_manager | ||
| 227 | +std::vector<std::string> GetFiles(const std::string& path, const std::string& ext) | ||
| 228 | +{ | ||
| 229 | + std::vector<std::string> files; | ||
| 230 | + DIR* dir = opendir(path.c_str()); | ||
| 231 | + if (dir == nullptr) { | ||
| 232 | + return files; | ||
| 233 | + } | ||
| 234 | + | ||
| 235 | + struct dirent* ent; | ||
| 236 | + while ((ent = readdir(dir)) != nullptr) { | ||
| 237 | + if (ent->d_type == DT_DIR) { | ||
| 238 | + continue; | ||
| 239 | + } | ||
| 240 | + if (!ext.empty()) { | ||
| 241 | + auto pos = strrchr(ent->d_name, '.'); | ||
| 242 | + if (pos == nullptr || strcmp(pos + 1, ext.c_str())) { | ||
| 243 | + continue; | ||
| 244 | + } | ||
| 245 | + } | ||
| 246 | + files.push_back(ent->d_name); | ||
| 247 | + } | ||
| 248 | + closedir(dir); | ||
| 249 | + std::sort(files.begin(), files.end()); | ||
| 250 | + return files; | ||
| 251 | +} | ||
| 252 | + | ||
| 253 | +std::string GetPyptoLibPath() | ||
| 254 | +{ | ||
| 255 | + static std::string path; | ||
| 256 | + static std::once_flag once; | ||
| 257 | + std::call_once(once, [&] { | ||
| 258 | + Dl_info info; | ||
| 259 | + if (!dladdr(reinterpret_cast<void*>(GetPyptoLibPath), &info) || info.dli_fname == nullptr) { | ||
| 260 | + return; // leave empty | ||
| 261 | + } | ||
| 262 | + if (const char* pos = strrchr(info.dli_fname, '/'); pos != nullptr) { | ||
| 263 | + path.assign(info.dli_fname, pos - info.dli_fname); | ||
| 264 | + } else { | ||
| 265 | + path = info.dli_fname; | ||
| 266 | + } | ||
| 267 | + }); | ||
| 268 | + return path; | ||
| 269 | +} | ||
| 270 | + | ||
| 271 | +} // namespace npu::tile_fwk | ||
| @@ -0,0 +1,88 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file file_utils.h | ||
| 13 | + * \brief Path / file / directory helpers (host side). | ||
| 14 | + */ | ||
| 15 | +#pragma once | ||
| 16 | + | ||
| 17 | +#include <climits> | ||
| 18 | +#include <cstdint> | ||
| 19 | +#include <cstring> | ||
| 20 | +#include <cstdlib> | ||
| 21 | +#include <string> | ||
| 22 | +#include <sys/stat.h> | ||
| 23 | +#include <vector> | ||
| 24 | +#include <dlfcn.h> | ||
| 25 | + | ||
| 26 | +namespace npu::tile_fwk { | ||
| 27 | +// --- Path utilities --- | ||
| 28 | +// Declared inline because leaf translation units that do not link the | ||
| 29 | +// file_utils object (e.g. tile_fwk_cann_host_runtime) use them. | ||
| 30 | + | ||
| 31 | +inline std::string RealPath(const std::string& path) | ||
| 32 | +{ | ||
| 33 | + char fullpath[PATH_MAX]; | ||
| 34 | + if (realpath(path.c_str(), fullpath) == nullptr) { | ||
| 35 | + return {}; | ||
| 36 | + } | ||
| 37 | + return std::string(fullpath); | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +inline bool IsPathExist(const std::string& path) | ||
| 41 | +{ | ||
| 42 | + struct stat buf; | ||
| 43 | + return (::stat(path.c_str(), &buf) == 0); | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +inline uint64_t GetFileSize(const std::string& path, bool* isSuccess = nullptr) | ||
| 47 | +{ | ||
| 48 | + struct stat buf; | ||
| 49 | + bool succ = ::stat(path.c_str(), &buf) == 0; | ||
| 50 | + if (isSuccess) { | ||
| 51 | + *isSuccess = succ; | ||
| 52 | + } | ||
| 53 | + return succ ? buf.st_size : 0; | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +inline void DeleteFile(const std::string& path) { remove(path.c_str()); } | ||
| 57 | + | ||
| 58 | +std::string GetPyptoLibPath(); | ||
| 59 | +std::string GetCwd(); | ||
| 60 | + | ||
| 61 | +bool CreateDir(const std::string& path, bool recursive = false); | ||
| 62 | +void DeleteDir(const std::string& path, bool recursive = false); | ||
| 63 | +void Rename(const std::string& src, const std::string& dst); | ||
| 64 | +bool SaveFile(const std::string& path, const uint8_t* data, size_t size); | ||
| 65 | +void SaveFileSafe(const std::string& path, const uint8_t* data, size_t size); | ||
| 66 | +std::vector<uint8_t> ReadFile(const std::string& path, bool* isSuccess = nullptr); | ||
| 67 | +bool CopyFile(const std::string& src, const std::string& dst); | ||
| 68 | + | ||
| 69 | +void RemoveOldDirectories(const std::string& path, const std::string& prefix, size_t kept); | ||
| 70 | +std::vector<std::string> GetFiles(const std::string& path, const std::string& ext = ""); | ||
| 71 | + | ||
| 72 | +inline bool SaveFile(const std::string& path, const std::string& content) | ||
| 73 | +{ | ||
| 74 | + return SaveFile(path, (const uint8_t*)content.c_str(), content.size()); | ||
| 75 | +} | ||
| 76 | +inline bool SaveFile(const std::string& path, const std::vector<uint8_t>& content) | ||
| 77 | +{ | ||
| 78 | + return SaveFile(path, (const uint8_t*)content.data(), content.size()); | ||
| 79 | +} | ||
| 80 | +inline void SaveFileSafe(const std::string& path, const std::string& content) | ||
| 81 | +{ | ||
| 82 | + SaveFileSafe(path, (const uint8_t*)content.c_str(), content.size()); | ||
| 83 | +} | ||
| 84 | +inline void SaveFileSafe(const std::string& path, const std::vector<uint8_t>& content) | ||
| 85 | +{ | ||
| 86 | + SaveFileSafe(path, (const uint8_t*)content.data(), content.size()); | ||
| 87 | +} | ||
| 88 | +} // namespace npu::tile_fwk | ||
| @@ -1,120 +0,0 @@ | |||
| 1 | -/** | ||
| 2 | - * Copyright (c) 2025-2026 Huawei Technologies Co., Ltd. | ||
| 3 | - * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | - * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | - * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | - * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | - * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | - */ | ||
| 10 | - | ||
| 11 | -/*! | ||
| 12 | - * \file log_file_utils.h | ||
| 13 | - * \brief | ||
| 14 | - */ | ||
| 15 | - | ||
| 16 | -#include <climits> | ||
| 17 | -#include <string> | ||
| 18 | -#include <sys/stat.h> | ||
| 19 | -#include <queue> | ||
| 20 | -#include <dirent.h> | ||
| 21 | - | ||
| 22 | -namespace npu::tile_fwk { | ||
| 23 | -inline std::string GetRealPath(const std::string& path) | ||
| 24 | -{ | ||
| 25 | - if (path.empty()) { | ||
| 26 | - return ""; | ||
| 27 | - } | ||
| 28 | - if (path.size() >= PATH_MAX) { | ||
| 29 | - return ""; | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - // PATH_MAX is the system marco, indicate the maximum length for file path | ||
| 33 | - // pclint check one param in stack can not exceed 1K bytes | ||
| 34 | - char resovedPath[PATH_MAX] = {0x00}; | ||
| 35 | - | ||
| 36 | - std::string res; | ||
| 37 | - | ||
| 38 | - // path not exists or not allowed to read return nullptr | ||
| 39 | - // path exists and readable, return the resoved path | ||
| 40 | - if (realpath(path.c_str(), resovedPath) != nullptr) { | ||
| 41 | - res = resovedPath; | ||
| 42 | - } | ||
| 43 | - return res; | ||
| 44 | -} | ||
| 45 | - | ||
| 46 | -bool CreateSingleLevelDir(const char* dirPath) | ||
| 47 | -{ | ||
| 48 | - std::string realPath = GetRealPath(dirPath); | ||
| 49 | - if (realPath.empty()) { | ||
| 50 | - int32_t ret = 0; | ||
| 51 | - ret = mkdir(dirPath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); // 755 | ||
| 52 | - if (ret != 0 && errno != EEXIST) { | ||
| 53 | - return false; | ||
| 54 | - } | ||
| 55 | - } | ||
| 56 | - return true; | ||
| 57 | -} | ||
| 58 | - | ||
| 59 | -bool CreateMultiLevelDirectory(const std::string& directoryPath) | ||
| 60 | -{ | ||
| 61 | - auto dirPathLen = directoryPath.length(); | ||
| 62 | - if (dirPathLen >= PATH_MAX) { | ||
| 63 | - return false; | ||
| 64 | - } | ||
| 65 | - char tmpDirPath[PATH_MAX] = {}; | ||
| 66 | - for (size_t i = 0; i < dirPathLen; ++i) { | ||
| 67 | - tmpDirPath[i] = directoryPath[i]; | ||
| 68 | - if ((tmpDirPath[i] == '\\') || (tmpDirPath[i] == '/')) { | ||
| 69 | - if (access(tmpDirPath, F_OK) == 0) { | ||
| 70 | - continue; | ||
| 71 | - } | ||
| 72 | - if (!CreateSingleLevelDir(tmpDirPath)) { | ||
| 73 | - return false; | ||
| 74 | - } | ||
| 75 | - } | ||
| 76 | - } | ||
| 77 | - | ||
| 78 | - return CreateSingleLevelDir(directoryPath.c_str()); | ||
| 79 | -} | ||
| 80 | - | ||
| 81 | -void RemoveFile(const std::string& path) | ||
| 82 | -{ | ||
| 83 | - if (path.empty()) { | ||
| 84 | - return; | ||
| 85 | - } | ||
| 86 | - struct stat statBuf; | ||
| 87 | - if (lstat(path.c_str(), &statBuf) != 0) { | ||
| 88 | - return; | ||
| 89 | - } | ||
| 90 | - if (S_ISREG(statBuf.st_mode) == 0) { | ||
| 91 | - return; | ||
| 92 | - } | ||
| 93 | - (void)remove(path.c_str()); | ||
| 94 | -} | ||
| 95 | - | ||
| 96 | -void LoadFileFromDir( | ||
| 97 | - const std::string& dirPath, const std::string& filePrefix, const std::string& fileSuffix, | ||
| 98 | - std::queue<std::string>& files) | ||
| 99 | -{ | ||
| 100 | - DIR* dir = opendir(dirPath.c_str()); | ||
| 101 | - if (dir == nullptr) { | ||
| 102 | - return; | ||
| 103 | - } | ||
| 104 | - struct dirent* dirp = nullptr; | ||
| 105 | - while ((dirp = readdir(dir)) != nullptr) { | ||
| 106 | - if (dirp->d_name[0] == '.') { | ||
| 107 | - continue; | ||
| 108 | - } | ||
| 109 | - std::string fileName = dirp->d_name; | ||
| 110 | - if (fileName.find(filePrefix) != 0) { | ||
| 111 | - continue; | ||
| 112 | - } | ||
| 113 | - if (fileName.find(fileSuffix) == std::string::npos) { | ||
| 114 | - continue; | ||
| 115 | - } | ||
| 116 | - files.push(dirPath + "/" + fileName); | ||
| 117 | - } | ||
| 118 | - closedir(dir); | ||
| 119 | -} | ||
| 120 | -} // namespace npu::tile_fwk | ||
| @@ -24,9 +24,8 @@ | |||
| 24 | #include <iomanip> | 24 | #include <iomanip> |
| 25 | #include <iostream> | 25 | #include <iostream> |
| 26 | #include <stdexcept> | 26 | #include <stdexcept> |
| 27 | -#include <map> | ||
| 28 | #include "securec.h" | 27 | #include "securec.h" |
| 29 | -#include "host_log/log_file_utils.h" | 28 | +#include "file_utils.h" |
| 30 | 29 | ||
| 31 | namespace npu::tile_fwk { | 30 | namespace npu::tile_fwk { |
| 32 | namespace { | 31 | namespace { |
| @@ -41,7 +40,6 @@ constexpr const char* kModuleName = "PYPTO"; | |||
| 41 | constexpr const char* kModulePrefix = "PYPTO="; | 40 | constexpr const char* kModulePrefix = "PYPTO="; |
| 42 | constexpr const char* kHostLogFilePrefix = "pypto-log-"; | 41 | constexpr const char* kHostLogFilePrefix = "pypto-log-"; |
| 43 | constexpr const char* kDevLogFilePrefix = "pypto-simulation-"; | 42 | constexpr const char* kDevLogFilePrefix = "pypto-simulation-"; |
| 44 | -constexpr const char* kLogFileSuffix = ".log"; | ||
| 45 | constexpr int64_t kMaxLogFileSize = 20 * 1024 * 1024; // 10MB | 43 | constexpr int64_t kMaxLogFileSize = 20 * 1024 * 1024; // 10MB |
| 46 | 44 | ||
| 47 | const std::string kLogLevelNoneStr = "[NONE] "; | 45 | const std::string kLogLevelNoneStr = "[NONE] "; |
| @@ -54,6 +52,12 @@ uint64_t GetTid() | |||
| 54 | return tid; | 52 | return tid; |
| 55 | } | 53 | } |
| 56 | 54 | ||
| 55 | +static bool StartWith(const std::string& s, const std::string& prefix) | ||
| 56 | +{ | ||
| 57 | + return s.size() >= prefix.size() && | ||
| 58 | + s.compare(0, prefix.size(), prefix) == 0; | ||
| 59 | +} | ||
| 60 | + | ||
| 57 | int64_t GetPid() { return getpid(); } | 61 | int64_t GetPid() { return getpid(); } |
| 58 | 62 | ||
| 59 | const std::string& GetLogLevelStr(const LogLevel logLevel) | 63 | const std::string& GetLogLevelStr(const LogLevel logLevel) |
| @@ -132,7 +136,7 @@ void RemoveRedundantLogFiles(const size_t maxLogFileNum, std::queue<std::string> | |||
| 132 | return; | 136 | return; |
| 133 | } | 137 | } |
| 134 | while (logFilesQueue.size() > maxLogFileNum) { | 138 | while (logFilesQueue.size() > maxLogFileNum) { |
| 135 | - RemoveFile(logFilesQueue.front()); | 139 | + DeleteFile(logFilesQueue.front().c_str()); |
| 136 | logFilesQueue.pop(); | 140 | logFilesQueue.pop(); |
| 137 | } | 141 | } |
| 138 | } | 142 | } |
| @@ -143,6 +147,7 @@ LogManager& LogManager::Instance() | |||
| 143 | return instance; | 147 | return instance; |
| 144 | } | 148 | } |
| 145 | 149 | ||
| 150 | +// take care to use file api in file_utils.h, ensue it does not use PYPTO_LOGX | ||
| 146 | LogManager::LogManager() | 151 | LogManager::LogManager() |
| 147 | { | 152 | { |
| 148 | std::string envGlobalLogLevel; | 153 | std::string envGlobalLogLevel; |
| @@ -187,17 +192,26 @@ LogManager::LogManager() | |||
| 187 | } | 192 | } |
| 188 | } | 193 | } |
| 189 | 194 | ||
| 195 | + auto getLogFiles = [](const std::string& path, const std::string& prefix, const std::string& ext) { | ||
| 196 | + std::queue<std::string> files; | ||
| 197 | + for (auto file: GetFiles(path, ext)) { | ||
| 198 | + if (StartWith(file, prefix)) { | ||
| 199 | + files.push(RealPath(path + "/" + file)); | ||
| 200 | + } | ||
| 201 | + } | ||
| 202 | + return files; | ||
| 203 | + }; | ||
| 204 | + | ||
| 190 | hostLogDir_ = envLogDirPath + "/debug/plog"; | 205 | hostLogDir_ = envLogDirPath + "/debug/plog"; |
| 191 | - deviceLogDir_ = envLogDirPath + "/debug/device-" + std::to_string(attr_.deviceId); | 206 | + if (CreateDir(hostLogDir_, true)) { |
| 192 | - if (CreateMultiLevelDirectory(hostLogDir_) && CreateMultiLevelDirectory(deviceLogDir_)) { | 207 | + hostLogFiles_ = getLogFiles(hostLogDir_, kHostLogFilePrefix, "log"); |
| 193 | - hostLogDir_ = GetRealPath(hostLogDir_); | ||
| 194 | - deviceLogDir_ = GetRealPath(deviceLogDir_); | ||
| 195 | - LoadFileFromDir(hostLogDir_, kHostLogFilePrefix, kLogFileSuffix, hostLogFiles_); | ||
| 196 | RemoveRedundantLogFiles(maxLogFileNum_, hostLogFiles_); | 208 | RemoveRedundantLogFiles(maxLogFileNum_, hostLogFiles_); |
| 197 | - LoadFileFromDir(deviceLogDir_, kDevLogFilePrefix, kLogFileSuffix, devLogFiles_); | 209 | + } |
| 210 | + | ||
| 211 | + deviceLogDir_ = envLogDirPath + "/debug/device-" + std::to_string(attr_.deviceId); | ||
| 212 | + if (CreateDir(deviceLogDir_, true)) { | ||
| 213 | + devLogFiles_ = getLogFiles(deviceLogDir_, kDevLogFilePrefix, "log"); | ||
| 198 | RemoveRedundantLogFiles(maxLogFileNum_, devLogFiles_); | 214 | RemoveRedundantLogFiles(maxLogFileNum_, devLogFiles_); |
| 199 | - } else { | ||
| 200 | - std::cerr << "Fail to create directory: " << envLogDirPath << std::endl; | ||
| 201 | } | 215 | } |
| 202 | } | 216 | } |
| 203 | 217 | ||
| @@ -206,8 +220,6 @@ LogManager::~LogManager() | |||
| 206 | level_ = LogLevel::ERROR; | 220 | level_ = LogLevel::ERROR; |
| 207 | enableStdOut_ = true; | 221 | enableStdOut_ = true; |
| 208 | maxLogFileNum_ = MAX_LOG_FILES_NUM; | 222 | maxLogFileNum_ = MAX_LOG_FILES_NUM; |
| 209 | - hostLogDir_.clear(); | ||
| 210 | - deviceLogDir_.clear(); | ||
| 211 | if (hostFileStream_.is_open()) { | 223 | if (hostFileStream_.is_open()) { |
| 212 | hostFileStream_.flush(); | 224 | hostFileStream_.flush(); |
| 213 | hostFileStream_.close(); | 225 | hostFileStream_.close(); |
| @@ -328,7 +340,7 @@ void LogManager::CreateAndOpenNewLogFile() | |||
| 328 | { | 340 | { |
| 329 | std::ostringstream oss; | 341 | std::ostringstream oss; |
| 330 | const std::string& logFilePrefix = attr_.isDevice ? kDevLogFilePrefix : kHostLogFilePrefix; | 342 | const std::string& logFilePrefix = attr_.isDevice ? kDevLogFilePrefix : kHostLogFilePrefix; |
| 331 | - oss << GetLogDir() << "/" << logFilePrefix << GetPid() << "_" << GetCurrentTimeStr() << kLogFileSuffix; | 343 | + oss << GetLogDir() << "/" << logFilePrefix << GetPid() << "_" << GetCurrentTimeStr() << ".log"; |
| 332 | std::string newLogFileName = oss.str(); | 344 | std::string newLogFileName = oss.str(); |
| 333 | GetCurrentFileStream().open(newLogFileName); | 345 | GetCurrentFileStream().open(newLogFileName); |
| 334 | AddNewLogFile(newLogFileName); | 346 | AddNewLogFile(newLogFileName); |
| @@ -339,7 +351,7 @@ void LogManager::AddNewLogFile(const std::string& newLogFileName) | |||
| 339 | std::queue<std::string>& logFilesQueue = GetLogFilesQueue(); | 351 | std::queue<std::string>& logFilesQueue = GetLogFilesQueue(); |
| 340 | logFilesQueue.push(newLogFileName); | 352 | logFilesQueue.push(newLogFileName); |
| 341 | while (logFilesQueue.size() > maxLogFileNum_) { | 353 | while (logFilesQueue.size() > maxLogFileNum_) { |
| 342 | - RemoveFile(logFilesQueue.front()); // remove file | 354 | + DeleteFile(logFilesQueue.front().c_str()); // remove file |
| 343 | logFilesQueue.pop(); | 355 | logFilesQueue.pop(); |
| 344 | } | 356 | } |
| 345 | } | 357 | } |
| @@ -13,7 +13,7 @@ | |||
| 13 | * \brief | 13 | * \brief |
| 14 | */ | 14 | */ |
| 15 | #include <gtest/gtest.h> | 15 | #include <gtest/gtest.h> |
| 16 | -#include "interface/utils/file_utils.h" | 16 | +#include "utils/file_utils.h" |
| 17 | #include "test_suite_stest_ops.h" | 17 | #include "test_suite_stest_ops.h" |
| 18 | #include "interface/interpreter/raw_tensor_data.h" | 18 | #include "interface/interpreter/raw_tensor_data.h" |
| 19 | #include "operator/models/deepseek/page_attention.h" | 19 | #include "operator/models/deepseek/page_attention.h" |
| @@ -15,7 +15,7 @@ | |||
| 15 | 15 | ||
| 16 | #include "test_suite_stest_ops.h" | 16 | #include "test_suite_stest_ops.h" |
| 17 | #include "codegen/npu/cloudnpu/codegen_cloudnpu.h" | 17 | #include "codegen/npu/cloudnpu/codegen_cloudnpu.h" |
| 18 | -#include "interface/utils/file_utils.h" | 18 | +#include "utils/file_utils.h" |
| 19 | 19 | ||
| 20 | namespace npu::tile_fwk { | 20 | namespace npu::tile_fwk { |
| 21 | 21 | ||
| @@ -23,7 +23,7 @@ class TestTileOpAdd : public npu::tile_fwk::stest::TestSuite_STest_Ops_Aihac {}; | |||
| 23 | 23 | ||
| 24 | int CompileCCEForSingleOpTest(const std::string& srcFile, const std::string& objFile, bool isCube) | 24 | int CompileCCEForSingleOpTest(const std::string& srcFile, const std::string& objFile, bool isCube) |
| 25 | { | 25 | { |
| 26 | - std::string curPath = GetCurRunningPath(); | 26 | + std::string curPath = GetCwd(); |
| 27 | std::string codeSrcPath = curPath.append("/../../../../"); | 27 | std::string codeSrcPath = curPath.append("/../../../../"); |
| 28 | 28 | ||
| 29 | std::string coreType = isCube ? "dav-c220-cube" : "dav-c220-vec"; | 29 | std::string coreType = isCube ? "dav-c220-cube" : "dav-c220-vec"; |
| @@ -62,7 +62,7 @@ void CompileTestCCE(const std::string& cceFileName) | |||
| 62 | { | 62 | { |
| 63 | CodeGenCtx ctx; | 63 | CodeGenCtx ctx; |
| 64 | CodeGenCloudNPU codegen(ctx); // used to PrepareDefaultOutputPath | 64 | CodeGenCloudNPU codegen(ctx); // used to PrepareDefaultOutputPath |
| 65 | - std::string cwd = GetCurRunningPath(); | 65 | + std::string cwd = GetCwd(); |
| 66 | std::string testDir = "test_add"; | 66 | std::string testDir = "test_add"; |
| 67 | std::string cceFilePath = cwd + "/../../../../" + TEST_TILE_OP_PATH + testDir + "/" + cceFileName + ".cce"; | 67 | std::string cceFilePath = cwd + "/../../../../" + TEST_TILE_OP_PATH + testDir + "/" + cceFileName + ".cce"; |
| 68 | 68 | ||
| @@ -21,6 +21,7 @@ | |||
| 21 | #include <nlohmann/json.hpp> | 21 | #include <nlohmann/json.hpp> |
| 22 | #define private public | 22 | #define private public |
| 23 | #include "platform/parser/platform_parser.h" | 23 | #include "platform/parser/platform_parser.h" |
| 24 | +#include "platform/parser/simulation_platform/simulation_platform.h" | ||
| 24 | 25 | ||
| 25 | using namespace npu::tile_fwk; | 26 | using namespace npu::tile_fwk; |
| 26 | 27 | ||
| @@ -45,7 +46,7 @@ public: | |||
| 45 | private: | 46 | private: |
| 46 | void CleanupTempConfig() | 47 | void CleanupTempConfig() |
| 47 | { | 48 | { |
| 48 | - std::string libPath = SimulationPlatform::GetCurrentSharedLibPath(); | 49 | + std::string libPath = GetPyptoLibPath(); |
| 49 | if (!libPath.empty()) { | 50 | if (!libPath.empty()) { |
| 50 | std::string configPath = libPath + "/tile_fwk_config.json"; | 51 | std::string configPath = libPath + "/tile_fwk_config.json"; |
| 51 | std::remove(configPath.c_str()); | 52 | std::remove(configPath.c_str()); |
| @@ -16,7 +16,7 @@ | |||
| 16 | #include "gtest/gtest.h" | 16 | #include "gtest/gtest.h" |
| 17 | #include "tilefwk/tilefwk.h" | 17 | #include "tilefwk/tilefwk.h" |
| 18 | #include "tilefwk/platform.h" | 18 | #include "tilefwk/platform.h" |
| 19 | -#include "interface/utils/file_utils.h" | 19 | +#include "utils/file_utils.h" |
| 20 | #define private public | 20 | #define private public |
| 21 | #include "platform/parser/platform_parser.h" | 21 | #include "platform/parser/platform_parser.h" |
| 22 | #include "platform/parser/internal_parser.h" | 22 | #include "platform/parser/internal_parser.h" |
| @@ -63,7 +63,7 @@ TEST_F(TestPlatform, TestParser) | |||
| 63 | const size_t expectubSize = 253952UL; | 63 | const size_t expectubSize = 253952UL; |
| 64 | 64 | ||
| 65 | std::unique_ptr<INIParser> parser = std::make_unique<INIParser>(); | 65 | std::unique_ptr<INIParser> parser = std::make_unique<INIParser>(); |
| 66 | - std::string iniPath = RealPath(GetCurrentSharedLibPath() + INI_PATH); | 66 | + std::string iniPath = RealPath(GetPyptoLibPath() + INI_PATH); |
| 67 | EXPECT_TRUE(parser->Initialize(iniPath)); | 67 | EXPECT_TRUE(parser->Initialize(iniPath)); |
| 68 | 68 | ||
| 69 | std::string socVersion; | 69 | std::string socVersion; |
| @@ -108,7 +108,7 @@ TEST_F(TestPlatform, AbnormalTest) | |||
| 108 | std::unordered_map<std::string, std::string> ccecVersion; | 108 | std::unordered_map<std::string, std::string> ccecVersion; |
| 109 | EXPECT_FALSE(parser->GetCCECVersion(ccecVersion)); | 109 | EXPECT_FALSE(parser->GetCCECVersion(ccecVersion)); |
| 110 | 110 | ||
| 111 | - std::string iniPath = RealPath(GetCurrentSharedLibPath() + INI_PATH); | 111 | + std::string iniPath = RealPath(GetPyptoLibPath() + INI_PATH); |
| 112 | EXPECT_TRUE(parser->Initialize(iniPath)); | 112 | EXPECT_TRUE(parser->Initialize(iniPath)); |
| 113 | 113 | ||
| 114 | std::string test; | 114 | std::string test; |
| @@ -16,7 +16,7 @@ | |||
| 16 | #include <gtest/gtest.h> | 16 | #include <gtest/gtest.h> |
| 17 | #include <string> | 17 | #include <string> |
| 18 | 18 | ||
| 19 | -#include "interface/utils/file_utils.h" | 19 | +#include "utils/file_utils.h" |
| 20 | 20 | ||
| 21 | using namespace npu::tile_fwk; | 21 | using namespace npu::tile_fwk; |
| 22 | 22 | ||
| @@ -40,25 +40,6 @@ TEST(FileTest, NullptrTest) | |||
| 40 | EXPECT_EQ(ret, false); | 40 | EXPECT_EQ(ret, false); |
| 41 | 41 | ||
| 42 | EXPECT_FALSE(CopyFile("", "")); | 42 | EXPECT_FALSE(CopyFile("", "")); |
| 43 | - EXPECT_EQ(LockAndOpenFile(""), nullptr); | ||
| 44 | -} | ||
| 45 | - | ||
| 46 | -constexpr const char* TEST_LOG_PATH = "/tmp/test_file.log"; | ||
| 47 | -TEST(FileTest, ReadBytesFromFileTest) | ||
| 48 | -{ | ||
| 49 | - std::vector<char> data; | ||
| 50 | - | ||
| 51 | - bool ret = ReadBytesFromFile("", data); | ||
| 52 | - EXPECT_EQ(ret, false); | ||
| 53 | - | ||
| 54 | - SaveFile(TEST_LOG_PATH, std::vector<uint8_t>({})); | ||
| 55 | - ret = ReadBytesFromFile(TEST_LOG_PATH, data); | ||
| 56 | - EXPECT_EQ(ret, false); | ||
| 57 | - DeleteFile(TEST_LOG_PATH); | ||
| 58 | - | ||
| 59 | - // for code coverage | ||
| 60 | - DeleteFile(""); | ||
| 61 | - DeleteFile("/nonexistent/file"); | ||
| 62 | } | 43 | } |
| 63 | 44 | ||
| 64 | TEST(FileTest, LoadFileTest) | 45 | TEST(FileTest, LoadFileTest) |
| @@ -66,12 +47,56 @@ TEST(FileTest, LoadFileTest) | |||
| 66 | uint8_t a = 255; | 47 | uint8_t a = 255; |
| 67 | std::vector<uint8_t> data{a}; | 48 | std::vector<uint8_t> data{a}; |
| 68 | 49 | ||
| 69 | - data = LoadFile(""); | 50 | + data = ReadFile(""); |
| 70 | EXPECT_EQ(data.size(), 0); | 51 | EXPECT_EQ(data.size(), 0); |
| 52 | +} | ||
| 71 | 53 | ||
| 72 | - nlohmann::json jsonObj; | 54 | +TEST(FileTest, ReadFileReturnsFullContents) |
| 73 | - EXPECT_FALSE(ReadJsonFile("", jsonObj)); | 55 | +{ |
| 74 | - EXPECT_FALSE(ReadJsonFile("/tmp/", jsonObj)); | 56 | + // 回归:ReadFile 必须以 std::ios::ate 打开,否则 tellg() 返回 0 导致读回空缓冲。 |
| 57 | + const std::string path = "/tmp/test_read_file_" + std::to_string(::getpid()); | ||
| 58 | + const std::vector<uint8_t> payload{0xde, 0xad, 0xbe, 0xef}; | ||
| 59 | + ASSERT_TRUE(SaveFile(path, payload)); | ||
| 60 | + | ||
| 61 | + const auto data = ReadFile(path); | ||
| 62 | + EXPECT_EQ(data.size(), payload.size()); | ||
| 63 | + EXPECT_EQ(data, payload); | ||
| 64 | + | ||
| 65 | + DeleteFile(path); | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +TEST(FileTest, RemoveOldDirectoriesKeepsNewestAndOnlyMatchingPrefix) | ||
| 69 | +{ | ||
| 70 | + // 回归:筛选条件曾被写反,导致既不按前缀过滤、又误把 "."/".." 放入候选。 | ||
| 71 | + const std::string base = "/tmp/test_remove_old_dirs_" + std::to_string(::getpid()); | ||
| 72 | + const std::string prefix = "run_"; | ||
| 73 | + DeleteDir(base, true); | ||
| 74 | + ASSERT_TRUE(CreateDir(base, true)); | ||
| 75 | + | ||
| 76 | + // 前缀匹配的目录(应受 kept 数量约束,最旧的被删)。 | ||
| 77 | + for (int i = 0; i < 3; ++i) { | ||
| 78 | + const std::string d = base + "/" + prefix + std::to_string(i); | ||
| 79 | + ASSERT_TRUE(CreateDir(d, true)); | ||
| 80 | + // 写入一个文件,验证删除走的是递归路径。 | ||
| 81 | + ASSERT_TRUE(SaveFile(d + "/f.bin", std::vector<uint8_t>{0x01})); | ||
| 82 | + } | ||
| 83 | + // 不匹配前缀的目录(必须被忽略,不被删除)。 | ||
| 84 | + const std::string other = base + "/unrelated"; | ||
| 85 | + ASSERT_TRUE(CreateDir(other, true)); | ||
| 86 | + | ||
| 87 | + // 保留最新的 1 个:按 mtime 排序后删除其余 prefix 目录。 | ||
| 88 | + RemoveOldDirectories(base, prefix, 1); | ||
| 89 | + | ||
| 90 | + EXPECT_TRUE(IsPathExist(other)); // 非匹配前缀必须保留 | ||
| 91 | + int surviving = 0; | ||
| 92 | + for (int i = 0; i < 3; ++i) { | ||
| 93 | + if (IsPathExist(base + "/" + prefix + std::to_string(i))) { | ||
| 94 | + ++surviving; | ||
| 95 | + } | ||
| 96 | + } | ||
| 97 | + EXPECT_EQ(surviving, 1); // 只保留 1 个 prefix 目录 | ||
| 98 | + | ||
| 99 | + DeleteDir(base, true); | ||
| 75 | } | 100 | } |
| 76 | 101 | ||
| 77 | TEST(FileTest, RealPathTest) | 102 | TEST(FileTest, RealPathTest) |
| @@ -81,9 +106,9 @@ TEST(FileTest, RealPathTest) | |||
| 81 | EXPECT_FALSE(RealPath("/tmp").empty()); | 106 | EXPECT_FALSE(RealPath("/tmp").empty()); |
| 82 | EXPECT_TRUE(RealPath(std::string(PATH_MAX, 'a')).empty()); | 107 | EXPECT_TRUE(RealPath(std::string(PATH_MAX, 'a')).empty()); |
| 83 | 108 | ||
| 84 | - EXPECT_FALSE(CreateMultiLevelDir(std::string(PATH_MAX, 'a'))); | 109 | + EXPECT_FALSE(CreateDir(std::string(PATH_MAX, 'a'))); |
| 85 | - EXPECT_FALSE(DeleteDir("")); | ||
| 86 | } | 110 | } |
| 111 | + | ||
| 87 | TEST(FileTest, CreateDirFailedWhenParentNotExist) | 112 | TEST(FileTest, CreateDirFailedWhenParentNotExist) |
| 88 | { | 113 | { |
| 89 | // 父目录不存在 -> mkdir 返回 -1,errno=ENOENT -> CreateDir 返回 false | 114 | // 父目录不存在 -> mkdir 返回 -1,errno=ENOENT -> CreateDir 返回 false |
| @@ -91,6 +116,23 @@ TEST(FileTest, CreateDirFailedWhenParentNotExist) | |||
| 91 | EXPECT_FALSE(CreateDir(path)); | 116 | EXPECT_FALSE(CreateDir(path)); |
| 92 | } | 117 | } |
| 93 | 118 | ||
| 119 | +TEST(FileTest, CreateDirHandlesConsecutiveSlashes) | ||
| 120 | +{ | ||
| 121 | + // "///" 等连续斜杠应被当作单个分隔符处理。 | ||
| 122 | + const std::string base = "/tmp/create_dir_slashes_" + std::to_string(::getpid()); | ||
| 123 | + const std::string path = base + "///a///b//c"; | ||
| 124 | + DeleteDir(base); | ||
| 125 | + ASSERT_TRUE(CreateDir(path, true)); | ||
| 126 | + EXPECT_TRUE(IsPathExist(path)); | ||
| 127 | + // 重复创建应幂等成功。 | ||
| 128 | + EXPECT_TRUE(CreateDir(path, true)); | ||
| 129 | + // 结尾斜杠也应支持。 | ||
| 130 | + EXPECT_TRUE(CreateDir(path + "/", true)); | ||
| 131 | + DeleteDir(base, true); | ||
| 132 | + | ||
| 133 | + EXPECT_TRUE(CreateDir("/tmp", true)); // 已存在 | ||
| 134 | +} | ||
| 135 | + | ||
| 94 | TEST(FileTest, CreateMultiLevelDirFailedWhenParentIsFile) | 136 | TEST(FileTest, CreateMultiLevelDirFailedWhenParentIsFile) |
| 95 | { | 137 | { |
| 96 | const std::string parent = "/tmp/parent_is_file_" + std::to_string(::getpid()); | 138 | const std::string parent = "/tmp/parent_is_file_" + std::to_string(::getpid()); |
| @@ -98,9 +140,9 @@ TEST(FileTest, CreateMultiLevelDirFailedWhenParentIsFile) | |||
| 98 | DeleteDir(target); | 140 | DeleteDir(target); |
| 99 | DeleteFile(parent); | 141 | DeleteFile(parent); |
| 100 | SaveFile(parent, std::vector<uint8_t>{0x01}); | 142 | SaveFile(parent, std::vector<uint8_t>{0x01}); |
| 101 | - EXPECT_FALSE(CreateMultiLevelDir(target)); | 143 | + EXPECT_FALSE(CreateDir(target, true)); |
| 102 | 144 | ||
| 103 | // 清理 | 145 | // 清理 |
| 104 | - DeleteDir(target); | 146 | + DeleteDir(target, true); |
| 105 | DeleteFile(parent); | 147 | DeleteFile(parent); |
| 106 | } | 148 | } |
| @@ -77,14 +77,6 @@ TEST_F(TestOpInfoManager, SetAndGetOpType) | |||
| 77 | EXPECT_EQ(mgr.GetOpType(), "tilefwk"); | 77 | EXPECT_EQ(mgr.GetOpType(), "tilefwk"); |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | -TEST_F(TestOpInfoManager, GetControlBufferDefault) | ||
| 81 | -{ | ||
| 82 | - auto& mgr = OpInfoManager::GetInstance(); | ||
| 83 | - auto& buf = mgr.GetControlBuffer(); | ||
| 84 | - EXPECT_EQ(buf.size(), 1u); | ||
| 85 | - EXPECT_EQ(buf[0], '0'); | ||
| 86 | -} | ||
| 87 | - | ||
| 88 | TEST_F(TestOpInfoManager, GetCustomJsonDefault) | 80 | TEST_F(TestOpInfoManager, GetCustomJsonDefault) |
| 89 | { | 81 | { |
| 90 | auto& mgr = OpInfoManager::GetInstance(); | 82 | auto& mgr = OpInfoManager::GetInstance(); |
| @@ -29,7 +29,7 @@ | |||
| 29 | #include "machine/device/machine_interface/pypto_aicpu_interface.h" | 29 | #include "machine/device/machine_interface/pypto_aicpu_interface.h" |
| 30 | #include "machine/utils/machine_ws_intf.h" | 30 | #include "machine/utils/machine_ws_intf.h" |
| 31 | #include "interface/program/program.h" | 31 | #include "interface/program/program.h" |
| 32 | -#include "interface/utils/file_utils.h" | 32 | +#include "utils/file_utils.h" |
| 33 | #include "tilefwk/aicpu_common.h" | 33 | #include "tilefwk/aicpu_common.h" |
| 34 | #include "machine/device/dynamic/device_utils.h" | 34 | #include "machine/device/dynamic/device_utils.h" |
| 35 | #include "machine/runtime/runner/dump_device_perf.h" | 35 | #include "machine/runtime/runner/dump_device_perf.h" |
| @@ -20,7 +20,7 @@ | |||
| 20 | #include <sstream> | 20 | #include <sstream> |
| 21 | 21 | ||
| 22 | #include "machine/compile/aicore_compiler.h" | 22 | #include "machine/compile/aicore_compiler.h" |
| 23 | -#include "interface/utils/file_utils.h" | 23 | +#include "utils/file_utils.h" |
| 24 | #include "interface/utils/op_info_manager.h" | 24 | #include "interface/utils/op_info_manager.h" |
| 25 | #include "tilefwk/pypto_fwk_log.h" | 25 | #include "tilefwk/pypto_fwk_log.h" |
| 26 | #include "tilefwk/platform.h" | 26 | #include "tilefwk/platform.h" |
| @@ -39,7 +39,7 @@ const std::string TEST_TMP_DIR = "/tmp/test_aicore_compiler"; | |||
| 39 | class TestAicoreCompiler : public testing::Test { | 39 | class TestAicoreCompiler : public testing::Test { |
| 40 | public: | 40 | public: |
| 41 | void SetUp() override {} | 41 | void SetUp() override {} |
| 42 | - static void SetUpTestCase() { CreateMultiLevelDir(TEST_TMP_DIR); } | 42 | + static void SetUpTestCase() { CreateDir(TEST_TMP_DIR); } |
| 43 | 43 | ||
| 44 | void TearDown() override {} | 44 | void TearDown() override {} |
| 45 | static void TearDownTestCase() | 45 | static void TearDownTestCase() |
| @@ -20,7 +20,7 @@ | |||
| 20 | #include <nlohmann/json.hpp> | 20 | #include <nlohmann/json.hpp> |
| 21 | 21 | ||
| 22 | #include "machine/compile/compile_control_bin.h" | 22 | #include "machine/compile/compile_control_bin.h" |
| 23 | -#include "interface/utils/file_utils.h" | 23 | +#include "utils/file_utils.h" |
| 24 | #include "interface/utils/op_info_manager.h" | 24 | #include "interface/utils/op_info_manager.h" |
| 25 | #include "machine/utils/machine_utils.h" | 25 | #include "machine/utils/machine_utils.h" |
| 26 | #include "tilefwk/pypto_fwk_log.h" | 26 | #include "tilefwk/pypto_fwk_log.h" |
| @@ -40,7 +40,7 @@ class TestCompileControlBin : public testing::Test { | |||
| 40 | public: | 40 | public: |
| 41 | void SetUp() override | 41 | void SetUp() override |
| 42 | { | 42 | { |
| 43 | - CreateMultiLevelDir(testDir_); | 43 | + CreateDir(testDir_); |
| 44 | setenv("LC_ALL", "C", 1); | 44 | setenv("LC_ALL", "C", 1); |
| 45 | setenv("LANG", "C", 1); | 45 | setenv("LANG", "C", 1); |
| 46 | } | 46 | } |
| @@ -60,7 +60,7 @@ TEST_F(TestCompileControlBin, GenCustomOpInfo_DumpFileFails) | |||
| 60 | TEST_F(TestCompileControlBin, GenCustomOpInfo_Success) | 60 | TEST_F(TestCompileControlBin, GenCustomOpInfo_Success) |
| 61 | { | 61 | { |
| 62 | std::string aicpuPath = testDir_ + "/gen_custom_op"; | 62 | std::string aicpuPath = testDir_ + "/gen_custom_op"; |
| 63 | - CreateMultiLevelDir(aicpuPath); | 63 | + CreateDir(aicpuPath); |
| 64 | 64 | ||
| 65 | GenCustomOpInfo("test_func", aicpuPath, "libtest_control"); | 65 | GenCustomOpInfo("test_func", aicpuPath, "libtest_control"); |
| 66 | 66 | ||
| @@ -77,7 +77,7 @@ TEST_F(TestCompileControlBin, GenCustomOpInfo_Success) | |||
| 77 | TEST_F(TestCompileControlBin, GenTilingFunc_Success) | 77 | TEST_F(TestCompileControlBin, GenTilingFunc_Success) |
| 78 | { | 78 | { |
| 79 | std::string controlPath = testDir_ + "/gen_tiling_func"; | 79 | std::string controlPath = testDir_ + "/gen_tiling_func"; |
| 80 | - CreateMultiLevelDir(controlPath); | 80 | + CreateDir(controlPath); |
| 81 | 81 | ||
| 82 | bool result = GenTilingFunc("test_op", controlPath); | 82 | bool result = GenTilingFunc("test_op", controlPath); |
| 83 | EXPECT_TRUE(result); | 83 | EXPECT_TRUE(result); |
| @@ -103,7 +103,7 @@ TEST_F(TestCompileControlBin, TileFwkAiCpuCompile_GenTilingFuncFails) | |||
| 103 | TEST_F(TestCompileControlBin, TieFwkAicpuPreCompile_CompileFails) | 103 | TEST_F(TestCompileControlBin, TieFwkAicpuPreCompile_CompileFails) |
| 104 | { | 104 | { |
| 105 | std::string compileDir = testDir_ + "/precompile_test/"; | 105 | std::string compileDir = testDir_ + "/precompile_test/"; |
| 106 | - CreateMultiLevelDir(compileDir); | 106 | + CreateDir(compileDir); |
| 107 | 107 | ||
| 108 | std::string cppFile = compileDir + "dummy.cpp"; | 108 | std::string cppFile = compileDir + "dummy.cpp"; |
| 109 | std::ofstream ofs(cppFile); | 109 | std::ofstream ofs(cppFile); |
| @@ -119,7 +119,7 @@ TEST_F(TestCompileControlBin, TieFwkAicpuPreCompile_CompileFails) | |||
| 119 | TEST_F(TestCompileControlBin, TieFwkAicpuPreCompile_NoFiles) | 119 | TEST_F(TestCompileControlBin, TieFwkAicpuPreCompile_NoFiles) |
| 120 | { | 120 | { |
| 121 | std::string emptyDir = testDir_ + "/empty_compile_dir/"; | 121 | std::string emptyDir = testDir_ + "/empty_compile_dir/"; |
| 122 | - CreateMultiLevelDir(emptyDir); | 122 | + CreateDir(emptyDir); |
| 123 | 123 | ||
| 124 | std::string preCompileO; | 124 | std::string preCompileO; |
| 125 | std::string dirPath = emptyDir; | 125 | std::string dirPath = emptyDir; |
| @@ -131,7 +131,7 @@ TEST_F(TestCompileControlBin, TieFwkAicpuPreCompile_NoFiles) | |||
| 131 | TEST_F(TestCompileControlBin, SharedAicpuCompile_CompileFails) | 131 | TEST_F(TestCompileControlBin, SharedAicpuCompile_CompileFails) |
| 132 | { | 132 | { |
| 133 | std::string aicpuDir = testDir_ + "/shared_compile_test"; | 133 | std::string aicpuDir = testDir_ + "/shared_compile_test"; |
| 134 | - CreateMultiLevelDir(aicpuDir); | 134 | + CreateDir(aicpuDir); |
| 135 | 135 | ||
| 136 | std::string preCompile0 = "/nonexistent_ut_fake.o"; | 136 | std::string preCompile0 = "/nonexistent_ut_fake.o"; |
| 137 | bool result = SharedAicpuCompile("test_func", aicpuDir, preCompile0); | 137 | bool result = SharedAicpuCompile("test_func", aicpuDir, preCompile0); |
| @@ -20,7 +20,7 @@ | |||
| 20 | #include "passes/pass_mgr/pass_manager.h" | 20 | #include "passes/pass_mgr/pass_manager.h" |
| 21 | #include "interface/configs/config_manager.h" | 21 | #include "interface/configs/config_manager.h" |
| 22 | #include "interface/utils/serialization.h" | 22 | #include "interface/utils/serialization.h" |
| 23 | -#include "interface/utils/file_utils.h" | 23 | +#include "utils/file_utils.h" |
| 24 | #include <iostream> | 24 | #include <iostream> |
| 25 | #include <fstream> | 25 | #include <fstream> |
| 26 | #include <vector> | 26 | #include <vector> |
| @@ -25,7 +25,7 @@ | |||
| 25 | #include <string> | 25 | #include <string> |
| 26 | 26 | ||
| 27 | #include "interface/configs/config_manager.h" | 27 | #include "interface/configs/config_manager.h" |
| 28 | -#include "interface/utils/file_utils.h" | 28 | +#include "utils/file_utils.h" |
| 29 | #include "interface/program/program.h" | 29 | #include "interface/program/program.h" |
| 30 | #include "passes/pass_interface/pass.h" | 30 | #include "passes/pass_interface/pass.h" |
| 31 | #include "passes/pass_log/pass_log.h" | 31 | #include "passes/pass_log/pass_log.h" |
| @@ -74,8 +74,8 @@ TEST_F(PassLogTest, ExtractPassLogByFunctionCoverSystemReturnBranches) | |||
| 74 | 74 | ||
| 75 | const std::string tmpDir = MakeTmpDir("/tmp/pypto_pass_log_ut_XXXXXX"); | 75 | const std::string tmpDir = MakeTmpDir("/tmp/pypto_pass_log_ut_XXXXXX"); |
| 76 | ASSERT_FALSE(tmpDir.empty()); | 76 | ASSERT_FALSE(tmpDir.empty()); |
| 77 | - ASSERT_TRUE(CreateMultiLevelDir(tmpDir + "/tools/scripts")); | 77 | + ASSERT_TRUE(CreateDir(tmpDir + "/tools/scripts", true)); |
| 78 | - ASSERT_TRUE(CreateMultiLevelDir(tmpDir + "/out")); | 78 | + ASSERT_TRUE(CreateDir(tmpDir + "/out", true)); |
| 79 | ASSERT_EQ(setenv("TILE_FWK_OUTPUT_DIR", (tmpDir + "/out").c_str(), 1), 0); | 79 | ASSERT_EQ(setenv("TILE_FWK_OUTPUT_DIR", (tmpDir + "/out").c_str(), 1), 0); |
| 80 | ConfigManager::Instance().ResetLog(tmpDir + "/out"); | 80 | ConfigManager::Instance().ResetLog(tmpDir + "/out"); |
| 81 | 81 | ||
| @@ -105,10 +105,10 @@ TEST_F(PassLogTest, PassLogUtilDeleteEmptyFolderOnDestruct) | |||
| 105 | { | 105 | { |
| 106 | const std::string tmpDir = MakeTmpDir("/tmp/pypto_pass_log_util_ut_XXXXXX"); | 106 | const std::string tmpDir = MakeTmpDir("/tmp/pypto_pass_log_util_ut_XXXXXX"); |
| 107 | ASSERT_FALSE(tmpDir.empty()); | 107 | ASSERT_FALSE(tmpDir.empty()); |
| 108 | - ASSERT_TRUE(CreateMultiLevelDir(tmpDir + "/out")); | 108 | + ASSERT_TRUE(CreateDir(tmpDir + "/out")); |
| 109 | ASSERT_EQ(setenv("TILE_FWK_OUTPUT_DIR", (tmpDir + "/out").c_str(), 1), 0); | 109 | ASSERT_EQ(setenv("TILE_FWK_OUTPUT_DIR", (tmpDir + "/out").c_str(), 1), 0); |
| 110 | ConfigManager::Instance().ResetLog(tmpDir + "/out"); | 110 | ConfigManager::Instance().ResetLog(tmpDir + "/out"); |
| 111 | - ASSERT_TRUE(CreateMultiLevelDir(config::LogTopFolder())); | 111 | + ASSERT_TRUE(CreateDir(config::LogTopFolder(), true)); |
| 112 | 112 | ||
| 113 | auto function = | 113 | auto function = |
| 114 | std::make_shared<Function>(Program::GetInstance(), "TENSOR_PassLogUtil", "PassLogUtil", nullptr); | 114 | std::make_shared<Function>(Program::GetInstance(), "TENSOR_PassLogUtil", "PassLogUtil", nullptr); |
| @@ -130,10 +130,10 @@ TEST_F(PassLogTest, PassLogUtilKeepFolderWhenNotEmpty) | |||
| 130 | { | 130 | { |
| 131 | const std::string tmpDir = MakeTmpDir("/tmp/pypto_pass_log_util_keep_ut_XXXXXX"); | 131 | const std::string tmpDir = MakeTmpDir("/tmp/pypto_pass_log_util_keep_ut_XXXXXX"); |
| 132 | ASSERT_FALSE(tmpDir.empty()); | 132 | ASSERT_FALSE(tmpDir.empty()); |
| 133 | - ASSERT_TRUE(CreateMultiLevelDir(tmpDir + "/out")); | 133 | + ASSERT_TRUE(CreateDir(tmpDir + "/out")); |
| 134 | ASSERT_EQ(setenv("TILE_FWK_OUTPUT_DIR", (tmpDir + "/out").c_str(), 1), 0); | 134 | ASSERT_EQ(setenv("TILE_FWK_OUTPUT_DIR", (tmpDir + "/out").c_str(), 1), 0); |
| 135 | ConfigManager::Instance().ResetLog(tmpDir + "/out"); | 135 | ConfigManager::Instance().ResetLog(tmpDir + "/out"); |
| 136 | - ASSERT_TRUE(CreateMultiLevelDir(config::LogTopFolder())); | 136 | + ASSERT_TRUE(CreateDir(config::LogTopFolder(), true)); |
| 137 | 137 | ||
| 138 | auto function = | 138 | auto function = |
| 139 | std::make_shared<Function>(Program::GetInstance(), "TENSOR_PassLogKeep", "PassLogKeep", nullptr); | 139 | std::make_shared<Function>(Program::GetInstance(), "TENSOR_PassLogKeep", "PassLogKeep", nullptr); |
| @@ -158,7 +158,7 @@ TEST_F(PassLogTest, PassLogUtilCreatesStrategyScopedFolder) | |||
| 158 | { | 158 | { |
| 159 | const std::string tmpDir = MakeTmpDir("/tmp/pypto_pass_log_strategy_ut_XXXXXX"); | 159 | const std::string tmpDir = MakeTmpDir("/tmp/pypto_pass_log_strategy_ut_XXXXXX"); |
| 160 | ASSERT_FALSE(tmpDir.empty()); | 160 | ASSERT_FALSE(tmpDir.empty()); |
| 161 | - ASSERT_TRUE(CreateMultiLevelDir(tmpDir + "/out")); | 161 | + ASSERT_TRUE(CreateDir(tmpDir + "/out", true)); |
| 162 | ASSERT_EQ(setenv("TILE_FWK_OUTPUT_DIR", (tmpDir + "/out").c_str(), 1), 0); | 162 | ASSERT_EQ(setenv("TILE_FWK_OUTPUT_DIR", (tmpDir + "/out").c_str(), 1), 0); |
| 163 | ConfigManager::Instance().ResetLog(tmpDir + "/out"); | 163 | ConfigManager::Instance().ResetLog(tmpDir + "/out"); |
| 164 | 164 | ||
| @@ -184,7 +184,7 @@ TEST_F(PassLogTest, PassLogUtilDeletesEmptyStrategyScopedParents) | |||
| 184 | { | 184 | { |
| 185 | const std::string tmpDir = MakeTmpDir("/tmp/pypto_pass_log_empty_strategy_ut_XXXXXX"); | 185 | const std::string tmpDir = MakeTmpDir("/tmp/pypto_pass_log_empty_strategy_ut_XXXXXX"); |
| 186 | ASSERT_FALSE(tmpDir.empty()); | 186 | ASSERT_FALSE(tmpDir.empty()); |
| 187 | - ASSERT_TRUE(CreateMultiLevelDir(tmpDir + "/out")); | 187 | + ASSERT_TRUE(CreateDir(tmpDir + "/out", true)); |
| 188 | ASSERT_EQ(setenv("TILE_FWK_OUTPUT_DIR", (tmpDir + "/out").c_str(), 1), 0); | 188 | ASSERT_EQ(setenv("TILE_FWK_OUTPUT_DIR", (tmpDir + "/out").c_str(), 1), 0); |
| 189 | ConfigManager::Instance().ResetLog(tmpDir + "/out"); | 189 | ConfigManager::Instance().ResetLog(tmpDir + "/out"); |
| 190 | 190 | ||
| @@ -25,7 +25,7 @@ | |||
| 25 | #include "passes/pass_mgr/pass_registry.h" | 25 | #include "passes/pass_mgr/pass_registry.h" |
| 26 | #include "ut_json/ut_json_tool.h" | 26 | #include "ut_json/ut_json_tool.h" |
| 27 | #include "computational_graph_builder.h" | 27 | #include "computational_graph_builder.h" |
| 28 | -#include "interface/utils/file_utils.h" | 28 | +#include "utils/file_utils.h" |
| 29 | 29 | ||
| 30 | namespace npu::tile_fwk { | 30 | namespace npu::tile_fwk { |
| 31 | class PassTestCast : public Pass { | 31 | class PassTestCast : public Pass { |