Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix (scaling)!: clamp to avoid inf/nan in forward/backward #1097

Merged
merged 5 commits into from
Nov 19, 2024

Conversation

Giuseppe5
Copy link
Collaborator

@Giuseppe5 Giuseppe5 commented Nov 18, 2024

Reason for this PR

Restrict scaling with log op could cause inf/nan in the forward pass and/or in the gradients.

Changes Made in this PR

Applying a restriction before (new behaviour) and after (existing behaviour) helps avoiding that.

Testing Summary

Add tests for standalone and runtime scaling to check inf/nan in forward/backwards.

Risk Highlight

This is a breaking change. Old checkpoints will work, new QAT/PTQ runs could produce different results from before.

  • This PR includes code from another work (please detail).
  • This PR contains API-breaking changes.
  • This PR depends on work in another PR (please provide links/details).
  • This PR introduces new dependencies (please detail).
  • There are coverage gaps not covered by tests.
  • Documentation updates required in subsequent PR.

Checklist

  • Code comments added to any hard-to-understand areas, if applicable.
  • Changes generate no new warnings.
  • Updated any relevant tests, if applicable.
  • No conflicts with destination dev branch.
  • I reviewed my own code changes.
  • Initial CI/CD passing.
  • 1+ reviews given, and any review issues addressed and approved.
  • Post-review full CI/CD passing.

out = self.restrict_scaling_pre(out)
else:
out = self.value
threshold = self.restrict_threshold(self.restrict_threshold_pre(threshold))
out = self.clamp_scaling(self.restrict_scaling(out))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe self.clamp_scaling is not necessary here because we're already in log domain, and self.restrict_scaling is responsible only for rounding and potential conversion from log to real domain (i.e., exponential).

Applying clamping before restrict_val means we can't get to negative values, thus scaling values between 0 and 1.
@nickfraser

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds right to me!

@Giuseppe5 Giuseppe5 requested review from nickfraser and removed request for nickfraser November 19, 2024 13:03
@Giuseppe5 Giuseppe5 requested review from nickfraser and removed request for nickfraser November 19, 2024 14:21
@Giuseppe5 Giuseppe5 merged commit abf4a40 into Xilinx:dev Nov 19, 2024
368 of 374 checks passed
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