Yong Liu commited on
Commit ·
02d7d65
1
Parent(s): 4aa4d08
update handler
Browse filesFiles changed (2)
- README.mdDeleted-81
- handler.py+436-229
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081# Phi-4 Mini Inference Endpoint Handler
This repository contains code for deploying the Phi-4 Mini model to a HuggingFace Inference Endpoint with an OpenAI-compatible API format.
## Setup
1. Install the required dependencies: ``` pip install -r requirements.txt ```
2. Set the environment variable to your model path (optional if model is in the same directory): ``` export MODEL_PATH=/path/to/your/model ```
## Usage
When deploying to a HuggingFace Inference Endpoint, the `handler.py` file will be used to process requests. The endpoint accepts requests in an OpenAI-compatible format:
```json{ "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Tell me about language models."} ], "max_tokens": 256, "temperature": 0.7, "top_p": 1.0, "n": 1, "stop": ["\n", "User:"]}```
The endpoint returns responses in an OpenAI-compatible format:
```json{ "id": "cmpl-12345", "object": "chat.completion", "created": 0, "model": "phi4-mini-raw", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Language models are computational systems designed to understand and generate human language..." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 42, "completion_tokens": 156, "total_tokens": 198 }}```
## Local Testing
To test the handler locally before deployment:
```pythonfrom handler import EndpointHandler
# Initialize the handler with your model pathhandler = EndpointHandler("./phi4-mini-raw")
# Test with a sample requestrequest = { "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello, how are you?"} ]}
response = handler(request)print(response)``` 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265import osimport jsonimport torchfrom transformers import AutoTokenizer, AutoModelForCausalLMfrom typing import Dict, List, Any# Fix for the rope_scaling validation issueimport transformers.models.phi3.configuration_phi3# Store original methodoriginal_validation = transformers.models.phi3.configuration_phi3.Phi3Config._rope_scaling_validation# Replace with a no-op functiondef no_validation(self):pass# Apply the patchtransformers.models.phi3.configuration_phi3.Phi3Config._rope_scaling_validation = no_validationclass EndpointHandler:def __init__(self, path=""):# Initialize model and tokenizerself.model_path = path if path else os.environ.get("MODEL_PATH", "")print(f"Loading model from: {self.model_path}")# Load tokenizerself.tokenizer = AutoTokenizer.from_pretrained(self.model_path)# Determine the device to useself.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")print(f"Using device: {self.device}")# Load model directly without pipelineself.model = AutoModelForCausalLM.from_pretrained(self.model_path,torch_dtype=torch.float16,device_map="auto")# Ensure model is on the correct deviceif torch.cuda.is_available():self.model = self.model.cuda()print("Model loaded successfully")# For Phi3 models, monkey patch the RotaryEmbeddingtry:from transformers.models.phi3.modeling_phi3 import PhiRotaryEmbeddingoriginal_forward = PhiRotaryEmbedding.forwarddef patched_forward(self, position_ids, query, key, value=None):# Ensure position_ids is on the same device as queryposition_ids = position_ids.to(query.device)return original_forward(self, position_ids, query, key, value)PhiRotaryEmbedding.forward = patched_forwardprint("Successfully patched PhiRotaryEmbedding.forward")except Exception as e:print(f"Could not patch PhiRotaryEmbedding: {str(e)}")def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:"""Handle inference request in OpenAI-like format or HuggingFace Inference API format"""try:# Debugging: Print the received dataprint(f"Received data: {json.dumps(data, indent=2)}")# Handle HuggingFace Inference API formatif "inputs" in data:# Extract data from inputs keyif isinstance(data["inputs"], dict):# If inputs contains a dictionary, extract itinput_data = data["inputs"]elif isinstance(data["inputs"], str):# If inputs is a string, create a simple messageinput_data = {"messages": [{"role": "user", "content": data["inputs"]}]}else:print(f"Unexpected inputs format: {type(data['inputs'])}")# Try to convert to string if possibletry:input_data = {"messages": [{"role": "user", "content": str(data["inputs"])}]}except:raise ValueError(f"Unsupported inputs format: {type(data['inputs'])}")else:# Assume direct OpenAI formatinput_data = data# Debugging: Print the parsed input dataprint(f"Parsed input data: {json.dumps(input_data, indent=2)}")# Parse input datainputs = self._parse_input(input_data)# Generate responseoutputs = self._generate(inputs)# Format response in OpenAI-like formatreturn self._format_response(outputs, inputs)except Exception as e:print(f"Error during processing: {str(e)}")import tracebacktraceback.print_exc()return {"error": {"message": str(e),"type": "invalid_request_error","code": 400}}def _parse_input(self, data: Dict[str, Any]) -> Dict[str, Any]:"""Parse input data to extract generation parameters"""# Extract messagesmessages = data.get("messages", [])if not messages:print(f"No messages found in data: {json.dumps(data, indent=2)}")raise ValueError("No messages provided")# Convert messages to promptprompt = self._convert_messages_to_prompt(messages)# Extract generation parameters with defaultsgeneration_params = {"max_tokens": data.get("max_tokens", 256),"temperature": data.get("temperature", 0.7),"top_p": data.get("top_p", 1.0),"n": data.get("n", 1),"stream": data.get("stream", False),"stop": data.get("stop", None),"presence_penalty": data.get("presence_penalty", 0.0),"frequency_penalty": data.get("frequency_penalty", 0.0),}return {"prompt": prompt,"messages": messages,"generation_params": generation_params}def _convert_messages_to_prompt(self, messages: List[Dict[str, str]]) -> str:"""Convert list of messages to a prompt string"""prompt = ""for message in messages:role = message.get("role", "")content = message.get("content", "")if role == "system":prompt += f"System: {content}\n\n"elif role == "user":prompt += f"User: {content}\n\n"elif role == "assistant":prompt += f"Assistant: {content}\n\n"# Add final assistant promptprompt += "Assistant: "return promptdef _generate(self, inputs: Dict[str, Any]) -> Dict[str, Any]:"""Generate response using the model directly"""prompt = inputs["prompt"]params = inputs["generation_params"]# Get the model's devicedevice = next(self.model.parameters()).deviceprint(f"Model is on device: {device}")# Tokenize input and ensure it's on the correct deviceinput_ids = self.tokenizer.encode(prompt, return_tensors="pt").to(device)print(f"Input tensor device: {input_ids.device}")# Count input tokensinput_tokens = input_ids.shape[1]# Convert OpenAI-like parameters to HF parametersgeneration_kwargs = {"max_new_tokens": params["max_tokens"],"temperature": params["temperature"],"top_p": params["top_p"],"num_return_sequences": params["n"],"do_sample": params["temperature"] > 0,"pad_token_id": self.tokenizer.eos_token_id,}# Generate outputtry:with torch.no_grad():outputs = self.model.generate(input_ids,**generation_kwargs)print(f"Output tensor device: {outputs.device}")except RuntimeError as e:if "Expected all tensors to be on the same device" in str(e):print("Caught device mismatch error, trying to fix...")# A more drastic approach: move the model completely to CPU if there's a device issueif torch.cuda.is_available():print("Moving everything to CPU as a fallback")self.model = self.model.cpu()input_ids = input_ids.cpu()with torch.no_grad():outputs = self.model.generate(input_ids,**generation_kwargs)else:raiseelse:raise# Decode outputgenerated_texts = []for i in range(params["n"]):gen_text = self.tokenizer.decode(outputs[i][input_tokens:], skip_special_tokens=True)# Apply stop sequences if providedif params["stop"]:for stop in params["stop"]:if stop in gen_text:gen_text = gen_text[:gen_text.find(stop)]generated_texts.append(gen_text)# Count completion tokenscompletion_tokens = [len(self.tokenizer.encode(text)) for text in generated_texts]return {"generated_texts": generated_texts,"prompt_tokens": input_tokens,"completion_tokens": completion_tokens,}def _format_response(self, outputs: Dict[str, Any], inputs: Dict[str, Any]) -> Dict[str, Any]:"""Format response in OpenAI-like format"""generated_texts = outputs["generated_texts"]prompt_tokens = outputs["prompt_tokens"]completion_tokens = outputs["completion_tokens"]choices = []for i, text in enumerate(generated_texts):choices.append({"index": i,"message": {"role": "assistant","content": text},"finish_reason": "stop"})return {"id": f"cmpl-{hash(inputs['prompt']) % 10000}","object": "chat.completion","created": int(torch.cuda.current_device()) if torch.cuda.is_available() else 0,"model": os.path.basename(self.model_path),"choices": choices,"usage": {"prompt_tokens": prompt_tokens,"completion_tokens": sum(completion_tokens),"total_tokens": prompt_tokens + sum(completion_tokens)}}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472import osimport torchimport loggingimport timeimport tracebackimport jsonimport refrom typing import Dict, List, Any, Union, Generatorfrom transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamerfrom threading import Thread# Set up logginglogging.basicConfig(level=logging.INFO,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')logger = logging.getLogger(__name__)class EndpointHandler:def __init__(self, path=""):"""Initialize the model and tokenizer for Phi-4 inference.Args:path (str): Path to the model directory"""# Set default parameters for inferenceself.max_new_tokens = 1024 # Keep at 1024 to avoid timeoutsself.temperature = 0.7self.top_p = 0.9self.do_sample = True# Determine if CUDA is availableself.device = "cuda" if torch.cuda.is_available() else "cpu"self.dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32logger.info(f"Initializing model from {path} on {self.device}")try:# Load tokenizer - use original model ID as fallback# This helps with common tokenizer mismatch issuestry:self.tokenizer = AutoTokenizer.from_pretrained(path)logger.info(f"Loaded tokenizer from local path")except Exception as e:logger.warning(f"Failed to load tokenizer from local path: {e}")self.tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-4-mini-instruct")logger.info("Loaded tokenizer from microsoft/Phi-4-mini-instruct")# Ensure tokenizer has EOS token setif self.tokenizer.eos_token_id is None:logger.warning("EOS token not set in tokenizer, using default")self.tokenizer.eos_token_id = 199999 # Phi-4's default EOS token# Load model with appropriate settingsself.model = AutoModelForCausalLM.from_pretrained(path,torch_dtype=self.dtype,device_map="auto" if self.device == "cuda" else None,trust_remote_code=True)# Move model to device if CPUif self.device == "cpu":self.model = self.model.to(self.device)# Set model to evaluation modeself.model.eval()# Print diagnostic informationlogger.info(f"Model loaded on {self.device} using {self.dtype}")logger.info(f"Tokenizer vocabulary size: {len(self.tokenizer)}")logger.info(f"Model vocabulary size: {self.model.config.vocab_size}")logger.info(f"Model embedding size: {self.model.get_input_embeddings().weight.shape}")if len(self.tokenizer) != self.model.config.vocab_size:logger.warning(f"Tokenizer vocab size ({len(self.tokenizer)}) doesn't match model vocab size ({self.model.config.vocab_size})")except Exception as e:logger.error(f"Error during model initialization: {str(e)}")logger.error(traceback.format_exc())raisedef format_prompt_with_system(self, user_message, system_message=None):"""Format the prompt with system and user messages according to Phi-4 format.Args:user_message (str): The user's messagesystem_message (str, optional): The system message/instructionReturns:str: Formatted prompt ready for the model"""# Format using Phi-4's expected chat template:# <|system|># {system_message}# <|user|># {user_message}# <|assistant|>if system_message:prompt = f"<|system|>\n{system_message}\n<|user|>\n{user_message}\n<|assistant|>"else:# If no system message, just use user message with assistant tagprompt = f"<|user|>\n{user_message}\n<|assistant|>"logger.info(f"Formatted prompt with {'system message and ' if system_message else ''}user message")return promptdef __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:"""Process the input data and generate a response using the Phi-4 model.Args:data (Dict[str, Any]): Input data containing the prompt and generation parametersReturns:Dict[str, Any]: Model response"""start_time = time.time()logger.info(f"Starting request processing")try:# Extract input parameters with defaultsif "inputs" not in data:logger.warning("No 'inputs' field in request data")error_msg = "Missing 'inputs' field in request"return self._format_error_response(error_msg)# Track user and system messagesuser_message = ""system_message = None# Handle different input formats# 1. Direct string inputif isinstance(data["inputs"], str):user_message = data["inputs"]system_message = data.get("parameters", {}).get("system_message", None)# 2. Dict with messages formatelif isinstance(data["inputs"], dict) and "messages" in data["inputs"]:messages = data["inputs"]["messages"]# Extract system and user messages for prompt formattingfor msg in messages:if msg.get("role") == "system":system_message = msg.get("content", "")elif msg.get("role") == "user":user_message = msg.get("content", "")# 3. Direct messages list formatelif isinstance(data["inputs"], list):messages = data["inputs"]# Extract system and user messages for prompt formattingfor msg in messages:if msg.get("role") == "system":system_message = msg.get("content", "")elif msg.get("role") == "user":user_message = msg.get("content", "")else:logger.warning(f"Unsupported input format: {type(data['inputs'])}")error_msg = "Unsupported input format. Expected string or messages object."return self._format_error_response(error_msg)logger.info(f"Extracted user message length: {len(user_message)} characters")if system_message:logger.info(f"Extracted system message length: {len(system_message)} characters")# Format the prompt with system and user messagesprompt = self.format_prompt_with_system(user_message, system_message)parameters = data.get("parameters", {})logger.info(f"Processing input with {len(prompt)} characters")# Get generation parameters with fallbacks to defaultsmax_new_tokens = min(parameters.get("max_new_tokens", self.max_new_tokens), 1024)temperature = parameters.get("temperature", self.temperature)top_p = parameters.get("top_p", self.top_p)do_sample = parameters.get("do_sample", self.do_sample)logger.info(f"Generation parameters: max_new_tokens={max_new_tokens}, temperature={temperature}, top_p={top_p}, do_sample={do_sample}")# Manually implement generation to avoid token index errorstry:input_ids = self.tokenizer.encode(prompt, return_tensors="pt").to(self.device)logger.info(f"Input tokens shape: {input_ids.shape}")# Create attention maskattention_mask = torch.ones_like(input_ids)# Perform safe generation with error handling for out-of-vocabulary issuesresponse_text = self._safe_generate(input_ids,attention_mask,max_new_tokens,temperature,top_p,do_sample,prompt)logger.info(f"Response generation completed, text length: {len(response_text) if isinstance(response_text, str) else 'N/A'}")# Format and return response in OpenAI formatif isinstance(response_text, str):response_tokens = len(self.tokenizer.encode(response_text)) if response_text else 0logger.info(f"Response token count: {response_tokens}")return self._format_openai_response(response_text,input_ids.shape[1],response_tokens)else:return self._format_error_response(f"Error during generation: {response_text}")except RuntimeError as e:logger.error(f"Runtime Error during generation: {str(e)}")logger.error(traceback.format_exc())return self._format_error_response(f"Error during generation: {str(e)}")except Exception as e:logger.error(f"Unexpected error during request processing: {str(e)}")logger.error(traceback.format_exc())return self._format_error_response(f"Unexpected error: {str(e)}")finally:duration = time.time() - start_timelogger.info(f"Request processing completed in {duration:.2f} seconds")def _complete_sentence(self, text):"""Ensure the text ends with a complete sentence"""# If text is already a complete sentence, return itif text.strip().endswith(('.', '!', '?')):return text# Find the last complete sentence endsentences = re.split(r'([.!?])\s+', text)if len(sentences) <= 1:# No complete sentences found, return as is with ellipsisreturn text + "..."# Reconstruct text up to the last complete sentenceresult = ""for i in range(len(sentences) - 1):if i % 2 == 0: # Content before punctuationresult += sentences[i]else: # Punctuationresult += sentences[i] + " "return result.strip()def _safe_generate(self, input_ids, attention_mask, max_new_tokens, temperature, top_p, do_sample, prompt):"""Safely generate text handling potential token index errors"""try:with torch.no_grad():logger.info("Starting safe generation")# Get the input text to exclude from final outputinput_text = promptlogger.info(f"Input prompt length: {len(input_text)} characters")# Generate one token at a time to avoid index errors# Use a lower absolute maximum to ensure completionmax_steps = min(max_new_tokens, 450) # Adjusted down from 500current_ids = input_ids.clone()logger.info(f"Generating up to {max_steps} tokens")# Keep track of last 5 tokens to detect repetitionlast_tokens = []repetition_detected = Falsefor i in range(max_steps):if i % 50 == 0:logger.info(f"Generated {i} tokens so far")# Early termination if we're getting close to the limit to allow for post-processingif i >= max_steps - 50:# Temporarily decode to check if we have a complete response alreadytemp_text = self.tokenizer.decode(current_ids[0], skip_special_tokens=True)if "<|assistant|>" in temp_text:temp_response = temp_text.split("<|assistant|>")[1].strip()# If we have a reasonably complete response, stop earlyif len(temp_response) > 100 and temp_response.count('.') >= 3:logger.info(f"Early termination at {i} tokens with complete response detected")break# Get logits for next tokenoutputs = self.model(input_ids=current_ids,attention_mask=attention_mask,return_dict=True)next_token_logits = outputs.logits[:, -1, :]# Apply temperature and samplingif temperature > 0:next_token_logits = next_token_logits / temperatureif do_sample:# Apply top_p samplingsorted_logits, sorted_indices = torch.sort(next_token_logits, descending=True)cumulative_probs = torch.cumsum(torch.softmax(sorted_logits, dim=-1), dim=-1)# Remove tokens with cumulative probability above the thresholdsorted_indices_to_remove = cumulative_probs > top_p# Shift the indices to the right to keep also the first token above the thresholdsorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()sorted_indices_to_remove[..., 0] = 0indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)next_token_logits[indices_to_remove] = -float('Inf')# Sample from the filtered distributionprobs = torch.softmax(next_token_logits, dim=-1)next_token = torch.multinomial(probs, num_samples=1)else:# Take the token with highest probabilitynext_token = torch.argmax(next_token_logits, dim=-1, keepdim=True)# Add the predicted token to the sequencecurrent_ids = torch.cat([current_ids, next_token], dim=-1)attention_mask = torch.cat([attention_mask, torch.ones_like(next_token)], dim=-1)# Add to last tokens list for repetition detectionlast_tokens.append(next_token.item())if len(last_tokens) > 5:last_tokens.pop(0)# Check for repetition (if we have at least 5 tokens)if len(last_tokens) >= 5:# Check if all last 5 tokens are the sameif len(set(last_tokens)) == 1:logger.warning(f"Repetition detected after {i+1} tokens, stopping generation")repetition_detected = Truebreak# Check if we've generated an EOS tokenif next_token[0, 0].item() == self.tokenizer.eos_token_id:logger.info(f"EOS token generated after {i+1} tokens")break# Decode the generated sequencegenerated_text = self.tokenizer.decode(current_ids[0], skip_special_tokens=True)logger.info(f"Decoded generated text: {len(generated_text)} characters")# Return only the newly generated text (after the assistant tag)split_text = generated_text.split("<|assistant|>")if len(split_text) > 1:assistant_response = split_text[1].strip()logger.info(f"Raw assistant response: {len(assistant_response)} characters")# Process the response to ensure complete sentencesresponse_text = self._complete_sentence(assistant_response)logger.info(f"Processed assistant response: {len(response_text)} characters")else:# Fallback if the expected format is not foundlogger.warning("Could not find assistant tag in generated text")response_text = generated_textreturn response_textexcept Exception as e:logger.error(f"Error in _safe_generate: {str(e)}")logger.error(traceback.format_exc())return f"Generation error: {str(e)}. Please try a simpler input."def _format_openai_response(self, response_text, prompt_tokens, completion_tokens):"""Format the response in OpenAI-style format"""try:# Create a response IDresponse_id = f"phi4-{int(time.time())}"# Build OpenAI-compatible responseopenai_response = {"id": response_id,"object": "chat.completion","created": int(time.time()),"model": "phi-4-mini","choices": [{"index": 0,"message": {"role": "assistant","content": response_text},"finish_reason": "stop"}],"usage": {"prompt_tokens": prompt_tokens,"completion_tokens": completion_tokens,"total_tokens": prompt_tokens + completion_tokens}}# For compatibility with Hugging Face UI, include the generated_text fieldopenai_response["generated_text"] = response_textlogger.info(f"Formatted OpenAI-style response: {len(json.dumps(openai_response))} bytes")return openai_responseexcept Exception as e:logger.error(f"Error formatting OpenAI response: {str(e)}")# Fall back to simple responsereturn {"generated_text": response_text}def _format_error_response(self, error_message):"""Format an error response in OpenAI-style format"""try:error_response = {"id": f"phi4-error-{int(time.time())}","object": "chat.completion","created": int(time.time()),"model": "phi-4-mini","choices": [{"index": 0,"message": {"role": "assistant","content": f"Error: {error_message}"},"finish_reason": "error"}],"usage": {"prompt_tokens": 0,"completion_tokens": 0,"total_tokens": 0},"error": {"message": error_message,"type": "invalid_request_error","code": "error"}}# For compatibility with Hugging Face UI, include the generated_text fielderror_response["generated_text"] = f"Error: {error_message}"logger.info(f"Formatted error response: {len(json.dumps(error_response))} bytes")return error_responseexcept Exception as e:logger.error(f"Error formatting error response: {str(e)}")# Fall back to simple error responsereturn {"generated_text": f"Error: {error_message}"}# For local testingif __name__ == "__main__":# Example usagehandler = EndpointHandler()# Test with messages formattest_with_messages = {"inputs": {"messages": [{"role": "system", "content": "You are an AI assistant that provides helpful, accurate, and concise information about AI models."},{"role": "user", "content": "What are the major features of Phi-4?"}]}}# Run the testresult = handler(test_with_messages)print(json.dumps(result, indent=2))
README.mdDeleted-81
handler.py+436-229