Skip to content

Distribute and run LLMs with a single file.

Polyfill $HOME on some Windows systems

Justine Tunney93178c9a9a4d
diff --git a/llama.cpp/llava/llava-quantize.cpp b/llama.cpp/llava/llava-quantize.cpp
index 5b1e686a..a15446e9 100644
--- a/llama.cpp/llava/llava-quantize.cpp
+++ b/llama.cpp/llava/llava-quantize.cpp
@@ -12,6 +12,7 @@ int main(int argc, char *argv[]) {
printf("llamafile v" LLAMAFILE_VERSION_STRING " llava-quantize\n");
exit(0);
}
+ llamafile_init();
llamafile_check_cpu();
if (argc != 4) {
fprintf(stderr,
diff --git a/llama.cpp/main/main.cpp b/llama.cpp/main/main.cpp
index e2144f23..5d194bcd 100644
--- a/llama.cpp/main/main.cpp
+++ b/llama.cpp/main/main.cpp
@@ -113,6 +113,7 @@ int main(int argc, char ** argv) {
exit(0);
}
+ llamafile_init();
llamafile_check_cpu();
ShowCrashReports();
LoadZipArgs(&argc, &argv);
diff --git a/llama.cpp/perplexity/perplexity.cpp b/llama.cpp/perplexity/perplexity.cpp
index 146f80e9..8d91db37 100644
--- a/llama.cpp/perplexity/perplexity.cpp
+++ b/llama.cpp/perplexity/perplexity.cpp
@@ -682,6 +682,7 @@ static void hellaswag_score(llama_context * ctx, const gpt_params & params) {
int main(int argc, char ** argv) {
gpt_params params;
+ llamafile_init();
llamafile_check_cpu();
params.n_batch = 512;
diff --git a/llama.cpp/quantize/quantize.cpp b/llama.cpp/quantize/quantize.cpp
index 30f2c608..3ab628a7 100644
--- a/llama.cpp/quantize/quantize.cpp
+++ b/llama.cpp/quantize/quantize.cpp
@@ -101,6 +101,7 @@ int main(int argc, char ** argv) {
exit(0);
}
+ llamafile_init();
llamafile_check_cpu();
if (argc < 3) {
diff --git a/llama.cpp/server/server.cpp b/llama.cpp/server/server.cpp
index 1a3fc2ab..98268df0 100644
--- a/llama.cpp/server/server.cpp
+++ b/llama.cpp/server/server.cpp
@@ -2644,6 +2644,7 @@ int main(int argc, char **argv)
exit(0);
}
+ llamafile_init();
llamafile_check_cpu();
LoadZipArgs(&argc, &argv);
ShowCrashReports();
diff --git a/llamafile/init.c b/llamafile/init.c
new file mode 100644
index 00000000..6104df3b
--- /dev/null
+++ b/llamafile/init.c
@@ -0,0 +1,38 @@
+// -*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
+// vi: set et ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi
+//
+// Copyright 2023 Mozilla Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <cosmo.h>
+#include <stdlib.h>
+#include <string.h>
+
+void llamafile_init(void) {
+
+ // todo(jart): cosmo runtime should do this
+ if (IsWindows()) {
+ static char home[256];
+ if (!getenv("HOME")) {
+ const char *homepath = getenv("HOMEPATH");
+ const char *homedrive = getenv("HOMEDRIVE");
+ if (homedrive && homepath) {
+ strlcpy(home, "HOME=", sizeof(home));
+ strlcat(home, homedrive, sizeof(home));
+ strlcat(home, homepath, sizeof(home));
+ putenv(home);
+ }
+ }
+ }
+}
diff --git a/llamafile/llamafile.h b/llamafile/llamafile.h
index ed975a55..d51c63dd 100644
--- a/llamafile/llamafile.h
+++ b/llamafile/llamafile.h
@@ -17,6 +17,7 @@ size_t llamafile_tell(struct llamafile *);
size_t llamafile_size(struct llamafile *);
FILE *llamafile_fp(struct llamafile *);
+void llamafile_init(void);
void llamafile_check_cpu(void);
const char *llamafile_get_tmp_dir(void);
bool llamafile_extract(const char *, const char *);