File size: 3,177 Bytes
f465594 208ef89 f465594 208ef89 f465594 208ef89 f465594 208ef89 f465594 208ef89 f465594 208ef89 f465594 208ef89 f465594 208ef89 f465594 208ef89 f465594 208ef89 f465594 208ef89 | 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 | import torch
from torch import nn, Tensor
class VecDyT(nn.Module):
def __init__(self, input_shape):
super().__init__()
self.alpha = nn.Parameter(torch.randn(input_shape))
def forward(self, x):
x = torch.tanh(self.alpha * x)
return x
class VecDyGeluSine(nn.Module):
def __init__(self, input_shape):
super().__init__()
self.alpha = nn.Parameter(torch.randn(input_shape))
self.beta = nn.Parameter(torch.randn(input_shape))
self.gamma = nn.Parameter(torch.randn(1))
self.etta = nn.Parameter(torch.randn(1))
self.gelu = nn.GELU()
def forward(self, x):
x = self.gamma * self.gelu(self.alpha * x) + self.etta * torch.sin(self.beta * x)
return x
class FFUnit(nn.Module):
def __init__(self,dim):
super().__init__()
self.proj = nn.Linear(dim,dim,bias=False)
self.modulate = VecDyGeluSine(dim)
def forward(self, x):
u, v = x, x
u = self.modulate(u)
v = self.proj(v)
g = u * v
return g
class TTT(nn.Module):
def __init__(self, dim: int):
super(TTT, self).__init__()
self.mapping = nn.Linear(dim,dim,bias=False)
def forward(self, in_seq: Tensor) -> Tensor:
outs = []
for seq in range(in_seq.size(1)):
state = in_seq[:,seq,:]
train_view = state + torch.randn_like(state)
label_view = state
loss = nn.functional.mse_loss(self.mapping(train_view), label_view)
grads = torch.autograd.grad(
loss, self.mapping.parameters(),create_graph=True)
with torch.no_grad():
for param, grad in zip(self.mapping.parameters(), grads):
param -= 0.01 * grad
readout = self.mapping(in_seq[:,seq,:]).detach()
outs.append(readout)
out = torch.stack(outs, dim=1)
return out
class FFUnit_TTT(nn.Module):
def __init__(self,dim):
super().__init__()
self.proj = TTT(dim)
self.modulate = VecDyGeluSine(dim)
def forward(self, x):
u, v = x, x
u = self.modulate(u)
v = self.proj(v)
g = u * v
return g
class LiteTensorMapperBlock(nn.Module):
def __init__(self, dim, num_patch):
super().__init__()
self.norm_1 = VecDyT(dim)
self.norm_2 = VecDyT(dim)
self.memory = FFUnit_TTT(dim)
self.feedforward = FFUnit(dim)
def forward(self, x):
memorypath,residual = x, x
memorypath = self.norm_1(memorypath)
memorypath = self.memory(memorypath)
x = memorypath + residual
FFpath, residual = x, x
FFpath = self.norm_2(FFpath)
FFpath = self.feedforward(FFpath)
x = FFpath + residual
return x
class LiteTensorMapper(nn.Module):
def __init__(self, d_model,num_patch, num_layers):
super().__init__()
self.model = nn.Sequential(
*[LiteTensorMapperBlock(d_model,num_patch) for _ in range(num_layers)]
)
def forward(self, x):
return self.model(x)
|