Why does the skip connection in a transformer decoder's residual cross attention block come from the queries rather than the values?

life_efficient: <p>Transformer’s residual transformer decoder cross attention layer use keys and values from the encoder, and queries from the decoder. These residual layers implement out = x + F(x).</p>
<p>As implemented in the PyTorch source code, and as the original transformer diagram shows, the residual layer skip connection comes from the queries (arrow coming out of decoder self-attention), not from the keys and values (arrow coming from the encoder). That is, out = queries + F(queries, keys, values) is implemented.</p>
<p>The values indicate “How each input is represented”, and the point of the attention layer is to created a weighted average of the available values, so it seems like out = values + F(queries, keys, values)  would make more sense.</p>
<p>Why is this not implemented?</p>

Wuuu: <p>This answer is from chatgpt:<br>
The design choice of using queries for the skip connection in the residual cross-attention block of a transformer decoder, rather than the values, is rooted in the fundamental working principles of transformers and the role of each component (queries, keys, and values) in the attention mechanism. Let’s break this down:</p>
<h3><a name="understanding-the-roles-of-queries-keys-and-values-1" class="anchor" href="#understanding-the-roles-of-queries-keys-and-values-1"></a>Understanding the Roles of Queries, Keys, and Values:</h3>
<ol>
<li><strong>Queries</strong>: Represent the current state of the decoder. In the context of sequence-to-sequence models, this could be the partially decoded sequence at a given step.</li>
<li><strong>Keys and Values</strong>: Derived from the encoder, they represent the information of the input sequence. Keys are used to compute attention weights, and values are used to construct the output based on these weights.</li>
</ol>
<h3><a name="why-queries-for-skip-connections-2" class="anchor" href="#why-queries-for-skip-connections-2"></a>Why Queries for Skip Connections?</h3>
<ol>
<li>
<p><strong>Maintaining Sequential Context</strong>: The primary role of the decoder is to generate the output sequence step-by-step. The queries represent the current ‘thought’ or state of the decoder at each step. The skip connection from queries ensures that this current state is always directly influencing the output, maintaining the sequential context inherent in the task.</p>
</li>
<li>
<p><strong>Residual Learning</strong>: In the original transformer architecture, the principle of residual learning (<code>out = x + F(x)</code>) is applied. Here, <code>x</code> is the input to a layer, and <code>F(x)</code> is the transformation applied by that layer. By using the queries (<code>x</code>) in the residual connection, the architecture ensures that each layer can learn to modify the current state incrementally. This is in line with the idea of residual networks where each layer ideally learns a residual modification to the identity function.</p>
</li>
<li>
<p><strong>Consistency in the Decoder</strong>: Throughout the decoder, each sub-layer (self-attention and cross-attention) uses its input (queries) for the residual connection. This consistency is important for the model’s learning process, as it allows each sub-layer to refine the decoder’s current state.</p>
</li>
<li>
<p><strong>Values as Contextual Information</strong>: The role of values is to provide contextual information from the encoder, used to generate the weighted average output based on the queries. This output is then added to the queries through the residual connection. If the values were used for the residual connection, it would mean directly adding raw encoder information to the output without the decoder’s current context, which could lead to a loss of sequential coherence in the decoder’s output.</p>
</li>
</ol>
<h3><a name="conceptual-perspective-3" class="anchor" href="#conceptual-perspective-3"></a>Conceptual Perspective:</h3>
<ul>
<li><strong>Attention as a Refinement Process</strong>: The attention mechanism can be viewed as a way to refine the decoder’s current state (queries) by considering additional information (keys and values from the encoder). The skip connection supports this view by ensuring that the refinement is an additive process to the current state.</li>
</ul>
<h3><a name="implementation-perspective-4" class="anchor" href="#implementation-perspective-4"></a>Implementation Perspective:</h3>
<ul>
<li><strong>Model Stability and Training Efficiency</strong>: Empirically, it has been found that using queries for residual connections provides a stable training dynamic and helps the model converge effectively. This might be due to the way the transformer balances learning from the encoder’s context and the decoder’s own generated context.</li>
</ul>
<p>In summary, using queries for skip connections in the transformer decoder’s cross-attention block is a design choice that aligns with the transformer’s overall architecture and its goal of sequential generation. It ensures the coherence and consistency of the decoder’s output while maintaining a stable and efficient learning process.</p>