Skip to content

Optional ScalarType support for native functions & JIT#15154

Closed
tugrulates wants to merge 2 commits intopytorch:masterfrom
tugrulates:optional-scalartype-base
Closed

Optional ScalarType support for native functions & JIT#15154
tugrulates wants to merge 2 commits intopytorch:masterfrom
tugrulates:optional-scalartype-base

Conversation

@tugrulates
Copy link

For #6593 and #9515

This completes the support for optional in native, JIT and autograd.

Note: Mostly following the existing implementation for optional that was added in #12582.

This PR introduces a way to make functions accept an optional dtype and it will unblock #9515 by allowing the dtype param for type promotion interface:

func: name(inputs, *, ScalarType? dtype=None, Casting casting=same_kind)

An alternative approach could have been using ScalarType::Undefined for the same purpose but without optional, though it would have been a bit hacky.

func: name(inputs, *, ScalarType dtype=Undefined, Casting casting=same_kind)

Here's an example use of this in action: 971f69e

There are already a bunch of native functions that were getting optional dtype through function overloading. #15133 is the attempt to migrate all of those. I will send those changes separately after this since some functions (e.g. sum) need quite a bit of change in the codebase. See the commits over there.

@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label Dec 13, 2018
@tugrulates tugrulates changed the title Optional ScalarType Optional ScalarType support for native functions & JIT Dec 13, 2018
else if(n->kind().is_prim()){
switch(n->kind()){
case prim::Constant:
case prim::None:
Copy link
Author

Choose a reason for hiding this comment

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

This just tells to copy None values in JIT graph to batch JIT graph as is.

{"Storage", ParameterType::STORAGE},
{"PyObject*", ParameterType::PYOBJECT},
{"ScalarType", ParameterType::SCALARTYPE},
{"optional<ScalarType>", ParameterType::SCALARTYPE},
Copy link
Author

Choose a reason for hiding this comment

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

This wasn't used and not needed.

@tugrulates tugrulates requested a review from zdevito December 13, 2018 19:43
if arg.get('default') is not None:
default = arg['default']
if default == 'nullptr' or default == 'nullopt' or default == '{}':
if default == 'nullptr' or default == 'nullopt' or default == 'c10::nullopt' or default == '{}':
Copy link
Contributor

Choose a reason for hiding this comment

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

why do you need c10::nullopt? where does that come from?

Copy link
Author

Choose a reason for hiding this comment

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

I remember JIT was generating that (register_aten_ops) last week. It doesn't seem to be needed any more, so removed this.

Copy link
Author

Choose a reason for hiding this comment

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

Ah, this was still needed. It's added in

return 'c10::nullopt'

I will either revert this or find a different fix in the next PR.

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.

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

@zou3519
Copy link
Contributor

zou3519 commented Dec 14, 2018

How hard is it to just use optional<ScalarType> instead of ScalarType??

Copy link
Contributor

@zou3519 zou3519 left a comment

Choose a reason for hiding this comment

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

Is there an easy way to add a test?

I don't know enough about this part of the JIT to comment on the correctness but it looks very similar to the original PR that added optional type. Maybe @wanchaol or @zdevito can take a look here?

Copy link
Collaborator

@wanchaol wanchaol left a comment

Choose a reason for hiding this comment

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

Thank for adding this! the PR LGTM. Can you add a test in JIT to test a op that takes Optional[ScalarType], pass a None into it and see if it works end to end?

@zasdfgbnm zasdfgbnm mentioned this pull request Dec 14, 2018
@zasdfgbnm
Copy link
Collaborator

zasdfgbnm commented Dec 14, 2018

Could we add test for this by creating a dummy aten ops on native_functions.yaml to use these features, and test it on test_jit.py by using these ops? Sounds ugly, but easy to implement. I'm adding support for optional<int64_t> in #15235 and looking for a good way to test my code too.

@tugrulates
Copy link
Author

I'm about to convert ? suffix in type system to optional. Will add a test for optional<Scalar> in test_jit soon here.

@tugrulates
Copy link
Author

Added another commit which converts all optional types except Tensor? from T? to optional<T>. cc @gchanan

Why leave Tensor out? Tensor? accepts undefined Tensors. There's a bunch of TODOs in the codebase to clean this up. I added a few more TODOs into locations that needs cleanup too.

Why not wait for Tensor? to be cleaned up? More and more needs for optional types and it's becoming harder to clean in ? suffix format.

#12582 added optional support by following Tensor? syntax. This PR now changes the syntax to use optional<>. @wanchaol @zdevito was there any discussion around this around that time? Any specific reason the optional<> syntax can horribly fail?

In any case, I can still drop the last commit and use ScalarType? for the original need in this PR.

@tugrulates
Copy link
Author

Existing JIT tests that use optional<Device> and optional<Scalar> should cover this end to end. I updated the expect files wherever needed. PTAL

There is currently no op using optional<ScalarType>. I have a stack of conversion here:
https://github.com/tugrulates/pytorch/commits/optional-scalartype

I'm holding off on sending them before this PR is resolved. I plan to add a JIT test for optional<ScalarType> with the first op that uses it.

@wanchaol
Copy link
Collaborator

wanchaol commented Dec 14, 2018

Added another commit which converts all optional types except Tensor? from T? to optional<T>. cc @gchanan

Why leave Tensor out? Tensor? accepts undefined Tensors. There's a bunch of TODOs in the codebase to clean this up. I added a few more TODOs into locations that needs cleanup too.

Why not wait for Tensor? to be cleaned up? More and more needs for optional types and it's becoming harder to clean in ? suffix format.

#12582 added optional support by following Tensor? syntax. This PR now changes the syntax to use optional<>. @wanchaol @zdevito was there any discussion around this around that time? Any specific reason the optional<> syntax can horribly fail?

In any case, I can still drop the last commit and use ScalarType? for the original need in this PR.

I don't see the real value of migrating from ? to optional<> syntax in schema. ? is from the schema side, it is just a short hand of the optional<> syntax is c++ side, they mean the same thing. It was originally designed to use ? to denote the optional types (as the same with bunch of other typed languages), it is just that Tensor? have slightly different meaning because of old TH behaviors. it is also good to keep the schema simple and easier to understand.

Also Tensor? will always accept undefined tensor and this might not going to have any changes, we will not have optional<Tensor> afaik. Keeping the same and one syntax for those kind of stuff will reduce the mental overhead as well.

@tugrulates
Copy link
Author

Reverted the last commit that changes the schema syntax to optional<T>. I will add the e2e tests with the next PR that migrates the ops to use this (#15133). This one should be ready to go.

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.

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

Copy link
Collaborator

@wanchaol wanchaol left a comment

Choose a reason for hiding this comment

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

Nice

@tugrulates tugrulates deleted the optional-scalartype-base branch December 19, 2018 18:58
@zasdfgbnm zasdfgbnm mentioned this pull request Jan 7, 2019
@ezyang ezyang added the merged label Jun 25, 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.

7 participants