Skip to content

Add periodic MLX.eval() in denoise loops to reduce memory pressure - #2

Open
ronaldmannak wants to merge 1 commit into
mzbac:mainfrom
PicoMLX:pr2-periodic-eval
Open

Add periodic MLX.eval() in denoise loops to reduce memory pressure#2
ronaldmannak wants to merge 1 commit into
mzbac:mainfrom
PicoMLX:pr2-periodic-eval

Conversation

@ronaldmannak

Copy link
Copy Markdown
Contributor

Summary

  • Add MLX.eval() calls every 5 steps in the denoise loops of all three pipeline implementations
  • This periodically materializes lazy computation graphs, preventing excessive memory buildup during long denoising sequences
  • Particularly beneficial for generating images with higher step counts

Files changed

  • Sources/Flux2/Flux2Pipeline.swift
  • Sources/Flux2/Flux2DevPipeline.swift
  • Sources/Flux2/Flux2KleinPipeline.swift

Test plan

  • Verify project builds with swift build
  • Compare peak memory usage before and after with a high step count (e.g., 50 steps)
  • Verify generated image quality is unchanged

🤖 Generated with Claude Code

Add evalInterval parameter (default 5) to denoiseLoop() and the Klein
pipeline's denoise(). Every evalInterval steps, MLX.eval(current) is
called to force materialization and free intermediate compute graph
nodes, preventing unbounded memory growth during long denoising runs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mzbac

mzbac commented Feb 10, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@mzbac

mzbac commented Feb 11, 2026

Copy link
Copy Markdown
Owner

@codex review the evaluation in denoise loop base on https://github.com/awni/mlx-skills/blob/main/mlx_skills/skills/fast-mlx/references/fast-mlx-guide.md, I was under the impression the eval in denoise loop would cause some performance issue

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e4ad506be9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

guidance: MLXArray? = nil,
modelTimestepScale: Float = 0.001
modelTimestepScale: Float = 0.001,
evalInterval: Int = 5

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Make periodic eval opt-in in denoiseLoop defaults

Setting evalInterval to 5 by default means denoiseLoop now triggers MLX.eval periodically in its hot path for every caller that relies on defaults. Because MLX.eval forces materialization/synchronization, this introduces avoidable per-step sync overhead and can noticeably reduce throughput on memory-safe GPU runs; the periodic evaluation behavior should be opt-in rather than the default execution mode.

Useful? React with 👍 / 👎.

guidance: guidance,
modelTimestepScale: modelTimestepScale
modelTimestepScale: modelTimestepScale,
evalInterval: 5

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Expose eval interval in Flux2DevPipeline.generate

This hard-coded evalInterval: 5 makes periodic MLX.eval unavoidable for the dev pipeline path, but Flux2DevPipeline.generate does not expose any parameter to disable or tune it. As a result, users cannot recover prior throughput behavior even when memory pressure is not a concern, so this should be configurable (or left to the shared pipeline default) instead of fixed here.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants