Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /stateset_stateset-agents /examples /finetune_qwen3_gspo.py
| """ | |
| Fine-tune Qwen Models with GSPO | |
| This script demonstrates text-only GSPO post-training for Qwen models in | |
| StateSet Agents. | |
| Recommended starting checkpoint for new post-training runs: | |
| - Qwen/Qwen3.5-0.8B-Base | |
| For a dedicated first-run path for Qwen3.5-0.8B, prefer | |
| `examples/finetune_qwen3_5_0_8b_gspo.py`. | |
| Other supported examples: | |
| - Qwen/Qwen3.5-0.8B | |
| - Qwen/Qwen3.5-27B | |
| - Qwen/Qwen2.5-0.5B | |
| - Qwen/Qwen2.5-1.5B | |
| - Qwen/Qwen2.5-3B | |
| - Qwen/Qwen2.5-7B | |
| - Qwen/Qwen2.5-14B | |
| - Qwen/Qwen2.5-32B | |
| - Qwen/Qwen2.5-72B | |
| Usage: | |
| # Recommended first run | |
| python examples/finetune_qwen3_gspo.py --model Qwen/Qwen3.5-0.8B-Base --task customer_service | |
| # 0.8B on a smaller GPU | |
| python examples/finetune_qwen3_gspo.py --model Qwen/Qwen3.5-0.8B-Base --task customer_service --use-4bit | |
| # Qwen3.5-27B tuned for vLLM-backed serving | |
| python examples/finetune_qwen3_gspo.py --model Qwen/Qwen3.5-27B --task customer_service --use-vllm --export-merged --language-model-only | |
| # Larger Qwen model with LoRA | |
| python examples/finetune_qwen3_gspo.py --model Qwen/Qwen2.5-7B --task technical_support --use-lora | |
| """ | |
| # ruff: noqa: E402 | |
| import argparse | |
| import asyncio | |
| import logging | |
| import os | |
| import sys | |
| from pathlib import Path | |
| REPO_ROOT = str(Path(__file__).resolve().parents[1]) | |
| if REPO_ROOT not in sys.path: | |
| sys.path.insert(0, REPO_ROOT) | |
| from stateset_agents.training.qwen3_5_starter import ( | |
| get_qwen3_5_config, | |
| get_qwen3_5_gspo_config, | |
| ) | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", | |
| ) | |
| logger = logging.getLogger(__name__) | |
| QWEN35_27B_MODEL = "Qwen/Qwen3.5-27B" | |
| def get_qwen3_serving_recommendations( | |
| *, | |
| language_model_only: bool = False, | |
| enable_auto_tool_choice: bool = True, | |
| ) -> dict[str, object]: | |
| """Return the recommended vLLM settings for Qwen3.5-27B serving.""" | |
| return { | |
| "tensor_parallel_size": 8, | |
| "max_model_len": 262144, | |
| "trust_remote_code": True, | |
| "reasoning_parser": "qwen3", | |
| "tool_call_parser": "qwen3_coder" if enable_auto_tool_choice else None, | |
| "enable_auto_tool_choice": enable_auto_tool_choice, | |
| "language_model_only": language_model_only, | |
| "gpu_memory_utilization": 0.90, | |
| } | |
| def get_qwen3_config( | |
| model_name: str, | |
| task: str, | |
| use_lora: bool = True, | |
| use_4bit: bool = False, | |
| use_8bit: bool = False, | |
| use_vllm: bool = False, | |
| output_dir: str = "./outputs/qwen3_5_gspo", | |
| ): | |
| """ | |
| Get optimized GSPO configuration for Qwen3 models. | |
| These configurations are based on the original GSPO paper | |
| and optimized for different model sizes. | |
| """ | |
| 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 "0.5B" in model_name or "0.5b" in model_name: | |
| # Small model config (0.5B) | |
| 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, | |
| use_vllm=use_vllm, | |
| # Generation | |
| max_prompt_length=512, | |
| max_completion_length=512, | |
| temperature=0.7, | |
| # Output | |
| output_dir=output_dir, | |
| save_steps=10, | |
| logging_steps=1, | |
| ) | |
| elif "0.8B" in model_name or "0.8b" in model_name: | |
| # Reuse the dedicated Qwen3.5-0.8B helper for the generic family script. | |
| qwen35_config = get_qwen3_5_config( | |
| model_name=model_name, | |
| task=task, | |
| use_lora=use_lora, | |
| use_4bit=use_4bit, | |
| use_8bit=use_8bit, | |
| output_dir=output_dir, | |
| num_outer_iterations=100, | |
| generations_per_iteration=40, | |
| save_steps_every=10, | |
| ) | |
| config = get_qwen3_5_gspo_config(qwen35_config, base_config=base_config) | |
| config.use_vllm = use_vllm | |
| elif ( | |
| "1.5B" in model_name | |
| or "1.5b" in model_name | |
| or "3B" in model_name | |
| or "3b" in model_name | |
| ): | |
| # Medium model config | |
| config = GSPOConfig.from_training_config( | |
| base_config, | |
| num_generations=4, | |
| clip_range_left=2.5e-4, | |
| clip_range_right=3.5e-4, | |
| learning_rate=8e-6, | |
| num_outer_iterations=100, | |
| generations_per_iteration=40, | |
| per_device_train_batch_size=2, | |
| gradient_accumulation_steps=4, | |
| use_lora=use_lora, | |
| lora_r=32, | |
| lora_alpha=64, | |
| 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, | |
| use_8bit=use_8bit, | |
| use_vllm=use_vllm, | |
| max_prompt_length=1024, | |
| max_completion_length=1024, | |
| temperature=0.7, | |
| output_dir=output_dir, | |
| save_steps=10, | |
| logging_steps=1, | |
| ) | |
| elif "qwen3.5-27b" in model_name.lower() or "27B" in model_name: | |
| # Qwen3.5-27B is best handled with LoRA plus vLLM-backed generation. | |
| config = GSPOConfig.from_training_config( | |
| base_config, | |
| num_generations=8, | |
| clip_range_left=1.5e-4, | |
| clip_range_right=2.5e-4, | |
| beta=0.01, | |
| learning_rate=3e-6, | |
| num_outer_iterations=50, | |
| generations_per_iteration=20, | |
| per_device_train_batch_size=1, | |
| gradient_accumulation_steps=16, | |
| use_lora=use_lora, | |
| 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, | |
| use_8bit=use_8bit if not use_4bit else False, | |
| use_vllm=use_vllm, | |
| use_reference_model=True, | |
| max_prompt_length=4096, | |
| max_completion_length=2048, | |
| temperature=1.0, | |
| top_p=0.95, | |
| output_dir=output_dir, | |
| save_steps=5, | |
| logging_steps=1, | |
| save_total_limit=3, | |
| ) | |
| elif ( | |
| "7B" in model_name | |
| or "7b" in model_name | |
| or "14B" in model_name | |
| or "14b" in model_name | |
| ): | |
| # Large model config | |
| config = GSPOConfig.from_training_config( | |
| base_config, | |
| num_generations=6, # Larger group for stability | |
| 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 large models | |
| 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 quantization for large models | |
| use_8bit=use_8bit if not use_4bit else False, | |
| use_vllm=use_vllm, | |
| max_prompt_length=2048, | |
| max_completion_length=2048, | |
| temperature=0.7, | |
| output_dir=output_dir, | |
| save_steps=5, | |
| logging_steps=1, | |
| ) | |
| else: | |
| # Very large model config (32B, 72B, MoE) | |
| config = GSPOConfig.from_training_config( | |
| base_config, | |
| num_generations=8, # Even larger group for MoE stability | |
| 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 very large models | |
| use_vllm=use_vllm, | |
| 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_qwen3( | |
| model_name: str, | |
| task: str = "customer_service", | |
| use_lora: bool = True, | |
| use_4bit: bool = False, | |
| use_8bit: bool = False, | |
| use_vllm: bool = False, | |
| export_merged: bool = False, | |
| language_model_only: bool = False, | |
| enable_auto_tool_choice: bool = True, | |
| output_dir: str = "./outputs/qwen3_5_gspo", | |
| use_wandb: bool = False, | |
| wandb_project: str | None = None, | |
| ): | |
| """ | |
| Fine-tune a Qwen model using GSPO. | |
| Args: | |
| model_name: Qwen model name (for post-training, prefer "Qwen/Qwen3.5-0.8B-Base") | |
| 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 | |
| use_vllm: Enable vLLM for faster generation during training | |
| export_merged: Merge LoRA adapters into a standalone serving checkpoint | |
| language_model_only: Skip the vision encoder at inference time to free KV cache | |
| enable_auto_tool_choice: Enable vLLM auto tool choice for tool-calling agents | |
| 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 | |
| from stateset_agents.training.serving_artifacts import ( | |
| export_merged_model_for_serving, | |
| write_serving_manifest, | |
| ) | |
| logger.info("=" * 80) | |
| logger.info("๐ Fine-tuning Qwen3.5 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"vLLM: {use_vllm}") | |
| logger.info(f"Export merged: {export_merged}") | |
| logger.info(f"Output: {output_dir}") | |
| logger.info("=" * 80) | |
| # System prompts for different tasks | |
| system_prompts = { | |
| "customer_service": "You are Qwen, a helpful and empathetic customer service representative created by Alibaba Cloud. You assist customers with their inquiries professionally and efficiently.", | |
| "technical_support": "You are Qwen, a knowledgeable technical support specialist created by Alibaba Cloud. You help users troubleshoot technical issues with clear, detailed explanations.", | |
| "sales": "You are Qwen, a friendly and persuasive sales representative created by Alibaba Cloud. You help customers discover products that meet their needs.", | |
| } | |
| # Create agent | |
| logger.info("Initializing Qwen3.5 agent...") | |
| agent_config = AgentConfig( | |
| model_name=model_name, | |
| system_prompt=system_prompts.get(task, system_prompts["customer_service"]), | |
| max_new_tokens=2048 if "27b" in model_name.lower() else 1024, | |
| trust_remote_code=True, | |
| attn_implementation="sdpa", | |
| ) | |
| 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_qwen3_config( | |
| model_name=model_name, | |
| task=task, | |
| use_lora=use_lora, | |
| use_4bit=use_4bit, | |
| use_8bit=use_8bit, | |
| use_vllm=use_vllm, | |
| 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"qwen3_5-gspo-{task}" | |
| gspo_config.wandb_tags = ["qwen3.5", "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}") | |
| logger.info(f" - vLLM backend: {gspo_config.use_vllm}") | |
| # 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, | |
| ) | |
| serving_recommendations = {} | |
| if "27b" in model_name.lower(): | |
| serving_recommendations = get_qwen3_serving_recommendations( | |
| language_model_only=language_model_only, | |
| enable_auto_tool_choice=enable_auto_tool_choice, | |
| ) | |
| merged_dir = None | |
| if export_merged and use_lora: | |
| merged_dir = export_merged_model_for_serving( | |
| base_model_name=model_name, | |
| adapter_dir=output_dir, | |
| output_dir=os.path.join(output_dir, "merged"), | |
| ) | |
| elif export_merged: | |
| logger.warning( | |
| "Skipping merged export because LoRA is disabled; no adapter weights were produced." | |
| ) | |
| write_serving_manifest( | |
| output_dir, | |
| model_name, | |
| use_lora=use_lora, | |
| use_vllm=use_vllm, | |
| merged_model_dir=merged_dir, | |
| recommended=serving_recommendations, | |
| ) | |
| logger.info("\n" + "=" * 80) | |
| logger.info("โ Training completed!") | |
| logger.info("=" * 80) | |
| # Test the trained agent | |
| logger.info("\n" + "=" * 80) | |
| logger.info("Testing trained Qwen3.5 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"Qwen3.5: {response}\n") | |
| logger.info("=" * 80) | |
| return trained_agent | |
| def main(): | |
| """Main entry point""" | |
| parser = argparse.ArgumentParser( | |
| description="Fine-tune Qwen models with GSPO", | |
| formatter_class=argparse.RawDescriptionHelpFormatter, | |
| epilog=""" | |
| Examples: | |
| # Recommended first run | |
| python examples/finetune_qwen3_gspo.py --model Qwen/Qwen3.5-0.8B-Base | |
| # 0.8B model with 4-bit quantization | |
| python examples/finetune_qwen3_gspo.py --model Qwen/Qwen3.5-0.8B-Base --use-4bit | |
| # Qwen3.5-27B with vLLM-backed training and merged serving export | |
| python examples/finetune_qwen3_gspo.py --model Qwen/Qwen3.5-27B --use-vllm --export-merged --language-model-only | |
| # 7B model with LoRA | |
| python examples/finetune_qwen3_gspo.py --model Qwen/Qwen2.5-7B --use-lora | |
| # Enable W&B logging | |
| python examples/finetune_qwen3_gspo.py --model Qwen/Qwen3.5-0.8B-Base --wandb --wandb-project my-project | |
| """, | |
| ) | |
| parser.add_argument( | |
| "--model", | |
| type=str, | |
| default="Qwen/Qwen3.5-0.8B-Base", | |
| help="Qwen model name (for post-training, prefer Qwen/Qwen3.5-0.8B-Base)", | |
| ) | |
| 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 (helpful on smaller GPUs)", | |
| ) | |
| parser.add_argument( | |
| "--use-8bit", | |
| action="store_true", | |
| help="Use 8-bit quantization", | |
| ) | |
| parser.add_argument( | |
| "--use-vllm", | |
| action="store_true", | |
| help="Enable vLLM for faster generation during training", | |
| ) | |
| parser.add_argument( | |
| "--export-merged", | |
| action="store_true", | |
| help="Merge LoRA adapters into a standalone checkpoint for vLLM serving", | |
| ) | |
| parser.add_argument( | |
| "--language-model-only", | |
| action="store_true", | |
| help="Recommend text-only vLLM serving mode for multimodal Qwen3.5 checkpoints", | |
| ) | |
| parser.add_argument( | |
| "--disable-auto-tool-choice", | |
| action="store_true", | |
| help="Do not recommend vLLM auto tool choice in the serving manifest", | |
| ) | |
| parser.add_argument( | |
| "--output-dir", | |
| type=str, | |
| default="./outputs/qwen3_5_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_qwen3( | |
| model_name=args.model, | |
| task=args.task, | |
| use_lora=use_lora, | |
| use_4bit=args.use_4bit, | |
| use_8bit=args.use_8bit, | |
| use_vllm=args.use_vllm, | |
| export_merged=args.export_merged, | |
| language_model_only=args.language_model_only, | |
| enable_auto_tool_choice=not args.disable_auto_tool_choice, | |
| output_dir=args.output_dir, | |
| use_wandb=args.wandb, | |
| wandb_project=args.wandb_project, | |
| ) | |
| ) | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 20.6 kB
- Xet hash:
- be9d27f54955ea23f1da3e052f0cdbdbdeda1c080e62e8a0095b99630f130464
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.