-
Notifications
You must be signed in to change notification settings - Fork 273
Fix wrong scale eps applied #1770
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
Open
alexsamardzic
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
alexsamardzic:fix-wrong-scale-eps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is
eps
used? I see that on L984 we are calculatingeps
again, just wondering if we need both calculations?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used in lines 957 and 961 (line numbers are after changes by this PR), to clamp scale in order to prevent 1/scale to become Inf. In second case, this is immediately necessary, as dividing by scale is already performed in line 966. Furthermore, towards the end of the function, in line 980, scale may be converted to different data type from what is used up to this point, which could trigger scale going into subnormal range for this final data type, so it's necessary to clamp again from below (and, while we're at it, I'm clamping it from above too).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so
eps
is read as an argument. In that case, it's a bit confusing to silently overrideeps
here. Is there a way to set it correctly at the callsite (using the logic you have here) so the setting is honored in this function?Overall logic of how to choose eps looks great, now I'm just trying to help fit this in cleanly :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand - it is not nice that the argument may get changed silently, but there is a number of call sites, so it seems to me for the maintenance etc., the best place to fix it is here, at single place. If silent change is considered very intrusive, maybe I can add a printout in case when change needed? Overall: I think I mentioned it elsewhere that the best way to fix it may be to drop eps from arguments list, but it's already in a public interface...
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in a codepath such as
quant_primitives.py
which is supposed to be the canonical way these calculations are done, IMO silently modifying a passed-in argument is not something that should be landedit's understandable if you don't want to sign up for changing every callsite - in that case a good way to wrap this up could be
then someone else could pick up the fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the definition of "visible to user"? Every function that I mentioned above is visible to user in the sense it is possible for user to import the module and use the function - and all of them expose
eps
as argument, so it's plain impossible to find all call sites, let alone fix them. On the other hand, if we sayquantize_
is the only API visible to users, then I believe theeps
is not visible to users at all (not 100% sure - maybe there is a config exposing it), which meanseps
argument is considered being used by torchao internally only, and as apparently it doesn't get used the right way, the best fix is just to remove it as an argument everywhere, and keep the check added by this PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can treat "visible to the user" as "in torchao repository".
it's definitely possible to fix this for callsites inside of torchao
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please understand that I'm not into nit-picking. However, here we have plain simple case of handling an invalid argument value, and we're going into great lengths about what is in a nutshell an aesthetics argument.
The invalid
eps
value could be silently changed (and I think it's the best idea, as this change does "the right thing", i.e. makes it possible to do the quantization later, keeping the quantized range as big as possible), with or without printing a warning to the user. Alternatively, we could throw an exception. Or, we could decide that what is this all about is just a contrived corner case that most likely won't happen in practice, so we change nothing. With any of these, we're resolving this issue once and for all. On the other hand, I really don't see if I for example make the fix here (the reproducer is below), how is that going to prevent another torchao developer down the road from making this same omission when writing alike handler for a new config? So it's not that I'm lazy or whatever to do what you're suggesting, it's simply that I don't want to do that.Script to reproduce issue in case of integer quantization - the problem manifests itself here with the too coarse quantization, with the fix in this PR quantization is much better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH, I'd agree with Vasiliy, that we can do assert for args, but not change args in the function. unless
eps
is not provided by user, then we can set a reasonable defaultwe are planning on some refactors and dropping the preserve_zero and zero_point_domain args for the quant primitive ops, cc @jainapurva will work on this soon, we could drop eps as well if it makes sense, or we can set it to None majority of times.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no issues with doing assert instead of changing arguments. I've updated the PR with this approach.