Embedding addition

×

Add token and position vectors element-wise before training-only embedding dropout initializes the residual stream.

E=WTE[input_ids]+WPE[position_ids]{\htmlData{light=embed-add}{E}}=\mathrm{WTE}[\mathrm{input\_ids}]+\mathrm{WPE}[\mathrm{position\_ids}]
E=WTE[input_ids]+WPE[position_ids]\begin{aligned}{\htmlData{light=embed-add}{E}}={}&\mathrm{WTE}[\mathrm{input\_ids}]\\&+\mathrm{WPE}[\mathrm{position\_ids}]\end{aligned}
in
EtE_{t} [B batch, T positions, 768 d]EposE_{\mathrm{pos}} [B batch, T positions, 768 d]
out
EE [B batch, T positions, 768 d]

During training the maintained code applies dropout to EE before the first layer (modeling_gpt2.py 604): x0=Dropout⁡(E;pembd)x_0=\operatorname{Dropout}(E;p_{\mathrm{embd}}). At evaluation, and in the released code, x0=Ex_0=E.

Sources

In codetf.gather(wte, X) + tf.gather(wpe, positions_for(X, past_length))

Open pinned code ↗

Direct evidence

For the base GPT-2 text path, embedding dropout follows token-plus-position addition and precedes the layer loop; optional token-type embeddings are outside the base text path.

modeling_gpt2.py 576–608 ↗ · huggingface/transformers @ 71c6f699ac9b

Excerpt
        hidden_states = inputs_embeds + position_embeds.to(inputs_embeds.device)

… lines 578–603 omitted …

        hidden_states = self.drop(hidden_states)

… lines 605–607 omitted …

        for i, block in enumerate(self.h):

The original graph gathers the two tables and adds them into the initial stream.

model.py 152–157 ↗ · openai/gpt-2 @ 9b63575ef427

Excerpt
        h = tf.gather(wte, X) + tf.gather(wpe, positions_for(X, past_length))