Buckets:
AnonymousSpottedToad/human_verified_bucket / codebase /python /stateset_stateset-agents /examples /finetune_gemma3_gspo.py
| """ | |
| Fine-tune Gemma 3 (Gemma 2) Models with GSPO | |
| This script demonstrates how to fine-tune Google's Gemma 3 (Gemma 2) models using GSPO, | |
| providing stable and efficient training for conversational AI tasks. | |
| Supported Gemma 3 Models: | |
| - google/gemma-2-2b | |
| - google/gemma-2-2b-it (instruction-tuned) | |
| - google/gemma-2-9b | |
| - google/gemma-2-9b-it (instruction-tuned) | |
| - google/gemma-2-27b | |
| - google/gemma-2-27b-it (instruction-tuned) | |
| Usage: | |
| # Small model (2B) - Great for testing | |
| python examples/finetune_gemma3_gspo.py --model google/gemma-2-2b-it --task customer_service | |
| # Medium model (9B) - Balanced performance | |
| python examples/finetune_gemma3_gspo.py --model google/gemma-2-9b-it --task technical_support --use-lora | |
| # Large model (27B) - High quality | |
| python examples/finetune_gemma3_gspo.py --model google/gemma-2-27b-it --task sales --use-lora --use-4bit | |
| """ | |
| import argparse | |
| import asyncio | |
| import logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", | |
| ) | |
| logger = logging.getLogger(__name__) | |
| def get_gemma3_config( | |
| model_name: str, | |
| task: str, | |
| use_lora: bool = True, | |
| use_4bit: bool = False, | |
| use_8bit: bool = False, | |
| output_dir: str = "./outputs/gemma3_gspo", | |
| ): | |
| """ | |
| Get optimized GSPO configuration for Gemma 3 models. | |
| These configurations are optimized for different Gemma 3 model sizes | |
| and provide stable training with GSPO. | |
| """ | |
| from stateset_agents.training.config import get_config_for_task | |
| from stateset_agents.training.gspo_trainer import GSPOConfig | |
| # Get base config for task | |
| base_config = get_config_for_task(task, model_name=model_name) | |
| # Determine model size and adjust hyperparameters | |
| if "2b" in model_name.lower() or "2B" in model_name: | |
| # Small model config (2B) | |
| config = GSPOConfig.from_training_config( | |
| base_config, | |
| # GSPO parameters | |
| num_generations=4, | |
| clip_range_left=3e-4, | |
| clip_range_right=4e-4, | |
| # Training | |
| learning_rate=1e-5, | |
| num_outer_iterations=100, | |
| generations_per_iteration=50, | |
| per_device_train_batch_size=4, | |
| gradient_accumulation_steps=2, | |
| # Model optimization | |
| use_lora=use_lora, | |
| lora_r=16, | |
| lora_alpha=32, | |
| lora_dropout=0.05, | |
| lora_target_modules=["q_proj", "k_proj", "v_proj", "o_proj"], | |
| gradient_checkpointing=True, | |
| use_4bit=use_4bit, | |
| use_8bit=use_8bit, | |
| # Generation | |
| max_prompt_length=1024, | |
| max_completion_length=1024, | |
| temperature=0.7, | |
| # Output | |
| output_dir=output_dir, | |
| save_steps=10, | |
| logging_steps=1, | |
| ) | |
| elif "9b" in model_name.lower() or "9B" in model_name: | |
| # Medium model config (9B) | |
| config = GSPOConfig.from_training_config( | |
| base_config, | |
| num_generations=6, | |
| clip_range_left=2e-4, | |
| clip_range_right=3e-4, | |
| learning_rate=5e-6, | |
| num_outer_iterations=100, | |
| generations_per_iteration=30, | |
| per_device_train_batch_size=1, | |
| gradient_accumulation_steps=8, | |
| use_lora=True, # Always use LoRA for 9B | |
| lora_r=64, | |
| lora_alpha=128, | |
| lora_dropout=0.05, | |
| lora_target_modules=[ | |
| "q_proj", | |
| "k_proj", | |
| "v_proj", | |
| "o_proj", | |
| "gate_proj", | |
| "up_proj", | |
| "down_proj", | |
| ], | |
| gradient_checkpointing=True, | |
| use_4bit=use_4bit if use_4bit else use_8bit, | |
| use_8bit=use_8bit if not use_4bit else False, | |
| max_prompt_length=2048, | |
| max_completion_length=2048, | |
| temperature=0.7, | |
| output_dir=output_dir, | |
| save_steps=5, | |
| logging_steps=1, | |
| ) | |
| else: | |
| # Large model config (27B) | |
| config = GSPOConfig.from_training_config( | |
| base_config, | |
| num_generations=8, | |
| clip_range_left=1.5e-4, | |
| clip_range_right=2.5e-4, | |
| learning_rate=3e-6, | |
| num_outer_iterations=100, | |
| generations_per_iteration=20, | |
| per_device_train_batch_size=1, | |
| gradient_accumulation_steps=16, | |
| use_lora=True, | |
| lora_r=64, | |
| lora_alpha=128, | |
| lora_dropout=0.05, | |
| lora_target_modules=[ | |
| "q_proj", | |
| "k_proj", | |
| "v_proj", | |
| "o_proj", | |
| "gate_proj", | |
| "up_proj", | |
| "down_proj", | |
| ], | |
| gradient_checkpointing=True, | |
| use_4bit=True, # Always use quantization for 27B | |
| max_prompt_length=4096, | |
| max_completion_length=2048, | |
| temperature=0.7, | |
| output_dir=output_dir, | |
| save_steps=5, | |
| logging_steps=1, | |
| ) | |
| return config | |
| async def finetune_gemma3( | |
| model_name: str, | |
| task: str = "customer_service", | |
| use_lora: bool = True, | |
| use_4bit: bool = False, | |
| use_8bit: bool = False, | |
| output_dir: str = "./outputs/gemma3_gspo", | |
| use_wandb: bool = False, | |
| wandb_project: str | None = None, | |
| ): | |
| """ | |
| Fine-tune a Gemma 3 model using GSPO. | |
| Args: | |
| model_name: Gemma 3 model name (e.g., "google/gemma-2-9b-it") | |
| task: Task type (customer_service, technical_support, sales) | |
| use_lora: Use LoRA for efficient fine-tuning | |
| use_4bit: Use 4-bit quantization | |
| use_8bit: Use 8-bit quantization | |
| output_dir: Output directory for checkpoints | |
| use_wandb: Enable Weights & Biases logging | |
| wandb_project: W&B project name | |
| """ | |
| from stateset_agents import MultiTurnAgent | |
| from stateset_agents.core.agent import AgentConfig | |
| from stateset_agents.core.environment import ( | |
| CONVERSATION_CONFIGS, | |
| ConversationEnvironment, | |
| ) | |
| from stateset_agents.rewards.multi_objective_reward import create_domain_reward | |
| from stateset_agents.training.gspo_trainer import train_with_gspo | |
| logger.info("=" * 80) | |
| logger.info("๐ Fine-tuning Gemma 3 with GSPO") | |
| logger.info("=" * 80) | |
| logger.info(f"Model: {model_name}") | |
| logger.info(f"Task: {task}") | |
| logger.info(f"LoRA: {use_lora}") | |
| logger.info( | |
| f"Quantization: {'4-bit' if use_4bit else '8-bit' if use_8bit else 'None'}" | |
| ) | |
| logger.info(f"Output: {output_dir}") | |
| logger.info("=" * 80) | |
| # System prompts for different tasks | |
| system_prompts = { | |
| "customer_service": "You are a helpful and empathetic customer service representative. You assist customers with their inquiries professionally and efficiently.", | |
| "technical_support": "You are a knowledgeable technical support specialist. You help users troubleshoot technical issues with clear, detailed explanations.", | |
| "sales": "You are a friendly and persuasive sales representative. You help customers discover products that meet their needs.", | |
| } | |
| # Create agent | |
| logger.info("Initializing Gemma 3 agent...") | |
| agent_config = AgentConfig( | |
| model_name=model_name, | |
| system_prompt=system_prompts.get(task, system_prompts["customer_service"]), | |
| max_new_tokens=2048, | |
| ) | |
| agent = MultiTurnAgent(agent_config) | |
| await agent.initialize() | |
| logger.info("โ Agent initialized") | |
| # Create environment | |
| logger.info("Setting up environment...") | |
| if task in CONVERSATION_CONFIGS: | |
| env_config = CONVERSATION_CONFIGS[task].copy() | |
| else: | |
| env_config = CONVERSATION_CONFIGS["customer_service"].copy() | |
| environment = ConversationEnvironment(**env_config) | |
| logger.info( | |
| f"โ Environment configured with {len(environment.scenarios)} scenarios" | |
| ) | |
| # Create reward model | |
| logger.info("Initializing reward model...") | |
| reward_model = create_domain_reward(task) | |
| logger.info("โ Reward model initialized") | |
| # Get GSPO configuration | |
| logger.info("Creating GSPO configuration...") | |
| gspo_config = get_gemma3_config( | |
| model_name=model_name, | |
| task=task, | |
| use_lora=use_lora, | |
| use_4bit=use_4bit, | |
| use_8bit=use_8bit, | |
| output_dir=output_dir, | |
| ) | |
| # Enable W&B if requested | |
| if use_wandb: | |
| gspo_config.report_to = "wandb" | |
| gspo_config.wandb_project = wandb_project or f"gemma3-gspo-{task}" | |
| gspo_config.wandb_tags = ["gemma3", "gspo", task, model_name.split("/")[-1]] | |
| logger.info(f"โ W&B enabled: {gspo_config.wandb_project}") | |
| logger.info("โ GSPO configuration ready") | |
| logger.info(f" - Group size: {gspo_config.num_generations}") | |
| logger.info( | |
| f" - Clipping: [{gspo_config.clip_range_left}, {gspo_config.clip_range_right}]" | |
| ) | |
| logger.info(f" - Learning rate: {gspo_config.learning_rate}") | |
| logger.info(f" - Iterations: {gspo_config.num_outer_iterations}") | |
| # Train | |
| logger.info("\n" + "=" * 80) | |
| logger.info("Starting GSPO training...") | |
| logger.info("=" * 80 + "\n") | |
| trained_agent = await train_with_gspo( | |
| config=gspo_config, | |
| agent=agent, | |
| environment=environment, | |
| reward_model=reward_model, | |
| ) | |
| logger.info("\n" + "=" * 80) | |
| logger.info("โ Training completed!") | |
| logger.info("=" * 80) | |
| # Test the trained agent | |
| logger.info("\n" + "=" * 80) | |
| logger.info("Testing trained Gemma 3 model...") | |
| logger.info("=" * 80) | |
| test_queries = { | |
| "customer_service": "My order hasn't arrived yet and it's been 2 weeks. Can you help?", | |
| "technical_support": "The application crashes every time I try to export a file. What should I do?", | |
| "sales": "I'm looking for a cloud computing solution for my small business. What do you recommend?", | |
| } | |
| test_query = test_queries.get(task, test_queries["customer_service"]) | |
| messages = [{"role": "user", "content": test_query}] | |
| logger.info(f"\nUser: {test_query}") | |
| response = await trained_agent.generate_response(messages) | |
| logger.info(f"Gemma 3: {response}\n") | |
| logger.info("=" * 80) | |
| return trained_agent | |
| def main(): | |
| """Main entry point""" | |
| parser = argparse.ArgumentParser( | |
| description="Fine-tune Gemma 3 models with GSPO", | |
| formatter_class=argparse.RawDescriptionHelpFormatter, | |
| epilog=""" | |
| Examples: | |
| # Small model for testing | |
| python examples/finetune_gemma3_gspo.py --model google/gemma-2-2b-it | |
| # 9B model with LoRA | |
| python examples/finetune_gemma3_gspo.py --model google/gemma-2-9b-it --use-lora | |
| # 27B model with 4-bit quantization | |
| python examples/finetune_gemma3_gspo.py --model google/gemma-2-27b-it --use-4bit | |
| # Enable W&B logging | |
| python examples/finetune_gemma3_gspo.py --model google/gemma-2-9b-it --wandb --wandb-project my-project | |
| """, | |
| ) | |
| parser.add_argument( | |
| "--model", | |
| type=str, | |
| default="google/gemma-2-2b-it", | |
| help="Gemma 3 model name (e.g., google/gemma-2-9b-it)", | |
| ) | |
| parser.add_argument( | |
| "--task", | |
| type=str, | |
| default="customer_service", | |
| choices=["customer_service", "technical_support", "sales"], | |
| help="Task type", | |
| ) | |
| parser.add_argument( | |
| "--use-lora", | |
| action="store_true", | |
| default=True, | |
| help="Use LoRA for efficient fine-tuning (recommended)", | |
| ) | |
| parser.add_argument( | |
| "--no-lora", | |
| action="store_true", | |
| help="Disable LoRA (full fine-tuning)", | |
| ) | |
| parser.add_argument( | |
| "--use-4bit", | |
| action="store_true", | |
| help="Use 4-bit quantization (for large models)", | |
| ) | |
| parser.add_argument( | |
| "--use-8bit", | |
| action="store_true", | |
| help="Use 8-bit quantization", | |
| ) | |
| parser.add_argument( | |
| "--output-dir", | |
| type=str, | |
| default="./outputs/gemma3_gspo", | |
| help="Output directory for checkpoints", | |
| ) | |
| parser.add_argument( | |
| "--wandb", | |
| action="store_true", | |
| help="Enable Weights & Biases logging", | |
| ) | |
| parser.add_argument( | |
| "--wandb-project", | |
| type=str, | |
| help="W&B project name", | |
| ) | |
| args = parser.parse_args() | |
| # Handle no-lora flag | |
| use_lora = args.use_lora and not args.no_lora | |
| # Run training | |
| asyncio.run( | |
| finetune_gemma3( | |
| model_name=args.model, | |
| task=args.task, | |
| use_lora=use_lora, | |
| use_4bit=args.use_4bit, | |
| use_8bit=args.use_8bit, | |
| output_dir=args.output_dir, | |
| use_wandb=args.wandb, | |
| wandb_project=args.wandb_project, | |
| ) | |
| ) | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 12.9 kB
- Xet hash:
- 5acc94a0032480b9e4564d03a237c8e83fc0f43d5873bbd2026c2db44cc2c3b4
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.