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

Fix wrong is_namedtuple implementation #2475

Merged
merged 4 commits into from
Feb 26, 2024
Merged

Fix wrong is_namedtuple implementation #2475

merged 4 commits into from
Feb 26, 2024

Conversation

fxmarty
Copy link
Contributor

@fxmarty fxmarty commented Feb 21, 2024

As per title.

The current implementation does not work in case of nested inheritance. Example:

from torch import Tensor
import torch
from typing import NamedTuple, Optional
from collections import namedtuple


class QuantTensorBase(NamedTuple):
    value: Tensor
    scale: Optional[Tensor]
    zero_point: Optional[Tensor]
    bit_width: Optional[Tensor]
    signed_t: Optional[Tensor]
    training_t: Optional[Tensor]

class Second(QuantTensorBase):
    pass

a = QuantTensorBase(torch.tensor(1.), None, None, None, None, None)
b = Second(torch.tensor(1.), None, None, None, None, None)

point = namedtuple('Point', ['x', 'y'])
p = point(11, y=22)

def isnamedtupleinstance(x):
    t = type(x)
    b = t.__bases__
    print("b", b)
    if len(b) != 1 or b[0] != tuple:
        print("here")
        return False
    f = getattr(t, '_fields', None)
    if not isinstance(f, tuple):
        print("there")
        return False
    return all(type(n)==str for n in f)

print("-----")
print(isnamedtupleinstance(p))
print("-----")
print(isnamedtupleinstance(a))
print("-----")
print(isnamedtupleinstance(b))

giving

-----
b (<class 'tuple'>,)
True
-----
b (<class 'tuple'>,)
True
-----
b (<class '__main__.QuantTensorBase'>,)
here
False

Using instead https://stackoverflow.com/questions/2166818/how-to-check-if-an-object-is-an-instance-of-a-namedtuple/62692640#62692640

cc @Giuseppe5 @nickfraser

@fxmarty fxmarty requested review from BenjaminBossan, muellerzr and SunMarc and removed request for BenjaminBossan and muellerzr February 21, 2024 12:30
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@fxmarty
Copy link
Contributor Author

fxmarty commented Feb 21, 2024

pytest is bugged

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

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

Thanks for fixing the function.

It looks like the old code corresponds to this old SO answer whereas the new code corresponds to a more recent SO answer. It always pays to check more than the first SO answer :)

Since you provide code to check this, it could be added as a unit test, WDYT?

pytest is bugged

Yes, it's unrelated, should be fixed by #2461.

@fxmarty
Copy link
Contributor Author

fxmarty commented Feb 21, 2024

@BenjaminBossan added a test

Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Thanks for the fix !

@fxmarty fxmarty merged commit 9b24f56 into main Feb 26, 2024
25 checks passed
@fxmarty fxmarty deleted the fix-isnamedtuple-bug branch February 26, 2024 11:11
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.

4 participants