|
@@ -3,6 +3,7 @@
|
|
|
|
|
|
|
|
#include "ggml-impl.h"
|
|
#include "ggml-impl.h"
|
|
|
#include "ggml-quants.h"
|
|
#include "ggml-quants.h"
|
|
|
|
|
+#include "ggml.h"
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
|
|
#include <malloc.h> // using malloc.h with MSC/MINGW
|
|
#include <malloc.h> // using malloc.h with MSC/MINGW
|
|
@@ -43,6 +44,10 @@
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
#if defined(_WIN32)
|
|
|
|
|
|
|
|
|
|
+#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
+#ifndef NOMINMAX
|
|
|
|
|
+ #define NOMINMAX
|
|
|
|
|
+#endif
|
|
|
#include <windows.h>
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
typedef volatile LONG atomic_int;
|
|
typedef volatile LONG atomic_int;
|
|
@@ -430,6 +435,57 @@ int64_t ggml_cycles_per_ms(void) {
|
|
|
#define ggml_perf_cycles_per_ms() 0
|
|
#define ggml_perf_cycles_per_ms() 0
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
|
|
+//
|
|
|
|
|
+// cross-platform UTF-8 file paths
|
|
|
|
|
+//
|
|
|
|
|
+
|
|
|
|
|
+#ifdef _WIN32
|
|
|
|
|
+static wchar_t * ggml_mbstowcs(const char * mbs) {
|
|
|
|
|
+ int wlen = MultiByteToWideChar(CP_UTF8, 0, mbs, -1, NULL, 0);
|
|
|
|
|
+ if (!wlen) {
|
|
|
|
|
+ errno = EINVAL;
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ wchar_t * wbuf = GGML_MALLOC(wlen * sizeof(wchar_t));
|
|
|
|
|
+ wlen = MultiByteToWideChar(CP_UTF8, 0, mbs, -1, wbuf, wlen);
|
|
|
|
|
+ if (!wlen) {
|
|
|
|
|
+ GGML_FREE(wbuf);
|
|
|
|
|
+ errno = EINVAL;
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return wbuf;
|
|
|
|
|
+}
|
|
|
|
|
+#endif
|
|
|
|
|
+
|
|
|
|
|
+FILE * ggml_fopen(const char * fname, const char * mode) {
|
|
|
|
|
+#ifdef _WIN32
|
|
|
|
|
+ FILE * file = NULL;
|
|
|
|
|
+
|
|
|
|
|
+ // convert fname (UTF-8)
|
|
|
|
|
+ wchar_t * wfname = ggml_mbstowcs(fname);
|
|
|
|
|
+ if (wfname) {
|
|
|
|
|
+ // convert mode (ANSI)
|
|
|
|
|
+ wchar_t * wmode = GGML_MALLOC(strlen(mode) + 1);
|
|
|
|
|
+ wchar_t * wmode_p = wmode;
|
|
|
|
|
+ do {
|
|
|
|
|
+ *wmode_p++ = (wchar_t)*mode;
|
|
|
|
|
+ } while (*mode++);
|
|
|
|
|
+
|
|
|
|
|
+ // open file
|
|
|
|
|
+ file = _wfopen(wfname, wmode);
|
|
|
|
|
+
|
|
|
|
|
+ GGML_FREE(wfname);
|
|
|
|
|
+ GGML_FREE(wmode);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return file;
|
|
|
|
|
+#else
|
|
|
|
|
+ return fopen(fname, mode);
|
|
|
|
|
+#endif
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
//
|
|
//
|
|
|
// cache line
|
|
// cache line
|
|
|
//
|
|
//
|
|
@@ -18739,7 +18795,7 @@ void ggml_graph_export(const struct ggml_cgraph * cgraph, const char * fname) {
|
|
|
|
|
|
|
|
// write binary data
|
|
// write binary data
|
|
|
{
|
|
{
|
|
|
- FILE * fout = fopen(fname, "wb");
|
|
|
|
|
|
|
+ FILE * fout = ggml_fopen(fname, "wb");
|
|
|
|
|
|
|
|
if (!fout) {
|
|
if (!fout) {
|
|
|
fprintf(stderr, "%s: failed to open %s\n", __func__, fname);
|
|
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 *
|
|
|
|
|
|
|
|
// read file into data
|
|
// read file into data
|
|
|
{
|
|
{
|
|
|
- FILE * fin = fopen(fname, "rb");
|
|
|
|
|
|
|
+ FILE * fin = ggml_fopen(fname, "rb");
|
|
|
if (!fin) {
|
|
if (!fin) {
|
|
|
fprintf(stderr, "%s: failed to open %s\n", __func__, fname);
|
|
fprintf(stderr, "%s: failed to open %s\n", __func__, fname);
|
|
|
return result;
|
|
return result;
|
|
@@ -19213,7 +19269,7 @@ static void ggml_graph_dump_dot_leaf_edge(FILE * fp, struct ggml_tensor * node,
|
|
|
void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename) {
|
|
void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename) {
|
|
|
char color[16];
|
|
char color[16];
|
|
|
|
|
|
|
|
- FILE * fp = fopen(filename, "w");
|
|
|
|
|
|
|
+ FILE * fp = ggml_fopen(filename, "w");
|
|
|
GGML_ASSERT(fp);
|
|
GGML_ASSERT(fp);
|
|
|
|
|
|
|
|
fprintf(fp, "digraph G {\n");
|
|
fprintf(fp, "digraph G {\n");
|
|
@@ -20531,7 +20587,7 @@ struct gguf_context * gguf_init_empty(void) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_params params) {
|
|
struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_params params) {
|
|
|
- FILE * file = fopen(fname, "rb");
|
|
|
|
|
|
|
+ FILE * file = ggml_fopen(fname, "rb");
|
|
|
if (!file) {
|
|
if (!file) {
|
|
|
return NULL;
|
|
return NULL;
|
|
|
}
|
|
}
|
|
@@ -21486,7 +21542,7 @@ static void gguf_write_to_buf(const struct gguf_context * ctx, struct gguf_buf *
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void gguf_write_to_file(const struct gguf_context * ctx, const char * fname, bool only_meta) {
|
|
void gguf_write_to_file(const struct gguf_context * ctx, const char * fname, bool only_meta) {
|
|
|
- FILE * file = fopen(fname, "wb");
|
|
|
|
|
|
|
+ FILE * file = ggml_fopen(fname, "wb");
|
|
|
if (!file) {
|
|
if (!file) {
|
|
|
GGML_ASSERT(false && "failed to open file for writing");
|
|
GGML_ASSERT(false && "failed to open file for writing");
|
|
|
}
|
|
}
|