Compare commits

...

3 Commits

Author SHA1 Message Date
538363413e More fixes
Signed-off-by: Yuanyuan Chen <cyyever@outlook.com>
2025-11-15 10:56:23 +08:00
d6761bb57f More fixes
Signed-off-by: Yuanyuan Chen <cyyever@outlook.com>
2025-11-15 09:00:51 +08:00
cdfb4788fb Use std::fs::path
Signed-off-by: Yuanyuan Chen <cyyever@outlook.com>
2025-11-15 09:00:51 +08:00
5 changed files with 24 additions and 131 deletions

View File

@ -1,4 +1,5 @@
#include <c10/util/Exception.h>
#include <c10/util/FileSystem.h>
#include <c10/util/Logging.h>
#include <c10/util/Type.h>
@ -27,7 +28,7 @@ Error::Error(
const void* caller)
: Error(
str("[enforce fail at ",
detail::StripBasename(file),
c10::filesystem::path(file).filename(),
":",
line,
"] ",

View File

@ -1,4 +1,5 @@
#include <c10/util/Backtrace.h>
#include <c10/util/FileSystem.h>
#include <c10/util/Flags.h>
#include <c10/util/Lazy.h>
#include <c10/util/Logging.h>
@ -478,8 +479,7 @@ MessageLogger::MessageLogger(
<< std::setfill('0') << " " << std::setw(2) << timeinfo->tm_hour
<< ":" << std::setw(2) << timeinfo->tm_min << ":" << std::setw(2)
<< timeinfo->tm_sec << "." << std::setw(9) << ns << " "
<< c10::detail::StripBasename(std::string(file)) << ":" << line
<< "] ";
<< c10::filesystem::path(file).filename() << ":" << line << "] ";
}
// Output the contents of the stream to the proper channel on destruction.

View File

@ -1,5 +1,6 @@
#if !defined(C10_MOBILE) && !defined(ANDROID)
#include <c10/util/FileSystem.h>
#include <c10/util/error.h>
#include <c10/util/string_view.h>
#include <torch/csrc/inductor/aoti_package/model_package_loader.h>
@ -30,8 +31,6 @@ namespace fs = std::filesystem;
#include <direct.h>
#include <io.h>
#include <process.h>
#define access _access
#define F_OK 0
#else
#include <sys/types.h>
#include <unistd.h>
@ -79,15 +78,6 @@ std::string normalize_path_separator(const std::string& orig_path) {
return normalized_path;
}
bool file_exists(const std::string& path) {
#ifdef _WIN32
return fs::exists(path);
#else
struct stat rc{};
return lstat(path.c_str(), &rc) == 0;
#endif
}
std::string create_temp_dir() {
#ifdef _WIN32
try {
@ -155,7 +145,8 @@ namespace torch::inductor {
namespace {
const nlohmann::json& load_json_file(const std::string& json_path) {
TORCH_CHECK(file_exists(json_path), "File not found: ", json_path);
TORCH_CHECK(
c10::filesystem::exists(json_path), "File not found: ", json_path);
std::ifstream json_file(json_path);
TORCH_CHECK(json_file.is_open());
@ -292,102 +283,6 @@ std::tuple<std::string, std::string> get_cpp_compile_command(
return std::make_tuple(cmd, target_file);
}
bool recursive_mkdir(const std::string& dir) {
// Creates directories recursively, copied from jit_utils.cpp
// Check if current dir exists
const char* p_dir = dir.c_str();
const bool dir_exists = (access(p_dir, F_OK) == 0);
if (dir_exists) {
return true;
}
// Try to create current directory
#ifdef _WIN32
int ret = _mkdir(dir.c_str());
#else
int ret = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
#endif
// Success
if (ret == 0) {
return true;
}
// Find folder separator and check if we are at the top
auto pos = dir.find_last_of(k_separator);
if (pos == std::string::npos) {
return false;
}
// Try to create parent directory
if (!(recursive_mkdir(dir.substr(0, pos)))) {
return false;
}
// Try to create complete path again
#ifdef _WIN32
ret = _mkdir(dir.c_str());
#else
ret = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
#endif
return ret == 0;
}
bool recursive_rmdir(const std::string& path) {
#ifdef _WIN32
std::error_code ec;
return fs::remove_all(path, ec) != static_cast<std::uintmax_t>(-1);
#else
DIR* dir = opendir(path.c_str());
if (!dir) {
return false;
}
struct dirent* entry = nullptr;
struct stat statbuf{};
bool success = true;
// Iterate through directory entries
while ((entry = readdir(dir)) != nullptr) {
std::string name = entry->d_name;
// Skip "." and ".."
if (name == "." || name == "..") {
continue;
}
std::string full_path = path;
full_path.append("/").append(name);
// Get file status
if (stat(full_path.c_str(), &statbuf) != 0) {
success = false;
continue;
}
if (S_ISDIR(statbuf.st_mode)) {
// Recursively delete subdirectory
if (!recursive_rmdir(full_path)) {
success = false;
}
} else {
// Delete file
if (unlink(full_path.c_str()) != 0) {
success = false;
}
}
}
closedir(dir);
// Remove the directory itself
if (rmdir(path.c_str()) != 0) {
success = false;
}
return success;
#endif
}
std::string compile_so(
const std::string& cpp_filename,
std::vector<std::string>& obj_filenames) {
@ -417,7 +312,7 @@ std::string compile_so(
// Move the mmapped weights onto the .so
std::string serialized_weights_path = filename + "_serialized_weights.bin";
if (file_exists(serialized_weights_path)) {
if (c10::filesystem::exists(serialized_weights_path)) {
std::ifstream serialized_weights_file(
serialized_weights_path, std::ios::binary);
TORCH_CHECK(
@ -639,11 +534,13 @@ std::unordered_map<std::string, std::string> AOTIModelPackageLoader::
parent_path_idx != std::string::npos,
"Failed to find parent path in " + output_path_str);
std::string parent_path = output_path_str.substr(0, parent_path_idx);
std::error_code ec{};
c10::filesystem::create_directories(parent_path, ec);
TORCH_CHECK(
recursive_mkdir(parent_path),
ec.value() == 0,
"Failed to create directory " + parent_path,
": ",
c10::utils::str_error(errno));
ec.message());
LOG(INFO) << "Extract file: " << metadata_filename << " to "
<< output_path_str;
@ -657,7 +554,7 @@ std::unordered_map<std::string, std::string> AOTIModelPackageLoader::
metadata[item.key()] = item.value().get<std::string>();
}
// Clean up temporary directory
recursive_rmdir(temp_dir);
c10::filesystem::remove_all(temp_dir, ec);
return metadata;
}
@ -749,11 +646,13 @@ AOTIModelPackageLoader::AOTIModelPackageLoader(
"Failed to find parent path in " + output_file_path);
std::string parent_path = output_file_path.substr(0, parent_path_idx);
std::error_code ec{};
c10::filesystem::create_directories(parent_path, ec);
TORCH_CHECK(
recursive_mkdir(parent_path),
ec.value() == 0,
"Failed to create directory " + parent_path,
": ",
c10::utils::str_error(errno));
ec.message());
// Extracts file to the temp directory
zip_archive.extract_file(zip_filename_str, output_path_str);
@ -832,7 +731,8 @@ AOTIModelPackageLoader::AOTIModelPackageLoader(
AOTIModelPackageLoader::~AOTIModelPackageLoader() {
// Clean up the temporary directory
if (!temp_dir_.empty()) {
recursive_rmdir(temp_dir_);
std::error_code ec;
c10::filesystem::remove_all(temp_dir_, ec);
}
}

View File

@ -8,7 +8,7 @@
#include <ATen/core/function.h>
#include <c10/util/Exception.h>
#include <c10/util/StringUtil.h>
#include <c10/util/FileSystem.h>
#include <c10/util/env.h>
#include <torch/csrc/jit/api/function_impl.h>
#include <torch/csrc/jit/frontend/error_report.h>
@ -113,12 +113,7 @@ void JitLoggingConfig::parse() {
bool is_enabled(const char* cfname, JitLoggingLevels level) {
const auto& files_to_levels =
JitLoggingConfig::getInstance().getFilesToLevels();
std::string fname{cfname};
fname = c10::detail::StripBasename(fname);
const auto end_index = fname.find_last_of('.') == std::string::npos
? fname.size()
: fname.find_last_of('.');
const auto fname_no_ext = fname.substr(0, end_index);
const auto fname_no_ext = c10::filesystem::path(cfname).stem().string();
const auto it = files_to_levels.find(fname_no_ext);
if (it == files_to_levels.end()) {
@ -161,7 +156,7 @@ std::string jit_log_prefix(
std::stringstream prefix_ss;
prefix_ss << "[";
prefix_ss << level << " ";
prefix_ss << c10::detail::StripBasename(std::string(fn)) << ":";
prefix_ss << c10::filesystem::path(fn).filename() << ":";
prefix_ss << std::setfill('0') << std::setw(3) << l;
prefix_ss << "] ";

View File

@ -1,11 +1,10 @@
#include <cstdlib>
#include <sstream>
#include <string>
#include <utility>
#include <ATen/core/function.h>
#include <c10/util/Exception.h>
#include <c10/util/StringUtil.h>
#include <c10/util/FileSystem.h>
#include <c10/util/env.h>
#include <torch/csrc/jit/api/function_impl.h>
#include <torch/csrc/jit/jit_opt_limit.h>
@ -57,9 +56,7 @@ bool opt_limit(const char* pass_name) {
static const std::unordered_map<std::string, int64_t> passes_to_opt_limits =
parseJITOptLimitOption(opt_limit.value());
std::string pass{pass_name};
pass = c10::detail::StripBasename(pass);
pass = c10::detail::ExcludeFileExtension(pass);
auto pass = c10::filesystem::path(pass_name).stem().string();
auto opt_limit_it = passes_to_opt_limits.find(pass);
if (opt_limit_it == passes_to_opt_limits.end()) {