flux_2 ¶
Classes¶
fastvideo.models.dits.flux_2.Flux2Attention ¶
Flux2Attention(query_dim: int, num_heads: int = 8, dim_head: int = 64, dropout: float = 0.0, bias: bool = False, added_kv_proj_dim: Optional[int] = None, added_proj_bias: Optional[bool] = True, out_bias: bool = True, eps: float = 1e-06, out_dim: int = None, elementwise_affine: bool = True, supported_attention_backends: Optional[Tuple[AttentionBackendEnum, ...]] = None)
Bases: Module, AttentionModuleMixin
Joint attention for Flux2 double-stream blocks (image + text).
Context (text) branch uses add_q_proj / add_k_proj / add_v_proj on encoder_hidden_states, QK norm via norm_added_*, concatenation text then image on sequence dim, RoPE on the full sequence, attention, then to_add_out on the text slice only. Diffusers does the same layout in Flux2AttnProcessor with nn.Linear; here those linears are ColumnParallelLinear (F.linear at tp_size==1). For parity debugging, see compare_flux2_context_branch_double0.py.
Source code in fastvideo/models/dits/flux_2.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
fastvideo.models.dits.flux_2.Flux2FeedForward ¶
Flux2FeedForward(dim: int, dim_out: Optional[int] = None, mult: float = 3.0, inner_dim: Optional[int] = None, bias: bool = False, swiglu_fp32: bool = False)
Bases: Module
FF sub-block: SwiGLU + two linears.
At tp_world_size == 1, use nn.Linear to match Diffusers numerics and checkpoint layout; under tensor parallel, use ColumnParallelLinear.
Source code in fastvideo/models/dits/flux_2.py
fastvideo.models.dits.flux_2.Flux2ParallelSelfAttention ¶
Flux2ParallelSelfAttention(query_dim: int, num_heads: int = 8, dim_head: int = 64, dropout: float = 0.0, bias: bool = False, out_bias: bool = True, eps: float = 1e-06, out_dim: int = None, elementwise_affine: bool = True, mlp_ratio: float = 4.0, mlp_mult_factor: int = 2, supported_attention_backends: Optional[Tuple[AttentionBackendEnum, ...]] = None)
Bases: Module, AttentionModuleMixin
Flux 2 parallel self-attention for the Flux 2 single-stream transformer blocks.
This implements a parallel transformer block, where the attention QKV projections are fused to the feedforward (FF) input projections, and the attention output projections are fused to the FF output projections. See the ViT-22B paper for a visual depiction of this type of transformer block.
Source code in fastvideo/models/dits/flux_2.py
fastvideo.models.dits.flux_2.Flux2PosEmbed ¶
Bases: Module
Flux2 N-D RoPE: matches Diffusers Flux2PosEmbed (transformer_flux2).
Source code in fastvideo/models/dits/flux_2.py
Methods:¶
fastvideo.models.dits.flux_2.Flux2PosEmbed.forward ¶
forward(ids: Tensor) -> tuple[Tensor, Tensor]
Per Diffusers: loop axes, 1D RoPE each, concat on channel dim.
Source code in fastvideo/models/dits/flux_2.py
fastvideo.models.dits.flux_2.Flux2SwiGLU ¶
Flux2SwiGLU(compute_in_fp32: bool = False)
Bases: Module
Flux 2 uses a SwiGLU-style activation in the transformer feedforward sub-blocks, but with the linear projection layer fused into the first linear layer of the FF sub-block. Thus, this module has no trainable parameters.
Source code in fastvideo/models/dits/flux_2.py
fastvideo.models.dits.flux_2.Flux2Transformer2DModel ¶
Flux2Transformer2DModel(config: Flux2Config, hf_config: dict[str, Any])
Bases: BaseDiT
The Transformer model introduced in Flux 2.
Reference: https://blackforestlabs.ai/announcing-black-forest-labs/
Source code in fastvideo/models/dits/flux_2.py
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 | |
Methods:¶
fastvideo.models.dits.flux_2.Flux2Transformer2DModel.forward ¶
forward(hidden_states: Tensor, encoder_hidden_states: Tensor = None, timestep: LongTensor = None, img_ids: Tensor = None, txt_ids: Tensor = None, guidance: Tensor = None, freqs_cis: Tensor = None, joint_attention_kwargs: Optional[Dict[str, Any]] = None) -> Tensor
The [Flux2Transformer2DModel] forward method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states | `torch.Tensor` of shape `(batch_size, image_sequence_length, in_channels)` | Input | required |
encoder_hidden_states | `torch.Tensor` of shape `(batch_size, text_sequence_length, joint_attention_dim)` | Conditional embeddings (embeddings computed from the input conditions such as prompts) to use. | None |
timestep | `torch.LongTensor` | Used to indicate denoising step. | None |
joint_attention_kwargs | `dict`, *optional* | A kwargs dictionary that if specified is passed along to the | None |
Source code in fastvideo/models/dits/flux_2.py
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 | |
Functions:¶
fastvideo.models.dits.flux_2.apply_qk_norm ¶
apply_qk_norm(q: Tensor, k: Tensor, q_norm: Module, k_norm: Module, head_dim: int, allow_inplace: bool = True) -> Tuple[Tensor, Tensor]
Apply QK normalization for query and key tensors.
Source code in fastvideo/models/dits/flux_2.py
fastvideo.models.dits.flux_2.compute_flux2_freqs_cis_from_ids ¶
compute_flux2_freqs_cis_from_ids(rotary_emb: Flux2PosEmbed, text_ids: Tensor, latent_ids: Tensor, device: device, dtype: dtype | None = None) -> tuple[Tensor, Tensor]
Build (cos, sin) like Diffusers: pos_embed(txt) then pos_embed(img), cat dim 0.
Returns cos/sin in fp32 on device (dtype is ignored) so RoPE matches Diffusers.