Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Fix type checking to be compatible with named tuples #3986

Merged
merged 4 commits into from
Apr 14, 2022

Conversation

artbataev
Copy link
Collaborator

@artbataev artbataev commented Apr 13, 2022

Signed-off-by: Vladimir Bataev [email protected]

What does this PR do ?

Fix inner type checking system for outputs to be compatible with named tuples.
This adds the ability to use named tuples (typing.NamedTuple or collections.namedtuple) for outputs. This is especially useful when the number of output elements is large.
Named tuples are compatible with simple tuples, there should be no problems with mixing them.

Usage

class OutputType(NamedTuple):
    y: torch.Tensor
    z: torch.Tensor

class MultipleOutputTypesWithNamedTuple(Typing):
    @property
    def output_types(self):
        return {"y": NeuralType(('B',), ElementType()), "z": NeuralType(('B',), ElementType())}

    @typecheck()
    def __call__(self, x):
        y = x + 1
        z = x + 2
        return OutputType(y=y, z=z)

obj = MultipleOutputTypesWithNamedTuple()
result = obj(x=torch.zeros(10))
result_y, result_z = result  # can unpack or use `result.y`, `result.z`

PR Type:

  • New Feature
  • Bugfix
  • Documentation

Who can review?

@titu1994, @okuchaiev, @ericharper

Copy link
Collaborator

@titu1994 titu1994 left a comment

Choose a reason for hiding this comment

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

Fantastic ! Merge when ready

Copy link
Collaborator

@ericharper ericharper left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@ericharper ericharper merged commit d4408cc into NVIDIA:main Apr 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants