0004-clip-unicode.patch 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Michael Yang <mxyng@pm.me>
  3. Date: Mon, 16 Sep 2024 15:53:15 -0700
  4. Subject: [PATCH] clip-unicode
  5. ---
  6. examples/llava/clip.cpp | 40 +++++++++++++++++++++++++++++++++++++++-
  7. 1 file changed, 39 insertions(+), 1 deletion(-)
  8. diff --git a/examples/llava/clip.cpp b/examples/llava/clip.cpp
  9. index 76d4a785..205af1eb 100644
  10. --- a/examples/llava/clip.cpp
  11. +++ b/examples/llava/clip.cpp
  12. @@ -58,6 +58,19 @@
  13. # define LOG_DBG(...) do { fprintf(stdout, __VA_ARGS__); } while (0)
  14. #endif // defined(LLAVA_LOG_OFF)
  15. +#if defined(_WIN32)
  16. +#define WIN32_LEAN_AND_MEAN
  17. +#ifndef NOMINMAX
  18. + #define NOMINMAX
  19. +#endif
  20. +#include <windows.h>
  21. +#if __GLIBCXX__
  22. +#include <cstdio>
  23. +#include <ext/stdio_filebuf.h>
  24. +#include <fcntl.h>
  25. +#endif
  26. +#endif
  27. +
  28. //#define CLIP_DEBUG_FUNCTIONS
  29. // RGB uint8 image
  30. @@ -1402,8 +1415,29 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
  31. gguf_free(ctx);
  32. return nullptr;
  33. }
  34. -
  35. +#ifdef _WIN32
  36. + int wlen = MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0);
  37. + if (!wlen) {
  38. + return NULL;
  39. + }
  40. + wchar_t * wbuf = (wchar_t *) malloc(wlen * sizeof(wchar_t));
  41. + wlen = MultiByteToWideChar(CP_UTF8, 0, fname, -1, wbuf, wlen);
  42. + if (!wlen) {
  43. + free(wbuf);
  44. + return NULL;
  45. + }
  46. +#if __GLIBCXX__
  47. + int fd = _wopen(wbuf, _O_RDONLY | _O_BINARY);
  48. + __gnu_cxx::stdio_filebuf<char> buffer(fd, std::ios_base::in);
  49. + std::istream fin(&buffer);
  50. +#else // MSVC
  51. + // unused in our current build
  52. + auto fin = std::ifstream(wbuf, std::ios::binary);
  53. +#endif
  54. + free(wbuf);
  55. +#else
  56. auto fin = std::ifstream(fname, std::ios::binary);
  57. +#endif
  58. if (!fin) {
  59. LOG_ERR("cannot open model file for loading tensors\n");
  60. clip_free(new_clip);
  61. @@ -1443,7 +1477,11 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
  62. ggml_backend_tensor_set(cur, read_buf.data(), 0, num_bytes);
  63. }
  64. }
  65. +#if defined(_WIN32) && defined(__GLIBCXX__)
  66. + close(fd);
  67. +#else
  68. fin.close();
  69. +#endif
  70. }
  71. // vision model