-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mtl : adapt the MNIST example as starter
- Loading branch information
Showing
4 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "ggml.h" | ||
#include "mtl.h" | ||
|
||
#include <cstdio> | ||
#include <cstring> | ||
#include <cstdlib> | ||
|
||
int main(int argc, char ** argv) { | ||
ggml_time_init(); | ||
|
||
if (argc != 2) { | ||
fprintf(stderr, "Usage: %s llama.ggml\n", argv[0]); | ||
return -1; | ||
} | ||
|
||
const char * fname_cgraph = argv[1]; | ||
|
||
// load the compute graph | ||
struct ggml_context * ctx_data = NULL; | ||
struct ggml_context * ctx_eval = NULL; | ||
|
||
struct ggml_cgraph gf = ggml_graph_import(fname_cgraph, &ctx_data, &ctx_eval); | ||
gf.n_threads = 1; | ||
|
||
// allocate work context | ||
static size_t buf_size = gf.work_size; // TODO | ||
static void * buf = malloc(buf_size); | ||
|
||
struct ggml_init_params params = { | ||
/*.mem_size =*/ buf_size, | ||
/*.mem_buffer =*/ buf, | ||
/*.no_alloc =*/ false, | ||
}; | ||
|
||
struct ggml_context * ctx_work = ggml_init(params); | ||
|
||
// this allocates all Metal resources and memory buffers | ||
auto * ctx_mtl = llama_mtl_init(ctx_data, ctx_eval, ctx_work, &gf); | ||
|
||
// the actual inference happens here | ||
llama_mtl_eval(ctx_mtl, &gf); | ||
|
||
llama_mtl_free(ctx_mtl); | ||
|
||
ggml_free(ctx_work); | ||
ggml_free(ctx_data); | ||
ggml_free(ctx_eval); | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
|
||
struct ggml_context; | ||
struct ggml_cgraph; | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
struct ggml_mtl_context; | ||
|
||
struct ggml_mtl_context * llama_mtl_init( | ||
struct ggml_context * ctx_data, | ||
struct ggml_context * ctx_eval, | ||
struct ggml_context * ctx_work, | ||
struct ggml_cgraph * gf); | ||
|
||
void llama_mtl_free(struct ggml_mtl_context * ctx); | ||
|
||
// return 0 on success | ||
int llama_mtl_eval( | ||
struct ggml_mtl_context * ctx, | ||
struct ggml_cgraph * gf); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
Oops, something went wrong.