ComfyUI Node

Flux2TTRTrainer

Distill Half of Flux's Attention Layers While You're Already Generating

By ttulttul·Created 7 months ago·Updated 7 months ago· 1
Flux2TTRTrainer
  • model
  • latents
  • conditioning
  • training_config
  • MODEL
  • loss_value
steps512
trainingtrue
training_preview_ttrtrue
checkpoint_path/tmp/ComfyUI/models/approximate_attention/flux2_ttr.pt
feature_dim256
query_chunk_size256
key_chunk_size1024
landmark_fraction0.080
landmark_min64
landmark_max0
text_tokens_guess77
alpha_init0.100
training_query_token_cap128
replay_buffer_size8
replay_offload_cputrue
replay_max_mb768
train_steps_per_call1
readiness_threshold0.1200
readiness_min_updates24
enable_memory_reservefalse
layer_start-1
layer_end-1
cfg_scale1.000
min_swap_layers1
max_swap_layers-1
inference_mixed_precisiontrue
controller_checkpoint_path

Flux2TTRTrainer is Phase 1 of the ComfyUI-Taylor-Attention project: it distills the fast "TTR" attention layers from native Flux attention, and it does it online, in the middle of normal sampling. You don't launch a separate training script. You build a normal-looking sampling graph with this node in the chain, and the training happens in the background of the images you're already generating. The catch: it's a research node, it's slow, and a useful run means leaving it on for a while.

Why this exists

Why the whole project exists: Flux's attention is quadratic in token count, which is why big resolutions choke consumer GPUs. This node trains a linear-time kernel-regression replacement for those layers - not a new checkpoint, not a LoRA, just swapped attention internals - and the pack's paper reports a few hours on a single RTX 4090 is enough to get roughly half of Flux's layers distillable.

How the distillation works

The mechanism is worth understanding because it's genuinely careful. The teacher is native Flux attention computed on real prompts and sampling states; the student is the TTR layer, which fuses a linear-time kernel branch with a small exact-softmax branch over "landmark" tokens, blended by a bounded weight. Samples pile into a replay buffer that keeps full keys and values but subsamples queries, so the student learns the same regression problem the teacher solved without the storage bill (CPU offload is on by default, budgeted to 768 MB). Per-layer readiness gating is fail-closed: a layer doesn't serve the student until it has accumulated enough updates and its EMA cosine distance to the teacher is low, with hysteresis so it doesn't flap at the boundary. Untrained layers silently fall back to teacher attention instead of wrecking your images. The loss is a Kendall-style uncertainty-weighted blend of huber and cosine, and the checkpoint persists per-layer optimizer state so you can resume across runs.

Inputs worth setting

Inputs worth setting on a first run:

  • model, latents, conditioning - the standard sampling inputs.
  • steps - default 512. This is your training budget per run.
  • checkpoint_path - defaults to ComfyUI/models/approximate_attention/flux2_ttr.pt.
  • feature_dim - default 256, must be a multiple of 256.

Everything else - chunk sizes, landmark fraction/min/max, replay knobs, readiness threshold, layer_start/layer_end - has sane defaults. training toggles distillation on; training_preview_ttr makes it output the student for visual comparison. Outputs are a patched MODEL (feed it to a KSampler) and loss_value.

Common issues

The honest gotchas: this is real training cost, not a slider trick. Keep an eye on VRAM - replay offloads to CPU by default, and enable_memory_reserve is off precisely because flipping it on can make ComfyUI evict memory aggressively. The pack requires a recent ComfyUI with the v3 node API and heavy Python deps. ## Installing

Install via ComfyUI Manager (search "Taylor-Attention") or:

cd ComfyUI/custom_nodes
git clone https://github.com/ttulttul/ComfyUI-Taylor-Attention
uv pip install -e custom_nodes/ComfyUI-Taylor-Attention

(README's one-liner says ComfyUI-Approximate-Attention - the old folder name; use the one you cloned.) No model files are downloaded; checkpoints land in ComfyUI/models/approximate_attention/. And don't expect tutorials - this pack has almost no community footprint. It's research code, well-engineered for what it is, and if you're not prepared to babysit a multi-hour distillation run, start with the controller article's opposite number and come back when you've got time.

Categoryadvanced/attention

Inputs (31)

NameTypeDefaultDescription
modelMODEL
latentsLATENT
conditioningCONDITIONING
stepsINT5120–200000
trainingBOOLEANtrueTrain TTR layers by distillation when enabled.
training_preview_ttrBOOLEANtrueWhen training, output TTR student attention for visual preview instead of teacher passthrough.
checkpoint_pathSTRING/tmp/ComfyUI/models/approximate_attention/flux2_ttr.ptCheckpoint file to load/save TTR layer weights (defaults to ComfyUI/models/approximate_attention/flux2_ttr.pt).
feature_dimINT256128–8192Kernel feature dimension (must be a multiple of 256).
query_chunk_sizeINT2561–4096Query chunk size for kernel attention evaluation.
key_chunk_sizeINT10241–8192Key chunk size for kernel KV/Ksum accumulation.
landmark_fractionFLOAT0.0800.01–0.5Fraction of image tokens used as landmarks for exact softmax residual.
landmark_minINT641–1024Minimum landmark count used at low resolution.
landmark_maxINT00–2048Maximum landmark count used at high resolution (0 means unlimited).
text_tokens_guessINT770–1024Assumed number of text tokens at the start of sequence for landmark selection.
alpha_initFLOAT0.1000–10Initial residual gate for landmark softmax branch.
training_query_token_capINT1281–4096Max number of query tokens per replay sample; keys/values always stay full length.
replay_buffer_sizeINT81–4096Replay buffer capacity per layer for distillation samples.
replay_offload_cpuBOOLEANtrueStore replay samples on CPU (reduced precision) to reduce VRAM pressure.
replay_max_mbINT76864–65536Global replay memory budget in MB across all layers.
train_steps_per_callINT11–32Number of replay optimization steps run per attention call.
readiness_thresholdFLOAT0.12000–10Enable student inference for a layer only when EMA loss is below this threshold.
readiness_min_updatesINT240–100000Minimum replay updates before a layer can be marked ready.
enable_memory_reserveBOOLEANfalseCall ComfyUI free_memory before HKR attention allocations (can offload aggressively).
layer_startINT-1-1–512Only apply TTR to single blocks with index >= layer_start (-1 disables).
layer_endINT-1-1–512Only apply TTR to single blocks with index <= layer_end (-1 disables).
cfg_scaleFLOAT1.0000–100Default CFG scale used when transformer_options does not provide guidance scale.
min_swap_layersINT10–512Training mode: minimum number of eligible single layers to swap to TTR per diffusion step.
max_swap_layersINT-1-1–512Training mode: maximum swapped layers per diffusion step (-1 means all eligible layers).
inference_mixed_precisionBOOLEANtrueUse input dtype (bf16/fp16) for TTR inference on CUDA for speed.
controller_checkpoint_pathSTRINGOptional Phase-2 controller checkpoint used for inference-time layer routing.
training_configoptTTR_TRAINING_CONFIG

Outputs (2)

NameTypeDescription
MODELMODEL
loss_valueFLOAT