Skip to content

Warns when performing integer division with div and addcdiv#34570

Closed
mruberry wants to merge 6 commits intomasterfrom
warn_integer_div
Closed

Warns when performing integer division with div and addcdiv#34570
mruberry wants to merge 6 commits intomasterfrom
warn_integer_div

Conversation

@mruberry
Copy link
Copy Markdown
Collaborator

@mruberry mruberry commented Mar 11, 2020

UPDATE

BC-breaking release note info:

In a future PyTorch release, torch.div (including the / operator) will perform "true" division as in Python3 and NumPy. Performing integer floor division with torch.div is deprecated. To floor divide integers use torch.floor_divide, instead.

// ------- (PR info below) ------- //

Per title.

In the future we want to make div(), the division operator, and addcdiv perform true division as in Python 3, NumPy, and JAX. To do this without silently breaking users we plan to:

  • Warn (once) in 1.5 when a user performs integer division using div or addcdiv
  • RuntimeError in 1.6 when a user attempts to perform integer division using div or addcdiv
  • Always perform true division in 1.7 using div, /, and addcdiv

Users can use true_divide or floor_divide today to explicitly specify the type of division they like.

A test for this behavior is added to test_type_promotion. Unfortunately, because we are only warning once (to avoid a deluge) the test only uses maybeWarns Regex.

The XLA failure is real but will be solved by #34552. I'll be sure to land that PR first to avoid temporarily breaking the XLA build.

@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented Mar 11, 2020

💊 CircleCI build failures summary and remediations

As of commit 23d9a8b (more details on the Dr. CI page):


None of the build failures appear to be your fault 💚


  • 4/4 broken upstream at merge base eef17ed on Mar 19 from 12:27am to 2:53pm (16 commits; 2a1c838 - 7c06b86)

    Please rebase on the viable/strict branch (expand for instructions)

    Since your merge base is older than viable/strict, run these commands:

    git fetch https://github.com/pytorch/pytorch viable/strict
    git rebase FETCH_HEAD
    

    Check out the recency history of this "viable master" tracking branch.


🚧 4 upstream failures:

These were probably caused by upstream breakages:


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker.

This comment has been revised 37 times.

Comment thread aten/src/ATen/native/BinaryOps.cpp Outdated
if (isIntegralType(result.scalar_type(), /*includeBool=*/ true)) {
TORCH_WARN_ONCE(
"Integer division of tensors using div or / is deprecated. ",
"In a future release div and / will ",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: I'd just specify the expected versions, I don't see why we'd deviate from the plan and even if we do, it's not the biggest deal.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, did we talk about having a future true_division for PyTorch? (So the error message could say "or import...").

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'll put up a separate PR shortly for that behavior.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

See #34794

Comment thread aten/src/ATen/native/BinaryOps.cpp Outdated
Comment thread aten/src/ATen/native/PointwiseOps.cpp Outdated
with self.maybeWarnsRegex(UserWarning, '^Integer division.+is deprecated.+'):
c = a / b
c = torch.div(a, b)
torch.div(a, b, out=o)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: add out= tests.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'll add some more.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Hm... I'm not sure how much more it makes sense to add here, actually. Trying to perform division with float inputs and an integral out type will already throw a runtime error.

Comment thread torch/_torch_docs.py Outdated
Comment thread torch/_torch_docs.py Outdated
Comment thread aten/src/ATen/native/PointwiseOps.cpp Outdated
Comment thread test/test_type_promotion.py
@mruberry mruberry requested a review from gchanan March 18, 2020 20:56
TORCH_WARN_ONCE(
"Integer division of tensors using div or / is deprecated, ",
"and in a future release div will perform true division as in Python 3. ",
"Use true_divide or floor_divide (// in Python) instead.");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we say something about "import future"?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It would leave master in a bad state. I'll rebase #34794 once this is in and update the doc.

Copy link
Copy Markdown
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.

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

Copy link
Copy Markdown
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.

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

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@mruberry merged this pull request in 0d8447a.

facebook-github-bot pushed a commit that referenced this pull request Mar 24, 2020
Summary:
Per title. See related #34570.

In PyTorch 1.7 the plan is for torch.div and Python's division operator to perform "true" division, like Python 3, JAX, and NumPy. To facilitate this change, this PR expands true_divide to be a method so it can cover all of torch.div's use cases.

New true_divide tests are added to test_torch.py, test_type_promotion.py, and test_sparse.py.
Pull Request resolved: #34794

Differential Revision: D20545507

Pulled By: mruberry

fbshipit-source-id: 55286f819716c8823d1930441a69008560ac2bd5
@mruberry mruberry deleted the warn_integer_div branch May 16, 2020 05:00
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
…34570)

Summary:
Per title.

In the future we want to make div(), the division operator, and addcdiv perform true division as in Python 3, NumPy, and JAX. To do this without silently breaking users we plan to:

- Warn (once) in 1.5 when a user performs integer division using div or addcdiv
- RuntimeError in 1.6 when a user attempts to perform integer division using div or addcdiv
- Always perform true division in 1.7 using div, /, and addcdiv

Users can use true_divide or floor_divide today to explicitly specify the type of division they like.

A test for this behavior is added to test_type_promotion. Unfortunately, because we are only warning once (to avoid a deluge) the test only uses maybeWarns Regex.

The XLA failure is real but will be solved by pytorch#34552. I'll be sure to land that PR first to avoid temporarily breaking the XLA build.
Pull Request resolved: pytorch#34570

Differential Revision: D20529211

Pulled By: mruberry

fbshipit-source-id: 65af5a9641c5825175d029e8413c9e1730c661d0
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Per title. See related pytorch#34570.

In PyTorch 1.7 the plan is for torch.div and Python's division operator to perform "true" division, like Python 3, JAX, and NumPy. To facilitate this change, this PR expands true_divide to be a method so it can cover all of torch.div's use cases.

New true_divide tests are added to test_torch.py, test_type_promotion.py, and test_sparse.py.
Pull Request resolved: pytorch#34794

Differential Revision: D20545507

Pulled By: mruberry

fbshipit-source-id: 55286f819716c8823d1930441a69008560ac2bd5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants