Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ollama Ollama MLXRunner Generator

From Leeroopedia
Knowledge Sources
Domains MLX Runtime, Code Generation
Last Updated 2025-02-15 00:00 GMT

Overview

Code generator that parses MLX C header files and produces the generated.c and generated.h files containing dynamic binding wrappers for all MLX C API functions.

Description

Uses tree-sitter with the C++ grammar to parse header files passed as command-line arguments. Extracts function declarations (name, return type, parameters) using a tree-sitter query for function_declarator nodes. Applies Go templates (*.gotmpl files embedded via //go:embed) to generate the C source and header files. Invoked via go:generate from mlx.go.

Usage

Build-time tooling invoked when MLX is updated to a new version. Running go generate regenerates the bindings from the updated headers.

Code Reference

Source Location

  • Repository: Ollama
  • File: x/mlxrunner/mlx/generator/main.go
  • Lines: 1-135

Signature

type Function struct {
    Type, Name, Parameters, Args string
}

func ParseFunction(node *tree_sitter.Node, tc *tree_sitter.TreeCursor, source []byte) Function
func ParseParameters(node *tree_sitter.Node, tc *tree_sitter.TreeCursor, source []byte) string
func main()

Import

// This is a standalone tool, run via go generate
//go:generate go run ./generator ...

I/O Contract

Inputs

Name Type Required Description
-output string No Output directory for generated files (default ".")
args []string Yes MLX C header file paths to parse

Outputs

Name Type Description
generated.c file C source with function pointers and dynamic loader
generated.h file C header with macros, externs, and inline wrappers

Usage Examples

// Invoked via go:generate in mlx.go
//go:generate go run ./generator -output . /path/to/mlx/c/headers/*.h

// The generator parses C headers with tree-sitter
// and applies .gotmpl templates to produce generated.c and generated.h

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment