Revert "use config.json model_type for tier detection, add unsloth/nvidia namespace guard"
This reverts commit fc49ae2453.
This commit is contained in:
parent
ca30bd0dc4
commit
463623b8b6
4 changed files with 59 additions and 99 deletions
|
|
@ -328,11 +328,7 @@ def _handle_load(backend, config: dict, resp_queue: Any) -> None:
|
|||
trust_remote_code = config.get("trust_remote_code", False)
|
||||
if not trust_remote_code:
|
||||
model_name = config["model_name"]
|
||||
_mn_lower = model_name.lower()
|
||||
if (
|
||||
"nemotron" in _mn_lower
|
||||
and (_mn_lower.startswith("unsloth/") or _mn_lower.startswith("nvidia/"))
|
||||
):
|
||||
if "nemotron" in model_name.lower():
|
||||
trust_remote_code = True
|
||||
logger.info(
|
||||
"Auto-enabled trust_remote_code for Nemotron model: %s",
|
||||
|
|
|
|||
|
|
@ -1869,11 +1869,7 @@ def run_training_process(
|
|||
# (Qwen3.5, Gemma 4, etc.) are native and do NOT need it — enabling it
|
||||
# bypasses the compiler (disabling fused CE).
|
||||
_lowered = model_name.lower()
|
||||
if (
|
||||
"nemotron" in _lowered
|
||||
and (_lowered.startswith("unsloth/") or _lowered.startswith("nvidia/"))
|
||||
and not config.get("trust_remote_code", False)
|
||||
):
|
||||
if "nemotron" in _lowered and not config.get("trust_remote_code", False):
|
||||
config["trust_remote_code"] = True
|
||||
logger.info(
|
||||
"Auto-enabled trust_remote_code for Nemotron model: %s",
|
||||
|
|
|
|||
|
|
@ -32,9 +32,8 @@ from utils.transformers_version import (
|
|||
_resolve_base_model,
|
||||
_check_tokenizer_config_needs_v5,
|
||||
_check_config_needs_550,
|
||||
_get_config_json,
|
||||
_tokenizer_class_cache,
|
||||
_config_json_cache,
|
||||
_config_needs_550_cache,
|
||||
needs_transformers_5,
|
||||
get_transformers_tier,
|
||||
)
|
||||
|
|
@ -203,7 +202,7 @@ class TestCheckConfigNeeds550:
|
|||
"""Tests for _check_config_needs_550() local config.json checks."""
|
||||
|
||||
def setup_method(self):
|
||||
_config_json_cache.clear()
|
||||
_config_needs_550_cache.clear()
|
||||
|
||||
def test_gemma4_architecture(self, tmp_path: Path):
|
||||
"""config.json with Gemma4ForConditionalGeneration should return True."""
|
||||
|
|
@ -243,8 +242,8 @@ class TestCheckConfigNeeds550:
|
|||
|
||||
key = str(tmp_path)
|
||||
_check_config_needs_550(key)
|
||||
assert key in _config_json_cache
|
||||
assert _config_json_cache[key] is not None
|
||||
assert key in _config_needs_550_cache
|
||||
assert _config_needs_550_cache[key] is True
|
||||
|
||||
def test_local_file_skips_network(self, tmp_path: Path):
|
||||
"""When local config.json exists, no network request should be made."""
|
||||
|
|
@ -266,7 +265,7 @@ class TestGetTransformersTier:
|
|||
|
||||
def setup_method(self):
|
||||
_tokenizer_class_cache.clear()
|
||||
_config_json_cache.clear()
|
||||
_config_needs_550_cache.clear()
|
||||
|
||||
def test_gemma4_substring_returns_550(self):
|
||||
assert get_transformers_tier("google/gemma-4-E2B-it") == "550"
|
||||
|
|
@ -318,20 +317,6 @@ class TestGetTransformersTier:
|
|||
# This shouldn't happen in practice, but verifies priority
|
||||
assert get_transformers_tier("gemma-4-model") == "550"
|
||||
|
||||
def test_config_json_model_type_530(self, tmp_path: Path):
|
||||
"""Local checkpoint with qwen3_moe model_type → 530."""
|
||||
cfg = {"model_type": "qwen3_moe", "architectures": ["Qwen3MoeForCausalLM"]}
|
||||
(tmp_path / "config.json").write_text(json.dumps(cfg))
|
||||
|
||||
assert get_transformers_tier(str(tmp_path)) == "530"
|
||||
|
||||
def test_config_json_model_type_glm4_moe(self, tmp_path: Path):
|
||||
"""Local checkpoint with glm4_moe model_type → 530."""
|
||||
cfg = {"model_type": "glm4_moe", "architectures": ["Glm4MoeForCausalLM"]}
|
||||
(tmp_path / "config.json").write_text(json.dumps(cfg))
|
||||
|
||||
assert get_transformers_tier(str(tmp_path)) == "530"
|
||||
|
||||
def test_needs_transformers_5_compat(self):
|
||||
"""needs_transformers_5 should return True for both 530 and 550 models."""
|
||||
assert needs_transformers_5("google/gemma-4-E2B-it") is True
|
||||
|
|
|
|||
|
|
@ -76,25 +76,14 @@ TRANSFORMERS_550_MODEL_SUBSTRINGS: tuple[str, ...] = (
|
|||
"qwen3.6",
|
||||
)
|
||||
|
||||
# Architecture classes that require transformers 5.5.0.
|
||||
# Architecture classes / model_type values that require transformers 5.5.0.
|
||||
# Checked via config.json (local or HuggingFace).
|
||||
_TRANSFORMERS_550_ARCHITECTURES: set[str] = {
|
||||
"Gemma4ForConditionalGeneration",
|
||||
}
|
||||
|
||||
# model_type values (from config.json) → tier mapping.
|
||||
_TRANSFORMERS_550_MODEL_TYPES: set[str] = {
|
||||
"gemma4",
|
||||
}
|
||||
_TRANSFORMERS_530_MODEL_TYPES: set[str] = {
|
||||
"qwen3_moe",
|
||||
"qwen3_5_moe",
|
||||
"qwen3_vl_moe",
|
||||
"qwen3_next",
|
||||
"deepseek_v3_moe",
|
||||
"glm4_moe",
|
||||
"glm4_moe_lite",
|
||||
"ministral",
|
||||
}
|
||||
|
||||
# Tokenizer classes that only exist in transformers>=5.x
|
||||
_TRANSFORMERS_5_TOKENIZER_CLASSES: set[str] = {
|
||||
|
|
@ -104,8 +93,8 @@ _TRANSFORMERS_5_TOKENIZER_CLASSES: set[str] = {
|
|||
# Cache for dynamic tokenizer_config.json lookups to avoid repeated fetches
|
||||
_tokenizer_class_cache: dict[str, bool] = {}
|
||||
|
||||
# Cache for config.json lookups (returns the parsed dict or None)
|
||||
_config_json_cache: dict[str, dict | None] = {}
|
||||
# Cache for dynamic config.json lookups (architecture/model_type checks)
|
||||
_config_needs_550_cache: dict[str, bool] = {}
|
||||
|
||||
# Versions
|
||||
TRANSFORMERS_550_VERSION = "5.5.0"
|
||||
|
|
@ -253,28 +242,43 @@ def _check_tokenizer_config_needs_v5(model_name: str) -> bool:
|
|||
return False
|
||||
|
||||
|
||||
_SENTINEL = object() # distinguishes "not cached" from "cached as None"
|
||||
def _check_config_needs_550(model_name: str) -> bool:
|
||||
"""Check ``config.json`` for architectures or model_type that require
|
||||
transformers 5.5.0 (e.g. Gemma 4).
|
||||
|
||||
|
||||
def _get_config_json(model_name: str) -> dict | None:
|
||||
"""Read and cache ``config.json`` for *model_name*.
|
||||
|
||||
Checks local path first, then fetches from HuggingFace.
|
||||
Returns the parsed dict, or ``None`` on any error (fail-open).
|
||||
The result is cached in ``_config_json_cache``.
|
||||
Checks locally first, then falls back to fetching from HuggingFace.
|
||||
Results are cached in ``_config_needs_550_cache``.
|
||||
Returns False on any error (fail-open to lower tier).
|
||||
"""
|
||||
cached = _config_json_cache.get(model_name, _SENTINEL)
|
||||
if cached is not _SENTINEL:
|
||||
return cached
|
||||
if model_name in _config_needs_550_cache:
|
||||
return _config_needs_550_cache[model_name]
|
||||
|
||||
def _check_cfg(cfg: dict) -> bool:
|
||||
archs = cfg.get("architectures", [])
|
||||
if any(a in _TRANSFORMERS_550_ARCHITECTURES for a in archs):
|
||||
return True
|
||||
if cfg.get("model_type") in _TRANSFORMERS_550_MODEL_TYPES:
|
||||
return True
|
||||
return False
|
||||
|
||||
# --- Check local config.json first ------------------------------------
|
||||
local_cfg = Path(model_name) / "config.json"
|
||||
local_path = Path(model_name)
|
||||
local_cfg = local_path / "config.json"
|
||||
if local_cfg.is_file():
|
||||
try:
|
||||
with open(local_cfg) as f:
|
||||
cfg = json.load(f)
|
||||
_config_json_cache[model_name] = cfg
|
||||
return cfg
|
||||
result = _check_cfg(cfg)
|
||||
if result:
|
||||
logger.info(
|
||||
"Local config.json check: %s needs transformers 5.5.0 "
|
||||
"(architectures=%s, model_type=%s)",
|
||||
model_name,
|
||||
cfg.get("architectures", []),
|
||||
cfg.get("model_type"),
|
||||
)
|
||||
_config_needs_550_cache[model_name] = result
|
||||
return result
|
||||
except Exception as exc:
|
||||
logger.debug("Could not read %s: %s", local_cfg, exc)
|
||||
|
||||
|
|
@ -291,26 +295,21 @@ def _get_config_json(model_name: str) -> dict | None:
|
|||
req = urllib.request.Request(url, headers = {"User-Agent": "unsloth-studio"})
|
||||
with urllib.request.urlopen(req, timeout = 10) as resp:
|
||||
cfg = json.loads(resp.read().decode())
|
||||
_config_json_cache[model_name] = cfg
|
||||
return cfg
|
||||
result = _check_cfg(cfg)
|
||||
if result:
|
||||
logger.info(
|
||||
"Dynamic config.json check: %s needs transformers 5.5.0 "
|
||||
"(architectures=%s, model_type=%s)",
|
||||
model_name,
|
||||
cfg.get("architectures", []),
|
||||
cfg.get("model_type"),
|
||||
)
|
||||
_config_needs_550_cache[model_name] = result
|
||||
return result
|
||||
except Exception as exc:
|
||||
logger.debug(
|
||||
"Could not fetch config.json for '%s': %s", model_name, exc
|
||||
)
|
||||
_config_json_cache[model_name] = None
|
||||
return None
|
||||
|
||||
|
||||
def _check_config_needs_550(model_name: str) -> bool:
|
||||
"""Check ``config.json`` for architectures or model_type that require
|
||||
transformers 5.5.0. Uses the shared ``_get_config_json`` cache."""
|
||||
cfg = _get_config_json(model_name)
|
||||
if cfg is None:
|
||||
logger.debug("Could not fetch config.json for '%s': %s", model_name, exc)
|
||||
_config_needs_550_cache[model_name] = False
|
||||
return False
|
||||
archs = cfg.get("architectures", [])
|
||||
if any(a in _TRANSFORMERS_550_ARCHITECTURES for a in archs):
|
||||
return True
|
||||
return cfg.get("model_type") in _TRANSFORMERS_550_MODEL_TYPES
|
||||
|
||||
|
||||
def get_transformers_tier(model_name: str) -> str:
|
||||
|
|
@ -320,35 +319,19 @@ def get_transformers_tier(model_name: str) -> str:
|
|||
``"530"`` for models needing transformers 5.3.0 (e.g. Ministral-3, Qwen3 MoE),
|
||||
or ``"default"`` for everything else (4.57.x).
|
||||
|
||||
Fast path: substring checks (no I/O) for both tiers run first.
|
||||
Slow path: single config.json fetch (cached) checks model_type for
|
||||
both tiers, then tokenizer_config.json as final fallback.
|
||||
The 5.5.0 check runs first, then 5.3.0.
|
||||
"""
|
||||
lowered = model_name.lower()
|
||||
|
||||
# --- Fast substring checks (no I/O) -----------------------------------
|
||||
# --- Check 5.5.0 first ------------------------------------------------
|
||||
if any(sub in lowered for sub in TRANSFORMERS_550_MODEL_SUBSTRINGS):
|
||||
return "550"
|
||||
if _check_config_needs_550(model_name):
|
||||
return "550"
|
||||
|
||||
# --- Check 5.3.0 ------------------------------------------------------
|
||||
if any(sub in lowered for sub in TRANSFORMERS_5_MODEL_SUBSTRINGS):
|
||||
return "530"
|
||||
|
||||
# --- config.json model_type / architecture check (single fetch) -------
|
||||
cfg = _get_config_json(model_name)
|
||||
if cfg is not None:
|
||||
model_type = cfg.get("model_type", "")
|
||||
archs = cfg.get("architectures", [])
|
||||
|
||||
# Check 5.5.0 first
|
||||
if model_type in _TRANSFORMERS_550_MODEL_TYPES:
|
||||
return "550"
|
||||
if any(a in _TRANSFORMERS_550_ARCHITECTURES for a in archs):
|
||||
return "550"
|
||||
|
||||
# Check 5.3.0
|
||||
if model_type in _TRANSFORMERS_530_MODEL_TYPES:
|
||||
return "530"
|
||||
|
||||
# --- Final fallback: tokenizer_config.json for 5.3.0 ------------------
|
||||
if _check_tokenizer_config_needs_v5(model_name):
|
||||
return "530"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue