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 Images: per-generation Shift for flow-matching models and SDXL textual inversion embeddings (#14563)
Daniel Han38d21245d093
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.pyindex 612d2d9fb9e..d556059032c 100644--- a/studio/backend/core/inference/diffusion.py+++ b/studio/backend/core/inference/diffusion.py@@ -72,6 +72,7 @@ from .diffusion_families import (canonical_base,cache_holds_files,comfy_flow_shift_for,+ comfy_shift_node_for,default_generation_params,generation_params_with_grid,detect_family_for_pick,@@ -275,6 +276,7 @@ from . import diffusion_compile_cache as compile_cachefrom .diffusion_compile_config import family_filters_reductionsfrom . import diffusion_cond_cache as cond_cachefrom . import diffusion_prompt_cache as prompt_cache+from . import diffusion_embeddingsfrom . import diffusion_te_release as te_releasefrom . import diffusion_gguf_compile as gguf_compilefrom . import diffusion_bg_compile as bg_compile@@ -333,6 +335,9 @@ from .diffusion_fast_load import start_load_prefetch, stop_prefetch, te_precast_from .diffusion_flow_shift import (SAMPLE_SIGMAS_KEY,apply_comfy_flow_shift,+ requested_flow_shift,+ scheduler_with_flow_shift,+ shift_control,install_sample_sigmas,pipe_sample_sigmas,sample_sigmas_for_steps,@@ -473,6 +478,17 @@ def _refuse_weight_change_on_streamed_graph(state: Any) -> None:)+def _state_shift_node(state: Any) -> Optional[str]:+ """The ComfyUI shift node (diffusion_flow_shift SHIFT_NODE_*) of the loaded checkpoint, or None."""+ return comfy_shift_node_for(+ state.family,+ getattr(state, "gguf_filename", None),+ getattr(state, "repo_id", None),+ getattr(state, "display_repo_id", None),+ getattr(state, "base_repo", None),+ )++def _guidance_supported(state: Any) -> bool:"""FLUX.2 klein base and distilled share a family; Flux2KleinPipeline turns guidance off on ``is_distilled``."""if not state.family.uses_guidance:@@ -12130,6 +12146,9 @@ class DiffusionBackend:live_preview: Optional[bool] = None,sampler: Optional[str] = None,schedule_type: Optional[str] = None,+ shift: Optional[float] = None,+ base_shift: Optional[float] = None,+ max_shift: Optional[float] = None,# Inpaint only: None / 0 / False keep the plain inpaint call.mask_blur: Optional[int] = None,inpaint_only_masked: Optional[bool] = None,@@ -12171,6 +12190,8 @@ class DiffusionBackend:# Reset in the finally, so a failed or cancelled generation frees its reused outputs.static_skip_pipe = Nonerestore_scheduler: Optional[tuple[Any, Any]] = None+ applied_embeddings: Optional[diffusion_embeddings.AppliedEmbeddings] = None+ applied_shift: Optional[float] = Nonerestore_vae: Optional[Callable[[], None]] = Nonerestore_resident: Optional[Callable[[], None]] = Nonebg = None@@ -12538,6 +12559,30 @@ class DiffusionBackend:kwargs["width"] = iwif "height" in call_params:kwargs["height"] = ih+ if shift is not None or base_shift is not None or max_shift is not None:+ shift_w, shift_h = kwargs.get("width"), kwargs.get("height")+ if (not shift_w or not shift_h) and init_pil is not None:+ shift_w, shift_h = init_pil.size+ if pipe_sample_sigmas(state.pipe) is not None:+ # Tuned on the shipped scheduler, like the load-time ComfyUI shift (which skips it too).+ raise ValueError(+ "This checkpoint ships its own sampling schedule, so Shift is not available."+ )+ shift_node = _state_shift_node(state)+ applied_shift = requested_flow_shift(+ shift_node,+ shift = shift,+ base_shift = base_shift,+ max_shift = max_shift,+ width = shift_w or width,+ height = shift_h or height,+ defaults = shift_control(shift_node, pipe.scheduler),+ )+ shifted = scheduler_with_flow_shift(pipe.scheduler, applied_shift)+ if shifted is not pipe.scheduler:+ if restore_scheduler is None:+ restore_scheduler = (pipe, pipe.scheduler)+ pipe.scheduler = shiftedif (negative_promptand _negative_prompt_engaged(state.family, guidance)@@ -12549,6 +12594,16 @@ class DiffusionBackend:):# Qwen-Image true CFG needs a negative present; ComfyUI encodes an empty one.kwargs["negative_prompt"] = ""+ applied_embeddings = diffusion_embeddings.apply(+ pipe,+ getattr(state.family, "textual_inversion", None),+ {+ "prompt": [p for p, _ in jobs],+ "negative_prompt": kwargs.get("negative_prompt"),+ },+ marker_owners = (state.pipe,),+ logger = logger,+ )if workflow == "controlnet" and control_pil is not None:# CN pipeline takes the control map + scale; guidance start/end bound its step range. Every kwarg# is signature-gated.@@ -12877,6 +12932,8 @@ class DiffusionBackend:elif state.transformer_cache:# diffusers resets FBCache only after a SUCCESSFUL __call__; a raised call leaves a stale residual.self._reset_step_cache(state.pipe)+ if applied_embeddings is not None:+ chunk_kwargs = applied_embeddings.rewrite(chunk_kwargs)# Per pipe, idempotent: a workflow pipe built by from_pipe has its own image processor.install_device_postprocess(pipe, logger)protect_ctx = protect_generation(pipe, denoise_steps, logger = logger)@@ -13093,6 +13150,14 @@ class DiffusionBackend:"inpaint_only_masked": True if inpaint_padding_applied is not None else None,"inpaint_padding": inpaint_padding_applied,}+ if applied_shift is not None:+ result["shift"] = shift+ result["base_shift"] = base_shift+ result["max_shift"] = max_shift+ result["applied_flow_shift"] = applied_shift+ if applied_embeddings is not None:+ result["embeddings"] = list(applied_embeddings.used)+ result["embeddings_skipped"] = list(applied_embeddings.skipped)logger.info("diffusion.generated: %s",format_generation_for_log(@@ -13107,6 +13172,8 @@ class DiffusionBackend:finally:if restore_scheduler is not None:restore_scheduler[0].scheduler = restore_scheduler[1]+ if applied_embeddings is not None:+ applied_embeddings.restore()if bg_token is not None:bg_compile._FORCE_EAGER.reset(bg_token)if static_skip_pipe is not None:@@ -13436,6 +13503,11 @@ class DiffusionBackend:"supports_live_preview": _live_preview_available(state),"prompt_hint": state.family.prompt_hint,"samplers": sampler_options(getattr(state.pipe, "scheduler", None)),+ # Hidden for a checkpoint that ships its own sampling grid (the load-time ComfyUI shift skips it too).+ "shift_control": None+ if pipe_sample_sigmas(state.pipe) is not None+ else shift_control(_state_shift_node(state), getattr(state.pipe, "scheduler", None)),+ "textual_inversion": getattr(state.family, "textual_inversion", None),"conditioning": conditioning_capabilities(state.family, _family_workflows(state.family)),diff --git a/studio/backend/core/inference/diffusion_cond_cache.py b/studio/backend/core/inference/diffusion_cond_cache.pyindex 8fa786ab690..66483021c06 100644--- a/studio/backend/core/inference/diffusion_cond_cache.py+++ b/studio/backend/core/inference/diffusion_cond_cache.py@@ -162,6 +162,9 @@ def install(# LoRA may target the text encoders; adapters attached -> always encode.if getattr(pipe, "_unsloth_loras", ()):return encode(*args, **kwargs)+ # Textual-inversion embeddings applied for this generation: the text alone does not key the result.+ if getattr(pipe, "__dict__", {}).get("_unsloth_embeddings"):+ return encode(*args, **kwargs)bound = signature.bind(*args, **kwargs)bound.apply_defaults()keyed = {diff --git a/studio/backend/core/inference/diffusion_embeddings.py b/studio/backend/core/inference/diffusion_embeddings.pynew file mode 100644index 00000000000..f9a00dd9e88--- /dev/null+++ b/studio/backend/core/inference/diffusion_embeddings.py@@ -0,0 +1,457 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Textual-inversion embeddings (EasyNegative and friends) for the CLIP-conditioned image families.++Files live in ``<studio_home>/embeddings/diffusion`` (beside ``loras/diffusion``). As in AUTOMATIC1111 the+filename stem is the trigger: a prompt or negative prompt naming it (any case, as a whole word, or ComfyUI's+``embedding:NAME``) uses the embedding for that generation. safetensors only (``.pt`` is pickled).++Formats, as diffusers' ``load_textual_inversion`` reads them (docs: "Textual inversion", SDXL section):+ * SD 1.x: one tensor ``[n, 768]`` (A1111 ``emb_params`` or a single diffusers ``<token>`` key);+ * SDXL: ``clip_l [n, 768]`` for ``text_encoder`` / ``tokenizer`` and ``clip_g [n, 1280]`` for+ ``text_encoder_2`` / ``tokenizer_2``.++Applying one never reloads the pipeline and never mutates a loaded component: each tokenizer is swapped for+a copy carrying the added tokens (``tok``, ``tok_1``, ... as diffusers names multi-vector embeddings, so the+pipeline's own ``maybe_convert_prompt`` expands them), and each text encoder's input embedding is wrapped by a+module that serves the new ids and calls the original for every other id. ``restore`` puts the original+objects back, so a generation that names no embedding runs the unchanged modules. The denoiser is untouched.+"""++from __future__ import annotations++import copy+import hashlib+import os+import re+from dataclasses import dataclass+from pathlib import Path+from typing import Any, Iterable, Optional++from utils.paths.path_utils import is_appledouble_metadata+from utils.paths.storage_roots import account_path++KIND_SD1 = "sd1"+KIND_SD2 = "sd2"+KIND_SDXL = "sdxl"+_EXTS = (".safetensors",)+_MAX_BYTES = 64 * 1024 * 1024+_MAX_VECTORS = 75+_PROMPT_KEYS = ("prompt", "prompt_2", "negative_prompt", "negative_prompt_2")+# Internal token prefix: lower case so a lowercasing tokenizer normaliser cannot split it.+_TOKEN_PREFIX = "<unsloth-ti-"+# (tokenizer attr, text encoder attr, SDXL tensor key)+_ENCODER_SLOTS = (+ ("tokenizer", "text_encoder", "clip_l"),+ ("tokenizer_2", "text_encoder_2", "clip_g"),+)+++def embeddings_dir() -> Path:+ d = account_path("embeddings/diffusion")+ d.mkdir(parents = True, exist_ok = True)+ return d+++@dataclass(frozen = True)+class EmbeddingEntry:+ id: str # filename stem: the trigger word+ path: str+ kind: Optional[str] # KIND_* or None when the tensors match no known encoder+ vectors: int+ size_bytes: int+++def _shapes(path: Path) -> dict[str, tuple[int, ...]]:+ """Tensor shapes from the safetensors header, without reading the data."""+ from safetensors import safe_open++ out: dict[str, tuple[int, ...]] = {}+ with safe_open(str(path), framework = "pt") as f:+ for key in f.keys():+ out[key] = tuple(f.get_slice(key).get_shape())+ return out+++def _vectors(shape: tuple[int, ...]) -> int:+ return shape[0] if len(shape) == 2 else 1 if len(shape) == 1 else 0+++def classify(shapes: dict[str, tuple[int, ...]]) -> tuple[Optional[str], int]:+ """``(kind, vectors)`` of an embedding file's tensors; kind None when it is not one we can apply."""+ if "clip_l" in shapes and "clip_g" in shapes:+ l, g = shapes["clip_l"], shapes["clip_g"]+ if l[-1:] == (768,) and g[-1:] == (1280,) and _vectors(l) == _vectors(g) > 0:+ return KIND_SDXL, _vectors(l)+ return None, 0+ if len(shapes) != 1:+ return None, 0+ (shape,) = shapes.values()+ n = _vectors(shape)+ if n <= 0:+ return None, 0+ if shape[-1] == 768:+ return KIND_SD1, n+ if shape[-1] == 1024:+ return KIND_SD2, n+ return None, 0+++def _account_allows():+ from .diffusion_lora import _account_allows as allows+ return allows()+++def list_embeddings(kind: Optional[str] = None) -> list[EmbeddingEntry]:+ """Embedding files in ``embeddings_dir()``, optionally only those of ``kind``. Never raises on one bad file."""+ root = embeddings_dir()+ allows = _account_allows()+ try:+ children = sorted(root.iterdir())+ except OSError:+ return []+ entries: list[EmbeddingEntry] = []+ seen: set[str] = set()+ for p in children:+ if p.suffix.lower() not in _EXTS or is_appledouble_metadata(p):+ continue+ try:+ if not p.is_file() or not allows(p):+ continue+ size = p.stat().st_size+ if size > _MAX_BYTES:+ continue+ found_kind, vectors = classify(_shapes(p))+ except Exception: # noqa: BLE001 - one unreadable file never fails the listing+ continue+ if vectors > _MAX_VECTORS:+ continue+ # Case-insensitive triggers: the first of two stems differing only in case wins.+ if p.stem.lower() in seen:+ continue+ seen.add(p.stem.lower())+ if kind is not None and found_kind != kind:+ continue+ entries.append(+ EmbeddingEntry(+ id = p.stem,+ path = str(p),+ kind = found_kind,+ vectors = vectors,+ size_bytes = size,+ )+ )+ return entries+++def _trigger_pattern(names: Iterable[str]) -> Optional[re.Pattern]:+ # Longest first so "bad-hands-5" wins over "bad"; one pass, so a replacement is never re-matched.+ ordered = sorted({n for n in names if n}, key = lambda n: (-len(n), n))+ if not ordered:+ return None+ alternation = "|".join(re.escape(n) for n in ordered)+ return re.compile(+ rf"(?<![\w-])(?:embedding:)?({alternation})(?![\w-])",+ re.IGNORECASE,+ )+++def _texts(value: Any) -> list[str]:+ if isinstance(value, str):+ return [value]+ if isinstance(value, (list, tuple)):+ return [v for v in value if isinstance(v, str)]+ return []+++def find_triggers(texts: Iterable[str], entries: Iterable[EmbeddingEntry]) -> list[EmbeddingEntry]:+ """The entries whose trigger appears in any of ``texts``, in listing order."""+ by_lower = {e.id.lower(): e for e in entries}+ pattern = _trigger_pattern(e.id for e in by_lower.values())+ if pattern is None:+ return []+ hit: set[str] = set()+ for text in texts:+ for m in pattern.finditer(text or ""):+ hit.add(m.group(1).lower())+ return [e for key, e in by_lower.items() if key in hit]+++def _internal_token(index: int, stem: str) -> str:+ slug = re.sub(r"[^a-z0-9]+", "-", stem.lower()).strip("-") or "emb"+ return f"{_TOKEN_PREFIX}{index}-{slug}>"+++def _read_tensors(path: str) -> dict[str, Any]:+ """The file's tensors, read through a handle pinned to the account-readable file."""+ from safetensors.torch import load as load_bytes++ from .diffusion_lora import _open_pinned++ with _open_pinned(Path(path)) as f:+ data = f.read(_MAX_BYTES + 1)+ if len(data) > _MAX_BYTES:+ raise ValueError(f"Embedding '{Path(path).name}' is too large.")+ return load_bytes(data)+++def _fingerprint(path: str) -> str:+ try:+ st = os.stat(path)+ except OSError:+ return "missing"+ return hashlib.sha1(f"{path}:{st.st_size}:{st.st_mtime_ns}".encode()).hexdigest()[:16]+++def _vectors_for_slot(tensors: dict[str, Any], kind: str, sdxl_key: str) -> Any:+ import torch++ if kind == KIND_SDXL:+ t = tensors[sdxl_key]+ else:+ (t,) = tensors.values()+ t = t.detach()+ if t.dim() == 1:+ t = t.unsqueeze(0)+ if t.dim() != 2:+ raise ValueError("embedding tensor must be [vectors, width]")+ return t.to(torch.float32)+++def _fits(tensors: dict[str, Any], kind: str, widths: list[int]) -> bool:+ """Whether ``tensors`` is an embedding for these text encoders (widths in slot order)."""++ def shape(t: Any) -> tuple[int, ...]:+ return tuple(getattr(t, "shape", ()))++ if kind == KIND_SDXL:+ if "clip_l" not in tensors or "clip_g" not in tensors or len(widths) < 2:+ return False+ l, g = shape(tensors["clip_l"]), shape(tensors["clip_g"])+ return (+ l[-1:] == (widths[0],)+ and g[-1:] == (widths[1],)+ and 0 < _vectors(l) == _vectors(g) <= _MAX_VECTORS+ )+ if len(tensors) != 1:+ return False+ (t,) = tensors.values()+ return shape(t)[-1:] == (widths[0],) and 0 < _vectors(shape(t)) <= _MAX_VECTORS+++def _make_extended_embedding(base: Any, extra: Any) -> Any:+ """``base`` (an nn.Embedding, left untouched) plus ``extra`` rows for ids >= ``base.num_embeddings``."""+ import torch++ class _ExtendedTokenEmbedding(torch.nn.Module):+ def __init__(self, base_module, extra_rows):+ super().__init__()+ self.base = base_module+ self.register_buffer("extra", extra_rows, persistent = False)+ self.base_count = int(base_module.num_embeddings)++ @property+ def weight(self): # read by code that inspects the embedding width / dtype+ return self.base.weight++ @property+ def num_embeddings(self):+ return self.base_count + int(self.extra.shape[0])++ @property+ def embedding_dim(self):+ return self.base.embedding_dim++ def forward(self, input_ids):+ is_extra = input_ids >= self.base_count+ out = self.base(input_ids.masked_fill(is_extra, 0))+ rows = self.extra.to(device = out.device, dtype = out.dtype)+ picked = rows[(input_ids - self.base_count).clamp(min = 0, max = rows.shape[0] - 1)]+ return torch.where(is_extra.unsqueeze(-1), picked, out)++ return _ExtendedTokenEmbedding(base, extra)+++@dataclass+class AppliedEmbeddings:+ """What ``apply`` installed on one pipe; ``restore`` undoes it exactly."""++ pipe: Any+ tokenizers: list[tuple[str, Any]]+ encoders: list[tuple[Any, Any]]+ tokens: dict[str, str]+ used: list[str]+ skipped: list[str]+ pattern: Optional[re.Pattern]+ marker_owners: list[Any]++ def rewrite(self, call_kwargs: dict) -> dict:+ """``call_kwargs`` with every trigger replaced by its internal token (a new dict; the input is kept)."""+ if self.pattern is None or not self.tokens:+ return call_kwargs+ out = dict(call_kwargs)++ def sub(text: str) -> str:+ return self.pattern.sub(lambda m: self.tokens.get(m.group(1).lower(), m.group(0)), text)++ for key in _PROMPT_KEYS:+ value = out.get(key)+ if isinstance(value, str):+ out[key] = sub(value)+ elif isinstance(value, list):+ out[key] = [sub(v) if isinstance(v, str) else v for v in value]+ return out++ def restore(self) -> None:+ for encoder, original in reversed(self.encoders):+ try:+ encoder.set_input_embeddings(original)+ except Exception: # noqa: BLE001 - restoring the rest still matters+ pass+ for attr, original in reversed(self.tokenizers):+ try:+ setattr(self.pipe, attr, original)+ except Exception: # noqa: BLE001+ pass+ for owner in self.marker_owners:+ try:+ if "_unsloth_embeddings" in owner.__dict__:+ del owner.__dict__["_unsloth_embeddings"]+ except Exception: # noqa: BLE001+ pass+ self.encoders.clear()+ self.tokenizers.clear()+++def _slots_for(pipe: Any, kind: str) -> list[tuple[str, Any, Any, str]]:+ slots = []+ for tok_attr, enc_attr, key in _ENCODER_SLOTS:+ tokenizer = getattr(pipe, tok_attr, None)+ encoder = getattr(pipe, enc_attr, None)+ if tokenizer is None or encoder is None:+ continue+ slots.append((tok_attr, tokenizer, encoder, key))+ if kind != KIND_SDXL:+ break # SD 1.x / 2.x: one encoder+ return slots+++def apply(+ pipe: Any,+ kind: Optional[str],+ call_kwargs: dict,+ *,+ entries: Optional[list[EmbeddingEntry]] = None,+ marker_owners: Iterable[Any] = (),+ logger: Any = None,+) -> Optional[AppliedEmbeddings]:+ """Install the embeddings the prompts in ``call_kwargs`` name; None (nothing touched) when they name none.++ ``kind`` is the family's ``textual_inversion`` format. A named file of another format is skipped (as A1111+ does) and reported in ``skipped``."""+ if kind is None:+ return None+ texts = [t for key in _PROMPT_KEYS for t in _texts(call_kwargs.get(key))]+ if not any(texts):+ return None+ if entries is None:+ entries = list_embeddings()+ named = find_triggers(texts, entries)+ if not named:+ return None+ slots = _slots_for(pipe, kind)+ want = 2 if kind == KIND_SDXL else 1+ if len(slots) < want:+ raise ValueError("The loaded pipeline has no CLIP text encoder to apply embeddings to.")+ widths = [int(enc.get_input_embeddings().embedding_dim) for _, _, enc, _ in slots]+ usable: list[EmbeddingEntry] = []+ loaded: list[tuple[int, EmbeddingEntry, dict]] = []+ skipped: list[str] = []+ for e in named:+ # Fit is checked against the loaded encoders, not the listing's kind: a file for another model is skipped.+ tensors = _read_tensors(e.path)+ if _fits(tensors, kind, widths):+ loaded.append((len(usable), e, tensors))+ usable.append(e)+ else:+ skipped.append(e.id)+ applied = AppliedEmbeddings(+ pipe = pipe,+ tokenizers = [],+ encoders = [],+ tokens = {},+ used = [],+ skipped = skipped,+ pattern = None,+ marker_owners = [],+ )+ if not usable:+ if logger is not None and skipped:+ logger.info("diffusion.embeddings: skipped (other model type): %s", ", ".join(skipped))+ return applied+ try:+ import torch+ for tok_attr, tokenizer, encoder, key in slots:+ original_embedding = encoder.get_input_embeddings()+ base_count = int(original_embedding.num_embeddings)+ width = int(original_embedding.embedding_dim)+ tok_copy = copy.deepcopy(tokenizer)+ extra_rows: dict[int, Any] = {}+ for i, entry, tensors in loaded:+ vectors = _vectors_for_slot(tensors, kind, key)+ if vectors.shape[-1] != width:+ raise ValueError(+ f"Embedding '{entry.id}' is {vectors.shape[-1]} wide; this text encoder takes {width}."+ )+ token = _internal_token(i, entry.id)+ names = [token] + [f"{token}_{j}" for j in range(1, vectors.shape[0])]+ tok_copy.add_tokens(names)+ for name, row in zip(names, vectors):+ token_id = tok_copy.convert_tokens_to_ids(name)+ if not isinstance(token_id, int) or token_id < base_count:+ raise ValueError(f"Could not add a token for embedding '{entry.id}'.")+ extra_rows[token_id] = row+ applied.tokens[entry.id.lower()] = token+ top = max(extra_rows) - base_count + 1+ table = torch.zeros(top, width, dtype = torch.float32)+ for token_id, row in extra_rows.items():+ table[token_id - base_count] = row+ weight = getattr(original_embedding, "weight", None)+ if weight is not None and getattr(weight, "device", None) is not None:+ if weight.device.type != "meta":+ table = table.to(device = weight.device, dtype = weight.dtype)+ applied.encoders.append((encoder, original_embedding))+ encoder.set_input_embeddings(_make_extended_embedding(original_embedding, table))+ applied.tokenizers.append((tok_attr, tokenizer))+ setattr(pipe, tok_attr, tok_copy)+ except BaseException:+ applied.restore()+ raise+ applied.used = [e.id for e in usable]+ applied.pattern = _trigger_pattern(e.id for e in usable)+ # Prompt caches key on this: the same text means a different encoding while it is set.+ marker = tuple((e.id, _fingerprint(e.path)) for e in usable)+ for owner in (pipe, *marker_owners):+ if owner is None or owner in applied.marker_owners:+ continue+ try:+ owner.__dict__["_unsloth_embeddings"] = marker+ applied.marker_owners.append(owner)+ except Exception: # noqa: BLE001+ pass+ if logger is not None:+ logger.info(+ "diffusion.embeddings: applied %s%s",+ ", ".join(applied.used),+ f" (skipped {', '.join(skipped)})" if skipped else "",+ )+ return applied+++def embedding_state(pipe: Any) -> tuple:+ """The embeddings applied on ``pipe`` right now, () when none (prompt-cache key)."""+ try:+ return tuple(pipe.__dict__.get("_unsloth_embeddings", ()) or ())+ except Exception: # noqa: BLE001+ return ()diff --git a/studio/backend/core/inference/diffusion_families.py b/studio/backend/core/inference/diffusion_families.pyindex c18af6701de..dcc2d2ecd76 100644--- a/studio/backend/core/inference/diffusion_families.py+++ b/studio/backend/core/inference/diffusion_families.py@@ -139,6 +139,11 @@ class DiffusionFamily:comfy_flow_shift_variants: tuple[tuple[str, Optional[float]], ...] = field(default_factory = tuple)+ # ComfyUI shift node (diffusion_flow_shift SHIFT_NODE_*); None = no Shift control. ``_variants``: (id substring, node), first match wins.+ comfy_shift_node: Optional[str] = None+ comfy_shift_node_variants: tuple[tuple[str, Optional[str]], ...] = field(default_factory = tuple)+ # Textual-inversion format (diffusion_embeddings KIND_*); None = no embeddings.+ textual_inversion: Optional[str] = None# (lowercased id substring, ((key, value), ...)) overriding ``base_repo``'s transformer config; first match wins.transformer_config_variants: tuple[tuple[str, tuple[tuple[str, Any], ...]], ...] = field(default_factory = tuple@@ -359,6 +364,13 @@ _FLUX3_SD_CPP_ARCH_MARKER = "Flux.3 Image Klein"_FAMILIES: tuple[DiffusionFamily, ...] = (DiffusionFamily(name = "flux.1",+ comfy_shift_node = "flux",+ # FLUX.1-schnell is ComfyUI's FLOW-type FluxSchnell (ModelSamplingDiscreteFlow, shift 1.0).+ comfy_shift_node_variants = tuple(+ (f"{prefix}-{model}", node)+ for model, node in (("schnell", "sd3"), ("krea-dev", "flux"), ("dev", "flux"))+ for prefix in ("flux.1", "flux1", "flux-1", "flux")+ ),label = "FLUX.1-dev",profile = FamilyProfile(auto_prefer = _GATED_AUTO_PREFER,@@ -423,6 +435,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (# flux match.DiffusionFamily(name = "flux.2-klein",+ comfy_shift_node = "flux",label = "FLUX.2 Klein",profile = FamilyProfile(preview_factors = "flux2",@@ -507,6 +520,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (# for reading order; the longest-alias match is what keeps "...-klein-base" off the distilled entry.DiffusionFamily(name = "flux.3-klein-base",+ comfy_shift_node = "flux",label = "FLUX.3 Klein Base",profile = FamilyProfile(# Left dense for EVERY transformer-quant scheme: per-token modulations, timestep embedders, norm_out, the@@ -561,6 +575,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (),DiffusionFamily(name = "flux.3-klein",+ comfy_shift_node = "flux",label = "FLUX.3 Klein",profile = FamilyProfile(# Left dense for EVERY transformer-quant scheme: per-token modulations, timestep embedders, norm_out, the@@ -612,6 +627,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (# BFL's own diffusers file.DiffusionFamily(name = "flux.2-dev",+ comfy_shift_node = "flux",label = "FLUX.2-dev",profile = FamilyProfile(te_int8_skip = (3, 0), preview_factors = "flux2"),pipeline_class = "Flux2Pipeline",@@ -651,6 +667,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (# FLUX instruction editing: FluxKontextPipeline takes an image + instruction. Specific aliases first so# detect_family prefers this over "flux.1".name = "flux.1-kontext",+ comfy_shift_node = "flux",profile = FamilyProfile(preview_factors = "flux1"),filter_reduction_configs_archs = _REDUCTION_RACE_ARCHS,comfy_flow_shift = flux_mu_shift(@@ -682,6 +699,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (# Qwen instruction editing: the 2511 checkpoint ships as QwenImageEditPlusPipeline. Specific aliases first so# detect_family prefers this over "qwen-image".name = "qwen-image-edit",+ comfy_shift_node = "sd3",# Same DiT class + unpadded text stream as qwen-image.profile = FamilyProfile(int8_exclude_tokens = _QWENIMAGE_INT8_EXCLUDES,@@ -757,6 +775,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (# qwen-image's DiT plus use_additional_t_cond / use_layer3d_rope (read from the base config for a GGUF) and an# RGBA VAE. Listed before qwen-image so the name outranks it.name = "qwen-image-layered",+ comfy_shift_node = "sd3",# Same DiT class + unpadded text stream as qwen-image.profile = FamilyProfile(int8_exclude_tokens = _QWENIMAGE_INT8_EXCLUDES,@@ -801,6 +820,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (),DiffusionFamily(name = "qwen-image",+ comfy_shift_node = "sd3",label = "Qwen-Image",profile = FamilyProfile(int8_exclude_tokens = _QWENIMAGE_INT8_EXCLUDES,@@ -870,6 +890,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (# qwen-image entry would hand it that family's pipeline, transformer, VAE and exclusion# rules, none of which fit.name = "qwen-image-2.1",+ comfy_shift_node = "flux",profile = FamilyProfile(# 2.1 is a 32-block SINGLE-stream DiT: no add_* projections, no txt_mlp, so the 20B MMDiT's exclusion list# does not apply and this one was measured rather than inherited. ``txt_in`` is here as a QUALITY lever,@@ -1011,6 +1032,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (),DiffusionFamily(name = "z-image",+ comfy_shift_node = "sd3",label = "Z-Image",profile = FamilyProfile(# exactly the Linears the hosted INT8 checkpoint quantizes (all input axes 256-divisible)@@ -1082,6 +1104,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (),DiffusionFamily(name = "krea-2",+ comfy_shift_node = "flux",label = "Krea 2",pipeline_class = "Krea2Pipeline",transformer_class = "Krea2Transformer2DModel",@@ -1111,6 +1134,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (# from_pretrained path loads it. NOT aliased to bare "lumina": Lumina-Next checkpoints are a different arch.DiffusionFamily(name = "lumina-2",+ comfy_shift_node = "sd3",profile = FamilyProfile(auto_bf16_when_resident = _LUMINA2_BF16_WHEN_RESIDENT),pipeline_class = "Lumina2Pipeline",transformer_class = "Lumina2Transformer2DModel",@@ -1136,6 +1160,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (# covers Full/Dev/Fast.DiffusionFamily(name = "hunyuanimage-2.1",+ comfy_shift_node = "sd3",profile = FamilyProfile(auto_static_skip = (("hunyuanvideo-community/hunyuanimage-2.1-diffusers", (("max", 2), ("steps", 50))),@@ -1168,6 +1193,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (),DiffusionFamily(name = "hidream-i1",+ comfy_shift_node = "sd3",profile = FamilyProfile(auto_bf16_when_resident = _HIDREAM_BF16_WHEN_RESIDENT,trusted_repos = ("hidream-ai/hidream-i1-dev", "hidream-ai/hidream-i1-fast"),@@ -1229,6 +1255,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (# pipeline. img2img / inpaint / ControlNet are the standard SDXL pipelines. No GGUF path.DiffusionFamily(name = "sdxl",+ textual_inversion = "sdxl",label = "SDXL",profile = FamilyProfile(preview_factors = "sdxl"),# ~1.8% slower warm, but compiled-UNet renders then match across servers (ComfyUI's default)@@ -2057,6 +2084,12 @@ def comfy_flow_shift_for(fam: Any, *identifiers: Optional[str]) -> Optional[floa_FLUX3_FLOW_MAP = {"flux.3-klein": True, "flux.3-klein-base": False}+def comfy_shift_node_for(fam: Any, *identifiers: Optional[str]) -> Optional[str]:+ """The ComfyUI shift node (diffusion_flow_shift SHIFT_NODE_*) for this checkpoint, or None."""+ row = _first_variant(getattr(fam, "comfy_shift_node_variants", ()), identifiers)+ return row[1] if row else getattr(fam, "comfy_shift_node", None)++def transformer_config_overrides_for(fam: Any, *identifiers: Optional[str]) -> dict[str, Any]:row = _first_variant(getattr(fam, "transformer_config_variants", ()), identifiers)overrides = dict(row[1]) if row else {}diff --git a/studio/backend/core/inference/diffusion_flow_shift.py b/studio/backend/core/inference/diffusion_flow_shift.pyindex 952ab6d67da..4c954b72aa0 100644--- a/studio/backend/core/inference/diffusion_flow_shift.py+++ b/studio/backend/core/inference/diffusion_flow_shift.py@@ -148,3 +148,119 @@ def sample_sigmas_for_steps(grid: tuple[float, ...], steps: int) -> list[float]:lo = min(int(pos), last - 1)out.append(grid[lo] + (grid[lo + 1] - grid[lo]) * (pos - lo))return out+++# Per-request shift (ComfyUI comfy_extras/nodes_model_advanced.py): one static shift on a per-generation scheduler copy.+# ModelSamplingSD3 / ModelSamplingAuraFlow: sigma = shift * t / (1 + (shift - 1) * t).+SHIFT_NODE_SD3 = "sd3"+# ModelSamplingFlux: mu = linear in latent tokens between (256, base_shift) and (4096, max_shift),+# sigma = e^mu / (e^mu + 1/t - 1), the same curve as the static shift e^mu (``flux_mu_shift``).+SHIFT_NODE_FLUX = "flux"+SHIFT_NODES = (SHIFT_NODE_SD3, SHIFT_NODE_FLUX)+COMFY_FLUX_MAX_SHIFT = 1.15+COMFY_FLUX_BASE_SHIFT = 0.5+_COMFY_FLUX_X1 = 256+_COMFY_FLUX_X2 = 4096+# The nodes' widget bounds (min 0.0, max 100.0); a static shift of 0 collapses every sigma to 0.+SHIFT_MAX = 100.0+++def comfy_model_sampling_flux_mu(+ max_shift: float, base_shift: float, width: int, height: int+) -> float:+ """ModelSamplingFlux.patch: the ``shift`` (mu) it sets for an image of ``width`` x ``height``."""+ mm = (max_shift - base_shift) / (_COMFY_FLUX_X2 - _COMFY_FLUX_X1)+ b = base_shift - mm * _COMFY_FLUX_X1+ return (width * height / (8 * 8 * 2 * 2)) * mm + b+++def requested_flow_shift(+ node: Optional[str],+ *,+ shift: Optional[float] = None,+ base_shift: Optional[float] = None,+ max_shift: Optional[float] = None,+ width: Optional[int] = None,+ height: Optional[int] = None,+ defaults: Optional[dict] = None,+) -> Optional[float]:+ """The static flow shift a request asks for, or None when it sets none (today's schedule).++ ``defaults`` (``shift_control``'s) fills the base / max shift a request leaves out, so changing one+ starts from what the load resolved. Raises ValueError for a field this family's node does not take."""+ if shift is None and base_shift is None and max_shift is None:+ return None+ if node not in SHIFT_NODES:+ raise ValueError("Shift is supported for flow-matching image models only.")+ if node == SHIFT_NODE_SD3:+ if base_shift is not None or max_shift is not None:+ raise ValueError(+ "This model takes a single Shift value (ComfyUI ModelSamplingSD3), not base / max shift."+ )+ value = float(shift)+ if not (math.isfinite(value) and 0.0 < value <= SHIFT_MAX):+ raise ValueError(f"Shift must be above 0 and at most {SHIFT_MAX:g}.")+ return value+ if shift is not None:+ raise ValueError(+ "This model takes base / max shift (ComfyUI ModelSamplingFlux), not a single Shift value."+ )+ defaults = defaults or {}+ hi = float(defaults.get("max_shift", COMFY_FLUX_MAX_SHIFT) if max_shift is None else max_shift)+ lo = float(+ defaults.get("base_shift", COMFY_FLUX_BASE_SHIFT) if base_shift is None else base_shift+ )+ for value in (hi, lo):+ if not (math.isfinite(value) and 0.0 <= value <= SHIFT_MAX):+ raise ValueError(f"Base and max shift must be between 0 and {SHIFT_MAX:g}.")+ if not width or not height:+ raise ValueError("Base / max shift needs the output width and height.")+ return flux_mu_shift(comfy_model_sampling_flux_mu(hi, lo, int(width), int(height)))+++def scheduler_with_flow_shift(scheduler: Any, shift: float) -> Any:+ """A copy of ``scheduler`` sampling at static ``shift`` (dynamic shifting and terminal stretch off), or+ ``scheduler`` itself when it already samples there (the resolved value: no rebuild, identical render).++ Raises ValueError when the scheduler has no flow shift Studio can set."""+ config = getattr(scheduler, "config", None)+ overrides = flow_shift_overrides(config, shift) if config is not None else None+ if not overrides:+ raise ValueError("The loaded model's scheduler has no flow shift to override.")+ if all(config.get(k) == v for k, v in overrides.items()):+ return scheduler+ return type(scheduler).from_config(config, **overrides)+++def shift_control(node: Optional[str], scheduler: Any) -> Optional[dict]:+ """What the Shift control offers on this load, or None to hide it: the node kind plus the+ defaults to seed it with (``shift`` None when the schedule is resolution-dependent)."""+ if node not in SHIFT_NODES:+ return None+ config = getattr(scheduler, "config", None)+ if config is None or not flow_shift_overrides(config, 1.0):+ return None+ if node == SHIFT_NODE_FLUX:+ base, top = COMFY_FLUX_BASE_SHIFT, COMFY_FLUX_MAX_SHIFT+ try:+ if not config.get("use_dynamic_shifting", False) and "flow_shift" not in config.keys():+ # The load resolved a fixed mu (ComfyUI's fixed-mu templates, ``flux_mu_shift``): base = max = mu+ # reproduces it at every size.+ static = float(config.get("shift"))+ if static > 0:+ base = top = math.log(static)+ else:+ base = float(config.get("base_shift", base))+ top = float(config.get("max_shift", top))+ except (TypeError, ValueError):+ base, top = COMFY_FLUX_BASE_SHIFT, COMFY_FLUX_MAX_SHIFT+ return {"node": node, "base_shift": base, "max_shift": top}+ current: Optional[float] = None+ try:+ if "flow_shift" in config.keys():+ current = float(config.get("flow_shift"))+ elif not config.get("use_dynamic_shifting", False):+ current = float(config.get("shift"))+ except (TypeError, ValueError):+ current = None+ return {"node": node, "shift": current}diff --git a/studio/backend/core/inference/diffusion_prompt_cache.py b/studio/backend/core/inference/diffusion_prompt_cache.pyindex e3ad3eb032c..a8df329df67 100644--- a/studio/backend/core/inference/diffusion_prompt_cache.py+++ b/studio/backend/core/inference/diffusion_prompt_cache.py@@ -216,6 +216,13 @@ def _lora_state(pipe: Any) -> Any:return None+def _embedding_state(pipe: Any) -> Any:+ try:+ return _plain(tuple(pipe.__dict__.get("_unsloth_embeddings", ()) or ()))+ except (AttributeError, TypeError):+ return None++def _hash(payload: dict) -> str:raw = json.dumps(payload, sort_keys = True, separators = (",", ":"))return hashlib.sha256(raw.encode("utf-8")).hexdigest()@@ -290,14 +297,17 @@ def _wrap_encode_prompt(keyed = {k: _plain(v) for k, v in bound.arguments.items() if k not in _KEY_EXCLUDED_ARGS}- key = _hash(- {- "load": load_fp,- "encoders": _encoder_identity(pipe),- "loras": _lora_state(lora_owner),- "args": keyed,- }- )+ payload = {+ "load": load_fp,+ "encoders": _encoder_identity(pipe),+ "loras": _lora_state(lora_owner),+ "args": keyed,+ }+ # Textual-inversion embeddings change what the same text encodes to; absent = the unchanged key.+ embeddings = _embedding_state(pipe)+ if embeddings:+ payload["embeddings"] = embeddings+ key = _hash(payload)hit = cache.get(key, bound.arguments.get("device"))except TypeError:cache.stats["bypassed"] += 1diff --git a/studio/backend/core/inference/image_gallery.py b/studio/backend/core/inference/image_gallery.pyindex bc5ee4179ea..71a655ca18a 100644--- a/studio/backend/core/inference/image_gallery.py+++ b/studio/backend/core/inference/image_gallery.py@@ -82,6 +82,9 @@ def _params_text(meta: dict[str, Any]) -> str:fields += ", Inpaint area: Only masked"if meta.get("inpaint_padding") is not None:fields += f", Masked area padding: {meta['inpaint_padding']}"+ # Static flow shift, read back by the paste parser; base / max shift stay in the recipe chunk only.+ if meta.get("shift") is not None:+ fields += f", Shift: {meta['shift']}"if meta.get("generator"):fields += f", Version: {meta['generator']}"lines.append(fields)diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.pyindex ad2b7808d7c..28d86e132a8 100644--- a/studio/backend/models/inference.py+++ b/studio/backend/models/inference.py@@ -5070,6 +5070,28 @@ class DiffusionGenerateRequest(BaseModel):description = "Noise schedule for the sampler. Omitted or 'automatic' uses the sampler's ""usual schedule (Karras for DPM++ 2M, Exponential for DPM++ 2M SDE, else the model's).",)+ shift: Optional[float] = Field(+ None,+ gt = 0.0,+ le = 100.0,+ description = "Static flow shift for this generation, as ComfyUI's ModelSamplingSD3 / "+ "ModelSamplingAuraFlow `shift`, on models whose status `shift_control.node` is 'sd3'. "+ "Omitted keeps the model's schedule.",+ )+ base_shift: Optional[float] = Field(+ None,+ ge = 0.0,+ le = 100.0,+ description = "ComfyUI ModelSamplingFlux `base_shift` (default 0.5) for models whose status "+ "`shift_control.node` is 'flux'. Omitted with `max_shift` keeps the model's schedule.",+ )+ max_shift: Optional[float] = Field(+ None,+ ge = 0.0,+ le = 100.0,+ description = "ComfyUI ModelSamplingFlux `max_shift` (default 1.15), applied at the output "+ "size, for models whose status `shift_control.node` is 'flux'.",+ )mask_blur: Optional[int] = Field(None,ge = 0,@@ -5282,6 +5304,18 @@ class GalleryImage(BaseModel):schedule_type: Optional[str] = Field(None, description = "Schedule type the sampler ran with (resolved); null = the config's")+ shift: Optional[float] = Field(+ None, description = "Requested static flow shift; null = the model's"+ )+ base_shift: Optional[float] = Field(+ None, description = "Requested ModelSamplingFlux base shift; null = the model's"+ )+ max_shift: Optional[float] = Field(+ None, description = "Requested ModelSamplingFlux max shift; null = the model's"+ )+ embeddings: Optional[list[str]] = Field(+ None, description = "Textual-inversion embeddings the prompts used (filename stems)"+ )mask_blur: Optional[int] = Field(None, description = "Inpaint mask blur radius (px) applied; null = none")@@ -5762,6 +5796,18 @@ class DiffusionStatusResponse(BaseModel):"first is 'default' (the shipped scheduler). Empty when there is no choice: flow-matching ""models, the native and MLX engines, or nothing loaded.",)+ shift_control: Optional[Dict[str, Any]] = Field(+ None,+ description = "The Shift control the loaded flow-matching model takes, or null to hide it: "+ "{node: 'sd3', shift} (one static shift, ComfyUI ModelSamplingSD3 / AuraFlow; shift = the "+ "loaded value or null when resolution-dependent) or {node: 'flux', base_shift, max_shift} "+ "(ComfyUI ModelSamplingFlux defaults). Diffusers engine only.",+ )+ textual_inversion: Optional[str] = Field(+ None,+ description = "Textual-inversion embedding format the loaded model takes ('sdxl'), or null when "+ "it takes none. Embeddings are listed by GET /api/models/diffusion-embeddings.",+ )# declare provenance explicitly because Pydantic extra="ignore" would otherwise discard it.resolved: Optional[Dict[str, DiffusionResolvedControl]] = Field(None,diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 576fc7c0e2a..68fb33f6bb7 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -51905,6 +51905,27 @@ def _sampler_kwargs(generate: Any, sampler: Optional[str], schedule_type: Optionreturn {}+def _shift_kwargs(generate: Any, request: Any) -> dict:+ """``shift`` / ``base_shift`` / ``max_shift`` for an engine whose generate() takes them (diffusers only).++ Omitted on every engine when unset, so an older call is unchanged; a value sent to another engine+ is refused (ValueError -> 400) rather than silently ignored."""+ values = {+ "shift": getattr(request, "shift", None),+ "base_shift": getattr(request, "base_shift", None),+ "max_shift": getattr(request, "max_shift", None),+ }+ if all(v is None for v in values.values()):+ return {}+ try:+ import inspect+ if "shift" in inspect.signature(generate).parameters:+ return values+ except (TypeError, ValueError):+ pass+ raise ValueError("Shift is supported on the diffusers engine only.")++def _inpaint_mask_kwargs(generate: Any, request: Any) -> dict:"""Inpaint mask kwargs generate() accepts; only the set ones, so an untouched request is unchanged."""try:@@ -52023,6 +52044,9 @@ async def _external_image_generation(or body.localized_editor body.sampleror body.schedule_type+ or body.shift is not None+ or body.base_shift is not None+ or body.max_shift is not Noneor body.mask_blur is not Noneor body.inpaint_only_masked is not Noneor body.inpaint_padding is not None@@ -52365,6 +52389,7 @@ async def _generate_diffusion_image_run(),**_live_preview_kwarg(backend.generate, request.live_preview),**_sampler_kwargs(backend.generate, request.sampler, request.schedule_type),+ **_shift_kwargs(backend.generate, request),**_inpaint_mask_kwargs(backend.generate, request),)break@@ -52484,9 +52509,17 @@ async def _generate_diffusion_image_run("sampler": result.get("sampler"),"schedule_type": result.get("schedule_type"),**{- k: result[k]- for k in ("mask_blur", "inpaint_only_masked", "inpaint_padding")- if result.get(k) is not None+ key: result[key]+ for key in (+ "shift",+ "base_shift",+ "max_shift",+ "embeddings",+ "mask_blur",+ "inpaint_only_masked",+ "inpaint_padding",+ )+ if result.get(key) not in (None, [])},"created_at": created_at,},diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.pyindex c6a93e288c6..8ce45d38d8c 100644--- a/studio/backend/routes/models.py+++ b/studio/backend/routes/models.py@@ -3216,6 +3216,50 @@ async def scan_diffusion_loras(}[email protected]("/diffusion-embeddings")+async def scan_diffusion_embeddings(+ family: Optional[str] = Query(+ default = None, description = "Filter to embeddings this diffusion family can use"+ ),+ current_subject: str = Depends(get_current_subject),+):+ """List textual-inversion embeddings in ``<studio_home>/embeddings/diffusion`` for the Images page.++ The filename stem is the trigger word: naming it in a prompt or negative prompt uses the embedding.+ ``family`` keeps only files of that family's format (an unknown family, or one without embeddings,+ lists none). One directory scan reading safetensors headers only; no network.+ """+ from core.inference import diffusion_embeddings++ kind = None+ if family:+ fam = next((f for f in _diffusion_families() if f.name == family.strip().lower()), None)+ kind = getattr(fam, "textual_inversion", None) if fam is not None else None+ entries = (+ await asyncio.to_thread(diffusion_embeddings.list_embeddings, kind)+ if kind is not None or not family+ else []+ )+ return {+ "embeddings": [+ {+ "id": e.id,+ "trigger": e.id,+ "kind": e.kind,+ "vectors": e.vectors,+ "size_bytes": e.size_bytes,+ }+ for e in entries+ ],+ "embeddings_dir": str(diffusion_embeddings.embeddings_dir()),+ }+++def _diffusion_families():+ from core.inference.diffusion_families import _FAMILIES+ return _FAMILIES++@router.get("/diffusion-controlnets")async def scan_diffusion_controlnets(family: Optional[str] = Query(diff --git a/studio/backend/tests/test_diffusion_embeddings.py b/studio/backend/tests/test_diffusion_embeddings.pynew file mode 100644index 00000000000..6efa0a11348--- /dev/null+++ b/studio/backend/tests/test_diffusion_embeddings.py@@ -0,0 +1,219 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Textual-inversion embeddings: discovery, A1111 trigger matching, and apply / restore on real tiny+SD 1.x and SDXL pipelines (bit-identical once restored, nothing touched when no trigger is named)."""++import pytest++torch = pytest.importorskip("torch")+pytest.importorskip("safetensors")+from safetensors.torch import save_file # noqa: E402++from core.inference import diffusion_embeddings as de # noqa: E402+++def emb_dir(tmp_path, monkeypatch):+ d = tmp_path / "embeddings"+ d.mkdir()+ monkeypatch.setattr(de, "embeddings_dir", lambda: d)+ return d+++def _entry(+ stem,+ path = "/nonexistent",+ kind = None,+):+ return de.EmbeddingEntry(id = stem, path = path, kind = kind, vectors = 1, size_bytes = 0)+++def test_classify_formats():+ assert de.classify({"clip_l": (8, 768), "clip_g": (8, 1280)}) == (de.KIND_SDXL, 8)+ assert de.classify({"emb_params": (4, 768)}) == (de.KIND_SD1, 4)+ assert de.classify({"<cat-toy>": (768,)}) == (de.KIND_SD1, 1)+ assert de.classify({"emb_params": (2, 1024)}) == (de.KIND_SD2, 2)+ assert de.classify({"clip_l": (8, 768), "clip_g": (4, 1280)}) == (None, 0)+ assert de.classify({"a": (1, 768), "b": (1, 768)}) == (None, 0)+++def test_listing_reads_headers_and_filters_by_kind(emb_dir):+ save_file(+ {"clip_l": torch.zeros(2, 768), "clip_g": torch.zeros(2, 1280)},+ str(emb_dir / "EasyNegXL.safetensors"),+ )+ save_file({"emb_params": torch.zeros(3, 768)}, str(emb_dir / "EasyNegative.safetensors"))+ (emb_dir / "notes.txt").write_text("x")+ (emb_dir / "broken.safetensors").write_bytes(b"not a safetensors file")+ (emb_dir / "pickled.pt").write_bytes(b"x")+ rows = {e.id: (e.kind, e.vectors) for e in de.list_embeddings()}+ assert rows == {"EasyNegXL": (de.KIND_SDXL, 2), "EasyNegative": (de.KIND_SD1, 3)}+ assert [e.id for e in de.list_embeddings(de.KIND_SDXL)] == ["EasyNegXL"]+++def test_triggers_follow_a1111_and_comfyui_syntax():+ entries = [_entry("EasyNegative"), _entry("bad-hands-5"), _entry("bad"), _entry("cat")]++ def hits(text):+ return sorted(e.id for e in de.find_triggers([text], entries))++ assert hits("blurry, easynegative, lowres") == ["EasyNegative"]+ assert hits("(embedding:EasyNegative:1.2)") == ["EasyNegative"]+ assert hits("bad-hands-5") == ["bad-hands-5"] # longest stem wins, "bad" is not also triggered+ assert hits("bad, bad-hands-5") == ["bad", "bad-hands-5"]+ assert hits("concatenate a category") == [] # whole words only+ assert hits("") == []+++def test_rewrite_replaces_only_triggers():+ applied = de.AppliedEmbeddings(+ pipe = None,+ tokenizers = [],+ encoders = [],+ tokens = {"easynegative": "<unsloth-ti-0-easynegative>"},+ used = ["EasyNegative"],+ skipped = [],+ pattern = de._trigger_pattern(["EasyNegative"]),+ marker_owners = [],+ )+ src = {+ "prompt": ["a cat", "EasyNegative cat"],+ "negative_prompt": "embedding:easynegative, blurry",+ "x": 1,+ }+ out = applied.rewrite(src)+ assert out["prompt"] == ["a cat", "<unsloth-ti-0-easynegative> cat"]+ assert out["negative_prompt"] == "<unsloth-ti-0-easynegative>, blurry"+ assert out["x"] == 1+ assert src["prompt"] == ["a cat", "EasyNegative cat"] # the caller's kwargs are kept+++def test_no_trigger_touches_nothing(emb_dir):+ save_file({"emb_params": torch.zeros(1, 768)}, str(emb_dir / "EasyNegative.safetensors"))++ class Exploding:+ def __getattr__(self, name):+ raise AssertionError(f"pipe.{name} read without a trigger")++ assert (+ de.apply(Exploding(), de.KIND_SD1, {"prompt": ["a cat"], "negative_prompt": "blurry"})+ is None+ )+ assert de.apply(Exploding(), None, {"prompt": ["EasyNegative"]}) is None+++def _tiny(repo, cls_name):+ diffusers = pytest.importorskip("diffusers")+ from huggingface_hub import snapshot_download++ try:+ path = snapshot_download(repo)+ except Exception as exc: # noqa: BLE001+ pytest.skip(f"tiny pipeline not reachable: {exc}")+ return getattr(diffusers, cls_name).from_pretrained(path, torch_dtype = torch.float32)+++def _encode(pipe, prompt, negative):+ with torch.no_grad():+ out = pipe.encode_prompt(+ prompt,+ device = "cpu",+ num_images_per_prompt = 1,+ do_classifier_free_guidance = True,+ negative_prompt = negative,+ )+ return [t for t in out if isinstance(t, torch.Tensor)]+++def _check_pipeline(pipe, kind, emb_dir, tensors_for):+ widths = [pipe.text_encoder.get_input_embeddings().embedding_dim]+ if kind == de.KIND_SDXL:+ widths.append(pipe.text_encoder_2.get_input_embeddings().embedding_dim)+ save_file(tensors_for(widths, 3), str(emb_dir / "EasyNeg.safetensors"))+ # A file for another text encoder (wrong width): skipped like A1111, never applied.+ save_file({"emb_params": torch.ones(2, widths[0] + 8)}, str(emb_dir / "OtherModel.safetensors"))+ originals = {attr: getattr(pipe, attr, None) for attr in ("tokenizer", "tokenizer_2")}+ embeds = {+ attr: getattr(pipe, attr).get_input_embeddings()+ for attr in ("text_encoder", "text_encoder_2")+ if getattr(pipe, attr, None) is not None+ }+ prompt, negative = "a photo of a cat", "blurry, EasyNeg, othermodel"+ base = _encode(pipe, prompt, negative)++ call = {"prompt": [prompt], "negative_prompt": negative}+ applied = de.apply(pipe, kind, call, marker_owners = ())+ assert applied is not None and applied.used == ["EasyNeg"] and applied.skipped == ["OtherModel"]+ assert pipe.__dict__["_unsloth_embeddings"][0][0] == "EasyNeg"+ rewritten = applied.rewrite(call)+ token = applied.tokens["easyneg"]+ assert token in rewritten["negative_prompt"] and "othermodel" in rewritten["negative_prompt"]+ for tok_attr, enc_attr in (("tokenizer", "text_encoder"), ("tokenizer_2", "text_encoder_2")):+ tok = getattr(pipe, tok_attr, None)+ if tok is None or enc_attr not in embeds:+ continue+ ids = tok(pipe.maybe_convert_prompt(rewritten["negative_prompt"], tok)).input_ids+ base_n = embeds[enc_attr].num_embeddings+ assert sum(1 for i in ids if i >= base_n) == 3+ with_ti = _encode(pipe, rewritten["prompt"][0], rewritten["negative_prompt"])+ assert torch.equal(with_ti[0], base[0])+ assert not torch.equal(with_ti[1], base[1])++ applied.restore()+ for attr, original in originals.items():+ assert getattr(pipe, attr, None) is original+ for attr, module in embeds.items():+ assert getattr(pipe, attr).get_input_embeddings() is module+ assert "_unsloth_embeddings" not in pipe.__dict__+ after = _encode(pipe, prompt, negative)+ assert all(torch.equal(a, b) for a, b in zip(after, base))++[email protected]_network+def test_sd1_pipeline_applies_and_restores(emb_dir):+ pipe = _tiny("hf-internal-testing/tinier-stable-diffusion-pipe", "StableDiffusionPipeline")+ _check_pipeline(pipe, de.KIND_SD1, emb_dir, lambda w, n: {"emb_params": torch.randn(n, w[0])})++[email protected]_network+def test_sdxl_pipeline_applies_both_encoders_and_restores(emb_dir):+ pipe = _tiny("hf-internal-testing/tiny-stable-diffusion-xl-pipe", "StableDiffusionXLPipeline")+ _check_pipeline(+ pipe,+ de.KIND_SDXL,+ emb_dir,+ lambda w, n: {"clip_l": torch.randn(n, w[0]), "clip_g": torch.randn(n, w[1])},+ )+++def test_prompt_cache_keys_on_applied_embeddings():+ from core.inference import diffusion_prompt_cache as pc++ class Pipe:+ def __init__(self):+ self.calls = 0++ def encode_prompt(+ self,+ prompt,+ device = None,+ ):+ self.calls += 1+ return torch.full((1,), float(self.calls))++ pipe = Pipe()+ cache = pc.PromptCache(budget = 1 << 20)+ assert pc._wrap_encode_prompt(pipe, cache, {}, pipe, None)+ first = pipe.encode_prompt("x")+ assert torch.equal(pipe.encode_prompt("x"), first) and pipe.calls == 1+ pipe.__dict__["_unsloth_embeddings"] = (("EasyNeg", "abc"),)+ assert not torch.equal(pipe.encode_prompt("x"), first) and pipe.calls == 2+ del pipe.__dict__["_unsloth_embeddings"]+ assert torch.equal(pipe.encode_prompt("x"), first) and pipe.calls == 2+++def test_families_gate_textual_inversion():+ from core.inference.diffusion_families import _FAMILIES+ assert {f.name for f in _FAMILIES if f.textual_inversion} == {"sdxl"}+ assert next(f for f in _FAMILIES if f.name == "sdxl").textual_inversion == de.KIND_SDXLdiff --git a/studio/backend/tests/test_diffusion_shift_override.py b/studio/backend/tests/test_diffusion_shift_override.pynew file mode 100644index 00000000000..1f52ab1a930--- /dev/null+++ b/studio/backend/tests/test_diffusion_shift_override.py@@ -0,0 +1,332 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Per-request Shift override: the ComfyUI node mapping per family, the schedule it builds, validation."""++import math++import pytest++from core.inference.diffusion_families import _FAMILIES, comfy_shift_node_for+from core.inference.diffusion_flow_shift import (+ COMFY_FLUX_BASE_SHIFT,+ COMFY_FLUX_MAX_SHIFT,+ SHIFT_NODE_FLUX,+ SHIFT_NODE_SD3,+ comfy_model_sampling_flux_mu,+ requested_flow_shift,+ scheduler_with_flow_shift,+ shift_control,+)+++# ComfyUI comfy/model_sampling.py: time_snr_shift (ModelSamplingDiscreteFlow, set by ModelSamplingSD3 /+# ModelSamplingAuraFlow) and flux_time_shift (ModelSamplingFlux), transcribed.+def _comfy_time_snr_shift(alpha, t):+ if alpha == 1.0:+ return t+ return alpha * t / (1 + (alpha - 1) * t)+++def _comfy_flux_time_shift(mu, sigma, t):+ return math.exp(mu) / (math.exp(mu) + (1 / t - 1) ** sigma)+++# comfy_extras/nodes_model_advanced.py ModelSamplingFlux.patch, transcribed.+def _comfy_model_sampling_flux_shift(max_shift, base_shift, width, height):+ x1 = 256+ x2 = 4096+ mm = (max_shift - base_shift) / (x2 - x1)+ b = base_shift - mm * x1+ return (width * height / (8 * 8 * 2 * 2)) * mm + b+++# The node a ComfyUI workflow for each family sets shift with: the model's native model_sampling (comfy/model_base.py+# ModelType.FLUX -> ModelSamplingFlux, ModelType.FLOW -> ModelSamplingDiscreteFlow), except Qwen-Image / Edit /+# Layered whose templates patch ModelSamplingAuraFlow over the FLUX-type QwenImage.+EXPECTED_NODES = {+ "flux.1": SHIFT_NODE_FLUX,+ "flux.1-kontext": SHIFT_NODE_FLUX,+ "flux.2-klein": SHIFT_NODE_FLUX,+ "flux.2-dev": SHIFT_NODE_FLUX,+ "flux.3-klein": SHIFT_NODE_FLUX,+ "flux.3-klein-base": SHIFT_NODE_FLUX,+ "krea-2": SHIFT_NODE_FLUX,+ "qwen-image-2.1": SHIFT_NODE_FLUX,+ "qwen-image": SHIFT_NODE_SD3,+ "qwen-image-edit": SHIFT_NODE_SD3,+ "qwen-image-layered": SHIFT_NODE_SD3,+ "z-image": SHIFT_NODE_SD3,+ "lumina-2": SHIFT_NODE_SD3,+ "hunyuanimage-2.1": SHIFT_NODE_SD3,+ "hidream-i1": SHIFT_NODE_SD3,+ # Not offered: Ideogram 4 runs its own mu/std preset, SDXL is not flow matching.+ "ideogram-4": None,+ "sdxl": None,+}+++def test_every_family_has_its_comfyui_shift_node():+ assert {f.name: f.comfy_shift_node for f in _FAMILIES} == EXPECTED_NODES+++ "ident, node",+ [+ (+ "black-forest-labs/FLUX.1-schnell",+ SHIFT_NODE_SD3,+ ), # ComfyUI FluxSchnell is ModelType.FLOW+ ("flux1-schnell-Q4_K_M.gguf", SHIFT_NODE_SD3),+ ("black-forest-labs/FLUX.1-dev", SHIFT_NODE_FLUX),+ ("black-forest-labs/FLUX.1-Krea-dev", SHIFT_NODE_FLUX),+ ("someone/my-flux-finetune", SHIFT_NODE_FLUX),+ ],+)+def test_flux1_checkpoint_variants(ident, node):+ fam = next(f for f in _FAMILIES if f.name == "flux.1")+ assert comfy_shift_node_for(fam, ident) == node++[email protected]("hi, lo", [(1.15, 0.5), (3.0, 0.5), (1.0, 1.0), (0.0, 2.0)])[email protected]("w, h", [(1024, 1024), (512, 768), (1344, 768), (64, 64)])+def test_flux_mu_is_comfyui_model_sampling_flux(hi, lo, w, h):+ assert comfy_model_sampling_flux_mu(hi, lo, w, h) == pytest.approx(+ _comfy_model_sampling_flux_shift(hi, lo, w, h), abs = 1e-12+ )+++def test_flux_mu_matches_diffusers_calculate_shift():+ """Same line as diffusers' FluxPipeline mu at its default anchors (image_seq_len = w*h/256)."""+ pytest.importorskip("diffusers")+ from diffusers.pipelines.flux.pipeline_flux import calculate_shift++ for w, h in ((1024, 1024), (768, 1344)):+ seq = (w // 16) * (h // 16)+ assert comfy_model_sampling_flux_mu(1.15, 0.5, w, h) == pytest.approx(+ calculate_shift(seq, 256, 4096, 0.5, 1.15), abs = 1e-9+ )+++def test_unset_requests_nothing():+ for node in (None, SHIFT_NODE_SD3, SHIFT_NODE_FLUX):+ assert requested_flow_shift(node, width = 1024, height = 1024) is None+++def test_sd3_node_is_the_static_shift():+ assert requested_flow_shift(SHIFT_NODE_SD3, shift = 3.1) == 3.1+++def test_flux_node_resolves_mu_at_the_output_size():+ got = requested_flow_shift(+ SHIFT_NODE_FLUX, base_shift = 0.5, max_shift = 1.15, width = 1024, height = 1024+ )+ assert got == pytest.approx(math.exp(1.15))+ only_max = requested_flow_shift(SHIFT_NODE_FLUX, max_shift = 2.0, width = 512, height = 512)+ assert only_max == pytest.approx(+ math.exp(_comfy_model_sampling_flux_shift(2.0, COMFY_FLUX_BASE_SHIFT, 512, 512))+ )+ only_base = requested_flow_shift(SHIFT_NODE_FLUX, base_shift = 0.8, width = 512, height = 512)+ assert only_base == pytest.approx(+ math.exp(_comfy_model_sampling_flux_shift(COMFY_FLUX_MAX_SHIFT, 0.8, 512, 512))+ )+++ "node, kwargs, needle",+ [+ (None, {"shift": 3.0}, "flow-matching"),+ (SHIFT_NODE_SD3, {"base_shift": 0.5}, "single Shift"),+ (SHIFT_NODE_SD3, {"shift": 0.0}, "above 0"),+ (SHIFT_NODE_SD3, {"shift": 101.0}, "above 0"),+ (SHIFT_NODE_SD3, {"shift": float("nan")}, "above 0"),+ (SHIFT_NODE_FLUX, {"shift": 3.0}, "base / max"),+ (SHIFT_NODE_FLUX, {"max_shift": -1.0}, "between 0"),+ ],+)+def test_invalid_requests_are_refused(node, kwargs, needle):+ with pytest.raises(ValueError, match = needle):+ requested_flow_shift(node, width = 1024, height = 1024, **kwargs)+++def _flow_euler(**config):+ diffusers = pytest.importorskip("diffusers")+ return diffusers.FlowMatchEulerDiscreteScheduler(**config)+++ "config",+ [+ dict(shift = 3.0, use_dynamic_shifting = True, base_shift = 0.5, max_shift = 1.15),+ dict(+ shift = 1.0, use_dynamic_shifting = True, base_shift = 0.5, max_shift = 0.9, shift_terminal = 0.02+ ),+ dict(shift = 6.0, use_dynamic_shifting = False),+ ],+)[email protected]("shift", [1.0, 3.1, 6.0])+def test_built_schedule_is_comfyui_time_snr_shift(config, shift):+ import numpy as np++ resident = _flow_euler(**config)+ built = scheduler_with_flow_shift(resident, shift)+ unchanged = not config["use_dynamic_shifting"] and shift == config["shift"]+ assert (built is resident) == unchanged+ assert resident.config.get("shift") == config["shift"] # the loaded scheduler is not touched+ steps = 8+ t = np.linspace(1.0, 1.0 / steps, steps)+ # The pipelines pass this grid (FluxPipeline / QwenImagePipeline); mu is ignored once dynamic shifting is off.+ built.set_timesteps(sigmas = t, mu = 0.123, device = "cpu")+ got = built.sigmas.double().numpy()[:-1]+ want = np.array([_comfy_time_snr_shift(shift, x) for x in t])+ assert np.abs(got - want).max() < 1e-6++[email protected]("hi, lo", [(1.15, 0.5), (3.0, 0.5)])+def test_flux_node_schedule_is_comfyui_flux_time_shift(hi, lo):+ import numpy as np++ resident = _flow_euler(shift = 3.0, use_dynamic_shifting = True, base_shift = 0.5, max_shift = 1.15)+ shift = requested_flow_shift(+ SHIFT_NODE_FLUX, base_shift = lo, max_shift = hi, width = 768, height = 1024+ )+ built = scheduler_with_flow_shift(resident, shift)+ steps = 6+ t = np.linspace(1.0, 1.0 / steps, steps)+ built.set_timesteps(sigmas = t, mu = 9.0, device = "cpu")+ mu = _comfy_model_sampling_flux_shift(hi, lo, 768, 1024)+ want = np.array([_comfy_flux_time_shift(mu, 1.0, x) for x in t])+ assert np.abs(built.sigmas.double().numpy()[:-1] - want).max() < 1e-6+++def test_flow_shift_schedulers_take_flow_shift():+ """HiDream ships UniPC with ``flow_shift``: the override sets that key."""+ diffusers = pytest.importorskip("diffusers")+ resident = diffusers.UniPCMultistepScheduler(+ prediction_type = "flow_prediction", use_flow_sigmas = True, flow_shift = 3.0+ )+ built = scheduler_with_flow_shift(resident, 6.0)+ assert built.config.flow_shift == 6.0 and resident.config.flow_shift == 3.0+++def test_scheduler_without_flow_shift_is_refused():+ diffusers = pytest.importorskip("diffusers")+ with pytest.raises(ValueError, match = "no flow shift"):+ scheduler_with_flow_shift(diffusers.EulerDiscreteScheduler(), 3.0)+++def test_shift_control_reports_the_node_and_defaults():+ static = _flow_euler(shift = 3.0, use_dynamic_shifting = False)+ dynamic = _flow_euler(shift = 1.0, use_dynamic_shifting = True)+ assert shift_control(SHIFT_NODE_SD3, static) == {"node": SHIFT_NODE_SD3, "shift": 3.0}+ assert shift_control(SHIFT_NODE_SD3, dynamic) == {"node": SHIFT_NODE_SD3, "shift": None}+ assert shift_control(SHIFT_NODE_FLUX, dynamic) == {+ "node": SHIFT_NODE_FLUX,+ "base_shift": 0.5,+ "max_shift": 1.15,+ }+ assert shift_control(None, static) is None+ diffusers = pytest.importorskip("diffusers")+ assert shift_control(SHIFT_NODE_SD3, diffusers.EulerDiscreteScheduler()) is None+++def test_route_refuses_shift_on_an_engine_without_it():+ from types import SimpleNamespace++ from routes.inference import _shift_kwargs++ def native_generate(*, prompt, sampler = None):+ return None++ def diffusers_generate(+ *,+ prompt,+ shift = None,+ base_shift = None,+ max_shift = None,+ ):+ return None++ unset = SimpleNamespace(shift = None, base_shift = None, max_shift = None)+ assert _shift_kwargs(native_generate, unset) == {}+ assert _shift_kwargs(diffusers_generate, unset) == {}+ req = SimpleNamespace(shift = 3.0, base_shift = None, max_shift = None)+ assert _shift_kwargs(diffusers_generate, req) == {+ "shift": 3.0,+ "base_shift": None,+ "max_shift": None,+ }+ with pytest.raises(ValueError, match = "diffusers engine"):+ _shift_kwargs(native_generate, req)+++def test_request_model_bounds():+ from pydantic import ValidationError++ from models.inference import DiffusionGenerateRequest++ assert DiffusionGenerateRequest(prompt = "x").shift is None+ assert DiffusionGenerateRequest(prompt = "x", shift = 3.1).shift == 3.1+ for bad in ({"shift": 0.0}, {"shift": 101.0}, {"max_shift": -0.1}, {"base_shift": 200.0}):+ with pytest.raises(ValidationError):+ DiffusionGenerateRequest(prompt = "x", **bad)+++def test_flux_node_defaults_to_the_resolved_fixed_mu():+ """A load that resolved ComfyUI's fixed mu (Kontext 1.15, Qwen-Image-2.1 0.69) reports base = max = mu, so+ a request that leaves one field out starts from it, and sending the defaults back changes nothing."""+ from core.inference.diffusion_flow_shift import flux_mu_shift++ resolved = _flow_euler(shift = flux_mu_shift(0.69), use_dynamic_shifting = False)+ control = shift_control(SHIFT_NODE_FLUX, resolved)+ assert control["base_shift"] == pytest.approx(0.69) and control["max_shift"] == pytest.approx(+ 0.69+ )+ same = requested_flow_shift(+ SHIFT_NODE_FLUX,+ base_shift = control["base_shift"],+ max_shift = control["max_shift"],+ width = 1344,+ height = 768,+ )+ assert scheduler_with_flow_shift(resolved, same) is resolved+ only_max = requested_flow_shift(+ SHIFT_NODE_FLUX, max_shift = 2.0, width = 1024, height = 1024, defaults = control+ )+ assert only_max == pytest.approx(+ math.exp(_comfy_model_sampling_flux_shift(2.0, 0.69, 1024, 1024))+ )+++def test_dynamic_flux_reports_its_config_line():+ krea2_like = _flow_euler(shift = 1.0, use_dynamic_shifting = True, base_shift = 0.5, max_shift = 1.15)+ assert shift_control(SHIFT_NODE_FLUX, krea2_like)["max_shift"] == 1.15+ qwen21_like = _flow_euler(shift = 1.0, use_dynamic_shifting = True, base_shift = 0.5, max_shift = 0.9)+ assert shift_control(SHIFT_NODE_FLUX, qwen21_like)["max_shift"] == 0.9+++def test_the_resolved_shift_is_not_rebuilt():+ """Sending the value the load resolved keeps the loaded scheduler object (no rebuild, identical render)."""+ resolved = _flow_euler(shift = 3.1, use_dynamic_shifting = False)+ assert scheduler_with_flow_shift(resolved, 3.1) is resolved+ assert scheduler_with_flow_shift(resolved, 3.2) is not resolved+++def test_parameters_text_carries_the_shift_the_run_used():+ """The A1111 parameters text names a requested static shift, so pasting it back restores Shift."""+ from core.inference import image_gallery as gallery++ meta = {+ "prompt": "a cat",+ "width": 64,+ "height": 64,+ "steps": 4,+ "guidance": 3.5,+ "seed": 1,+ "model": "m",+ }+ assert "Shift:" not in gallery._params_text(meta)+ assert ", Shift: 3.1" in gallery._params_text({**meta, "shift": 3.1})+ assert (+ "shift" not in gallery._params_text({**meta, "base_shift": 0.5, "max_shift": 1.15}).lower()+ )diff --git a/studio/frontend/src/features/images/api.ts b/studio/frontend/src/features/images/api.tsindex 9f4186b480b..1707af81986 100644--- a/studio/frontend/src/features/images/api.ts+++ b/studio/frontend/src/features/images/api.ts@@ -12,6 +12,7 @@ import {MemoryEstimateRefusalError,} from "./lib/memory-refusal";import type { PromptEnhancement } from "./lib/prompt-enhance";+import type { ShiftControl } from "./lib/shift-choice";// One Advanced control's resolved value and provenance, for the Advanced-panel badges. `value` is the engaged// value (null when off), `requested` is what the caller asked for (null = left to the backend), `source` is "auto"@@ -102,6 +103,10 @@ export interface DiffusionStatus {prompt_hint?: string | null;// "default" first; empty or absent = no choice (flow models, native / MLX engines, older backend).samplers?: DiffusionSamplerOption[];+ // The Shift override the loaded flow-matching model takes; null/absent = no Shift control.+ shift_control?: ShiftControl | null;+ // Textual-inversion embedding format the loaded model takes ("sdxl"); null/absent = no embeddings.+ textual_inversion?: string | null;// Per-Advanced-control provenance, keyed by control name. Present only when a model is loaded on a// backend that records it; absent on older backends.resolved?: Record<string, DiffusionResolvedControl> | null;@@ -264,6 +269,10 @@ export interface DiffusionGenerateRequest {controlnet?: ControlNetSpecInput;sampler?: string;schedule_type?: string;+ // `shift` for "sd3", `base_shift` / `max_shift` for "flux"; omitted = the model's own schedule.+ shift?: number;+ base_shift?: number;+ max_shift?: number;// Rewrite the prompt with the family's prompt upsampler first. null/absent = the family default. Sent only for a// family that supports it.enhance_prompt?: boolean | null;@@ -355,6 +364,10 @@ export interface GalleryImage {// null = the model's shipped scheduler; schedule_type is the resolved schedule.sampler?: string | null;schedule_type?: string | null;+ shift?: number | null;+ base_shift?: number | null;+ max_shift?: number | null;+ embeddings?: string[] | null;// The caption the prompt upsampler wrote and the model actually ran; `prompt` stays the user's text.enhanced_prompt?: string | null;prompt_enhancement?: PromptEnhancement | null;@@ -373,7 +386,7 @@ export interface DiffusionGenerateResponse {images: GalleryImage[];}-async function parseJson<T>(response: Response): Promise<T> {+export async function parseJson<T>(response: Response): Promise<T> {if (!response.ok) {throw new Error(await readFastApiError(response));}diff --git a/studio/frontend/src/features/images/embeddings-api.ts b/studio/frontend/src/features/images/embeddings-api.tsnew file mode 100644index 00000000000..b1f2c7b7784--- /dev/null+++ b/studio/frontend/src/features/images/embeddings-api.ts@@ -0,0 +1,24 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// Kept out of api.ts, which the eager chat chunk carries: only the Images page lists embeddings.+import { authFetch } from "@/features/auth";+import { parseJson } from "./api";++// A textual-inversion embedding file (from GET /api/models/diffusion-embeddings); `trigger` is the file stem.+export interface DiffusionEmbeddingInfo {+ id: string;+ trigger: string;+ kind: string | null;+ vectors: number;+ size_bytes: number;+}++export async function listDiffusionEmbeddings(+ family: string,+): Promise<{ embeddings: DiffusionEmbeddingInfo[]; embeddings_dir?: string }> {+ const data = await parseJson<{ embeddings: DiffusionEmbeddingInfo[]; embeddings_dir?: string }>(+ await authFetch(`/api/models/diffusion-embeddings?family=${encodeURIComponent(family)}`),+ );+ return { embeddings: data.embeddings ?? [], embeddings_dir: data.embeddings_dir };+}diff --git a/studio/frontend/src/features/images/images-page.tsx b/studio/frontend/src/features/images/images-page.tsxindex 31b9b9100d8..79f84f8ab12 100644--- a/studio/frontend/src/features/images/images-page.tsx+++ b/studio/frontend/src/features/images/images-page.tsx@@ -319,6 +319,7 @@ import {type ImportedRecipe,importedRecipe,importNotApplied,+ textualInversionLabel,isGenerationParametersPaste,loraTagNames,matchLoraTagsForImport,@@ -355,6 +356,17 @@ import {SCHEDULE_LABELS,withSamplerChoice,} from "./lib/sampler-choice";+import {+ EMPTY_SHIFT,+ shiftFromRecipe,+ shiftNotApplied,+ shiftRequestFields,+ splitInstalledEmbeddings,+ type ShiftValue,+ withEmbeddingTrigger,+} from "./lib/shift-choice";+import { listDiffusionEmbeddings } from "./embeddings-api";+import { EmbeddingsField, ShiftField } from "./shift-embeddings-controls";import {enhancePromptRequestField,enhanceRestoreFor,@@ -2217,6 +2229,21 @@ export function ImagesPage({},[samplerChoice, samplerModelKey, samplerOptions],);+ const shiftControl = !external && status?.loaded ? (status.shift_control ?? null) : null;+ const [shiftState, setShiftState] = useState<{ key: string; value: ShiftValue }>({+ key: "",+ value: EMPTY_SHIFT,+ });+ const shiftValue = shiftState.key === samplerModelKey ? shiftState.value : EMPTY_SHIFT;+ const setShiftValue = useCallback(+ (value: ShiftValue) => setShiftState({ key: samplerModelKey, value }),+ [samplerModelKey],+ );+ const embeddingsEnabled = !external && status?.loaded === true && !!status.textual_inversion;+ const insertEmbedding = useCallback((trigger: string, target: "prompt" | "negative") => {+ if (target === "negative") setNegativePrompt((p) => withEmbeddingTrigger(p, trigger));+ else setPrompt((p) => withEmbeddingTrigger(p, trigger));+ }, [setPrompt]);// Enhance prompt (FLUX.3's prompt upsampler): stored per family, defaulting to the family's own default. A// restore or "Use as prompt" sets a one-off override that the next toggle or family change drops.const upsamplerCapability = promptUpsamplerCapability(status, Boolean(external));@@ -2778,6 +2805,7 @@ export function ImagesPage({setHeight(restored.height);// The batch shared one base seed, so a batch_index>0 image only reproduces by replaying the whole batch.setBatchSize(image.batch_size ?? 1);+ setShiftValue(shiftFromRecipe(image));const m = matchAspect(restored.width, restored.height);setAspect(m.key);setPortrait(m.portrait);@@ -2842,7 +2870,7 @@ export function ImagesPage({} else {toast.success("Settings restored to inputs", rescaled);}- }, [changeSamplerChoice, samplerOptions.length, setEnhanceOverride, setPromptFor, setWorkflow, sizeLimits]);+ }, [changeSamplerChoice, samplerOptions.length, setEnhanceOverride, setPromptFor, setShiftValue, setWorkflow, sizeLimits]);// Like dropping an image on ComfyUI: Unsloth's recipe first, else AUTOMATIC1111 / ComfyUI metadata.const importFileRef = useRef<HTMLInputElement>(null);@@ -2894,6 +2922,21 @@ export function ImagesPage({notes: loraTagNames(recipe.lora_tags).map((n) => `LoRA ${n} (model changed during import)`),};}+ // TI hashes: the prompt names each embedding, so on a model that takes them an installed one applies.+ const tiNames = Array.isArray(recipe.textual_inversions)+ ? recipe.textual_inversions.filter((x): x is string => typeof x === "string")+ : [];+ let tiMatch: { installed: string[]; missing: string[] } | null = null;+ if (tiNames.length > 0 && embeddingsEnabled && status?.family) {+ const triggers = await listDiffusionEmbeddings(status.family).then(+ (r) => r.embeddings.map((e) => e.trigger),+ () => null,+ );+ if (seq !== importSeq.current) return false;+ // A list fetched for a model that is no longer loaded says nothing: the names are reported plainly.+ if (triggers && loadedModelKey(statusRef.current) === modelBefore)+ tiMatch = splitInstalledEmbeddings(tiNames, triggers);+ }const merged =tagged.loras.length > 0? { ...recipe, loras: [...(Array.isArray(recipe.loras) ? recipe.loras : []), ...tagged.loras] }@@ -2942,8 +2985,22 @@ export function ImagesPage({...tagged.unmatched.map((name) => `LoRA ${name} (not installed)`),...lorasOverLimit(merged).map((id) => `LoRA ${id} (over the ${MAX_LORAS} adapter limit)`),...tagged.notes,+ ...shiftNotApplied(recipe, shiftControl),...unknownRecipeKeys(recipe),];+ if (tiMatch) {+ const { installed, missing } = tiMatch;+ const resolved = new Map<string, string | null>([+ ...installed.map((n): [string, null] => [textualInversionLabel(n), null]),+ ...missing.map((n): [string, string] => [textualInversionLabel(n), `${textualInversionLabel(n)} (not installed)`]),+ ]);+ const kept = notApplied.flatMap((item) => {+ if (!resolved.has(item)) return [item];+ const label = resolved.get(item);+ return label ? [label] : [];+ });+ notApplied.splice(0, notApplied.length, ...kept);+ }const baked = Array.isArray(recipe.baked_loras)? recipe.baked_loras.filter((x): x is string => typeof x === "string"): [];@@ -2992,7 +3049,7 @@ export function ImagesPage({}return true;},- [restoreSettings, width, height, steps, guidance, status, samplerOptions, availableLoras, loraCapable, loraCatalogFamily],+ [restoreSettings, width, height, steps, guidance, status, samplerOptions, availableLoras, loraCapable, loraCatalogFamily, embeddingsEnabled, shiftControl],);const importSettingsFromFile = useCallback(async (file: File | Uint8Array) => {@@ -5249,6 +5306,7 @@ export function ImagesPage({allow_oversized: allowOversizedSent ? true : undefined,live_preview: livePreview,...samplerRequestFields(samplerOptions, samplerChoice),+ ...shiftRequestFields(shiftControl, shiftValue),...enhancePromptRequestField(upsamplerCapability, enhanceOn),...condFields,// Drop empty and zero-weight rows and trim hand-typed repo ids, so the recipe records only@@ -5346,7 +5404,7 @@ export function ImagesPage({setGenStep(null);setStopping(false);}- }, [external, allowOversized, livePreview, enhanceOn, upsamplerCapability, prompt, negativePrompt, negativeEngaged, restoredSampler, samplerModelKey, samplerOptions, samplerChoice, width, height, steps, effectiveGuidance, seed, batchSize, count, workflow, initImage, maskImage, strength, maskBlur, inpaintOnlyMasked, inpaintPadding, status?.supports_inpaint_only_masked, extendPct, extendSides, upscaleFactor, upscaleStrength, referenceImages, loras, loraCapable, controlnetCapable, controlnetId, controlImage, controlType, controlStrength, ensureSrc, loadGallery, refreshStatus, unifiedEdit, multiImageEdit, localizedMode, localizedLayer, maxExtras, referenceResolution, conditioning, editSize, editSizing, sizeLimits]);+ }, [external, allowOversized, livePreview, enhanceOn, upsamplerCapability, prompt, negativePrompt, negativeEngaged, restoredSampler, samplerModelKey, samplerOptions, samplerChoice, shiftControl, shiftValue, width, height, steps, effectiveGuidance, seed, batchSize, count, workflow, initImage, maskImage, strength, maskBlur, inpaintOnlyMasked, inpaintPadding, status?.supports_inpaint_only_masked, extendPct, extendSides, upscaleFactor, upscaleStrength, referenceImages, loras, loraCapable, controlnetCapable, controlnetId, controlImage, controlType, controlStrength, ensureSrc, loadGallery, refreshStatus, unifiedEdit, multiImageEdit, localizedMode, localizedLayer, maxExtras, referenceResolution, conditioning, editSize, editSizing, sizeLimits]);// Stop the in-flight generation. Latch FIRST, so a multi-run request stops even if the POST// races the run that is already finishing.@@ -6697,6 +6755,12 @@ export function ImagesPage({</div></Field>)}+ <ShiftField control={shiftControl} value={shiftValue} onChange={setShiftValue} />+ <EmbeddingsField+ family={status?.family}+ enabled={embeddingsEnabled}+ onInsert={insertEmbedding}+ />{!external && (<><Fielddiff --git a/studio/frontend/src/features/images/lib/image-metadata.ts b/studio/frontend/src/features/images/lib/image-metadata.tsindex 2315765f925..4163ae41c58 100644--- a/studio/frontend/src/features/images/lib/image-metadata.ts+++ b/studio/frontend/src/features/images/lib/image-metadata.ts@@ -784,13 +784,16 @@ export function parseA1111Parameters(text: string): ImportedRecipe | null {n !== undefined)out.inpaint_padding = n;- // `TI hashes: "name: hash, ..."` lists the embeddings the prompt used; Studio loads none.+ // `TI hashes`: the prompt names each embedding, so an installed one applies; importNotApplied names the rest.else if (key === "ti hashes") {+ const names: string[] = [];for (const item of value.split(",")) {const name = item.slice(0, item.lastIndexOf(":") > 0 ? item.lastIndexOf(":") : undefined).trim();- if (name) notApplied.push(`textual inversion ${name}`);+ if (name) names.push(name);}+ if (names.length > 0) out.textual_inversions = names;}+ else if (key === "shift" && n !== undefined) out.shift = n;// A1111 writes Emphasis only when it is not the default; "Original" is what Studio's prompt encoding does.else if (key === "emphasis") {if (value && value.toLowerCase() !== "original")@@ -900,6 +903,9 @@ export interface RestorableRecipe {model_kind?: string;sampler?: string;schedule_type?: string;+ shift?: number;+ base_shift?: number;+ max_shift?: number;// The prompt upsampler's caption and outcome; restore decides the Enhance prompt toggle from them.enhanced_prompt?: string | null;prompt_enhancement?: "applied" | "off" | "unavailable" | "failed" | null;@@ -927,6 +933,9 @@ export const APPLIED_RECIPE_KEYS = ["localized_edit","sampler","schedule_type",+ "shift",+ "base_shift",+ "max_shift","enhanced_prompt","prompt_enhancement","mask_blur",@@ -955,6 +964,8 @@ export const INFO_RECIPE_KEYS = ["engine","generator","schema_version",+ // The prompt names the embeddings it used, so restoring the prompt is what applies them.+ "embeddings",] as const;/** Matches RECIPE_SCHEMA_VERSION in core/inference/image_gallery.py. */@@ -970,6 +981,7 @@ export const PENDING_RECIPE_KEYS = ["lora_tags","prompt_emphasis","prompt_escaped_brackets",+ "textual_inversions","parameters_not_applied",] as const;@@ -1063,6 +1075,10 @@ export function importNotApplied(.map(([k, v]) => `${k}: ${String(v)}`);if (parts.length > 0) out.push(`hires fix (${parts.join(", ")})`);}+ if (Array.isArray(r.textual_inversions)) {+ for (const name of r.textual_inversions)+ if (typeof name === "string" && name) out.push(textualInversionLabel(name));+ }if (Array.isArray(r.parameters_not_applied)) {for (const item of r.parameters_not_applied)if (typeof item === "string") out.push(item);@@ -1070,6 +1086,11 @@ export function importNotApplied(return out;}+/** How importNotApplied names a TI hashes embedding; the page re-labels the ones it matched. */+export function textualInversionLabel(name: string): string {+ return `textual inversion ${name}`;+}+export function newerRecipeSchema(r: ImportedRecipe): boolean {return (typeof r.schema_version === "number" &&@@ -1159,6 +1180,13 @@ export function recipeSettings(model_kind: r.model_kind === "connection" ? "connection" : undefined,sampler: str(r.sampler),schedule_type: str(r.schedule_type),+ // The request's bounds: shift in (0, 100], base / max shift in [0, 100].+ shift: (() => {+ const n = num(r.shift);+ return n !== undefined && n > 0 && n <= 100 ? n : undefined;+ })(),+ base_shift: clamp(num(r.base_shift), 0, 100),+ max_shift: clamp(num(r.max_shift), 0, 100),enhanced_prompt: str(r.enhanced_prompt) ?? null,prompt_enhancement:r.prompt_enhancement === "applied" ||diff --git a/studio/frontend/src/features/images/lib/shift-choice.ts b/studio/frontend/src/features/images/lib/shift-choice.tsnew file mode 100644index 00000000000..1fb92df6be9--- /dev/null+++ b/studio/frontend/src/features/images/lib/shift-choice.ts@@ -0,0 +1,103 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++/** Per-generation Shift override; backend: core/inference/diffusion_flow_shift.py. "sd3" takes one static shift+ * (ComfyUI ModelSamplingSD3 / AuraFlow), "flux" takes base / max shift (ComfyUI ModelSamplingFlux). */+export interface ShiftControl {+ node: "sd3" | "flux";+ // sd3: the loaded value, null when the model's schedule depends on resolution.+ shift?: number | null;+ base_shift?: number;+ max_shift?: number;+}++export interface ShiftValue {+ shift: string;+ baseShift: string;+ maxShift: string;+}++export const EMPTY_SHIFT: ShiftValue = { shift: "", baseShift: "", maxShift: "" };++export const SHIFT_MAX = 100;++function parse(text: string, min: number, exclusiveMin: boolean): number | undefined {+ const trimmed = text.trim();+ if (!trimmed) return undefined;+ const value = Number(trimmed);+ if (!Number.isFinite(value) || value > SHIFT_MAX) return undefined;+ if (exclusiveMin ? value <= min : value < min) return undefined;+ return value;+}++/** Request fields for the typed values; nothing when the model offers no Shift or the boxes are empty/invalid. */+export function shiftRequestFields(+ control: ShiftControl | null | undefined,+ value: ShiftValue,+): { shift?: number; base_shift?: number; max_shift?: number } {+ if (!control) return {};+ if (control.node === "sd3") {+ const shift = parse(value.shift, 0, true);+ return shift === undefined ? {} : { shift };+ }+ const base = parse(value.baseShift, 0, false);+ const max = parse(value.maxShift, 0, false);+ return {+ ...(base !== undefined ? { base_shift: base } : {}),+ ...(max !== undefined ? { max_shift: max } : {}),+ };+}++export function shiftFieldInvalid(text: string, exclusiveMin: boolean): boolean {+ return text.trim() !== "" && parse(text, 0, exclusiveMin) === undefined;+}++export function withEmbeddingTrigger(prompt: string, trigger: string): string {+ const trimmed = prompt.trimEnd();+ if (!trimmed) return trigger;+ return /[,(]$/.test(trimmed) ? `${trimmed} ${trigger}` : `${trimmed}, ${trigger}`;+}++/** The Shift boxes a gallery record or imported recipe restores; absent values leave the model's schedule. */+export function shiftFromRecipe(r: {+ shift?: number | null;+ base_shift?: number | null;+ max_shift?: number | null;+}): ShiftValue {+ const text = (n: number | null | undefined) => (typeof n === "number" && Number.isFinite(n) ? String(n) : "");+ return { shift: text(r.shift), baseShift: text(r.base_shift), maxShift: text(r.max_shift) };+}++/** Imported Shift values the loaded model cannot take (no Shift control, or the other ComfyUI node's fields). */+export function shiftNotApplied(+ r: Readonly<Record<string, unknown>>,+ control: ShiftControl | null | undefined,+): string[] {+ const out: string[] = [];+ const num = (v: unknown) => (typeof v === "number" && Number.isFinite(v) ? v : undefined);+ const shift = num(r.shift);+ const base = num(r.base_shift);+ const max = num(r.max_shift);+ // The request's bounds: shift in (0, SHIFT_MAX], base / max shift in [0, SHIFT_MAX].+ const name = (label: string, v: number, ok: boolean) =>+ out.push(ok ? `${label} ${v}` : `${label} ${v} (out of range)`);+ if (shift !== undefined && !(control?.node === "sd3" && shift > 0 && shift <= SHIFT_MAX))+ name("shift", shift, control?.node !== "sd3");+ if (base !== undefined && !(control?.node === "flux" && base >= 0 && base <= SHIFT_MAX))+ name("base shift", base, control?.node !== "flux");+ if (max !== undefined && !(control?.node === "flux" && max >= 0 && max <= SHIFT_MAX))+ name("max shift", max, control?.node !== "flux");+ return out;+}++/** Split imported textual-inversion names by whether an installed embedding (its trigger, any case) matches. */+export function splitInstalledEmbeddings(+ names: readonly string[],+ triggers: readonly string[],+): { installed: string[]; missing: string[] } {+ const known = new Set(triggers.map((t) => t.toLowerCase()));+ const installed: string[] = [];+ const missing: string[] = [];+ for (const name of names) (known.has(name.toLowerCase()) ? installed : missing).push(name);+ return { installed, missing };+}diff --git a/studio/frontend/src/features/images/shift-embeddings-controls.tsx b/studio/frontend/src/features/images/shift-embeddings-controls.tsxnew file mode 100644index 00000000000..56dcb76cb8f--- /dev/null+++ b/studio/frontend/src/features/images/shift-embeddings-controls.tsx@@ -0,0 +1,187 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import { useEffect, useState, type ReactNode } from "react";+import { Button } from "@/components/ui/button";+import { InfoHint } from "@/components/ui/info-hint";+import { Input } from "@/components/ui/input";+import { cn } from "@/lib/utils";+import { listDiffusionEmbeddings, type DiffusionEmbeddingInfo } from "./embeddings-api";+import { shiftFieldInvalid, type ShiftControl, type ShiftValue } from "./lib/shift-choice";++// The resolved defaults can be e.g. ln(e^0.69); show them the way a user would type them.+function roundShift(value: number): number {+ return Number(value.toFixed(3));+}++function Label({ label, hint }: { label: string; hint: ReactNode }) {+ return (+ <div className="flex items-center gap-1">+ <label className="text-xs font-medium text-muted-foreground">{label}</label>+ <InfoHint>{hint}</InfoHint>+ </div>+ );+}++function NumberBox({+ ariaLabel,+ value,+ placeholder,+ exclusiveMin,+ onChange,+}: {+ ariaLabel: string;+ value: string;+ placeholder: string;+ exclusiveMin: boolean;+ onChange: (v: string) => void;+}) {+ return (+ <Input+ aria-label={ariaLabel}+ inputMode="decimal"+ className={cn("h-8 text-xs", shiftFieldInvalid(value, exclusiveMin) && "border-destructive")}+ placeholder={placeholder}+ value={value}+ onChange={(e) => onChange(e.target.value)}+ />+ );+}++/** The Shift override for flow-matching models; renders nothing when the loaded model offers none. */+export function ShiftField({+ control,+ value,+ onChange,+}: {+ control: ShiftControl | null | undefined;+ value: ShiftValue;+ onChange: (next: ShiftValue) => void;+}) {+ if (!control) return null;+ if (control.node === "sd3") {+ return (+ <div className="flex flex-col gap-1.5 pt-2">+ <Label+ label="Shift"+ hint="Moves denoising effort toward the early, composition-forming steps (higher) or the late, detail steps (lower). Same as ComfyUI's ModelSamplingSD3 / AuraFlow shift. Leave empty for the model's default."+ />+ <NumberBox+ ariaLabel="Shift"+ value={value.shift}+ placeholder={control.shift != null ? `Model default (${roundShift(control.shift)})` : "Model default"}+ exclusiveMin+ onChange={(shift) => onChange({ ...value, shift })}+ />+ </div>+ );+ }+ return (+ <div className="flex flex-col gap-1.5 pt-2">+ <Label+ label="Shift (base / max)"+ hint="Resolution-dependent shift, as ComfyUI's ModelSamplingFlux: base shift applies at 256 latent tokens, max shift at 4096 (1024x1024), interpolated for your size. Higher spends more steps on composition. Empty boxes keep the value Studio already uses for this model (shown in grey)."+ />+ <div className="flex items-center gap-2">+ <NumberBox+ ariaLabel="Base shift"+ value={value.baseShift}+ placeholder={`Base (${roundShift(control.base_shift ?? 0.5)})`}+ exclusiveMin={false}+ onChange={(baseShift) => onChange({ ...value, baseShift })}+ />+ <NumberBox+ ariaLabel="Max shift"+ value={value.maxShift}+ placeholder={`Max (${roundShift(control.max_shift ?? 1.15)})`}+ exclusiveMin={false}+ onChange={(maxShift) => onChange({ ...value, maxShift })}+ />+ </div>+ </div>+ );+}++/** Textual-inversion embeddings in the Studio embeddings folder; clicking one adds its trigger word. */+export function EmbeddingsField({+ family,+ enabled,+ onInsert,+}: {+ family: string | null | undefined;+ enabled: boolean;+ onInsert: (trigger: string, target: "prompt" | "negative") => void;+}) {+ // Keyed by family, so a list fetched for the previous model is never shown for this one.+ const [listing, setListing] = useState<{+ family: string;+ items: DiffusionEmbeddingInfo[];+ dir: string | null;+ } | null>(null);+ useEffect(() => {+ if (!enabled || !family) return;+ let cancelled = false;+ listDiffusionEmbeddings(family)+ .then((res) => {+ if (!cancelled) {+ setListing({ family, items: res.embeddings, dir: res.embeddings_dir ?? null });+ }+ })+ .catch(() => {+ if (!cancelled) setListing({ family, items: [], dir: null });+ });+ return () => {+ cancelled = true;+ };+ }, [enabled, family]);+ if (!enabled || !family) return null;+ const items = listing?.family === family ? listing.items : [];+ const dir = listing?.family === family ? listing.dir : null;+ return (+ <div className="flex flex-col gap-1.5 pt-2">+ <Label+ label="Embeddings"+ hint={+ <>+ Textual inversion embeddings (.safetensors) such as EasyNegative. Put files in{" "}+ {dir ? <code className="break-all">{dir}</code> : "the Studio embeddings folder"}; the file name is the+ trigger word, so writing it in the prompt or negative prompt uses the embedding.+ </>+ }+ />+ {items.length === 0 ? (+ <p className="text-xs text-muted-foreground">No embeddings for this model yet.</p>+ ) : (+ <div className="flex flex-wrap gap-1.5">+ {items.map((e) => (+ <div key={e.id} className="flex items-center rounded-md border text-xs">+ <span className="px-2 py-1" title={`${e.vectors} vector${e.vectors === 1 ? "" : "s"}`}>+ {e.trigger}+ </span>+ <Button+ type="button"+ variant="ghost"+ size="sm"+ className="h-6 px-1.5 text-xs"+ aria-label={`Add ${e.trigger} to the prompt`}+ onClick={() => onInsert(e.trigger, "prompt")}+ >+ + Prompt+ </Button>+ <Button+ type="button"+ variant="ghost"+ size="sm"+ className="h-6 px-1.5 text-xs"+ aria-label={`Add ${e.trigger} to the negative prompt`}+ onClick={() => onInsert(e.trigger, "negative")}+ >+ + Negative+ </Button>+ </div>+ ))}+ </div>+ )}+ </div>+ );+}diff --git a/studio/frontend/tests/images-shift-embeddings-import.test.ts b/studio/frontend/tests/images-shift-embeddings-import.test.tsnew file mode 100644index 00000000000..987a8cdc282--- /dev/null+++ b/studio/frontend/tests/images-shift-embeddings-import.test.ts@@ -0,0 +1,100 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import test from "node:test";++import {+ APPLIED_RECIPE_KEYS,+ importedRecipe,+ importNotApplied,+ INFO_RECIPE_KEYS,+ parseA1111Parameters,+ PENDING_RECIPE_KEYS,+ recipeSettings,+ unknownRecipeKeys,+} from "../src/features/images/lib/image-metadata.ts";+import {+ shiftFromRecipe,+ shiftNotApplied,+ shiftRequestFields,+ splitInstalledEmbeddings,+} from "../src/features/images/lib/shift-choice.ts";+import { readSrc } from "./helpers/kit.ts";++const DEFAULTS = { width: 1024, height: 1024, steps: 30, guidance: 4 };+const SD3 = { node: "sd3" as const, shift: 3 };+const FLUX = { node: "flux" as const, base_shift: 0.5, max_shift: 1.15 };++test("a pasted Shift restores into the Shift box and is sent on a model with the sd3 node", () => {+ const r = parseA1111Parameters("a cat\nSteps: 20, CFG scale: 4, Seed: 1, Size: 1024x1024, Shift: 3.1");+ assert.ok(r);+ assert.equal(r.shift, 3.1);+ const settings = recipeSettings(r, DEFAULTS);+ const value = shiftFromRecipe(settings);+ assert.deepEqual(value, { shift: "3.1", baseShift: "", maxShift: "" });+ assert.deepEqual(shiftRequestFields(SD3, value), { shift: 3.1 });+ assert.deepEqual(shiftNotApplied(r, SD3), []);+ // No Shift control, or the ModelSamplingFlux node: named, not dropped.+ assert.deepEqual(shiftNotApplied(r, null), ["shift 3.1"]);+ assert.deepEqual(shiftNotApplied(r, FLUX), ["shift 3.1"]);+ assert.deepEqual(importNotApplied(r, "sd3"), []);+});++test("a Studio recipe's base / max shift restore on a flux-node model; out-of-range values are named", () => {+ const recipe = importedRecipe({+ recipe: JSON.stringify({+ prompt: "p",+ width: 1024,+ height: 1024,+ steps: 20,+ guidance: 3.5,+ seed: 5,+ base_shift: 0.6,+ max_shift: 1.4,+ embeddings: ["easynegative"],+ schema_version: 1,+ }),+ });+ assert.ok(recipe);+ assert.deepEqual(unknownRecipeKeys(recipe), []);+ const settings = recipeSettings(recipe, DEFAULTS);+ assert.deepEqual(shiftRequestFields(FLUX, shiftFromRecipe(settings)), { base_shift: 0.6, max_shift: 1.4 });+ assert.deepEqual(shiftNotApplied(recipe, FLUX), []);+ assert.deepEqual(shiftNotApplied(recipe, SD3), ["base shift 0.6", "max shift 1.4"]);+ assert.deepEqual(shiftNotApplied({ shift: 0 }, SD3), ["shift 0 (out of range)"]);+ assert.equal(recipeSettings({ prompt: "p", shift: 0 }, DEFAULTS).shift, undefined);+ assert.equal(recipeSettings({ prompt: "p", shift: 250 }, DEFAULTS).shift, undefined);+ assert.equal(recipeSettings({ prompt: "p", max_shift: 250 }, DEFAULTS).max_shift, 100);+ for (const key of ["shift", "base_shift", "max_shift"]) assert.ok((APPLIED_RECIPE_KEYS as readonly string[]).includes(key));+ assert.ok((INFO_RECIPE_KEYS as readonly string[]).includes("embeddings"));+ assert.ok((PENDING_RECIPE_KEYS as readonly string[]).includes("textual_inversions"));+});++test("TI hashes names stay reported unless an installed embedding matches", () => {+ const r = parseA1111Parameters(+ 'a cat, EasyNegative\nSteps: 20, Seed: 1, TI hashes: "easynegative: c74b4e810b03, badhandv4: 5e40d722fc3d"',+ );+ assert.ok(r);+ assert.deepEqual(r.textual_inversions, ["easynegative", "badhandv4"]);+ // Without a model that takes embeddings (or before the lookup) every name is reported, as #14508 did.+ assert.deepEqual(importNotApplied(r, "sdxl"), [+ "textual inversion easynegative",+ "textual inversion badhandv4",+ ]);+ assert.deepEqual(splitInstalledEmbeddings(r.textual_inversions as string[], ["EasyNegative", "other"]), {+ installed: ["easynegative"],+ missing: ["badhandv4"],+ });+ const page = readSrc("features/images/images-page.tsx");+ assert.match(page, /const resolved = new Map<string, string \| null>\(\[\n\s+\.\.\.installed\.map\(\(n\): \[string, null\] => \[textualInversionLabel\(n\), null\]\),/);+ assert.match(page, /`\$\{textualInversionLabel\(n\)\} \(not installed\)`/);+ // Superseded or model-switched imports drop the embedding match, like the LoRA lookup.+ assert.match(+ page,+ /if \(seq !== importSeq\.current\) return false;\n[^\n]*\n\s+if \(triggers && loadedModelKey\(statusRef\.current\) === modelBefore\)\n\s+tiMatch = splitInstalledEmbeddings\(tiNames, triggers\);/,+ );+ assert.match(page, /if \(tiNames\.length > 0 && embeddingsEnabled && status\?\.family\)/);+ assert.match(page, /\.\.\.shiftNotApplied\(recipe, shiftControl\),/);+ assert.match(page, /setBatchSize\(image\.batch_size \?\? 1\);\n\s+setShiftValue\(shiftFromRecipe\(image\)\);/);+});