Upload 2 files
Browse files- main.py +64 -99
- model_Custm.py +25 -13
main.py
CHANGED
|
@@ -1,59 +1,54 @@
|
|
| 1 |
-
# Main.py
|
| 2 |
-
import sys
|
| 3 |
-
sys.stdout.reconfigure(line_buffering=True) # Force line buffering
|
| 4 |
-
import logging
|
| 5 |
-
import sys
|
| 6 |
-
logging.basicConfig(
|
| 7 |
-
level=logging.DEBUG,
|
| 8 |
-
stream=sys.stdout,
|
| 9 |
-
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
| 10 |
-
force=True
|
| 11 |
-
)
|
| 12 |
import os
|
| 13 |
import sys
|
| 14 |
import time
|
| 15 |
-
import
|
|
|
|
| 16 |
import logging
|
| 17 |
import argparse
|
| 18 |
import importlib
|
|
|
|
| 19 |
from typing import Dict, Any, Optional, List, Union, Generator, Tuple
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
import torch
|
| 24 |
-
if torch.cuda.is_available():
|
| 25 |
-
torch.cuda.empty_cache()
|
| 26 |
-
print(f"GPU Memory: {torch.cuda.memory_allocated()/1e9:.2f}GB / {torch.cuda.get_device_properties(0).total_memory/1e9:.2f}GB")
|
| 27 |
-
except Exception as e:
|
| 28 |
-
print(f"Error checking GPU memory: {e}")
|
| 29 |
|
| 30 |
-
# Configure logging
|
| 31 |
-
logging.basicConfig(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
logger = logging.getLogger(__name__)
|
| 33 |
|
| 34 |
-
# Add file
|
| 35 |
file_handler = logging.FileHandler('/tmp/app_debug.log')
|
| 36 |
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
| 37 |
logger.addHandler(file_handler)
|
| 38 |
-
logger.info("File logging enabled at /tmp/app_debug.log")
|
| 39 |
|
| 40 |
-
# Add a file handler to persist logs
|
| 41 |
fh = logging.FileHandler("/tmp/container.log")
|
| 42 |
fh.setLevel(logging.DEBUG)
|
| 43 |
-
|
| 44 |
-
fh.setFormatter(formatter)
|
| 45 |
logging.getLogger().addHandler(fh)
|
|
|
|
| 46 |
|
| 47 |
# Force early initialization of vital environment variables
|
| 48 |
if not os.environ.get("TLM_DATA_DIR"):
|
| 49 |
os.environ["TLM_DATA_DIR"] = "/tmp/tlm_data"
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
logger.info("
|
| 54 |
|
|
|
|
| 55 |
try:
|
| 56 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
if hasattr(torch, 'set_default_device'):
|
| 58 |
torch.set_default_device("cpu")
|
| 59 |
_original_device = torch.device
|
|
@@ -62,11 +57,11 @@ try:
|
|
| 62 |
torch.device = patched_device
|
| 63 |
logger.info("✅ Applied PyTorch device attribute fix")
|
| 64 |
except Exception as e:
|
| 65 |
-
logger.warning(f"Error
|
| 66 |
|
| 67 |
# Import configuration
|
| 68 |
try:
|
| 69 |
-
from config import app_config, load_config
|
| 70 |
|
| 71 |
# Create an emergency patch for config.py issue
|
| 72 |
if hasattr(app_config, 'TRANSFORMER_CONFIG'):
|
|
@@ -100,9 +95,9 @@ except ImportError:
|
|
| 100 |
|
| 101 |
# Import service registry
|
| 102 |
try:
|
| 103 |
-
from service_registry import registry, MODEL, TOKENIZER, MODEL_MANAGER, COMMUNICATOR, PIPELINE
|
| 104 |
|
| 105 |
-
# Import event system types
|
| 106 |
from utils.event_system import (
|
| 107 |
EVENT_STDP_REQUEST, EVENT_STDP_RESPONSE, EVENT_TOKEN_GENERATED,
|
| 108 |
EVENT_USER_INPUT, EVENT_MODEL_REQUEST, EVENT_MODEL_RESPONSE,
|
|
@@ -133,6 +128,7 @@ except ImportError as e:
|
|
| 133 |
MODEL_MANAGER = "model_manager"
|
| 134 |
COMMUNICATOR = "communicator"
|
| 135 |
PIPELINE = "pipeline"
|
|
|
|
| 136 |
|
| 137 |
# Define minimal event constants
|
| 138 |
EVENT_STDP_REQUEST = "stdp_request"
|
|
@@ -151,7 +147,7 @@ except ImportError as e:
|
|
| 151 |
|
| 152 |
event_bus = EventBus()
|
| 153 |
|
| 154 |
-
from find_weights import find_transformer_weights, find_snn_weights
|
| 155 |
|
| 156 |
# Import API components
|
| 157 |
try:
|
|
@@ -169,7 +165,8 @@ except Exception as e:
|
|
| 169 |
def verify_model_repo_access():
|
| 170 |
return False
|
| 171 |
|
| 172 |
-
#
|
|
|
|
| 173 |
def fix_config_file(config_path="config.json"):
|
| 174 |
"""Fix the config file directly"""
|
| 175 |
import os
|
|
@@ -788,53 +785,6 @@ def setup_environment(args):
|
|
| 788 |
model_dir = os.path.join(data_dir, "models")
|
| 789 |
os.makedirs(model_dir, exist_ok=True)
|
| 790 |
|
| 791 |
-
import logging
|
| 792 |
-
from app import app as fastapi_app
|
| 793 |
-
|
| 794 |
-
logger = logging.getLogger(__name__)
|
| 795 |
-
logging.basicConfig(level=logging.INFO)
|
| 796 |
-
|
| 797 |
-
def main():
|
| 798 |
-
logger.info("Starting TLM application")
|
| 799 |
-
# serve FastAPI instead of Gradio
|
| 800 |
-
import uvicorn
|
| 801 |
-
uvicorn.run(
|
| 802 |
-
"app:app",
|
| 803 |
-
host="0.0.0.0",
|
| 804 |
-
port=int(os.getenv("PORT",7860)),
|
| 805 |
-
workers=os.cpu_count() or 1,
|
| 806 |
-
loop="auto"
|
| 807 |
-
)
|
| 808 |
-
|
| 809 |
-
if __name__ == "__main__":
|
| 810 |
-
import os
|
| 811 |
-
import uvicorn
|
| 812 |
-
from app import app
|
| 813 |
-
|
| 814 |
-
if __name__ == "__main__":
|
| 815 |
-
uvicorn.run(
|
| 816 |
-
app,
|
| 817 |
-
host="0.0.0.0",
|
| 818 |
-
port=int(os.getenv("PORT", 7860)),
|
| 819 |
-
workers=os.cpu_count() or 1
|
| 820 |
-
)
|
| 821 |
-
|
| 822 |
-
# Create a main.py that properly initializes everything
|
| 823 |
-
|
| 824 |
-
import os
|
| 825 |
-
import sys
|
| 826 |
-
import logging
|
| 827 |
-
|
| 828 |
-
logging.basicConfig(level=logging.INFO)
|
| 829 |
-
logger = logging.getLogger(__name__)
|
| 830 |
-
|
| 831 |
-
# Add near the top, after imports but before loading model modules
|
| 832 |
-
try:
|
| 833 |
-
import generate_tokens_fix
|
| 834 |
-
logger.info("Applied generate_tokens fix to prevent recursion errors")
|
| 835 |
-
except Exception as e:
|
| 836 |
-
logger.error(f"Failed to apply generate_tokens fix: {e}")
|
| 837 |
-
|
| 838 |
def initialize_system():
|
| 839 |
"""Initialize all components in the correct order"""
|
| 840 |
logger.info("Starting system initialization")
|
|
@@ -852,13 +802,12 @@ def initialize_system():
|
|
| 852 |
tokenizer = TokenizerWrapper(model_name="gpt2")
|
| 853 |
|
| 854 |
# Then register tokenizer
|
| 855 |
-
from service_registry import registry, TOKENIZER, PRETRAINED_MODEL
|
| 856 |
registry.register(TOKENIZER, tokenizer, overwrite=True)
|
| 857 |
logger.info("Tokenizer registered")
|
| 858 |
|
| 859 |
# Initialize pretrained model (GPT-2)
|
| 860 |
try:
|
| 861 |
-
from model_PrTr import
|
| 862 |
pretrained = PretrainedModel(model_name="gpt2", tokenizer=tokenizer)
|
| 863 |
registry.register(PRETRAINED_MODEL, pretrained, overwrite=True)
|
| 864 |
logger.info("GPT-2 pretrained model registered")
|
|
@@ -868,24 +817,26 @@ def initialize_system():
|
|
| 868 |
# Now load custom model
|
| 869 |
try:
|
| 870 |
from model_Custm import Wildnerve_tlm01
|
|
|
|
|
|
|
|
|
|
| 871 |
model = Wildnerve_tlm01(
|
| 872 |
-
vocab_size=
|
| 873 |
specialization="general",
|
| 874 |
dataset_path=None,
|
| 875 |
model_name="gpt2",
|
| 876 |
-
embedding_dim=
|
| 877 |
-
num_heads=
|
| 878 |
-
hidden_dim=
|
| 879 |
-
num_layers=
|
| 880 |
-
output_size=
|
| 881 |
-
dropout=
|
| 882 |
-
max_seq_length=
|
| 883 |
pooling_mode="last",
|
| 884 |
tokenizer=tokenizer
|
| 885 |
)
|
| 886 |
|
| 887 |
# Register model
|
| 888 |
-
from service_registry import MODEL
|
| 889 |
registry.register(MODEL, model, overwrite=True)
|
| 890 |
logger.info("Custom model registered successfully")
|
| 891 |
return True
|
|
@@ -893,10 +844,24 @@ def initialize_system():
|
|
| 893 |
logger.error(f"Failed to initialize custom model: {e}", exc_info=True)
|
| 894 |
return False
|
| 895 |
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 899 |
|
| 900 |
if __name__ == "__main__":
|
| 901 |
-
|
| 902 |
-
logger.info(f"Initialization {'successful' if success else 'failed'}")
|
|
|
|
| 1 |
+
# Main.py - Main entry point for Wildnerve-tlm_HF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import time
|
| 5 |
+
import json
|
| 6 |
+
import gc
|
| 7 |
import logging
|
| 8 |
import argparse
|
| 9 |
import importlib
|
| 10 |
+
import threading
|
| 11 |
from typing import Dict, Any, Optional, List, Union, Generator, Tuple
|
| 12 |
+
from pathlib import Path
|
| 13 |
|
| 14 |
+
# Set up line buffering early
|
| 15 |
+
sys.stdout.reconfigure(line_buffering=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Configure logging once at the top level
|
| 18 |
+
logging.basicConfig(
|
| 19 |
+
level=logging.INFO,
|
| 20 |
+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
| 21 |
+
force=True
|
| 22 |
+
)
|
| 23 |
logger = logging.getLogger(__name__)
|
| 24 |
|
| 25 |
+
# Add file handlers for persistent logs
|
| 26 |
file_handler = logging.FileHandler('/tmp/app_debug.log')
|
| 27 |
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
| 28 |
logger.addHandler(file_handler)
|
|
|
|
| 29 |
|
|
|
|
| 30 |
fh = logging.FileHandler("/tmp/container.log")
|
| 31 |
fh.setLevel(logging.DEBUG)
|
| 32 |
+
fh.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levellevel)s - %(message)s"))
|
|
|
|
| 33 |
logging.getLogger().addHandler(fh)
|
| 34 |
+
logger.info("Logging configured")
|
| 35 |
|
| 36 |
# Force early initialization of vital environment variables
|
| 37 |
if not os.environ.get("TLM_DATA_DIR"):
|
| 38 |
os.environ["TLM_DATA_DIR"] = "/tmp/tlm_data"
|
| 39 |
|
| 40 |
+
# Select GPU if available
|
| 41 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 42 |
+
logger.info(f"Using device: {device}")
|
| 43 |
|
| 44 |
+
# Add GPU memory monitoring
|
| 45 |
try:
|
| 46 |
import torch
|
| 47 |
+
if torch.cuda.is_available():
|
| 48 |
+
torch.cuda.empty_cache()
|
| 49 |
+
logger.info(f"GPU Memory: {torch.cuda.memory_allocated()/1e9:.2f}GB / {torch.cuda.get_device_properties(0).total_memory/1e9:.2f}GB")
|
| 50 |
+
|
| 51 |
+
# Apply PyTorch device fixes
|
| 52 |
if hasattr(torch, 'set_default_device'):
|
| 53 |
torch.set_default_device("cpu")
|
| 54 |
_original_device = torch.device
|
|
|
|
| 57 |
torch.device = patched_device
|
| 58 |
logger.info("✅ Applied PyTorch device attribute fix")
|
| 59 |
except Exception as e:
|
| 60 |
+
logger.warning(f"Error with PyTorch setup: {e}")
|
| 61 |
|
| 62 |
# Import configuration
|
| 63 |
try:
|
| 64 |
+
from config import app_config, load_config, get_model_architecture_params
|
| 65 |
|
| 66 |
# Create an emergency patch for config.py issue
|
| 67 |
if hasattr(app_config, 'TRANSFORMER_CONFIG'):
|
|
|
|
| 95 |
|
| 96 |
# Import service registry
|
| 97 |
try:
|
| 98 |
+
from service_registry import registry, MODEL, TOKENIZER, MODEL_MANAGER, COMMUNICATOR, PIPELINE, PRETRAINED_MODEL
|
| 99 |
|
| 100 |
+
# Import event system types
|
| 101 |
from utils.event_system import (
|
| 102 |
EVENT_STDP_REQUEST, EVENT_STDP_RESPONSE, EVENT_TOKEN_GENERATED,
|
| 103 |
EVENT_USER_INPUT, EVENT_MODEL_REQUEST, EVENT_MODEL_RESPONSE,
|
|
|
|
| 128 |
MODEL_MANAGER = "model_manager"
|
| 129 |
COMMUNICATOR = "communicator"
|
| 130 |
PIPELINE = "pipeline"
|
| 131 |
+
PRETRAINED_MODEL = "pretrained_model" # Added this constant
|
| 132 |
|
| 133 |
# Define minimal event constants
|
| 134 |
EVENT_STDP_REQUEST = "stdp_request"
|
|
|
|
| 147 |
|
| 148 |
event_bus = EventBus()
|
| 149 |
|
| 150 |
+
from find_weights import find_transformer_weights, find_snn_weights
|
| 151 |
|
| 152 |
# Import API components
|
| 153 |
try:
|
|
|
|
| 165 |
def verify_model_repo_access():
|
| 166 |
return False
|
| 167 |
|
| 168 |
+
# --- Helper functions ---
|
| 169 |
+
|
| 170 |
def fix_config_file(config_path="config.json"):
|
| 171 |
"""Fix the config file directly"""
|
| 172 |
import os
|
|
|
|
| 785 |
model_dir = os.path.join(data_dir, "models")
|
| 786 |
os.makedirs(model_dir, exist_ok=True)
|
| 787 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 788 |
def initialize_system():
|
| 789 |
"""Initialize all components in the correct order"""
|
| 790 |
logger.info("Starting system initialization")
|
|
|
|
| 802 |
tokenizer = TokenizerWrapper(model_name="gpt2")
|
| 803 |
|
| 804 |
# Then register tokenizer
|
|
|
|
| 805 |
registry.register(TOKENIZER, tokenizer, overwrite=True)
|
| 806 |
logger.info("Tokenizer registered")
|
| 807 |
|
| 808 |
# Initialize pretrained model (GPT-2)
|
| 809 |
try:
|
| 810 |
+
from model_PrTr import GPT_2 as PretrainedModel
|
| 811 |
pretrained = PretrainedModel(model_name="gpt2", tokenizer=tokenizer)
|
| 812 |
registry.register(PRETRAINED_MODEL, pretrained, overwrite=True)
|
| 813 |
logger.info("GPT-2 pretrained model registered")
|
|
|
|
| 817 |
# Now load custom model
|
| 818 |
try:
|
| 819 |
from model_Custm import Wildnerve_tlm01
|
| 820 |
+
# Use architecture parameters from config
|
| 821 |
+
arch_params = get_model_architecture_params()
|
| 822 |
+
|
| 823 |
model = Wildnerve_tlm01(
|
| 824 |
+
vocab_size=arch_params["vocab_size"],
|
| 825 |
specialization="general",
|
| 826 |
dataset_path=None,
|
| 827 |
model_name="gpt2",
|
| 828 |
+
embedding_dim=arch_params["embedding_dim"],
|
| 829 |
+
num_heads=arch_params["num_heads"],
|
| 830 |
+
hidden_dim=arch_params["hidden_dim"],
|
| 831 |
+
num_layers=arch_params["num_layers"],
|
| 832 |
+
output_size=arch_params["vocab_size"],
|
| 833 |
+
dropout=arch_params["dropout"],
|
| 834 |
+
max_seq_length=arch_params["max_seq_length"],
|
| 835 |
pooling_mode="last",
|
| 836 |
tokenizer=tokenizer
|
| 837 |
)
|
| 838 |
|
| 839 |
# Register model
|
|
|
|
| 840 |
registry.register(MODEL, model, overwrite=True)
|
| 841 |
logger.info("Custom model registered successfully")
|
| 842 |
return True
|
|
|
|
| 844 |
logger.error(f"Failed to initialize custom model: {e}", exc_info=True)
|
| 845 |
return False
|
| 846 |
|
| 847 |
+
def main():
|
| 848 |
+
"""Main application entry point with consolidated functionality"""
|
| 849 |
+
# Initialize the system first
|
| 850 |
+
success = initialize_system()
|
| 851 |
+
logger.info(f"System initialization {'successful' if success else 'failed'}")
|
| 852 |
+
|
| 853 |
+
# Start the server
|
| 854 |
+
from app import app
|
| 855 |
+
import uvicorn
|
| 856 |
+
|
| 857 |
+
logger.info("Starting TLM application")
|
| 858 |
+
uvicorn.run(
|
| 859 |
+
app,
|
| 860 |
+
host="0.0.0.0",
|
| 861 |
+
port=int(os.getenv("PORT", 7860)),
|
| 862 |
+
workers=os.cpu_count() or 1,
|
| 863 |
+
loop="auto"
|
| 864 |
+
)
|
| 865 |
|
| 866 |
if __name__ == "__main__":
|
| 867 |
+
main()
|
|
|
model_Custm.py
CHANGED
|
@@ -355,18 +355,18 @@ class Wildnerve_tlm01(nn.Module, AbstractModel):
|
|
| 355 |
output = output.unsqueeze(1)
|
| 356 |
|
| 357 |
# Apply final projection to vocabulary space
|
| 358 |
-
|
| 359 |
|
| 360 |
# CRITICAL: Ensure output is always 3D [batch_size, seq_length, vocab_size]
|
| 361 |
-
if
|
| 362 |
# If 2D tensor [batch_size, vocab_size], reshape to 3D [batch_size, 1, vocab_size]
|
| 363 |
-
batch_size, vocab_size =
|
| 364 |
logger.info(f"2D tensor: batch_size={batch_size}, vocab_size={vocab_size}")
|
| 365 |
-
|
| 366 |
-
logger.info(f"Reshaped 2D output to 3D tensor: {
|
| 367 |
|
| 368 |
# Record the output shape and dimensions for debugging
|
| 369 |
-
logger.info(f"Output shape: {
|
| 370 |
|
| 371 |
# Calculate loss if labels are provided
|
| 372 |
loss = None
|
|
@@ -377,16 +377,21 @@ class Wildnerve_tlm01(nn.Module, AbstractModel):
|
|
| 377 |
logger.info(f"Reshaped labels to {labels.shape}")
|
| 378 |
|
| 379 |
# Calculate loss with properly shaped tensors
|
| 380 |
-
batch_size, seq_length, vocab_size =
|
| 381 |
loss_fct = nn.CrossEntropyLoss()
|
| 382 |
-
loss = loss_fct(
|
| 383 |
logger.info(f"Returning loss tensor: {loss.item()}")
|
| 384 |
|
| 385 |
# Return the proper format
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
|
| 391 |
except Exception as e:
|
| 392 |
logger.error(f"Error in forward pass: {str(e)}")
|
|
@@ -405,7 +410,14 @@ class Wildnerve_tlm01(nn.Module, AbstractModel):
|
|
| 405 |
# CRITICAL: Return a proper 3D tensor even in error case
|
| 406 |
dummy_output = torch.zeros((dummy_batch, 1, self.vocab_size), device=next(self.parameters()).device)
|
| 407 |
dummy_loss = torch.tensor(float('nan'), device=next(self.parameters()).device)
|
| 408 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
|
| 410 |
# Add sentence transformer methods
|
| 411 |
def encode_sentences(self, sentences, batch_size=32, normalize_embeddings=True):
|
|
|
|
| 355 |
output = output.unsqueeze(1)
|
| 356 |
|
| 357 |
# Apply final projection to vocabulary space
|
| 358 |
+
logits = self.final_layer(output)
|
| 359 |
|
| 360 |
# CRITICAL: Ensure output is always 3D [batch_size, seq_length, vocab_size]
|
| 361 |
+
if logits.dim() == 2:
|
| 362 |
# If 2D tensor [batch_size, vocab_size], reshape to 3D [batch_size, 1, vocab_size]
|
| 363 |
+
batch_size, vocab_size = logits.shape
|
| 364 |
logger.info(f"2D tensor: batch_size={batch_size}, vocab_size={vocab_size}")
|
| 365 |
+
logits = logits.unsqueeze(1) # Add sequence dimension
|
| 366 |
+
logger.info(f"Reshaped 2D output to 3D tensor: {logits.shape}")
|
| 367 |
|
| 368 |
# Record the output shape and dimensions for debugging
|
| 369 |
+
logger.info(f"Output shape: {logits.shape}, dimensions: {logits.dim()}")
|
| 370 |
|
| 371 |
# Calculate loss if labels are provided
|
| 372 |
loss = None
|
|
|
|
| 377 |
logger.info(f"Reshaped labels to {labels.shape}")
|
| 378 |
|
| 379 |
# Calculate loss with properly shaped tensors
|
| 380 |
+
batch_size, seq_length, vocab_size = logits.shape
|
| 381 |
loss_fct = nn.CrossEntropyLoss()
|
| 382 |
+
loss = loss_fct(logits.reshape(-1, vocab_size), labels)
|
| 383 |
logger.info(f"Returning loss tensor: {loss.item()}")
|
| 384 |
|
| 385 |
# Return the proper format
|
| 386 |
+
from transformers.modeling_outputs import CausalLMOutputWithCrossAttentions
|
| 387 |
+
return CausalLMOutputWithCrossAttentions(
|
| 388 |
+
loss=loss,
|
| 389 |
+
logits=logits,
|
| 390 |
+
past_key_values=None,
|
| 391 |
+
hidden_states=None,
|
| 392 |
+
attentions=None,
|
| 393 |
+
cross_attentions=None
|
| 394 |
+
)
|
| 395 |
|
| 396 |
except Exception as e:
|
| 397 |
logger.error(f"Error in forward pass: {str(e)}")
|
|
|
|
| 410 |
# CRITICAL: Return a proper 3D tensor even in error case
|
| 411 |
dummy_output = torch.zeros((dummy_batch, 1, self.vocab_size), device=next(self.parameters()).device)
|
| 412 |
dummy_loss = torch.tensor(float('nan'), device=next(self.parameters()).device)
|
| 413 |
+
return CausalLMOutputWithCrossAttentions(
|
| 414 |
+
loss=dummy_loss,
|
| 415 |
+
logits=dummy_output,
|
| 416 |
+
past_key_values=None,
|
| 417 |
+
hidden_states=None,
|
| 418 |
+
attentions=None,
|
| 419 |
+
cross_attentions=None
|
| 420 |
+
)
|
| 421 |
|
| 422 |
# Add sentence transformer methods
|
| 423 |
def encode_sentences(self, sentences, batch_size=32, normalize_embeddings=True):
|