Spaces:
Build error
Build error
Update app.py
Browse files315 unmodified lines316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372315 unmodified linesself._initialized = Falseraisedef _initialize_model(self):try:# Clear any existing model firstif DynamicLLM._model is not None:del DynamicLLM._modelself.memory_manager.force_memory_release()if DynamicLLM._tokenizer is None:DynamicLLM._tokenizer = AutoTokenizer.from_pretrained(self.model_name,use_fast=False # Use slower but more stable tokenizer)self.tokenizer = DynamicLLM._tokenizer# Configure quantizationquantization_config = BitsAndBytesConfig(load_in_8bit=True,llm_int8_threshold=6.0,llm_int8_has_fp16_weight=True)# Load model with proper error handlingif DynamicLLM._model is None:self.memory_manager.force_memory_release()# Load model configuration firstconfig = AutoConfig.from_pretrained(self.model_name)config.use_cache = False # Disable KV cache to save memory# Load the model with quantization and safety measuresDynamicLLM._model = AutoModelForCausalLM.from_pretrained(self.model_name,config=config,quantization_config=quantization_config,device_map='auto',torch_dtype=torch.float16,low_cpu_mem_usage=True,offload_folder="offload_folder", # Add offload folder for safetyoffload_state_dict=True # Enable state dict offloading)# Verify model loaded correctlyif DynamicLLM._model is None:raise RuntimeError("Model failed to load")self.model = DynamicLLM._modelexcept Exception as e:print(f"Model initialization error: {str(e)}")self.memory_manager.force_memory_release()raisedef generate_response(self, prompt: str, role: str = None, context: Dict = None) -> str:try:315 unmodified lines316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378315 unmodified linesself._initialized = Falseraisedef _initialize_model(self):try:# Clear any existing model firstif DynamicLLM._model is not None:del DynamicLLM._modelself.memory_manager.force_memory_release()if DynamicLLM._tokenizer is None:DynamicLLM._tokenizer = AutoTokenizer.from_pretrained(self.model_name,use_fast=False)self.tokenizer = DynamicLLM._tokenizer# Modified quantization configurationquantization_config = BitsAndBytesConfig(load_in_4bit=True, # Changed to 4-bit quantizationbnb_4bit_compute_dtype=torch.float16,bnb_4bit_quant_type="nf4",bnb_4bit_use_double_quant=False)# Load model with proper error handlingif DynamicLLM._model is None:self.memory_manager.force_memory_release()# Load model configuration firstconfig = AutoConfig.from_pretrained(self.model_name)config.use_cache = False # Disable KV cache to save memory# Modified model loading with safer defaultsDynamicLLM._model = AutoModelForCausalLM.from_pretrained(self.model_name,config=config,quantization_config=quantization_config,device_map='auto',torch_dtype=torch.float16,low_cpu_mem_usage=True,offload_folder="offload_folder",offload_state_dict=True,trust_remote_code=False # Added safety measure)# Add model verificationif DynamicLLM._model is None:raise RuntimeError("Model failed to load")# Verify model is on correct deviceif torch.cuda.is_available():assert next(DynamicLLM._model.parameters()).is_cuda, "Model not loaded on GPU"self.model = DynamicLLM._modelexcept Exception as e:print(f"Model initialization error: {str(e)}")self.memory_manager.force_memory_release()raisedef generate_response(self, prompt: str, role: str = None, context: Dict = None) -> str:try:
app.py+51-45