"src/vscode:/vscode.git/clone" did not exist on "251cdd746ce3928488ba1d31f2a0cfb722be4a03"
0004-clip-unicode.patch 2.58 KB
Newer Older
1
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2
3
From: jmorganca <jmorganca@gmail.com>
Date: Tue, 8 Apr 2025 15:34:37 -0700
4
5
Subject: [PATCH] clip-unicode

6
7
fixes loading vision models in llama.cpp on windows
filesystems for paths that include wide characters
8
---
9
10
 examples/llava/clip.cpp | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
11

12
diff --git a/examples/llava/clip.cpp b/examples/llava/clip.cpp
13
index ad3e7df1..b3218c78 100644
14
15
--- a/examples/llava/clip.cpp
+++ b/examples/llava/clip.cpp
16
@@ -30,6 +30,19 @@
17
 #include <array>
18
 #include <numeric>
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
+#if defined(_WIN32)
+#define WIN32_LEAN_AND_MEAN
+#ifndef NOMINMAX
+    #define NOMINMAX
+#endif
+#include <windows.h>
+#if __GLIBCXX__
+#include <cstdio>
+#include <ext/stdio_filebuf.h>
+#include <fcntl.h>
+#endif
+#endif
+
33
34
 struct clip_logger_state g_logger_state = {GGML_LOG_LEVEL_CONT, clip_log_callback_default, NULL};
 
35
 //#define CLIP_DEBUG_FUNCTIONS
36
@@ -1971,7 +1984,29 @@ struct clip_model_loader {
37
38
         {
             std::vector<uint8_t> read_buf;
39
40
 
+#ifdef _WIN32
41
42
43
44
45
46
47
48
49
50
+            int wlen = MultiByteToWideChar(CP_UTF8, 0, fname.c_str(), -1, NULL, 0);
+            if (!wlen) {
+                throw std::runtime_error(string_format("%s: failed to convert filename to wide string\n", __func__));
+            }
+            wchar_t * wbuf = (wchar_t *) malloc(wlen * sizeof(wchar_t));
+            wlen = MultiByteToWideChar(CP_UTF8, 0, fname.c_str(), -1, wbuf, wlen);
+            if (!wlen) {
+                free(wbuf);
+                throw std::runtime_error(string_format("%s: failed to convert filename to wide string\n", __func__));
+            }
51
+#if __GLIBCXX__
52
53
54
+            int fd = _wopen(wbuf, _O_RDONLY | _O_BINARY);
+            __gnu_cxx::stdio_filebuf<char> buffer(fd, std::ios_base::in);
+            std::istream fin(&buffer);
55
+#else // MSVC
56
57
+            // unused in our current build
+            auto fin = std::ifstream(wbuf, std::ios::binary);
58
+#endif
59
+            free(wbuf);
60
+#else
61
             auto fin = std::ifstream(fname, std::ios::binary);
62
+#endif
63
64
65
             if (!fin) {
                 throw std::runtime_error(string_format("%s: failed to open %s\n", __func__, fname.c_str()));
             }
66
@@ -1998,7 +2033,11 @@ struct clip_model_loader {
67
68
                     ggml_backend_tensor_set(cur, read_buf.data(), 0, num_bytes);
                 }
69
70
             }
+#if defined(_WIN32) && defined(__GLIBCXX__)
71
+            close(fd);
72
+#else
73
             fin.close();
74
75
+#endif
 
76
77
             LOG_DBG("%s: loaded %zu tensors from %s\n", __func__, tensors_to_load.size(), fname.c_str());
         }