Skip to content

Commit f841b29

Browse files
committed
fixed unicode paths
1 parent 75ec0ba commit f841b29

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

model_adapter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "gguf.h"
1616

1717
#include <chrono>
18+
#include <filesystem>
1819

1920
static auto bench_timer = std::chrono::high_resolution_clock().now();
2021

@@ -86,7 +87,12 @@ void print_tok_vec(std::vector<float> &embd)
8687
{
8788
std::vector<char> f_buf(1024*1024);
8889

89-
auto fin = std::ifstream(fname, std::ios::binary);
90+
#ifdef _WIN32
91+
std::filesystem::path fpath = std::filesystem::u8path(fname);
92+
#else
93+
std::filesystem::path fpath = std::filesystem::path(fname);
94+
#endif
95+
auto fin = std::ifstream(fpath, std::ios::binary);
9096
fin.rdbuf()->pubsetbuf(f_buf.data(), f_buf.size());
9197
if (!fin) {
9298
fprintf(stderr, "%s: failed to open '%s'\n", __func__, fname.c_str());

otherarch/sdcpp/ggml_extend.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <string>
2020
#include <unordered_map>
2121
#include <vector>
22+
#include <filesystem>
2223

2324
#include "ggml-alloc.h"
2425
#include "ggml-backend.h"
@@ -218,7 +219,12 @@ __STATIC_INLINE__ void print_ggml_tensor(struct ggml_tensor* tensor, bool shape_
218219
}
219220

220221
__STATIC_INLINE__ ggml_tensor* load_tensor_from_file(ggml_context* ctx, const std::string& file_path) {
221-
std::ifstream file(file_path, std::ios::binary);
222+
#ifdef _WIN32
223+
std::filesystem::path fpath = std::filesystem::u8path(file_path);
224+
#else
225+
std::filesystem::path fpath = std::filesystem::path(file_path);
226+
#endif
227+
std::ifstream file(fpath, std::ios::binary);
222228
if (!file.is_open()) {
223229
LOG_ERROR("failed to open '%s'", file_path.c_str());
224230
return NULL;

otherarch/sdcpp/model.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string>
66
#include <unordered_map>
77
#include <vector>
8+
#include <filesystem>
89

910
#include "model.h"
1011
#include "stable-diffusion.h"
@@ -907,7 +908,12 @@ bool is_zip_file(const std::string& file_path) {
907908
}
908909

909910
bool is_gguf_file(const std::string& file_path) {
910-
std::ifstream file(file_path, std::ios::binary);
911+
#ifdef _WIN32
912+
std::filesystem::path fpath = std::filesystem::u8path(file_path);
913+
#else
914+
std::filesystem::path fpath = std::filesystem::path(file_path);
915+
#endif
916+
std::ifstream file(fpath, std::ios::binary);
911917
if (!file.is_open()) {
912918
return false;
913919
}
@@ -928,7 +934,12 @@ bool is_gguf_file(const std::string& file_path) {
928934
}
929935

930936
bool is_safetensors_file(const std::string& file_path) {
931-
std::ifstream file(file_path, std::ios::binary);
937+
#ifdef _WIN32
938+
std::filesystem::path fpath = std::filesystem::u8path(file_path);
939+
#else
940+
std::filesystem::path fpath = std::filesystem::path(file_path);
941+
#endif
942+
std::ifstream file(fpath, std::ios::binary);
932943
if (!file.is_open()) {
933944
return false;
934945
}
@@ -1052,7 +1063,12 @@ bool ModelLoader::init_from_safetensors_file(const std::string& file_path, const
10521063
LOG_DEBUG("init from '%s'", file_path.c_str());
10531064
file_paths_.push_back(file_path);
10541065
size_t file_index = file_paths_.size() - 1;
1055-
std::ifstream file(file_path, std::ios::binary);
1066+
#ifdef _WIN32
1067+
std::filesystem::path fpath = std::filesystem::u8path(file_path);
1068+
#else
1069+
std::filesystem::path fpath = std::filesystem::path(file_path);
1070+
#endif
1071+
std::ifstream file(fpath, std::ios::binary);
10561072
if (!file.is_open()) {
10571073
LOG_ERROR("failed to open '%s'", file_path.c_str());
10581074
return false;
@@ -1809,7 +1825,12 @@ bool ModelLoader::load_tensors(on_new_tensor_cb_t on_new_tensor_cb, ggml_backend
18091825
std::string file_path = file_paths_[file_index];
18101826
LOG_DEBUG("loading tensors from %s\n", file_path.c_str());
18111827

1812-
std::ifstream file(file_path, std::ios::binary);
1828+
#ifdef _WIN32
1829+
std::filesystem::path fpath = std::filesystem::u8path(file_path);
1830+
#else
1831+
std::filesystem::path fpath = std::filesystem::path(file_path);
1832+
#endif
1833+
std::ifstream file(fpath, std::ios::binary);
18131834
if (!file.is_open()) {
18141835
LOG_ERROR("failed to open '%s'", file_path.c_str());
18151836
return false;

tools/mtmd/clip.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <array>
4545
#include <numeric>
4646
#include <functional>
47+
#include <filesystem>
4748

4849
struct clip_logger_state g_logger_state = {GGML_LOG_LEVEL_CONT, clip_log_callback_default, NULL};
4950

@@ -2107,7 +2108,12 @@ struct clip_model_loader {
21072108
{
21082109
std::vector<uint8_t> read_buf;
21092110

2110-
auto fin = std::ifstream(fname, std::ios::binary);
2111+
#ifdef _WIN32
2112+
std::filesystem::path fpath = std::filesystem::u8path(fname);
2113+
#else
2114+
std::filesystem::path fpath = std::filesystem::path(fname);
2115+
#endif
2116+
auto fin = std::ifstream(fpath, std::ios::binary);
21112117
if (!fin) {
21122118
throw std::runtime_error(string_format("%s: failed to open %s\n", __func__, fname.c_str()));
21132119
}

0 commit comments

Comments
 (0)