Implementation:Ollama Ollama Imagegen MLX Wrappers
| Knowledge Sources | |
|---|---|
| Domains | Image Generation, Code Generation |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
A code generator tool that produces dynamic loading wrappers for the MLX-C library, enabling dlopen/dlsym-based function resolution at runtime.
Description
The generate_wrappers.go file is a build-time tool (tagged with go:build ignore) that scans MLX-C header files to extract function declarations and generates wrapper code. It produces both a header file (mlx.h) with wrapper function declarations that shadow the original MLX-C functions, and an implementation file (mlx.c) with function pointer definitions and initialization code. The tool handles complex C signatures including function pointers, ARM64-guarded declarations, and parameter name extraction from type-heavy declarations.
Usage
Run during the build process to regenerate MLX-C dynamic loading wrappers when the MLX-C library is updated.
Code Reference
Source Location
- Repository: Ollama
- File: x/imagegen/mlx/generate_wrappers.go
- Lines: 1-439
Signature
type Function struct {
Name string
ReturnType string
Params string
ParamNames []string
NeedsARM64Guard bool
}
func findHeaders(directory string) ([]string, error)
func cleanContent(content string) string
func extractParamNames(params string) []string
func splitParams(params string) []string
Import
// Build-time tool, not imported directly
// Usage: go run generate_wrappers.go <mlx-c-include-dir> <output-header> [output-impl]
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| mlx-c-include-dir | string | Yes | Directory containing MLX-C header files |
| output-header | string | Yes | Path for generated wrapper header file |
| output-impl | string | No | Path for generated implementation file |
Outputs
| Name | Type | Description |
|---|---|---|
| mlx.h | file | Generated header with wrapper function declarations |
| mlx.c | file | Generated implementation with function pointer definitions |
Usage Examples
// Run from command line:
// go run generate_wrappers.go ./build/_deps/mlx-c-src ./mlx.h ./mlx.c
// The tool parses C headers and generates wrappers like:
// static inline int mlx_add(...) { return mlx_add_ptr(...); }