[core] DyPE + SEGA hook for training-free high-resolution generation (upstream candidate) - #30
Draft
smellslikeml wants to merge 1 commit into
Draft
[core] DyPE + SEGA hook for training-free high-resolution generation (upstream candidate)#30smellslikeml wants to merge 1 commit into
smellslikeml wants to merge 1 commit into
Conversation
…th optional spectral attention) DyPE schedule adapted from https://github.com/guyyariv/DyPE (MIT). Spectral-attention mode 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>
smellslikeml
force-pushed
the
dype-upstream
branch
from
August 17, 2026 20:37
4eae61f to
9dc44b1
Compare
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.
Coordination: huggingface/diffusers#14520 — placement (core hook vs community pipeline) is being settled with maintainers before this is filed upstream.
What does this PR do?
Adds
apply_dype, a training-free hook that lets RoPE-based diffusion transformers such as [FluxTransformer2DModel] generate well above their trained resolution (e.g. 4096×4096 from a 1024×1024-trained model), with no fine-tuning and no extra sampling cost. Two modes:method="yarn") — a timestep-aware YaRN / NTK-by-parts RoPE schedule that engages only above the trained resolution and is otherwise a no-op.method="spectral") — DyPE positions plus a per-frequency, spectral-energy-guided attention temperature that removes the residual high-frequency speckle a scalar temperature leaves in flat regions at very high resolutions.The hook swaps the transformer's positional embedding and feeds it the normalized timestep — and, for SEGA, the latent's per-step spectral energy — through a native
register_forward_pre_hook, so it keeps working whenforwardis re-wrapped byenable_model_cpu_offload.Motivation
DyPE reaches 4K on Flux training-free, but its YaRN attention temperature is a single scalar applied to every RoPE frequency, which leaves a fine multicolored speckle in flat regions. Turning the scalar down removes the speckle but also flattens genuine detail — a scalar cannot separate noise from wanted high-frequency content. SEGA does the scaling per-frequency, guided by the image's own spectrum, so it suppresses the speckle while keeping detail.
Results — FLUX.1-Krea-dev, 4096², guidance 4.5, 28 steps
method="yarn") reproduces the reference implementation (guyyariv/DyPE, MIT) bit-for-bit: positional-embedding output matches toΔ = 0.method="spectral") drops flat-sky high-frequency energy (Laplacian variance) ~6× while keeping detail crisp:method="yarn")method="spectral")Full
method="spectral"frame — clean sky, crisp daisies and snow/ridge detail:Usage
Implementation
pos_embedis swapped for a_DyPEPosEmbed; a no-op at/below the trained resolution (1024² for Flux).img_ids→ axis/radial FFT energy profiles) each step. One 2D FFT per step for SEGA; negligible cost.method="yarn": YaRN / NTK-by-parts frequencies with DyPE timestep modulationκ = t².method="spectral": NTK frequencies + per-dim spectral mscalem_ref·(1 − α·σ·s_d)(κ=0.08, α=0.15, β=1.5), exposed asspectral_*kwargs.mugrows with the sequence length and collapses the sigma schedule; pinningbase_shift == max_shiftholdsmuconstant. This is a pipeline-level setting and is intentionally kept out of the hook's scope (see usage).Documentation & tests
docs/source/en/api/dype.md+_toctree.ymlentry ("Resolution extrapolation").tests/hooks/test_dype.py— schedule helpers, the no-op-at-trained-resolution guarantee, the DyPE timestep schedule, SEGA spectral allocation, and the hook lifecycle (swap → per-step feed → teardown).AI assistance & self-review notes
Written with Claude Code, reviewed with the repo
self-reviewrubric.np.floor/np.ceil→math) permodels.md'storch.compilerule; DyPE parity re-verified byte-identical afterward..item()device syncs in the pos-embed forward — data-dependent extrapolation-engagement control flow the stock embedding doesn't have; (b) tests use a hand-rolledDummyFluxLikeTransformerrather than a tiny realFluxTransformer2DModel(testing.mdprefers real classes); (c) the high-res shift is documented usage, not code — a pos-embed hook can't reach the pipeline schedule; (d) minor dead branches in_dype_rotary_pos_embedinherited from the generic rotary helper.Attribution
DyPE schedule from https://github.com/guyyariv/DyPE (MIT). SEGA spectral mscale adapted from https://github.com/wildminder/ComfyUI-DyPE (Apache-2.0); method from SEGA (arXiv:2605.22668).
Before submitting
self-reviewskill on the diff.docs/source/en/api/dype.md+ toctree).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