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

Class GEGLU

src/diffusers/models/activations.py:93–123  ·  view source on GitHub ↗

r""" A [variant](https://huggingface.co/papers/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. bias (`bool`, defaults to True): Whether t

Source from the content-addressed store, hash-verified

91
92
93class GEGLU(nn.Module):
94 r"""
95 A [variant](https://huggingface.co/papers/2002.05202) of the gated linear unit activation function.
96
97 Parameters:
98 dim_in (`int`): The number of channels in the input.
99 dim_out (`int`): The number of channels in the output.
100 bias (`bool`, defaults to True): Whether to use a bias in the linear layer.
101 """
102
103 def __init__(self, dim_in: int, dim_out: int, bias: bool = True):
104 super().__init__()
105 self.proj = nn.Linear(dim_in, dim_out * 2, bias=bias)
106
107 def gelu(self, gate: torch.Tensor) -> torch.Tensor:
108 if gate.device.type == "mps" and is_torch_version("<", "2.0.0"):
109 # fp16 gelu not supported on mps before torch 2.0
110 return F.gelu(gate.to(dtype=torch.float32)).to(dtype=gate.dtype)
111 return F.gelu(gate)
112
113 def forward(self, hidden_states, *args, **kwargs):
114 if len(args) > 0 or kwargs.get("scale", None) is not None:
115 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`."
116 deprecate("scale", "1.0.0", deprecation_message)
117 hidden_states = self.proj(hidden_states)
118 if is_torch_npu_available():
119 # using torch_npu.npu_geglu can run faster and save memory on NPU.
120 return torch_npu.npu_geglu(hidden_states, dim=-1, approximate=1)[0]
121 else:
122 hidden_states, gate = hidden_states.chunk(2, dim=-1)
123 return hidden_states * self.gelu(gate)
124
125
126class SwiGLU(nn.Module):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…