Spaces:
Sleeping
Sleeping
Fix runtime: src/llm_handler.py
Browse files77 unmodified lines7879808182838485868788898182838485869091929394959697983 unmodified lines102103104961051061071089910010110911011111211311411511611711811912012112210410510610710812312412512612712812913013113213313413513613711013813914014114214314414514614714814915015115215315415515615715815916011416116216316477 unmodified lines dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32 device_map = "auto" if torch.cuda.is_available() else None
# Prefer `dtype=` (torch_dtype is deprecated in recent transformers) try: self._model = AutoModelForCausalLM.from_pretrained( self.model_name, dtype=dtype, device_map=device_map, trust_remote_code=True, ) except TypeError: self._model = AutoModelForCausalLM.from_pretrained( self.model_name, torch_dtype=dtype, device_map=device_map, trust_remote_code=True, ) self._model = AutoModelForCausalLM.from_pretrained( self.model_name, torch_dtype=dtype, device_map=device_map, trust_remote_code=True, ) if device_map is None: self._model = self._model.to("cpu") self._model.eval()3 unmodified lines """Use chat template when available; fall back to raw prompt.""" return [{"role": "user", "content": prompt}]
def _generate_text(self, prompt: str) -> str: def _tokenize_prompt(self, prompt: str) -> tuple[Any, Any]: """Tokenize prompt into (input_ids, attention_mask) tensors.""" import torch
self._ensure_loaded() assert self._tokenizer is not None and self._model is not None
messages = self._build_messages(prompt) attention_mask = None
if hasattr(self._tokenizer, "apply_chat_template"): # Newer transformers may return BatchEncoding when return_dict=True, # or a raw Tensor / BatchEncoding depending on version. try: encoded = self._tokenizer.apply_chat_template( messages, add_generation_prompt=True, return_tensors="pt", return_dict=True, ) except TypeError: input_ids = self._tokenizer.apply_chat_template( messages, add_generation_prompt=True, return_tensors="pt", ) encoded = self._tokenizer.apply_chat_template( messages, add_generation_prompt=True, return_tensors="pt", )
if hasattr(encoded, "input_ids"): input_ids = encoded["input_ids"] attention_mask = encoded.get("attention_mask") elif isinstance(encoded, dict): input_ids = encoded["input_ids"] attention_mask = encoded.get("attention_mask") else: input_ids = encoded else: input_ids = self._tokenizer(prompt, return_tensors="pt").input_ids encoded = self._tokenizer(prompt, return_tensors="pt") input_ids = encoded["input_ids"] attention_mask = encoded.get("attention_mask")
if not torch.is_tensor(input_ids): input_ids = torch.tensor(input_ids)
if attention_mask is None: attention_mask = torch.ones_like(input_ids) elif not torch.is_tensor(attention_mask): attention_mask = torch.tensor(attention_mask)
return input_ids, attention_mask
def _generate_text(self, prompt: str) -> str: import torch
self._ensure_loaded() assert self._tokenizer is not None and self._model is not None
input_ids, attention_mask = self._tokenize_prompt(prompt) model_device = next(self._model.parameters()).device input_ids = input_ids.to(model_device) attention_mask = torch.ones_like(input_ids) attention_mask = attention_mask.to(model_device)
with torch.inference_mode(): output_ids = self._model.generate(src/llm_handler.py+64-17