tensorflow / tensorflow/text

Add&Norm layer is missing after each attention layer

Open
#1,104 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
1.3k
Forks
379
Avg merge
3h 30m
Merged PRs (30d)
8

Description

Hi,
According to the transformer architecture, there should be a add&norm layer after each attention layer. However for code in /docs/tutorials/transformer.ipynb, those Add&Norm layers are missing.
transformer_img


Take decoder layer for example, the original code is:

  def call(self, x, context):
      x = self.causal_self_attention(x=x)
      x = self.cross_attention(x=x, context=context)

      \# Cache the last attention scores for plotting later
      self.last_attn_scores = self.cross_attention.last_attn_scores

      x = self.ffn(x)  # Shape `(batch_size, seq_len, d_model)`.
  return x

If we add Add&Norm layers, it should be:

  def call(self, x, context):
      x = self.add([x, self.causal_self_attention(x=x)])
      x = self.layer_norm1(x)

      x = self.add([x, self.cross_attention(x=x, context=context)])
      x = self.layer_norm2(x)

      \# Cache the last attention scores for plotting later
      self.last_attn_scores = self.cross_attention.last_attn_scores

      x = self.ffn(x)  # Shape `(batch_size, seq_len, d_model)`.
  return x

I wonder if those add&norm layers are ignored on purpose.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open /docs/tutorials/transformer.ipynb and inspect the encoder and decoder layer call methods around the attention operations. Compare the tutorial with the transformer architecture shown in the issue and determine whether the missing Add&Norm steps are intentional. Done means the tutorial reflects the intended architecture and the notebook still runs successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation, machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.