RS2002 commited on
Commit
3afa065
·
verified ·
1 Parent(s): 9888ab1

Upload 2 files

Browse files
Files changed (2) hide show
  1. model.py +185 -0
  2. trained.zip +3 -0
model.py ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from torch.ao.nn.quantized import Sigmoid
2
+ from transformers import BartModel
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+ import torch.nn.init as init
7
+ from peft import get_peft_model, LoraConfig
8
+ from huggingface_hub import PyTorchModelHubMixin
9
+ from transformers import BartConfig
10
+
11
+
12
+ class MLP(nn.Module):
13
+ def __init__(self, layer_sizes=[64, 64, 64, 1], arl=False, dropout=0.0):
14
+ super().__init__()
15
+ self.arl = arl
16
+ self.attention = nn.Sequential(
17
+ nn.Linear(layer_sizes[0], layer_sizes[0]),
18
+ nn.ReLU(),
19
+ nn.Dropout(dropout),
20
+ nn.Linear(layer_sizes[0], layer_sizes[0])
21
+ )
22
+
23
+ self.layer_sizes = layer_sizes
24
+ if len(layer_sizes) < 2:
25
+ raise ValueError()
26
+ self.layers = nn.ModuleList()
27
+ self.act = nn.LeakyReLU(negative_slope=0.01, inplace=True)
28
+ self.dropout = nn.Dropout(dropout)
29
+ for i in range(len(layer_sizes) - 1):
30
+ self.layers.append(nn.Linear(layer_sizes[i], layer_sizes[i + 1]))
31
+
32
+ def forward(self, x):
33
+ if self.arl:
34
+ x = x * self.attention(x)
35
+ for layer in self.layers[:-1]:
36
+ x = self.dropout(self.act(layer(x)))
37
+ x = self.layers[-1](x)
38
+ return x
39
+
40
+
41
+ class BART(nn.Module):
42
+ def __init__(self, bartconfig, class_num=100):
43
+ super().__init__()
44
+ d_model = bartconfig.d_model
45
+ self.decoder_emb = nn.Embedding(class_num, d_model)
46
+ self.bart = BartModel(bartconfig)
47
+
48
+ def forward(self, x_encoder, x_decoder, attn_mask_encoder=None, attn_mask_decoder=None):
49
+ emb_encoder = x_encoder
50
+ emb_decoder = self.decoder_emb(x_decoder)
51
+ y = self.bart(inputs_embeds=emb_encoder, decoder_inputs_embeds=emb_decoder,
52
+ attention_mask=attn_mask_encoder, decoder_attention_mask=attn_mask_decoder,
53
+ output_hidden_states=False)
54
+ y = y.last_hidden_state
55
+ return y
56
+
57
+ def encode(self, x_encoder, attn_mask_encoder=None):
58
+ emb_encoder = x_encoder
59
+ y = self.bart.encoder(inputs_embeds=emb_encoder, attention_mask=attn_mask_encoder, output_hidden_states=False)
60
+ y = y.last_hidden_state
61
+ return y
62
+
63
+
64
+ class ML_BART(nn.Module):
65
+ def __init__(self, bartconfig, class_num=[180, 256], pretrain=False, music_dim=512):
66
+ super().__init__()
67
+ d_model = bartconfig.d_model
68
+
69
+ self.decoder_emb = nn.ModuleList([
70
+ nn.Embedding(class_num[0] + 1, d_model // 4),
71
+ nn.Embedding(class_num[1] + 1, d_model // 4)
72
+ ])
73
+ self.decoder = MLP([music_dim, d_model // 2])
74
+
75
+ self.bart = BartModel(bartconfig)
76
+ self.pretrain = pretrain
77
+
78
+ self.encoder = MLP([music_dim, d_model])
79
+ self.lora_config = LoraConfig(
80
+ r=4,
81
+ lora_alpha=16,
82
+ lora_dropout=0.1
83
+ )
84
+
85
+ def forward(self, x_encoder, x_decoder, attn_mask_encoder=None, attn_mask_decoder=None):
86
+ # emb_encoder = x_encoder
87
+ emb_encoder = self.encoder(x_encoder)
88
+
89
+ if self.pretrain:
90
+ # emb_decoder = x_decoder
91
+ emb_decoder = self.encoder(x_decoder)
92
+ else:
93
+ emb_decoder = torch.concatenate(
94
+ [self.decoder_emb[0](x_decoder[..., 0]), self.decoder_emb[1](x_decoder[..., 1]),
95
+ self.decoder(x_encoder)], dim=-1)
96
+
97
+ y = self.bart(inputs_embeds=emb_encoder, decoder_inputs_embeds=emb_decoder,
98
+ attention_mask=attn_mask_encoder, decoder_attention_mask=attn_mask_decoder,
99
+ output_hidden_states=False)
100
+ y = y.last_hidden_state
101
+ return y
102
+
103
+ def encode(self, x_encoder, attn_mask_encoder=None):
104
+ # emb_encoder = x_encoder
105
+ emb_encoder = self.encoder(x_encoder)
106
+
107
+ y = self.bart.encoder(inputs_embeds=emb_encoder, attention_mask=attn_mask_encoder, output_hidden_states=False)
108
+ y = y.last_hidden_state
109
+ return y
110
+
111
+ def reset_decoder(self):
112
+ for name, param in self.bart.decoder.named_parameters():
113
+ if param.dim() >= 2:
114
+ init.xavier_uniform_(param)
115
+ elif param.dim() == 1:
116
+ init.zeros_(param)
117
+
118
+
119
+ class ML_Classifier(nn.Module):
120
+ def __init__(self, hidden_dim=512, class_num=[180, 256]):
121
+ super().__init__()
122
+ self.classifier = nn.ModuleList([
123
+ MLP([hidden_dim, hidden_dim, class_num[0] + 1]),
124
+ MLP([hidden_dim, hidden_dim, class_num[1] + 1])
125
+ ])
126
+
127
+ def forward(self, x):
128
+ h = self.classifier[0](x)
129
+ v = self.classifier[1](x)
130
+ return h, v
131
+
132
+
133
+ class SelfAttention(nn.Module):
134
+ def __init__(self, input_dim, da, r):
135
+ super().__init__()
136
+ self.ws1 = nn.Linear(input_dim, da, bias=False)
137
+ self.ws2 = nn.Linear(da, r, bias=False)
138
+
139
+ def forward(self, h):
140
+ attn_mat = F.softmax(self.ws2(torch.tanh(self.ws1(h))), dim=1)
141
+ attn_mat = attn_mat.permute(0, 2, 1)
142
+ return attn_mat
143
+
144
+
145
+ class Sequence_Classifier(nn.Module):
146
+ def __init__(self, class_num=1, hs=512, da=512, r=8):
147
+ super().__init__()
148
+ self.attention = SelfAttention(hs, da, r)
149
+ self.classifier = MLP([hs * r, (hs * r + class_num) // 2, class_num])
150
+
151
+ def forward(self, x):
152
+ attn_mat = self.attention(x)
153
+ m = torch.bmm(attn_mat, x)
154
+ flatten = m.view(m.size()[0], -1)
155
+ res = self.classifier(flatten)
156
+ return res
157
+
158
+
159
+ class Token_Predictor(nn.Module):
160
+ def __init__(self, hidden_dim=512, class_num=1):
161
+ super().__init__()
162
+ self.classifier = MLP([hidden_dim, (hidden_dim + class_num) // 2, class_num])
163
+
164
+ def forward(self, x):
165
+ x = self.classifier(x)
166
+ return x
167
+
168
+ class Skip_BART(nn.Module,
169
+ PyTorchModelHubMixin
170
+ ):
171
+ def __init__(self, class_num=[180, 256], max_position_embeddings=1024, hidden_size=1024, layers=8, heads=8, ffn_dims=2048, pretrain=False):
172
+ super().__init__()
173
+ self.config = BartConfig(max_position_embeddings=max_position_embeddings,
174
+ d_model=hidden_size,
175
+ encoder_layers=layers,
176
+ encoder_ffn_dim=ffn_dims,
177
+ encoder_attention_heads=heads,
178
+ decoder_layers=layers,
179
+ decoder_ffn_dim=ffn_dims,
180
+ decoder_attention_heads=heads
181
+ )
182
+ self.model = ML_BART(self.config, class_num = class_num, pretrain = pretrain)
183
+
184
+ def forward(self, x_encoder, x_decoder, attn_mask_encoder=None, attn_mask_decoder=None):
185
+ return self.model(x_encoder, x_decoder, attn_mask_encoder, attn_mask_decoder)
trained.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:737603ca3921a070ea8b70e623f7b789219656e0a02ec65721a7698a0ceb9d3c
3
+ size 854009394