Update modeling.py
Browse files- modeling.py +18 -54
modeling.py
CHANGED
|
@@ -1,54 +1,18 @@
|
|
| 1 |
-
# modeling.py
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
# 3) Minimal HF-compatible model implementation (harmless).
|
| 22 |
-
from transformers import PreTrainedModel, PretrainedConfig
|
| 23 |
-
|
| 24 |
-
class SimpleWarningConfig(PretrainedConfig):
|
| 25 |
-
model_type = "simple-warning-model"
|
| 26 |
-
|
| 27 |
-
def __init__(self, hidden_size=8, **kwargs):
|
| 28 |
-
super().__init__(**kwargs)
|
| 29 |
-
self.hidden_size = hidden_size
|
| 30 |
-
|
| 31 |
-
class SimpleModel(PreTrainedModel):
|
| 32 |
-
config_class = SimpleWarningConfig
|
| 33 |
-
base_model_prefix = "simple_model"
|
| 34 |
-
|
| 35 |
-
def __init__(self, config: SimpleWarningConfig):
|
| 36 |
-
super().__init__(config)
|
| 37 |
-
# keep internals minimal and harmless
|
| 38 |
-
try:
|
| 39 |
-
import torch.nn as nn
|
| 40 |
-
self.dummy = nn.Linear(config.hidden_size, config.hidden_size)
|
| 41 |
-
except Exception:
|
| 42 |
-
# if torch not available, we still want the module importable
|
| 43 |
-
self.dummy = None
|
| 44 |
-
|
| 45 |
-
def forward(self, *args, **kwargs):
|
| 46 |
-
# harmless placeholder forward
|
| 47 |
-
try:
|
| 48 |
-
import torch
|
| 49 |
-
if self.dummy is None:
|
| 50 |
-
return torch.zeros(1, self.config.hidden_size)
|
| 51 |
-
return self.dummy(torch.zeros(1, self.config.hidden_size))
|
| 52 |
-
except Exception:
|
| 53 |
-
# if torch is missing, return a plain Python fallback
|
| 54 |
-
return [[0.0] * self.config.hidden_size]
|
|
|
|
| 1 |
+
# modeling.py
|
| 2 |
+
from transformers import PreTrainedModel
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
|
| 5 |
+
class SimpleWarningConfig:
|
| 6 |
+
def __init__(self, hidden_size=8):
|
| 7 |
+
self.hidden_size = hidden_size
|
| 8 |
+
|
| 9 |
+
class SimpleModel(PreTrainedModel):
|
| 10 |
+
config_class = SimpleWarningConfig
|
| 11 |
+
|
| 12 |
+
def __init__(self, config):
|
| 13 |
+
super().__init__(config)
|
| 14 |
+
print("⚠️ WARNING: You are loading a model with trust_remote_code=True! Be careful with remote code execution!")
|
| 15 |
+
self.linear = nn.Linear(config.hidden_size, config.hidden_size)
|
| 16 |
+
|
| 17 |
+
def forward(self, x):
|
| 18 |
+
return self.linear(x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|