Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /stateset_stateset-agents /examples /archive /enhanced_grpo_demo.py
| """ | |
| Enhanced GRPO Framework Demonstration | |
| This script demonstrates the comprehensive improvements made to the GRPO Agent Framework, | |
| including adaptive learning, neural architecture search, multimodal processing, and | |
| intelligent orchestration capabilities. | |
| """ | |
| import asyncio | |
| import logging | |
| import numpy as np | |
| import torch | |
| import torch.nn as nn | |
| # Import enhanced GRPO framework components | |
| from stateset_agents.core import ( # Core components; Enhanced components; Advanced AI capabilities; Enums and configs | |
| RewardFunction, | |
| create_adaptive_learning_controller, | |
| create_intelligent_orchestrator, | |
| create_modality_input, | |
| create_multimodal_processor, | |
| create_nas_controller, | |
| ) | |
| # Set up logging | |
| logging.basicConfig( | |
| level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" | |
| ) | |
| logger = logging.getLogger(__name__) | |
| class DemoRewardFunction(RewardFunction): | |
| """Simple reward function for demonstration""" | |
| async def compute_reward(self, turns, context=None): | |
| # Simulate reward based on conversation quality | |
| if not turns: | |
| return {"score": 0.0, "breakdown": {}, "metadata": {}} | |
| last_turn = turns[-1] | |
| response_quality = min( | |
| 1.0, len(last_turn.agent_response) / 100.0 | |
| ) # Longer = better | |
| coherence_score = np.random.uniform(0.6, 1.0) # Simulate coherence analysis | |
| final_score = response_quality * 0.6 + coherence_score * 0.4 | |
| return { | |
| "score": final_score, | |
| "breakdown": { | |
| "response_quality": response_quality, | |
| "coherence": coherence_score, | |
| }, | |
| "metadata": {"turn_count": len(turns)}, | |
| } | |
| async def demo_training_function( | |
| model: nn.Module, optimizer: torch.optim.Optimizer | |
| ) -> float: | |
| """Simplified training function for NAS demonstration""" | |
| # Simulate training step | |
| batch_size = 8 | |
| seq_length = 32 | |
| vocab_size = 1000 | |
| # Create dummy input | |
| input_ids = torch.randint(0, vocab_size, (batch_size, seq_length)) | |
| labels = torch.randint(0, vocab_size, (batch_size, seq_length)) | |
| # Forward pass | |
| outputs = model(input_ids) | |
| if isinstance(outputs, tuple): | |
| logits = outputs[0] | |
| else: | |
| logits = outputs | |
| # Compute loss | |
| loss_fn = nn.CrossEntropyLoss() | |
| loss = loss_fn(logits.view(-1, logits.size(-1)), labels.view(-1)) | |
| # Backward pass | |
| optimizer.zero_grad() | |
| loss.backward() | |
| optimizer.step() | |
| return loss.item() | |
| async def demo_evaluation_function(model: nn.Module) -> float: | |
| """Simplified evaluation function for NAS demonstration""" | |
| model.eval() | |
| # Simulate evaluation | |
| batch_size = 4 | |
| seq_length = 32 | |
| vocab_size = 1000 | |
| with torch.no_grad(): | |
| input_ids = torch.randint(0, vocab_size, (batch_size, seq_length)) | |
| _ = model(input_ids) | |
| # Return simulated performance score | |
| performance = np.random.uniform(0.7, 0.95) | |
| model.train() | |
| return performance | |
| async def demonstrate_adaptive_learning(): | |
| """Demonstrate adaptive learning controller capabilities""" | |
| logger.info("๐ง Demonstrating Adaptive Learning Controller") | |
| # Create adaptive learning controller | |
| controller = create_adaptive_learning_controller( | |
| curriculum_strategy="performance_based", exploration_strategy="curiosity_driven" | |
| ) | |
| # Simulate training episodes | |
| task_id = "conversation_task_1" | |
| current_hyperparams = {"learning_rate": 0.001, "epsilon": 0.1, "temperature": 0.7} | |
| logger.info("Running adaptive learning simulation...") | |
| for episode in range(10): | |
| # Simulate environment state and actions | |
| state = f"conversation_state_{episode}" | |
| available_actions = ["respond", "clarify", "ask_question", "summarize"] | |
| # Simulate performance (improving over time) | |
| base_reward = 0.5 + (episode * 0.05) + np.random.normal(0, 0.1) | |
| reward = max(0.0, min(1.0, base_reward)) | |
| success = reward > 0.6 | |
| # Get adaptive learning recommendations | |
| ( | |
| difficulty, | |
| should_explore, | |
| selected_action, | |
| optimized_params, | |
| ) = await controller.step( | |
| task_id, state, available_actions, reward, success, current_hyperparams | |
| ) | |
| logger.info( | |
| f"Episode {episode + 1}: Reward={reward:.3f}, Difficulty={difficulty:.3f}, " | |
| f"Explore={should_explore}, Action={selected_action}" | |
| ) | |
| current_hyperparams = optimized_params | |
| # Get learning insights | |
| insights = await controller.get_learning_insights() | |
| logger.info(f"Learning insights: {insights['overall_stats']}") | |
| return controller | |
| async def demonstrate_neural_architecture_search(): | |
| """Demonstrate neural architecture search capabilities""" | |
| logger.info("๐๏ธ Demonstrating Neural Architecture Search") | |
| # Create NAS controller | |
| nas_controller = create_nas_controller( | |
| strategy="evolutionary", min_depth=2, max_depth=6, min_width=64, max_width=512 | |
| ) | |
| # Run architecture search (simplified for demo) | |
| logger.info("Running neural architecture search...") | |
| optimal_architecture = await nas_controller.search_optimal_architecture( | |
| train_fn=demo_training_function, | |
| eval_fn=demo_evaluation_function, | |
| input_dim=512, | |
| output_dim=1000, | |
| max_search_time=120, # 2 minutes for demo | |
| ) | |
| if optimal_architecture: | |
| logger.info( | |
| f"Found optimal architecture with {optimal_architecture.total_parameters} parameters" | |
| ) | |
| logger.info(f"Performance score: {optimal_architecture.performance_score:.4f}") | |
| logger.info( | |
| f"Architecture depth: {optimal_architecture.depth}, width: {optimal_architecture.width}" | |
| ) | |
| # Get search insights | |
| insights = nas_controller.get_search_insights() | |
| logger.info( | |
| f"NAS evaluated {insights['total_architectures_evaluated']} architectures" | |
| ) | |
| return optimal_architecture | |
| async def demonstrate_multimodal_processing(): | |
| """Demonstrate multimodal processing capabilities""" | |
| logger.info("๐ฏ Demonstrating Multimodal Processing") | |
| # Create multimodal processor | |
| processor = create_multimodal_processor( | |
| modalities=["text", "structured"], fusion_strategy="attention_fusion" | |
| ) | |
| # Initialize with available modalities (skip image/audio for demo) | |
| from stateset_agents.core.multimodal_processing import ModalityType | |
| await processor.initialize([ModalityType.TEXT, ModalityType.STRUCTURED]) | |
| # Create sample multimodal inputs | |
| text_input = create_modality_input( | |
| modality="text", | |
| data="This is a sample conversation about machine learning and AI capabilities.", | |
| metadata={"source": "user_message"}, | |
| ) | |
| structured_input = create_modality_input( | |
| modality="structured", | |
| data={"user_score": 0.85, "conversation_length": 15, "topic": "ai"}, | |
| metadata={"source": "conversation_metrics"}, | |
| ) | |
| # Process multimodal input | |
| logger.info("Processing multimodal input...") | |
| features, metadata = await processor.process_multimodal_input( | |
| [text_input, structured_input] | |
| ) | |
| logger.info(f"Processed {len(metadata['modalities_processed'])} modalities") | |
| logger.info(f"Fused feature shape: {metadata['feature_shape']}") | |
| logger.info(f"Processing time: {metadata['total_processing_time']:.3f}s") | |
| # Get processor stats | |
| stats = processor.get_processing_stats() | |
| logger.info(f"Supported modalities: {stats['supported_modalities']}") | |
| return features, metadata | |
| async def demonstrate_intelligent_orchestration(): | |
| """Demonstrate intelligent orchestration capabilities""" | |
| logger.info("๐ผ Demonstrating Intelligent Orchestration") | |
| # Create intelligent orchestrator | |
| orchestrator = create_intelligent_orchestrator( | |
| mode="adaptive", | |
| optimization_objective="balanced", | |
| enable_adaptive_learning=True, | |
| enable_nas=False, # Disable for demo speed | |
| enable_multimodal=True, | |
| performance_threshold=0.75, | |
| ) | |
| # Initialize orchestrator | |
| await orchestrator.initialize( | |
| enabled_modalities=["text", "structured"], | |
| training_function=demo_training_function, | |
| evaluation_function=demo_evaluation_function, | |
| ) | |
| # Simulate orchestrated training steps | |
| logger.info("Running orchestrated training simulation...") | |
| task_id = "demo_task" | |
| hyperparams = {"learning_rate": 0.001, "temperature": 0.8} | |
| for step in range(8): | |
| # Create sample multimodal inputs | |
| text_input = create_modality_input( | |
| modality="text", | |
| data=f"Training step {step + 1} conversation content", | |
| quality="medium", | |
| ) | |
| struct_input = create_modality_input( | |
| modality="structured", | |
| data={"step": step, "difficulty": 0.5 + step * 0.05}, | |
| quality="high", | |
| ) | |
| # Simulate performance (with some variation) | |
| reward = 0.6 + (step * 0.04) + np.random.normal(0, 0.08) | |
| reward = max(0.2, min(1.0, reward)) | |
| success = reward > 0.7 | |
| # Orchestrate training step | |
| results, decision = await orchestrator.orchestrate_training_step( | |
| task_id=task_id, | |
| state=f"step_{step}", | |
| available_actions=["continue", "adjust", "optimize"], | |
| reward=reward, | |
| success=success, | |
| current_hyperparams=hyperparams, | |
| multimodal_inputs=[text_input, struct_input], | |
| ) | |
| logger.info( | |
| f"Step {step + 1}: Reward={reward:.3f}, Success={success}, " | |
| f"Decision={decision.action} (confidence={decision.confidence:.2f})" | |
| ) | |
| # Update hyperparameters if suggested | |
| if ( | |
| "adaptive_learning" in results | |
| and "optimized_hyperparams" in results["adaptive_learning"] | |
| ): | |
| hyperparams = results["adaptive_learning"]["optimized_hyperparams"] | |
| # Get orchestration status and insights | |
| status = orchestrator.get_orchestration_status() | |
| insights = orchestrator.get_optimization_insights() | |
| logger.info( | |
| f"Orchestration completed: {status['performance_summary']['total_steps']} steps" | |
| ) | |
| logger.info( | |
| f"Recent performance: {status['performance_summary']['recent_performance']:.3f}" | |
| ) | |
| logger.info( | |
| f"Performance trend: {status['performance_summary']['performance_trend']:.4f}" | |
| ) | |
| if insights["performance_bottlenecks"]: | |
| logger.info("Performance bottlenecks detected:") | |
| for bottleneck in insights["performance_bottlenecks"]: | |
| logger.info(f" - {bottleneck['component']}: {bottleneck['issue']}") | |
| await orchestrator.shutdown() | |
| return status, insights | |
| async def demonstrate_integration_example(): | |
| """Demonstrate integrated usage of all enhanced features""" | |
| logger.info("๐ Demonstrating Full Integration") | |
| # This would be a more realistic example showing how all components | |
| # work together in a production setting | |
| logger.info("Creating integrated GRPO agent with all enhancements...") | |
| # In a real scenario, you would: | |
| # 1. Initialize the orchestrator with all components | |
| # 2. Set up multimodal inputs from your application | |
| # 3. Use adaptive learning for curriculum and exploration | |
| # 4. Periodically run NAS for architecture optimization | |
| # 5. Monitor and optimize performance continuously | |
| integration_summary = { | |
| "adaptive_learning": "Dynamically adjusts difficulty and exploration", | |
| "neural_architecture_search": "Optimizes model architecture for performance", | |
| "multimodal_processing": "Handles text, images, audio, and structured data", | |
| "intelligent_orchestration": "Coordinates all components intelligently", | |
| "performance_optimization": "Manages memory, computation, and resources", | |
| "error_handling": "Provides resilient error recovery and circuit breakers", | |
| "monitoring": "Real-time metrics and performance tracking", | |
| } | |
| logger.info("Integration capabilities:") | |
| for component, description in integration_summary.items(): | |
| logger.info(f" โ {component}: {description}") | |
| return integration_summary | |
| async def main(): | |
| """Run the comprehensive demonstration""" | |
| logger.info("๐ Starting Enhanced GRPO Framework Demonstration") | |
| logger.info("=" * 60) | |
| try: | |
| # Demonstrate each major enhancement | |
| await demonstrate_adaptive_learning() | |
| print() | |
| await demonstrate_neural_architecture_search() | |
| print() | |
| ( | |
| multimodal_features, | |
| multimodal_metadata, | |
| ) = await demonstrate_multimodal_processing() | |
| print() | |
| orchestration_status, insights = await demonstrate_intelligent_orchestration() | |
| print() | |
| await demonstrate_integration_example() | |
| print() | |
| # Summary | |
| logger.info("=" * 60) | |
| logger.info("๐ Demonstration Complete!") | |
| logger.info("") | |
| logger.info("Enhanced GRPO Framework now includes:") | |
| logger.info(" โข Adaptive Learning with curriculum and exploration strategies") | |
| logger.info(" โข Neural Architecture Search for optimal model design") | |
| logger.info( | |
| " โข Multimodal Processing for text, images, audio, and structured data" | |
| ) | |
| logger.info(" โข Intelligent Orchestration for coordinated optimization") | |
| logger.info(" โข Advanced error handling and performance optimization") | |
| logger.info(" โข Comprehensive monitoring and state management") | |
| logger.info("") | |
| logger.info("The framework is now production-ready for:") | |
| logger.info(" โ Large-scale AI agent deployment") | |
| logger.info(" โ Complex multimodal applications") | |
| logger.info(" โ Adaptive and self-improving systems") | |
| logger.info(" โ Enterprise-grade reliability and performance") | |
| except Exception as e: | |
| logger.error(f"Demonstration failed: {str(e)}") | |
| import traceback | |
| traceback.print_exc() | |
| if __name__ == "__main__": | |
| # Run the demonstration | |
| asyncio.run(main()) | |
Xet Storage Details
- Size:
- 14.3 kB
- Xet hash:
- 0472803ddcbf573910e7a3ccc3d09cddbb64462e3e87f26b04209bd6f7c0136a
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.