Skip to content

Fix LoraEmbedding casting outputs to the token ID dtype#67

Open
Robertboy18 wants to merge 1 commit into
aws-neuron:mainfrom
Robertboy18:fix-lora-embedding-output-dtype
Open

Fix LoraEmbedding casting outputs to the token ID dtype#67
Robertboy18 wants to merge 1 commit into
aws-neuron:mainfrom
Robertboy18:fix-lora-embedding-output-dtype

Conversation

@Robertboy18

Copy link
Copy Markdown

Hi! I noticed that LoraEmbedding.forward saves 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:

  • Base embedding: torch.float32 [[0.25, 0.5, 0.75]]
  • LoRA embedding: 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.

Copilot AI review requested due to automatic review settings July 14, 2026 15:05
@Robertboy18 Robertboy18 requested a review from aws-maens as a code owner July 14, 2026 15:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.forward to 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:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants