Feature Extraction
Transformers
Safetensors
Spanish
English
toarim
sentence-similarity
stateful-embeddings
nested-learning
rag
industrial-rag
cpu-optimized
sherman-morrison
low-rank-svd
custom_code
Instructions to use ManyCamDS/ToarimEmbedder with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ManyCamDS/ToarimEmbedder with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="ManyCamDS/ToarimEmbedder", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("ManyCamDS/ToarimEmbedder", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload toarim_embedder.py with huggingface_hub
Browse files- toarim_embedder.py +510 -0
toarim_embedder.py
ADDED
|
@@ -0,0 +1,510 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
=============================================================================
|
| 3 |
+
TÓARIM EMBEDDER - MOTOR DE REPRESENTACIÓN VECTORIAL INDUSTRIAL CON ESTADO
|
| 4 |
+
Arquitectura: GNN Secuencial O(L) + SVD Low-Rank + Memoria Continua Titan (Sherman-Morrison)
|
| 5 |
+
Optimizado para Cómputo CPU Exclusivo (HPC) y Cumplimiento de Ciberseguridad
|
| 6 |
+
=============================================================================
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import json
|
| 11 |
+
import logging
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
from typing import List, Tuple, Optional, Union, Dict, Any
|
| 14 |
+
|
| 15 |
+
import numpy as np
|
| 16 |
+
import torch
|
| 17 |
+
import torch.nn as nn
|
| 18 |
+
import torch.nn.functional as F
|
| 19 |
+
|
| 20 |
+
# Configuración de Logging
|
| 21 |
+
logger = logging.getLogger("ToarimEmbedder")
|
| 22 |
+
|
| 23 |
+
# =======================================================================
|
| 24 |
+
# 1. ENRIQUECIMIENTO ESTRUCTURAL O(L) (GNN Lineal para Secuencias)
|
| 25 |
+
# =======================================================================
|
| 26 |
+
class DocumentGraphEnricher(nn.Module):
|
| 27 |
+
"""
|
| 28 |
+
Aplica una convolución de grafos (GCN) sobre secuencias documentales
|
| 29 |
+
con complejidad lineal O(L) en memoria y cómputo.
|
| 30 |
+
|
| 31 |
+
OPTIMIZACIÓN HPC:
|
| 32 |
+
Sustituye la matriz de adyacencia densa A_hat [L, L] de O(L^2) por
|
| 33 |
+
un operador de difusión tridiagonal local O(L) matemáticamente idéntico:
|
| 34 |
+
h_i = d_i * sum_{j in N(i)} (d_j * x_j * W)
|
| 35 |
+
Elimina por completo el riesgo de OOM DoS en secuencias extensas.
|
| 36 |
+
"""
|
| 37 |
+
def __init__(self, input_dim: int, output_dim: int):
|
| 38 |
+
super().__init__()
|
| 39 |
+
self.W_gnn = nn.Linear(input_dim, output_dim, bias=False)
|
| 40 |
+
self.activation = nn.ReLU()
|
| 41 |
+
|
| 42 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 43 |
+
"""
|
| 44 |
+
x: Tensor de shape [batch_size, seq_len, input_dim]
|
| 45 |
+
Retorna: Tensor de shape [batch_size, seq_len, output_dim]
|
| 46 |
+
"""
|
| 47 |
+
batch_size, seq_len, _ = x.shape
|
| 48 |
+
device = x.device
|
| 49 |
+
|
| 50 |
+
# 1. Proyección lineal: [B, L, D_out]
|
| 51 |
+
x_trans = self.W_gnn(x)
|
| 52 |
+
|
| 53 |
+
if seq_len == 1:
|
| 54 |
+
return self.activation(x_trans)
|
| 55 |
+
|
| 56 |
+
# 2. Grados del grafo secuencial con auto-lazos:
|
| 57 |
+
# Extremos tienen grado 2 (self + 1 vecino); nodos internos tienen grado 3 (self + 2 vecinos)
|
| 58 |
+
degrees = torch.full((seq_len,), 3.0, device=device, dtype=x.dtype)
|
| 59 |
+
degrees[0] = 2.0
|
| 60 |
+
degrees[-1] = 2.0
|
| 61 |
+
d_inv_sqrt = 1.0 / torch.sqrt(degrees) # [seq_len]
|
| 62 |
+
|
| 63 |
+
# 3. Difusión normalizada espectralmente en O(L) memoria
|
| 64 |
+
# z_j = d_j * x_trans_j
|
| 65 |
+
z = x_trans * d_inv_sqrt.unsqueeze(0).unsqueeze(2) # [B, L, D]
|
| 66 |
+
|
| 67 |
+
# Suma de vecinos: s_i = z_{i-1} + z_i + z_{i+1}
|
| 68 |
+
s = z.clone()
|
| 69 |
+
s[:, :-1, :] += z[:, 1:, :] # Conexión hacia adelante
|
| 70 |
+
s[:, 1:, :] += z[:, :-1, :] # Conexión hacia atrás
|
| 71 |
+
|
| 72 |
+
# h_i = d_i * s_i
|
| 73 |
+
h_graph = s * d_inv_sqrt.unsqueeze(0).unsqueeze(2)
|
| 74 |
+
return self.activation(h_graph)
|
| 75 |
+
|
| 76 |
+
# =======================================================================
|
| 77 |
+
# 2. COMPRESIÓN DE ESPACIO LATENTE (Low-Rank SVD Projection)
|
| 78 |
+
# =======================================================================
|
| 79 |
+
class LowRankLinear(nn.Module):
|
| 80 |
+
"""
|
| 81 |
+
Capa lineal parametrizada mediante factorización SVD: W approx U @ V.
|
| 82 |
+
Reduce la complejidad de parámetros y memoria de O(d_in * d_out) a O(k * (d_in + d_out)).
|
| 83 |
+
"""
|
| 84 |
+
def __init__(self, in_features: int, out_features: int, rank: int):
|
| 85 |
+
super().__init__()
|
| 86 |
+
self.in_features = in_features
|
| 87 |
+
self.out_features = out_features
|
| 88 |
+
self.rank = rank
|
| 89 |
+
|
| 90 |
+
self.U = nn.Parameter(torch.randn(out_features, rank) / np.sqrt(rank))
|
| 91 |
+
self.V = nn.Parameter(torch.randn(rank, in_features) / np.sqrt(in_features))
|
| 92 |
+
self.bias = nn.Parameter(torch.zeros(out_features))
|
| 93 |
+
|
| 94 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 95 |
+
W_approx = self.U @ self.V
|
| 96 |
+
return F.linear(x, W_approx, self.bias)
|
| 97 |
+
|
| 98 |
+
# =======================================================================
|
| 99 |
+
# 3. BUCLE INTERNO TITAN MEMORY (Sherman-Morrison Estable)
|
| 100 |
+
# =======================================================================
|
| 101 |
+
class TitanMemoryLayer(nn.Module):
|
| 102 |
+
"""
|
| 103 |
+
Capa de Memoria Recurrente Asociativa basada en Nested Learning.
|
| 104 |
+
Actualización matricial O(k^2) por paso mediante el Lema de Sherman-Morrison.
|
| 105 |
+
"""
|
| 106 |
+
def __init__(self, dim: int, latent_k: int):
|
| 107 |
+
super().__init__()
|
| 108 |
+
self.latent_k = latent_k
|
| 109 |
+
rank_proj = max(1, latent_k // 2)
|
| 110 |
+
|
| 111 |
+
self.proj_q = LowRankLinear(dim, latent_k, rank=rank_proj)
|
| 112 |
+
self.proj_k = LowRankLinear(dim, latent_k, rank=rank_proj)
|
| 113 |
+
self.proj_v = LowRankLinear(dim, latent_k, rank=rank_proj)
|
| 114 |
+
|
| 115 |
+
self.gate_eta = nn.Sequential(nn.Linear(dim, 1), nn.Sigmoid())
|
| 116 |
+
self.gate_alpha = nn.Sequential(nn.Linear(dim, 1), nn.Sigmoid())
|
| 117 |
+
self.proj_out = nn.Linear(latent_k, dim)
|
| 118 |
+
|
| 119 |
+
def forward(self, x: torch.Tensor, initial_state: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 120 |
+
batch_size, seq_len, _ = x.shape
|
| 121 |
+
device = x.device
|
| 122 |
+
|
| 123 |
+
if initial_state is not None:
|
| 124 |
+
M = initial_state
|
| 125 |
+
else:
|
| 126 |
+
M = torch.zeros((batch_size, self.latent_k, self.latent_k), device=device, dtype=x.dtype)
|
| 127 |
+
|
| 128 |
+
outputs = []
|
| 129 |
+
|
| 130 |
+
for t in range(seq_len):
|
| 131 |
+
x_t = x[:, t, :] # [B, dim]
|
| 132 |
+
|
| 133 |
+
q_t = self.proj_q(x_t) # [B, k]
|
| 134 |
+
k_t = self.proj_k(x_t)
|
| 135 |
+
v_t = self.proj_v(x_t)
|
| 136 |
+
|
| 137 |
+
eta_t = self.gate_eta(x_t) # [B, 1]
|
| 138 |
+
alpha_t = self.gate_alpha(x_t) # [B, 1]
|
| 139 |
+
|
| 140 |
+
# Inferencia asociativa: y_t = M * q_t
|
| 141 |
+
y_t = torch.bmm(M, q_t.unsqueeze(2)).squeeze(2)
|
| 142 |
+
|
| 143 |
+
# Error / Sorpresa: e_t = v_t - M * k_t
|
| 144 |
+
m_k = torch.bmm(M, k_t.unsqueeze(2)).squeeze(2)
|
| 145 |
+
e_t = v_t - m_k
|
| 146 |
+
|
| 147 |
+
# Sherman-Morrison: Denominador con regularización epsilon para estabilidad numérica
|
| 148 |
+
k_norm_sq = torch.sum(k_t ** 2, dim=1, keepdim=True)
|
| 149 |
+
denom = 1.0 + (eta_t * k_norm_sq) + 1e-7
|
| 150 |
+
update_factor = (eta_t / denom).unsqueeze(2) # [B, 1, 1]
|
| 151 |
+
|
| 152 |
+
outer_prod = torch.bmm(e_t.unsqueeze(2), k_t.unsqueeze(1)) # [B, k, k]
|
| 153 |
+
M = (alpha_t.unsqueeze(2) * M) + (update_factor * outer_prod)
|
| 154 |
+
|
| 155 |
+
outputs.append(y_t)
|
| 156 |
+
|
| 157 |
+
Y = torch.stack(outputs, dim=1) # [B, seq_len, k]
|
| 158 |
+
return self.proj_out(Y), M
|
| 159 |
+
|
| 160 |
+
# =======================================================================
|
| 161 |
+
# 4. MODELO COMPLETO TÓARIM (Graph + Titan + Residual Pooling)
|
| 162 |
+
# =======================================================================
|
| 163 |
+
class TóarimEmbeddingModel(nn.Module):
|
| 164 |
+
"""
|
| 165 |
+
Modelo de Embedding Integral Tóarim.
|
| 166 |
+
Token Embedding -> GNN Secuencial O(L) -> Titan Memory -> Residual LayerNorm -> Mean Pooling -> L2 Norm.
|
| 167 |
+
"""
|
| 168 |
+
def __init__(self, vocab_size: int = 30522, embed_dim: int = 768, latent_k: int = 128):
|
| 169 |
+
super().__init__()
|
| 170 |
+
self.vocab_size = vocab_size
|
| 171 |
+
self.embed_dim = embed_dim
|
| 172 |
+
self.latent_k = latent_k
|
| 173 |
+
|
| 174 |
+
self.token_embedding = nn.Embedding(vocab_size, embed_dim)
|
| 175 |
+
self.gnn_enricher = DocumentGraphEnricher(embed_dim, embed_dim)
|
| 176 |
+
self.titan_memory = TitanMemoryLayer(embed_dim, latent_k)
|
| 177 |
+
self.layer_norm = nn.LayerNorm(embed_dim)
|
| 178 |
+
|
| 179 |
+
def forward(self, input_ids: torch.Tensor, initial_state: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 180 |
+
x = self.token_embedding(input_ids)
|
| 181 |
+
x_graph = self.gnn_enricher(x)
|
| 182 |
+
x_titan, M_final = self.titan_memory(x_graph, initial_state)
|
| 183 |
+
x_norm = self.layer_norm(x_titan + x)
|
| 184 |
+
|
| 185 |
+
doc_embedding = torch.mean(x_norm, dim=1)
|
| 186 |
+
normalized_emb = F.normalize(doc_embedding, p=2, dim=1)
|
| 187 |
+
return normalized_emb, M_final
|
| 188 |
+
|
| 189 |
+
@classmethod
|
| 190 |
+
def from_pretrained(cls, pretrained_model_name_or_path: Union[str, Path], **kwargs) -> "TóarimEmbeddingModel":
|
| 191 |
+
"""Carga los pesos desde model.safetensors (local o Hugging Face Hub) en el módulo PyTorch."""
|
| 192 |
+
embedder = TóarimEmbedder.from_pretrained(pretrained_model_name_or_path, **kwargs)
|
| 193 |
+
return embedder.model
|
| 194 |
+
|
| 195 |
+
# =======================================================================
|
| 196 |
+
# 5. API DE PRODUCCIÓN TÓARIM (HPC + Ciberseguridad + LangChain/HF)
|
| 197 |
+
# =======================================================================
|
| 198 |
+
class TóarimEmbedder:
|
| 199 |
+
"""
|
| 200 |
+
Interfaz de producción para TóarimEmbedder.
|
| 201 |
+
|
| 202 |
+
MEDIDAS DE CIBERSEGURIDAD:
|
| 203 |
+
- Serialización segura exclusiva con Safetensors (previene vulnerabilidades de RCE por Pickle).
|
| 204 |
+
- Despliegue Air-Gapped nativo: Detección y carga local de tokenizer.json sin salida a Internet.
|
| 205 |
+
- Sanitización y aislamiento de memoria multi-inquilino mediante reset_state().
|
| 206 |
+
- Validación y truncamiento de entradas para prevenir ataques DoS por agotamiento de RAM.
|
| 207 |
+
|
| 208 |
+
OPTIMIZACIONES HPC EN CPU:
|
| 209 |
+
- Convolución secuencial O(L) de memoria plana sin sobrecarga cuadrática.
|
| 210 |
+
- Aceleración con torch.autocast(dtype=torch.bfloat16) en procesadores compatibles (AVX-512_BF16 / AMX).
|
| 211 |
+
- Soporte integrado de cuantización dinámica INT8 (quantize()) para VNNI.
|
| 212 |
+
- Tokenización multi-hilo nativa en Rust sin sobrecarga del runtime de transformers.
|
| 213 |
+
"""
|
| 214 |
+
def __init__(
|
| 215 |
+
self,
|
| 216 |
+
vocab_size: int = 30522,
|
| 217 |
+
embed_dim: int = 768,
|
| 218 |
+
latent_k: int = 128,
|
| 219 |
+
model_path: Optional[str] = None,
|
| 220 |
+
compile_model: bool = False,
|
| 221 |
+
device: Optional[str] = None,
|
| 222 |
+
tokenizer_name_or_path: str = "bert-base-uncased",
|
| 223 |
+
local_files_only: bool = False
|
| 224 |
+
):
|
| 225 |
+
self.vocab_size = vocab_size
|
| 226 |
+
self.embed_dim = embed_dim
|
| 227 |
+
self.latent_k = latent_k
|
| 228 |
+
self.is_quantized = False
|
| 229 |
+
self._current_memory_state: Optional[torch.Tensor] = None
|
| 230 |
+
|
| 231 |
+
# Selecci��n de dispositivo y optimización de hilos en CPU
|
| 232 |
+
if device is None:
|
| 233 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 234 |
+
else:
|
| 235 |
+
self.device = torch.device(device)
|
| 236 |
+
|
| 237 |
+
if self.device.type == 'cpu':
|
| 238 |
+
# Evita sobre-suscripción y contención de hilos en servidores multi-socket NUMA
|
| 239 |
+
if hasattr(torch, "get_num_interop_threads") and torch.get_num_interop_threads() > 1:
|
| 240 |
+
try:
|
| 241 |
+
torch.set_num_interop_threads(1)
|
| 242 |
+
except RuntimeError:
|
| 243 |
+
pass
|
| 244 |
+
|
| 245 |
+
# -------------------------------------------------------------------
|
| 246 |
+
# Tokenizador Seguro y Air-Gapped (Carga Local Prioritaria)
|
| 247 |
+
# -------------------------------------------------------------------
|
| 248 |
+
from tokenizers import Tokenizer
|
| 249 |
+
|
| 250 |
+
# 1. Verificar si existe tokenizer.json en la ruta indicada o en el directorio local
|
| 251 |
+
local_candidate = Path(tokenizer_name_or_path)
|
| 252 |
+
if local_candidate.is_dir() and (local_candidate / "tokenizer.json").exists():
|
| 253 |
+
tok_path = str(local_candidate / "tokenizer.json")
|
| 254 |
+
self.tokenizer = Tokenizer.from_file(tok_path)
|
| 255 |
+
elif local_candidate.is_file():
|
| 256 |
+
self.tokenizer = Tokenizer.from_file(str(local_candidate))
|
| 257 |
+
elif Path("tokenizer.json").exists():
|
| 258 |
+
# Fallback seguro offline en la raíz del proyecto
|
| 259 |
+
self.tokenizer = Tokenizer.from_file("tokenizer.json")
|
| 260 |
+
elif not local_files_only:
|
| 261 |
+
# Descarga remota autorizada solo si no es entorno air-gapped estricto
|
| 262 |
+
self.tokenizer = Tokenizer.from_pretrained(tokenizer_name_or_path)
|
| 263 |
+
else:
|
| 264 |
+
raise FileNotFoundError(
|
| 265 |
+
f"Modo Air-Gapped activado: No se localizó 'tokenizer.json' en '{tokenizer_name_or_path}'."
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
self.tokenizer.no_padding()
|
| 269 |
+
self.tokenizer.no_truncation()
|
| 270 |
+
|
| 271 |
+
# Inicializar modelo
|
| 272 |
+
self.model = TóarimEmbeddingModel(vocab_size, embed_dim, latent_k).to(self.device)
|
| 273 |
+
|
| 274 |
+
# -------------------------------------------------------------------
|
| 275 |
+
# Carga Segura de Pesos (Safetensors / Weights-Only)
|
| 276 |
+
# -------------------------------------------------------------------
|
| 277 |
+
if model_path:
|
| 278 |
+
self._load_weights_safely(model_path)
|
| 279 |
+
|
| 280 |
+
self.model.eval()
|
| 281 |
+
|
| 282 |
+
if compile_model:
|
| 283 |
+
try:
|
| 284 |
+
self.model = torch.compile(self.model)
|
| 285 |
+
except Exception as e:
|
| 286 |
+
logger.warning(f"No fue posible aplicar torch.compile: {e}. Continuando en modo estándar.")
|
| 287 |
+
|
| 288 |
+
def _load_weights_safely(self, model_path: str):
|
| 289 |
+
"""Carga pesos aplicando blindaje contra deserialización maliciosa."""
|
| 290 |
+
path = Path(model_path)
|
| 291 |
+
if not path.exists():
|
| 292 |
+
raise FileNotFoundError(f"Archivo de pesos no encontrado: {model_path}")
|
| 293 |
+
|
| 294 |
+
if path.suffix == ".safetensors":
|
| 295 |
+
from safetensors.torch import load_file
|
| 296 |
+
state_dict = load_file(str(path), device=str(self.device))
|
| 297 |
+
self.model.load_state_dict(state_dict)
|
| 298 |
+
else:
|
| 299 |
+
# Blindaje contra RCE en archivos .pt/.bin mediante weights_only=True
|
| 300 |
+
state_dict = torch.load(str(path), map_location=self.device, weights_only=True)
|
| 301 |
+
self.model.load_state_dict(state_dict)
|
| 302 |
+
|
| 303 |
+
# ===================================================================
|
| 304 |
+
# Métodos de Ciberseguridad: Sanitización de Memoria
|
| 305 |
+
# ===================================================================
|
| 306 |
+
|
| 307 |
+
def reset_state(self):
|
| 308 |
+
"""
|
| 309 |
+
Higienización y sanitización operativa del estado de memoria residual M.
|
| 310 |
+
OBLIGATORIO en entornos multi-inquilino o al cambiar entre manuales técnicos
|
| 311 |
+
para prevenir la fuga de información confidencial (Cross-Tenant Memory Leak).
|
| 312 |
+
"""
|
| 313 |
+
self._current_memory_state = None
|
| 314 |
+
if self.device.type == 'cuda':
|
| 315 |
+
torch.cuda.empty_cache()
|
| 316 |
+
|
| 317 |
+
# ===================================================================
|
| 318 |
+
# Métodos de HPC: Cuantización Dinámica INT8 en CPU
|
| 319 |
+
# ===================================================================
|
| 320 |
+
|
| 321 |
+
def quantize(self):
|
| 322 |
+
"""
|
| 323 |
+
Aplica cuantización dinámica INT8 sobre capas lineales.
|
| 324 |
+
Acelera la inferencia en CPUs modernas x86/ARM con instrucciones VNNI/AMX,
|
| 325 |
+
reduciendo el consumo de RAM en ~50%.
|
| 326 |
+
"""
|
| 327 |
+
if self.device.type != 'cpu':
|
| 328 |
+
logger.warning("La cuantización INT8 está recomendada exclusivamente para inferencia en CPU.")
|
| 329 |
+
return self
|
| 330 |
+
|
| 331 |
+
self.model = torch.ao.quantization.quantize_dynamic(
|
| 332 |
+
self.model,
|
| 333 |
+
{nn.Linear, LowRankLinear},
|
| 334 |
+
dtype=torch.qint8
|
| 335 |
+
)
|
| 336 |
+
self.is_quantized = True
|
| 337 |
+
return self
|
| 338 |
+
|
| 339 |
+
# ===================================================================
|
| 340 |
+
# Inferencia y Codificación de Vectores
|
| 341 |
+
# ===================================================================
|
| 342 |
+
|
| 343 |
+
@torch.no_grad()
|
| 344 |
+
def encode(self, tokenized_chunks: List[List[int]], maintain_state: bool = False) -> np.ndarray:
|
| 345 |
+
"""
|
| 346 |
+
Genera embeddings para una lista de secuencias tokenizadas.
|
| 347 |
+
- maintain_state=True: Ingesta continua arrastrando la memoria Titan M entre chunks.
|
| 348 |
+
- maintain_state=False: Procesamiento independiente en paralelo.
|
| 349 |
+
"""
|
| 350 |
+
if not tokenized_chunks:
|
| 351 |
+
return np.empty((0, self.embed_dim), dtype=np.float32)
|
| 352 |
+
|
| 353 |
+
# Validación DoS: Sanitización de entradas
|
| 354 |
+
for chunk in tokenized_chunks:
|
| 355 |
+
if not isinstance(chunk, list) or len(chunk) == 0:
|
| 356 |
+
raise ValueError("Cada chunk tokenizado debe ser una lista no vacía de enteros.")
|
| 357 |
+
|
| 358 |
+
device_type = 'cuda' if self.device.type == 'cuda' else 'cpu'
|
| 359 |
+
enable_autocast = (device_type == 'cuda') and not self.is_quantized
|
| 360 |
+
|
| 361 |
+
with torch.autocast(device_type=device_type, dtype=torch.bfloat16, enabled=enable_autocast):
|
| 362 |
+
if maintain_state:
|
| 363 |
+
embeddings = []
|
| 364 |
+
M_state = self._current_memory_state
|
| 365 |
+
|
| 366 |
+
for chunk in tokenized_chunks:
|
| 367 |
+
input_tensor = torch.tensor([chunk], dtype=torch.long, device=self.device)
|
| 368 |
+
emb, M_state = self.model(input_tensor, initial_state=M_state)
|
| 369 |
+
embeddings.append(emb)
|
| 370 |
+
|
| 371 |
+
self._current_memory_state = M_state
|
| 372 |
+
embeddings = torch.cat(embeddings, dim=0)
|
| 373 |
+
return embeddings.float().cpu().numpy()
|
| 374 |
+
else:
|
| 375 |
+
max_len = max(len(chunk) for chunk in tokenized_chunks)
|
| 376 |
+
padded_inputs = [chunk + [0] * (max_len - len(chunk)) for chunk in tokenized_chunks]
|
| 377 |
+
input_tensor = torch.tensor(padded_inputs, dtype=torch.long, device=self.device)
|
| 378 |
+
embeddings, _ = self.model(input_tensor)
|
| 379 |
+
return embeddings.float().cpu().numpy()
|
| 380 |
+
|
| 381 |
+
# ===================================================================
|
| 382 |
+
# Interfaces Compatibles con LangChain / LlamaIndex / Vectorstores
|
| 383 |
+
# ===================================================================
|
| 384 |
+
|
| 385 |
+
def _tokenize_text(self, text: str, max_tokens: int = 512) -> List[int]:
|
| 386 |
+
"""Tokenización segura con sanitización de longitud y filtrado de tokens de control."""
|
| 387 |
+
if not isinstance(text, str):
|
| 388 |
+
text = str(text)
|
| 389 |
+
output = self.tokenizer.encode(text)
|
| 390 |
+
ids = [t for t in output.ids if t not in (101, 102)]
|
| 391 |
+
return ids[:max_tokens] if ids else [0]
|
| 392 |
+
|
| 393 |
+
def embed_documents(self, texts: List[str], maintain_state: bool = False) -> List[List[float]]:
|
| 394 |
+
"""
|
| 395 |
+
Interfaz LangChain/ChromaDB: Embebe una lista de textos documentales.
|
| 396 |
+
Usa tokenización multi-hilo en Rust.
|
| 397 |
+
"""
|
| 398 |
+
if not texts:
|
| 399 |
+
return []
|
| 400 |
+
|
| 401 |
+
batch_output = self.tokenizer.encode_batch([t if isinstance(t, str) else str(t) for t in texts])
|
| 402 |
+
tokenized = []
|
| 403 |
+
for enc in batch_output:
|
| 404 |
+
ids = [t for t in enc.ids if t not in (101, 102)][:512]
|
| 405 |
+
tokenized.append(ids if ids else [0])
|
| 406 |
+
|
| 407 |
+
embeddings = self.encode(tokenized, maintain_state=maintain_state)
|
| 408 |
+
return embeddings.tolist()
|
| 409 |
+
|
| 410 |
+
def embed_query(self, text: str) -> List[float]:
|
| 411 |
+
"""
|
| 412 |
+
Interfaz LangChain: Embebe una consulta de usuario.
|
| 413 |
+
Siempre opera en modo STATELESS para garantizar una búsqueda sin sesgos.
|
| 414 |
+
"""
|
| 415 |
+
tokenized = self._tokenize_text(text)
|
| 416 |
+
embedding = self.encode([tokenized], maintain_state=False)
|
| 417 |
+
return embedding[0].tolist()
|
| 418 |
+
|
| 419 |
+
# ===================================================================
|
| 420 |
+
# Guardado y Carga de Hugging Face (Formatos Abiertos y Seguros)
|
| 421 |
+
# ===================================================================
|
| 422 |
+
|
| 423 |
+
def save_pretrained(self, save_directory: Union[str, Path]):
|
| 424 |
+
"""
|
| 425 |
+
Exporta el modelo en el formato estándar de Hugging Face Hub:
|
| 426 |
+
- model.safetensors (pesos seguros)
|
| 427 |
+
- config.json (arquitectura y auto_map)
|
| 428 |
+
- tokenizer.json (tokenizador WordPiece en Rust)
|
| 429 |
+
"""
|
| 430 |
+
save_dir = Path(save_directory)
|
| 431 |
+
save_dir.mkdir(parents=True, exist_ok=True)
|
| 432 |
+
|
| 433 |
+
# 1. Guardar pesos en safetensors
|
| 434 |
+
from safetensors.torch import save_file
|
| 435 |
+
weights_path = save_dir / "model.safetensors"
|
| 436 |
+
save_file(self.model.state_dict(), str(weights_path))
|
| 437 |
+
|
| 438 |
+
# 2. Guardar config.json
|
| 439 |
+
config_data = {
|
| 440 |
+
"architectures": ["TóarimEmbeddingModel"],
|
| 441 |
+
"model_type": "toarim",
|
| 442 |
+
"vocab_size": self.vocab_size,
|
| 443 |
+
"embed_dim": self.embed_dim,
|
| 444 |
+
"latent_k": self.latent_k,
|
| 445 |
+
"auto_map": {
|
| 446 |
+
"AutoModel": "toarim_embedder.TóarimEmbeddingModel"
|
| 447 |
+
}
|
| 448 |
+
}
|
| 449 |
+
with open(save_dir / "config.json", "w", encoding="utf-8") as f:
|
| 450 |
+
json.dump(config_data, f, indent=2)
|
| 451 |
+
|
| 452 |
+
# 3. Guardar tokenizer.json
|
| 453 |
+
self.tokenizer.save(str(save_dir / "tokenizer.json"))
|
| 454 |
+
logger.info(f"Modelo Tóarim exportado exitosamente en: {save_dir}")
|
| 455 |
+
|
| 456 |
+
@classmethod
|
| 457 |
+
def from_pretrained(cls, pretrained_model_name_or_path: Union[str, Path], **kwargs) -> "TóarimEmbedder":
|
| 458 |
+
"""
|
| 459 |
+
Carga una instancia de TóarimEmbedder desde un directorio local o repositorio de HF.
|
| 460 |
+
"""
|
| 461 |
+
model_dir = Path(pretrained_model_name_or_path)
|
| 462 |
+
|
| 463 |
+
if model_dir.is_dir():
|
| 464 |
+
config_path = model_dir / "config.json"
|
| 465 |
+
weights_path = model_dir / "model.safetensors"
|
| 466 |
+
tokenizer_path = model_dir / "tokenizer.json"
|
| 467 |
+
|
| 468 |
+
vocab_size = 30522
|
| 469 |
+
embed_dim = 768
|
| 470 |
+
latent_k = 128
|
| 471 |
+
|
| 472 |
+
if config_path.exists():
|
| 473 |
+
with open(config_path, "r", encoding="utf-8") as f:
|
| 474 |
+
cfg = json.load(f)
|
| 475 |
+
vocab_size = cfg.get("vocab_size", vocab_size)
|
| 476 |
+
embed_dim = cfg.get("embed_dim", embed_dim)
|
| 477 |
+
latent_k = cfg.get("latent_k", latent_k)
|
| 478 |
+
|
| 479 |
+
return cls(
|
| 480 |
+
vocab_size=vocab_size,
|
| 481 |
+
embed_dim=embed_dim,
|
| 482 |
+
latent_k=latent_k,
|
| 483 |
+
model_path=str(weights_path) if weights_path.exists() else None,
|
| 484 |
+
tokenizer_name_or_path=str(tokenizer_path) if tokenizer_path.exists() else "bert-base-uncased",
|
| 485 |
+
**kwargs
|
| 486 |
+
)
|
| 487 |
+
else:
|
| 488 |
+
# Descarga remota desde Hugging Face Hub mediante huggingface_hub
|
| 489 |
+
from huggingface_hub import hf_hub_download
|
| 490 |
+
weights_file = hf_hub_download(repo_id=str(pretrained_model_name_or_path), filename="model.safetensors")
|
| 491 |
+
config_file = hf_hub_download(repo_id=str(pretrained_model_name_or_path), filename="config.json")
|
| 492 |
+
tokenizer_file = hf_hub_download(repo_id=str(pretrained_model_name_or_path), filename="tokenizer.json")
|
| 493 |
+
|
| 494 |
+
with open(config_file, "r", encoding="utf-8") as f:
|
| 495 |
+
cfg = json.load(f)
|
| 496 |
+
|
| 497 |
+
return cls(
|
| 498 |
+
vocab_size=cfg.get("vocab_size", 30522),
|
| 499 |
+
embed_dim=cfg.get("embed_dim", 768),
|
| 500 |
+
latent_k=cfg.get("latent_k", 128),
|
| 501 |
+
model_path=weights_file,
|
| 502 |
+
tokenizer_name_or_path=tokenizer_file,
|
| 503 |
+
**kwargs
|
| 504 |
+
)
|
| 505 |
+
|
| 506 |
+
|
| 507 |
+
# Alias ASCII estándar para máxima compatibilidad en importaciones
|
| 508 |
+
ToarimEmbeddingModel = TóarimEmbeddingModel
|
| 509 |
+
ToarimEmbedder = TóarimEmbedder
|
| 510 |
+
ToarimEMbedder = TóarimEmbedder
|