Make RelPosEncXL able to encode the sign of the relative position - #3073
Open
yentur wants to merge 1 commit into
Open
Make RelPosEncXL able to encode the sign of the relative position#3073yentur wants to merge 1 commit into
yentur wants to merge 1 commit into
Conversation
make_pe() fills the negative half of the embedding with sin(+p) instead of sin(-p), so the rows for relative distance -d come out identical to the rows for +d. Both halves are read by RelPosMHAXL.rel_shift, so a non-causal encoder gets the same positional bias for a key d steps ahead as for one d steps behind. Every released checkpoint was trained this way, so the default is left alone and the Transformer-XL formulation is available through the new use_legacy_symmetric=False. Reported by @coollip. Add a test comparing make_pe() against a scalar reference written from the definition, following test_rope_rotate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
RelPosEncXL.make_pe()fills the negative half of the relative positional embedding withsin(+p)instead ofsin(-p).cosis even so the cosine terms were already right, butsinis odd, so the rows for relative distance-dcome out bit-identical to the rows for+d.Both halves are read.
rel_shiftmaps rowpofpos_embsonto the (query, key) pairs at relative distanced = (klen - 1) - p, so rows past the centre are the keys sitting to the right of the query. Probing it with a one-hot matrix,klen = 5:Rows 3 and 5 are equal on
develop, so a non-causalRelPosMHAXLadds the same positional bias to a key one step ahead as to a key one step behind. Running a constant sequence through it, so that the content term is flat and only the positional term shapes the map, gives an attention row that is exactly symmetric about the query:ESPnet's
RelPositionalEncodingnegates the position for both terms, and Transformer-XL'sPositionalEmbeddingtakes a signedpos_seq, so both are odd in the sine terms.Refs #3070, reported and diagnosed by @coollip.
Why the default is unchanged
Every checkpoint SpeechBrain has released was trained against the symmetric embedding, so switching it silently would degrade all of them. The corrected formulation is behind
use_legacy_symmetric=Falseand the default output is bit-identical todevelop:To put a number on what flipping the default would cost,
speechbrain/asr-streaming-conformer-librispeechdecoded in full-context greedy mode over the first 500 utterances of LibriSpeech test-clean, on CPU:That is a compatibility cost and not evidence that the symmetric encoding is better, since the checkpoint was trained with it. I have no way to train a Conformer from scratch to compare the two properly, so this PR makes no claim that the corrected encoding trains better, only that it is the one Transformer-XL and ESPnet define.
Models on the hub built with
attention_type: RelPosMHAXLandcausal: False, which is what would need retraining or pinning if the default were flipped:plus 27 recipe hparams using
RelPosMHAXL, none of which setcausal: True. Causal encoders are unaffected either way, the negative rows are masked out and the module output is bit-identical.Verification
The new test compares
make_pe()against a scalar reference written from the definition, in the same shape as the existingtest_rope_rotate. Ondevelop:With the patch, and with the surrounding modules:
The 4 unittest failures are
test_RNN,test_SpectrogramDrop,test_fallback_when_all_failandtest_run_shell. They fail identically on an unpatched checkout here (torch 2.13, python 3.12, macOS), so they are unrelated.Open question
The flag is only reachable by constructing
RelPosEncXLdirectly. Making it settable from a recipe needs a pass-through inTransformerInterfaceandTransformerASR, and flipping the default instead would need the affected hyperparams pinned the way #2604 was handled. I kept this PR to the one module so the choice stays with you, and I am happy to follow up with either.Before submitting