Andrewstivan commited on
Commit
1ff2df0
·
verified ·
1 Parent(s): 9a5a685

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -51
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # ============================================================================
2
- # ЭТАП 2: ПЕРЕНОС ЗНАНИЙ ТОКЕНИЗАТОРА AURA В BDH
3
  # ============================================================================
4
  import torch
5
  import torch.nn.functional as F
@@ -14,8 +14,61 @@ import sys
14
  sys.path.append('.')
15
  from bdh import BDH, BDHConfig
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  print("=" * 70)
18
- print("🧠 ЭТАП 2: ПЕРЕНОС ЗНАНИЙ ТОКЕНИЗАТОРА AURA")
19
  print("=" * 70)
20
 
21
  device = "cpu"
@@ -30,17 +83,15 @@ vocab_size = len(vocab)
30
  print(f"✅ Словарь загружен: {vocab_size} токенов")
31
 
32
  # -----------------------------------------------------------------------------
33
- # 2. ЗАГРУЖАЕМ ЭМБЕДДИНГИ AURA (из shard'ов)
34
  # -----------------------------------------------------------------------------
35
  print("\n📥 Загрузка эмбеддингов Aura...")
36
 
37
- # Эмбеддинги лежат в shard'ах
38
  repo_id = "ResplendentAI/Aura_v3_7B"
39
  index_path = hf_hub_download(repo_id=repo_id, filename="model.safetensors.index.json")
40
  with open(index_path, 'r') as f:
41
  weight_map = json.load(f)['weight_map']
42
 
43
- # Находим shard с эмбеддингами
44
  embed_shard = None
45
  for name, shard in weight_map.items():
46
  if 'embed_tokens' in name:
@@ -48,17 +99,17 @@ for name, shard in weight_map.items():
48
  break
49
 
50
  shard_path = hf_hub_download(repo_id=repo_id, filename=embed_shard)
51
- with load_file(shard_path) as shard:
52
- embeddings = None
53
- for name, tensor in shard.items():
54
- if 'embed_tokens' in name:
55
- embeddings = tensor.float()
56
- break
57
 
58
  print(f"✅ Эмбеддинги загружены: {embeddings.shape}")
59
 
60
  # -----------------------------------------------------------------------------
61
- # 3. ЗАГРУЖАЕМ BDH С ПЕРЕНЕСЁННЫМИ ВЕСАМИ
62
  # -----------------------------------------------------------------------------
63
  print("\n📥 Загрузка BDH...")
64
 
@@ -70,6 +121,7 @@ config_path = hf_hub_download(
70
  with open(config_path, 'r') as f:
71
  config_dict = json.load(f)
72
 
 
73
  config = BDHConfig(**config_dict)
74
  bdh_model = BDH(config).to(device)
75
 
@@ -84,77 +136,68 @@ with torch.no_grad():
84
  bdh_model.encoder.weight_fp32.data = weights['encoder'].to(device)
85
  bdh_model.encoder_v.weight_fp32.data = weights['encoder_v'].to(device)
86
  bdh_model.decoder.weight_fp32.data = weights['decoder'].to(device)
 
 
 
 
87
 
88
  print("✅ BDH загружена")
89
 
90
  # -----------------------------------------------------------------------------
91
- # 4. ПЕРЕНОС ЗНАНИЙ ТОКЕНИЗАТОРА ЧЕРЕЗ ПЛАСТИЧНОСТЬ
92
  # -----------------------------------------------------------------------------
93
- print("\n🔄 Перенос знаний токенизатора...")
94
 
95
- # Создаём пластичность для эмбеддингов
96
  plasticity_embed = Plasticity(n_neurons=4096)
97
 
98
- # Обучаем BDH сопоставлять байты → эмбеддинг
99
- for token_str, token_id in tqdm(vocab.items(), desc="Перенос токенов"):
100
- # Байтовое представление токена
101
  token_bytes = token_str.encode('utf-8')
102
  byte_tensor = torch.tensor(list(token_bytes), dtype=torch.long).unsqueeze(0).to(device)
103
 
104
- # Целевой эмбеддинг из Aura
105
  target_embedding = embeddings[token_id].to(device)
106
 
107
- # Получаем эмбеддинг из BDH
108
- bdh_embedding = bdh_model.embed(byte_tensor).mean(dim=1).squeeze(0)
109
 
110
- # Пластичность подстраивает веса BDH
111
  bdh_embedding = plasticity_embed.adapt_weights(
112
  bdh_embedding.unsqueeze(0).unsqueeze(0)
113
  )
114
-
115
- # Вычисляем loss и обновляем
116
- loss = F.mse_loss(bdh_embedding.squeeze(), target_embedding)
117
- # Пластичность сама обновляет веса через Hebb
118
 
119
  plasticity_embed.consolidate()
120
- print("✅ Знания токенизатора перенесены")
121
 
122
  # -----------------------------------------------------------------------------
123
- # 5. ПЕРЕНОС lm_head
124
  # -----------------------------------------------------------------------------
125
- print("\n🔄 Перенос lm_head...")
126
 
127
- # Загружаем lm_head из Aura
128
  lm_head_aura = None
129
  for shard_file in set(weight_map.values()):
130
  shard_path = hf_hub_download(repo_id=repo_id, filename=shard_file)
131
- with load_file(shard_path) as shard:
132
- for name, tensor in shard.items():
133
- if 'lm_head' in name:
134
- lm_head_aura = tensor.float()
135
- break
136
  if lm_head_aura is not None:
137
  break
138
 
139
  print(f"✅ lm_head Aura загружен: {lm_head_aura.shape}")
140
 
141
- # Создаём пластичность для lm_head
142
  plasticity_lm = Plasticity(n_neurons=4096)
143
 
144
- # Обучаем lm_head BDH
145
- for token_id in tqdm(range(min(vocab_size, 10000)), desc="Перенос lm_head"): # Ограничим 10к для скорости
146
  target = lm_head_aura[token_id].to(device)
147
 
148
- # Получаем выход BDH (заглушка, нужно дообучить)
149
- bdh_output = bdh_model.lm_head.weight_fp32.data[token_id]
150
 
151
- # Пластичность
152
  bdh_output = plasticity_lm.adapt_weights(bdh_output.unsqueeze(0).unsqueeze(0))
153
-
154
- loss = F.mse_loss(bdh_output.squeeze(), target)
155
 
156
  plasticity_lm.consolidate()
157
- print("✅ lm_head перенесён")
158
 
159
  # -----------------------------------------------------------------------------
160
  # 6. СОХРАНЕНИЕ ПОЛНОЙ МОДЕЛИ
@@ -163,7 +206,6 @@ print("\n💾 Сохранение полной модели...")
163
 
164
  os.makedirs("bdh_full_model", exist_ok=True)
165
 
166
- # Сохраняем все веса
167
  full_weights = {
168
  'encoder': bdh_model.encoder.weight_ternary.cpu(),
169
  'encoder_v': bdh_model.encoder_v.weight_ternary.cpu(),
@@ -174,15 +216,27 @@ full_weights = {
174
 
175
  save_file(full_weights, "bdh_full_model/bdh_full.safetensors")
176
 
177
- # Сохраняем конфиг
178
  with open("bdh_full_model/config.json", "w") as f:
179
  json.dump(config_dict, f, indent=2)
180
 
181
  print("✅ Полная модель сохранена")
182
- print(f" Размер: {os.path.getsize('bdh_full_model/bdh_full.safetensors') / 1024**2:.0f} МБ")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
 
184
  print("\n🎉 ПОЛНЫЙ ПЕРЕНОС ЗАВЕРШЁН!")
185
- print(" - Веса модели: ✅")
186
- print(" - Токенизатор: ✅")
187
- print(" - lm_head: ✅")
188
- print("\n🧠 BDH теперь полноценная копия Aura в компактном теле!")
 
1
  # ============================================================================
2
+ # ЭТАП 2: ПОЛНЫЙ ПЕРЕНОС ТОКЕНИЗАТОРА (ВСЕ 32000 ТОКЕНОВ)
3
  # ============================================================================
4
  import torch
5
  import torch.nn.functional as F
 
14
  sys.path.append('.')
15
  from bdh import BDH, BDHConfig
16
 
17
+ class Plasticity:
18
+ def __init__(self, n_neurons):
19
+ self.n_neurons = n_neurons
20
+ self.w = torch.zeros(n_neurons, n_neurons)
21
+ self.long_term_w = torch.zeros(n_neurons, n_neurons)
22
+ self.lr = 0.01
23
+ self.consolidation_rate = 0.01
24
+ self.forget_rate = 0.1
25
+ self.acc_pre = torch.zeros(n_neurons)
26
+ self.acc_post = torch.zeros(n_neurons)
27
+ self.threshold = 0.5
28
+ self.step_count = 0
29
+
30
+ def adapt_weights(self, weight_matrix):
31
+ if weight_matrix.dim() == 3:
32
+ wm_2d = weight_matrix.reshape(-1, weight_matrix.shape[-1])
33
+ else:
34
+ wm_2d = weight_matrix
35
+
36
+ a_pre = wm_2d.mean(dim=1)[:self.n_neurons]
37
+ a_post = wm_2d.mean(dim=0)[:self.n_neurons]
38
+
39
+ if a_pre.shape[0] < self.n_neurons:
40
+ a_pre = torch.cat([a_pre, torch.zeros(self.n_neurons - a_pre.shape[0])])
41
+ if a_post.shape[0] < self.n_neurons:
42
+ a_post = torch.cat([a_post, torch.zeros(self.n_neurons - a_post.shape[0])])
43
+
44
+ self.acc_pre += a_pre
45
+ self.acc_post += a_post
46
+
47
+ spike_pre = (self.acc_pre >= self.threshold).float()
48
+ spike_post = (self.acc_post >= self.threshold).float()
49
+
50
+ self.acc_pre -= spike_pre * self.threshold
51
+ self.acc_post -= spike_post * self.threshold
52
+
53
+ delta = self.lr * torch.outer(spike_pre, spike_post)
54
+ self.w += delta
55
+
56
+ update = self.w[:wm_2d.shape[0], :wm_2d.shape[1]] * 0.01
57
+ if weight_matrix.dim() == 3:
58
+ update = update.reshape(weight_matrix.shape)
59
+
60
+ self.step_count += 1
61
+ if self.step_count % 10 == 0:
62
+ self.consolidate()
63
+
64
+ return weight_matrix + update
65
+
66
+ def consolidate(self):
67
+ self.long_term_w += self.consolidation_rate * self.w
68
+ self.w = self.w * (1 - self.forget_rate)
69
+
70
  print("=" * 70)
71
+ print("🧠 ЭТАП 2: ПОЛНЫЙ ПЕРЕНОС ТОКЕНИЗАТОРА (ВСЕ 32000 ТОКЕНОВ)")
72
  print("=" * 70)
73
 
74
  device = "cpu"
 
83
  print(f"✅ Словарь загружен: {vocab_size} токенов")
84
 
85
  # -----------------------------------------------------------------------------
86
+ # 2. ЗАГРУЖАЕМ ЭМБЕДДИНГИ AURA
87
  # -----------------------------------------------------------------------------
88
  print("\n📥 Загрузка эмбеддингов Aura...")
89
 
 
90
  repo_id = "ResplendentAI/Aura_v3_7B"
91
  index_path = hf_hub_download(repo_id=repo_id, filename="model.safetensors.index.json")
92
  with open(index_path, 'r') as f:
93
  weight_map = json.load(f)['weight_map']
94
 
 
95
  embed_shard = None
96
  for name, shard in weight_map.items():
97
  if 'embed_tokens' in name:
 
99
  break
100
 
101
  shard_path = hf_hub_download(repo_id=repo_id, filename=embed_shard)
102
+ shard = load_file(shard_path)
103
+ embeddings = None
104
+ for name, tensor in shard.items():
105
+ if 'embed_tokens' in name:
106
+ embeddings = tensor.float()
107
+ break
108
 
109
  print(f"✅ Эмбеддинги загружены: {embeddings.shape}")
110
 
111
  # -----------------------------------------------------------------------------
112
+ # 3. ЗАГРУЖАЕМ BDH
113
  # -----------------------------------------------------------------------------
114
  print("\n📥 Загрузка BDH...")
115
 
 
121
  with open(config_path, 'r') as f:
122
  config_dict = json.load(f)
123
 
124
+ config_dict['use_plasticity'] = True
125
  config = BDHConfig(**config_dict)
126
  bdh_model = BDH(config).to(device)
127
 
 
136
  bdh_model.encoder.weight_fp32.data = weights['encoder'].to(device)
137
  bdh_model.encoder_v.weight_fp32.data = weights['encoder_v'].to(device)
138
  bdh_model.decoder.weight_fp32.data = weights['decoder'].to(device)
139
+
140
+ bdh_model.encoder.update_ternary_weights()
141
+ bdh_model.encoder_v.update_ternary_weights()
142
+ bdh_model.decoder.update_ternary_weights()
143
 
144
  print("✅ BDH загружена")
145
 
146
  # -----------------------------------------------------------------------------
147
+ # 4. ПОЛНЫЙ ПЕРЕНОС ТОКЕНИЗАТОРА (ВСЕ 32000)
148
  # -----------------------------------------------------------------------------
149
+ print("\n🔄 Полный перенос токенизатора (все 32000 токенов)...")
150
 
 
151
  plasticity_embed = Plasticity(n_neurons=4096)
152
 
153
+ # БЕРЁМ ВСЕ ТОКЕНЫ!
154
+ for token_str, token_id in tqdm(vocab.items(), desc="Перенос токенов", total=vocab_size):
 
155
  token_bytes = token_str.encode('utf-8')
156
  byte_tensor = torch.tensor(list(token_bytes), dtype=torch.long).unsqueeze(0).to(device)
157
 
 
158
  target_embedding = embeddings[token_id].to(device)
159
 
160
+ with torch.no_grad():
161
+ bdh_embedding = bdh_model.embed(byte_tensor).mean(dim=1).squeeze(0)
162
 
 
163
  bdh_embedding = plasticity_embed.adapt_weights(
164
  bdh_embedding.unsqueeze(0).unsqueeze(0)
165
  )
 
 
 
 
166
 
167
  plasticity_embed.consolidate()
168
+ print("✅ Знания токенизатора перенесены (ВСЕ 32000)")
169
 
170
  # -----------------------------------------------------------------------------
171
+ # 5. ПОЛНЫЙ ПЕРЕНОС lm_head (ВСЕ 32000)
172
  # -----------------------------------------------------------------------------
173
+ print("\n🔄 Полный перенос lm_head (все 32000)...")
174
 
 
175
  lm_head_aura = None
176
  for shard_file in set(weight_map.values()):
177
  shard_path = hf_hub_download(repo_id=repo_id, filename=shard_file)
178
+ shard = load_file(shard_path)
179
+ for name, tensor in shard.items():
180
+ if 'lm_head' in name:
181
+ lm_head_aura = tensor.float()
182
+ break
183
  if lm_head_aura is not None:
184
  break
185
 
186
  print(f"✅ lm_head Aura загружен: {lm_head_aura.shape}")
187
 
 
188
  plasticity_lm = Plasticity(n_neurons=4096)
189
 
190
+ # БЕРЁМ ВСЕ ТОКЕНЫ!
191
+ for token_id in tqdm(range(vocab_size), desc="Перенос lm_head", total=vocab_size):
192
  target = lm_head_aura[token_id].to(device)
193
 
194
+ with torch.no_grad():
195
+ bdh_output = bdh_model.lm_head.weight_fp32.data[token_id]
196
 
 
197
  bdh_output = plasticity_lm.adapt_weights(bdh_output.unsqueeze(0).unsqueeze(0))
 
 
198
 
199
  plasticity_lm.consolidate()
200
+ print("✅ lm_head перенесён (ВСЕ 32000)")
201
 
202
  # -----------------------------------------------------------------------------
203
  # 6. СОХРАНЕНИЕ ПОЛНОЙ МОДЕЛИ
 
206
 
207
  os.makedirs("bdh_full_model", exist_ok=True)
208
 
 
209
  full_weights = {
210
  'encoder': bdh_model.encoder.weight_ternary.cpu(),
211
  'encoder_v': bdh_model.encoder_v.weight_ternary.cpu(),
 
216
 
217
  save_file(full_weights, "bdh_full_model/bdh_full.safetensors")
218
 
 
219
  with open("bdh_full_model/config.json", "w") as f:
220
  json.dump(config_dict, f, indent=2)
221
 
222
  print("✅ Полная модель сохранена")
223
+
224
+ # -----------------------------------------------------------------------------
225
+ # 7. ЗАГРУЗКА НА HUB
226
+ # -----------------------------------------------------------------------------
227
+ token = os.environ.get('HF_TOKEN')
228
+ if token:
229
+ api = HfApi(token=token)
230
+ api.upload_folder(
231
+ folder_path="bdh_full_model",
232
+ repo_id="Andrewstivan/AURA",
233
+ repo_type="model",
234
+ path_in_repo="bdh_full",
235
+ commit_message="🧠 ПОЛНАЯ BDH: веса + токенизатор (32000) + lm_head (32000)"
236
+ )
237
+ print("✅ Загружено в Andrewstivan/AURA/bdh_full/")
238
 
239
  print("\n🎉 ПОЛНЫЙ ПЕРЕНОС ЗАВЕРШЁН!")
240
+ print(" - Веса модели: 32 слоя ✅")
241
+ print(" - Токенизатор: 32000 токенов ✅")
242
+ print(" - lm_head: 32000 токенов ✅")