Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# ============================================================================
|
| 2 |
-
# ИСПРАВЛЕННАЯ ЗАГРУЗКА
|
| 3 |
# ============================================================================
|
| 4 |
|
| 5 |
import torch
|
|
@@ -13,7 +13,7 @@ sys.path.append('.')
|
|
| 13 |
from bdh import BDH, BDHConfig
|
| 14 |
|
| 15 |
print("=" * 60)
|
| 16 |
-
print("🧪 ИСПРАВЛЕННОЕ ТЕСТИРОВАНИЕ
|
| 17 |
print("=" * 60)
|
| 18 |
|
| 19 |
# -----------------------------------------------------------------------------
|
|
@@ -47,30 +47,71 @@ weights_path = hf_hub_download(
|
|
| 47 |
)
|
| 48 |
weights = load_file(weights_path)
|
| 49 |
|
| 50 |
-
#
|
|
|
|
|
|
|
| 51 |
with torch.no_grad():
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
print(f"
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
bdh_model.
|
| 72 |
-
bdh_model.
|
| 73 |
-
bdh_model.
|
|
|
|
| 74 |
|
| 75 |
# Обновляем тернарные веса
|
| 76 |
bdh_model.encoder.update_ternary_weights()
|
|
@@ -82,7 +123,7 @@ bdh_model.eval()
|
|
| 82 |
print(f"\n✅ Модель готова! Параметров: {sum(p.numel() for p in bdh_model.parameters()):,}")
|
| 83 |
|
| 84 |
# -----------------------------------------------------------------------------
|
| 85 |
-
#
|
| 86 |
# -----------------------------------------------------------------------------
|
| 87 |
def text_to_bytes(text):
|
| 88 |
return torch.tensor(list(text.encode('utf-8')), dtype=torch.long).unsqueeze(0).to(device)
|
|
|
|
| 1 |
# ============================================================================
|
| 2 |
+
# ИСПРАВЛЕННАЯ ЗАГРУЗКА ВСЕХ ВЕСОВ
|
| 3 |
# ============================================================================
|
| 4 |
|
| 5 |
import torch
|
|
|
|
| 13 |
from bdh import BDH, BDHConfig
|
| 14 |
|
| 15 |
print("=" * 60)
|
| 16 |
+
print("🧪 ПОЛНОСТЬЮ ИСПРАВЛЕННОЕ ТЕСТИРОВАНИЕ")
|
| 17 |
print("=" * 60)
|
| 18 |
|
| 19 |
# -----------------------------------------------------------------------------
|
|
|
|
| 47 |
)
|
| 48 |
weights = load_file(weights_path)
|
| 49 |
|
| 50 |
+
# -----------------------------------------------------------------------------
|
| 51 |
+
# 2. Исправляем и загружаем ВСЕ веса
|
| 52 |
+
# -----------------------------------------------------------------------------
|
| 53 |
with torch.no_grad():
|
| 54 |
+
# Получаем целевые размерности
|
| 55 |
+
target_enc_shape = bdh_model.encoder.weight_fp32.shape # [32, 128, 4096]
|
| 56 |
+
target_encv_shape = bdh_model.encoder_v.weight_fp32.shape # [32, 128, 4096]
|
| 57 |
+
target_dec_shape = bdh_model.decoder.weight_fp32.shape # [4096, 4096]
|
| 58 |
+
target_lm_shape = bdh_model.lm_head.weight_fp32.shape # [4096, 256]
|
| 59 |
+
|
| 60 |
+
print("\n📐 Исправление размерностей:")
|
| 61 |
+
|
| 62 |
+
# encoder
|
| 63 |
+
enc_w = weights['encoder'].to(device)
|
| 64 |
+
print(f" encoder: {enc_w.shape} -> {target_enc_shape}")
|
| 65 |
+
if enc_w.shape == (32, 4096, 128):
|
| 66 |
+
enc_w = enc_w.transpose(1, 2).contiguous()
|
| 67 |
+
elif enc_w.shape != target_enc_shape:
|
| 68 |
+
raise ValueError(f"Неожиданная форма encoder: {enc_w.shape}")
|
| 69 |
+
|
| 70 |
+
# encoder_v
|
| 71 |
+
encv_w = weights['encoder_v'].to(device)
|
| 72 |
+
print(f" encoder_v: {encv_w.shape} -> {target_encv_shape}")
|
| 73 |
+
if encv_w.shape == (32, 4096, 128):
|
| 74 |
+
encv_w = encv_w.transpose(1, 2).contiguous()
|
| 75 |
+
elif encv_w.shape != target_encv_shape:
|
| 76 |
+
raise ValueError(f"Неожиданная форма encoder_v: {encv_w.shape}")
|
| 77 |
|
| 78 |
+
# decoder
|
| 79 |
+
dec_w = weights['decoder'].to(device)
|
| 80 |
+
print(f" decoder: {dec_w.shape} -> {target_dec_shape}")
|
| 81 |
+
if dec_w.shape == (4096, 4096):
|
| 82 |
+
pass # ОК
|
| 83 |
+
elif dec_w.shape == (4096, 4096):
|
| 84 |
+
pass
|
| 85 |
+
else:
|
| 86 |
+
# Обрезаем или дополняем
|
| 87 |
+
if dec_w.shape[0] > target_dec_shape[0]:
|
| 88 |
+
dec_w = dec_w[:target_dec_shape[0], :]
|
| 89 |
+
if dec_w.shape[1] > target_dec_shape[1]:
|
| 90 |
+
dec_w = dec_w[:, :target_dec_shape[1]]
|
| 91 |
+
if dec_w.shape[0] < target_dec_shape[0] or dec_w.shape[1] < target_dec_shape[1]:
|
| 92 |
+
new_w = torch.zeros(target_dec_shape, device=device)
|
| 93 |
+
new_w[:dec_w.shape[0], :dec_w.shape[1]] = dec_w
|
| 94 |
+
dec_w = new_w
|
| 95 |
+
print(f" decoder исправлен: {dec_w.shape}")
|
| 96 |
|
| 97 |
+
# lm_head
|
| 98 |
+
lm_w = weights['lm_head'].to(device)
|
| 99 |
+
print(f" lm_head: {lm_w.shape} -> {target_lm_shape}")
|
| 100 |
+
if lm_w.shape != target_lm_shape:
|
| 101 |
+
if lm_w.shape[0] > target_lm_shape[0]:
|
| 102 |
+
lm_w = lm_w[:target_lm_shape[0], :]
|
| 103 |
+
if lm_w.shape[1] > target_lm_shape[1]:
|
| 104 |
+
lm_w = lm_w[:, :target_lm_shape[1]]
|
| 105 |
+
if lm_w.shape[0] < target_lm_shape[0] or lm_w.shape[1] < target_lm_shape[1]:
|
| 106 |
+
new_w = torch.zeros(target_lm_shape, device=device)
|
| 107 |
+
new_w[:lm_w.shape[0], :lm_w.shape[1]] = lm_w
|
| 108 |
+
lm_w = new_w
|
| 109 |
|
| 110 |
+
# Применяем
|
| 111 |
+
bdh_model.encoder.weight_fp32.data = enc_w
|
| 112 |
+
bdh_model.encoder_v.weight_fp32.data = encv_w
|
| 113 |
+
bdh_model.decoder.weight_fp32.data = dec_w
|
| 114 |
+
bdh_model.lm_head.weight_fp32.data = lm_w
|
| 115 |
|
| 116 |
# Обновляем тернарные веса
|
| 117 |
bdh_model.encoder.update_ternary_weights()
|
|
|
|
| 123 |
print(f"\n✅ Модель готова! Параметров: {sum(p.numel() for p in bdh_model.parameters()):,}")
|
| 124 |
|
| 125 |
# -----------------------------------------------------------------------------
|
| 126 |
+
# 3. Генерация
|
| 127 |
# -----------------------------------------------------------------------------
|
| 128 |
def text_to_bytes(text):
|
| 129 |
return torch.tensor(list(text.encode('utf-8')), dtype=torch.long).unsqueeze(0).to(device)
|