-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Add RAE Diffusion Transformer inference/preliminary training pipelines #13231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
plugyawn
wants to merge
13
commits into
huggingface:main
Choose a base branch
from
plugyawn:rae-dit-training
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,622
−1
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
828d9da
Preserve torch init return contract under no_init_weights
plugyawn df855b7
Add Stage-2 RAE DiT model, pipeline, and tooling
plugyawn d88d1a6
Fix RAE DiT review regressions
plugyawn 38826eb
Add RAE DiT resume-order verifier
plugyawn 5847b07
Add RAE DiT training smoke test
plugyawn 5ec84b0
Sync RAE DiT stack with diffusers quality checks
plugyawn 33c57ce
Add RAE DiT API docs
plugyawn 8b74498
Rename RAEDiTTransformer2DModel to RAEDiT2DModel
plugyawn dc437f9
Fix RAE DiT review regressions
plugyawn fe21820
Remove RAE DiT validation helper scripts from PR
plugyawn 92455c1
Add RAE DiT training validation sampling
plugyawn 794d350
Align RAE DiT with diffusers patterns
plugyawn fd0bf6c
Localize RAE loading and drop unused guidance transformer
plugyawn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <!-- Copyright 2026 The NYU Vision-X and HuggingFace Teams. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
| --> | ||
|
|
||
| # RAEDiT2DModel | ||
|
|
||
| The `RAEDiT2DModel` is the Stage-2 latent diffusion transformer introduced in | ||
| [Diffusion Transformers with Representation Autoencoders](https://huggingface.co/papers/2510.11690). | ||
|
|
||
| Unlike DiT models that operate on VAE latents, this transformer denoises the latent space learned by | ||
| [`AutoencoderRAE`](./autoencoder_rae). It is designed to be used with [`FlowMatchEulerDiscreteScheduler`] and | ||
| decoded back to RGB with [`AutoencoderRAE`]. | ||
|
|
||
| ## Loading a pretrained transformer | ||
|
|
||
| ```python | ||
| from diffusers import RAEDiT2DModel | ||
|
|
||
| transformer = RAEDiT2DModel.from_pretrained("path/to/converted-stage2-transformer") | ||
| ``` | ||
|
|
||
| ## RAEDiT2DModel | ||
|
|
||
| [[autodoc]] RAEDiT2DModel |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <!-- Copyright 2026 The NYU Vision-X and HuggingFace Teams. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
| --> | ||
|
|
||
| # RAE DiT | ||
|
|
||
| [Diffusion Transformers with Representation Autoencoders](https://huggingface.co/papers/2510.11690) introduces a | ||
| two-stage recipe: first train a representation autoencoder (RAE), then train a diffusion transformer on the resulting | ||
| latent space. | ||
|
|
||
| [`RAEDiTPipeline`] implements the Stage-2 class-conditional generator in Diffusers. It combines: | ||
|
|
||
| - [`RAEDiT2DModel`] for latent denoising | ||
| - [`FlowMatchEulerDiscreteScheduler`] for the denoising trajectory | ||
| - [`AutoencoderRAE`] for decoding latent samples to RGB images | ||
|
|
||
| > [!TIP] | ||
| > [`RAEDiTPipeline`] expects a Stage-2 checkpoint converted to Diffusers format together with a compatible | ||
| > [`AutoencoderRAE`] checkpoint. | ||
|
|
||
| ## Loading a converted pipeline | ||
|
|
||
| ```python | ||
| import torch | ||
| from diffusers import RAEDiTPipeline | ||
|
|
||
| pipe = RAEDiTPipeline.from_pretrained( | ||
| "path/to/converted-rae-dit-imagenet256", | ||
| torch_dtype=torch.bfloat16, | ||
| ).to("cuda") | ||
|
|
||
| image = pipe(class_labels=[207], num_inference_steps=25).images[0] | ||
| image.save("golden_retriever.png") | ||
| ``` | ||
|
|
||
| If the converted pipeline includes an `id2label` mapping, you can also look up class ids by name: | ||
|
|
||
| ```python | ||
| class_id = pipe.get_label_ids("golden retriever")[0] | ||
| image = pipe(class_labels=[class_id], num_inference_steps=25).images[0] | ||
| ``` | ||
|
|
||
| ## RAEDiTPipeline | ||
|
|
||
| [[autodoc]] RAEDiTPipeline | ||
| - all | ||
| - __call__ | ||
|
|
||
| ## ImagePipelineOutput | ||
|
|
||
| [[autodoc]] pipelines.ImagePipelineOutput |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Training RAEDiT Stage 2 | ||
|
|
||
| This folder contains the minimal Stage-2 follow-up for the RAE integration: training `RAEDiT2DModel` on top of a frozen `AutoencoderRAE`. | ||
|
|
||
| It is intentionally placed under `examples/research_projects/rae_dit/` rather than the top-level `examples/` trainers because this is still an experimental follow-up to the new RAE support. | ||
|
|
||
| ## What this mirrors | ||
|
|
||
| The scaffold is deliberately composed from existing `diffusers` patterns instead of introducing a new training style: | ||
|
|
||
| - `examples/research_projects/autoencoder_rae/train_autoencoder_rae.py` | ||
| for ImageFolder loading, RAE-specific preprocessing, and the experimental research-project placement. | ||
| - `examples/dreambooth/train_dreambooth_flux.py` | ||
| for the flow-matching training loop structure, checkpoint resume flow, and `accelerate.save_state(...)` hooks. | ||
| - `examples/flux-control/train_control_flux.py` | ||
| for the transformer-only save layout and SD3-style flow-matching timestep weighting helpers. | ||
|
|
||
| ## Current scope | ||
|
|
||
| This is a minimal full-finetuning scaffold, not a paper-complete training stack. It currently does the following: | ||
|
|
||
| - loads a frozen pretrained `AutoencoderRAE` | ||
| - encodes RGB images to normalized Stage-1 latents on the fly | ||
| - trains only the Stage-2 `RAEDiT2DModel` | ||
| - uses `FlowMatchEulerDiscreteScheduler` with the same shifted-sigma schedule shape already used elsewhere in `diffusers` | ||
| - consumes ImageFolder class ids as `class_labels` | ||
| - can generate validation samples through `RAEDiTPipeline` during training | ||
| - saves the trained transformer under `output_dir/transformer` | ||
| - saves the scheduler config under `output_dir/scheduler` | ||
| - writes `id2label.json` from the ImageFolder class mapping | ||
|
|
||
| It intentionally does not yet include: | ||
|
|
||
| - a latent-caching path | ||
| - autoguidance or the broader upstream transport stack | ||
| - exact upstream distributed training/runtime features | ||
|
|
||
| ## Dataset format | ||
|
|
||
| The script expects an `ImageFolder`-compatible dataset: | ||
|
|
||
| ```text | ||
| train_data_dir/ | ||
| n01440764/ | ||
| img_0001.jpeg | ||
| n01443537/ | ||
| img_0002.jpeg | ||
| ``` | ||
|
|
||
| The folder names define the class labels used during Stage-2 training. | ||
|
|
||
| ## Quickstart | ||
|
|
||
| ```bash | ||
| accelerate launch examples/research_projects/rae_dit/train_rae_dit.py \ | ||
| --pretrained_rae_model_name_or_path nyu-visionx/RAE-dinov2-wReg-base-ViTXL-n08 \ | ||
| --train_data_dir /path/to/imagenet_like_folder \ | ||
| --output_dir /tmp/rae-dit \ | ||
| --resolution 256 \ | ||
| --train_batch_size 8 \ | ||
| --gradient_accumulation_steps 1 \ | ||
| --gradient_checkpointing \ | ||
| --learning_rate 1e-4 \ | ||
| --lr_scheduler cosine \ | ||
| --lr_warmup_steps 1000 \ | ||
| --max_train_steps 200000 \ | ||
| --mixed_precision bf16 \ | ||
| --report_to wandb \ | ||
| --allow_tf32 | ||
| ``` | ||
|
|
||
| To emit validation samples during training, add: | ||
|
|
||
| ```bash | ||
| --validation_steps 1000 \ | ||
| --validation_class_label 207 \ | ||
| --num_validation_images 4 \ | ||
| --validation_num_inference_steps 25 \ | ||
| --validation_guidance_scale 1.0 | ||
| ``` | ||
|
|
||
| Validation images are written to `output_dir/validation/step-<global_step>/`. | ||
|
|
||
| If you already have a converted or partially trained Stage-2 checkpoint, resume from it with: | ||
|
|
||
| ```bash | ||
| accelerate launch examples/research_projects/rae_dit/train_rae_dit.py \ | ||
| --pretrained_rae_model_name_or_path nyu-visionx/RAE-dinov2-wReg-base-ViTXL-n08 \ | ||
| --pretrained_transformer_model_name_or_path /path/to/previous/transformer \ | ||
| --train_data_dir /path/to/imagenet_like_folder \ | ||
| --output_dir /tmp/rae-dit-finetune \ | ||
| --resolution 256 \ | ||
| --train_batch_size 8 \ | ||
| --max_train_steps 50000 | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - The script derives a default flow shift from the latent dimensionality as `sqrt(latent_dim / time_shift_base)`, matching the upstream Stage-2 heuristic at a high level. | ||
| - The trainer assumes the selected `AutoencoderRAE` uses `reshape_to_2d=True`, because `RAEDiT2DModel` operates on 2D latent feature maps. | ||
| - Validation sampling uses a fresh scheduler cloned from the training config so sampling does not mutate the in-flight training scheduler state. | ||
| - This example is meant to land first as a training scaffold that matches the new Stage-2 model and export layout. A later follow-up can add cached latents and other training conveniences. | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't belong here.