Implementation:Ollama Ollama Imagegen MLX C
| Knowledge Sources | |
|---|---|
| Domains | Image Generation, Native Bindings |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Auto-generated C file containing function pointer definitions and initialization for dynamic loading of the MLX-C library.
Description
The mlx.c file is auto-generated by generate_wrappers.go and contains all function pointer definitions for the MLX-C library's API surface. It defines NULL-initialized function pointers for hundreds of MLX operations (array creation, manipulation, math ops, neural network ops, etc.) and provides an init_mlx_wrappers() function that resolves all symbols via dlsym at runtime. This enables Ollama to load the MLX-C shared library dynamically rather than linking at compile time, supporting graceful degradation when MLX is unavailable. ARM64-guarded declarations handle platform-specific types like float16_t and bfloat16_t.
Usage
Compiled as part of the MLX CGo bridge; function pointers are initialized once at startup via mlx_dynamic_init() and init_mlx_wrappers().
Code Reference
Source Location
- Repository: Ollama
- File: x/imagegen/mlx/mlx.c
- Lines: 1-5786
Signature
// Function pointer definitions (examples from ~5786 lines)
size_t (*mlx_dtype_size_ptr)(mlx_dtype dtype) = NULL;
mlx_array (*mlx_array_new_ptr)(void) = NULL;
int (*mlx_array_free_ptr)(mlx_array arr) = NULL;
mlx_array (*mlx_array_new_data_ptr)(const void* data, const int* shape, int dim, mlx_dtype dtype) = NULL;
// Initialization function
int init_mlx_wrappers(void);
Import
#include "mlx/c/mlx.h"
#include "mlx_dynamic.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | - | - | Function pointers are initialized from the loaded MLX-C shared library handle |
Outputs
| Name | Type | Description |
|---|---|---|
| return | int | 0 on success, -1 if any required symbol cannot be resolved |
Usage Examples
// Called once at startup after mlx_dynamic_init()
if (init_mlx_wrappers() != 0) {
fprintf(stderr, "Failed to initialize MLX wrappers\n");
return -1;
}
// After initialization, function pointers can be called:
mlx_array arr = mlx_array_new_float32_ptr(3.14f);