Fix rope_theta missing in patch
Browse files- longvu/builder.py +25 -10
longvu/builder.py
CHANGED
|
@@ -27,18 +27,21 @@ try:
|
|
| 27 |
|
| 28 |
def _patched_qwen2_rotary_init(self, config=None, device=None):
|
| 29 |
if config is not None:
|
| 30 |
-
# Pastikan rope_parameters ada
|
| 31 |
rp = getattr(config, "rope_parameters", None)
|
| 32 |
rs = getattr(config, "rope_scaling", None)
|
| 33 |
if rp is None:
|
| 34 |
-
#
|
|
|
|
| 35 |
if rs is not None and isinstance(rs, dict) and "rope_type" in rs:
|
|
|
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
-
config.rope_parameters =
|
| 38 |
except Exception:
|
| 39 |
-
object.__setattr__(config, "rope_parameters",
|
| 40 |
else:
|
| 41 |
-
default_rp = {"rope_type": "default"}
|
| 42 |
try:
|
| 43 |
config.rope_parameters = default_rp
|
| 44 |
except Exception:
|
|
@@ -48,6 +51,10 @@ try:
|
|
| 48 |
config.rope_scaling = default_rp
|
| 49 |
except Exception:
|
| 50 |
object.__setattr__(config, "rope_scaling", default_rp)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return _orig_qwen2_rotary_init(self, config, device)
|
| 52 |
|
| 53 |
Qwen2RotaryEmbedding.__init__ = _patched_qwen2_rotary_init
|
|
@@ -195,17 +202,25 @@ def load_pretrained_model(
|
|
| 195 |
from longvu.language_model.cambrian_qwen import CambrianConfig
|
| 196 |
|
| 197 |
_cfg = CambrianConfig.from_pretrained(model_path)
|
| 198 |
-
|
|
|
|
| 199 |
if getattr(_cfg, "rope_scaling", None) is None:
|
| 200 |
try:
|
| 201 |
-
_cfg.rope_scaling =
|
| 202 |
except Exception:
|
| 203 |
-
object.__setattr__(_cfg, "rope_scaling",
|
| 204 |
if not getattr(_cfg, "rope_parameters", None):
|
| 205 |
try:
|
| 206 |
-
_cfg.rope_parameters =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
except Exception:
|
| 208 |
-
|
| 209 |
model = CambrianQwenForCausalLM.from_pretrained(
|
| 210 |
model_path, low_cpu_mem_usage=True, config=_cfg, **kwargs
|
| 211 |
)
|
|
|
|
| 27 |
|
| 28 |
def _patched_qwen2_rotary_init(self, config=None, device=None):
|
| 29 |
if config is not None:
|
|
|
|
| 30 |
rp = getattr(config, "rope_parameters", None)
|
| 31 |
rs = getattr(config, "rope_scaling", None)
|
| 32 |
if rp is None:
|
| 33 |
+
# Build default dengan rope_theta dari config
|
| 34 |
+
theta = getattr(config, "rope_theta", 1000000.0)
|
| 35 |
if rs is not None and isinstance(rs, dict) and "rope_type" in rs:
|
| 36 |
+
default_rp = dict(rs)
|
| 37 |
+
if "rope_theta" not in default_rp:
|
| 38 |
+
default_rp["rope_theta"] = theta
|
| 39 |
try:
|
| 40 |
+
config.rope_parameters = default_rp
|
| 41 |
except Exception:
|
| 42 |
+
object.__setattr__(config, "rope_parameters", default_rp)
|
| 43 |
else:
|
| 44 |
+
default_rp = {"rope_type": "default", "rope_theta": theta}
|
| 45 |
try:
|
| 46 |
config.rope_parameters = default_rp
|
| 47 |
except Exception:
|
|
|
|
| 51 |
config.rope_scaling = default_rp
|
| 52 |
except Exception:
|
| 53 |
object.__setattr__(config, "rope_scaling", default_rp)
|
| 54 |
+
else:
|
| 55 |
+
# rp ada tapi mungkin missing rope_theta
|
| 56 |
+
if isinstance(rp, dict) and "rope_theta" not in rp:
|
| 57 |
+
rp["rope_theta"] = getattr(config, "rope_theta", 1000000.0)
|
| 58 |
return _orig_qwen2_rotary_init(self, config, device)
|
| 59 |
|
| 60 |
Qwen2RotaryEmbedding.__init__ = _patched_qwen2_rotary_init
|
|
|
|
| 202 |
from longvu.language_model.cambrian_qwen import CambrianConfig
|
| 203 |
|
| 204 |
_cfg = CambrianConfig.from_pretrained(model_path)
|
| 205 |
+
_theta = getattr(_cfg, "rope_theta", 1000000.0)
|
| 206 |
+
_default_rp = {"rope_type": "default", "rope_theta": _theta}
|
| 207 |
if getattr(_cfg, "rope_scaling", None) is None:
|
| 208 |
try:
|
| 209 |
+
_cfg.rope_scaling = dict(_default_rp)
|
| 210 |
except Exception:
|
| 211 |
+
object.__setattr__(_cfg, "rope_scaling", dict(_default_rp))
|
| 212 |
if not getattr(_cfg, "rope_parameters", None):
|
| 213 |
try:
|
| 214 |
+
_cfg.rope_parameters = dict(_default_rp)
|
| 215 |
+
except Exception:
|
| 216 |
+
object.__setattr__(_cfg, "rope_parameters", dict(_default_rp))
|
| 217 |
+
else:
|
| 218 |
+
# Pastikan rope_theta ada
|
| 219 |
+
try:
|
| 220 |
+
if "rope_theta" not in _cfg.rope_parameters:
|
| 221 |
+
_cfg.rope_parameters["rope_theta"] = _theta
|
| 222 |
except Exception:
|
| 223 |
+
pass
|
| 224 |
model = CambrianQwenForCausalLM.from_pretrained(
|
| 225 |
model_path, low_cpu_mem_usage=True, config=_cfg, **kwargs
|
| 226 |
)
|