GPT-2

Position-wise feed-forward network

Approximate GELU

×

A smooth feature-wise activation, computed through its tanh approximation.

g=h2[1+tanh⁡ ⁣(2π(h+0.044715h3))]{\htmlData{light=activated gelu}{g}}=\frac{{\htmlData{light=pre-activation c_fc}{h}}}{2}\left[1+\tanh\!\left(\sqrt{\frac{2}{\pi}}\left({\htmlData{light=pre-activation c_fc}{h}}+0.044715{\htmlData{light=pre-activation c_fc}{h^3}}\right)\right)\right]
g=h2[1+tanh⁡u]u=2π (h+0.044715h3)\begin{aligned}{\htmlData{light=activated gelu}{g}}&=\frac{{\htmlData{light=pre-activation c_fc}{h}}}{2}\big[1+\tanh u\big]\\u&=\sqrt{\tfrac{2}{\pi}}\,\big({\htmlData{light=pre-activation c_fc}{h}}+0.044715{\htmlData{light=pre-activation c_fc}{h^3}}\big)\end{aligned}
in
hh [B batch, T positions, 3072 d_ff]
out
gg [B batch, T positions, 3072 d_ff]
Explanation

The function is near zero for strongly negative inputs, dips to about −0.17 near −0.75, and approaches the identity for large positive ones.

-3-2-10123y = xGELU
The dashed line is y = x. Function evaluated from −4 to 4.
Sources

In codegelu(…), applied to conv1d(x, 'c_fc', n_state)

Open pinned code ↗

Direct evidence

The released graph defines the tanh approximation used by GPT-2.

model.py 25–26 ↗ · openai/gpt-2 @ 9b63575ef427

Excerpt
def gelu(x):
    return 0.5*x*(1+tf.tanh(np.sqrt(2/np.pi)*(x+0.044715*tf.pow(x, 3))))