Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /stateset_stateset-agents /stateset_agents /training /config.py
| """ | |
| Training configuration and profiles for GRPO Agent Framework | |
| This module provides training configurations that implement best practices | |
| and integrate seamlessly with HuggingFace and Weights & Biases. | |
| """ | |
| import json | |
| import math | |
| from dataclasses import dataclass | |
| from enum import Enum | |
| from pathlib import Path | |
| from typing import Any | |
| class TrainingProfile(Enum): | |
| """Pre-defined training profiles based on stability/performance trade-offs""" | |
| CONSERVATIVE = "conservative" # Maximum stability | |
| BALANCED = "balanced" # Default, good balance | |
| AGGRESSIVE = "aggressive" # Maximum performance | |
| EXPERIMENTAL = "experimental" # Cutting edge | |
| class TrainingConfig: | |
| """ | |
| Comprehensive training configuration compatible with HuggingFace patterns | |
| """ | |
| # Basic training parameters | |
| model_name: str = "gpt2" | |
| # Immutable Hugging Face commit/tag used for tokenizer, policy, reference | |
| # model, and rollout backend. Leave unset for normal exploratory use; | |
| # publication-grade benchmarks should always pin a full commit SHA. | |
| model_revision: str | None = None | |
| num_episodes: int = 1000 | |
| num_epochs: int = 1 | |
| learning_rate: float = 5e-6 | |
| # Optimization parameters (HuggingFace compatible) | |
| adam_beta1: float = 0.9 | |
| adam_beta2: float = 0.99 | |
| weight_decay: float = 0.01 | |
| max_grad_norm: float = 1.0 | |
| warmup_ratio: float = 0.1 | |
| lr_scheduler_type: str = "cosine" # "cosine", "linear", "constant" | |
| # Batch and generation settings | |
| per_device_train_batch_size: int = 1 | |
| # Aliases for compatibility with some tests/configs | |
| batch_size: int | None = None | |
| gradient_accumulation_steps: int = 4 | |
| num_generations: int = 16 # GRPO: trajectories per scenario | |
| generation_batch_size: int = 4 | |
| # Rollout/evaluation parallelism (requires environments that support clone()). | |
| rollout_concurrency: int = 1 | |
| # Evaluation settings | |
| eval_num_generations: int = 4 | |
| # Length constraints | |
| max_prompt_length: int = 512 | |
| max_completion_length: int = 512 | |
| # Alias used by some tests; not directly consumed by trainer but accepted | |
| max_steps_per_episode: int | None = None | |
| # Hardware optimization | |
| bf16: bool = True | |
| fp16: bool = False | |
| use_cpu: bool = False | |
| dataloader_num_workers: int = 0 | |
| dataloader_pin_memory: bool = True | |
| gradient_checkpointing: bool = False | |
| # Evaluation and checkpointing | |
| eval_steps: int = 50 | |
| save_steps: int = 100 | |
| logging_steps: int = 10 | |
| save_total_limit: int = 3 | |
| # Output configuration | |
| output_dir: str = "./outputs" | |
| run_name: str | None = None | |
| # Backwards-compat alias (some tests/configs use this name). | |
| wandb_run_name: str | None = None | |
| overwrite_output_dir: bool = True | |
| # Weights & Biases integration | |
| report_to: str = "wandb" # "wandb", "tensorboard", "none" | |
| wandb_project: str | None = None | |
| wandb_entity: str | None = None | |
| wandb_tags: list[str] | None = None | |
| wandb_notes: str | None = None | |
| # Training stability and monitoring | |
| auto_adjust: bool = True | |
| early_stopping: bool = False | |
| patience: int = 50 | |
| # GRPO specific parameters | |
| gamma: float = 0.99 # Discount factor for cumulative returns | |
| gae_lambda: float = 0.95 # GAE lambda for advantage estimation | |
| reward_clip: float = 10.0 | |
| advantage_normalization: bool = True | |
| baseline_type: str = "group_mean" # "group_mean", "global_mean" | |
| # Enhanced GRPO parameters from real-world implementation | |
| beta: float = 0.0 # KL penalty coefficient | |
| use_reference_model: bool = False # Whether to use reference model for KL | |
| clip_ratio: float = ( | |
| 0.2 # PPO ratio clipping when old log probs are available; else advantage clamp | |
| ) | |
| # The GRPO importance ratio is a per-token-MEAN (length-normalised) sequence | |
| # ratio, so it sits within ~1e-3 of 1.0 and the classic +/-0.2 token-level | |
| # PPO clip (`clip_ratio`) is meaningless for it — it never triggers. GSPO | |
| # clips this quantity at ~3e-4/4e-4 instead; see `gspo_config.py`. | |
| seq_clip_ratio: float = 3e-4 # Sequence-mean ratio clip (GSPO scale) | |
| value_clip: float = 0.2 # Value function clipping | |
| entropy_coef: float = 0.01 # Entropy bonus coefficient | |
| # Continual learning (replay + LwF + EWC) | |
| continual_strategy: str = "none" # "none", "replay", "lwf", "ewc", combos | |
| replay_buffer_size: int = 2000 | |
| replay_ratio: float = 0.5 | |
| replay_min_size: int = 100 | |
| replay_sampling: str = "uniform" # "uniform", "recent", "reward", "balanced" | |
| replay_storage: str = "reservoir" # "reservoir", "fifo" | |
| continual_kl_beta: float = 0.0 # KL penalty for LwF reference model | |
| ewc_lambda: float = 0.0 | |
| ewc_num_samples: int = 64 | |
| ewc_decay: float = 0.9 | |
| task_id_key: str = "task_id" | |
| # Data processing | |
| max_examples: int | None = None | |
| eval_split_size: float = 0.1 | |
| stratify_by_task: bool = True | |
| data_format: str = "jsonl" # "jsonl", "json", "csv" | |
| # Post-training evaluation | |
| run_post_eval: bool = True | |
| post_eval_samples: int = 5 | |
| post_eval_detailed: bool = True | |
| # Model optimization | |
| use_lora: bool = True | |
| lora_r: int = 8 | |
| lora_alpha: int = 16 | |
| lora_dropout: float = 0.05 | |
| lora_target_modules: list[str] | None = None | |
| # Generation settings for evaluation | |
| temperature: float = 0.7 | |
| top_p: float = 0.9 | |
| top_k: int = 50 | |
| repetition_penalty: float = 1.1 | |
| # vLLM configuration (5-20x faster generation) | |
| use_vllm: bool = False # Enable vLLM for generation | |
| vllm_gpu_memory_utilization: float = 0.85 # Fraction of GPU memory for vLLM | |
| vllm_tensor_parallel_size: int = 1 # Number of GPUs for tensor parallelism | |
| vllm_enable_prefix_caching: bool = True # Cache KV for repeated prefixes | |
| vllm_max_model_len: int | None = None # Max sequence length (auto if None) | |
| vllm_quantization: str | None = None # "awq", "gptq", or None | |
| vllm_enable_chunked_prefill: bool = True # Better memory efficiency | |
| # Advanced options | |
| seed: int = 42 | |
| remove_unused_columns: bool = False | |
| disable_tqdm: bool = False | |
| # Continual learning task schedule (optional) | |
| task_schedule: list[str] | None = None | |
| task_switch_steps: int = 0 | |
| resume_from_checkpoint: str | None = None | |
| # Policy objective selection. ``None`` keeps each trainer's native | |
| # objective (GRPO -> sequence-clipped GRPO, DAPO -> dapo, ...). Set to a | |
| # preset name from ``stateset_agents.training.objectives.OBJECTIVES`` | |
| # (grpo, dr_grpo, bnpo, dapo, gspo, gspo_token, gepo, rloo, | |
| # reinforce_pp_baseline, cispo, ppo) to train with that objective instead; | |
| # ``objective_overrides`` tweaks individual PolicyObjective fields, e.g. | |
| # ``{"clip_high": 0.3, "is_cap": 3.0}``. See docs/OBJECTIVES.md. | |
| objective: str | None = None | |
| objective_overrides: dict[str, Any] | None = None | |
| # Inner optimizer updates per rollout batch on the GRPO trainers' token | |
| # path (PPO/DAPO-style mini-epochs). 1 keeps the on-policy single update; | |
| # above 1 the old policy's per-token log-probs are frozen once and each | |
| # update re-evaluates the clipped objective against them. | |
| num_gradient_updates: int = 1 | |
| def __post_init__(self): | |
| # Map compatibility aliases | |
| if self.batch_size is not None and self.batch_size > 0: | |
| self.per_device_train_batch_size = int(self.batch_size) | |
| if self.wandb_run_name is not None and self.run_name is None: | |
| self.run_name = self.wandb_run_name | |
| def from_profile(cls, profile: TrainingProfile, **overrides) -> "TrainingConfig": | |
| """Create configuration from predefined profile""" | |
| base_config: dict[str, Any] = {} | |
| if profile == TrainingProfile.CONSERVATIVE: | |
| base_config = { | |
| "learning_rate": 1e-6, | |
| "num_generations": 32, | |
| "gradient_accumulation_steps": 8, | |
| "max_grad_norm": 0.5, | |
| "warmup_ratio": 0.2, | |
| "eval_steps": 25, | |
| "auto_adjust": True, | |
| "early_stopping": True, | |
| "patience": 30, | |
| } | |
| elif profile == TrainingProfile.BALANCED: | |
| base_config = { | |
| "learning_rate": 5e-6, | |
| "num_generations": 16, | |
| "gradient_accumulation_steps": 4, | |
| "max_grad_norm": 1.0, | |
| "warmup_ratio": 0.1, | |
| "eval_steps": 50, | |
| "auto_adjust": True, | |
| "early_stopping": False, | |
| "patience": 50, | |
| } | |
| elif profile == TrainingProfile.AGGRESSIVE: | |
| base_config = { | |
| "learning_rate": 1e-5, | |
| "num_generations": 8, | |
| "gradient_accumulation_steps": 2, | |
| "max_grad_norm": 2.0, | |
| "warmup_ratio": 0.05, | |
| "eval_steps": 100, | |
| "auto_adjust": False, | |
| "early_stopping": False, | |
| "patience": 100, | |
| } | |
| elif profile == TrainingProfile.EXPERIMENTAL: | |
| base_config = { | |
| "learning_rate": 2e-5, | |
| "num_generations": 12, | |
| "gradient_accumulation_steps": 1, | |
| "max_grad_norm": 1.5, | |
| "warmup_ratio": 0.0, | |
| "eval_steps": 20, | |
| "auto_adjust": True, | |
| "early_stopping": True, | |
| "patience": 20, | |
| "lr_scheduler_type": "linear", | |
| } | |
| # Apply overrides | |
| base_config.update(overrides) | |
| return cls(**base_config) | |
| def to_dict(self) -> dict[str, Any]: | |
| """Convert to dictionary for serialization""" | |
| return { | |
| field.name: getattr(self, field.name) | |
| for field in self.__dataclass_fields__.values() | |
| } | |
| def from_dict(cls, config_dict: dict[str, Any]) -> "TrainingConfig": | |
| """Create from dictionary""" | |
| return cls(**config_dict) | |
| def save(self, path: str): | |
| """Save configuration to file""" | |
| config_dict = self.to_dict() | |
| # Convert enum values to strings | |
| if isinstance(config_dict.get("wandb_tags"), list): | |
| # Ensure all tags are strings | |
| config_dict["wandb_tags"] = [str(tag) for tag in config_dict["wandb_tags"]] | |
| with open(path, "w", encoding="utf-8") as f: | |
| json.dump(config_dict, f, indent=2, default=str) | |
| def load(cls, path: str) -> "TrainingConfig": | |
| """Load configuration from file""" | |
| with open(path) as f: | |
| config_dict = json.load(f) | |
| return cls.from_dict(config_dict) | |
| def validate(self) -> list[str]: | |
| """Validate configuration and return warnings""" | |
| warnings = [] | |
| # Learning rate checks | |
| if self.learning_rate > 1e-4: | |
| warnings.append( | |
| f"Learning rate {self.learning_rate} is very high, consider reducing" | |
| ) | |
| if self.learning_rate < 1e-7: | |
| warnings.append( | |
| f"Learning rate {self.learning_rate} is very low, training may be slow" | |
| ) | |
| # Batch size checks | |
| effective_batch_size = ( | |
| self.per_device_train_batch_size * self.gradient_accumulation_steps | |
| ) | |
| if effective_batch_size < 4: | |
| warnings.append( | |
| f"Effective batch size {effective_batch_size} is small, may cause instability" | |
| ) | |
| # Generation checks | |
| if self.num_generations < 4: | |
| warnings.append( | |
| f"num_generations {self.num_generations} is low for GRPO, consider increasing" | |
| ) | |
| # Hardware checks | |
| if self.bf16 and self.fp16: | |
| warnings.append("Both bf16 and fp16 enabled, bf16 will take precedence") | |
| # W&B checks | |
| if self.report_to == "wandb" and not self.wandb_project: | |
| warnings.append("W&B reporting enabled but no project specified") | |
| # Objective selection checks (torch-free: only the preset names). | |
| if self.objective is not None: | |
| from .objectives import OBJECTIVES | |
| if self.objective not in OBJECTIVES: | |
| warnings.append( | |
| f"objective '{self.objective}' is not a known preset; " | |
| f"expected one of: {', '.join(sorted(OBJECTIVES))}" | |
| ) | |
| if int(self.num_gradient_updates) < 1: | |
| warnings.append("num_gradient_updates must be >= 1") | |
| # Continual learning checks | |
| valid_strategies = { | |
| "none", | |
| "replay", | |
| "lwf", | |
| "ewc", | |
| "replay_lwf", | |
| "replay_ewc", | |
| "replay_lwf_ewc", | |
| } | |
| if self.continual_strategy not in valid_strategies: | |
| warnings.append( | |
| f"continual_strategy '{self.continual_strategy}' not recognized" | |
| ) | |
| if self.replay_sampling not in {"uniform", "recent", "reward", "balanced"}: | |
| warnings.append(f"replay_sampling '{self.replay_sampling}' not recognized") | |
| if self.replay_ratio < 0: | |
| warnings.append("replay_ratio must be non-negative") | |
| if self.replay_ratio > 1.0: | |
| warnings.append("replay_ratio > 1.0 may overweight replay data") | |
| if self.replay_buffer_size < 0: | |
| warnings.append("replay_buffer_size must be non-negative") | |
| if self.replay_min_size > self.replay_buffer_size: | |
| warnings.append("replay_min_size exceeds replay_buffer_size") | |
| if self.ewc_lambda > 0 and self.ewc_num_samples <= 0: | |
| warnings.append("ewc_num_samples must be > 0 when ewc_lambda > 0") | |
| if self.ewc_decay < 0.0 or self.ewc_decay > 1.0: | |
| warnings.append("ewc_decay should be between 0 and 1 for stable updates") | |
| if self.continual_kl_beta > 0 and "lwf" not in self.continual_strategy: | |
| warnings.append( | |
| "continual_kl_beta set but continual_strategy does not include lwf" | |
| ) | |
| if self.task_schedule and self.task_switch_steps <= 0: | |
| warnings.append( | |
| "task_schedule set but task_switch_steps is <= 0; tasks will not advance" | |
| ) | |
| if self.eval_split_size < 0.0 or self.eval_split_size > 1.0: | |
| warnings.append("eval_split_size should be between 0 and 1") | |
| if self.resume_from_checkpoint: | |
| try: | |
| if not Path(self.resume_from_checkpoint).exists(): | |
| warnings.append( | |
| f"resume_from_checkpoint '{self.resume_from_checkpoint}' does not exist" | |
| ) | |
| except (OSError, ValueError): | |
| warnings.append("resume_from_checkpoint path could not be validated") | |
| return warnings | |
| def get_effective_batch_size(self) -> int: | |
| """Get effective batch size""" | |
| return self.per_device_train_batch_size * self.gradient_accumulation_steps | |
| def get_total_steps(self, num_episodes: int | None = None) -> int: | |
| """Calculate total training steps""" | |
| episodes = num_episodes or self.num_episodes | |
| grad_steps = max(1, int(self.gradient_accumulation_steps or 1)) | |
| return math.ceil(episodes / grad_steps) | |
| def get_warmup_steps(self, total_steps: int | None = None) -> int: | |
| """Calculate warmup steps""" | |
| if total_steps is None: | |
| total_steps = self.get_total_steps() | |
| return int(total_steps * self.warmup_ratio) | |
| # Pre-defined configurations for common use cases | |
| CUSTOMER_SERVICE_CONFIG = TrainingConfig( | |
| num_episodes=800, | |
| learning_rate=3e-6, | |
| num_generations=12, | |
| max_completion_length=256, | |
| eval_steps=40, | |
| wandb_tags=["customer-service", "conversation"], | |
| wandb_notes="Customer service agent training with politeness and helpfulness focus", | |
| ) | |
| TUTORING_CONFIG = TrainingConfig( | |
| num_episodes=1200, | |
| learning_rate=5e-6, | |
| num_generations=16, | |
| max_completion_length=512, | |
| eval_steps=60, | |
| patience=60, | |
| wandb_tags=["tutoring", "education"], | |
| wandb_notes="Educational tutoring agent with step-by-step explanation focus", | |
| ) | |
| CREATIVE_WRITING_CONFIG = TrainingConfig( | |
| num_episodes=600, | |
| learning_rate=8e-6, | |
| num_generations=8, | |
| max_completion_length=1024, | |
| eval_steps=30, | |
| lr_scheduler_type="linear", | |
| wandb_tags=["creative-writing", "generation"], | |
| wandb_notes="Creative writing assistant with imagination and storytelling focus", | |
| ) | |
| RESEARCH_ASSISTANT_CONFIG = TrainingConfig( | |
| num_episodes=1000, | |
| learning_rate=4e-6, | |
| num_generations=20, | |
| max_completion_length=768, | |
| eval_steps=50, | |
| early_stopping=True, | |
| patience=40, | |
| wandb_tags=["research", "analysis"], | |
| wandb_notes="Research assistant with factual accuracy and citation focus", | |
| ) | |
| def get_config_for_task(task_type: str, **overrides) -> TrainingConfig: | |
| """Get pre-configured setup for common tasks""" | |
| task_configs = { | |
| "customer_service": CUSTOMER_SERVICE_CONFIG, | |
| "tutoring": TUTORING_CONFIG, | |
| "creative_writing": CREATIVE_WRITING_CONFIG, | |
| "research_assistant": RESEARCH_ASSISTANT_CONFIG, | |
| } | |
| if task_type in task_configs: | |
| base_config = task_configs[task_type] | |
| # Create new config with overrides | |
| config_dict = base_config.to_dict() | |
| config_dict.update(overrides) | |
| return TrainingConfig.from_dict(config_dict) | |
| else: | |
| # Return default balanced config with overrides | |
| return TrainingConfig.from_profile(TrainingProfile.BALANCED, **overrides) | |
| def optimize_config_for_hardware( | |
| config: TrainingConfig, gpu_memory_gb: float | None = None, num_gpus: int = 1 | |
| ) -> TrainingConfig: | |
| """Optimize configuration based on available hardware""" | |
| optimized_dict = config.to_dict() | |
| # GPU memory optimizations | |
| if gpu_memory_gb is not None: | |
| if gpu_memory_gb < 8: | |
| # Small GPU optimizations | |
| optimized_dict.update( | |
| { | |
| "per_device_train_batch_size": 1, | |
| "gradient_accumulation_steps": 8, | |
| "num_generations": 8, | |
| "max_completion_length": 256, | |
| "bf16": True, | |
| "dataloader_num_workers": 0, | |
| } | |
| ) | |
| elif gpu_memory_gb < 16: | |
| # Medium GPU optimizations | |
| optimized_dict.update( | |
| { | |
| "per_device_train_batch_size": 1, | |
| "gradient_accumulation_steps": 4, | |
| "num_generations": 12, | |
| "max_completion_length": 512, | |
| "bf16": True, | |
| "dataloader_num_workers": 2, | |
| } | |
| ) | |
| else: | |
| # Large GPU optimizations | |
| optimized_dict.update( | |
| { | |
| "per_device_train_batch_size": 2, | |
| "gradient_accumulation_steps": 4, | |
| "num_generations": 16, | |
| "max_completion_length": 768, | |
| "bf16": True, | |
| "dataloader_num_workers": 4, | |
| } | |
| ) | |
| # Multi-GPU optimizations | |
| if num_gpus > 1: | |
| optimized_dict["per_device_train_batch_size"] = max( | |
| 1, optimized_dict["per_device_train_batch_size"] // num_gpus | |
| ) | |
| optimized_dict["dataloader_num_workers"] = min( | |
| 8, optimized_dict["dataloader_num_workers"] * num_gpus | |
| ) | |
| return TrainingConfig.from_dict(optimized_dict) | |
| def create_config_from_args( | |
| model_name: str, task_type: str = "general", profile: str = "balanced", **kwargs | |
| ) -> TrainingConfig: | |
| """Create configuration from command line arguments""" | |
| # Start with task-specific config or profile | |
| if task_type != "general": | |
| config = get_config_for_task(task_type) | |
| else: | |
| config = TrainingConfig.from_profile(TrainingProfile(profile)) | |
| # Apply model-specific optimizations | |
| if "large" in model_name.lower() or "13b" in model_name.lower(): | |
| # Large model optimizations | |
| config_dict = config.to_dict() | |
| config_dict.update( | |
| { | |
| "per_device_train_batch_size": 1, | |
| "gradient_accumulation_steps": 8, | |
| "bf16": True, | |
| "max_completion_length": min(512, config_dict["max_completion_length"]), | |
| } | |
| ) | |
| config = TrainingConfig.from_dict(config_dict) | |
| # Apply user overrides | |
| if kwargs: | |
| config_dict = config.to_dict() | |
| config_dict.update(kwargs) | |
| config = TrainingConfig.from_dict(config_dict) | |
| return config | |
| # Example usage and testing | |
| def example_config_usage(): | |
| """Example of how to use training configurations""" | |
| # Create from profile | |
| conservative_config = TrainingConfig.from_profile(TrainingProfile.CONSERVATIVE) | |
| print(f"Conservative LR: {conservative_config.learning_rate}") | |
| # Create for specific task | |
| customer_config = get_config_for_task("customer_service", num_episodes=1200) | |
| print(f"Customer service episodes: {customer_config.num_episodes}") | |
| # Optimize for hardware | |
| optimized_config = optimize_config_for_hardware( | |
| customer_config, gpu_memory_gb=12.0, num_gpus=2 | |
| ) | |
| print(f"Optimized batch size: {optimized_config.per_device_train_batch_size}") | |
| # Validate configuration | |
| warnings = optimized_config.validate() | |
| if warnings: | |
| print("Configuration warnings:") | |
| for warning in warnings: | |
| print(f" - {warning}") | |
| # Save and load | |
| optimized_config.save("config.json") | |
| loaded_config = TrainingConfig.load("config.json") | |
| print(f"Loaded config learning rate: {loaded_config.learning_rate}") | |
| if __name__ == "__main__": | |
| example_config_usage() | |
| def cuda_is_available() -> bool: | |
| """Return whether a CUDA device is usable, without importing torch eagerly. | |
| ``torch`` is an optional heavy dependency and configuration modules must | |
| stay importable without it (see ``tests/unit/test_torch_import_policy.py``), | |
| so the import happens inside this helper rather than at module level. | |
| """ | |
| try: | |
| import torch | |
| except ImportError: # pragma: no cover - torch is installed in CI | |
| return False | |
| return bool(torch.cuda.is_available()) | |
Xet Storage Details
- Size:
- 22.5 kB
- Xet hash:
- 78f58817965d578dd97731038e515bc4ff8dae22361ddcdb701337ac4445689e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.