[core] Add SEGA spectral attention mode to the DyPE hook - #29
Draft
smellslikeml wants to merge 3 commits into
Draft
[core] Add SEGA spectral attention mode to the DyPE hook#29smellslikeml wants to merge 3 commits into
smellslikeml wants to merge 3 commits into
Conversation
Adapted from https://github.com/wildminder/ComfyUI-DyPE (Apache-2.0); method from SEGA (arXiv:2605.22668). Co-Authored-By: remyx-ai[bot] <289541483+remyx-ai[bot]@users.noreply.github.com> Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: remyx-ai[bot] <289541483+remyx-ai[bot]@users.noreply.github.com> Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Owner
Author
Self-review notesRan the repo Fixed before submitting
Left for the reviewer (flagged, not guessed at)
Advisory (dead code, inherited from the generic rotary signature)
Verdict after the fixes: ready apart from the flagged items above, which I'd rather raise than guess at. |
8 tasks
…sh with Semantic Guidance 'SEGA') Co-Authored-By: remyx-ai[bot] <289541483+remyx-ai[bot]@users.noreply.github.com> Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Adds an optional SEGA (Spectral-Energy Guided Attention) mode to the DyPE hook. It removes the high-frequency speckle that training-free resolution extrapolation leaves in flat regions at 4K, while preserving fine detail.
apply_dype(pipe.transformer, method="spectral")keeps DyPE's NTK positions but replaces the single YaRN attention-temperature scalar with a per-RoPE-dimension, content-aware temperature derived from the latent's spectral energy at each denoising step — sharpening low-energy frequency bands and damping high-energy ones.method="yarn"(plain DyPE) remains the default and is unchanged.Stacked on #28 (DyPE); this PR is the
method="spectral"delta.Motivation
DyPE's YaRN temperature is a single scalar (
0.1·ln(scale)+1) applied to every RoPE frequency, and it is the source of the residual speckle: dialing it down removes the speckle but also flattens genuine detail — a scalar cannot tell noise from wanted high-frequency content. SEGA does the scaling per-frequency, guided by the image's own spectrum.Results — FLUX.1-Krea-dev, 4096², guidance 4.5, 28 steps
Flat-sky high-frequency energy (Laplacian variance) drops ~6× with SEGA, with detail retained:
method="yarn")method="spectral")Full
method="spectral"frame — clean sky, crisp daisies and snow/ridge detail, coherent composition:(SEGA modulates attention per step, so at a fixed seed it yields a different — still coherent — composition than plain DyPE. It is an alternate mode, not a reproduction of the
yarnimage.)Usage
Implementation
method="spectral": NTK-scaled RoPE frequencies + per-dim spectral mscalem_ref·(1 − α·σ·s_d)(κ=0.08, α=0.15, β=1.5), exposed asspectral_*kwargs.img_idseach step, computes axis + radial FFT energy profiles, and feeds them viaset_spectral_data. One 2D FFT per step; offload-robust (same plumbing as the timestep feed).method="yarn"is byte-identical (parity preserved); SEGA is a no-op at/below the trained resolution.AI assistance & self-review notes
Written with Claude Code. Self-review of the diff:
yarnpath unchanged — verified byte-identical positional-embedding output (Δ = 0) vs the base branch.spectral_alpha; (d)deinitalize_hookintentionally matches diffusers' framework spelling (commented inline) — renaming would stop the override from being called on removal.Attribution
SEGA spectral mscale adapted from https://github.com/wildminder/ComfyUI-DyPE (Apache-2.0); method from SEGA (arXiv:2605.22668). DyPE schedule from https://github.com/guyyariv/DyPE (MIT).
Before submitting
self-reviewskill on the diff — one blocking finding (NumPy in the forward path) fixed; full notes in a comment below.docs/source/en/api/dype.md+_toctree.ymlentry.tests/hooks/test_dype.py).Who can review?
General functionalities / hooks: yiyixuxu, DN6, sayakpaul (live @-tags to be added when filed against
huggingface/diffusers).🤖 Generated with Claude Code