Princess3 commited on
Commit
392bc58
·
verified ·
1 Parent(s): 930e8c0

Delete m3.py

Browse files
Files changed (1) hide show
  1. m3.py +0 -179
m3.py DELETED
@@ -1,179 +0,0 @@
1
- import os, xml.etree.ElementTree as ET, torch, torch.nn as nn, torch.nn.functional as F, numpy as np
2
- from typing import List, Dict, Any, Optional
3
- from collections import defaultdict
4
- from accelerate import Accelerator
5
- from transformers import AutoTokenizer, AutoModel
6
- from termcolor import colored
7
- from sklearn.metrics.pairwise import cosine_similarity
8
-
9
- class DM(nn.Module):
10
- def __init__(self, s: Dict[str, List[Dict[str, Any]]]):
11
- super(DM, self).__init__()
12
- self.s = nn.ModuleDict()
13
- if not s: s = {'default': [{'input_size': 128, 'output_size': 256, 'activation': 'relu', 'batch_norm': True, 'dropout': 0.1}]}
14
- for sn, l in s.items():
15
- self.s[sn] = nn.ModuleList()
16
- for lp in l:
17
- print(colored(f"Creating layer in section '{sn}' with params: {lp}", 'cyan'))
18
- self.s[sn].append(self.cl(lp))
19
-
20
- def cl(self, lp: Dict[str, Any]) -> nn.Module:
21
- l = [nn.Linear(lp['input_size'], lp['output_size'])]
22
- if lp.get('batch_norm', True): l.append(nn.BatchNorm1d(lp['output_size']))
23
- a = lp.get('activation', 'relu')
24
- if a == 'relu': l.append(nn.ReLU(inplace=True))
25
- elif a == 'tanh': l.append(nn.Tanh())
26
- elif a == 'sigmoid': l.append(nn.Sigmoid())
27
- elif a == 'leaky_relu': l.append(nn.LeakyReLU(negative_slope=0.01, inplace=True))
28
- elif a == 'elu': l.append(nn.ELU(alpha=1.0, inplace=True))
29
- elif a is not None: raise ValueError(f"Unsupported activation function: {a}")
30
- if dr := lp.get('dropout', 0.0): l.append(nn.Dropout(p=dr))
31
- if hl := lp.get('hidden_layers', []):
32
- for hlp in hl: l.append(self.cl(hlp))
33
- if lp.get('memory_augmentation', True): l.append(MAL(lp['output_size']))
34
- if lp.get('hybrid_attention', True): l.append(HAL(lp['output_size']))
35
- if lp.get('dynamic_flash_attention', True): l.append(DFAL(lp['output_size']))
36
- return nn.Sequential(*l)
37
-
38
- def forward(self, x: torch.Tensor, sn: Optional[str] = None) -> torch.Tensor:
39
- if sn is not None:
40
- if sn not in self.s: raise KeyError(f"Section '{sn}' not found in model")
41
- for l in self.s[sn]: x = l(x)
42
- else:
43
- for sn, l in self.s.items():
44
- for l in l: x = l(x)
45
- return x
46
-
47
- class MAL(nn.Module):
48
- def __init__(self, s: int):
49
- super(MAL, self).__init__()
50
- self.m = nn.Parameter(torch.randn(s))
51
-
52
- def forward(self, x: torch.Tensor) -> torch.Tensor:
53
- return x + self.m
54
-
55
- class HAL(nn.Module):
56
- def __init__(self, s: int):
57
- super(HAL, self).__init__()
58
- self.a = nn.MultiheadAttention(s, num_heads=8)
59
-
60
- def forward(self, x: torch.Tensor) -> torch.Tensor:
61
- x = x.unsqueeze(1)
62
- ao, _ = self.a(x, x, x)
63
- return ao.squeeze(1)
64
-
65
- class DFAL(nn.Module):
66
- def __init__(self, s: int):
67
- super(DFAL, self).__init__()
68
- self.a = nn.MultiheadAttention(s, num_heads=8)
69
-
70
- def forward(self, x: torch.Tensor) -> torch.Tensor:
71
- x = x.unsqueeze(1)
72
- ao, _ = self.a(x, x, x)
73
- return ao.squeeze(1)
74
-
75
- def px(file_path: str) -> List[Dict[str, Any]]:
76
- t = ET.parse(file_path)
77
- r = t.getroot()
78
- l = []
79
- for ly in r.findall('.//layer'):
80
- lp = {'input_size': int(ly.get('input_size', 128)), 'output_size': int(ly.get('output_size', 256)), 'activation': ly.get('activation', 'relu').lower()}
81
- if lp['activation'] not in ['relu', 'tanh', 'sigmoid', 'none']: raise ValueError(f"Unsupported activation function: {lp['activation']}")
82
- if lp['input_size'] <= 0 or lp['output_size'] <= 0: raise ValueError("Layer dimensions must be positive integers")
83
- l.append(lp)
84
- if not l: l.append({'input_size': 128, 'output_size': 256, 'activation': 'relu'})
85
- return l
86
-
87
- def cmf(folder_path: str) -> DM:
88
- s = defaultdict(list)
89
- if not os.path.exists(folder_path):
90
- print(colored(f"Warning: Folder {folder_path} does not exist. Creating model with default configuration.", 'yellow'))
91
- return DM({})
92
- xf = True
93
- for r, d, f in os.walk(folder_path):
94
- for file in f:
95
- if file.endswith('.xml'):
96
- xf = True
97
- fp = os.path.join(r, file)
98
- try:
99
- l = px(fp)
100
- sn = os.path.basename(r).replace('.', '_')
101
- s[sn].extend(l)
102
- except Exception as e:
103
- print(colored(f"Error processing {fp}: {str(e)}", 'red'))
104
- if not xf:
105
- print(colored("Warning: No XML files found. Creating model with default configuration.", 'yellow'))
106
- return DM({})
107
- return DM(dict(s))
108
-
109
- def ceas(folder_path: str, model_name: str = "sentence-transformers/all-MiniLM-L6-v2"):
110
- t = AutoTokenizer.from_pretrained(model_name)
111
- m = AutoModel.from_pretrained(model_name)
112
- embeddings = []
113
- ds = []
114
- for r, d, f in os.walk(folder_path):
115
- for file in f:
116
- if file.endswith('.xml'):
117
- fp = os.path.join(r, file)
118
- try:
119
- tree = ET.parse(fp)
120
- root = tree.getroot()
121
- for e in root.iter():
122
- if e.text:
123
- text = e.text.strip()
124
- i = t(text, return_tensors="pt", truncation=True, padding=True)
125
- with torch.no_grad():
126
- emb = m(**i).last_hidden_state.mean(dim=1).numpy()
127
- embeddings.append(emb)
128
- ds.append(text)
129
- except Exception as e:
130
- print(colored(f"Error processing {fp}: {str(e)}", 'red'))
131
- embeddings = np.vstack(embeddings)
132
- return embeddings, ds
133
-
134
- def qvs(query: str, embeddings, ds, model_name: str = "sentence-transformers/all-MiniLM-L6-v2"):
135
- t = AutoTokenizer.from_pretrained(model_name)
136
- m = AutoModel.from_pretrained(model_name)
137
- i = t(query, return_tensors="pt", truncation=True, padding=True)
138
- with torch.no_grad():
139
- qe = m(**i).last_hidden_state.mean(dim=1).numpy()
140
- similarities = cosine_similarity(qe, embeddings)
141
- top_k_indices = similarities[0].argsort()[-5:][::-1]
142
- return [ds[i] for i in top_k_indices]
143
-
144
- def main():
145
- fp = 'data'
146
- m = cmf(fp)
147
- print(colored(f"Created dynamic PyTorch model with sections: {list(m.s.keys())}", 'green'))
148
- fs = next(iter(m.s.keys()))
149
- fl = m.s[fs][0]
150
- ife = fl[0].in_features
151
- si = torch.randn(1, ife)
152
- o = m(si)
153
- print(colored(f"Sample output shape: {o.shape}", 'green'))
154
- embeddings, ds = ceas(fp)
155
- a = Accelerator()
156
- o = torch.optim.Adam(m.parameters(), lr=0.001)
157
- c = nn.CrossEntropyLoss()
158
- ne = 10
159
- d = torch.utils.data.TensorDataset(torch.randn(100, ife), torch.randint(0, 2, (100,)))
160
- td = torch.utils.data.DataLoader(d, batch_size=16, shuffle=True)
161
- m, o, td = a.prepare(m, o, td)
162
- for e in range(ne):
163
- m.train()
164
- tl = 0
165
- for bi, (i, l) in enumerate(td):
166
- o.zero_grad()
167
- o = m(i)
168
- l = c(o, l)
169
- a.backward(l)
170
- o.step()
171
- tl += l.item()
172
- al = tl / len(td)
173
- print(colored(f"Epoch {e+1}/{ne}, Average Loss: {al:.4f}", 'blue'))
174
- uq = "example query text"
175
- r = qvs(uq, embeddings, ds)
176
- print(colored(f"Query results: {r}", 'magenta'))
177
-
178
- if __name__ == "__main__":
179
- main()