activations.py 6.36 KB
Newer Older
1
# coding=utf-8
2
# Copyright 2025 HuggingFace Inc.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import torch
import torch.nn.functional as F
18
19
from torch import nn

20
from ..utils import deprecate
21
from ..utils.import_utils import is_torch_npu_available, is_torch_version
22

23

24
25
26
if is_torch_npu_available():
    import torch_npu

27
28
29
30
31
32
ACT2CLS = {
    "swish": nn.SiLU,
    "silu": nn.SiLU,
    "mish": nn.Mish,
    "gelu": nn.GELU,
    "relu": nn.ReLU,
33
34
35
}


36
37
38
39
40
41
42
43
44
def get_activation(act_fn: str) -> nn.Module:
    """Helper function to get activation function from string.

    Args:
        act_fn (str): Name of activation function.

    Returns:
        nn.Module: Activation function.
    """
45
46

    act_fn = act_fn.lower()
47
48
    if act_fn in ACT2CLS:
        return ACT2CLS[act_fn]()
49
    else:
50
        raise ValueError(f"activation function {act_fn} not found in ACT2FN mapping {list(ACT2CLS.keys())}")
51
52


53
54
55
56
57
58
59
60
61
62
63
64
class FP32SiLU(nn.Module):
    r"""
    SiLU activation function with input upcasted to torch.float32.
    """

    def __init__(self):
        super().__init__()

    def forward(self, inputs: torch.Tensor) -> torch.Tensor:
        return F.silu(inputs.float(), inplace=False).to(inputs.dtype)


65
66
67
68
69
70
71
72
class GELU(nn.Module):
    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.
73
        bias (`bool`, defaults to True): Whether to use a bias in the linear layer.
74
75
    """

76
    def __init__(self, dim_in: int, dim_out: int, approximate: str = "none", bias: bool = True):
77
        super().__init__()
78
        self.proj = nn.Linear(dim_in, dim_out, bias=bias)
79
80
81
        self.approximate = approximate

    def gelu(self, gate: torch.Tensor) -> torch.Tensor:
82
83
84
85
        if gate.device.type == "mps" and is_torch_version("<", "2.0.0"):
            # fp16 gelu not supported on mps before torch 2.0
            return F.gelu(gate.to(dtype=torch.float32), approximate=self.approximate).to(dtype=gate.dtype)
        return F.gelu(gate, approximate=self.approximate)
86
87
88
89
90
91
92
93
94

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


class GEGLU(nn.Module):
    r"""
Quentin Gallouédec's avatar
Quentin Gallouédec committed
95
    A [variant](https://huggingface.co/papers/2002.05202) of the gated linear unit activation function.
96
97
98
99

    Parameters:
        dim_in (`int`): The number of channels in the input.
        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
109
110
111
        if gate.device.type == "mps" and is_torch_version("<", "2.0.0"):
            # fp16 gelu not supported on mps before torch 2.0
            return F.gelu(gate.to(dtype=torch.float32)).to(dtype=gate.dtype)
        return F.gelu(gate)
112

113
114
115
116
    def forward(self, hidden_states, *args, **kwargs):
        if len(args) > 0 or kwargs.get("scale", None) is not None:
            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`."
            deprecate("scale", "1.0.0", deprecation_message)
117
118
119
120
121
122
123
        hidden_states = self.proj(hidden_states)
        if is_torch_npu_available():
            # using torch_npu.npu_geglu can run faster and save memory on NPU.
            return torch_npu.npu_geglu(hidden_states, dim=-1, approximate=1)[0]
        else:
            hidden_states, gate = hidden_states.chunk(2, dim=-1)
            return hidden_states * self.gelu(gate)
124
125


126
127
class SwiGLU(nn.Module):
    r"""
Quentin Gallouédec's avatar
Quentin Gallouédec committed
128
129
    A [variant](https://huggingface.co/papers/2002.05202) of the gated linear unit activation function. It's similar to
    `GEGLU` but uses SiLU / Swish instead of GeLU.
130
131
132
133
134
135
136
137
138

    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 to use a bias in the linear layer.
    """

    def __init__(self, dim_in: int, dim_out: int, bias: bool = True):
        super().__init__()
Aryan's avatar
Aryan committed
139

140
141
142
143
144
145
146
147
148
        self.proj = nn.Linear(dim_in, dim_out * 2, bias=bias)
        self.activation = nn.SiLU()

    def forward(self, hidden_states):
        hidden_states = self.proj(hidden_states)
        hidden_states, gate = hidden_states.chunk(2, dim=-1)
        return hidden_states * self.activation(gate)


149
150
151
class ApproximateGELU(nn.Module):
    r"""
    The approximate form of the Gaussian Error Linear Unit (GELU). For more details, see section 2 of this
Quentin Gallouédec's avatar
Quentin Gallouédec committed
152
    [paper](https://huggingface.co/papers/1606.08415).
153
154
155
156

    Parameters:
        dim_in (`int`): The number of channels in the input.
        dim_out (`int`): The number of channels in the output.
157
        bias (`bool`, defaults to True): Whether to use a bias in the linear layer.
158
159
    """

160
    def __init__(self, dim_in: int, dim_out: int, bias: bool = True):
161
        super().__init__()
162
        self.proj = nn.Linear(dim_in, dim_out, bias=bias)
163
164
165
166

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.proj(x)
        return x * torch.sigmoid(1.702 * x)
Aryan's avatar
Aryan committed
167
168
169
170
171
172
173
174
175
176
177
178


class LinearActivation(nn.Module):
    def __init__(self, dim_in: int, dim_out: int, bias: bool = True, activation: str = "silu"):
        super().__init__()

        self.proj = nn.Linear(dim_in, dim_out, bias=bias)
        self.activation = get_activation(activation)

    def forward(self, hidden_states):
        hidden_states = self.proj(hidden_states)
        return self.activation(hidden_states)