PiPPy icon indicating copy to clipboard operation
PiPPy copied to clipboard

problem with split_module method

Open HaiShin opened this issue 4 years ago • 2 comments

hi, When I use your example code, I found that the split_module has inconsistent number of parameters. How to solve this problem? This is your example code:

import torch
from typing import Any

class MyNetworkBlock(torch.nn.Module):
    def __init__(self, in_dim, out_dim):
        super().__init__()
        self.lin = torch.nn.Linear(in_dim, out_dim)

    def forward(self, x):
        x = self.lin(x)
        x = torch.relu(x)
        return x
   `class MyNetwork(torch.nn.Module):
    def __init__(self, in_dim, layer_dims):
        super().__init__()

        prev_dim = in_dim
        for i, dim in enumerate(layer_dims):
            setattr(self, f'layer{i}', MyNetworkBlock(prev_dim, dim))
            prev_dim = dim

        self.num_layers = len(layer_dims)
        # 10 output classes
        self.output_proj = torch.nn.Linear(layer_dims[-1], 10)

    def forward(self, x):
        for i in range(self.num_layers):
            x = getattr(self, f'layer{i}')(x)

        return self.output_proj(x)

mn = MyNetwork(512, [512, 1024, 256])

from pippy.IR import Pipe

pipe = Pipe.from_tracing(mn)
print(pipe)
print(pipe.split_gm.submod_0)

and this error is :

Traceback (most recent call last):
  File "test1.py", line 38, in <module>
    pipe = Pipe.from_tracing(mn)
  File "/var/haishin/lab/PiPPy-main/pippy/IR.py", line 833, in from_tracing
    return Pipe._from_traced(mod, traced, multi_use_param_spec, output_loss_value_spec=output_loss_value_spec)
  File "/var/haishin/lab/PiPPy-main/pippy/IR.py", line 604, in _from_traced
    split = split_module(traced, mod, split_callback, qualname_map)
TypeError: split_module() takes 3 positional arguments but 4 were given

HaiShin avatar Jun 02 '22 09:06 HaiShin

if update the torch.fx.passes.split_module?

       qualname_map : Dict[str, str] = {}
       # TODO: what does split do with module invocations? does it move the modules
       # into the submodules?
       split = split_module(traced, mod, split_callback)

I found that qualname_map variable is undefined in split_module:

@compatibility(is_backward_compatible=True)
def split_module(
    m: GraphModule,
    root_m: torch.nn.Module,
    split_callback: Callable[[torch.fx.node.Node], int],
):

HaiShin avatar Jun 02 '22 11:06 HaiShin

@HaiShin, PiPPy requires PyTorch 1.12+ where split_module with 4 arguments is available. The best option is to use PyTorch nightly build

pbelevich avatar Jun 08 '22 18:06 pbelevich