Skip to content

Conversation

@li-roy
Copy link
Contributor

@li-roy li-roy commented Jun 1, 2018

closes #7929

@apaszke
Copy link
Contributor

apaszke commented Jun 1, 2018

I can see why we might want to change the C code, but do we really think that breaking backward compatibility to make people say reduce=Reduce.NONE instead of reduce=False is a good idea? It's certainly less Pythonic. The new thing is much more verbose, and the old API seemed to work quite well.

@zou3519
Copy link
Contributor

zou3519 commented Jun 1, 2018

In Python-land, maybe "reduce=None" should default to reduce=Reduce.NONE?

@li-roy
Copy link
Contributor Author

li-roy commented Jun 1, 2018

This current version won't break backwards compatibility, but I was planning to stop supporting it in a future version.

A couple motivators behind this change:

  • size_average and reduce don't act independently of each other, this seems like bad API design.
  • we want to add more support for other times of reduction, like average by dividing by batch size instead of number of elements. I don't think it's a good idea to keep adding more boolean flags

@fmassa
Copy link
Member

fmassa commented Jun 2, 2018

One more random idea: deprecate size_average and reduce, and introduce a new argument (maybe reduction? Idk)?
While I'm ok with reduce, it seems to be a special keyword in python and gets coloured differently. It might also make for a simpler deprecation path.

@apaszke
Copy link
Contributor

apaszke commented Jun 5, 2018

reduce is not a keyword, but a name of a builtin that was actually removed in Python 2 (it's no longer imported by default).

I really don't think that using enums is very user friendly. It's definitely more verbose, and very rare in Python APIs. What's the issue with the two boolean flags we have right now? Sure, there are configurations which are incompatible, but assuming we keep our current defaults, you only specify at most one of those two arguments, and no issues should arise.

@fmassa
Copy link
Member

fmassa commented Jun 5, 2018

I think there is value removing both boolean flags and keeping only a single one.
I'd say that the behavior of size_average is unexpected in a few cases, and this could be a way of fixing it without breaking backward compatibility.

I also agree that enum is not very pythonic: what about having strings instead?

@li-roy
Copy link
Contributor Author

li-roy commented Jun 5, 2018

We also want to add support for different types of averaging (dividing by batch_size instead of number of elements). Under the current scheme the only way to do that is to add a third boolean argument, which again, will only be valid if the others are in certain configurations.

I think combining the arguments makes a lot of sense if we look at the perspective of a user: Most people are going to be looking at the documentation before using any of these functions, and I think the documentation would be a simpler to understand if all these similar functionalities were under a single argument.

@ssnl
Copy link
Collaborator

ssnl commented Jun 5, 2018

I definitely have been confused by what certain combination would do, so at least in the front of making the behavior clearer to users, this certainly helps.

That said, enum indeed isn't really used in Python API, as @apaszke said. A more common pattern is to directly use strings, as we are already doing in F.pad.

@apaszke
Copy link
Contributor

apaszke commented Jun 6, 2018

Strings aren’t ideal, but they’re better than enums. I would be ok with that

@ssnl
Copy link
Collaborator

ssnl commented Jun 6, 2018 via email

@li-roy li-roy changed the title [wip] replace size_average and reduce args in loss functions with single re… combine size_average and reduce args in loss functions Jun 12, 2018
@li-roy
Copy link
Contributor Author

li-roy commented Jun 13, 2018

@pytorchbot retest this please

@ezyang
Copy link
Contributor

ezyang commented Jun 14, 2018

@pytorchbot retest this please

1 similar comment
@ezyang
Copy link
Contributor

ezyang commented Jun 14, 2018

@pytorchbot retest this please

Copy link
Collaborator

@ssnl ssnl left a comment

Choose a reason for hiding this comment

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

I added a few general questions and comments to understand the general goal of this PR. After they are resolved I can make a detailed pass.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@ssnl
Copy link
Collaborator

ssnl commented Jun 22, 2018

Please fix tests and rebase too

@li-roy li-roy force-pushed the reducearg branch 2 times, most recently from 4cfb9a7 to 2bec94f Compare June 25, 2018 23:49
@li-roy
Copy link
Contributor Author

li-roy commented Jun 26, 2018

@pytorchbot retest this please

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@ezyang
Copy link
Contributor

ezyang commented Jun 27, 2018

@pytorchbot retest this please

Copy link
Collaborator

@ssnl ssnl left a comment

Choose a reason for hiding this comment

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

Code looks great. Please prefix the enum class with _. Thanks!

@li-roy
Copy link
Contributor Author

li-roy commented Jun 27, 2018

@ssnl You're talking about Reduction in nn/functional.py, right? Should that still be prefixed if I'm using it in other files as well?

@ssnl
Copy link
Collaborator

ssnl commented Jun 27, 2018

@li-roy yes, because that's not part of public api. In other words, it shouldn't show up in user's IDE code autocompletion.

@li-roy
Copy link
Contributor Author

li-roy commented Jun 27, 2018

@ssnl Oh that makes sense now, thanks for the explanation.

@ssnl
Copy link
Collaborator

ssnl commented Jun 27, 2018 via email

@ssnl
Copy link
Collaborator

ssnl commented Jun 28, 2018

btw, if you haven't, you should rebase & import to phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@li-roy has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@li-roy has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@li-roy is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@li-roy is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@li-roy is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

zdevito pushed a commit to zdevito/ATen that referenced this pull request Jul 1, 2018
Summary:
closes #7929
Closes pytorch/pytorch#8018

Differential Revision: D8682540

Pulled By: li-roy

fbshipit-source-id: 649170dd1a7f373151c1d4e949838bd1c5651936
@li-roy li-roy deleted the reducearg branch July 2, 2018 20:53
zdevito pushed a commit to zdevito/ATen that referenced this pull request Jul 13, 2018
Summary:
closes #7929
Closes pytorch/pytorch#8018

Differential Revision: D8682540

Pulled By: li-roy

fbshipit-source-id: 649170dd1a7f373151c1d4e949838bd1c5651936
goodlux pushed a commit to goodlux/pytorch that referenced this pull request Aug 15, 2018
Summary:
closes pytorch#7929
Closes pytorch#8018

Differential Revision: D8682540

Pulled By: li-roy

fbshipit-source-id: 649170dd1a7f373151c1d4e949838bd1c5651936
facebook-github-bot pushed a commit that referenced this pull request Sep 19, 2018
Summary:
Currently one of our GPU perf tests `test_gpu_speed_mnist` reports NaN after this commit (#8018), and we didn't have the logic in place to raise error when this happens. This PR fixes the problem and will also update the baseline properly even if its previous value is NaN.
Pull Request resolved: #11588

Differential Revision: D9831798

Pulled By: yf225

fbshipit-source-id: b95eee38d69b3b8273f48b8ac7b7e0e79cf756ed
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.

Combine size_average and reduce args in Loss modules

8 participants