Skip to content

Commit

Permalink
Revert "【Hackathon 5th No.83】PaddleMIX ppdiffusers models模块功能升级同步HF" (P…
Browse files Browse the repository at this point in the history
…addlePaddle#354)

Reverts PaddlePaddle#322
回滚该PR,等之后修复后再合入,下面是对应的问题。
1. fastdeploy的sd和sdxl 推理造成了错误。无法顺利加载vae的decoder模型,可能当前升级有部分修改,导致这个发生错误。
2.
sd的预训练和lora微调存在性能下降10%,可能怀疑是recompute粒度发生了修改,部分模块之前没有加recompute现在加上了recompute导致的。
  • Loading branch information
JunnYu authored Dec 19, 2023
1 parent 9fd8c06 commit 8339d14
Show file tree
Hide file tree
Showing 46 changed files with 2,375 additions and 8,809 deletions.
2 changes: 0 additions & 2 deletions ppdiffusers/ppdiffusers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
LVDMAutoencoderKL,
LVDMUNet3DModel,
ModelMixin,
MotionAdapter,
MultiAdapter,
PriorTransformer,
T2IAdapter,
Expand All @@ -73,7 +72,6 @@
UNet2DConditionModel,
UNet2DModel,
UNet3DConditionModel,
UNetMotionModel,
VQModel,
)
from .optimization import (
Expand Down
597 changes: 174 additions & 423 deletions ppdiffusers/ppdiffusers/loaders.py

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion ppdiffusers/ppdiffusers/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
from .unet_2d import UNet2DModel
from .unet_2d_condition import UNet2DConditionModel
from .unet_3d_condition import UNet3DConditionModel
from .unet_motion_model import MotionAdapter, UNetMotionModel
from .vq_model import VQModel

try:
Expand Down
94 changes: 7 additions & 87 deletions ppdiffusers/ppdiffusers/models/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,94 +13,14 @@
# limitations under the License.

import paddle.nn as nn
import paddle.nn.functional as F

from ..utils import USE_PEFT_BACKEND
from .lora import LoRACompatibleLinear

ACTIVATION_FUNCTIONS = {
"swish": nn.Silu(),
"silu": nn.Silu(),
"mish": nn.Mish(),
"gelu": nn.GELU(),
"relu": nn.ReLU(),
}


def get_activation(act_fn: str) -> nn.Layer:
"""Helper function to get activation function from string.
Args:
act_fn (str): Name of activation function.
Returns:
nn.Layer: Activation function.
"""

act_fn = act_fn.lower()
if act_fn in ACTIVATION_FUNCTIONS:
return ACTIVATION_FUNCTIONS[act_fn]
def get_activation(act_fn):
if act_fn in ["swish", "silu"]:
return nn.Silu()
elif act_fn == "mish":
return nn.Mish()
elif act_fn == "gelu":
return nn.GELU()
else:
raise ValueError(f"Unsupported activation function: {act_fn}")


class GELU(nn.Layer):
r"""
GELU activation function with tanh approximation support with `approximate="tanh"`.
Parameters:
dim_in (`int`): The number of channels in the input.
dim_out (`int`): The number of channels in the output.
approximate (`str`, *optional*, defaults to `"none"`): If `"tanh"`, use tanh approximation.
"""

def __init__(self, dim_in: int, dim_out: int, approximate: str = "none"):
super().__init__()
self.proj = LoRACompatibleLinear(dim_in, dim_out)
self.approximate = approximate
self.approximate_bool = approximate == "tanh"

def forward(self, hidden_states):
hidden_states = self.proj(hidden_states)
hidden_states = F.gelu(hidden_states, approximate=self.approximate_bool)
return hidden_states


class GEGLU(nn.Layer):
r"""
A [variant](https://arxiv.org/abs/2002.05202) of the gated linear unit activation function.
Parameters:
dim_in (`int`): The number of channels in the input.
dim_out (`int`): The number of channels in the output.
"""

def __init__(self, dim_in: int, dim_out: int):
super().__init__()
linear_cls = LoRACompatibleLinear if not USE_PEFT_BACKEND else nn.Linear

self.proj = linear_cls(dim_in, dim_out * 2)

def forward(self, hidden_states, scale: float = 1.0):
args = () if USE_PEFT_BACKEND else (scale,)
hidden_states, gate = self.proj(hidden_states, *args).chunk(2, axis=-1)
return hidden_states * F.gelu(gate)


class ApproximateGELU(nn.Layer):
r"""
The approximate form of the Gaussian Error Linear Unit (GELU). For more details, see section 2 of this
[paper](https://arxiv.org/abs/1606.08415).
Parameters:
dim_in (`int`): The number of channels in the input.
dim_out (`int`): The number of channels in the output.
"""

def __init__(self, dim_in: int, dim_out: int):
super().__init__()
self.proj = nn.Linear(dim_in, dim_out)

def forward(self, x):
x = self.proj(x)
return x * F.sigmoid(1.702 * x)
Loading

0 comments on commit 8339d14

Please sign in to comment.