Skip to content

fix: use return value of torch.clamp for weight clamping - #4401

Closed
Mr-Neutr0n wants to merge 1 commit into
coqui-ai:devfrom
Mr-Neutr0n:fix/torch-clamp-return-value
Closed

fix: use return value of torch.clamp for weight clamping#4401
Mr-Neutr0n wants to merge 1 commit into
coqui-ai:devfrom
Mr-Neutr0n:fix/torch-clamp-return-value

Conversation

@Mr-Neutr0n

Copy link
Copy Markdown

Summary

  • torch.clamp() is not in-place — it returns a new tensor. In both GE2ELoss.forward() and AngleProtoLoss.forward() in TTS/encoder/losses.py, the call torch.clamp(self.w, 1e-6) discards the return value, so the weight parameter self.w is never actually clamped to a minimum of 1e-6.
  • Replaced with self.w.data.clamp_(1e-6) which correctly clamps the parameter's underlying data in-place, ensuring the weight scaling factor stays positive as intended.

Details

The bug exists in two locations in TTS/encoder/losses.py:

GE2ELoss.forward() (line 115):

# Before (bug): return value discarded, self.w unchanged
torch.clamp(self.w, 1e-6)

# After (fix): in-place clamp on parameter data
self.w.data.clamp_(1e-6)

AngleProtoLoss.forward() (line 159):

# Before (bug): return value discarded, self.w unchanged
torch.clamp(self.w, 1e-6)

# After (fix): in-place clamp on parameter data
self.w.data.clamp_(1e-6)

Without this fix, if self.w becomes negative during training (via gradient updates), the cosine similarity matrix scaling would be inverted, potentially destabilizing speaker encoder training.

Test plan

  • Verify self.w stays non-negative during GE2E loss training
  • Verify self.w stays non-negative during AngleProto loss training
  • Run existing encoder loss unit tests

torch.clamp() is not in-place and returns a new tensor. The previous
code called torch.clamp(self.w, 1e-6) without using the return value,
so the weight parameter was never actually clamped. This affected both
GE2ELoss and AngleProtoLoss classes.

Replace with self.w.data.clamp_(1e-6) which correctly clamps the
parameter's data in-place.
@CLAassistant

CLAassistant commented Feb 11, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Mr-Neutr0n

Copy link
Copy Markdown
Author

closing this out — saw the note about the repo being unmaintained. will redirect to the active fork if still relevant

@Mr-Neutr0n Mr-Neutr0n closed this Feb 12, 2026
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