Process a batch of prompts with all optimizations
(
self,
system_prompts: List[str],
user_prompts: List[str],
generation_params: Optional[Dict[str, Any]] = None,
active_adapter: str = None,
return_token_count: bool = True
)
| 1513 | ]) |
| 1514 | |
| 1515 | def process_batch( |
| 1516 | self, |
| 1517 | system_prompts: List[str], |
| 1518 | user_prompts: List[str], |
| 1519 | generation_params: Optional[Dict[str, Any]] = None, |
| 1520 | active_adapter: str = None, |
| 1521 | return_token_count: bool = True |
| 1522 | ) -> Tuple[List[str], List[int]]: |
| 1523 | """Process a batch of prompts with all optimizations""" |
| 1524 | |
| 1525 | # Set the requested adapter if specified |
| 1526 | if isinstance(self.current_model, PeftModel) and active_adapter is not None: |
| 1527 | self.lora_manager.set_active_adapter(self.current_model, active_adapter) |
| 1528 | |
| 1529 | all_responses = [] |
| 1530 | token_counts = [] |
| 1531 | |
| 1532 | # Format all prompts using chat template |
| 1533 | formatted_prompts = [ |
| 1534 | self.format_chat_prompt(system_prompt, user_prompt) |
| 1535 | for system_prompt, user_prompt in zip(system_prompts, user_prompts) |
| 1536 | ] |
| 1537 | |
| 1538 | # Get number of completions requested |
| 1539 | n = generation_params.get("num_return_sequences", 1) if generation_params else 1 |
| 1540 | |
| 1541 | for i in range(0, len(formatted_prompts), self.optimal_batch_size): |
| 1542 | batch_prompts = formatted_prompts[i:i + self.optimal_batch_size] |
| 1543 | batch_system = system_prompts[i:i + self.optimal_batch_size] |
| 1544 | batch_user = user_prompts[i:i + self.optimal_batch_size] |
| 1545 | |
| 1546 | # Check cache first if enabled |
| 1547 | if self.model_config.enable_prompt_caching: |
| 1548 | cached_responses = [] |
| 1549 | uncached_indices = [] |
| 1550 | |
| 1551 | for idx, prompt in enumerate(batch_prompts): |
| 1552 | temp = generation_params.get("temperature", self.model_config.temperature) if generation_params else self.model_config.temperature |
| 1553 | top_p = generation_params.get("top_p", self.model_config.top_p) if generation_params else self.model_config.top_p |
| 1554 | |
| 1555 | cached_response = self.cache_manager.prompt_cache.get_cached_response( |
| 1556 | prompt, |
| 1557 | temp, |
| 1558 | top_p |
| 1559 | ) |
| 1560 | if cached_response is not None: |
| 1561 | # For cached responses, replicate n times if multiple completions requested |
| 1562 | cached_responses.extend([cached_response] * n) |
| 1563 | else: |
| 1564 | uncached_indices.append(idx) |
| 1565 | |
| 1566 | if uncached_indices: |
| 1567 | batch_prompts = [batch_prompts[i] for i in uncached_indices] |
| 1568 | else: |
| 1569 | batch_prompts = [] |
| 1570 | |
| 1571 | if batch_prompts: # If there are any uncached prompts |
| 1572 | # Configure generation parameters |
nothing calls this directly
no test coverage detected