Triton Torchscript 예제 모델

임정민·2025년 2월 18일

메모장

목록 보기
26/34
post-thumbnail
import torch
import torch.nn as nn
import torch.nn.functional as F

# 간단한 CNN 모델 정의
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

# 모델 인스턴스 생성
net = Net()

# 모델을 eval 모드로 설정
net.eval()

# 예제 입력 생성 (CIFAR-10 이미지 크기: 3x32x32)
example = torch.rand(1, 3, 32, 32)

# TorchScript로 변환
traced_script_module = torch.jit.trace(net, example)

# 모델 저장
traced_script_module.save("model.pt")
profile
https://github.com/min731

0개의 댓글