From 0db7da96cce0c6263ef3459ecc4f233c7b4c2fff Mon Sep 17 00:00:00 2001 From: Shine1i Date: Mon, 16 Feb 2026 21:33:24 +0100 Subject: [PATCH] feat: support disabling top-k sampling with -1 and standardize normalization logic - Updated top-k parameter range to accept -1 in models and frontend. - Added utility to normalize top-k for backend compatibility. --- studio/backend/core/inference/inference.py | 6 ++++++ studio/backend/models/inference.py | 4 ++-- studio/frontend/src/features/chat/chat-settings-sheet.tsx | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/studio/backend/core/inference/inference.py b/studio/backend/core/inference/inference.py index 38394f6f33..31e23f5370 100644 --- a/studio/backend/core/inference/inference.py +++ b/studio/backend/core/inference/inference.py @@ -45,6 +45,11 @@ class InferenceBackend: logger.info(f"InferenceBackend initialized on {self.device}") + @staticmethod + def _normalize_top_k(top_k: int) -> int: + # API supports -1 as "disable top-k"; transformers expects 0 to disable. + return 0 if top_k < 0 else top_k + def load_model(self, config: ModelConfig, max_seq_length: int = 2048, @@ -560,6 +565,7 @@ class InferenceBackend: model_info = self.models[self.active_model_name] is_vision = model_info.get("is_vision", False) tokenizer = model_info.get("tokenizer") or model_info.get("processor") + top_k = self._normalize_top_k(top_k) if is_vision: # Vision model generation diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py index 1b661047ae..d2d98d7944 100644 --- a/studio/backend/models/inference.py +++ b/studio/backend/models/inference.py @@ -30,7 +30,7 @@ class GenerateRequest(BaseModel): system_prompt: str = Field("You are a helpful AI assistant.", description="System prompt") temperature: float = Field(0.7, ge=0.0, le=2.0, description="Sampling temperature") top_p: float = Field(0.9, ge=0.0, le=1.0, description="Top-p sampling") - top_k: int = Field(40, ge=1, le=100, description="Top-k sampling") + top_k: int = Field(40, ge=-1, le=100, description="Top-k sampling") max_new_tokens: int = Field(512, ge=1, le=4096, description="Maximum tokens to generate") repetition_penalty: float = Field(1.1, ge=1.0, le=2.0, description="Repetition penalty") image_base64: Optional[str] = Field(None, description="Base64 encoded image for vision models") @@ -128,7 +128,7 @@ class ChatCompletionRequest(BaseModel): max_tokens: Optional[int] = Field(512, ge=1, le=4096, description="Maximum tokens to generate") # ── Unsloth extensions (ignored by standard OpenAI clients) ── - top_k: int = Field(40, ge=1, le=100, description="[x-unsloth] Top-k sampling") + top_k: int = Field(40, ge=-1, le=100, description="[x-unsloth] Top-k sampling") min_p: float = Field(0.0, ge=0.0, le=1.0, description="[x-unsloth] Min-p sampling threshold") repetition_penalty: float = Field(1.1, ge=1.0, le=2.0, description="[x-unsloth] Repetition penalty") image_base64: Optional[str] = Field(None, description="[x-unsloth] Base64-encoded image for vision models") diff --git a/studio/frontend/src/features/chat/chat-settings-sheet.tsx b/studio/frontend/src/features/chat/chat-settings-sheet.tsx index 900a82a652..5a72e085ff 100644 --- a/studio/frontend/src/features/chat/chat-settings-sheet.tsx +++ b/studio/frontend/src/features/chat/chat-settings-sheet.tsx @@ -295,7 +295,7 @@ export function ChatSettingsPanel({