Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
metal : use C++ to store piplienes
ggml-ci
  • Loading branch information
ggerganov committed Sep 16, 2025
commit 45d6c8ef62551a7a6e115b90a34a23ef45183098
1 change: 1 addition & 0 deletions ggml/src/ggml-metal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ggml_add_backend_library(ggml-metal
ggml-metal-device.cpp
ggml-metal-common.cpp
ggml-metal-context.m
ggml-metal-context.cpp
ggml-metal-ops.cpp
)

Expand Down
34 changes: 34 additions & 0 deletions ggml/src/ggml-metal/ggml-metal-context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "ggml-metal-context.h"

#include <string>
#include <unordered_map>

struct ggml_metal_pipelines {
std::unordered_map<std::string, ggml_metal_pipeline_t> data;
};

ggml_metal_pipelines_t ggml_metal_pipelines_init(void) {
ggml_metal_pipelines_t res = new ggml_metal_pipelines();

return res;
}

void ggml_metal_pipelines_free(ggml_metal_pipelines_t ppls) {
for (auto it = ppls->data.begin(); it != ppls->data.end(); ++it) {
ggml_metal_pipeline_free(it->second);
}

delete ppls;
}

void ggml_metal_pipelines_add(ggml_metal_pipelines_t ppls, const char * name, ggml_metal_pipeline_t pipeline) {
ppls->data[name] = pipeline;
}

ggml_metal_pipeline_t ggml_metal_pipelines_get(ggml_metal_pipelines_t ppls, const char * name) {
if (ppls->data.find(name) == ppls->data.end()) {
return nullptr;
}

return ppls->data[name];
}
30 changes: 23 additions & 7 deletions ggml/src/ggml-metal/ggml-metal-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ void ggml_metal_cv_free(ggml_metal_cv_t cv);
void ggml_metal_cv_set_int32(ggml_metal_cv_t cv, int32_t value, int32_t idx);
void ggml_metal_cv_set_bool (ggml_metal_cv_t cv, bool value, int32_t idx);

//
// MTLComputePipelineState wrapper
//

typedef struct ggml_metal_pipeline * ggml_metal_pipeline_t;

ggml_metal_pipeline_t ggml_metal_pipeline_init(void);
void ggml_metal_pipeline_free(ggml_metal_pipeline_t pipeline);

void * ggml_metal_pipeline_get_obj(ggml_metal_pipeline_t pipeline);

// a collection of pipelines
typedef struct ggml_metal_pipelines * ggml_metal_pipelines_t;

ggml_metal_pipelines_t ggml_metal_pipelines_init(void);
void ggml_metal_pipelines_free(ggml_metal_pipelines_t ppls);

void ggml_metal_pipelines_add(ggml_metal_pipelines_t ppls, const char * name, ggml_metal_pipeline_t pipeline);
ggml_metal_pipeline_t ggml_metal_pipelines_get(ggml_metal_pipelines_t ppls, const char * name);

//
// backend
//
Expand All @@ -27,20 +47,16 @@ typedef struct ggml_metal * ggml_metal_t;
ggml_metal_t ggml_metal_init(ggml_metal_device_t ctx_dev);
void ggml_metal_free(ggml_metal_t ctx);

typedef void * ggml_metal_pipeline_t;

ggml_metal_pipeline_t ggml_metal_get_pipeline(ggml_metal_t ctx, const char * name);

ggml_metal_pipeline_t ggml_metal_get_pipeline (ggml_metal_t ctx, const char * name);
ggml_metal_pipeline_t ggml_metal_compile_pipeline(ggml_metal_t ctx, const char * base, const char * name, ggml_metal_cv_t cv);

void ggml_metal_synchronize(ggml_metal_t ctx);

void ggml_metal_set_tensor_async(ggml_metal_t ctx, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
void ggml_metal_get_tensor_async(ggml_metal_t ctx, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);

enum ggml_status ggml_metal_graph_compute(ggml_metal_t ctx, struct ggml_cgraph * gf);

void ggml_metal_graph_optimize(ggml_metal_t ctx, struct ggml_cgraph * gf);
enum ggml_status ggml_metal_graph_compute (ggml_metal_t ctx, struct ggml_cgraph * gf);
void ggml_metal_graph_optimize(ggml_metal_t ctx, struct ggml_cgraph * gf);

void ggml_metal_set_n_cb (ggml_metal_t ctx, int n_cb);
void ggml_metal_set_abort_callback (ggml_metal_t ctx, ggml_abort_callback abort_callback, void * user_data);
Expand Down
Loading