Skip to content

Resolve builtins using a dict rather than by name#10927

Closed
zdevito wants to merge 1 commit intopytorch:masterfrom
zdevito:pr/better_builtin_resolve
Closed

Resolve builtins using a dict rather than by name#10927
zdevito wants to merge 1 commit intopytorch:masterfrom
zdevito:pr/better_builtin_resolve

Conversation

@zdevito
Copy link
Contributor

@zdevito zdevito commented Aug 28, 2018

Changes the approach for resolving builtin ops so that the following works

add = torch.add
@script
def foo(x):
  return add(x, x)

This handles cases when people alias torch and torch.nn.functional to
shorter names.

This works by building a table of id -> builtin name for the know builtin
ops in torch, torch.nn.functional, and for any user-defined
op created by accessing in torch.ops.foo.bar

This allows us to clean up many SugaredValue types in the compiler.

Notes:

  • we now consider any attributes on python modules to be constants
    (e.g. math.pi, and torch.double).
  • fixes a bug where we incorrectly allowed attribute lookup on arbitrary
    pyton objects. It is now restricted to modules only.

@zdevito zdevito added the oncall: jit Add this issue/PR to JIT oncall triage queue label Aug 28, 2018
@zdevito
Copy link
Contributor Author

zdevito commented Aug 28, 2018

Note: this doesn't attempt to change the fact that not all things in torch.nn.functional are precisely the same thing as the builtin op. It preserves the behavior we had before this patch where we assumed torch.nn.functional.foo means the op aten::foo. In the future we can built the _builtins_table more accurately.

@zdevito zdevito force-pushed the pr/better_builtin_resolve branch from 8432852 to b73318d Compare August 28, 2018 06:07
Changes the approach for resolving builtin ops so that the following works

```
add = torch.add
@script
def foo(x):
  return add(x, x)
```

This handles cases when people alias torch and torch.nn.functional to
shorter names.

This works by building a table of id -> builtin name for the know builtin
ops in torch, torch.nn.functional, and for any user-defined
op created by accessing in torch.ops.foo.bar

This allows us to clean up many SugaredValue types in the compiler.

Notes:
* we now consider any attributes on python modules to be constants
(e.g. math.pi, and torch.double).
* fixes a bug where we incorrectly allowed attribute lookup on arbitrary
pyton objects. It is now restricted to modules only.
@zdevito zdevito force-pushed the pr/better_builtin_resolve branch from b73318d to 49107d5 Compare August 28, 2018 06:12
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.

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

Copy link
Contributor

@apaszke apaszke left a comment

Choose a reason for hiding this comment

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

Minor nits, because you're shadowing builtins unnecessarily. LGTM.

return toSugaredValue(member, m, loc);
// note: is_constant = true because we consider that global properties
// on modules like math.pi or torch.float to be constants
// eventhough it is possible, though rare, for someone to mutate them

This comment was marked as off-topic.

This comment was marked as off-topic.



def _register_builtin(callable, op):
_get_builtin_table()[id(callable)] = op

This comment was marked as off-topic.

This comment was marked as off-topic.

PenghuiCheng pushed a commit to PenghuiCheng/pytorch that referenced this pull request Sep 11, 2018
Summary:
Changes the approach for resolving builtin ops so that the following works

```
add = torch.add
script
def foo(x):
  return add(x, x)
```

This handles cases when people alias torch and torch.nn.functional to
shorter names.

This works by building a table of id -> builtin name for the know builtin
ops in torch, torch.nn.functional, and for any user-defined
op created by accessing in torch.ops.foo.bar

This allows us to clean up many SugaredValue types in the compiler.

Notes:
* we now consider any attributes on python modules to be constants
(e.g. math.pi, and torch.double).
* fixes a bug where we incorrectly allowed attribute lookup on arbitrary
pyton objects. It is now restricted to modules only.
Pull Request resolved: pytorch#10927

Differential Revision: D9527522

Pulled By: zdevito

fbshipit-source-id: 0280422af08b4b0f48f302766d5a9c0deee47660
@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.

4 participants