-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
45 lines (31 loc) · 1014 Bytes
/
test.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
import torch.nn as nn
import torch.nn.functional as F
class TestBlock(nn.Module):
def __init__(self, *shape):
super().__init__()
self.layer = nn.Linear(*shape)
def forward(self, x):
return F.relu(self.layer(x))
class TestLMShard1(nn.Module):
"""A small toy model for testing purpose.
"""
def __init__(self):
super().__init__()
self.blk1 = TestBlock(10, 10)
self.blk2 = TestBlock(10, 10)
def forward(self, x):
return {"x": self.blk2(self.blk1(x))}
class TestLMShard2(nn.Module):
"""A small toy model for testing purpose.
"""
def __init__(self):
super().__init__()
self.blk3 = TestBlock(10, 10)
self.out = nn.Linear(10, 10)
def forward(self, x, labels = None):
logits = F.softmax(self.out(self.blk3(x)), dim=1)
if labels is not None:
loss = F.mse_loss(logits, labels)
else:
loss = None
return {"loss": loss, "logits": logits}