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

Cast inputs type to dictionary. #2585

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ostrowskimarcin
Copy link
Contributor

I run some tests of hf_clip model using torchbench.py (https://github.com/pytorch/pytorch/blob/main/benchmarks/dynamo/torchbench.py). This script at one point clones input data with custom function (https://github.com/pytorch/pytorch/blob/main/torch/_dynamo/utils.py, L1926):

def clone_inputs(example_inputs):
    res: Union[dict[Any, Any], list[Any]]
    if type(example_inputs) is dict:
        res = dict(example_inputs)
        for key, value in res.items():
            if isinstance(value, tuple):
                res[key] = clone_inputs(value)
            else:
                assert isinstance(value, torch.Tensor), type(value)
                res[key] = clone_input(value)
        return res

    res = list(example_inputs)
    for i in range(len(res)):
        if isinstance(res[i], torch.Tensor):
            res[i] = clone_input(res[i])
    return res

hf_clip model generates example data using 'CLIPProcessor' class imported from transformers repo. We can use this data as a dictionary, but officialy its type is BatchEncoding (<class 'transformers.tokenization_utils_base.BatchEncoding'>). When clone_inputs() compares input types it doesn't see it as a dict type. Hence, it casts the data to list and returns only its keys leading to error: AttributeError: 'str' object has no attribute 'shape'

I proposed simple casting to dict in model's init script. This fixed the issue and doesn't affect other test scripts.

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.

2 participants