Skip to content

improve StrEnum #5512

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

Merged
merged 6 commits into from
Mar 2, 2022
Merged

improve StrEnum #5512

merged 6 commits into from
Mar 2, 2022

Conversation

pmeier
Copy link
Collaborator

@pmeier pmeier commented Mar 2, 2022

This adds a from_str class method to StrEnum to clean up the interface. This seems to be fully scriptable

import torch
from torchvision.prototype.utils._internal import StrEnum


class Foo(StrEnum):
    BAR = StrEnum.auto()
    BAZ = 42


@torch.jit.script
def check_str_enum(input: torch.Tensor, foo: Foo) -> torch.Tensor:
    if foo == Foo.BAR:
        return input.abs()
    elif foo == Foo.BAZ:
        return input + 1
    else:
        raise ValueError("This should not happen!")


input = torch.tensor(-1)

print("should be 1: ", check_str_enum(input, Foo.BAR))

print("should be 0: ", check_str_enum(input, Foo.from_str("BAZ")))

check_str_enum(input, Foo.from_str("CAR"))
should be 1:  tensor(1)
should be 0:  tensor(0)
ValueError: Unknown value 'CAR' for Foo. Did you mean 'BAR'?

@facebook-github-bot
Copy link

facebook-github-bot commented Mar 2, 2022

💊 CI failures summary and remediations

As of commit d990996 (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

Copy link
Contributor

@datumbox datumbox left a comment

Choose a reason for hiding this comment

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

Looks good overall but it's blocked by the dependency on the internal of prototype. See comment below. Thoughts?

@pmeier pmeier requested a review from datumbox March 2, 2022 11:39
@pmeier pmeier merged commit 71d2bb0 into pytorch:main Mar 2, 2022
@pmeier pmeier deleted the str-enum branch March 2, 2022 13:17
@github-actions
Copy link

github-actions bot commented Mar 2, 2022

Hey @pmeier!

You merged this PR, but no labels were added. The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py

facebook-github-bot pushed a commit that referenced this pull request Mar 15, 2022
Summary:
* improve StrEnum

* use StrEnum for model weights

* fix test

* migrate StrEnum to main area

Reviewed By: vmoens

Differential Revision: D34878985

fbshipit-source-id: 6a37cb694a3fcc55fa39382b59a1c7e65edacd5e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants