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 Imagegen Runner

From Leeroopedia
Knowledge Sources
Domains Image Generation, Server
Last Updated 2025-02-15 00:00 GMT

Overview

Entry point for the unified MLX runner subprocess, initializing MLX, detecting model mode, and starting the HTTP server.

Description

The runner.go file implements the Execute function, which is the main entry point for the MLX runner subprocess spawned by the Server. It parses --model and --port flags, initializes the MLX library (dynamic loading and GPU setup), detects whether the model is an image generation or LLM model via DetectModelType (checking model_index.json for pipeline class names like ZImagePipeline, FluxPipeline, Flux2KleinPipeline), and creates the server with appropriate model loading. The HTTP server provides /health, /completion, /tokenize, and /embedding endpoints with graceful shutdown via SIGINT/SIGTERM handling. The server struct routes requests based on mode (ModeLLM or ModeImageGen).

Usage

Called by the Ollama binary when spawning an MLX runner subprocess (ollama runner --imagegen-engine --model <name> --port <port>).

Code Reference

Source Location

  • Repository: Ollama
  • File: x/imagegen/runner.go
  • Lines: 1-203

Signature

func Execute(args []string) error

type ModelMode int

const (
	ModeImageGen ModelMode = iota
	ModeLLM
)

type server struct {
	mode       ModelMode
	modelName  string
	port       int
	imageModel ImageModel
	llmModel   *llmState
}

func newServer(modelName string, port int, mode ModelMode) (*server, error)
func detectModelMode(modelName string) ModelMode

Import

import "github.com/ollama/ollama/x/imagegen"

I/O Contract

Inputs

Name Type Required Description
args []string Yes Command-line arguments (--model, --port)

Outputs

Name Type Description
error error Error from initialization or server startup

Usage Examples

// Called internally by Server subprocess:
err := imagegen.Execute([]string{"--model", "flux2-klein:latest", "--port", "54321"})
// Starts HTTP server on 127.0.0.1:54321 with model loaded

Related Pages

Page Connections

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