Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
[Studio][Optimization]Add vision detection cache to is_vision_model() (#4853)
Roland Tannous278f46299623
diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.pyindex ab1825d94a7..77cbda6b45c 100644--- a/studio/backend/core/training/trainer.py+++ b/studio/backend/core/training/trainer.py@@ -190,7 +190,11 @@ class UnslothTrainer:self._cuda_audio_used = False# --- Detect VLM ---- vision = is_vision_model(model_name) if not self.is_audio else False+ vision = (+ is_vision_model(model_name, hf_token = hf_token)+ if not self.is_audio+ else False+ )self.is_vlm = not self.is_audio_vlm and vision and is_dataset_imagelogger.info(@@ -558,7 +562,11 @@ class UnslothTrainer:self._cuda_audio_used = False# VLM: vision model with image dataset (mutually exclusive with audio paths)- vision = is_vision_model(model_name) if not self.is_audio else False+ vision = (+ is_vision_model(model_name, hf_token = hf_token)+ if not self.is_audio+ else False+ )self.is_vlm = not self.is_audio_vlm and vision and is_dataset_imageself.model_name = model_nameself.max_seq_length = max_seq_lengthdiff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.pyindex 1e31a91e26a..ea385436b8b 100644--- a/studio/backend/routes/models.py+++ b/studio/backend/routes/models.py@@ -618,7 +618,7 @@ async def get_model_config(config_dict = load_model_defaults(model_name)# Detect model capabilities (pass HF token for gated models)- is_vision = is_vision_model(model_name)+ is_vision = is_vision_model(model_name, hf_token = hf_token)is_embedding = is_embedding_model(model_name, hf_token = hf_token)audio_type = detect_audio_type(model_name, hf_token = hf_token)diff --git a/studio/backend/utils/models/model_config.py b/studio/backend/utils/models/model_config.pyindex f97ea993ebc..9d72846ca3b 100644--- a/studio/backend/utils/models/model_config.py+++ b/studio/backend/utils/models/model_config.py@@ -26,7 +26,9 @@ import subprocessimport sysfrom pathlib import Pathfrom typing import List, Tuple+import hashlibimport json+import threadingimport yaml@@ -548,12 +550,17 @@ except Exception as exc:def _is_vision_model_subprocess(model_name: str, hf_token: Optional[str] = None-) -> bool:+) -> Optional[bool]:"""Run is_vision_model check in a subprocess with transformers 5.x.Same pattern as training/inference workers: spawn a clean subprocesswith .venv_t5/ prepended to sys.path so AutoConfig recognizes newerarchitectures (glm4_moe_lite, etc.).++ Returns True/False for definitive results, or None for transient failures+ (timeouts, subprocess errors) so callers can decide whether to cache+ the result. Subprocess failures are treated as transient because they+ can be caused by temporary HF/auth/network issues."""token_arg = hf_token or ""@@ -580,7 +587,7 @@ def _is_vision_model_subprocess(model_name,stderr or result.stdout.strip(),)- return False+ return Nonedata = json.loads(result.stdout.strip())if "error" in data:@@ -589,7 +596,7 @@ def _is_vision_model_subprocess(model_name,data["error"],)- return False+ return Noneis_vlm = data["is_vision"]logger.info(@@ -604,10 +611,28 @@ def _is_vision_model_subprocess(except subprocess.TimeoutExpired:logger.warning("Vision check subprocess timed out for '%s'", model_name)- return False+ return Noneexcept Exception as exc:logger.warning("Vision check subprocess failed for '%s': %s", model_name, exc)- return False+ return None+++def _token_fingerprint(token: Optional[str]) -> Optional[str]:+ """Return a SHA256 digest of the token for use as a cache key.++ Avoids storing the raw bearer token in process memory as a dict key.+ """+ if token is None:+ return None+ return hashlib.sha256(token.encode("utf-8")).hexdigest()+++# Cache vision detection results per session to avoid repeated subprocess spawns.+# Keyed by (normalized_model_name, token_fingerprint) to handle gated models correctly.+# Only definitive results (True/False from successful detection) are cached;+# transient failures (network errors, timeouts) are NOT cached so they can be retried.+_vision_detection_cache: Dict[Tuple[str, Optional[str]], bool] = {}+_vision_cache_lock = threading.Lock()def is_vision_model(model_name: str, hf_token: Optional[str] = None) -> bool:@@ -616,13 +641,66 @@ def is_vision_model(model_name: str, hf_token: Optional[str] = None) -> bool:Works for fine-tuned models since they inherit the base architecture.For models that require transformers 5.x (e.g. GLM-4.7-Flash), the check- runs in a subprocess with .venv_t5/ activated — same pattern as the+ runs in a subprocess with .venv_t5/ activated -- same pattern as thetraining and inference workers.+ Results are cached per (model_name, token_fingerprint) for the lifetime of+ the process to avoid repeated subprocess spawns and HuggingFace API calls.+ Transient failures are not cached so they can be retried on the next call.+Args:model_name: Model identifier (HF repo or local path)hf_token: Optional HF token for accessing gated/private models"""+ # Normalize model name for cache key to avoid duplicate entries for+ # different casings of the same HF repo (e.g. "Org/Model" vs "org/model").+ try:+ if is_local_path(model_name):+ resolved_name = normalize_path(model_name)+ else:+ resolved_name = resolve_cached_repo_id_case(model_name)+ except Exception as exc:+ logger.debug(+ "Could not normalize model name '%s' for cache key: %s",+ model_name,+ exc,+ )+ resolved_name = model_name+ cache_key = (resolved_name, _token_fingerprint(hf_token))++ # Lock-free fast path for cache hits. Uses a sentinel to distinguish+ # "key not found" from "value is False" in a single atomic dict.get() call.+ _MISS = object()+ cached = _vision_detection_cache.get(cache_key, _MISS)+ if cached is not _MISS:+ return cached++ # Compute outside the lock to avoid serializing long-running detection+ # (subprocess spawns with 60s timeout, HF API calls) across all models.+ # The tradeoff: two concurrent calls for the same uncached model may+ # both run detection, but they produce the same result and the second+ # write is a benign no-op.+ result = _is_vision_model_uncached(resolved_name, hf_token)+ # Only cache definitive results; None means a transient failure occurred+ # and we should retry on the next call instead of locking in a wrong answer.+ if result is not None:+ with _vision_cache_lock:+ _vision_detection_cache[cache_key] = result+ return result+ return False+++def _is_vision_model_uncached(+ model_name: str, hf_token: Optional[str] = None+) -> Optional[bool]:+ """Uncached vision model detection -- called by is_vision_model().++ Returns True/False for definitive results, or None when detection failed+ due to a transient error (network, timeout, subprocess failure) so the+ caller knows not to cache the result.++ Do not call directly; use is_vision_model() instead.+ """# Models that need transformers 5.x must be checked in a subprocess# because AutoConfig in the main process (transformers 4.57.x) doesn't# recognize their architectures.@@ -630,7 +708,7 @@ def is_vision_model(model_name: str, hf_token: Optional[str] = None) -> bool:if needs_transformers_5(model_name):logger.info(- "Model '%s' needs transformers 5.x — checking vision via subprocess",+ "Model '%s' needs transformers 5.x -- checking vision via subprocess",model_name,)return _is_vision_model_subprocess(model_name, hf_token = hf_token)@@ -681,7 +759,25 @@ def is_vision_model(model_name: str, hf_token: Optional[str] = None) -> bool:except Exception as e:logger.warning(f"Could not determine if {model_name} is vision model: {e}")- return False+ # Permanent failures (model not found, gated, bad config) should be+ # cached as False. Transient failures (network, timeout) should not.+ try:+ from huggingface_hub.errors import RepositoryNotFoundError, GatedRepoError+ except ImportError:+ try:+ from huggingface_hub.utils import (+ RepositoryNotFoundError,+ GatedRepoError,+ )+ except ImportError:+ RepositoryNotFoundError = GatedRepoError = None+ if RepositoryNotFoundError is not None and isinstance(+ e, (RepositoryNotFoundError, GatedRepoError)+ ):+ return False+ if isinstance(e, (ValueError, json.JSONDecodeError)):+ return False+ return NoneVALID_AUDIO_TYPES = ("snac", "csm", "bicodec", "dac", "whisper", "audio_vlm")