fix: PositionalEncoding Shape Mismatch on Odd Dimensions#609
Open
agam263 wants to merge 1 commit intogc-os-ai:mainfrom
Open
fix: PositionalEncoding Shape Mismatch on Odd Dimensions#609agam263 wants to merge 1 commit intogc-os-ai:mainfrom
agam263 wants to merge 1 commit intogc-os-ai:mainfrom
Conversation
Contributor
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.
Fix: PositionalEncoding Shape Mismatch on Odd Dimensions
fix #607
📖 Summary
This PR resolves a persistent runtime crash in the
PositionalEncodinglayer that occurred whenever the model dimension (d_model) was an odd number. The fix ensures that the layer correctly handles arbitrary embedding dimensions, making theAptaTransarchitecture more flexible and robust.🔍 Technical Root Cause
The
PositionalEncodinglayer generates fixed sinusoids to inject positional information into embeddings. The implementation uses a frequency-divisor vector (div_term) that is shared between sine and cosine operations:0, 2, 4...) and cosine waves to odd indices (1, 3, 5...).div_termis calculated based ontorch.arange(0, d_model, 2), which has a length ofceil(d_model / 2).d_modelis 128 (even):div_term: 64 values (No error)d_modelis 127 (odd):div_term: 64 valuespe[0, :, 1::2] = torch.cos(position * div_term)fails for oddd_modelbecause PyTorch cannot fit 64 values into a tensor with 63 slots.🛠️ Proposed Changes
1. Corrected Frequency Mapping
Adjusted the cosine assignment to correctly slice the
div_termto match the number of available odd-indexed slots.div_term[: d_model // 2]to ensure the number of frequencies exactly matches the number of odd positions, regardless of whetherd_modelis even or odd.2. Dimensionality Regression Tests
Introduced
pyaptamer/aptatrans/tests/test_pe_robustness.pyto safeguard against future regressions:d_model=1).d_modelis even.✅ Verification Results
pytest pyaptamer/aptatrans/tests/test_pe_robustness.py-> Passed.