Upload 2 files
Browse files- config.py +9 -0
- model_Custm.py +25 -68
config.py
CHANGED
|
@@ -394,6 +394,15 @@ def load_config() -> AppConfig:
|
|
| 394 |
try:
|
| 395 |
with open(config_path, "r") as f:
|
| 396 |
raw = json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
except Exception as e:
|
| 398 |
logger.error(f"Failed to read config.json: {e}", exc_info=True)
|
| 399 |
raise
|
|
|
|
| 394 |
try:
|
| 395 |
with open(config_path, "r") as f:
|
| 396 |
raw = json.load(f)
|
| 397 |
+
|
| 398 |
+
# helper to convert a dict into an object with attribute access
|
| 399 |
+
class AttrDict(dict):
|
| 400 |
+
__getattr__ = dict.get
|
| 401 |
+
__setattr__ = dict.__setitem__
|
| 402 |
+
|
| 403 |
+
# wrap TRANSFORMER_CONFIG if it's a dict
|
| 404 |
+
if isinstance(raw.get("TRANSFORMER_CONFIG"), dict):
|
| 405 |
+
raw["TRANSFORMER_CONFIG"] = AttrDict(raw["TRANSFORMER_CONFIG"])
|
| 406 |
except Exception as e:
|
| 407 |
logger.error(f"Failed to read config.json: {e}", exc_info=True)
|
| 408 |
raise
|
model_Custm.py
CHANGED
|
@@ -1,31 +1,26 @@
|
|
| 1 |
# model_Custm.py
|
|
|
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import math
|
| 5 |
-
import torch
|
| 6 |
import logging
|
| 7 |
-
import
|
| 8 |
import torch.nn as nn
|
|
|
|
|
|
|
| 9 |
from typing import Optional, List, Dict, Union
|
| 10 |
|
| 11 |
-
|
| 12 |
-
from codecarbon import EmissionsTracker # Import EmissionsTracker
|
| 13 |
-
|
| 14 |
-
# Apply patches before importing transformers
|
| 15 |
import transformer_patches
|
| 16 |
|
| 17 |
-
# Now we can safely import transformers
|
| 18 |
-
import transformers
|
| 19 |
-
|
| 20 |
-
# Continue with standard imports
|
| 21 |
from service_registry import registry, MODEL, TOKENIZER
|
| 22 |
from utils.transformer_utils import get_tokenizer
|
| 23 |
from utils.smartHybridAttention import SmartHybridAttention, get_hybrid_attention_config
|
| 24 |
-
|
| 25 |
-
# Import base interfaces
|
| 26 |
from base_interfaces.common_types import *
|
| 27 |
from base_interfaces.model_interface import AbstractModel
|
| 28 |
|
|
|
|
|
|
|
| 29 |
logger = logging.getLogger(__name__)
|
| 30 |
|
| 31 |
# Check if transformers integrations has CodeCarbonCallback
|
|
@@ -40,58 +35,34 @@ if hasattr(transformers.integrations, 'CodeCarbonCallback'):
|
|
| 40 |
else:
|
| 41 |
logger.info(f"Using original implementation for CodeCarbonCallback from {callback_module}")
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
app_config.DATA_DIR = data_dir
|
| 52 |
-
app_config.MODEL_DIR = model_dir
|
| 53 |
-
app_config.TRANSFORMER_CONFIG = SimpleNamespace()
|
| 54 |
-
app_config.TRANSFORMER_CONFIG.MAX_SEQ_LENGTH = 512
|
| 55 |
-
try:
|
| 56 |
-
from config import load_config, app_config as config_app_config
|
| 57 |
-
app_config = load_config() if not hasattr(config_app_config, 'DATA_DIR') else config_app_config
|
| 58 |
-
except Exception as config_error:
|
| 59 |
-
logging.warning(f"Using minimal config due to error: {config_error}")
|
| 60 |
-
else:
|
| 61 |
-
from config import load_config, app_config as config_app_config
|
| 62 |
-
app_config = load_config() if not hasattr(config_app_config, 'DATA_DIR') else config_app_config
|
| 63 |
-
except Exception as e:
|
| 64 |
-
logging.warning(f"Error importing config: {e}")
|
| 65 |
-
from types import SimpleNamespace
|
| 66 |
-
app_config = SimpleNamespace()
|
| 67 |
-
app_config.DATA_DIR = '/tmp/tlm_data'
|
| 68 |
-
app_config.MODEL_DIR = '/tmp/tlm_data/models'
|
| 69 |
-
app_config.TRANSFORMER_CONFIG = SimpleNamespace()
|
| 70 |
-
app_config.TRANSFORMER_CONFIG.MAX_SEQ_LENGTH = 512
|
| 71 |
-
|
| 72 |
-
# Ensure the necessary directories exist, but don't fail if they can't be created
|
| 73 |
-
try:
|
| 74 |
-
os.makedirs(getattr(app_config, "DATA_DIR", "/tmp/tlm_data"), exist_ok=True)
|
| 75 |
-
os.makedirs(getattr(app_config, "MODEL_DIR", "/tmp/tlm_data/models"), exist_ok=True)
|
| 76 |
-
except Exception as e:
|
| 77 |
-
logging.warning(f"Could not create directories: {e}")
|
| 78 |
-
|
| 79 |
-
# Configure logging and suppress TensorFlow warnings
|
| 80 |
-
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
|
| 81 |
-
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
| 82 |
|
| 83 |
# ----------------------------
|
| 84 |
# Positional Encoding Module
|
| 85 |
# ----------------------------
|
| 86 |
class PositionalEncoding(nn.Module):
|
| 87 |
-
def __init__(self, d_model: int, max_len: int =
|
| 88 |
super().__init__()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
pe = torch.zeros(max_len, d_model)
|
| 90 |
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
|
| 91 |
div_term = torch.exp(torch.arange(0, d_model, 2, dtype=torch.float) * (-math.log(10000.0) / d_model))
|
| 92 |
pe[:, 0::2] = torch.sin(position * div_term)
|
| 93 |
pe[:, 1::2] = torch.cos(position * div_term)
|
| 94 |
-
pe = pe.unsqueeze(1)
|
| 95 |
self.register_buffer("pe", pe)
|
| 96 |
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 97 |
# x shape: (seq_len, batch_size, d_model)
|
|
@@ -772,18 +743,4 @@ def initialize_tokenizer():
|
|
| 772 |
except Exception as e:
|
| 773 |
logger.error(f"Default tokenizer initialization failed: {e}")
|
| 774 |
tokenizer = None
|
| 775 |
-
return tokenizer
|
| 776 |
-
|
| 777 |
-
# NOTE: This file is currently empty (no class or functions defined).
|
| 778 |
-
# As a result:
|
| 779 |
-
# 1. service_registry.ensure_models_registered() cannot find Wildnerve_tlm01 here,
|
| 780 |
-
# so it falls back (or errors) when registering the custom model.
|
| 781 |
-
# 2. The handler tries to import `find_weights`, which doesn’t exist in this module
|
| 782 |
-
# (hence "No module named 'find_weights'" in the logs).
|
| 783 |
-
# 3. Since no model class is present, the pipeline never registers a MODEL or PIPELINE,
|
| 784 |
-
# triggering "cannot import name 'PIPELINE' from 'service_registry'".
|
| 785 |
-
#
|
| 786 |
-
# To fix:
|
| 787 |
-
# - Merge the real Wildnerve_tlm01 implementation into this file (or import it).
|
| 788 |
-
# - Provide a stub `find_weights.py` with the expected functions.
|
| 789 |
-
# - Define or register PIPELINE in service_registry, or remove its import from main/handler.
|
|
|
|
| 1 |
# model_Custm.py
|
| 2 |
+
# Consolidated imports
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import math
|
|
|
|
| 6 |
import logging
|
| 7 |
+
import torch
|
| 8 |
import torch.nn as nn
|
| 9 |
+
import numpy as np
|
| 10 |
+
import transformers
|
| 11 |
from typing import Optional, List, Dict, Union
|
| 12 |
|
| 13 |
+
from codecarbon import EmissionsTracker
|
|
|
|
|
|
|
|
|
|
| 14 |
import transformer_patches
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
from service_registry import registry, MODEL, TOKENIZER
|
| 17 |
from utils.transformer_utils import get_tokenizer
|
| 18 |
from utils.smartHybridAttention import SmartHybridAttention, get_hybrid_attention_config
|
|
|
|
|
|
|
| 19 |
from base_interfaces.common_types import *
|
| 20 |
from base_interfaces.model_interface import AbstractModel
|
| 21 |
|
| 22 |
+
from config import app_config
|
| 23 |
+
|
| 24 |
logger = logging.getLogger(__name__)
|
| 25 |
|
| 26 |
# Check if transformers integrations has CodeCarbonCallback
|
|
|
|
| 35 |
else:
|
| 36 |
logger.info(f"Using original implementation for CodeCarbonCallback from {callback_module}")
|
| 37 |
|
| 38 |
+
# Ensure data/model directories exist (silently ignore errors)
|
| 39 |
+
for d in (app_config.DATA_DIR, app_config.MODEL_DIR):
|
| 40 |
+
try: os.makedirs(d, exist_ok=True)
|
| 41 |
+
except Exception as _e: logger.warning(f"Could not create directory {d}: {_e}")
|
| 42 |
+
|
| 43 |
+
# Suppress TensorFlow logs if present
|
| 44 |
+
os.environ.setdefault("TF_ENABLE_ONEDNN_OPTS", "0")
|
| 45 |
+
os.environ.setdefault("TF_CPP_MIN_LOG_LEVEL", "2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# ----------------------------
|
| 48 |
# Positional Encoding Module
|
| 49 |
# ----------------------------
|
| 50 |
class PositionalEncoding(nn.Module):
|
| 51 |
+
def __init__(self, d_model: int, max_len: Optional[int] = None):
|
| 52 |
super().__init__()
|
| 53 |
+
# determine max_len dynamically
|
| 54 |
+
if max_len is None:
|
| 55 |
+
cfg = app_config.TRANSFORMER_CONFIG
|
| 56 |
+
if isinstance(cfg, dict):
|
| 57 |
+
max_len = cfg.get("MAX_SEQ_LENGTH", 512)
|
| 58 |
+
else:
|
| 59 |
+
max_len = getattr(cfg, "MAX_SEQ_LENGTH", 512)
|
| 60 |
pe = torch.zeros(max_len, d_model)
|
| 61 |
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
|
| 62 |
div_term = torch.exp(torch.arange(0, d_model, 2, dtype=torch.float) * (-math.log(10000.0) / d_model))
|
| 63 |
pe[:, 0::2] = torch.sin(position * div_term)
|
| 64 |
pe[:, 1::2] = torch.cos(position * div_term)
|
| 65 |
+
pe = pe.unsqueeze(1)
|
| 66 |
self.register_buffer("pe", pe)
|
| 67 |
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 68 |
# x shape: (seq_len, batch_size, d_model)
|
|
|
|
| 743 |
except Exception as e:
|
| 744 |
logger.error(f"Default tokenizer initialization failed: {e}")
|
| 745 |
tokenizer = None
|
| 746 |
+
return tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|