Fix LoraEmbedding casting outputs to the token ID dtype#67
Open
Robertboy18 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes an incorrect dtype restoration in LoraEmbedding.forward where the output was being cast to the token ID dtype (typically torch.int64), truncating embedding values. The change aligns LoraEmbedding with expected embedding semantics by preserving the base embedding’s floating-point output dtype, and adds a regression test to prevent reintroducing the issue.
Changes:
- Update
LoraEmbedding.forwardto restore dtype based on the base layer’s output dtype rather than the input token IDs’ dtype. - Add a unit test verifying the wrapped embedding preserves floating-point output dtype and initially matches the base layer output.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/neuronx_distributed/modules/lora/layer.py |
Preserves LoraEmbedding output dtype by restoring to the base embedding output dtype instead of input token ID dtype. |
test/unit_test/modules/lora/test_lora_layers.py |
Adds a regression test ensuring LoRA-wrapped embeddings keep floating dtype and match the base output initially. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
316
to
319
| def forward(self, x: torch.Tensor, *args: Any, **kwargs: Any) -> torch.Tensor: | ||
| previous_dtype = x.dtype | ||
| result = self.base_layer(x, *args, **kwargs) | ||
| previous_dtype = result.dtype | ||
| if not self.merged: |
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.
Hi! I noticed that
LoraEmbedding.forwardsaves the dtype of its input tensor.For embeddings, the input contains token IDs and is normally
torch.int64, while the embedding output is floating-point. The final cast therefore converts the embedding output to integers and truncates its values.I reproduced this on CPU:
torch.float32 [[0.25, 0.5, 0.75]]torch.int64 [[0, 0, 0]]This change preserves the dtype of the base embedding output instead. I also added a regression test confirming that the wrapped layer retains its floating-point dtype and initially matches the base layer.
I also modeled and checked this behavior using TorchLean :) The proof shows that casting fractional embedding values through the integer token dtype changes the output. If helpful, I can also attach the full TorchLean formalization.