-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathcustom_models.py
190 lines (147 loc) · 5.12 KB
/
custom_models.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from typing import Dict, List, Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
# Sample Pool Model (for testing plugin serialization)
class Pool(nn.Module):
def __init__(self):
super(Pool, self).__init__()
def forward(self, x):
return F.adaptive_avg_pool2d(x, (5, 5))
# Sample Nested Module (for module-level fallback testing)
class ModuleFallbackSub(nn.Module):
def __init__(self):
super(ModuleFallbackSub, self).__init__()
self.conv = nn.Conv2d(1, 3, 3)
self.relu = nn.ReLU()
def forward(self, x):
return self.relu(self.conv(x))
class ModuleFallbackMain(nn.Module):
def __init__(self):
super(ModuleFallbackMain, self).__init__()
self.layer1 = ModuleFallbackSub()
self.conv = nn.Conv2d(3, 6, 3)
self.relu = nn.ReLU()
def forward(self, x):
return self.relu(self.conv(self.layer1(x)))
# Sample Looping Modules (for loop fallback testing)
class LoopFallbackEval(nn.Module):
def __init__(self):
super(LoopFallbackEval, self).__init__()
def forward(self, x):
add_list = torch.empty(0).to(x.device)
for i in range(x.shape[1]):
add_list = torch.cat((add_list, torch.tensor([x.shape[1]]).to(x.device)), 0)
return x + add_list
class LoopFallbackNoEval(nn.Module):
def __init__(self):
super(LoopFallbackNoEval, self).__init__()
def forward(self, x):
for _ in range(x.shape[1]):
x = x + torch.ones_like(x)
return x
# Sample Conditional Model (for testing partitioning and fallback in conditionals)
class FallbackIf(torch.nn.Module):
def __init__(self):
super(FallbackIf, self).__init__()
self.relu1 = torch.nn.ReLU()
self.conv1 = torch.nn.Conv2d(3, 32, 3, 1, 1)
self.log_sig = torch.nn.LogSigmoid()
self.conv2 = torch.nn.Conv2d(32, 32, 3, 1, 1)
self.conv3 = torch.nn.Conv2d(32, 3, 3, 1, 1)
def forward(self, x):
x = self.relu1(x)
x_first = x[0][0][0][0].item()
if x_first > 0:
x = self.conv1(x)
x1 = self.log_sig(x)
x2 = self.conv2(x)
x = self.conv3(x1 + x2)
else:
x = self.log_sig(x)
x = self.conv1(x)
return x
# Sample Inplace OP in Conditional Block Model
class FallbackInplaceOPIf(nn.Module):
def __init__(self):
super(FallbackInplaceOPIf, self).__init__()
def forward(self, x, y):
mod_list = [x]
if x.sum() > y.sum():
mod_list.append(y)
z = torch.cat(mod_list)
return z
# Collection input/output models
class StandardTensorInput(nn.Module):
def __init__(self):
super(StandardTensorInput, self).__init__()
def forward(self, x, y):
r = x + y
return r
class TupleInput(nn.Module):
def __init__(self):
super(TupleInput, self).__init__()
def forward(self, z: Tuple[torch.Tensor, torch.Tensor]):
r = z[0] + z[1]
return r
class ListInput(nn.Module):
def __init__(self):
super(ListInput, self).__init__()
def forward(self, z: List[torch.Tensor]):
r = z[0] + z[1]
return r
class TupleInputOutput(nn.Module):
def __init__(self):
super(TupleInputOutput, self).__init__()
def forward(self, z: Tuple[torch.Tensor, torch.Tensor]):
r1 = z[0] + z[1]
r2 = z[0] - z[1]
r1 = r1 * 10
r = (r1, r2)
return r
class ListInputOutput(nn.Module):
def __init__(self):
super(ListInputOutput, self).__init__()
def forward(self, z: List[torch.Tensor]):
r1 = z[0] + z[1]
r2 = z[0] - z[1]
r = [r1, r2]
return r
class ListInputTupleOutput(nn.Module):
def __init__(self):
super(ListInputTupleOutput, self).__init__()
self.list_model = ListInputOutput()
self.tuple_model = TupleInputOutput()
def forward(self, z: List[torch.Tensor]):
r1 = z[0] + z[1]
r2 = z[0] - z[1]
r3 = (r1, r2)
r4 = [r2, r1]
tuple_out = self.tuple_model(r3)
list_out = self.list_model(r4)
r = (tuple_out[1], list_out[0])
return r
def BertModule():
from transformers import BertConfig, BertModel, BertTokenizer
enc = BertTokenizer.from_pretrained("google-bert/bert-base-uncased")
text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]"
tokenized_text = enc.tokenize(text)
masked_index = 8
tokenized_text[masked_index] = "[MASK]"
indexed_tokens = enc.convert_tokens_to_ids(tokenized_text)
segments_ids = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]
tokens_tensor = torch.tensor([indexed_tokens])
segments_tensors = torch.tensor([segments_ids])
dummy_input = [tokens_tensor, segments_tensors]
config = BertConfig(
vocab_size_or_config_json_file=32000,
hidden_size=768,
num_hidden_layers=12,
num_attention_heads=12,
intermediate_size=3072,
torchscript=True,
)
model = BertModel(config)
model.eval()
traced_model = torch.jit.trace(model, [tokens_tensor, segments_tensors])
return traced_model