ncnn icon indicating copy to clipboard operation
ncnn copied to clipboard

[pnnx] convert error when have nn.Parameters, example provided

Open luohao123 opened this issue 2 years ago • 0 comments

error:

foldable_constant output_mean.1
libc++abi: terminating with uncaught exception of type c10::Error: Tensors of type TensorImpl do not have sizes
Exception raised from sizes_custom at /Users/runner/work/pytorch/pytorch/pytorch/c10/core/TensorImpl.cpp:416 (most recent call first):
frame #0: c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 92 (0x104fa7a1c in libc10.dylib)

Here is the pytorch model I want to convet:


import torch
import torch.nn as nn
import torch.nn.functional as F
from packaging import version
import os


class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

        self.fc1 = nn.Linear(864, 256)
        self.fc2 = nn.Linear(256, 140)
        self.lnw1 = self._build_params([8, 512, 140])

    def _build_params(self, shape):
        return nn.Parameter(torch.randn(shape))

    def forward(self, x, ph):
        x = self.fc1(x)
        x = F.elu(x)
        x = self.fc2(x)
        x = F.elu(x)
        
        ph = ph.unsqueeze(-1).unsqueeze(-1)
        print_shape(x)
        lpn_w1 = torch.sum(self.lnw1 * ph, dim=1)

        print_shape(lpn_w1, x)
        lpn_h1 = torch.einsum("bij,bj->bi", lpn_w1, x)
        return lpn_h1

def to_np_bin(data, bin_f):
    if isinstance(data, torch.Tensor):
        data = data.numpy()
    data.tofile(bin_f)


def test():
    torch.set_grad_enabled(False)
    torch.manual_seed(1024)


    net = Model()
    net.eval()

    x = torch.ones(1, 864)
    ph = torch.ones(1, 8)

    a = net(x, ph)
    print(a)

    # export torchscript
    mod = torch.jit.trace(net, [x, ph])
    save_f = "test_simple_fc.pt"
    mod.save(save_f)

    a = mod(x, ph)
    print(a[:, :40])
    print(a.shape)

    to_np_bin(a, 'data.bin')
    to_np_bin(x, 'in.bin')


if __name__ == "__main__":
    if test():
        exit(0)
    else:
        exit(1)


when convert pnnx got error above.

Be note that, this will got at any scenarios when I have nn.Parameters (tested with another more huge model same error).

Please take a look and give some advice how to fix

luohao123 avatar Aug 09 '22 14:08 luohao123