Implementation:Ollama Ollama Imagegen MLX Header
| Knowledge Sources | |
|---|---|
| Domains | Image Generation, Native Bindings |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Auto-generated C header providing wrapper function declarations that shadow MLX-C originals for dynamic loading via dlopen/dlsym.
Description
The mlx.h file is auto-generated by generate_wrappers.go and provides the indirection layer between Go CGo calls and the dynamically loaded MLX-C library. It includes the original MLX-C headers for type definitions, then undefines all original function macros and replaces them with inline wrapper functions that delegate to function pointers (defined in mlx.c). This allows Go code to call C.mlx_add() etc. which transparently routes through dlsym-resolved function pointers. ARM64-guarded sections handle platform-specific float16/bfloat16 operations.
Usage
Included by mlx.go via CGo (#include "mlx.h") to provide all MLX-C function declarations for the Go bindings.
Code Reference
Source Location
- Repository: Ollama
- File: x/imagegen/mlx/mlx.h
- Lines: 1-2337
Signature
#ifndef MLX_WRAPPERS_H
#define MLX_WRAPPERS_H
#include "mlx/c/mlx.h"
#include "mlx_dynamic.h"
// Undefine originals then provide wrappers:
#undef mlx_add
static inline int mlx_add(mlx_array* res, const mlx_array a, const mlx_array b, const mlx_stream s) {
return mlx_add_ptr(res, a, b, s);
}
// ... hundreds more wrappers for all MLX-C operations
#endif
Import
#include "mlx.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (N/A) | - | - | Header file providing declarations; no runtime inputs |
Outputs
| Name | Type | Description |
|---|---|---|
| (N/A) | - | Provides inline wrapper functions for all MLX-C operations |
Usage Examples
// In Go CGo code (mlx.go):
// #include "mlx.h"
// The wrapper transparently routes through function pointers:
mlx_array result;
mlx_add(&result, a, b, default_stream());