Graph exploration of unknown models

Hi,

is there a way to explore the graph of an unknown model in a way that we can recreate the model module by module?
I need to develop a framework that can modify unknown models.

Thank you

Hi,

Which graph are you talking about? How do you get that unknown model?

Basicaly the user should be able to provide a model found on internet or other, he supply the module class so I can instantiate the model from it, and I should be able to do some modifications on it.

Usually we represent DL models as a graph of nodes of computation, that is what I mean by the graph.
So I would like to explore the graph of computation and recreate an altered equivalent module.

Do you mean something like this?

def dfs(module:nn.Module, modify_module:Function, is_root=True) -> nn.Module:
    
    i = -1
    for i, (child_name, child_module) in enumerate(module.named_children()):
        dfs(child_module, modify_module, is_root=False)
    is_leaf = i == -1
    
    modify_module(module, is_root, is_leaf)
    
    return module

Seems like it yes!
So with this I can iterate through the graph modules and eventually create the same one or altered?

Yes, you only need to copy.deepcopy your model before calling dfs if you dont want to modify it inplace. modify_module is your function that does with a given child module what you want, for example you can add more child modules with .add_module, or modify the forward behavior by registering forward pre hooks (executed before forward call) and forward hooks (executed after forward call see here.

Awesome, thank you!
Should I edit the title to mark it as solved?

I think theres a button on replies that says something like “solved” or some button at the bottom of the post you can click