Overwrite foreign steps/cfg in sparse patches when the user did not ask for params, and skip the cold-load path when Ollama already has the chat model resident. Co-authored-by: Cursor <cursoragent@cursor.com>
115 lines
3.6 KiB
JavaScript
115 lines
3.6 KiB
JavaScript
/**
|
||
* Krea turbo/raw profile detection + Exact param alignment helpers (pure).
|
||
* Prevents labeling ambiguous checkpoints as turbo while Swarm still has SD-like 20/7.
|
||
*/
|
||
|
||
export function numClose(a, b, eps = 0.051) {
|
||
const x = Number(a);
|
||
const y = Number(b);
|
||
if (!Number.isFinite(x) || !Number.isFinite(y)) {
|
||
return false;
|
||
}
|
||
return Math.abs(x - y) <= eps;
|
||
}
|
||
|
||
/**
|
||
* Detect turbo|raw from checkpoint name/title.
|
||
* Names without turbo/raw (e.g. realismByStableYogi) must NOT invent "turbo".
|
||
* Note: INT8Turbo has no word-boundary before "turbo" — use substring match.
|
||
*/
|
||
export function detectKreaProfileName(modelBlob, exactProfile) {
|
||
const blob = String(modelBlob || '').toLowerCase();
|
||
const hasTurbo = /turbo/.test(blob);
|
||
const hasRaw = /\braw\b|_raw\b|-raw\b|\/raw\b/.test(blob);
|
||
if (hasTurbo) {
|
||
return 'turbo';
|
||
}
|
||
if (hasRaw) {
|
||
return 'raw';
|
||
}
|
||
// Unlabeled ckpt — Exact generation.profile alone must not invent turbo.
|
||
void exactProfile;
|
||
return 'raw';
|
||
}
|
||
|
||
/** Profile-only defaults (no session overlay). */
|
||
export function profileParamDefaults(profiles, profileName, generation = {}) {
|
||
const profile = String(profileName || 'raw');
|
||
const fromProfile = profiles && typeof profiles[profile] === 'object' ? profiles[profile] : {};
|
||
const gen = generation && typeof generation === 'object' ? generation : {};
|
||
return {
|
||
profile,
|
||
steps: fromProfile.steps ?? gen.steps ?? null,
|
||
cfg: fromProfile.cfg ?? gen.cfg ?? null,
|
||
sigma_shift: fromProfile.sigma_shift ?? gen.sigma_shift ?? null,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* Whether live Swarm numbers already match the Exact profile (steps/cfg/σ).
|
||
*/
|
||
export function liveMatchesExactProfile(live, defaults) {
|
||
if (!defaults || defaults.steps == null || defaults.cfg == null) {
|
||
return true;
|
||
}
|
||
const stepsOk = live?.steps == null || numClose(live.steps, defaults.steps, 0.5);
|
||
const cfgOk = live?.cfg == null || numClose(live.cfg, defaults.cfg);
|
||
const sigmaOk = defaults.sigma_shift == null
|
||
|| live?.sigma_shift == null
|
||
|| numClose(live.sigma_shift, defaults.sigma_shift);
|
||
return stepsOk && cfgOk && sigmaOk;
|
||
}
|
||
|
||
/**
|
||
* Keys that should be forced from Exact onto live before Generate.
|
||
* - Always rewrite live when it disagrees with Exact profile defaults.
|
||
* - Skip only when the patch sets a *different* intentional value, or
|
||
* sessionExact holds an override and userParamIntent is true.
|
||
* - Stale sessionExact leftovers (SD 20/7) must NOT block Exact.
|
||
*/
|
||
export function exactKeysToForce(live, defaults, {
|
||
patch = null,
|
||
sessionExact = null,
|
||
userParamIntent = false,
|
||
} = {}) {
|
||
if (!defaults) {
|
||
return [];
|
||
}
|
||
const out = [];
|
||
for (const key of ['steps', 'cfg', 'sigma_shift']) {
|
||
const want = defaults[key];
|
||
if (want == null) {
|
||
continue;
|
||
}
|
||
const eps = key === 'steps' ? 0.5 : 0.051;
|
||
// Honor intentional non-Exact patch only when the user asked for params.
|
||
// Sparse LLM echoes of live 20/7 must not block Exact (turbo/raw label -> numbers).
|
||
if (
|
||
userParamIntent
|
||
&& patch
|
||
&& patch[key] != null
|
||
&& !numClose(patch[key], want, eps)
|
||
) {
|
||
continue;
|
||
}
|
||
if (userParamIntent && sessionExact && sessionExact[key] != null) {
|
||
continue;
|
||
}
|
||
const have = live?.[key];
|
||
if (have == null || !numClose(have, want, eps)) {
|
||
out.push(key);
|
||
}
|
||
}
|
||
return out;
|
||
}
|
||
|
||
export function attachKreaProfile(SA) {
|
||
SA.kreaProfile = {
|
||
numClose,
|
||
detectKreaProfileName,
|
||
profileParamDefaults,
|
||
liveMatchesExactProfile,
|
||
exactKeysToForce,
|
||
};
|
||
}
|