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 functional::smooth_l1_loss signatures to not override beta #109798

Closed
wants to merge 1 commit into from

Conversation

kurtamohler
Copy link
Collaborator

@kurtamohler kurtamohler commented Sep 21, 2023

This splits nn::functional::smooth_l1_loss into two different signatures in order to keep backward compatibility for calling the function like smooth_l1_loss(input, target, /*reduction=*/..., /*beta=*/...)

Fixes #70163

cc @ezyang @gchanan @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki

@kurtamohler kurtamohler added module: nn Related to torch.nn release notes: nn release notes category labels Sep 21, 2023
@pytorch-bot
Copy link

pytorch-bot bot commented Sep 21, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/109798

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit 38edd66 with merge base 733368a (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

Copy link
Contributor

@mikaylagawarecki mikaylagawarecki left a comment

Choose a reason for hiding this comment

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

LGTM

@mikaylagawarecki mikaylagawarecki added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Sep 21, 2023
inline Tensor smooth_l1_loss(
const Tensor& input,
const Tensor& target,
SmoothL1LossFuncOptions::reduction_t reduction,
Copy link
Contributor

Choose a reason for hiding this comment

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

actually, should this be just const SmoothL1LossFuncOptions& options = {} to keep BC?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

smooth_l1_loss(..., const SmoothL1LossFuncOptions& options = {}, double beta) doesn't build because a default argument cannot be followed by a non-default argument.

smooth_l1_loss(..., const SmoothL1LossFuncOptions& options, double beta) does build, but it's semantically ambiguous because beta is given in two places. For instance, what should happen if it's called like smooth_l1_loss(input, target, SmoothL1LossFuncOptions().beta(0.5), /*beta=*/0.1)?

With the signatures in this PR, it's no longer possible to call the function in an ambiguous way. In that sense, it is BC-breaking. However, I would think that BC does not actually matter for ambiguous cases. Maybe I'm mistaken though

We do keep BC for the non-ambiguous case smooth_l1_loss(input, target, /*reduction=*/..., /*beta=*/...). There is an existing test for this in test/cpp/api/functional.cpp

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Although if we do break BC like this to remove the ambiguous case, maybe we would still need a deprecation cycle. torch_extensions builds in CI are failing from my change:

2023-09-21T18:54:43.9035006Z /var/lib/jenkins/.cache/torch_extensions/py311_cpu/functional_impl_check/main.cpp:1900:21: error: no matching function for call to 'smooth_l1_loss'
2023-09-21T18:54:43.9035367Z   auto cpp_output = F::smooth_l1_loss(
2023-09-21T18:54:43.9035584Z                     ^~~~~~~~~~~~~~~~~
2023-09-21T18:54:43.9036468Z /opt/conda/envs/py_3.11/lib/python3.11/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/loss.h:400:15: note: candidate function not viable: no known conversion from 'torch::nn::SmoothL1LossOptions' to 'SmoothL1LossFuncOptions::reduction_t' (aka 'variant<enumtype::kNone, enumtype::kMean, enumtype::kSum>') for 3rd argument
2023-09-21T18:54:43.9037033Z inline Tensor smooth_l1_loss(

Perhaps a better strategy is to go with smooth_l1_loss(..., const SmoothL1LossFuncOptions& options, double beta) after all, but just error out in ambiguous case where both beta parameters are set like smooth_l1_loss(input, target, SmoothL1LossFuncOptions().beta(0.5), /*beta=*/0.1). This should make the failing builds pass again. In order to do this though, we would need to change the beta field in SmoothL1LossFuncOptions from double to optional<double>, so we can detect if it was explicitly set or not. Is this alright with you?

This is still technically BC-breaking in the ambiguous case, but that case has to be broken in order to address the issue

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Perhaps a better strategy is to go with smooth_l1_loss(..., const SmoothL1LossFuncOptions& options, double beta) after all, but just error out in ambiguous case where both beta parameters are set like smooth_l1_loss(input, target, SmoothL1LossFuncOptions().beta(0.5), /*beta=*/0.1). This should make the failing builds pass again. In order to do this though, we would need to change the beta field in SmoothL1LossFuncOptions from double to optional<double>, so we can detect if it was explicitly set or not.

I've changed the PR to do this

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@mikaylagawarecki, are these changes alright with you?

@kurtamohler kurtamohler force-pushed the smooth_l1_loss-beta branch 2 times, most recently from c6204e5 to 95511ed Compare September 25, 2023 16:44
@mikaylagawarecki mikaylagawarecki added the module: bc-breaking Related to a BC-breaking change label Oct 4, 2023
@mikaylagawarecki mikaylagawarecki added topic: bc breaking topic category release notes: cpp release notes category and removed topic: bc_breaking module: bc-breaking Related to a BC-breaking change release notes: nn release notes category labels Oct 4, 2023
Copy link
Contributor

@mikaylagawarecki mikaylagawarecki left a comment

Choose a reason for hiding this comment

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

Apologies for the delay, I think the BC-break for the C++ API for the type of beta is ok in this case as it is necessary to resolve the issue of beta being passed in both the options argument and as its own argument which is an unfortunate bug

@kurtamohler kurtamohler force-pushed the smooth_l1_loss-beta branch 4 times, most recently from e589a0b to 4927e5f Compare October 9, 2023 19:17
@kurtamohler
Copy link
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 11, 2023
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/trunk Trigger trunk jobs on your pull request Merged module: nn Related to torch.nn open source release notes: cpp release notes category topic: bc breaking topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C++ functional::smooth_l1_loss overrides beta in options with default
4 participants