Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export to JIT script #46

Open
OnceJune opened this issue Sep 8, 2023 · 4 comments
Open

Export to JIT script #46

OnceJune opened this issue Sep 8, 2023 · 4 comments

Comments

@OnceJune
Copy link

OnceJune commented Sep 8, 2023

Hi, I tried to export the model to JIT script but got this error:
reversed(Tensor 0) -> (Tensor 0):
Expected a value of type 'Tensor' for argument '0' but instead found type 'torch.torch.nn.modules.container.ModuleList
Seems JIT doesn't not support reversed, how to solve this?

@p0p4k
Copy link
Owner

p0p4k commented Sep 8, 2023

Does this help?

@OnceJune
Copy link
Author

OnceJune commented Sep 8, 2023

seems not the same issue. you can try below scripts:

import torch
import torch.nn as nn
import torch.jit as jit

class ReverseModule(nn.Module):
    def __init__(self):
        super(ReverseModule, self).__init__()
        self.layers = nn.ModuleList([nn.Linear(10, 10) for _ in range(5)])

    def forward(self, x):
        reversed_layers = list(reversed(self.layers))
        for layers in reversed_layers:
            x = layer(x)
        return x

model = ReverseModule()
scripted_module = torch.jit.script(model)

Where the reversed_layers = list(reversed(self.layers)) is the same as

flows = list(reversed(self.flows))

Torch.jit does not have support for reversed()

@p0p4k
Copy link
Owner

p0p4k commented Sep 8, 2023

Will something like this work for flows?

import torch
import torch.nn as nn
import torch.jit as jit

class ReverseModule(nn.Module):
    def __init__(self):
        super(ReverseModule, self).__init__()
        self.layers = nn.ModuleList([nn.Linear(10, 10) for _ in range(5)])
        # convert to a mutable dict
        self.layers = nn.ModuleDict({str(i): layer for i, layer in enumerate(self.layers)})
        self.reversed_layers = nn.ModuleDict()
        for i in range(len(self.layers) - 1, -1, -1):
            self.reversed_layers[str(i)] = self.layers[str(i)]

    def forward(self, x):
        for k, v in self.reversed_layers.items():
            x = v(x)
        return x

model = ReverseModule()
scripted_module = torch.jit.script(model)

@EmreOzkose
Copy link

Hi @OnceJune , what are your versions of pytorch and repo ? I am trying to export jit and faces lots of issues like typing, missing module issues, etc..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants