MCPcopy Create free account
hub / github.com/huggingface/diffusers / FeedForward

Class FeedForward

src/diffusers/models/attention.py:1682–1742  ·  view source on GitHub ↗

r""" A feed-forward layer. Parameters: dim (`int`): The number of channels in the input. dim_out (`int`, *optional*): The number of channels in the output. If not given, defaults to `dim`. mult (`int`, *optional*, defaults to 4): The multiplier to use for the hidden

Source from the content-addressed store, hash-verified

1680
1681
1682class FeedForward(nn.Module):
1683 r"""
1684 A feed-forward layer.
1685
1686 Parameters:
1687 dim (`int`): The number of channels in the input.
1688 dim_out (`int`, *optional*): The number of channels in the output. If not given, defaults to `dim`.
1689 mult (`int`, *optional*, defaults to 4): The multiplier to use for the hidden dimension.
1690 dropout (`float`, *optional*, defaults to 0.0): The dropout probability to use.
1691 activation_fn (`str`, *optional*, defaults to `"geglu"`): Activation function to be used in feed-forward.
1692 final_dropout (`bool` *optional*, defaults to False): Apply a final dropout.
1693 bias (`bool`, defaults to True): Whether to use a bias in the linear layer.
1694 """
1695
1696 def __init__(
1697 self,
1698 dim: int,
1699 dim_out: int | None = None,
1700 mult: int = 4,
1701 dropout: float = 0.0,
1702 activation_fn: str = "geglu",
1703 final_dropout: bool = False,
1704 inner_dim=None,
1705 bias: bool = True,
1706 ):
1707 super().__init__()
1708 if inner_dim is None:
1709 inner_dim = int(dim * mult)
1710 dim_out = dim_out if dim_out is not None else dim
1711
1712 if activation_fn == "gelu":
1713 act_fn = GELU(dim, inner_dim, bias=bias)
1714 if activation_fn == "gelu-approximate":
1715 act_fn = GELU(dim, inner_dim, approximate="tanh", bias=bias)
1716 elif activation_fn == "geglu":
1717 act_fn = GEGLU(dim, inner_dim, bias=bias)
1718 elif activation_fn == "geglu-approximate":
1719 act_fn = ApproximateGELU(dim, inner_dim, bias=bias)
1720 elif activation_fn == "swiglu":
1721 act_fn = SwiGLU(dim, inner_dim, bias=bias)
1722 elif activation_fn == "linear-silu":
1723 act_fn = LinearActivation(dim, inner_dim, bias=bias, activation="silu")
1724
1725 self.net = nn.ModuleList([])
1726 # project in
1727 self.net.append(act_fn)
1728 # project dropout
1729 self.net.append(nn.Dropout(dropout))
1730 # project out
1731 self.net.append(nn.Linear(inner_dim, dim_out, bias=bias))
1732 # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout
1733 if final_dropout:
1734 self.net.append(nn.Dropout(dropout))
1735
1736 def forward(self, hidden_states: torch.Tensor, *args, **kwargs) -> torch.Tensor:
1737 if len(args) > 0 or kwargs.get("scale", None) is not None:
1738 deprecation_message = "The `scale` argument is deprecated and will be ignored. Please remove it, as passing it will raise an error in the future. `scale` should directly be passed while calling the underlying pipeline component i.e., via `cross_attention_kwargs`."
1739 deprecate("scale", "1.0.0", deprecation_message)

Callers 14

__init__Method · 0.90
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…