diffusers/tools
1128
1#!/usr/bin/env python32import torch3import torch.nn as nn4import torch.nn.functional as F5 6 7class SuperConv(nn.Conv2d):8 9 def __init__(self, *args, is_lora=False, **kwargs):10 super().__init__(*args, **kwargs)11 12 self.is_lora = is_lora13 14 def forward(self, *args, **kwargs):15 if self.is_lora:16 return 3 + super().forward(*args, **kwargs)17 else:18 return super().forward(*args, **kwargs)19 20# Define a simple Convolutional Neural Network21class SimpleCNN(nn.Module):22 def __init__(self):23 super(SimpleCNN, self).__init__()24 self.conv1 = SuperConv(3, 6, 5) # Assuming input images are RGB, so 3 input channels25 self.pool = nn.MaxPool2d(2, 2)26 self.conv2 = SuperConv(6, 16, 5)27 self.fc1 = nn.Linear(16 * 5 * 5, 120)28 self.fc2 = nn.Linear(120, 84)29 self.fc3 = nn.Linear(84, 10)30 31 def forward(self, x):32 x = self.pool(F.relu(self.conv1(x)))33 x = self.pool(F.relu(self.conv2(x)))34 x = x.view(-1, 16 * 5 * 5)35 x = F.relu(self.fc1(x))36 x = F.relu(self.fc2(x))37 x = self.fc3(x)38 return x39 40# Create the network41net = SimpleCNN()42 43# Initialize weights with dummy values44for m in net.modules():45 if isinstance(m, nn.Conv2d):46 nn.init.constant_(m.weight, 0.1)47 nn.init.constant_(m.bias, 0.1)48 elif isinstance(m, nn.Linear):49 nn.init.constant_(m.weight, 0.1)50 nn.init.constant_(m.bias, 0.1)51 52# Perform inference53input = torch.randn(1, 3, 32, 32).to("cuda")54net = net.to("cuda")55output = net(input)56 57print(output)58 59net = torch.compile(net, mode="reduce-overhead", fullgraph=True)60 61output = net(input)62 63print(output)64 