Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.
x/imagegen: add naive TeaCache and FP8 quantization support (#13683)
Jeffrey Morgan9667c2282f47
diff --git a/api/client.go b/api/client.goindex c70516899..d70672a6b 100644--- a/api/client.go+++ b/api/client.go@@ -165,7 +165,7 @@ func (c *Client) do(ctx context.Context, method, path string, reqData, respDatareturn nil}-const maxBufferSize = 512 * format.KiloByte+const maxBufferSize = 8 * format.MegaBytefunc (c *Client) stream(ctx context.Context, method, path string, data any, fn func([]byte) error) error {var buf io.Readerdiff --git a/cmd/cmd.go b/cmd/cmd.goindex 187191be3..8a0811638 100644--- a/cmd/cmd.go+++ b/cmd/cmd.go@@ -100,7 +100,8 @@ func CreateHandler(cmd *cobra.Command, args []string) error {if filename == "" {// No Modelfile found - check if current directory is an image gen modelif imagegen.IsTensorModelDir(".") {- return imagegenclient.CreateModel(args[0], ".", p)+ quantize, _ := cmd.Flags().GetString("quantize")+ return imagegenclient.CreateModel(args[0], ".", quantize, p)}reader = strings.NewReader("FROM .\n")} else {@@ -464,14 +465,6 @@ func RunHandler(cmd *cobra.Command, args []string) error {name := args[0]- // Check if this is a known image generation model (skip Show/Pull)- if imagegen.HasTensorLayers(name) {- if opts.Prompt == "" && !interactive {- return errors.New("image generation models require a prompt. Usage: ollama run " + name + " \"your prompt here\"")- }- return imagegen.RunCLI(cmd, name, opts.Prompt, interactive, opts.KeepAlive)- }-info, err := func() (*api.ShowResponse, error) {showReq := &api.ShowRequest{Name: name}info, err := client.Show(cmd.Context(), showReq)@@ -533,6 +526,14 @@ func RunHandler(cmd *cobra.Command, args []string) error {return generateEmbedding(cmd, name, opts.Prompt, opts.KeepAlive, truncate, dimensions)}+ // Check if this is an image generation model+ if slices.Contains(info.Capabilities, model.CapabilityImageGeneration) {+ if opts.Prompt == "" && !interactive {+ return errors.New("image generation models require a prompt. Usage: ollama run " + name + " \"your prompt here\"")+ }+ return imagegen.RunCLI(cmd, name, opts.Prompt, interactive, opts.KeepAlive)+ }+// Check for experimental flagisExperimental, _ := cmd.Flags().GetBool("experimental")yoloMode, _ := cmd.Flags().GetBool("experimental-yolo")@@ -671,7 +672,11 @@ func PushHandler(cmd *cobra.Command, args []string) error {bar, ok := bars[resp.Digest]if !ok {- bar = progress.NewBar(fmt.Sprintf("pushing %s...", resp.Digest[7:19]), resp.Total, resp.Completed)+ msg := resp.Status+ if msg == "" {+ msg = fmt.Sprintf("pushing %s...", resp.Digest[7:19])+ }+ bar = progress.NewBar(msg, resp.Total, resp.Completed)bars[resp.Digest] = barp.Add(resp.Digest, bar)}@@ -837,11 +842,6 @@ func DeleteHandler(cmd *cobra.Command, args []string) error {}func ShowHandler(cmd *cobra.Command, args []string) error {- // Check if this is an image generation model- if imagegen.HasTensorLayers(args[0]) {- return imagegen.Show(args[0], os.Stdout)- }-client, err := api.ClientFromEnvironment()if err != nil {return errdiff --git a/cmd/cmd_test.go b/cmd/cmd_test.goindex 7dc3d0093..4f651135b 100644--- a/cmd/cmd_test.go+++ b/cmd/cmd_test.go@@ -1547,6 +1547,79 @@ func TestRunOptions_Copy_ThinkValueVariants(t *testing.T) {}}+func TestShowInfoImageGen(t *testing.T) {+ var b bytes.Buffer+ err := showInfo(&api.ShowResponse{+ Details: api.ModelDetails{+ Family: "ZImagePipeline",+ ParameterSize: "10.3B",+ QuantizationLevel: "FP8",+ },+ Capabilities: []model.Capability{model.CapabilityImageGeneration},+ Requires: "0.14.0",+ }, false, &b)+ if err != nil {+ t.Fatal(err)+ }++ expect := " Model\n" ++ " architecture ZImagePipeline \n" ++ " parameters 10.3B \n" ++ " quantization FP8 \n" ++ " requires 0.14.0 \n" ++ "\n" ++ " Capabilities\n" ++ " image \n" ++ "\n"+ if diff := cmp.Diff(expect, b.String()); diff != "" {+ t.Errorf("unexpected output (-want +got):\n%s", diff)+ }+}++func TestPushProgressMessage(t *testing.T) {+ tests := []struct {+ name string+ status string+ digest string+ wantMsg string+ }{+ {+ name: "uses status when provided",+ status: "uploading model",+ digest: "sha256:abc123456789def",+ wantMsg: "uploading model",+ },+ {+ name: "falls back to digest when status empty",+ status: "",+ digest: "sha256:abc123456789def",+ wantMsg: "pushing abc123456789...",+ },+ {+ name: "handles short digest gracefully",+ status: "",+ digest: "sha256:abc",+ wantMsg: "pushing sha256:abc...",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ msg := tt.status+ if msg == "" {+ if len(tt.digest) >= 19 {+ msg = fmt.Sprintf("pushing %s...", tt.digest[7:19])+ } else {+ msg = fmt.Sprintf("pushing %s...", tt.digest)+ }+ }+ if msg != tt.wantMsg {+ t.Errorf("got %q, want %q", msg, tt.wantMsg)+ }+ })+ }+}+func TestRunOptions_Copy_Independence(t *testing.T) {// Test that modifications to original don't affect copyoriginalThink := &api.ThinkValue{Value: "original"}diff --git a/server/routes.go b/server/routes.goindex c58a3db51..7748e7629 100644--- a/server/routes.go+++ b/server/routes.go@@ -1124,6 +1124,15 @@ func GetModelInfo(req api.ShowRequest) (*api.ShowResponse, error) {QuantizationLevel: m.Config.FileType,}+ // For image generation models, populate details from imagegen package+ if slices.Contains(m.Capabilities(), model.CapabilityImageGeneration) {+ if info, err := imagegen.GetModelInfo(name.String()); err == nil {+ modelDetails.Family = info.Architecture+ modelDetails.ParameterSize = format.HumanNumber(uint64(info.ParameterCount))+ modelDetails.QuantizationLevel = info.Quantization+ }+ }+if req.System != "" {m.System = req.System}@@ -1206,6 +1215,10 @@ func GetModelInfo(req api.ShowRequest) (*api.ShowResponse, error) {return resp, nil}+ if slices.Contains(m.Capabilities(), model.CapabilityImageGeneration) {+ return resp, nil+ }+kvData, tensors, err := getModelData(m.ModelPath, req.Verbose)if err != nil {return nil, errdiff --git a/x/imagegen/README.md b/x/imagegen/README.mdindex 38abfc427..10d4beafa 100644--- a/x/imagegen/README.md+++ b/x/imagegen/README.md@@ -234,3 +234,17 @@ ollama create z-image3. Copy config files (*.json) as config layers4. Write manifest```++## FP8 Quantization++Z-Image supports FP8 quantization to reduce memory usage by ~50% while maintaining image quality.++### Usage++```bash+cd ./weights/Z-Image-Turbo+ollama create z-image-fp8 --quantize fp8+```++This quantizes weights during import. The resulting model will be ~15GB instead of ~31GB.+diff --git a/x/imagegen/api/handler.go b/x/imagegen/api/handler.goindex f66ed6d85..7d489f3dd 100644--- a/x/imagegen/api/handler.go+++ b/x/imagegen/api/handler.go@@ -1,10 +1,8 @@package apiimport (- "encoding/base64""fmt""net/http"- "os""strconv""strings""time"@@ -101,10 +99,10 @@ func handleStreamingResponse(c *gin.Context, runner llm.LlamaServer, req llm.Comc.Header("Cache-Control", "no-cache")c.Header("Connection", "keep-alive")- var imagePath string+ var imageBase64 stringerr := runner.Completion(c.Request.Context(), req, func(resp llm.CompletionResponse) {if resp.Done {- imagePath = extractPath(resp.Content)+ imageBase64 = extractBase64(resp.Content)} else {progress := parseProgress(resp.Content)if progress.Total > 0 {@@ -118,14 +116,14 @@ func handleStreamingResponse(c *gin.Context, runner llm.LlamaServer, req llm.Comreturn}- c.SSEvent("done", buildResponse(imagePath, format))+ c.SSEvent("done", buildResponse(imageBase64, format))}func handleNonStreamingResponse(c *gin.Context, runner llm.LlamaServer, req llm.CompletionRequest, format string) {- var imagePath string+ var imageBase64 stringerr := runner.Completion(c.Request.Context(), req, func(resp llm.CompletionResponse) {if resp.Done {- imagePath = extractPath(resp.Content)+ imageBase64 = extractBase64(resp.Content)}})if err != nil {@@ -133,7 +131,7 @@ func handleNonStreamingResponse(c *gin.Context, runner llm.LlamaServer, req llm.return}- c.JSON(http.StatusOK, buildResponse(imagePath, format))+ c.JSON(http.StatusOK, buildResponse(imageBase64, format))}func parseSize(size string) (int32, int32) {@@ -152,9 +150,9 @@ func parseSize(size string) (int32, int32) {return int32(w), int32(h)}-func extractPath(content string) string {- if idx := strings.Index(content, "Image saved to: "); idx >= 0 {- return strings.TrimSpace(content[idx+16:])+func extractBase64(content string) string {+ if strings.HasPrefix(content, "IMAGE_BASE64:") {+ return content[13:]}return ""}@@ -165,23 +163,21 @@ func parseProgress(content string) ImageProgressEvent {return ImageProgressEvent{Step: step, Total: total}}-func buildResponse(imagePath, format string) ImageGenerationResponse {+func buildResponse(imageBase64, format string) ImageGenerationResponse {resp := ImageGenerationResponse{Created: time.Now().Unix(),Data: make([]ImageData, 1),}- if imagePath == "" {+ if imageBase64 == "" {return resp}if format == "url" {- resp.Data[0].URL = "file://" + imagePath+ // URL format not supported when using base64 transfer+ resp.Data[0].B64JSON = imageBase64} else {- data, err := os.ReadFile(imagePath)- if err == nil {- resp.Data[0].B64JSON = base64.StdEncoding.EncodeToString(data)- }+ resp.Data[0].B64JSON = imageBase64}return respdiff --git a/x/imagegen/cache/teacache.go b/x/imagegen/cache/teacache.gonew file mode 100644index 000000000..60031d8cb--- /dev/null+++ b/x/imagegen/cache/teacache.go@@ -0,0 +1,197 @@+//go:build mlx++// Package cache provides caching mechanisms for diffusion model inference.+package cache++import (+ "github.com/ollama/ollama/x/imagegen/mlx"+)++// TeaCache implements Timestep Embedding Aware Caching for diffusion models.+// It caches the transformer output and reuses it when timestep values+// are similar between consecutive steps.+//+// For CFG (classifier-free guidance), it caches pos and neg predictions+// separately and always computes CFG fresh to avoid error amplification.+//+// Reference: "Timestep Embedding Tells: It's Time to Cache for Video Diffusion Model"+// https://github.com/ali-vilab/TeaCache+type TeaCache struct {+ // Cached transformer output from last computed step (non-CFG mode)+ cachedOutput *mlx.Array++ // Cached CFG outputs (pos and neg separately)+ cachedPosOutput *mlx.Array+ cachedNegOutput *mlx.Array++ // Previous timestep value for difference calculation+ prevTimestep float32++ // Accumulated difference for rescaling+ accumulatedDiff float32++ // Configuration+ threshold float32 // Threshold for recomputation decision+ rescaleFactor float32 // Model-specific rescaling factor+ skipEarlySteps int // Number of early steps to never cache++ // Statistics+ cacheHits int+ cacheMisses int+}++// TeaCacheConfig holds configuration for TeaCache.+type TeaCacheConfig struct {+ // Threshold for recomputation. Lower = more cache hits, potential quality loss.+ // Recommended: 0.05-0.15 for image models+ Threshold float32++ // Rescale factor to adjust timestep embedding differences.+ // Model-specific, typically 1.0-2.0+ RescaleFactor float32++ // SkipEarlySteps: number of early steps to always compute (never cache).+ // Set to 2-3 for CFG mode to preserve structure. 0 = no skipping.+ SkipEarlySteps int+}++// DefaultTeaCacheConfig returns default configuration for TeaCache.+func DefaultTeaCacheConfig() *TeaCacheConfig {+ return &TeaCacheConfig{+ Threshold: 0.1,+ RescaleFactor: 1.0,+ }+}++// NewTeaCache creates a new TeaCache instance.+func NewTeaCache(cfg *TeaCacheConfig) *TeaCache {+ if cfg == nil {+ cfg = DefaultTeaCacheConfig()+ }+ return &TeaCache{+ threshold: cfg.Threshold,+ rescaleFactor: cfg.RescaleFactor,+ skipEarlySteps: cfg.SkipEarlySteps,+ }+}++// ShouldCompute determines if we should compute the full forward pass+// or reuse the cached output based on timestep similarity.+//+// Algorithm:+// 1. First step always computes+// 2. Subsequent steps compare |currTimestep - prevTimestep| * rescaleFactor+// 3. If accumulated difference > threshold, compute new output+// 4. Otherwise, reuse cached output+func (tc *TeaCache) ShouldCompute(step int, timestep float32) bool {+ // Always compute early steps (critical for structure)+ // Check both regular cache and CFG cache+ hasCachedOutput := tc.cachedOutput != nil || tc.HasCFGCache()+ if step < tc.skipEarlySteps || step == 0 || !hasCachedOutput {+ return true+ }++ // Compute absolute difference between current and previous timestep+ diff := timestep - tc.prevTimestep+ if diff < 0 {+ diff = -diff+ }++ // Apply rescaling factor+ scaledDiff := diff * tc.rescaleFactor++ // Accumulate difference (helps track drift over multiple cached steps)+ tc.accumulatedDiff += scaledDiff++ // Decision based on accumulated difference+ if tc.accumulatedDiff > tc.threshold {+ tc.accumulatedDiff = 0 // Reset accumulator+ return true+ }++ return false+}++// UpdateCache stores the computed output for potential reuse (non-CFG mode).+func (tc *TeaCache) UpdateCache(output *mlx.Array, timestep float32) {+ // Free previous cached output+ if tc.cachedOutput != nil {+ tc.cachedOutput.Free()+ }++ // Store new cached values+ tc.cachedOutput = output+ tc.prevTimestep = timestep+ tc.cacheMisses+++}++// UpdateCFGCache stores pos and neg outputs separately for CFG mode.+// This allows CFG to be computed fresh each step, avoiding error amplification.+func (tc *TeaCache) UpdateCFGCache(posOutput, negOutput *mlx.Array, timestep float32) {+ // Free previous cached outputs+ if tc.cachedPosOutput != nil {+ tc.cachedPosOutput.Free()+ }+ if tc.cachedNegOutput != nil {+ tc.cachedNegOutput.Free()+ }++ // Store new cached values+ tc.cachedPosOutput = posOutput+ tc.cachedNegOutput = negOutput+ tc.prevTimestep = timestep+ tc.cacheMisses+++}++// GetCached returns the cached output (non-CFG mode).+func (tc *TeaCache) GetCached() *mlx.Array {+ tc.cacheHits+++ return tc.cachedOutput+}++// GetCFGCached returns cached pos and neg outputs for CFG mode.+func (tc *TeaCache) GetCFGCached() (pos, neg *mlx.Array) {+ tc.cacheHits+++ return tc.cachedPosOutput, tc.cachedNegOutput+}++// HasCFGCache returns true if CFG cache is available.+func (tc *TeaCache) HasCFGCache() bool {+ return tc.cachedPosOutput != nil && tc.cachedNegOutput != nil+}++// Arrays returns all arrays that should be kept alive.+func (tc *TeaCache) Arrays() []*mlx.Array {+ var arrays []*mlx.Array+ if tc.cachedOutput != nil {+ arrays = append(arrays, tc.cachedOutput)+ }+ if tc.cachedPosOutput != nil {+ arrays = append(arrays, tc.cachedPosOutput)+ }+ if tc.cachedNegOutput != nil {+ arrays = append(arrays, tc.cachedNegOutput)+ }+ return arrays+}++// Stats returns cache hit/miss statistics.+func (tc *TeaCache) Stats() (hits, misses int) {+ return tc.cacheHits, tc.cacheMisses+}++// Free releases all cached arrays.+func (tc *TeaCache) Free() {+ if tc.cachedOutput != nil {+ tc.cachedOutput.Free()+ tc.cachedOutput = nil+ }+ if tc.cachedPosOutput != nil {+ tc.cachedPosOutput.Free()+ tc.cachedPosOutput = nil+ }+ if tc.cachedNegOutput != nil {+ tc.cachedNegOutput.Free()+ tc.cachedNegOutput = nil+ }+}diff --git a/x/imagegen/cli.go b/x/imagegen/cli.goindex 1268f449a..f287fa3fb 100644--- a/x/imagegen/cli.go+++ b/x/imagegen/cli.go@@ -44,62 +44,64 @@ func DefaultOptions() ImageGenOptions {}}-// Show displays information about an image generation model.-func Show(modelName string, w io.Writer) error {+// ModelInfo contains metadata about an image generation model.+type ModelInfo struct {+ Architecture string+ ParameterCount int64+ Quantization string+}++// GetModelInfo returns metadata about an image generation model.+func GetModelInfo(modelName string) (*ModelInfo, error) {manifest, err := LoadManifest(modelName)if err != nil {- return fmt.Errorf("failed to load manifest: %w", err)+ return nil, fmt.Errorf("failed to load manifest: %w", err)}- // Count total size- var totalSize int64- for _, layer := range manifest.Manifest.Layers {- if layer.MediaType == "application/vnd.ollama.image.tensor" {- totalSize += layer.Size- }- }+ info := &ModelInfo{}- // Read model_index.json for architecture- var architecture string+ // Read model_index.json for architecture, parameter count, and quantizationif data, err := manifest.ReadConfig("model_index.json"); err == nil {var index struct {- Architecture string `json:"architecture"`+ Architecture string `json:"architecture"`+ ParameterCount int64 `json:"parameter_count"`+ Quantization string `json:"quantization"`}if json.Unmarshal(data, &index) == nil {- architecture = index.Architecture+ info.Architecture = index.Architecture+ info.ParameterCount = index.ParameterCount+ info.Quantization = index.Quantization}}- // Estimate parameter count from total size (assuming BF16 = 2 bytes per param)- paramCount := totalSize / 2- paramStr := formatParamCount(paramCount)-- // Print Model info- fmt.Fprintln(w, " Model")- if architecture != "" {- fmt.Fprintf(w, " %-20s %s\n", "architecture", architecture)+ // Fallback: detect quantization from tensor names if not in config+ if info.Quantization == "" {+ for _, layer := range manifest.Manifest.Layers {+ if strings.HasSuffix(layer.Name, ".weight_scale") {+ info.Quantization = "FP8"+ break+ }+ }+ if info.Quantization == "" {+ info.Quantization = "BF16"+ }}- fmt.Fprintf(w, " %-20s %s\n", "parameters", paramStr)- fmt.Fprintf(w, " %-20s %s\n", "quantization", "BF16")- fmt.Fprintln(w)-- // Print Capabilities- fmt.Fprintln(w, " Capabilities")- fmt.Fprintf(w, " %s\n", "image")- fmt.Fprintln(w)- return nil-}--// formatParamCount formats parameter count as human-readable string.-func formatParamCount(count int64) string {- if count >= 1_000_000_000 {- return fmt.Sprintf("%.1fB", float64(count)/1_000_000_000)- }- if count >= 1_000_000 {- return fmt.Sprintf("%.1fM", float64(count)/1_000_000)+ // Fallback: estimate parameter count if not in config+ if info.ParameterCount == 0 {+ var totalSize int64+ for _, layer := range manifest.Manifest.Layers {+ if layer.MediaType == "application/vnd.ollama.image.tensor" {+ if !strings.HasSuffix(layer.Name, "_scale") && !strings.HasSuffix(layer.Name, "_qbias") {+ totalSize += layer.Size+ }+ }+ }+ // Assume BF16 (2 bytes/param) as rough estimate+ info.ParameterCount = totalSize / 2}- return fmt.Sprintf("%d", count)++ return info, nil}// RegisterFlags adds image generation flags to the given command.@@ -183,8 +185,7 @@ func generateImageWithOptions(cmd *cobra.Command, modelName, prompt string, keepp.Add("", spinner)var stepBar *progress.StepBar- var imagePath string-+ var imageBase64 stringerr = client.Generate(cmd.Context(), req, func(resp api.GenerateResponse) error {content := resp.Response@@ -203,11 +204,9 @@ func generateImageWithOptions(cmd *cobra.Command, modelName, prompt string, keepreturn nil}- // Handle final response with image path- if resp.Done && strings.Contains(content, "Image saved to:") {- if idx := strings.Index(content, "Image saved to: "); idx >= 0 {- imagePath = strings.TrimSpace(content[idx+16:])- }+ // Handle final response with base64 image data+ if resp.Done && strings.HasPrefix(content, "IMAGE_BASE64:") {+ imageBase64 = content[13:]}return nil@@ -218,9 +217,27 @@ func generateImageWithOptions(cmd *cobra.Command, modelName, prompt string, keepreturn err}- if imagePath != "" {- displayImageInTerminal(imagePath)- fmt.Printf("Image saved to: %s\n", imagePath)+ if imageBase64 != "" {+ // Decode base64 and save to CWD+ imageData, err := base64.StdEncoding.DecodeString(imageBase64)+ if err != nil {+ return fmt.Errorf("failed to decode image: %w", err)+ }++ // Create filename from prompt+ safeName := sanitizeFilename(prompt)+ if len(safeName) > 50 {+ safeName = safeName[:50]+ }+ timestamp := time.Now().Format("20060102-150405")+ filename := fmt.Sprintf("%s-%s.png", safeName, timestamp)++ if err := os.WriteFile(filename, imageData, 0o644); err != nil {+ return fmt.Errorf("failed to save image: %w", err)+ }++ displayImageInTerminal(filename)+ fmt.Printf("Image saved to: %s\n", filename)}return nil@@ -306,7 +323,7 @@ func runInteractive(cmd *cobra.Command, modelName string, keepAlive *api.Duratiop.Add("", spinner)var stepBar *progress.StepBar- var imagePath string+ var imageBase64 stringerr = client.Generate(cmd.Context(), req, func(resp api.GenerateResponse) error {content := resp.Response@@ -326,11 +343,9 @@ func runInteractive(cmd *cobra.Command, modelName string, keepAlive *api.Duratioreturn nil}- // Handle final response with image path- if resp.Done && strings.Contains(content, "Image saved to:") {- if idx := strings.Index(content, "Image saved to: "); idx >= 0 {- imagePath = strings.TrimSpace(content[idx+16:])- }+ // Handle final response with base64 image data+ if resp.Done && strings.HasPrefix(content, "IMAGE_BASE64:") {+ imageBase64 = content[13:]}return nil@@ -342,25 +357,30 @@ func runInteractive(cmd *cobra.Command, modelName string, keepAlive *api.Duratiocontinue}- // Copy image to current directory with descriptive name- if imagePath != "" {+ // Save image to current directory with descriptive name+ if imageBase64 != "" {+ // Decode base64 image data+ imageData, err := base64.StdEncoding.DecodeString(imageBase64)+ if err != nil {+ fmt.Fprintf(os.Stderr, "Error decoding image: %v\n", err)+ continue+ }+// Create filename from prompt (sanitized)safeName := sanitizeFilename(line)if len(safeName) > 50 {safeName = safeName[:50]}timestamp := time.Now().Format("20060102-150405")- newName := fmt.Sprintf("%s-%s.png", safeName, timestamp)+ filename := fmt.Sprintf("%s-%s.png", safeName, timestamp)- // Copy file to CWD- if err := copyFile(imagePath, newName); err != nil {- fmt.Fprintf(os.Stderr, "Error saving to current directory: %v\n", err)- displayImageInTerminal(imagePath)- fmt.Printf("Image saved to: %s\n", imagePath)- } else {- displayImageInTerminal(newName)- fmt.Printf("Image saved to: %s\n", newName)+ if err := os.WriteFile(filename, imageData, 0o644); err != nil {+ fmt.Fprintf(os.Stderr, "Error saving image: %v\n", err)+ continue}++ displayImageInTerminal(filename)+ fmt.Printf("Image saved to: %s\n", filename)}fmt.Println()@@ -381,24 +401,6 @@ func sanitizeFilename(s string) string {return result.String()}-// copyFile copies a file from src to dst.-func copyFile(src, dst string) error {- sourceFile, err := os.Open(src)- if err != nil {- return err- }- defer sourceFile.Close()-- destFile, err := os.Create(dst)- if err != nil {- return err- }- defer destFile.Close()-- _, err = io.Copy(destFile, sourceFile)- return err-}-// printInteractiveHelp prints help for interactive mode commands.func printInteractiveHelp(opts ImageGenOptions) {fmt.Fprintln(os.Stderr, "Commands:")diff --git a/x/imagegen/client/create.go b/x/imagegen/client/create.goindex 7c9a23435..f4a3d50d9 100644--- a/x/imagegen/client/create.go+++ b/x/imagegen/client/create.go@@ -29,9 +29,10 @@ const MinOllamaVersion = "0.14.0"// CreateModel imports a tensor-based model from a local directory.// This creates blobs and manifest directly on disk, bypassing the HTTP API.+// If quantize is "fp8", weights will be quantized to mxfp8 format during import.//// TODO (jmorganca): Replace with API-based creation when promoted to production.-func CreateModel(modelName, modelDir string, p *progress.Progress) error {+func CreateModel(modelName, modelDir, quantize string, p *progress.Progress) error {if !imagegen.IsTensorModelDir(modelDir) {return fmt.Errorf("%s is not an image generation model directory (model_index.json not found)", modelDir)}@@ -58,18 +59,77 @@ func CreateModel(modelName, modelDir string, p *progress.Progress) error {// Create tensor layer callback for individual tensors// name is path-style: "component/tensor_name"- createTensorLayer := func(r io.Reader, name, dtype string, shape []int32) (imagegen.LayerInfo, error) {+ // When quantize is true, returns multiple layers (weight + scales)+ createTensorLayer := func(r io.Reader, name, dtype string, shape []int32, doQuantize bool) ([]imagegen.LayerInfo, error) {+ if doQuantize {+ // Check if quantization is supported+ if !QuantizeSupported() {+ return nil, fmt.Errorf("quantization requires MLX support")+ }++ // Quantize the tensor (affine mode returns weight, scales, qbiases)+ qweightData, scalesData, qbiasData, _, _, _, err := quantizeTensor(r, name, dtype, shape)+ if err != nil {+ return nil, fmt.Errorf("failed to quantize %s: %w", name, err)+ }++ // Create layer for quantized weight+ weightLayer, err := server.NewLayer(bytes.NewReader(qweightData), server.MediaTypeImageTensor)+ if err != nil {+ return nil, err+ }++ // Create layer for scales (use _scale suffix convention)+ scalesLayer, err := server.NewLayer(bytes.NewReader(scalesData), server.MediaTypeImageTensor)+ if err != nil {+ return nil, err+ }++ layers := []imagegen.LayerInfo{+ {+ Digest: weightLayer.Digest,+ Size: weightLayer.Size,+ MediaType: weightLayer.MediaType,+ Name: name, // Keep original name for weight+ },+ {+ Digest: scalesLayer.Digest,+ Size: scalesLayer.Size,+ MediaType: scalesLayer.MediaType,+ Name: name + "_scale", // Add _scale suffix+ },+ }++ // Add qbiases layer if present (affine mode)+ if qbiasData != nil {+ qbiasLayer, err := server.NewLayer(bytes.NewReader(qbiasData), server.MediaTypeImageTensor)+ if err != nil {+ return nil, err+ }+ layers = append(layers, imagegen.LayerInfo{+ Digest: qbiasLayer.Digest,+ Size: qbiasLayer.Size,+ MediaType: qbiasLayer.MediaType,+ Name: name + "_qbias", // Add _qbias suffix+ })+ }++ return layers, nil+ }++ // Non-quantized path: just create a single layerlayer, err := server.NewLayer(r, server.MediaTypeImageTensor)if err != nil {- return imagegen.LayerInfo{}, err+ return nil, err}- layer.Name = name- return imagegen.LayerInfo{- Digest: layer.Digest,- Size: layer.Size,- MediaType: layer.MediaType,- Name: name,+ return []imagegen.LayerInfo{+ {+ Digest: layer.Digest,+ Size: layer.Size,+ MediaType: layer.MediaType,+ Name: name,+ },}, nil}@@ -119,7 +179,7 @@ func CreateModel(modelName, modelDir string, p *progress.Progress) error {p.Add("imagegen", spinner)}- err := imagegen.CreateModel(modelName, modelDir, createLayer, createTensorLayer, writeManifest, progressFn)+ err := imagegen.CreateModel(modelName, modelDir, quantize, createLayer, createTensorLayer, writeManifest, progressFn)spinner.Stop()if err != nil {return errdiff --git a/x/imagegen/client/quantize.go b/x/imagegen/client/quantize.gonew file mode 100644index 000000000..569dc6baf--- /dev/null+++ b/x/imagegen/client/quantize.go@@ -0,0 +1,120 @@+//go:build mlx++package client++import (+ "fmt"+ "io"+ "os"+ "path/filepath"++ "github.com/ollama/ollama/x/imagegen/mlx"+)++// quantizeTensor loads a tensor from safetensors format, quantizes it to affine int8,+// and returns safetensors data for the quantized weights, scales, and biases.+// Uses MLX's native SaveSafetensors to ensure correct dtype handling (especially uint32 for quantized weights).+func quantizeTensor(r io.Reader, name, dtype string, shape []int32) (qweightData, scalesData, qbiasData []byte, qweightShape, scalesShape, qbiasShape []int32, err error) {+ tmpDir := ensureTempDir()++ // Read safetensors data to a temp file (LoadSafetensorsNative needs a path)+ tmpFile, err := os.CreateTemp(tmpDir, "quant-input-*.safetensors")+ if err != nil {+ return nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to create temp file: %w", err)+ }+ tmpPath := tmpFile.Name()+ defer os.Remove(tmpPath)++ if _, err := io.Copy(tmpFile, r); err != nil {+ tmpFile.Close()+ return nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to write temp file: %w", err)+ }+ tmpFile.Close()++ // Load the tensor using MLX's native loader+ st, err := mlx.LoadSafetensorsNative(tmpPath)+ if err != nil {+ return nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to load safetensors: %w", err)+ }+ defer st.Free()++ // Get the tensor (it's stored as "data" in our minimal safetensors format)+ arr := st.Get("data")+ if arr == nil {+ return nil, nil, nil, nil, nil, nil, fmt.Errorf("tensor 'data' not found in safetensors")+ }++ // Convert to BFloat16 if needed (quantize expects float type)+ if arr.Dtype() != mlx.DtypeBFloat16 && arr.Dtype() != mlx.DtypeFloat32 && arr.Dtype() != mlx.DtypeFloat16 {+ arr = mlx.AsType(arr, mlx.DtypeBFloat16)+ mlx.Eval(arr)+ }++ // Quantize with affine mode: group_size=32, bits=8+ // Note: mxfp8 mode doesn't have matmul kernels in MLX, affine mode does+ qweight, scales, qbiases := mlx.Quantize(arr, 32, 8, "affine")++ // Eval and make contiguous for data access+ qweight = mlx.Contiguous(qweight)+ scales = mlx.Contiguous(scales)+ if qbiases != nil {+ qbiases = mlx.Contiguous(qbiases)+ mlx.Eval(qweight, scales, qbiases)+ } else {+ mlx.Eval(qweight, scales)+ }++ // Get shapes+ qweightShape = qweight.Shape()+ scalesShape = scales.Shape()++ // Save quantized weight using MLX's native safetensors (correctly handles uint32 dtype)+ qweightPath := filepath.Join(tmpDir, "qweight.safetensors")+ defer os.Remove(qweightPath)+ if err := mlx.SaveSafetensors(qweightPath, map[string]*mlx.Array{"data": qweight}); err != nil {+ return nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to save quantized weight: %w", err)+ }+ qweightData, err = os.ReadFile(qweightPath)+ if err != nil {+ return nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to read quantized weight: %w", err)+ }++ // Save scales using MLX's native safetensors+ scalesPath := filepath.Join(tmpDir, "scales.safetensors")+ defer os.Remove(scalesPath)+ if err := mlx.SaveSafetensors(scalesPath, map[string]*mlx.Array{"data": scales}); err != nil {+ return nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to save scales: %w", err)+ }+ scalesData, err = os.ReadFile(scalesPath)+ if err != nil {+ return nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to read scales: %w", err)+ }++ // Affine mode returns qbiases for zero-point offset+ if qbiases != nil {+ qbiasShape = qbiases.Shape()+ qbiasPath := filepath.Join(tmpDir, "qbias.safetensors")+ defer os.Remove(qbiasPath)+ if err := mlx.SaveSafetensors(qbiasPath, map[string]*mlx.Array{"data": qbiases}); err != nil {+ return nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to save qbiases: %w", err)+ }+ qbiasData, err = os.ReadFile(qbiasPath)+ if err != nil {+ return nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to read qbiases: %w", err)+ }+ }++ return qweightData, scalesData, qbiasData, qweightShape, scalesShape, qbiasShape, nil+}++// QuantizeSupported returns true if quantization is supported (MLX build)+func QuantizeSupported() bool {+ return true+}++// ensureTempDir creates the temp directory for quantization if it doesn't exist+func ensureTempDir() string {+ tmpDir := filepath.Join(os.TempDir(), "ollama-quantize")+ os.MkdirAll(tmpDir, 0755)+ return tmpDir+}diff --git a/x/imagegen/client/quantize_stub.go b/x/imagegen/client/quantize_stub.gonew file mode 100644index 000000000..cb992dd48--- /dev/null+++ b/x/imagegen/client/quantize_stub.go@@ -0,0 +1,18 @@+//go:build !mlx++package client++import (+ "fmt"+ "io"+)++// quantizeTensor is not available without MLX+func quantizeTensor(r io.Reader, name, dtype string, shape []int32) (qweightData, scalesData, qbiasData []byte, qweightShape, scalesShape, qbiasShape []int32, err error) {+ return nil, nil, nil, nil, nil, nil, fmt.Errorf("quantization requires MLX support (build with mlx tag)")+}++// QuantizeSupported returns false when MLX is not available+func QuantizeSupported() bool {+ return false+}diff --git a/x/imagegen/cmd/engine/main.go b/x/imagegen/cmd/engine/main.goindex 4d80fc1d0..69ac8471d 100644--- a/x/imagegen/cmd/engine/main.go+++ b/x/imagegen/cmd/engine/main.go@@ -67,6 +67,9 @@ func main() {flag.Var(&inputImages, "input-image", "Input image for image editing (can be specified multiple times)")negativePrompt := flag.String("negative-prompt", "", "Negative prompt for CFG (empty = no CFG, matching Python)")cfgScale := flag.Float64("cfg-scale", 4.0, "CFG scale for image editing")+ teaCache := flag.Bool("teacache", false, "Enable TeaCache for faster inference")+ teaCacheThreshold := flag.Float64("teacache-threshold", 0.1, "TeaCache threshold (lower = more aggressive caching)")+ fusedQKV := flag.Bool("fused-qkv", false, "Enable fused QKV projection for faster attention")flag.Parse()@@ -99,13 +102,17 @@ func main() {}var img *mlx.Arrayimg, err = m.GenerateFromConfig(context.Background(), &zimage.GenerateConfig{- Prompt: *prompt,- Width: int32(*width),- Height: int32(*height),- Steps: *steps,- Seed: *seed,- CapturePath: *gpuCapture,- LayerCache: *layerCache,+ Prompt: *prompt,+ NegativePrompt: *negativePrompt,+ CFGScale: float32(*cfgScale),+ Width: int32(*width),+ Height: int32(*height),+ Steps: *steps,+ Seed: *seed,+ CapturePath: *gpuCapture,+ TeaCache: *teaCache,+ TeaCacheThreshold: float32(*teaCacheThreshold),+ FusedQKV: *fusedQKV,})if err == nil {err = saveImageArray(img, *out)diff --git a/x/imagegen/create.go b/x/imagegen/create.goindex 69d2846bb..c2e22d3df 100644--- a/x/imagegen/create.go+++ b/x/imagegen/create.go@@ -40,10 +40,12 @@ type ManifestWriter func(modelName string, config LayerInfo, layers []LayerInfo)// CreateModel imports an image generation model from a directory.// Stores each tensor as a separate blob for fine-grained deduplication.+// If quantize is "fp8", linear weights in transformer/text_encoder are quantized to mxfp8 format.// Layer creation and manifest writing are done via callbacks to avoid import cycles.-func CreateModel(modelName, modelDir string, createLayer LayerCreator, createTensorLayer TensorLayerCreator, writeManifest ManifestWriter, fn func(status string)) error {+func CreateModel(modelName, modelDir, quantize string, createLayer LayerCreator, createTensorLayer QuantizingTensorLayerCreator, writeManifest ManifestWriter, fn func(status string)) error {var layers []LayerInfovar configLayer LayerInfo+ var totalParams int64 // Count parameters from original tensor shapes// Components to process - extract individual tensors from eachcomponents := []string{"text_encoder", "transformer", "vae"}@@ -74,7 +76,11 @@ func CreateModel(modelName, modelDir string, createLayer LayerCreator, createTen}tensorNames := extractor.ListTensors()- fn(fmt.Sprintf("importing %s/%s (%d tensors)", component, entry.Name(), len(tensorNames)))+ quantizeMsg := ""+ if quantize == "fp8" && component != "vae" {+ quantizeMsg = ", quantizing to fp8"+ }+ fn(fmt.Sprintf("importing %s/%s (%d tensors%s)", component, entry.Name(), len(tensorNames), quantizeMsg))for _, tensorName := range tensorNames {td, err := extractor.GetTensor(tensorName)@@ -83,16 +89,30 @@ func CreateModel(modelName, modelDir string, createLayer LayerCreator, createTenreturn fmt.Errorf("failed to get tensor %s: %w", tensorName, err)}+ // Count parameters from original tensor shape+ if len(td.Shape) > 0 {+ numElements := int64(1)+ for _, dim := range td.Shape {+ numElements *= int64(dim)+ }+ totalParams += numElements+ }+// Store as minimal safetensors format (88 bytes header overhead)// This enables native mmap loading via mlx_load_safetensors// Use path-style name: "component/tensor_name"fullName := component + "/" + tensorName- layer, err := createTensorLayer(td.SafetensorsReader(), fullName, td.Dtype, td.Shape)++ // Determine if this tensor should be quantized+ doQuantize := quantize == "fp8" && ShouldQuantize(tensorName, component)++ // createTensorLayer returns multiple layers if quantizing (weight + scales)+ newLayers, err := createTensorLayer(td.SafetensorsReader(), fullName, td.Dtype, td.Shape, doQuantize)if err != nil {extractor.Close()return fmt.Errorf("failed to create layer for %s: %w", fullName, err)}- layers = append(layers, layer)+ layers = append(layers, newLayers...)}extractor.Close()@@ -122,7 +142,7 @@ func CreateModel(modelName, modelDir string, createLayer LayerCreator, createTenvar r io.Reader- // For model_index.json, normalize to Ollama format+ // For model_index.json, normalize to Ollama format and add metadataif cfgPath == "model_index.json" {data, err := os.ReadFile(fullPath)if err != nil {@@ -141,6 +161,16 @@ func CreateModel(modelName, modelDir string, createLayer LayerCreator, createTen}delete(cfg, "_diffusers_version")+ // Add parameter count (counted from tensor shapes during import)+ cfg["parameter_count"] = totalParams++ // Add quantization info+ if quantize == "fp8" {+ cfg["quantization"] = "FP8"+ } else {+ cfg["quantization"] = "BF16"+ }+data, err = json.MarshalIndent(cfg, "", " ")if err != nil {return fmt.Errorf("failed to marshal %s: %w", cfgPath, err)diff --git a/x/imagegen/image.go b/x/imagegen/image.goindex 6e9a84d90..214efcb74 100644--- a/x/imagegen/image.go+++ b/x/imagegen/image.go@@ -60,9 +60,12 @@ func ArrayToImage(arr *mlx.Array) (*image.RGBA, error) {}// Transform to [H, W, C] for image conversion- img := mlx.Squeeze(arr, 0)- img = mlx.Transpose(img, 1, 2, 0)- img = mlx.Contiguous(img)+ // Free intermediate arrays to avoid memory leak+ squeezed := mlx.Squeeze(arr, 0)+ transposed := mlx.Transpose(squeezed, 1, 2, 0)+ squeezed.Free()+ img := mlx.Contiguous(transposed)+ transposed.Free()mlx.Eval(img)imgShape := img.Shape()diff --git a/x/imagegen/mlx/mlx.go b/x/imagegen/mlx/mlx.goindex 6b141d8c4..9cb04e8f2 100644--- a/x/imagegen/mlx/mlx.go+++ b/x/imagegen/mlx/mlx.go@@ -607,6 +607,11 @@ func (a *Array) Valid() bool {return a != nil && a.c.ctx != nil}+// Kept returns true if the array is marked to survive Eval() cleanup.+func (a *Array) Kept() bool {+ return a != nil && a.kept+}+func int32ToCInt(s []int32) *C.int {if len(s) == 0 {return nil@@ -1480,6 +1485,44 @@ func (a *Array) ItemInt32() int32 {return int32(val)}+// Bytes copies the raw bytes out of the array without type conversion.+// Works with common dtypes (float32, int32, uint32, uint8).+// For non-contiguous arrays, call Contiguous() first.+// Note: Triggers cleanup of non-kept arrays.+func (a *Array) Bytes() []byte {+ cleanup()+ nbytes := a.Nbytes()+ if nbytes == 0 {+ return nil+ }++ // Get raw pointer based on dtype+ var ptr unsafe.Pointer+ switch a.Dtype() {+ case DtypeFloat32:+ ptr = unsafe.Pointer(C.mlx_array_data_float32(a.c))+ case DtypeInt32:+ ptr = unsafe.Pointer(C.mlx_array_data_int32(a.c))+ case DtypeUint32:+ ptr = unsafe.Pointer(C.mlx_array_data_uint32(a.c))+ case DtypeUint8:+ ptr = unsafe.Pointer(C.mlx_array_data_uint8(a.c))+ default:+ // For other types (bf16, f16, etc), convert to float32+ arr := AsType(a, DtypeFloat32)+ arr.Eval()+ ptr = unsafe.Pointer(C.mlx_array_data_float32(arr.c))+ nbytes = arr.Nbytes()+ }++ if ptr == nil {+ return nil+ }+ data := make([]byte, nbytes)+ copy(data, unsafe.Slice((*byte)(ptr), nbytes))+ return data+}+// ============ Utility ============// String returns a string representation@@ -1658,6 +1701,34 @@ func (s *SafetensorsFile) Free() {C.mlx_map_string_to_string_free(s.metadata)}+// SaveSafetensors saves arrays to a safetensors file using MLX's native implementation.+// This correctly handles all dtypes including uint32 for quantized weights.+func SaveSafetensors(path string, arrays map[string]*Array) error {+ cPath := C.CString(path)+ defer C.free(unsafe.Pointer(cPath))++ // Create the map+ cArrays := C.mlx_map_string_to_array_new()+ defer C.mlx_map_string_to_array_free(cArrays)++ // Add each array to the map+ for name, arr := range arrays {+ cName := C.CString(name)+ C.mlx_map_string_to_array_insert(cArrays, cName, arr.c)+ C.free(unsafe.Pointer(cName))+ }++ // Create empty metadata (optional)+ cMeta := C.mlx_map_string_to_string_new()+ defer C.mlx_map_string_to_string_free(cMeta)++ // Save+ if C.mlx_save_safetensors(cPath, cArrays, cMeta) != 0 {+ return fmt.Errorf("failed to save safetensors: %s", path)+ }+ return nil+}+// ============ NPY Loading ============// LoadNpy loads a numpy array from an npy file@@ -1986,7 +2057,8 @@ func GatherQMM(x, w, scales *Array, biases, lhsIndices, rhsIndices *Array, trans// Returns (quantized_weights, scales, biases).// groupSize: number of elements quantized together (default 64)// bits: bits per element, 2, 4, or 8 (default 4)-// mode: "affine" (default) or "mxfp4"+// mode: "affine" (default), "mxfp4", or "mxfp8"+// Note: mxfp8 mode returns nil biases (only weights and scales)func Quantize(w *Array, groupSize, bits int, mode string) (weights, scales, biases *Array) {cMode := C.CString(mode)defer C.free(unsafe.Pointer(cMode))@@ -1995,14 +2067,21 @@ func Quantize(w *Array, groupSize, bits int, mode string) (weights, scales, biasres := C.mlx_vector_array_new()C.mlx_quantize(&res, w.c, optGroupSize, optBits, cMode, C.default_stream())- // Result is a vector of 3 arrays: [weights, scales, biases]+ // Result is a vector of arrays: [weights, scales, biases?]+ // mxfp8 mode returns only 2 elements (no biases)+ vecSize := int(C.mlx_vector_array_size(res))var w0, w1, w2 C.mlx_arrayC.mlx_vector_array_get(&w0, res, 0)C.mlx_vector_array_get(&w1, res, 1)- C.mlx_vector_array_get(&w2, res, 2)+ if vecSize >= 3 {+ C.mlx_vector_array_get(&w2, res, 2)+ }C.mlx_vector_array_free(res)- return newArray(w0), newArray(w1), newArray(w2)+ if vecSize >= 3 {+ return newArray(w0), newArray(w1), newArray(w2)+ }+ return newArray(w0), newArray(w1), nil}// Dequantize reconstructs weights from quantized form.diff --git a/x/imagegen/models/qwen_image/qwen_image.go b/x/imagegen/models/qwen_image/qwen_image.goindex 97dbb089e..a061c3a0a 100644--- a/x/imagegen/models/qwen_image/qwen_image.go+++ b/x/imagegen/models/qwen_image/qwen_image.go@@ -222,6 +222,14 @@ func (m *Model) generate(cfg *GenerateConfig) (*mlx.Array, error) {mlx.Keep(posEmb, negEmb)}+ // Pre-compute batched embeddings for CFG (single forward pass optimization)+ var batchedEmb *mlx.Array+ if useCFG {+ batchedEmb = mlx.Concatenate([]*mlx.Array{posEmb, negEmb}, 0)+ mlx.Keep(batchedEmb)+ mlx.Eval(batchedEmb)+ }+// Schedulerscheduler := NewFlowMatchScheduler(DefaultSchedulerConfig())scheduler.SetTimesteps(cfg.Steps, imgSeqLen)@@ -264,10 +272,19 @@ func (m *Model) generate(cfg *GenerateConfig) (*mlx.Array, error) {var output *mlx.Arrayif useCFG {- // True CFG: run twice and combine with norm rescaling+ // CFG Batching: single forward pass with batch=2// Note: layer caching with CFG is not supported yet (would need 2 caches)- posOutput := m.Transformer.Forward(patches, posEmb, timestep, ropeCache.ImgFreqs, ropeCache.TxtFreqs)- negOutput := m.Transformer.Forward(patches, negEmb, timestep, ropeCache.ImgFreqs, ropeCache.TxtFreqs)+ batchedPatches := mlx.Tile(patches, []int32{2, 1, 1})+ batchedTimestep := mlx.Tile(timestep, []int32{2})++ // Single batched forward pass+ batchedOutput := m.Transformer.Forward(batchedPatches, batchedEmb, batchedTimestep, ropeCache.ImgFreqs, ropeCache.TxtFreqs)++ // Split output: [2, L, D] -> pos [1, L, D], neg [1, L, D]+ L := batchedOutput.Shape()[1]+ D := batchedOutput.Shape()[2]+ posOutput := mlx.Slice(batchedOutput, []int32{0, 0, 0}, []int32{1, L, D})+ negOutput := mlx.Slice(batchedOutput, []int32{1, 0, 0}, []int32{2, L, D})diff := mlx.Sub(posOutput, negOutput)scaledDiff := mlx.MulScalar(diff, cfg.CFGScale)@@ -305,6 +322,9 @@ func (m *Model) generate(cfg *GenerateConfig) (*mlx.Array, error) {if negEmb != nil {negEmb.Free()}+ if batchedEmb != nil {+ batchedEmb.Free()+ }ropeCache.ImgFreqs.Free()ropeCache.TxtFreqs.Free()if stepCache != nil {diff --git a/x/imagegen/models/qwen_image_edit/qwen_image_edit.go b/x/imagegen/models/qwen_image_edit/qwen_image_edit.goindex 991205c96..67eb1bcf7 100644--- a/x/imagegen/models/qwen_image_edit/qwen_image_edit.go+++ b/x/imagegen/models/qwen_image_edit/qwen_image_edit.go@@ -241,6 +241,14 @@ func (m *Model) edit(inputImagePaths []string, cfg *GenerateConfig) (*mlx.Array,mlx.Eval(posEmb, negEmb)}+ // Pre-compute batched embeddings for CFG (single forward pass optimization)+ var batchedEmb *mlx.Array+ if useCFG {+ batchedEmb = mlx.Concatenate([]*mlx.Array{posEmb, negEmb}, 0)+ mlx.Keep(batchedEmb)+ mlx.Eval(batchedEmb)+ }+// Encode all input images to latents and concatenatefmt.Println("Encoding images to latents...")allImageLatentsPacked := make([]*mlx.Array, len(vaeImages))@@ -291,11 +299,18 @@ func (m *Model) edit(inputImagePaths []string, cfg *GenerateConfig) (*mlx.Array,var output *mlx.Arrayif useCFG {- posOutput := m.Transformer.Forward(latentInput, posEmb, timestep, ropeCache.ImgFreqs, ropeCache.TxtFreqs)- negOutput := m.Transformer.Forward(latentInput, negEmb, timestep, ropeCache.ImgFreqs, ropeCache.TxtFreqs)+ // CFG Batching: single forward pass with batch=2+ // Tile inputs: [1, L, D] -> [2, L, D]+ batchedLatentInput := mlx.Tile(latentInput, []int32{2, 1, 1})+ batchedTimestep := mlx.Tile(timestep, []int32{2})- posOutput = mlx.Slice(posOutput, []int32{0, 0, 0}, []int32{1, imgSeqLen, posOutput.Shape()[2]})- negOutput = mlx.Slice(negOutput, []int32{0, 0, 0}, []int32{1, imgSeqLen, negOutput.Shape()[2]})+ // Single batched forward pass+ batchedOutput := m.Transformer.Forward(batchedLatentInput, batchedEmb, batchedTimestep, ropeCache.ImgFreqs, ropeCache.TxtFreqs)++ // Split output: [2, L, D] -> pos [1, L, D], neg [1, L, D]+ D := batchedOutput.Shape()[2]+ posOutput := mlx.Slice(batchedOutput, []int32{0, 0, 0}, []int32{1, imgSeqLen, D})+ negOutput := mlx.Slice(batchedOutput, []int32{1, 0, 0}, []int32{2, imgSeqLen, D})output = applyCFGWithNormRescale(posOutput, negOutput, cfg.CFGScale)} else {@@ -317,6 +332,9 @@ func (m *Model) edit(inputImagePaths []string, cfg *GenerateConfig) (*mlx.Array,if negEmb != nil {negEmb.Free()}+ if batchedEmb != nil {+ batchedEmb.Free()+ }ropeCache.ImgFreqs.Free()ropeCache.TxtFreqs.Free()imageLatentsPacked.Free()diff --git a/x/imagegen/models/zimage/text_encoder.go b/x/imagegen/models/zimage/text_encoder.goindex 3f9a946c9..e591ea3f7 100644--- a/x/imagegen/models/zimage/text_encoder.go+++ b/x/imagegen/models/zimage/text_encoder.go@@ -28,12 +28,12 @@ type Qwen3Config struct {// Qwen3Attention implements Qwen3 attention with QK normstype Qwen3Attention struct {- QProj *nn.Linear `weight:"q_proj"`- KProj *nn.Linear `weight:"k_proj"`- VProj *nn.Linear `weight:"v_proj"`- OProj *nn.Linear `weight:"o_proj"`- QNorm *nn.RMSNorm `weight:"q_norm"`- KNorm *nn.RMSNorm `weight:"k_norm"`+ QProj nn.LinearLayer `weight:"q_proj"`+ KProj nn.LinearLayer `weight:"k_proj"`+ VProj nn.LinearLayer `weight:"v_proj"`+ OProj nn.LinearLayer `weight:"o_proj"`+ QNorm *nn.RMSNorm `weight:"q_norm"`+ KNorm *nn.RMSNorm `weight:"k_norm"`// Computed fieldsNHeads int32NKVHeads int32@@ -136,9 +136,9 @@ func repeatKV(x *mlx.Array, repeats int32) *mlx.Array {// Qwen3MLP implements Qwen3 SwiGLU MLPtype Qwen3MLP struct {- GateProj *nn.Linear `weight:"gate_proj"`- UpProj *nn.Linear `weight:"up_proj"`- DownProj *nn.Linear `weight:"down_proj"`+ GateProj nn.LinearLayer `weight:"gate_proj"`+ UpProj nn.LinearLayer `weight:"up_proj"`+ DownProj nn.LinearLayer `weight:"down_proj"`}// Forward applies the MLPdiff --git a/x/imagegen/models/zimage/transformer.go b/x/imagegen/models/zimage/transformer.goindex ec93bf20c..4164fed8c 100644--- a/x/imagegen/models/zimage/transformer.go+++ b/x/imagegen/models/zimage/transformer.go@@ -36,8 +36,8 @@ type TransformerConfig struct {// TimestepEmbedder creates sinusoidal timestep embeddings// Output dimension is 256 (fixed), used for AdaLN modulationtype TimestepEmbedder struct {- Linear1 *nn.Linear `weight:"mlp.0"`- Linear2 *nn.Linear `weight:"mlp.2"`+ Linear1 nn.LinearLayer `weight:"mlp.0"`+ Linear2 nn.LinearLayer `weight:"mlp.2"`FreqEmbedSize int32 // 256 (computed)}@@ -74,7 +74,7 @@ func (te *TimestepEmbedder) Forward(t *mlx.Array) *mlx.Array {// XEmbedder embeds image patches to model dimensiontype XEmbedder struct {- Linear *nn.Linear `weight:"2-1"`+ Linear nn.LinearLayer `weight:"2-1"`}// Forward embeds patchified image latents@@ -86,7 +86,7 @@ func (xe *XEmbedder) Forward(x *mlx.Array) *mlx.Array {// CapEmbedder projects caption features to model dimensiontype CapEmbedder struct {Norm *nn.RMSNorm `weight:"0"`- Linear *nn.Linear `weight:"1"`+ Linear nn.LinearLayer `weight:"1"`PadToken *mlx.Array // loaded separately at root level}@@ -100,12 +100,13 @@ func (ce *CapEmbedder) Forward(capFeats *mlx.Array) *mlx.Array {// FeedForward implements SwiGLU FFNtype FeedForward struct {- W1 *nn.Linear `weight:"w1"` // gate projection- W2 *nn.Linear `weight:"w2"` // down projection- W3 *nn.Linear `weight:"w3"` // up projection+ W1 nn.LinearLayer `weight:"w1"` // gate projection+ W2 nn.LinearLayer `weight:"w2"` // down projection+ W3 nn.LinearLayer `weight:"w3"` // up projectionOutDim int32 // computed from W2}+// Forward applies SwiGLU: silu(W1(x)) * W3(x), then W2func (ff *FeedForward) Forward(x *mlx.Array) *mlx.Array {shape := x.Shape()@@ -115,6 +116,7 @@ func (ff *FeedForward) Forward(x *mlx.Array) *mlx.Array {// Reshape for matmulx = mlx.Reshape(x, B*L, D)+gate := ff.W1.Forward(x)gate = mlx.SiLU(gate)up := ff.W3.Forward(x)@@ -126,17 +128,69 @@ func (ff *FeedForward) Forward(x *mlx.Array) *mlx.Array {// Attention implements multi-head attention with QK normtype Attention struct {- ToQ *nn.Linear `weight:"to_q"`- ToK *nn.Linear `weight:"to_k"`- ToV *nn.Linear `weight:"to_v"`- ToOut *nn.Linear `weight:"to_out.0"`+ ToQ nn.LinearLayer `weight:"to_q"`+ ToK nn.LinearLayer `weight:"to_k"`+ ToV nn.LinearLayer `weight:"to_v"`+ ToOut nn.LinearLayer `weight:"to_out.0"`NormQ *mlx.Array `weight:"norm_q.weight"` // [head_dim] for per-head RMSNormNormK *mlx.Array `weight:"norm_k.weight"`- // Computed fields- NHeads int32- HeadDim int32- Dim int32- Scale float32+ // Fused QKV (computed at init time for efficiency, not loaded from weights)+ ToQKV nn.LinearLayer `weight:"-"` // Fused Q+K+V projection (created by FuseQKV)+ Fused bool `weight:"-"` // Whether to use fused QKV path+ // Computed fields (not loaded from weights)+ NHeads int32 `weight:"-"`+ HeadDim int32 `weight:"-"`+ Dim int32 `weight:"-"`+ Scale float32 `weight:"-"`+}++// FuseQKV creates a fused QKV projection by concatenating weights.+// This reduces 3 matmuls to 1 for a ~5-10% speedup.+// Note: Fusion is skipped for quantized weights as it would require complex+// dequant-concat-requant operations. The FP8 memory bandwidth savings outweigh+// the ~5% fusion benefit.+func (attn *Attention) FuseQKV() {+ if attn.ToQ == nil || attn.ToK == nil || attn.ToV == nil {+ return+ }++ // Skip fusion for quantized weights - type assert to check+ toQ, qOk := attn.ToQ.(*nn.Linear)+ toK, kOk := attn.ToK.(*nn.Linear)+ toV, vOk := attn.ToV.(*nn.Linear)+ if !qOk || !kOk || !vOk {+ // One or more are QuantizedLinear, skip fusion+ return+ }++ if toQ.Weight == nil || toK.Weight == nil || toV.Weight == nil {+ return+ }++ // Concatenate weights: [dim, dim] x 3 -> [3*dim, dim]+ // Weight shapes: ToQ.Weight [out_dim, in_dim], etc.+ qWeight := toQ.Weight+ kWeight := toK.Weight+ vWeight := toV.Weight++ // Concatenate along output dimension (axis 0)+ fusedWeight := mlx.Concatenate([]*mlx.Array{qWeight, kWeight, vWeight}, 0)++ // Evaluate fused weight to ensure it's materialized+ mlx.Eval(fusedWeight)++ // Create fused linear layer+ fusedLinear := &nn.Linear{Weight: fusedWeight}++ // Handle bias if present+ if toQ.Bias != nil && toK.Bias != nil && toV.Bias != nil {+ fusedBias := mlx.Concatenate([]*mlx.Array{toQ.Bias, toK.Bias, toV.Bias}, 0)+ mlx.Eval(fusedBias)+ fusedLinear.Bias = fusedBias+ }++ attn.ToQKV = fusedLinear+ attn.Fused = true}// Forward computes attention@@ -146,11 +200,24 @@ func (attn *Attention) Forward(x *mlx.Array, cos, sin *mlx.Array) *mlx.Array {L := shape[1]D := shape[2]- // Project Q, K, VxFlat := mlx.Reshape(x, B*L, D)- q := attn.ToQ.Forward(xFlat)- k := attn.ToK.Forward(xFlat)- v := attn.ToV.Forward(xFlat)++ var q, k, v *mlx.Array+ if attn.Fused && attn.ToQKV != nil {+ // Fused QKV path: single matmul then split+ qkv := attn.ToQKV.Forward(xFlat) // [B*L, 3*dim]++ // Split into Q, K, V along last dimension+ // Each has shape [B*L, dim]+ q = mlx.Slice(qkv, []int32{0, 0}, []int32{B * L, attn.Dim})+ k = mlx.Slice(qkv, []int32{0, attn.Dim}, []int32{B * L, 2 * attn.Dim})+ v = mlx.Slice(qkv, []int32{0, 2 * attn.Dim}, []int32{B * L, 3 * attn.Dim})+ } else {+ // Separate Q, K, V projections+ q = attn.ToQ.Forward(xFlat)+ k = attn.ToK.Forward(xFlat)+ v = attn.ToV.Forward(xFlat)+ }// Reshape to [B, L, nheads, head_dim]q = mlx.Reshape(q, B, L, attn.NHeads, attn.HeadDim)@@ -227,7 +294,7 @@ type TransformerBlock struct {AttentionNorm2 *nn.RMSNorm `weight:"attention_norm2"`FFNNorm1 *nn.RMSNorm `weight:"ffn_norm1"`FFNNorm2 *nn.RMSNorm `weight:"ffn_norm2"`- AdaLN *nn.Linear `weight:"adaLN_modulation.0,optional"` // only if modulation+ AdaLN nn.LinearLayer `weight:"adaLN_modulation.0,optional"` // only if modulation// Computed fieldsHasModulation boolDim int32@@ -281,8 +348,8 @@ func (tb *TransformerBlock) Forward(x *mlx.Array, adaln *mlx.Array, cos, sin *ml// FinalLayer outputs the denoised patchestype FinalLayer struct {- AdaLN *nn.Linear `weight:"adaLN_modulation.1"` // [256] -> [dim]- Output *nn.Linear `weight:"linear"` // [dim] -> [out_channels]+ AdaLN nn.LinearLayer `weight:"adaLN_modulation.1"` // [256] -> [dim]+ Output nn.LinearLayer `weight:"linear"` // [dim] -> [out_channels]OutDim int32 // computed from Output}@@ -350,12 +417,11 @@ func (m *Transformer) Load(manifest *imagegen.ModelManifest) error {m.ContextRefiners = make([]*TransformerBlock, cfg.NRefinerLayers)m.Layers = make([]*TransformerBlock, cfg.NLayers)- // Load weights from tensor blobs with BF16 conversionweights, err := imagegen.LoadWeightsFromManifest(manifest, "transformer")if err != nil {return fmt.Errorf("weights: %w", err)}- if err := weights.Load(mlx.DtypeBFloat16); err != nil {+ if err := weights.Load(0); err != nil {return fmt.Errorf("load weights: %w", err)}defer weights.ReleaseAll()@@ -377,7 +443,7 @@ func (m *Transformer) loadWeights(weights safetensors.WeightSource) error {func (m *Transformer) initComputedFields() {cfg := m.TransformerConfigm.TEmbed.FreqEmbedSize = 256- m.FinalLayer.OutDim = m.FinalLayer.Output.Weight.Shape()[0]+ m.FinalLayer.OutDim = m.FinalLayer.Output.OutputDim()m.CapEmbed.Norm.Eps = 1e-6for _, block := range m.NoiseRefiners {@@ -391,6 +457,20 @@ func (m *Transformer) initComputedFields() {}}+// FuseAllQKV fuses QKV projections in all attention layers for efficiency.+// This reduces 3 matmuls to 1 per attention layer, providing ~5-10% speedup.+func (m *Transformer) FuseAllQKV() {+ for _, block := range m.NoiseRefiners {+ block.Attention.FuseQKV()+ }+ for _, block := range m.ContextRefiners {+ block.Attention.FuseQKV()+ }+ for _, block := range m.Layers {+ block.Attention.FuseQKV()+ }+}+// initTransformerBlock sets computed fields on a transformer blockfunc initTransformerBlock(block *TransformerBlock, cfg *TransformerConfig) {block.Dim = cfg.Dim@@ -404,7 +484,7 @@ func initTransformerBlock(block *TransformerBlock, cfg *TransformerConfig) {attn.Scale = float32(1.0 / math.Sqrt(float64(attn.HeadDim)))// Init feedforward OutDim- block.FeedForward.OutDim = block.FeedForward.W2.Weight.Shape()[0]+ block.FeedForward.OutDim = block.FeedForward.W2.OutputDim()// Set eps on all RMSNorm layersblock.AttentionNorm1.Eps = cfg.NormEps@@ -423,6 +503,8 @@ type RoPECache struct {UnifiedSin *mlx.ArrayImgLen int32CapLen int32+ GridH int32 // Image token grid height+ GridW int32 // Image token grid width}// PrepareRoPECache precomputes RoPE values for the given image and caption lengths.@@ -456,6 +538,8 @@ func (m *Transformer) PrepareRoPECache(hTok, wTok, capLen int32) *RoPECache {UnifiedSin: unifiedSin,ImgLen: imgLen,CapLen: capLen,+ GridH: hTok,+ GridW: wTok,}}diff --git a/x/imagegen/models/zimage/vae.go b/x/imagegen/models/zimage/vae.goindex 09b0dfa91..b87524cc3 100644--- a/x/imagegen/models/zimage/vae.go+++ b/x/imagegen/models/zimage/vae.go@@ -104,6 +104,8 @@ func (gn *GroupNormLayer) forwardTiled(x *mlx.Array, B, H, W, C int32) *mlx.ArragroupSize := C / gn.NumGroups// Keep the input - we need it for slicing tiles later+ // Track if we were the ones who kept it, so we can restore state after+ wasKept := x.Kept()mlx.Keep(x)// Compute per-group mean and variance using flattened spatial dimensions@@ -205,6 +207,10 @@ func (gn *GroupNormLayer) forwardTiled(x *mlx.Array, B, H, W, C int32) *mlx.Arra}// Clean up kept arrays+ // Restore x's kept state - only free if we were the ones who kept it+ if !wasKept {+ x.Free()+ }mean.Free()invStd.Free()if weightGN != nil {@@ -734,18 +740,26 @@ func (vae *VAEDecoder) Decode(latents *mlx.Array) *mlx.Array {h := vae.ConvIn.Forward(z)mlx.Eval(h)+ prev := hh = vae.MidBlock.Forward(h)+ prev.Free()for _, upBlock := range vae.UpBlocks {+ prev = hh = upBlock.Forward(h)+ prev.Free()}- prev := h+ prev = hh = vae.ConvNormOut.Forward(h)mlx.Eval(h) // Eval after GroupNorm to avoid grid dimension issues+ prev.Free()++ prev = hh = mlx.SiLU(h)h = vae.ConvOut.Forward(h)mlx.Eval(h)+ prev.Free()// VAE outputs [-1, 1], convert to [0, 1]h = mlx.MulScalar(h, 0.5)@@ -754,7 +768,6 @@ func (vae *VAEDecoder) Decode(latents *mlx.Array) *mlx.Array {// Convert NHWC -> NCHW for outputh = mlx.Transpose(h, 0, 3, 1, 2)- prev.Free()mlx.Eval(h)return hdiff --git a/x/imagegen/models/zimage/zimage.go b/x/imagegen/models/zimage/zimage.goindex af4e70841..6fa5f483b 100644--- a/x/imagegen/models/zimage/zimage.go+++ b/x/imagegen/models/zimage/zimage.go@@ -26,10 +26,12 @@ type GenerateConfig struct {Progress ProgressFunc // Optional progress callbackCapturePath string // GPU capture path (debug)- // Layer caching options (speedup via shallow layer reuse)- LayerCache bool // Enable layer caching (default: false)- CacheInterval int // Refresh cache every N steps (default: 3)- CacheLayers int // Number of shallow layers to cache (default: 15)+ // TeaCache options (timestep embedding aware caching)+ TeaCache bool // TeaCache is always enabled for faster inference+ TeaCacheThreshold float32 // Threshold for cache reuse (default: 0.1, lower = more aggressive)++ // Fused QKV (fuse Q/K/V projections into single matmul)+ FusedQKV bool // Enable fused QKV projection (default: false)}// ProgressFunc is called during generation with step progress.@@ -42,6 +44,7 @@ type Model struct {TextEncoder *Qwen3TextEncoderTransformer *TransformerVAEDecoder *VAEDecoder+ qkvFused bool // Track if QKV has been fused (do only once)}// Load loads the Z-Image model from ollama blob storage.@@ -196,13 +199,17 @@ func (m *Model) generate(ctx context.Context, cfg *GenerateConfig) (*mlx.Array,if cfg.CFGScale <= 0 {cfg.CFGScale = 4.0}- if cfg.LayerCache {- if cfg.CacheInterval <= 0 {- cfg.CacheInterval = 3- }- if cfg.CacheLayers <= 0 {- cfg.CacheLayers = 15 // Half of 30 layers- }+ // TeaCache enabled by default+ cfg.TeaCache = true+ if cfg.TeaCacheThreshold <= 0 {+ cfg.TeaCacheThreshold = 0.15+ }++ // Enable fused QKV if requested (only fuse once)+ if cfg.FusedQKV && !m.qkvFused {+ m.Transformer.FuseAllQKV()+ m.qkvFused = true+ fmt.Println(" Fused QKV enabled")}useCFG := cfg.NegativePrompt != ""@@ -260,12 +267,54 @@ func (m *Model) generate(ctx context.Context, cfg *GenerateConfig) (*mlx.Array,mlx.Eval(ropeCache.UnifiedCos)}- // Step cache for shallow layer reuse (DeepCache/Learning-to-Cache style)- var stepCache *cache.StepCache- if cfg.LayerCache {- stepCache = cache.NewStepCache(cfg.CacheLayers)- fmt.Printf(" Layer caching enabled: %d layers, refresh every %d steps\n",- cfg.CacheLayers, cfg.CacheInterval)+ // Pre-compute batched embeddings for CFG (outside the loop for efficiency)+ var batchedEmb *mlx.Array+ if useCFG {+ // Concatenate embeddings once: [1, L, D] + [1, L, D] -> [2, L, D]+ batchedEmb = mlx.Concatenate([]*mlx.Array{posEmb, negEmb}, 0)+ mlx.Keep(batchedEmb)+ mlx.Eval(batchedEmb)+ }++ // TeaCache for timestep-aware caching+ // For CFG mode, we cache pos/neg separately, skip early steps, and always compute CFG fresh+ var teaCache *cache.TeaCache+ if cfg.TeaCache {+ skipEarly := 0+ if useCFG {+ skipEarly = 3 // Skip first 3 steps for CFG to preserve structure+ }+ teaCache = cache.NewTeaCache(&cache.TeaCacheConfig{+ Threshold: cfg.TeaCacheThreshold,+ RescaleFactor: 1.0,+ SkipEarlySteps: skipEarly,+ })+ if useCFG {+ fmt.Printf(" TeaCache enabled (CFG mode): threshold=%.2f, skip first %d steps\n", cfg.TeaCacheThreshold, skipEarly)+ } else {+ fmt.Printf(" TeaCache enabled: threshold=%.2f\n", cfg.TeaCacheThreshold)+ }+ }++ // cleanup frees all kept arrays when we need to abort early+ cleanup := func() {+ posEmb.Free()+ if negEmb != nil {+ negEmb.Free()+ }+ ropeCache.ImgCos.Free()+ ropeCache.ImgSin.Free()+ ropeCache.CapCos.Free()+ ropeCache.CapSin.Free()+ ropeCache.UnifiedCos.Free()+ ropeCache.UnifiedSin.Free()+ if batchedEmb != nil {+ batchedEmb.Free()+ }+ if teaCache != nil {+ teaCache.Free()+ }+ latents.Free()}// Denoising loop@@ -277,6 +326,7 @@ func (m *Model) generate(ctx context.Context, cfg *GenerateConfig) (*mlx.Array,if ctx != nil {select {case <-ctx.Done():+ cleanup()return nil, ctx.Err()default:}@@ -289,50 +339,77 @@ func (m *Model) generate(ctx context.Context, cfg *GenerateConfig) (*mlx.Array,}tCurr := scheduler.Timesteps[i]- timestep := mlx.ToBFloat16(mlx.NewArray([]float32{1.0 - tCurr}, []int32{1}))+ var noisePred *mlx.Array- patches := PatchifyLatents(latents, tcfg.PatchSize)+ // TeaCache: check if we should compute or reuse cached output+ shouldCompute := teaCache == nil || teaCache.ShouldCompute(i, tCurr)- var output *mlx.Array- if stepCache != nil {- // Use layer caching for faster inference- if useCFG {- posOutput := m.Transformer.ForwardWithCache(patches, timestep, posEmb, ropeCache,- stepCache, i, cfg.CacheInterval)- // Note: CFG with layer cache shares the cache between pos/neg- // This is approximate but fast - neg prompt uses same cached shallow layers- negOutput := m.Transformer.ForwardWithCache(patches, timestep, negEmb, ropeCache,- stepCache, i, cfg.CacheInterval)- diff := mlx.Sub(posOutput, negOutput)- scaledDiff := mlx.MulScalar(diff, cfg.CFGScale)- output = mlx.Add(negOutput, scaledDiff)- } else {- output = m.Transformer.ForwardWithCache(patches, timestep, posEmb, ropeCache,- stepCache, i, cfg.CacheInterval)- }- } else {- // Standard forward without caching+ if shouldCompute {+ timestep := mlx.ToBFloat16(mlx.NewArray([]float32{1.0 - tCurr}, []int32{1}))+ patches := PatchifyLatents(latents, tcfg.PatchSize)++ var output *mlx.Arrayif useCFG {- posOutput := m.Transformer.Forward(patches, timestep, posEmb, ropeCache)- negOutput := m.Transformer.Forward(patches, timestep, negEmb, ropeCache)- diff := mlx.Sub(posOutput, negOutput)+ // CFG Batching: single forward pass with batch=2+ // Tile patches: [1, L, D] -> [2, L, D]+ batchedPatches := mlx.Tile(patches, []int32{2, 1, 1})+ // Tile timestep: [1] -> [2]+ batchedTimestep := mlx.Tile(timestep, []int32{2})++ // Single batched forward pass (RoPE broadcasts from [1,L,H,D] to [2,L,H,D])+ batchedOutput := m.Transformer.Forward(batchedPatches, batchedTimestep, batchedEmb, ropeCache)++ // Split output: [2, L, D] -> pos [1, L, D], neg [1, L, D]+ outputShape := batchedOutput.Shape()+ L := outputShape[1]+ D := outputShape[2]+ posOutput := mlx.Slice(batchedOutput, []int32{0, 0, 0}, []int32{1, L, D})+ negOutput := mlx.Slice(batchedOutput, []int32{1, 0, 0}, []int32{2, L, D})++ // Convert to noise predictions (unpatchify and negate)+ posPred := UnpatchifyLatents(posOutput, tcfg.PatchSize, latentH, latentW, tcfg.InChannels)+ posPred = mlx.Neg(posPred)+ negPred := UnpatchifyLatents(negOutput, tcfg.PatchSize, latentH, latentW, tcfg.InChannels)+ negPred = mlx.Neg(negPred)++ // Cache pos/neg separately for TeaCache+ if teaCache != nil {+ teaCache.UpdateCFGCache(posPred, negPred, tCurr)+ mlx.Keep(teaCache.Arrays()...)+ }++ // Apply CFG: noisePred = neg + scale * (pos - neg)+ diff := mlx.Sub(posPred, negPred)scaledDiff := mlx.MulScalar(diff, cfg.CFGScale)- output = mlx.Add(negOutput, scaledDiff)+ noisePred = mlx.Add(negPred, scaledDiff)} else {+ // Non-CFG forward passoutput = m.Transformer.Forward(patches, timestep, posEmb, ropeCache)+ noisePred = UnpatchifyLatents(output, tcfg.PatchSize, latentH, latentW, tcfg.InChannels)+ noisePred = mlx.Neg(noisePred)++ // Update TeaCache+ if teaCache != nil {+ teaCache.UpdateCache(noisePred, tCurr)+ mlx.Keep(teaCache.Arrays()...)+ }}+ } else if useCFG && teaCache != nil && teaCache.HasCFGCache() {+ // CFG mode: get cached pos/neg and compute CFG fresh+ posPred, negPred := teaCache.GetCFGCached()+ diff := mlx.Sub(posPred, negPred)+ scaledDiff := mlx.MulScalar(diff, cfg.CFGScale)+ noisePred = mlx.Add(negPred, scaledDiff)+ fmt.Printf(" [TeaCache: reusing cached pos/neg outputs]\n")+ } else {+ // Non-CFG mode: reuse cached noise prediction+ noisePred = teaCache.GetCached()+ fmt.Printf(" [TeaCache: reusing cached output]\n")}- noisePred := UnpatchifyLatents(output, tcfg.PatchSize, latentH, latentW, tcfg.InChannels)- noisePred = mlx.Neg(noisePred)-oldLatents := latentslatents = scheduler.Step(noisePred, latents, i)- // Keep latents and any cached arrays- if stepCache != nil {- mlx.Keep(stepCache.Arrays()...)- }mlx.Eval(latents)oldLatents.Free()@@ -361,8 +438,14 @@ func (m *Model) generate(ctx context.Context, cfg *GenerateConfig) (*mlx.Array,ropeCache.CapSin.Free()ropeCache.UnifiedCos.Free()ropeCache.UnifiedSin.Free()- if stepCache != nil {- stepCache.Free()+ if batchedEmb != nil {+ batchedEmb.Free()+ }+ if teaCache != nil {+ hits, misses := teaCache.Stats()+ fmt.Printf(" TeaCache stats: %d hits, %d misses (%.1f%% cache rate)\n",+ hits, misses, float64(hits)/float64(hits+misses)*100)+ teaCache.Free()}// VAE decodediff --git a/x/imagegen/nn/nn.go b/x/imagegen/nn/nn.goindex c61e59939..65bf7fa22 100644--- a/x/imagegen/nn/nn.go+++ b/x/imagegen/nn/nn.go@@ -10,6 +10,13 @@ type Layer interface {Forward(x *mlx.Array) *mlx.Array}+// LinearLayer is an interface for linear layers (both regular and quantized).+// This allows swapping between Linear and QuantizedLinear at runtime.+type LinearLayer interface {+ Forward(x *mlx.Array) *mlx.Array+ OutputDim() int32 // Returns the output dimension of the layer+}+// Linear applies an affine transformation: y = x @ W.T + b// Weight is stored as [out_features, in_features], matching PyTorch/MLX convention.type Linear struct {@@ -49,6 +56,11 @@ func (l *Linear) Forward(x *mlx.Array) *mlx.Array {return mlx.Linear(x, w)}+// OutputDim returns the output dimension of the linear layer.+func (l *Linear) OutputDim() int32 {+ return l.Weight.Shape()[0]+}+// ToQuantized converts this Linear to a QuantizedLinear.func (l *Linear) ToQuantized(groupSize, bits int, mode string) *QuantizedLinear {qw, scales, qbiases := mlx.Quantize(l.Weight, groupSize, bits, mode)@@ -84,6 +96,13 @@ func (ql *QuantizedLinear) Forward(x *mlx.Array) *mlx.Array {return out}+// OutputDim returns the output dimension of the quantized linear layer.+// For mxfp8/mxfp4, quantized weight shape is [out_features, in_features / group_size].+// The output dimension is the first dimension of the weight.+func (ql *QuantizedLinear) OutputDim() int32 {+ return ql.Weight.Shape()[0]+}+// RMSNorm represents an RMS normalization layer.type RMSNorm struct {Weight *mlx.Array `weight:"weight"`diff --git a/x/imagegen/quantize.go b/x/imagegen/quantize.gonew file mode 100644index 000000000..09f815caf--- /dev/null+++ b/x/imagegen/quantize.go@@ -0,0 +1,22 @@+package imagegen++import (+ "io"+ "strings"+)++// QuantizingTensorLayerCreator creates tensor layers with optional quantization.+// When quantize is true, returns multiple layers (weight + scales + biases).+type QuantizingTensorLayerCreator func(r io.Reader, name, dtype string, shape []int32, quantize bool) ([]LayerInfo, error)++// ShouldQuantize returns true if a tensor should be quantized.+// Quantizes linear weights only, skipping VAE, embeddings, norms, and biases.+func ShouldQuantize(name, component string) bool {+ if component == "vae" {+ return false+ }+ if strings.Contains(name, "embed") || strings.Contains(name, "norm") {+ return false+ }+ return strings.HasSuffix(name, ".weight")+}diff --git a/x/imagegen/runner/runner.go b/x/imagegen/runner/runner.goindex fe7531fc1..d00748188 100644--- a/x/imagegen/runner/runner.go+++ b/x/imagegen/runner/runner.go@@ -13,7 +13,6 @@ import ("net/http""os""os/signal"- "path/filepath""sync""syscall""time"@@ -34,7 +33,8 @@ type Request struct {// Response is streamed back for each progress updatetype Response struct {- Content string `json:"content"`+ Content string `json:"content,omitempty"`+ Image string `json:"image,omitempty"` // Base64-encoded PNGDone bool `json:"done"`}@@ -191,10 +191,10 @@ func (s *Server) completionHandler(w http.ResponseWriter, r *http.Request) {return}- // Save image- outPath := filepath.Join(os.TempDir(), fmt.Sprintf("ollama-image-%d.png", time.Now().UnixNano()))- if err := imagegen.SaveImage(img, outPath); err != nil {- resp := Response{Content: fmt.Sprintf("error saving: %v", err), Done: true}+ // Encode image as base64 PNG+ imageData, err := imagegen.EncodeImageBase64(img)+ if err != nil {+ resp := Response{Content: fmt.Sprintf("error encoding: %v", err), Done: true}data, _ := json.Marshal(resp)w.Write(data)w.Write([]byte("\n"))@@ -204,11 +204,12 @@ func (s *Server) completionHandler(w http.ResponseWriter, r *http.Request) {// Free the generated image array and clean up MLX stateimg.Free()mlx.ClearCache()+ mlx.MetalResetPeakMemory()- // Send final response+ // Send final response with image dataresp := Response{- Content: fmt.Sprintf("\n\nImage saved to: %s\n", outPath),- Done: true,+ Image: imageData,+ Done: true,}data, _ := json.Marshal(resp)w.Write(data)diff --git a/x/imagegen/safetensors/loader.go b/x/imagegen/safetensors/loader.goindex 0353d04a7..31bb01e01 100644--- a/x/imagegen/safetensors/loader.go+++ b/x/imagegen/safetensors/loader.go@@ -8,6 +8,7 @@ import ("strings""github.com/ollama/ollama/x/imagegen/mlx"+ "github.com/ollama/ollama/x/imagegen/nn")// WeightSource is an interface for loading weights.@@ -102,6 +103,22 @@ func loadStruct(v reflect.Value, weights WeightSource, prefix string, errs *[]st}}+ // Handle nn.LinearLayer interface fields specially+ if field.Type == reflect.TypeOf((*nn.LinearLayer)(nil)).Elem() {+ if !hasTag {+ continue // no tag = skip+ }+ layer, err := LoadLinearLayer(weights, fullPath)+ if err != nil {+ if !optional {+ *errs = append(*errs, fullPath+": "+err.Error())+ }+ continue+ }+ fieldVal.Set(reflect.ValueOf(layer))+ continue+ }+// Handle by kindswitch fieldVal.Kind() {case reflect.Ptr:@@ -176,3 +193,64 @@ func joinPath(prefix, suffix string) string {}return prefix + "." + suffix}++// LoadLinearLayer loads a linear layer from weights, automatically detecting if it's quantized.+// If {path}.weight_scale exists, dequantizes the weights.+func LoadLinearLayer(weights WeightSource, path string) (nn.LinearLayer, error) {+ // Check if this is a quantized layer by looking for scale tensor+ scalePath := path + ".weight_scale"+ if weights.HasTensor(scalePath) {+ weight, err := weights.GetTensor(path + ".weight")+ if err != nil {+ return nil, fmt.Errorf("failed to load quantized weight %s: %w", path, err)+ }++ scales, err := weights.GetTensor(scalePath)+ if err != nil {+ return nil, fmt.Errorf("failed to load scales %s: %w", scalePath, err)+ }++ // Bias is optional+ var bias *mlx.Array+ biasPath := path + ".bias"+ if weights.HasTensor(biasPath) {+ bias, _ = weights.GetTensor(biasPath)+ }++ var qbiases *mlx.Array+ qbiasPath := path + ".weight_qbias"+ if weights.HasTensor(qbiasPath) {+ qbiases, _ = weights.GetTensor(qbiasPath)+ }++ if mlx.MetalIsAvailable() {+ return &nn.QuantizedLinear{+ Weight: weight,+ Scales: scales,+ QBiases: qbiases,+ Bias: bias,+ GroupSize: 32,+ Bits: 8,+ Mode: "affine",+ }, nil+ }++ dequantized := mlx.Dequantize(weight, scales, qbiases, 32, 8, "affine")+ return nn.NewLinear(dequantized, bias), nil+ }++ // Load as regular Linear+ weight, err := weights.GetTensor(path + ".weight")+ if err != nil {+ return nil, fmt.Errorf("failed to load weight %s: %w", path, err)+ }++ // Bias is optional+ var bias *mlx.Array+ biasPath := path + ".bias"+ if weights.HasTensor(biasPath) {+ bias, _ = weights.GetTensor(biasPath)+ }++ return nn.NewLinear(weight, bias), nil+}diff --git a/x/imagegen/server.go b/x/imagegen/server.goindex e84007a09..e96bdc08a 100644--- a/x/imagegen/server.go+++ b/x/imagegen/server.go@@ -46,7 +46,8 @@ type completionRequest struct {// completionResponse is received from the subprocesstype completionResponse struct {- Content string `json:"content"`+ Content string `json:"content,omitempty"`+ Image string `json:"image,omitempty"`Done bool `json:"done"`}@@ -250,15 +251,23 @@ func (s *Server) Completion(ctx context.Context, req llm.CompletionRequest, fn freturn fmt.Errorf("completion request failed: %d", resp.StatusCode)}- // Stream responses+ // Stream responses - use large buffer for base64 image datascanner := bufio.NewScanner(resp.Body)+ scanner.Buffer(make([]byte, 1024*1024), 16*1024*1024) // 16MB maxfor scanner.Scan() {var cresp completionResponseif err := json.Unmarshal(scanner.Bytes(), &cresp); err != nil {continue}++ content := cresp.Content+ // If this is the final response with an image, encode it in the content+ if cresp.Done && cresp.Image != "" {+ content = "IMAGE_BASE64:" + cresp.Image+ }+fn(llm.CompletionResponse{- Content: cresp.Content,+ Content: content,Done: cresp.Done,})if cresp.Done {