#55Token-to-token path lengthEasy

Token-to-token path length

Background

A key reason attention beat recurrence is path length — how many hops information takes to travel from token ii to token jj:

  • RNN/LSTM: the signal flows along the chain of hidden states, so the distance is ij|i - j|.
  • Attention: any token can read any other in a single layer, so the distance is 11.

Long paths are exactly what make long-range dependencies hard for RNNs.

Problem statement

Implement path_length(i, j, arch) returning the information path length between positions i and j.

Input

  • i, jint token positions.
  • arch"rnn" or "attention".

Output

Returns an int: abs(i - j) for "rnn", 1 for "attention".

Examples

Example 1 — far apart in an RNN

Input:  i = 0, j = 5, arch = "rnn"
Output: 5

Example 2 — direct in attention

Input:  i = 0, j = 5, arch = "attention"
Output: 1

Constraints

  • "rnn"abs(i - j); "attention"1.
  • Return a plain int.

Notes

  • Shorter paths mean shorter gradient routes too — one reason attention trains more stably over long contexts.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • rnn distance example
  • attention one hop sample
  • rnn absolute value reference