Skip to content

Conversation

@driazati
Copy link
Contributor

@driazati driazati commented Jun 4, 2019

This adds support for inferred attributes (everything except empty lists, dicts, and tuples) as well as using the PEP 526 style annotations on a class, so this eliminates the need for torch.jit.Attribute

Differential Revision: D15718537

@pytorchbot pytorchbot added the oncall: jit Add this issue/PR to JIT oncall triage queue label Jun 4, 2019
@driazati driazati requested review from eellison and suo June 4, 2019 22:27
return_type = ann_to_type(as_ann(sig.return_annotation))
return arg_types, return_type

def try_to_infer_type(value):
Copy link
Contributor

Choose a reason for hiding this comment

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

Could consider erroring here if we see a None value, and say we need an annotated type for Optionals

# Copy annotations, pull types from `__annotations__` or try to infer
# the type if possible
class_annotations = getattr(original, '__annotations__', {})
for name in dir(original):
Copy link
Contributor

Choose a reason for hiding this comment

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

doesn't dir(value) gets a ton of arbitrary python implementation details ? these are all nn.Modules, we could try register a hook in setattr or something else

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I know we don't really have any other choice since things like __dict__ only have the current class' info (not anything from superclasses), plus not everything goes through __setattr__ (e.g. static members). This is mostly safe since the only things it takes are types it can infer, so it would pick up the __module__ and __doc__

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like it would also pick up _version inplace dump_patches

Copy link
Contributor

Choose a reason for hiding this comment

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

why doens't it work if you add it here ?

object.__setattr__(self, name, value)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That'd be mostly fine but if something is defined at the class level it doesn't go through __setattr__, it's just on the class type itself, we need to copy those too (including any up the inheritance chain) so it doesn't work generally

@eellison
Copy link
Contributor

eellison commented Jun 7, 2019

you re-requested review but my existing comments are unaddressed

@pytorchbot pytorchbot added module: build Build system issues module: ci Related to continuous integration module: docs Related to our documentation, both in docs/ and docblocks module: internals Related to internal abstractions in c10 and ATen labels Jun 10, 2019
@driazati driazati changed the base branch from driazati/apimdo to master June 10, 2019 18:24
@pytorchbot pytorchbot added the module: pybind Related to our Python bindings / interactions with other Python libraries label Jun 13, 2019
@driazati driazati changed the base branch from master to driazati/refactor_pybind June 13, 2019 00:12
@suo
Copy link
Member

suo commented Jun 14, 2019

is this ready for re-review?

@driazati
Copy link
Contributor Author

@suo Yeah it's ready

ssnl pushed a commit to ssnl/pytorch that referenced this pull request Jun 15, 2019
Summary:
This refactors pybind_utils so we can have all our type-inferring stuff in
1 place (e.g. for pytorch#21379)

There is some follow up work to make the error messages better, but I think that's fine to save for another PR.
](https://our.intern.facebook.com/intern/diff/15727002/)
Pull Request resolved: pytorch#21550

Pulled By: driazati

Differential Revision: D15727002

fbshipit-source-id: a6974f2e1e5879f0503a18efc138da31cda7afa2
@driazati driazati changed the base branch from driazati/refactor_pybind to master June 15, 2019 00:31
@driazati driazati changed the base branch from master to driazati/refactor_pybind June 15, 2019 00:37
@driazati driazati changed the base branch from driazati/refactor_pybind to master June 15, 2019 00:38
@pytorchbot pytorchbot added the module: tests Issues related to tests (not the torch.testing module) label Jun 15, 2019
@driazati driazati changed the base branch from master to driazati/refactor_pybind June 15, 2019 00:39
@driazati driazati changed the base branch from driazati/refactor_pybind to master June 15, 2019 00:40
@suo
Copy link
Member

suo commented Jun 17, 2019

looks like there is a py2 error, please resolve that before landing

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.

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

@facebook-github-bot
Copy link
Contributor

@driazati merged this pull request in afad3e4.

@facebook-github-bot facebook-github-bot deleted the driazati/attrs branch July 13, 2020 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: build Build system issues module: ci Related to continuous integration module: docs Related to our documentation, both in docs/ and docblocks module: internals Related to internal abstractions in c10 and ATen module: pybind Related to our Python bindings / interactions with other Python libraries module: tests Issues related to tests (not the torch.testing module) oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants