atakan commited on
Commit
cb61511
·
1 Parent(s): 306a458

feat: Enforce high-speed GGUF Q4_K_M llama_cpp execution on Hugging Face Spaces CPU

Browse files
Files changed (2) hide show
  1. controlai_agent/orchestrator.py +46 -27
  2. requirements.txt +2 -0
controlai_agent/orchestrator.py CHANGED
@@ -182,35 +182,54 @@ class ControlAIAgent:
182
  self.model, self.mlx_tokenizer = mlx_load(model_path)
183
  self.hf_tokenizer = AutoTokenizer.from_pretrained(model_path)
184
  else:
185
- # Universal PyTorch / Transformers fallback on Linux, Colab, HuggingFace, CUDA
186
- import torch
187
- num_threads = min(os.cpu_count() or 2, 4)
188
- try:
189
- torch.set_num_threads(num_threads)
190
- except Exception:
191
- pass
192
-
193
- hf_id = "Qwen/Qwen2.5-3B-Instruct" if "mlx" in str(model_path) else model_path
194
- print(f"Loading PyTorch model: {hf_id} (CPU threads: {num_threads})...")
195
- self.hf_tokenizer = AutoTokenizer.from_pretrained(hf_id, trust_remote_code=True)
196
-
197
- # Use bfloat16 on CUDA, float32 on CPU for clean numerical stability
198
- dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
199
- self.model = AutoModelForCausalLM.from_pretrained(
200
- hf_id,
201
- torch_dtype=dtype,
202
- low_cpu_mem_usage=True,
203
- device_map="auto",
204
- trust_remote_code=True,
205
- )
206
- if adapter_path and Path(adapter_path).exists():
207
  try:
208
- from peft import PeftModel
209
- self.model = PeftModel.from_pretrained(self.model, adapter_path)
210
- print(f"Loaded PEFT LoRA adapter from: {adapter_path}")
 
 
 
 
 
 
 
 
 
211
  except Exception as exc:
212
- print(f"Warning: Could not load LoRA adapter in PyTorch: {exc}")
213
- print("PyTorch model loaded successfully.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
 
215
  # Initialize local offline RAG index
216
  try:
 
182
  self.model, self.mlx_tokenizer = mlx_load(model_path)
183
  self.hf_tokenizer = AutoTokenizer.from_pretrained(model_path)
184
  else:
185
+ # Universal Linux / Cloud / HuggingFace Spaces backend: Fast 4-bit C++ GGUF
186
+ self.llama_model = None
187
+ if HAS_LLAMA_CPP:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  try:
189
+ threads = min(os.cpu_count() or 2, 4)
190
+ print(f"Loading high-speed GGUF Q4_K_M engine via llama_cpp (threads: {threads})...")
191
+ self.llama_model = llama_cpp.Llama.from_pretrained(
192
+ repo_id="Qwen/Qwen2.5-3B-Instruct-GGUF",
193
+ filename="*q4_k_m.gguf",
194
+ n_ctx=2048,
195
+ n_threads=threads,
196
+ verbose=False,
197
+ )
198
+ self.is_gguf = True
199
+ self.hf_tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-3B-Instruct", trust_remote_code=True)
200
+ print("GGUF Q4_K_M model loaded successfully via llama_cpp.")
201
  except Exception as exc:
202
+ print(f"Notice: llama_cpp GGUF auto-load failed, falling back to PyTorch: {exc}")
203
+ self.is_gguf = False
204
+
205
+ if not self.is_gguf:
206
+ # PyTorch fallback on CUDA or if GGUF is disabled
207
+ import torch
208
+ num_threads = min(os.cpu_count() or 2, 4)
209
+ try:
210
+ torch.set_num_threads(num_threads)
211
+ except Exception:
212
+ pass
213
+
214
+ hf_id = "Qwen/Qwen2.5-3B-Instruct" if "mlx" in str(model_path) else model_path
215
+ print(f"Loading PyTorch model: {hf_id} (threads: {num_threads})...")
216
+ self.hf_tokenizer = AutoTokenizer.from_pretrained(hf_id, trust_remote_code=True)
217
+ dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
218
+ self.model = AutoModelForCausalLM.from_pretrained(
219
+ hf_id,
220
+ torch_dtype=dtype,
221
+ low_cpu_mem_usage=True,
222
+ device_map="auto",
223
+ trust_remote_code=True,
224
+ )
225
+ if adapter_path and Path(adapter_path).exists():
226
+ try:
227
+ from peft import PeftModel
228
+ self.model = PeftModel.from_pretrained(self.model, adapter_path)
229
+ print(f"Loaded PEFT LoRA adapter from: {adapter_path}")
230
+ except Exception as exc:
231
+ print(f"Warning: Could not load LoRA adapter in PyTorch: {exc}")
232
+ print("PyTorch model loaded successfully.")
233
 
234
  # Initialize local offline RAG index
235
  try:
requirements.txt CHANGED
@@ -1,3 +1,5 @@
 
 
1
  torch>=2.0.0
2
  transformers>=4.40.0
3
  accelerate>=0.28.0
 
1
+ llama-cpp-python>=0.2.80
2
+ huggingface-hub>=0.23.0
3
  torch>=2.0.0
4
  transformers>=4.40.0
5
  accelerate>=0.28.0