Flux2TTRTrainer
Distill Half of Flux's Attention Layers While You're Already Generating
- model
- latents
- conditioning
- training_config
- MODEL
- loss_value
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.
Inputs (31)
| Name | Type | Default | Description |
|---|---|---|---|
| model | MODEL | — | |
| latents | LATENT | — | |
| conditioning | CONDITIONING | — | |
| steps | INT | 5120–200000 | — |
| training | BOOLEAN | true | Train TTR layers by distillation when enabled. |
| training_preview_ttr | BOOLEAN | true | When training, output TTR student attention for visual preview instead of teacher passthrough. |
| checkpoint_path | STRING | /tmp/ComfyUI/models/approximate_attention/flux2_ttr.pt | Checkpoint file to load/save TTR layer weights (defaults to ComfyUI/models/approximate_attention/flux2_ttr.pt). |
| feature_dim | INT | 256128–8192 | Kernel feature dimension (must be a multiple of 256). |
| query_chunk_size | INT | 2561–4096 | Query chunk size for kernel attention evaluation. |
| key_chunk_size | INT | 10241–8192 | Key chunk size for kernel KV/Ksum accumulation. |
| landmark_fraction | FLOAT | 0.0800.01–0.5 | Fraction of image tokens used as landmarks for exact softmax residual. |
| landmark_min | INT | 641–1024 | Minimum landmark count used at low resolution. |
| landmark_max | INT | 00–2048 | Maximum landmark count used at high resolution (0 means unlimited). |
| text_tokens_guess | INT | 770–1024 | Assumed number of text tokens at the start of sequence for landmark selection. |
| alpha_init | FLOAT | 0.1000–10 | Initial residual gate for landmark softmax branch. |
| training_query_token_cap | INT | 1281–4096 | Max number of query tokens per replay sample; keys/values always stay full length. |
| replay_buffer_size | INT | 81–4096 | Replay buffer capacity per layer for distillation samples. |
| replay_offload_cpu | BOOLEAN | true | Store replay samples on CPU (reduced precision) to reduce VRAM pressure. |
| replay_max_mb | INT | 76864–65536 | Global replay memory budget in MB across all layers. |
| train_steps_per_call | INT | 11–32 | Number of replay optimization steps run per attention call. |
| readiness_threshold | FLOAT | 0.12000–10 | Enable student inference for a layer only when EMA loss is below this threshold. |
| readiness_min_updates | INT | 240–100000 | Minimum replay updates before a layer can be marked ready. |
| enable_memory_reserve | BOOLEAN | false | Call ComfyUI free_memory before HKR attention allocations (can offload aggressively). |
| layer_start | INT | -1-1–512 | Only apply TTR to single blocks with index >= layer_start (-1 disables). |
| layer_end | INT | -1-1–512 | Only apply TTR to single blocks with index <= layer_end (-1 disables). |
| cfg_scale | FLOAT | 1.0000–100 | Default CFG scale used when transformer_options does not provide guidance scale. |
| min_swap_layers | INT | 10–512 | Training mode: minimum number of eligible single layers to swap to TTR per diffusion step. |
| max_swap_layers | INT | -1-1–512 | Training mode: maximum swapped layers per diffusion step (-1 means all eligible layers). |
| inference_mixed_precision | BOOLEAN | true | Use input dtype (bf16/fp16) for TTR inference on CUDA for speed. |
| controller_checkpoint_path | STRING | Optional Phase-2 controller checkpoint used for inference-time layer routing. | |
| training_configopt | TTR_TRAINING_CONFIG | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| MODEL | MODEL | — |
| loss_value | FLOAT | — |