Spaces:
Running on Zero
Running on Zero
Update src/llm_handler.py
Browse files- src/llm_handler.py +63 -25
src/llm_handler.py
CHANGED
|
@@ -38,9 +38,10 @@ GROQ_MODELS = [
|
|
| 38 |
|
| 39 |
DEFAULT_GROQ_MODEL = "openai/gpt-oss-120b"
|
| 40 |
|
| 41 |
-
DEFAULT_HF_MODEL = "Qwen/
|
| 42 |
|
| 43 |
HF_MODELS = [
|
|
|
|
| 44 |
"Qwen/Qwen2.5-3B-Instruct",
|
| 45 |
"Qwen/Qwen2.5-1.5B-Instruct",
|
| 46 |
"microsoft/Phi-3.5-mini-instruct",
|
|
@@ -78,21 +79,48 @@ class HuggingFaceTransformersLLM(LLM):
|
|
| 78 |
dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 79 |
device_map = "auto" if torch.cuda.is_available() else None
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# Prefer `dtype=` (torch_dtype is deprecated in recent transformers)
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if device_map is None:
|
| 97 |
self._model = self._model.to("cpu")
|
| 98 |
self._model.eval()
|
|
@@ -108,23 +136,33 @@ class HuggingFaceTransformersLLM(LLM):
|
|
| 108 |
|
| 109 |
messages = self._build_messages(prompt)
|
| 110 |
attention_mask = None
|
|
|
|
| 111 |
|
| 112 |
if hasattr(self._tokenizer, "apply_chat_template"):
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
try:
|
| 116 |
encoded = self._tokenizer.apply_chat_template(
|
| 117 |
messages,
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
return_dict=True,
|
| 121 |
)
|
| 122 |
except TypeError:
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
if hasattr(encoded, "input_ids"):
|
| 130 |
input_ids = encoded["input_ids"]
|
|
|
|
| 38 |
|
| 39 |
DEFAULT_GROQ_MODEL = "openai/gpt-oss-120b"
|
| 40 |
|
| 41 |
+
DEFAULT_HF_MODEL = "Qwen/Qwen3.5-4B"
|
| 42 |
|
| 43 |
HF_MODELS = [
|
| 44 |
+
"Qwen/Qwen3.5-4B",
|
| 45 |
"Qwen/Qwen2.5-3B-Instruct",
|
| 46 |
"Qwen/Qwen2.5-1.5B-Instruct",
|
| 47 |
"microsoft/Phi-3.5-mini-instruct",
|
|
|
|
| 79 |
dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 80 |
device_map = "auto" if torch.cuda.is_available() else None
|
| 81 |
|
| 82 |
+
load_kwargs = {
|
| 83 |
+
"trust_remote_code": True,
|
| 84 |
+
"device_map": device_map,
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
# Prefer `dtype=` (torch_dtype is deprecated in recent transformers)
|
| 88 |
+
model = None
|
| 89 |
+
for dtype_key in ("dtype", "torch_dtype"):
|
| 90 |
+
try:
|
| 91 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 92 |
+
self.model_name,
|
| 93 |
+
**{dtype_key: dtype},
|
| 94 |
+
**load_kwargs,
|
| 95 |
+
)
|
| 96 |
+
break
|
| 97 |
+
except TypeError:
|
| 98 |
+
continue
|
| 99 |
+
except Exception as causal_err:
|
| 100 |
+
logger.warning(
|
| 101 |
+
"AutoModelForCausalLM failed for %s (%s); trying image-text class",
|
| 102 |
+
self.model_name,
|
| 103 |
+
causal_err,
|
| 104 |
+
)
|
| 105 |
+
break
|
| 106 |
+
|
| 107 |
+
if model is None:
|
| 108 |
+
try:
|
| 109 |
+
from transformers import AutoModelForImageTextToText
|
| 110 |
+
|
| 111 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 112 |
+
self.model_name,
|
| 113 |
+
dtype=dtype,
|
| 114 |
+
**load_kwargs,
|
| 115 |
+
)
|
| 116 |
+
except Exception:
|
| 117 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 118 |
+
self.model_name,
|
| 119 |
+
torch_dtype=dtype,
|
| 120 |
+
**load_kwargs,
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
self._model = model
|
| 124 |
if device_map is None:
|
| 125 |
self._model = self._model.to("cpu")
|
| 126 |
self._model.eval()
|
|
|
|
| 136 |
|
| 137 |
messages = self._build_messages(prompt)
|
| 138 |
attention_mask = None
|
| 139 |
+
enable_thinking = bool(get_config().get("llm.enable_thinking", False))
|
| 140 |
|
| 141 |
if hasattr(self._tokenizer, "apply_chat_template"):
|
| 142 |
+
template_kwargs: dict[str, Any] = {
|
| 143 |
+
"add_generation_prompt": True,
|
| 144 |
+
"return_tensors": "pt",
|
| 145 |
+
"return_dict": True,
|
| 146 |
+
}
|
| 147 |
+
# Qwen3 / Qwen3.5 support enable_thinking in chat template
|
| 148 |
try:
|
| 149 |
encoded = self._tokenizer.apply_chat_template(
|
| 150 |
messages,
|
| 151 |
+
enable_thinking=enable_thinking,
|
| 152 |
+
**template_kwargs,
|
|
|
|
| 153 |
)
|
| 154 |
except TypeError:
|
| 155 |
+
try:
|
| 156 |
+
encoded = self._tokenizer.apply_chat_template(
|
| 157 |
+
messages,
|
| 158 |
+
**template_kwargs,
|
| 159 |
+
)
|
| 160 |
+
except TypeError:
|
| 161 |
+
encoded = self._tokenizer.apply_chat_template(
|
| 162 |
+
messages,
|
| 163 |
+
add_generation_prompt=True,
|
| 164 |
+
return_tensors="pt",
|
| 165 |
+
)
|
| 166 |
|
| 167 |
if hasattr(encoded, "input_ids"):
|
| 168 |
input_ids = encoded["input_ids"]
|