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

Bug in smooth_quant.py #1265

Closed
MatteoPagliani opened this issue Sep 19, 2023 · 1 comment
Closed

Bug in smooth_quant.py #1265

MatteoPagliani opened this issue Sep 19, 2023 · 1 comment

Comments

@MatteoPagliani
Copy link

MatteoPagliani commented Sep 19, 2023

Hi, I think there's a bug affecting both get_module and set_module methods in neural_compressor/adaptor/torch_utils/smooth_quant.py.

The original methods are:

def get_module(model, key):
    """Get module from model by key name.

    Args:
        model (torch.nn.Module): original model
        key (str): module name to be replaced
    """
    attrs = key.split(".")
    module = model
    for attr in attrs:
        try:
            attr = int(attr)
            module = module[attr]
        except:
            module = getattr(module, attr)
    return module


def set_module(model, key, new_module):
    """Set new module into model by key name.

    Args:
        model (torch.nn.Module): original model
        key (str): module name to be replaced
        new_module (torch.nn.Module): new module to be inserted
    """
    attrs = key.split(".")
    module = model
    for attr in attrs[:-1]:
        try:
            attr = int(attr)
            module = module[attr]
        except:
            module = getattr(module, attr)
    setattr(module, attrs[-1], new_module)

When the execution goes into the except blocks, getattr() raises this error: "TypeError: getattr(): attribute name must be string". I think this is due to the fact that attr got cast to int in the try blocks. So, I propose the following modifications:

def get_module(model, key):
    """Get module from model by key name.

    Args:
        model (torch.nn.Module): original model
        key (str): module name to be replaced
    """
    attrs = key.split(".")
    module = model
    for attr in attrs:
        try:
            module = module[int(attr)]
        except:
            module = getattr(module, attr)
    return module


def set_module(model, key, new_module):
    """Set new module into model by key name.

    Args:
        model (torch.nn.Module): original model
        key (str): module name to be replaced
        new_module (torch.nn.Module): new module to be inserted
    """
    attrs = key.split(".")
    module = model
    for attr in attrs[:-1]:
        try:
            module = module[int(attr)]
        except:
            module = getattr(module, attr)
    setattr(module, attrs[-1], new_module)

In this way I'm able to run my code without errors. Let me know what you think. Thanks.

@xin3he
Copy link
Contributor

xin3he commented Sep 20, 2023

Hi @MatteoPagliani Thank you for raising it. Actually we don't need to use int. I will remove the try except and use getattr only. Please contact me if you have more concerns.

def fetch_module(model, op_name):

image

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

No branches or pull requests

3 participants