|
3 | 3 |
|
4 | 4 | #include "ggml-impl.h"
|
5 | 5 | #include "ggml-quants.h"
|
| 6 | +#include "ggml.h" |
6 | 7 |
|
7 | 8 | #if defined(_MSC_VER) || defined(__MINGW32__)
|
8 | 9 | #include <malloc.h> // using malloc.h with MSC/MINGW
|
|
43 | 44 |
|
44 | 45 | #if defined(_WIN32)
|
45 | 46 |
|
| 47 | +#define WIN32_LEAN_AND_MEAN |
| 48 | +#ifndef NOMINMAX |
| 49 | + #define NOMINMAX |
| 50 | +#endif |
46 | 51 | #include <windows.h>
|
47 | 52 |
|
48 | 53 | typedef volatile LONG atomic_int;
|
@@ -430,6 +435,57 @@ int64_t ggml_cycles_per_ms(void) {
|
430 | 435 | #define ggml_perf_cycles_per_ms() 0
|
431 | 436 | #endif
|
432 | 437 |
|
| 438 | +// |
| 439 | +// cross-platform UTF-8 file paths |
| 440 | +// |
| 441 | + |
| 442 | +#ifdef _WIN32 |
| 443 | +static wchar_t * ggml_mbstowcs(const char * mbs) { |
| 444 | + int wlen = MultiByteToWideChar(CP_UTF8, 0, mbs, -1, NULL, 0); |
| 445 | + if (!wlen) { |
| 446 | + errno = EINVAL; |
| 447 | + return NULL; |
| 448 | + } |
| 449 | + |
| 450 | + wchar_t * wbuf = GGML_MALLOC(wlen * sizeof(wchar_t)); |
| 451 | + wlen = MultiByteToWideChar(CP_UTF8, 0, mbs, -1, wbuf, wlen); |
| 452 | + if (!wlen) { |
| 453 | + GGML_FREE(wbuf); |
| 454 | + errno = EINVAL; |
| 455 | + return NULL; |
| 456 | + } |
| 457 | + |
| 458 | + return wbuf; |
| 459 | +} |
| 460 | +#endif |
| 461 | + |
| 462 | +FILE * ggml_fopen(const char * fname, const char * mode) { |
| 463 | +#ifdef _WIN32 |
| 464 | + FILE * file = NULL; |
| 465 | + |
| 466 | + // convert fname (UTF-8) |
| 467 | + wchar_t * wfname = ggml_mbstowcs(fname); |
| 468 | + if (wfname) { |
| 469 | + // convert mode (ANSI) |
| 470 | + wchar_t * wmode = GGML_MALLOC(strlen(mode) + 1); |
| 471 | + wchar_t * wmode_p = wmode; |
| 472 | + do { |
| 473 | + *wmode_p++ = (wchar_t)*mode; |
| 474 | + } while (*mode++); |
| 475 | + |
| 476 | + // open file |
| 477 | + file = _wfopen(wfname, wmode); |
| 478 | + |
| 479 | + GGML_FREE(wfname); |
| 480 | + GGML_FREE(wmode); |
| 481 | + } |
| 482 | + |
| 483 | + return file; |
| 484 | +#else |
| 485 | + return fopen(fname, mode); |
| 486 | +#endif |
| 487 | +} |
| 488 | + |
433 | 489 | //
|
434 | 490 | // cache line
|
435 | 491 | //
|
@@ -18739,7 +18795,7 @@ void ggml_graph_export(const struct ggml_cgraph * cgraph, const char * fname) {
|
18739 | 18795 |
|
18740 | 18796 | // write binary data
|
18741 | 18797 | {
|
18742 |
| - FILE * fout = fopen(fname, "wb"); |
| 18798 | + FILE * fout = ggml_fopen(fname, "wb"); |
18743 | 18799 |
|
18744 | 18800 | if (!fout) {
|
18745 | 18801 | fprintf(stderr, "%s: failed to open %s\n", __func__, fname);
|
@@ -18877,7 +18933,7 @@ struct ggml_cgraph * ggml_graph_import(const char * fname, struct ggml_context *
|
18877 | 18933 |
|
18878 | 18934 | // read file into data
|
18879 | 18935 | {
|
18880 |
| - FILE * fin = fopen(fname, "rb"); |
| 18936 | + FILE * fin = ggml_fopen(fname, "rb"); |
18881 | 18937 | if (!fin) {
|
18882 | 18938 | fprintf(stderr, "%s: failed to open %s\n", __func__, fname);
|
18883 | 18939 | return result;
|
@@ -19213,7 +19269,7 @@ static void ggml_graph_dump_dot_leaf_edge(FILE * fp, struct ggml_tensor * node,
|
19213 | 19269 | void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename) {
|
19214 | 19270 | char color[16];
|
19215 | 19271 |
|
19216 |
| - FILE * fp = fopen(filename, "w"); |
| 19272 | + FILE * fp = ggml_fopen(filename, "w"); |
19217 | 19273 | GGML_ASSERT(fp);
|
19218 | 19274 |
|
19219 | 19275 | fprintf(fp, "digraph G {\n");
|
@@ -20531,7 +20587,7 @@ struct gguf_context * gguf_init_empty(void) {
|
20531 | 20587 | }
|
20532 | 20588 |
|
20533 | 20589 | struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_params params) {
|
20534 |
| - FILE * file = fopen(fname, "rb"); |
| 20590 | + FILE * file = ggml_fopen(fname, "rb"); |
20535 | 20591 | if (!file) {
|
20536 | 20592 | return NULL;
|
20537 | 20593 | }
|
@@ -21486,7 +21542,7 @@ static void gguf_write_to_buf(const struct gguf_context * ctx, struct gguf_buf *
|
21486 | 21542 | }
|
21487 | 21543 |
|
21488 | 21544 | void gguf_write_to_file(const struct gguf_context * ctx, const char * fname, bool only_meta) {
|
21489 |
| - FILE * file = fopen(fname, "wb"); |
| 21545 | + FILE * file = ggml_fopen(fname, "wb"); |
21490 | 21546 | if (!file) {
|
21491 | 21547 | GGML_ASSERT(false && "failed to open file for writing");
|
21492 | 21548 | }
|
|
0 commit comments