Transformer models have limited sequence length at inference time
because of positional embeddings. But there are workarounds.
Self-attention in transformer does not distinguish the order of keys/values,
it works as if the sequence is a bag of words.
So to expose the sequence order to the model, one typically adds an extra
"positional embedding" vector to each input token embedding.
This extra positional information then allows the model to construct primitives like "attention head that looks at previous token".
There are several variants of how to do this exactly.
One way is to use an extra trainable parameter matrix [L,M]
where L is maximum input length and M is model dimension.
After training a model this way, it simply becomes impossible
to embed tokens with position > L. So we get a hard limit for which there is no workaround.
Another way is to use a non-trainable matrix [L,M]
initialized with a specially crafted set of sinusoids
(original "attention is all you need" paper does this).
A model trained this way would not have a length limit,
because this matrix of sinusoids you can extend to arbitrary size.
Yes another way is to use "relative positional encoding".
With this, for each pair of tokens you take relative position,
which would be in range [-L ... +L], and embed that instead.
Then you inject this vector into attention layer in the right way
(section 3.3 in https://arxiv.org/pdf/1901.02860.pdf).
Now you still kind of have a limit of [-L, L].
But you can always "clip" relative position to this range,
and pretend that all pairs of tokens at distance >= L
have the same relative position ("very far from each other").
And this allows to run inference with longer inputs.