Nodes/MD Nodes/MD: Apply TPG (Token Perturbation)
ComfyUI Node

MD: Apply TPG (Token Perturbation)

A ComfyUI node in MD_Nodes/Optimization with 10 inputs and 2 outputs.

By MDMAchine·Created about a year ago·Updated 2 months ago· 14
MD: Apply TPG (Token Perturbation)
  • model
  • patched_model
  • debug_info
enable_tpgtrue
target_layersDown (Encoder)
perturbation_strength1.00
start_sigma1000.0
end_sigma0.10
protect_first_tokens1
split_modeUncond First (Standard)
seed0
debug_modeOff
CategoryMD_Nodes/Optimization

Inputs (10)

NameTypeDefaultDescription
modelMODELMODEL INPUT • Purpose: The diffusion model to patch with TPG. • Compatibility: Works with most Unet/DiT architectures (SD1.5, SDXL, FLUX). • Output: Returns a patched model ready for sampling.
enable_tpgBOOLEANtrueENABLE TPG • Purpose: Master switch for the effect. • Use Case: Quickly toggle for A/B testing without rewiring nodes. ⭐ Recommended: True
target_layersCOMBODown (Encoder)TARGET LAYERS • Purpose: Select which U-Net blocks to patch. • Options: - Down (Encoder): Best FID scores (Paper recommendation) - Mid: Affects global structure/composition - Up (Decoder): Affects fine details and textures - All: Strongest effect, may be destructive ⭐ Recommended: Down (Encoder)
perturbation_strengthFLOAT1.000–1PERTURBATION STRENGTH • Purpose: Controls the blend between original and shuffled tokens. • Range: 0.0 (No effect) to 1.0 (Full Shuffle). • Trade-offs: Lower values are safer but less effective at guiding. ⭐ Recommended: 1.0 (Control intensity via Sigma instead)
start_sigmaFLOAT1000.00–10000START SIGMA • Purpose: Noise level to START applying TPG. • Note: High value (1000+) = Start immediately at beginning of generation. • Use Case: TPG is most effective at high noise levels (structural formation). ⭐ Recommended: 1000.0 (Always on at start)
end_sigmaFLOAT0.100–1000END SIGMA • Purpose: Noise level to STOP applying TPG. • Use Case: Stop before 0.1 to prevent damaging fine textures. • Range: 0.0 (End of gen) to 1000.0 (Start of gen). ⭐ Recommended: 0.1 - 0.5
protect_first_tokensINT10–16PROTECT TOKENS • Purpose: Prevent shuffling of initial special tokens (like [CLS]). • Range: 0-16 tokens. • Importance: Preserves global conditioning context. ⭐ Recommended: 1 (Protects [CLS])
split_modeCOMBOUncond First (Standard)SPLIT MODE HEURISTIC • Purpose: How the node detects the Unconditional batch. • Options: - Standard: Assumes Uncond batch comes first (ComfyUI default) - Inverted: Use if generation looks corrupted or inverted ⭐ Recommended: Uncond First (Standard)
seedINT00–9007199254740991PERMUTATION SEED • Purpose: Base seed for the token shuffle generator. • Note: Actual permutation changes every step/layer deterministically. • Range: 0 to 9,007,199,254,740,991 (JS-safe limit). ⭐ Most users: Leave random or fixed for reproducibility.
debug_modeCOMBOOffDEBUG MODE • Purpose: Console feedback level. • Options: - Off: Silent (Production) - Basic: Prints active layers/sigma info - Visual: Prints ASCII map of token shuffles (WARNING: Spammy!) ⭐ Recommended: Off

Outputs (2)

NameTypeDescription
patched_modelMODEL
debug_infoSTRING