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

Torch jit bug #147

Open
vodan37 opened this issue Jan 9, 2025 · 0 comments
Open

Torch jit bug #147

vodan37 opened this issue Jan 9, 2025 · 0 comments

Comments

@vodan37
Copy link

vodan37 commented Jan 9, 2025

Can't use torch.jit.trace + torch.jit.script.
Error:
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

Reproduce:

import torch
import torch.nn as nn
from fastervit import create_model

class FasterVit(nn.Module):
    def __init__(
        self,
        encoder_name="faster_vit_0_any_res",
        classes: int = 1,
        window_size=[7, 7, 12, 6],
        height = 512,
        width = 512,
        dim = 64,
        pretrained=True,
        **kwargs,
    ) -> None:
        super().__init__()
        self.model = create_model(
            encoder_name, 
            resolution=[height, width],
            window_size=window_size,
            ct_size=2,
            dim=dim,
            pretrained=pretrained,
        )

        self.model.head = nn.Linear(self.model.head.in_features, classes)

    def forward(self, x):
        return self.model(x)

model = FasterVit()

class MyModule(nn.Module):
    def __init__(
        self, 
        model,
        classes,
        # thresholds,
        height,
        width,
    ):
        super().__init__()
        self.classes = classes
        self.model = model.eval()
        self.classes = classes
        self.height = height
        self.width = width
        self.model = torch.jit.trace(self.model, torch.rand(1, 3, height, width))

    def forward(self, input):
        return self.model(input)

my_script_module = torch.jit.script(
    MyModule(
        model.to("cpu"),
        classes=["1", "2"],
        height=512,
        width=512,
    )
)

device = "cuda:0"

my_script_module.to(device)
example_input = torch.rand((1, 3, 512, 512)).to(device)
my_script_module(example_input)
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

1 participant