File size: 1,688 Bytes
811d1c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import torch
import torch.nn as nn
import torch.nn.functional as F


class SuperConv(nn.Conv2d):

    def __init__(self, *args, is_lora=False, **kwargs):
        super().__init__(*args, **kwargs)

        self.is_lora = is_lora

    def forward(self, *args, **kwargs):
        if self.is_lora:
            return 3 + super().forward(*args, **kwargs)
        else:
            return super().forward(*args, **kwargs)

# Define a simple Convolutional Neural Network
class SimpleCNN(nn.Module):
    def __init__(self):
        super(SimpleCNN, self).__init__()
        self.conv1 = SuperConv(3, 6, 5) # Assuming input images are RGB, so 3 input channels
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = SuperConv(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

# Create the network
net = SimpleCNN()

# Initialize weights with dummy values
for m in net.modules():
    if isinstance(m, nn.Conv2d):
        nn.init.constant_(m.weight, 0.1)
        nn.init.constant_(m.bias, 0.1)
    elif isinstance(m, nn.Linear):
        nn.init.constant_(m.weight, 0.1)
        nn.init.constant_(m.bias, 0.1)

# Perform inference
input = torch.randn(1, 3, 32, 32).to("cuda")
net = net.to("cuda")
output = net(input)

print(output)

net = torch.compile(net, mode="reduce-overhead", fullgraph=True)

output = net(input)

print(output)