pytorch / executorch

On-device AI across mobile, embedded and edge for PyTorch
https://pytorch.org/executorch/
Other
2.09k stars 343 forks source link

Debugging to_executorch() call #1890

Open stanislawmorawski-digica opened 8 months ago

stanislawmorawski-digica commented 8 months ago

I was converting ultralytics/yolov5 from torch hub to executorch. Unfortunately one of the layers was unsupported, and the program exited with an error on this line: executorch_program: exir.ExecutorchProgramManager = edge_program.to_executorch(). The traceback was:

image

I've looked into the file being exported and this is what I saw:

class BaseModel(nn.Module):

    def _forward_once(self, x, p, v):
        for m in self.model: # nn.Sequential
            ...
            x = m(x)  # run
            ...
        return x

What is the recommended way to find out which layer in the nn.Sequential failed?

Given that:

I've ended up monkey-patching the model and unwinding the nn.Sequential by hand:

class DebugModel(nn.Module):

    def forward(self, x):
        x0 = self.model[0](x)
        x1 = self.model[1](x0)
        x2 = self.model[2](x1)
        x3 = self.model[3](x2)
        x4 = self.model[4](x3)
        x5 = self.model[5](x4)
        x6 = self.model[6](x5)
        x7 = self.model[7](x6)
        x8 = self.model[8](x7)
        x9 = self.model[9](x8)
        x10 = self.model[10](x9)
        x11 = self.model[11](x10)
        x12 = self.model[12]([x11, x6])  # [-1, 6],
        x13 = self.model[13](x12)
        x14 = self.model[14](x13)
        x15 = self.model[15](x14)
        x16 = self.model[16]([x15, x4])  # [-1, 4],
        x17 = self.model[17](x16)
        x18 = self.model[18](x17)
        x19 = self.model[19]([x18, x14])  # [-1, 14],
        x20 = self.model[20](x19)
        x21 = self.model[21](x20)
        x22 = self.model[22]([x21, x10])  # [-1, 10],
        x23 = self.model[23](x22)
        x24 = self.model[24]([x17, x20, x23])  # [17, 20, 23]

        return x24

Executing this way I've found out that 11th layer is actually nn.Upsample which is unsupported. But my question is: Is there a better way? Is there a torch.export.log() or something similar?

iseeyuan commented 8 months ago

@Olivia-liu , is it possible to print out the module stack when a node is not supported? cc @tarun292 @Jack-Khuu

hietalajulius commented 5 months ago

@stanislawmorawski-digica were you able to resolve the issue / do you have an example how you are creating the export?

liuyibox commented 1 month ago

Hi @hietalajulius @stanislawmorawski-digica , are you able to successfully export any yolo models to executorch? I have been stuck on this point for a while.

hietalajulius commented 1 month ago

Hi @liuyibox! I have not tried, I'm assuming it does not work until someone posts a working example :D