From f841b29c41d7987fd15348fbb508c16db21f4ca6 Mon Sep 17 00:00:00 2001 From: Concedo <39025047+LostRuins@users.noreply.github.com> Date: Sun, 11 May 2025 14:05:54 +0800 Subject: [PATCH] fixed unicode paths --- model_adapter.cpp | 8 +++++++- otherarch/sdcpp/ggml_extend.hpp | 8 +++++++- otherarch/sdcpp/model.cpp | 29 +++++++++++++++++++++++++---- tools/mtmd/clip.cpp | 8 +++++++- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/model_adapter.cpp b/model_adapter.cpp index 763ce02ec..c09352ea9 100644 --- a/model_adapter.cpp +++ b/model_adapter.cpp @@ -15,6 +15,7 @@ #include "gguf.h" #include +#include static auto bench_timer = std::chrono::high_resolution_clock().now(); @@ -86,7 +87,12 @@ void print_tok_vec(std::vector &embd) { std::vector f_buf(1024*1024); - auto fin = std::ifstream(fname, std::ios::binary); + #ifdef _WIN32 + std::filesystem::path fpath = std::filesystem::u8path(fname); + #else + std::filesystem::path fpath = std::filesystem::path(fname); + #endif + auto fin = std::ifstream(fpath, std::ios::binary); fin.rdbuf()->pubsetbuf(f_buf.data(), f_buf.size()); if (!fin) { fprintf(stderr, "%s: failed to open '%s'\n", __func__, fname.c_str()); diff --git a/otherarch/sdcpp/ggml_extend.hpp b/otherarch/sdcpp/ggml_extend.hpp index 5ff42122e..ab40dc861 100644 --- a/otherarch/sdcpp/ggml_extend.hpp +++ b/otherarch/sdcpp/ggml_extend.hpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "ggml-alloc.h" #include "ggml-backend.h" @@ -218,7 +219,12 @@ __STATIC_INLINE__ void print_ggml_tensor(struct ggml_tensor* tensor, bool shape_ } __STATIC_INLINE__ ggml_tensor* load_tensor_from_file(ggml_context* ctx, const std::string& file_path) { - std::ifstream file(file_path, std::ios::binary); + #ifdef _WIN32 + std::filesystem::path fpath = std::filesystem::u8path(file_path); + #else + std::filesystem::path fpath = std::filesystem::path(file_path); + #endif + std::ifstream file(fpath, std::ios::binary); if (!file.is_open()) { LOG_ERROR("failed to open '%s'", file_path.c_str()); return NULL; diff --git a/otherarch/sdcpp/model.cpp b/otherarch/sdcpp/model.cpp index 3ce7af730..80af64f8d 100644 --- a/otherarch/sdcpp/model.cpp +++ b/otherarch/sdcpp/model.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "model.h" #include "stable-diffusion.h" @@ -907,7 +908,12 @@ bool is_zip_file(const std::string& file_path) { } bool is_gguf_file(const std::string& file_path) { - std::ifstream file(file_path, std::ios::binary); + #ifdef _WIN32 + std::filesystem::path fpath = std::filesystem::u8path(file_path); + #else + std::filesystem::path fpath = std::filesystem::path(file_path); + #endif + std::ifstream file(fpath, std::ios::binary); if (!file.is_open()) { return false; } @@ -928,7 +934,12 @@ bool is_gguf_file(const std::string& file_path) { } bool is_safetensors_file(const std::string& file_path) { - std::ifstream file(file_path, std::ios::binary); + #ifdef _WIN32 + std::filesystem::path fpath = std::filesystem::u8path(file_path); + #else + std::filesystem::path fpath = std::filesystem::path(file_path); + #endif + std::ifstream file(fpath, std::ios::binary); if (!file.is_open()) { return false; } @@ -1052,7 +1063,12 @@ bool ModelLoader::init_from_safetensors_file(const std::string& file_path, const LOG_DEBUG("init from '%s'", file_path.c_str()); file_paths_.push_back(file_path); size_t file_index = file_paths_.size() - 1; - std::ifstream file(file_path, std::ios::binary); + #ifdef _WIN32 + std::filesystem::path fpath = std::filesystem::u8path(file_path); + #else + std::filesystem::path fpath = std::filesystem::path(file_path); + #endif + std::ifstream file(fpath, std::ios::binary); if (!file.is_open()) { LOG_ERROR("failed to open '%s'", file_path.c_str()); return false; @@ -1809,7 +1825,12 @@ bool ModelLoader::load_tensors(on_new_tensor_cb_t on_new_tensor_cb, ggml_backend std::string file_path = file_paths_[file_index]; LOG_DEBUG("loading tensors from %s\n", file_path.c_str()); - std::ifstream file(file_path, std::ios::binary); + #ifdef _WIN32 + std::filesystem::path fpath = std::filesystem::u8path(file_path); + #else + std::filesystem::path fpath = std::filesystem::path(file_path); + #endif + std::ifstream file(fpath, std::ios::binary); if (!file.is_open()) { LOG_ERROR("failed to open '%s'", file_path.c_str()); return false; diff --git a/tools/mtmd/clip.cpp b/tools/mtmd/clip.cpp index 1f86a8b2d..73b5c6476 100644 --- a/tools/mtmd/clip.cpp +++ b/tools/mtmd/clip.cpp @@ -44,6 +44,7 @@ #include #include #include +#include struct clip_logger_state g_logger_state = {GGML_LOG_LEVEL_CONT, clip_log_callback_default, NULL}; @@ -2107,7 +2108,12 @@ struct clip_model_loader { { std::vector read_buf; - auto fin = std::ifstream(fname, std::ios::binary); + #ifdef _WIN32 + std::filesystem::path fpath = std::filesystem::u8path(fname); + #else + std::filesystem::path fpath = std::filesystem::path(fname); + #endif + auto fin = std::ifstream(fpath, std::ios::binary); if (!fin) { throw std::runtime_error(string_format("%s: failed to open %s\n", __func__, fname.c_str())); }