Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,18 @@ if (LLAMA_CUBLAS)
endif()

if (LLAMA_METAL)
add_executable(embedfile embedfile.c)

add_custom_command(
OUTPUT ggml_metal_file.c
COMMAND embedfile ggml_metal_file bin/ggml-metal.metal
DEPENDS ggml-metal.metal)

find_library(FOUNDATION_LIBRARY Foundation REQUIRED)
find_library(METAL_FRAMEWORK Metal REQUIRED)
find_library(METALKIT_FRAMEWORK MetalKit REQUIRED)

set(GGML_SOURCES_METAL ggml-metal.m ggml-metal.h)
set(GGML_SOURCES_METAL ggml_metal_file.c ggml-metal.m ggml-metal.h)

add_compile_definitions(GGML_USE_METAL)
#add_compile_definitions(GGML_METAL_NDEBUG)
Expand Down
52 changes: 52 additions & 0 deletions embedfile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdlib.h>
#include <stdio.h>

FILE* open_or_exit(const char* fname, const char* mode)
{
FILE* f = fopen(fname, mode);
if (f == NULL) {
perror(fname);
exit(EXIT_FAILURE);
}
return f;
}

int main(int argc, char** argv)
{
if (argc < 3) {
fprintf(stderr, "USAGE: %s {sym} {rsrc}\n\n"
" Creates {sym}.c from the contents of {rsrc}\n",
argv[0]);
return EXIT_FAILURE;
}

const char* sym = argv[1];
FILE* in = open_or_exit(argv[2], "r");

char symfile[256];
snprintf(symfile, sizeof(symfile), "%s.c", sym);

FILE* out = open_or_exit(symfile,"w");
fprintf(out, "#include <stdlib.h>\n");
fprintf(out, "const char %s[] = {\n", sym);

unsigned char buf[256];
size_t nread = 0;
size_t linecount = 0;
do {
nread = fread(buf, 1, sizeof(buf), in);
size_t i;
for (i=0; i < nread; i++) {
fprintf(out, "0x%02x, ", buf[i]);
if (++linecount == 10) { fprintf(out, "\n"); linecount = 0; }
}
} while (nread > 0);
if (linecount > 0) fprintf(out, "\n");
fprintf(out, "};\n");
fprintf(out, "const size_t %s_len = sizeof(%s);\n\n",sym,sym);

fclose(in);
fclose(out);

return EXIT_SUCCESS;
}
7 changes: 6 additions & 1 deletion ggml-metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@
#undef GGML_METAL_DECL_KERNEL
};

const char ggml_metal_file[];
const size_t ggml_metal_file_len;

// MSL code
// TODO: move the contents here when ready
// for now it is easier to work in a separate file
Expand Down Expand Up @@ -140,7 +143,8 @@ @implementation GGMLMetalClass

ctx->d_queue = dispatch_queue_create("llama.cpp", DISPATCH_QUEUE_CONCURRENT);

#if 0
#if 1
NSString * const msl_library_source = [[NSString alloc] initWithBytes: ggml_metal_file length: ggml_metal_file_len encoding: NSASCIIStringEncoding];
// compile from source string and show compile log
{
NSError * error = nil;
Expand All @@ -151,6 +155,7 @@ @implementation GGMLMetalClass
return NULL;
}
}
[msl_library_source release];
#else
UNUSED(msl_library_source);

Expand Down