Skip to content
This repository was archived by the owner on Oct 31, 2023. It is now read-only.
This repository was archived by the owner on Oct 31, 2023. It is now read-only.

metaclass issue with fastai #113

Open

Description

@shayanfazeli

Minimum running example for the error:

import  tsai.models.XResNet1d
import torch
import torch.nn
import ipdb

model = tsai.models.XResNet1d.xresnet1d50(c_in=1024, c_out=512)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
with higher.innerloop_ctx(
    model,
    optimizer,
    copy_initial_weights=False,
    track_higher_grads=True) as (fnet, diffopt):
    ipdb.set_trace()

The issue:

lib/python3.9/site-packages/higher-0.2.1-py3.9.egg/higher/patch.py in _make_functional(module, params_box, params_offset, root_patched)
    217 
    218     # type checking of next line disabled as mypy is iffy with dynamic types
--> 219     class MonkeyPatched(_ModuleType, _MonkeyPatchBase):  # type: ignore
    220         __metaclass__=makecls()
    221         _wrapped_name = type(module).__name__

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Activity

  1. changed the title [-]metaclass issue[/-] [+]metaclass issue with tsai[/+] on Aug 3, 2021
  2. changed the title [-]metaclass issue with tsai[/-] [+]metaclass issue with fastai[/+] on Aug 3, 2021
  3. shayanfazeli commented on Aug 3, 2021

    @shayanfazeli
    Author

    It appears that this trick is causing the issue, redefining the individual blocks (such as ResBlock) inheriting from torch.nn.Module seems to get rid of the error.

  4. Stanwang1210 commented on Mar 2, 2023

    @Stanwang1210

    I met the same issue when defining my own metaclass. Can you explain to me how to deal with this issue?
    I don't quite understand the trick mentioned above.

  5. jeevster commented on Mar 7, 2023

    @jeevster

    I'm also seeing the same issue with a custom model which is a torch.nn.Module. I didn't understand the trick mentioned above. Any suggestions?

  6. Stanwang1210 commented on Mar 8, 2023

    @Stanwang1210

    I'm also seeing the same issue with a custom model which is a torch.nn.Module. I didn't understand the trick mentioned above. Any suggestions?

    It seems like we can't use higher on model which inherits custom metaclass. I solve this issue by remove the meta class of my model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions