Skip to content

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.

Studio: let a model loaded at a set context reload on Auto (#13555)

Michael Han2890f2c376c7
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
index 4f17c5e9d21..1619ee56311 100644
--- a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
+++ b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
@@ -1652,6 +1652,7 @@ export function useChatModelRuntime() {
),
defaultMaxSeqLength: live.params.maxSeqLength || DEFAULT_MAX_SEQ_LENGTH,
presetSource: live.activePresetSource,
+ loadedContextPin: live.loadedCustomContextLength,
});
},
parallelSlots: managedFlags?.defaultParallelSlots || null,
@@ -2296,9 +2297,26 @@ export function useChatModelRuntime() {
const resetsPerModelSettings = Boolean(
currentCheckpoint && switchingModelOrVariant && !keepSpeculative,
);
+ // Pinning layers on the SAME model keeps the currently resolved context: with no explicit pin, a
+ // manual+pinned reload would send 0, which the backend's --fit off branch treats as the NATIVE
+ // context, far larger than the sheet shows when the load was fit-sized, a likely OOM.
+ // loadedContextLength is that resolved value; a model already at native reloads unchanged.
+ // Validation takes it too, so /validate and /load size the same context.
+ const fixedLayerContext = (
+ customContextLength: number | null,
+ gpuLayers: number,
+ ): number | null =>
+ isGguf &&
+ !switchingModelOrVariant &&
+ loadGpuMemoryMode === "manual" &&
+ gpuLayers >= 0 &&
+ customContextLength == null &&
+ (loadContextLength ?? 0) > 0
+ ? loadContextLength
+ : customContextLength;
const validateCustomContextLength = resetsPerModelSettings
? null
- : loadCustomContextLength;
+ : fixedLayerContext(loadCustomContextLength, loadGpuLayers);
const validateGpuIds = resetsPerModelSettings
? stagedGpuIds
: loadSelectedGpuIds;
@@ -2353,6 +2371,7 @@ export function useChatModelRuntime() {
DEFAULT_MAX_SEQ_LENGTH,
),
presetSource: loadActivePresetSource,
+ loadedContextPin: stateBeforeUnload.loadedCustomContextLength,
}),
);
const validation = await validateModel({
@@ -2674,20 +2693,10 @@ export function useChatModelRuntime() {
targetIsMlx,
pinnedMaxSeqLength,
);
- // Pinning layers on the SAME model keeps the currently resolved context: with no explicit pin, a
- // manual+pinned reload would send 0, which the backend's --fit off branch treats as the NATIVE
- // context, far larger than the sheet shows when the load was fit-sized, a likely OOM.
- // loadedContextLength is that resolved value; a model already at native reloads unchanged.
- if (
- isGguf &&
- !switchingModelOrVariant &&
- loadGpuMemoryMode === "manual" &&
- loadGpuLayers >= 0 &&
- loadCustomContextLength == null &&
- (loadContextLength ?? 0) > 0
- ) {
- loadCustomContextLength = loadContextLength;
- }
+ loadCustomContextLength = fixedLayerContext(
+ loadCustomContextLength,
+ loadGpuLayers,
+ );
const effectiveMaxSeqLength = resolveLoadMaxSeqLength({
modelId,
ggufVariant,
@@ -2704,6 +2713,7 @@ export function useChatModelRuntime() {
DEFAULT_MAX_SEQ_LENGTH,
),
presetSource: loadActivePresetSource,
+ loadedContextPin: stateBeforeUnload.loadedCustomContextLength,
});
const loadMaxSeqLength = resolveFitMaxSeqLength(
isGguf,
diff --git a/studio/frontend/src/features/chat/presets/preset-policy.ts b/studio/frontend/src/features/chat/presets/preset-policy.ts
index e9af04b1e19..c28a1f258a3 100644
--- a/studio/frontend/src/features/chat/presets/preset-policy.ts
+++ b/studio/frontend/src/features/chat/presets/preset-policy.ts
@@ -345,6 +345,7 @@ export function resolveLoadMaxSeqLength({
pinnedMaxSeqLength,
defaultMaxSeqLength,
presetSource,
+ loadedContextPin = null,
}: {
modelId: string;
ggufVariant?: string | null;
@@ -357,6 +358,9 @@ export function resolveLoadMaxSeqLength({
pinnedMaxSeqLength: number | null;
defaultMaxSeqLength: number;
presetSource: ChatPresetSource;
+ /** `loadedCustomContextLength`: the context the resident server was pinned to, or null when
+ * it was loaded on Auto. */
+ loadedContextPin?: number | null;
}): number {
const isDirectGgufFile = modelId.toLowerCase().endsWith(".gguf");
const isGgufLoad = isGguf === true || ggufVariant != null || isDirectGgufFile;
@@ -371,8 +375,10 @@ export function resolveLoadMaxSeqLength({
if (isGgufLoad && presetSource === "builtin-default") {
return 0;
}
+ // Keeping the resident size only holds an Auto window steady. When that size was a pin, Auto is
+ // a request to drop it, and resending it made the reload a no-op that kept the old context.
if (isReloadingCurrentGguf) {
- return loadedContextLength ?? 0;
+ return loadedContextPin != null ? 0 : (loadedContextLength ?? 0);
}
if (isGgufLoad) {
return 0;
diff --git a/studio/frontend/tests/context-pin-survives-load.test.ts b/studio/frontend/tests/context-pin-survives-load.test.ts
index 12c102ea855..281cff65b4b 100644
--- a/studio/frontend/tests/context-pin-survives-load.test.ts
+++ b/studio/frontend/tests/context-pin-survives-load.test.ts
@@ -87,7 +87,9 @@ function sentNCtx(
currentCheckpoint = MODEL,
// The configuration the bug was reported in.
presetSource = "builtin-default",
+ loadedPin = null,
}: {
+ loadedPin?: number | null;
mode?: Mode;
gpuLayers?: number;
residentCtx?: number;
@@ -112,6 +114,7 @@ function sentNCtx(
pinnedMaxSeqLength: null,
defaultMaxSeqLength: 4096,
presetSource,
+ loadedContextPin: loadedPin,
}),
);
}
@@ -274,6 +277,29 @@ test("a poll landing mid-load cannot plant the outgoing model's context", () =>
);
});
+test("choosing Auto after a pinned load asks the server to size itself again", () => {
+ // Resending the resident size for Auto made the resident check read the reload as a no-op,
+ // so a model loaded at a set context could never go back to Auto.
+ for (const presetSource of PRESET_SOURCES) {
+ assert.equal(
+ sentNCtx(null, { presetSource, residentCtx: 8192, loadedPin: 8192 }),
+ 0,
+ presetSource,
+ );
+ }
+ // A context the user sets is still sent as is over a pinned resident.
+ assert.equal(sentNCtx(16384, { presetSource: "modified", residentCtx: 8192, loadedPin: 8192 }), 16384);
+ // Another model ignores the resident pin: cross-model Auto is 0 already.
+ assert.equal(
+ sentNCtx(null, { presetSource: "modified", residentCtx: 8192, loadedPin: 8192, currentCheckpoint: OTHER_MODEL }),
+ 0,
+ );
+});
+
+test("each same-model resolver call hands over the resident pin", () => {
+ assert.equal(RUNTIME.match(/loadedContextPin: (?:live|stateBeforeUnload)\.loadedCustomContextLength,/g)?.length, 3);
+});
+
test("Auto stays Auto across a same-model reload, under every preset source", () => {
// resolveLoadMaxSeqLength's isReloadingCurrentGguf branch: outside
// builtin-default, a same-model reload on Auto puts the RESOLVED context on the
@@ -329,7 +355,7 @@ test("the clamp that stops a manual reload resizing is not a user pin", () => {
// branch would take that as the NATIVE context. That is the app protecting the
// load, not the user choosing a length, so the pin is captured BEFORE it.
const capture = RUNTIME.indexOf("const explicitCtxPin = loadRequestContextPin(");
- const clamp = RUNTIME.indexOf("loadCustomContextLength = loadContextLength;");
+ const clamp = RUNTIME.indexOf("loadCustomContextLength = fixedLayerContext(");
assert.notEqual(capture, -1, "the load no longer captures the user's setting");
assert.notEqual(clamp, -1);
assert.ok(
@@ -337,6 +363,11 @@ test("the clamp that stops a manual reload resizing is not a user pin", () => {
"the clamp now runs before the pin is captured, so the app's substituted length " +
"would be recorded as the user's choice",
);
+ // /validate sizes the context the load will send, clamp included.
+ assert.match(
+ RUNTIME,
+ /const validateCustomContextLength = resetsPerModelSettings\s*\?\s*null\s*:\s*fixedLayerContext\(loadCustomContextLength, loadGpuLayers\);/,
+ );
});
test("the three in-app writers pin what the user asked for, not what they sent", () => {