-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.get_pytorch_IR.py
executable file
·147 lines (108 loc) · 4.09 KB
/
6.get_pytorch_IR.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/env python
import time
from module import *
from typing import List
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.datasets import MNIST
from torch.utils.data import DataLoader
from torchvision import transforms as tfs
import torchopt
import functorch
device = torch.device("cpu")
class MyTransform:
def __init__(self):
pass
def __call__(self, img):
img = tfs.functional.rotate(img, -90)
img = tfs.functional.hflip(img)
img = img.map_(img, lambda a, b: 1.0 - a)
return img
trainset = MNIST(root = "",
#split = "digits",
train = True,
download = True,
transform = tfs.Compose([tfs.PILToTensor(),
tfs.Resize(29),
tfs.ConvertImageDtype(torch.float32),
MyTransform()
]))
testset = MNIST(root = "",
#split = "digits",
train = False,
download = True,
transform = tfs.Compose([tfs.PILToTensor(),
tfs.Resize(29),
tfs.ConvertImageDtype(torch.float32),
MyTransform()
]))
trainset_size = len(trainset)
testset_size = len(testset)
trainset_dataloader = DataLoader(trainset, batch_size = 16, shuffle = False);
testset_dataloader = DataLoader(testset, batch_size = 16, shuffle = False);
my_module = MyModule()
random_input = torch.randn([1, 29, 29])
random_target = torch.randn([10])
#scripted_module = torch.jit.trace(my_module, random_input)
loss_fn = nn.CrossEntropyLoss()
loss_fn.to(device)
lr = 0.01
optimizer = torchopt.sgd(lr)
#opt_state = optimizer.init(params)
def train_one_batch(params, imgs, targets, opt_state):
grads, (loss, preds) = grad_and_preds_and_loss(params, imgs, targets)
updates, opt_state = optimizer.update(grads, opt_state)
params = torchopt.apply_updates(params, updates)
return params, loss, preds
for imgs, labels in trainset_dataloader:
def label_to_target(label):
ret = [0] * 10
ret[label] = 1
return ret
targets = torch.Tensor([label_to_target(label) for label in labels.tolist()]).float()
imgs, targets = (imgs.to(device), targets.to(device))
# 1. functorch
fmodule, params, buffers = functorch.make_functional_with_buffers(my_module)
print(type(params))
print(type(buffers))
from torch._subclasses.fake_tensor import FakeTensorMode
fake_mode = FakeTensorMode()
def to_fake_tensor(fake_mode, tensor):
if isinstance(tensor, torch.Tensor):
return fake_mode.from_tensor(tensor)
elif isinstance(tensor, tuple):
rets = tuple()
for elem in tensor:
ret = to_fake_tensor(fake_mode, elem)
rets = rets + (ret, )
return rets
assert False
def print_compile_fn(fx_module, args):
fx_module.print_readable()
return fx_module
def compute_loss(prarams, buffers, datas, labels):
preds = fmodule(params, buffers, datas)
loss = loss_fn(preds, labels)
return loss
from functorch.compile import aot_function, compiled_function
from torch._guards import detect_fake_mode
aot_fn = aot_function(compute_loss, fw_compiler = print_compile_fn, bw_compiler = print_compile_fn)
for elem in params:
print(type(elem))
def clone_tensor(tensor, require_grad):
if isinstance(tensor, torch.Tensor):
return tensor.clone().detach().requires_grad_(require_grad)
elif isinstance(tensor, tuple):
rets = tuple()
for elem in tensor:
ret = clone_tensor(elem, require_grad)
rets = rets + (ret, )
return rets
params_clone = clone_tensor(params, True)
buffers_clone = clone_tensor(buffers, True)
imgs_clone = clone_tensor(imgs, True)
targets_clone = clone_tensor(targets, True)
the_loss = aot_fn(params_clone, buffers_clone, imgs_clone, targets_clone)
the_loss.backward()
exit()