dit_block_transformer
dit_block_transformer
¶
'DiT Block' architecture inspired by the original implementation by Peebles and Xie, adapted for robotics in "The Ingredients for Robotic Diffusion Transformers" by Dasari et al.
The implementation by Dasari had several bugs/differences from the original DiT paper, which have been corrected here.
The architecture consists of an encoder-decoder transformer where the encoder processes pooled observation features, and the decoder generates action tokens conditioned on both the timestep embedding and the encoder output mean.
References
https://arxiv.org/html/2410.10088v1 https://github.com/SudeepDasari/dit-policy/blob/main/data4robotics/models/diffusion.py#L282 https://arxiv.org/abs/2212.09748 https://github.com/facebookresearch/DiT/blob/main/models.py
DiTBlock
¶
DiTBlock(number_of_encoder_layers, number_of_decoder_layers, embedding_dimension, number_of_heads, number_of_key_value_heads=None, feedforward_dimension=None, dropout=0.1, attention_dropout=0.0, activation=value, normalization_type=value, attention_type=value, positional_encoding_type=None, maximum_sequence_length=2048, maximum_decoder_length=256, timestep_embedding_dimension=256, bias=True, normalization_epsilon=1e-06, use_gating=True, initializer_range=0.02)
Bases: Module
DiT-Block paper network architecture.
The encoder processes input tokens bidirectionally and pools output to a single vector. The decoder generates output tokens conditioned on (timestep + pooled encoder output) via AdaLN.
Shape notation
B: batch size S: encoder sequence length T: decoder sequence length D: embedding dimension
Initialize the DiffusionTransformer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number_of_encoder_layers
|
int
|
Number of encoder layers. |
required |
number_of_decoder_layers
|
int
|
Number of decoder layers. |
required |
embedding_dimension
|
int
|
Hidden dimension of the transformer. |
required |
number_of_heads
|
int
|
Number of attention heads. |
required |
number_of_key_value_heads
|
int | None
|
Number of Key/Values heads (for Group Query Attention). |
None
|
feedforward_dimension
|
int | None
|
Feedforward network hidden dimension. |
None
|
dropout
|
float
|
Dropout rate. |
0.1
|
attention_dropout
|
float
|
Dropout rate for attention. |
0.0
|
activation
|
str
|
Activation function. |
value
|
normalization_type
|
str
|
Type of normalization. |
value
|
attention_type
|
str
|
Type of attention. |
value
|
positional_encoding_type
|
str | None
|
Type of positional encoding for encoder. |
None
|
maximum_sequence_length
|
int
|
Maximum encoder sequence length. |
2048
|
maximum_decoder_length
|
int
|
Maximum decoder sequence length. |
256
|
timestep_embedding_dimension
|
int
|
Dimension for timestep sinusoidal embedding. |
256
|
bias
|
bool
|
Whether to use bias in linear layers. |
True
|
normalization_epsilon
|
float
|
Epsilon for normalization layers. |
1e-06
|
use_gating
|
bool
|
Whether to use gating in decoder AdaNorm (often referred to as AdaLNZeroNorm). |
True
|
initializer_range
|
float
|
Standard deviation for weight initialization. |
0.02
|
Source code in src/versatil/models/layers/diffusion_transformer/dit_block_transformer.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
forward
¶
forward(decoder_hidden_states, timesteps, encoder_hidden_states, encoder_padding_mask=None, decoder_padding_mask=None, encoder_cache=None)
Forward pass through the transformer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decoder_hidden_states
|
Tensor
|
Decoder input tokens (batch size (B), decoder sequence length (T), embedding dimension (D)). |
required |
timesteps
|
Tensor
|
Diffusion timesteps (B,). |
required |
encoder_hidden_states
|
Tensor
|
Encoder input tokens (B, encoder sequence length (S), D). |
required |
encoder_padding_mask
|
Tensor | None
|
Padding mask for encoder (B, S). |
None
|
decoder_padding_mask
|
Tensor | None
|
Padding mask for decoder (B, T). |
None
|
encoder_cache
|
Tensor | None
|
Precomputed encoder output mean (B, D) for inference. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor, Tensor]
|
Tuple of (encoder_output_mean, decoder_output, conditioning): - encoder_output_mean: Mean of encoder outputs (B, D) for caching. - decoder_output: Decoder output tokens (B, T, D). - conditioning: Timestep plus encoder conditioning (B, D). |
Source code in src/versatil/models/layers/diffusion_transformer/dit_block_transformer.py
forward_features
¶
forward_features(decoder_hidden_states, timesteps, encoder_hidden_states, encoder_padding_mask=None, decoder_padding_mask=None, encoder_cache=None)
Return encoder cache, decoder hidden states, and conditioning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decoder_hidden_states
|
Tensor
|
Decoder input tokens (B, T, D). |
required |
timesteps
|
Tensor
|
Timesteps (B,). |
required |
encoder_hidden_states
|
Tensor
|
Encoder input tokens (B, S, D). |
required |
encoder_padding_mask
|
Tensor | None
|
Padding mask (B, S). |
None
|
decoder_padding_mask
|
Tensor | None
|
Padding mask (B, T). |
None
|
encoder_cache
|
Tensor | None
|
Precomputed encoder output mean (B, D). |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple of encoder output mean, decoder hidden states, and |
Tensor
|
conditioning with shapes |
Source code in src/versatil/models/layers/diffusion_transformer/dit_block_transformer.py
forward_encoder
¶
Encode input tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Input tokens (batch size (B), encoder sequence length (S), embedding dimension (D)). |
required |
padding_mask
|
Tensor | None
|
Padding mask (B, S) where True means masked. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Mean of encoder outputs (B, D) for conditioning the decoder. |
Source code in src/versatil/models/layers/diffusion_transformer/dit_block_transformer.py
forward_decoder
¶
Decode with timestep conditioning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Decoder input tokens (batch size (B), decoder sequence length (T), embedding dimension (D)). |
required |
timesteps
|
Tensor
|
Timesteps (B,). |
required |
encoder_output_mean
|
Tensor
|
Mean encoder output (B, D). |
required |
padding_mask
|
Tensor | None
|
Padding mask (B, T). |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Decoder output tokens and conditioning with shapes |
Tensor
|
and |
Source code in src/versatil/models/layers/diffusion_transformer/dit_block_transformer.py
forward_decoder_features
¶
Return decoder hidden states and combined conditioning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Decoder input tokens (B, T, D). |
required |
timesteps
|
Tensor
|
Timesteps (B,). |
required |
encoder_output_mean
|
Tensor
|
Mean encoder output (B, D). |
required |
padding_mask
|
Tensor | None
|
Padding mask (B, T). |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Decoder hidden states and conditioning, with shapes |
Tensor
|
and |