Skip to content
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

FLUX #999

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open

FLUX #999

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ppdiffusers/ppdiffusers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
"SD3MultiControlNetModel",
# new add
"VCtrlModel",
# new add
"FluxTransformer2DModel",
]
)

Expand Down Expand Up @@ -277,6 +279,7 @@
"CLIPImageProjection",
"CogVideoXPipeline",
"CycleDiffusionPipeline",
"FluxPipeline",
"IFImg2ImgPipeline",
"IFImg2ImgSuperResolutionPipeline",
"IFInpaintingPipeline",
Expand Down Expand Up @@ -506,6 +509,7 @@
ControlNetModel,
DiTLLaMA2DModel,
DiTLLaMAT2IModel,
FluxTransformer2DModel,
GaussianDiffusion,
GaussianDiffusion_SDEdit,
Kandinsky3UNet,
Expand Down Expand Up @@ -558,6 +562,7 @@
DDPMPipeline,
DiffusionPipeline,
DiTPipeline,
FluxPipeline,
ImagePipelineOutput,
ImgToVideoSDPipeline,
KarrasVePipeline,
Expand Down
2 changes: 1 addition & 1 deletion ppdiffusers/ppdiffusers/loaders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
text_encoder_lora_state_dict,
)
from .ip_adapter import IPAdapterMixin
from .lora import LoraLoaderMixin, SD3LoraLoaderMixin, StableDiffusionXLLoraLoaderMixin
from .lora import LoraLoaderMixin, SD3LoraLoaderMixin, StableDiffusionXLLoraLoaderMixin, FluxLoraLoaderMixin
from .single_file import FromCkptMixin, FromSingleFileMixin
from .textual_inversion import TextualInversionLoaderMixin
else:
Expand Down
3 changes: 3 additions & 0 deletions ppdiffusers/ppdiffusers/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
# NOTE, new add
_import_structure["vctrl"] = ["VCtrlModel"]
_import_structure["cogvideox_transformer_3d_vctrl"] = ["CogVideoXTransformer3DVCtrlModel"]
# NOTE, new add
_import_structure["transformer_flux"] = ["FluxTransformer2DModel"]


if TYPE_CHECKING or PPDIFFUSERS_SLOW_IMPORT:
Expand Down Expand Up @@ -95,6 +97,7 @@
from .prior_transformer import PriorTransformer
from .t5_film_transformer import T5FilmDecoder
from .transformer_2d import Transformer2DModel
from .transformer_flux import FluxTransformer2DModel
from .transformer_sd3 import SD3Transformer2DModel
from .transformer_temporal import TransformerTemporalModel
from .unet_1d import UNet1DModel
Expand Down
192 changes: 188 additions & 4 deletions ppdiffusers/ppdiffusers/models/attention_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def __init__(
processor: Optional["AttnProcessor"] = None,
out_dim: int = None,
context_pre_only=None,
pre_only=False,
elementwise_affine: bool = True,
):
super().__init__()
Expand All @@ -124,6 +125,7 @@ def __init__(
self.dropout = dropout
self.out_dim = out_dim if out_dim is not None else query_dim
self.context_pre_only = context_pre_only
self.pre_only = pre_only

# we make use of this private variable to know whether this class is loaded
# with an deprecated state dict so that we can convert it on the fly
Expand Down Expand Up @@ -224,10 +226,12 @@ def __init__(
self.add_v_proj = linear_cls(added_kv_proj_dim, self.inner_dim)
if self.context_pre_only is not None:
self.add_q_proj = nn.Linear(added_kv_proj_dim, self.inner_dim)

self.to_out = nn.LayerList([])
self.to_out.append(linear_cls(self.inner_dim, query_dim, bias_attr=out_bias))
self.to_out.append(nn.Dropout(dropout))
if not self.pre_only:
self.to_out = nn.LayerList([])
self.to_out.append(linear_cls(self.inner_dim, query_dim, bias_attr=out_bias))
self.to_out.append(nn.Dropout(dropout))
else:
self.to_out = None

if self.context_pre_only is not None and not self.context_pre_only:
self.to_add_out = nn.Linear(self.inner_dim, self.out_dim, bias_attr=out_bias)
Expand Down Expand Up @@ -2149,6 +2153,184 @@ def __call__(
return out


class FluxAttnProcessor2_0:
"""Attention processor used typically in processing the SD3-like self-attention projections."""

def __init__(self):
if not hasattr(F, "scaled_dot_product_attention"):
raise ImportError("FluxAttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0.")

def __call__(
self,
attn: Attention,
hidden_states: paddle.Tensor,
encoder_hidden_states: paddle.Tensor = None,
attention_mask: Optional[paddle.Tensor] = None,
image_rotary_emb: Optional[paddle.Tensor] = None,
) -> paddle.Tensor:
batch_size, _, _ = hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape

# `sample` projections.
query = attn.to_q(hidden_states)
key = attn.to_k(hidden_states)
value = attn.to_v(hidden_states)

inner_dim = key.shape[-1]
head_dim = inner_dim // attn.heads

query = query.reshape([batch_size, -1, attn.heads, head_dim]).transpose([0, 2, 1, 3])
key = key.reshape([batch_size, -1, attn.heads, head_dim]).transpose([0, 2, 1, 3])
value = value.reshape([batch_size, -1, attn.heads, head_dim]).transpose([0, 2, 1, 3])

if attn.norm_q is not None:
query = attn.norm_q(query)
if attn.norm_k is not None:
key = attn.norm_k(key)

# the attention in FluxSingleTransformerBlock does not use `encoder_hidden_states`
if encoder_hidden_states is not None:
# `context` projections.
encoder_hidden_states_query_proj = attn.add_q_proj(encoder_hidden_states)
encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states)
encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states)

encoder_hidden_states_query_proj = encoder_hidden_states_query_proj.reshape([batch_size, -1, attn.heads, head_dim]).transpose([0, 2, 1, 3])
encoder_hidden_states_key_proj = encoder_hidden_states_key_proj.reshape([batch_size, -1, attn.heads, head_dim]).transpose([0, 2, 1, 3])
encoder_hidden_states_value_proj = encoder_hidden_states_value_proj.reshape([batch_size, -1, attn.heads, head_dim]).transpose([0, 2, 1, 3])

if attn.norm_added_q is not None:
encoder_hidden_states_query_proj = attn.norm_added_q(encoder_hidden_states_query_proj)
if attn.norm_added_k is not None:
encoder_hidden_states_key_proj = attn.norm_added_k(encoder_hidden_states_key_proj)

# attention
query = paddle.concat([encoder_hidden_states_query_proj, query], axis=2)
key = paddle.concat([encoder_hidden_states_key_proj, key], axis=2)
value = paddle.concat([encoder_hidden_states_value_proj, value], axis=2)

if image_rotary_emb is not None:
from .embeddings import apply_rotary_emb

query = apply_rotary_emb(query, image_rotary_emb)
key = apply_rotary_emb(key, image_rotary_emb)

hidden_states = F.scaled_dot_product_attention_(
query.transpose([0, 2, 1, 3]),
key.transpose([0, 2, 1, 3]),
value.transpose([0, 2, 1, 3]),
attn_mask=attention_mask,
dropout_p=0.0,
is_causal=False
)
hidden_states = hidden_states.reshape([batch_size, -1, attn.heads * head_dim])
hidden_states = hidden_states.astype(query.dtype)

if encoder_hidden_states is not None:
encoder_hidden_states, hidden_states = (
hidden_states[:, : encoder_hidden_states.shape[1]],
hidden_states[:, encoder_hidden_states.shape[1] :],
)

# linear proj
hidden_states = attn.to_out[0](hidden_states)
# dropout
hidden_states = attn.to_out[1](hidden_states)

encoder_hidden_states = attn.to_add_out(encoder_hidden_states)

return hidden_states, encoder_hidden_states
else:
return hidden_states


class FusedFluxAttnProcessor2_0:
"""Attention processor used typically in processing the SD3-like self-attention projections."""

def __init__(self):
if not hasattr(F, "scaled_dot_product_attention"):
raise ImportError(
"FusedFluxAttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0."
)

def __call__(
self,
attn: Attention,
hidden_states: paddle.Tensor,
encoder_hidden_states: paddle.Tensor = None,
attention_mask: Optional[paddle.Tensor] = None,
image_rotary_emb: Optional[paddle.Tensor] = None,
) -> paddle.Tensor:
batch_size, _, _ = hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape

# `sample` projections.
qkv = attn.to_qkv(hidden_states)
# split_size = qkv.shape[-1] // 3
query, key, value = paddle.split(qkv, 3, axis=-1)

inner_dim = key.shape[-1]
head_dim = inner_dim // attn.heads

query = query.reshape([batch_size, -1, attn.heads, head_dim])
key = key.reshape([batch_size, -1, attn.heads, head_dim])
value = value.reshape([batch_size, -1, attn.heads, head_dim])

if attn.norm_q is not None:
query = attn.norm_q(query)
if attn.norm_k is not None:
key = attn.norm_k(key)

# the attention in FluxSingleTransformerBlock does not use `encoder_hidden_states`
# `context` projections.
if encoder_hidden_states is not None:
encoder_qkv = attn.to_added_qkv(encoder_hidden_states)
# split_size = encoder_qkv.shape[-1] // 3
(
encoder_hidden_states_query_proj,
encoder_hidden_states_key_proj,
encoder_hidden_states_value_proj,
) = paddle.split(encoder_qkv, 3, dim=-1)

encoder_hidden_states_query_proj = encoder_hidden_states_query_proj.reshape([batch_size, -1, attn.heads, head_dim])
encoder_hidden_states_key_proj = encoder_hidden_states_key_proj.reshape([batch_size, -1, attn.heads, head_dim])
encoder_hidden_states_value_proj = encoder_hidden_states_value_proj.reshape([batch_size, -1, attn.heads, head_dim])

if attn.norm_added_q is not None:
encoder_hidden_states_query_proj = attn.norm_added_q(encoder_hidden_states_query_proj)
if attn.norm_added_k is not None:
encoder_hidden_states_key_proj = attn.norm_added_k(encoder_hidden_states_key_proj)

# attention
query = paddle.concat([encoder_hidden_states_query_proj, query], axis=1)
key = paddle.concat([encoder_hidden_states_key_proj, key], axis=1)
value = paddle.concat([encoder_hidden_states_value_proj, value], axis=1)

if image_rotary_emb is not None:
from .embeddings import apply_rotary_emb

query = apply_rotary_emb(query, image_rotary_emb)
key = apply_rotary_emb(key, image_rotary_emb)

hidden_states = F.scaled_dot_product_attention_(query, key, value, dropout_p=0.0, is_causal=False)
hidden_states = hidden_states.reshape([batch_size, -1, attn.heads * head_dim])
hidden_states = hidden_states.astype(query.dtype)

if encoder_hidden_states is not None:
encoder_hidden_states, hidden_states = (
hidden_states[:, : encoder_hidden_states.shape[1]],
hidden_states[:, encoder_hidden_states.shape[1] :],
)

# linear proj
hidden_states = attn.to_out[0](hidden_states)
# dropout
hidden_states = attn.to_out[1](hidden_states)
encoder_hidden_states = attn.to_add_out(encoder_hidden_states)

return hidden_states, encoder_hidden_states
else:
return hidden_states


class CogVideoXAttnProcessor2_0:
r"""
Processor for implementing scaled dot-product attention for the CogVideoX model. It applies a rotary embedding on
Expand Down Expand Up @@ -2347,6 +2529,8 @@ def __call__(
CustomDiffusionAttnProcessor,
CustomDiffusionXFormersAttnProcessor,
CustomDiffusionAttnProcessor2_5,
FluxAttnProcessor2_0,
FusedFluxAttnProcessor2_0,
# deprecated
LoRAAttnProcessor,
LoRAAttnProcessor2_5,
Expand Down
Loading