Skip to content

Conversation

@zou3519
Copy link
Contributor

@zou3519 zou3519 commented Aug 29, 2018

  • In Python 2, use of / (regardless of int/float/Tensor) causes a compiler error if
    from __future__ import division is not imported in the file.
  • The / operator is universally set to do "true" division for integers
  • Added a prim::FloorDiv operator because it is used in loop unrolling.

The error if users use '/' in python 2 without importing from future
occurs when building the JIT AST.

cc @apaszke @zdevito

@zou3519 zou3519 added the oncall: jit Add this issue/PR to JIT oncall triage queue label Aug 29, 2018

This comment was marked as off-topic.

@zdevito
Copy link
Contributor

zdevito commented Aug 30, 2018

I think the fix should be:

  • In Python 2, use of / should cause a compiler error if from __future__ import division is not imported in the file.
  • The / operator is universally set to do "true" division for integers, meaning it returns a float.
  • (optional, for now) We add the // operator support to script.

@zou3519 zou3519 force-pushed the div-py3 branch 2 times, most recently from fb3a86e to 12091d4 Compare August 31, 2018 14:26

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@zou3519
Copy link
Contributor Author

zou3519 commented Aug 31, 2018

This should be good for another review. I'm a little concerned about ONNX export though (I'm not sure if I should be) because of the following:

  • ints get exported as scalar tensors to ONNX
  • aten::div(int, int) gives a float now because / is truediv. However, aten::div(int scalar tensor, int scalar tensor) gives an int scalar tensor because of division semantics on tensors...

@jamesr66a should I be concerned?

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.

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

@zou3519
Copy link
Contributor Author

zou3519 commented Aug 31, 2018

Had offline chat with @apaszke, I am going to fix the ONNX export

Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

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

This looks good -- I have some minor comments.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

test/test_jit.py Outdated

This comment was marked as off-topic.

This comment was marked as off-topic.

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.

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

// - aten::div(int, int) -> float is the python truediv operator. This doesn't
// exist in ONNX so we express it in terms of ONNX ops.
//
TORCH_API void PrepareDivisionForONNX(const std::shared_ptr<Graph>& graph);

This comment was marked as off-topic.

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.

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

torch._C._jit_pass_lint(graph)

# onnx only supports tensors, but 1 / 2 = 0.5 and tensor(1) / tensor(2) = 0
torch._C._jit_pass_prepare_division_for_onnx(graph)

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

Copy link
Collaborator

@jamesr66a jamesr66a left a comment

Choose a reason for hiding this comment

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

Fixup the ONNX pass and add TORCH_API to Node::matches (to make windows CI pass) then this should be good to go

Value* longtensor = subgraph->insertNode(subgraph->createNumToTensor(input))->output();
// FLOAT = 1
// https://github.com/onnx/onnx/blob/6bedd27b0307c9295039bd847895a27275160a98/onnx/onnx.in.proto#L282
Node* cast = subgraph->create(onnx::Cast, {longtensor})->i_(attr::to, 1);

This comment was marked as off-topic.

@zou3519 zou3519 force-pushed the div-py3 branch 2 times, most recently from 95a42f2 to 943f4e7 Compare September 4, 2018 14:50
@ezyang
Copy link
Contributor

ezyang commented Sep 4, 2018

@pytorchbot retest this please

2 similar comments
@ezyang
Copy link
Contributor

ezyang commented Sep 4, 2018

@pytorchbot retest this please

@zou3519
Copy link
Contributor Author

zou3519 commented Sep 4, 2018

@pytorchbot retest this please

@zdevito
Copy link
Contributor

zdevito commented Sep 4, 2018

This still looks good. There are some expect file issues.

- In Python 2, use of `/` (regardless of int/float/Tensor) causes a compiler error if
  `from __future__ import division` is not imported in the file.
- The / operator is universally set to do "true" division for integers
- Added a `prim::FloorDiv` operator because it is used in loop unrolling.

The error if users use '/' in python 2 without importing from __future__
occurs when building the JIT AST.
- s/prim::FloorDiv/aten::floordiv
- Fix onnx export for true division on integers
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.

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

@zou3519 zou3519 deleted the div-py3 branch September 6, 2018 14:37
PenghuiCheng pushed a commit to PenghuiCheng/pytorch that referenced this pull request Sep 11, 2018
Summary:
- In Python 2, use of `/` (regardless of int/float/Tensor) causes a compiler error if
  `from __future__ import division` is not imported in the file.
- The / operator is universally set to do "true" division for integers
- Added a `prim::FloorDiv` operator because it is used in loop unrolling.

The error if users use '/' in python 2 without importing from __future__
occurs when building the JIT AST.

cc apaszke zdevito
Pull Request resolved: pytorch#11016

Differential Revision: D9613527

Pulled By: zou3519

fbshipit-source-id: 0cebf44d5b8c92e203167733692ad33c4ec9dac6
@ezyang ezyang added the merged label Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants