# SPDX-License-Identifier: Apache-2.0 import argparse import json import math import os import re import warnings from collections.abc import Mapping from dataclasses import MISSING as dataclass_missing from dataclasses import asdict, dataclass, field, fields from enum import Enum from pathlib import Path from typing import TYPE_CHECKING, Any, ClassVar, Literal, TypeVar import uvloop import yaml from hydra import compose as hydra_compose from hydra import initialize as hydra_init from hydra.core.global_hydra import GlobalHydra from omegaconf import MISSING, DictConfig, OmegaConf from areal.engine.fsdp_utils.attn_impl import ( BUILTIN_ATTN_IMPLS, get_attn_impl_validation_error, is_valid_attn_impl, ) from areal.utils import logging, name_resolve, pkg_version from areal.utils.config_utils import redact_sensitive_config from areal.utils.constants import ( PROX_LOGP_METHOD_RECOMPUTE, PROX_LOGP_METHODS_ALL, ) from areal.utils.seqpack import PACKING_ALGORITHMS if TYPE_CHECKING: from transformers import PreTrainedTokenizerFast _LossReduction = Literal[ "token_mean", "seq_mean", "prompt_mean", "constant", "turn_mean" ] else: # OmegaConf structured configs reject Literal annotations at runtime. # TODO(agent): use the Literal directly when OmegaConf supports it. _LossReduction = str uvloop.install() logger = logging.getLogger("CLIArgs") ConfigT = TypeVar("ConfigT") @dataclass class NormConfig: """Configuration for reward/advantage normalization.""" mean_level: str | None = field( default="batch", metadata={ "help": "Mean level for normalization. None for no mean normalization.", "choices": ["batch", "group", None], }, ) mean_leave1out: bool = field( default=False, metadata={"help": "Whether to use leave-one-out average."}, ) std_level: str | None = field( default="batch", metadata={ "help": "Standard deviation level for normalization. None for no std normalization.", "choices": ["batch", "group", None], }, ) std_unbiased: bool = field( default=True, metadata={ "help": "Whether to use unbiased standard deviation computation. Defaults to True (changed from False in v0.3.4)." }, ) eps: float = field( default=1e-5, metadata={ "help": "The eps when dividing by standard deviation to avoid numerical issues." }, ) group_size: int = field( default=1, metadata={"help": "Group size for group-level normalization"} ) @property def uses_group_statistics(self) -> bool: """Whether normalization derives statistics from prompt groups.""" return self.mean_level == "group" or self.std_level == "group" def __post_init__(self): """Validate normalization configuration.""" valid_levels = {"batch", "group", None} if self.mean_level not in valid_levels: raise ValueError( f"mean_level must be 'batch', 'group' or None, got {self.mean_level}" ) if self.std_level not in valid_levels: raise ValueError( f"std_level must be 'batch', 'group', or None, got {self.std_level}" ) if ( self.mean_level == "group" or self.std_level == "group" ) and self.group_size < 1: raise ValueError( f"group_size must be a positive integer when using group normalization, got {self.group_size}" ) @dataclass class MicroBatchSpec: """Specification for splitting micro-batches during training.""" n_mbs: int | None = field( default=1, metadata={ "help": "Number of micro-batches (or minimum number if max_tokens_per_mb is set). Used when max_tokens_per_mb is None or as minimum count", }, ) granularity: int = field( default=1, metadata={ "help": "Granularity of each micro-batch. Adjacent sequences are grouped by this size when dividing microbatches.", }, ) max_tokens_per_mb: int | None = field( default=None, metadata={ "help": "Maximum tokens per micro-batch for each forward pass. When set, n_mbs becomes the minimum number of micro-batches.", }, ) n_mbs_divisor: int = field( default=1, metadata={ "help": "Divisor for the number of micro-batches. The final number of micro-batches will be adjusted to be divisible by this value.", }, ) packing_algorithm: str = field( default="ffd", metadata={ "help": ( "Sequence packing algorithm for micro-batch allocation. " "Supported values: 'ffd' (First Fit Decreasing, default), " "'kk' (Karmarkar-Karp, better balance but slightly slower). " "KK is recommended when workload balance across DP ranks is " "critical (e.g., large-scale RL training with variable-length sequences)." ), "choices": ["ffd", "kk"], }, ) def __post_init__(self): """Validate packing algorithm configuration.""" if self.packing_algorithm not in PACKING_ALGORITHMS: raise ValueError( f"packing_algorithm must be one of {sorted(PACKING_ALGORITHMS)}, " f"got '{self.packing_algorithm}'" ) @classmethod def new(cls, mb_spec: "MicroBatchSpec", **kwargs): """Create new spec with updated fields while maintaining Omegaconf compatibility.""" fields = dict( n_mbs=mb_spec.n_mbs, granularity=mb_spec.granularity, max_tokens_per_mb=mb_spec.max_tokens_per_mb, n_mbs_divisor=mb_spec.n_mbs_divisor, packing_algorithm=mb_spec.packing_algorithm, ) fields.update(kwargs) return cls(**fields) @dataclass class GenerationHyperparameters: """Controls text generation behavior for rollout.""" n_samples: int = field( default=1, metadata={"help": "Number of sequences to generate per prompt."} ) max_new_tokens: int = field( default=16384, metadata={"help": "Maximum number of tokens to generate."} ) min_new_tokens: int = field( default=0, metadata={"help": "Minimum number of tokens to generate."} ) max_tokens: int = field( default=32768, metadata={ "help": "Maximum number of tokens including prompt and generated tokens." }, ) greedy: bool = field( default=False, metadata={"help": "Whether to use greedy decoding (max probability)."}, ) top_p: float = field( default=1.0, metadata={"help": "Nucleus sampling probability threshold (0.0, 1.0]."}, ) top_k: int = field( default=int(1e8), metadata={"help": "Number of highest probability tokens to consider."}, ) temperature: float = field( default=1.0, metadata={"help": "Sampling temperature. Higher values increase diversity."}, ) stop_token_ids: list[int] = field( default_factory=list, metadata={"help": "Stop generation when encountering these token IDs."}, ) ignore_eos: bool = field( default=False, metadata={"help": "Do not stop generation when EOS is encountered."}, ) skip_special_tokens: bool = field( default=True, metadata={"help": "Skip special tokens when decoding/displaying outputs."}, ) stop: list[str] | None = field( default=None, metadata={ "help": "One or multiple stop words. Generation will stop if one of these words is sampled." }, ) frequency_penalty: float = field( default=0.0, metadata={ "help": ( "Penalizes tokens based on their frequency in generation so far. " "Must be between -2 and 2 where negative numbers encourage repetition." ) }, ) seed: int | None = field( default=None, metadata={ "help": "Per-request sampling seed sent to the inference backend. Leave " "unset for grouped deterministic rollouts so each sample receives a " "stable, distinct derived seed." }, ) lora_name: str = field( default="default_lora", metadata={"help": "Lora name to be used for this generation."}, ) use_beam_search: bool = field( default=False, metadata={ "help": "Enable beam search in the vLLM engine. When enabled, sampling parameters like temperature, top-p, and top-k are auto ignored." }, ) reward_normalization: bool = field( default=False, metadata={ "help": ( "If True, apply per-prompt reward normalization across the " "n_samples rollouts of the same prompt inside " "grouped rollout workflows. Only affects " "InteractionWithTokenLogpReward workflows such as SWE agent " "workflows." ) }, ) reward_normalization_use_std: bool = field( default=True, metadata={ "help": "Divide grouped rollout rewards by their standard deviation. " "Set False to subtract only the mean when reward_normalization is enabled." }, ) drop_incomplete_group: bool = field( default=False, metadata={ "help": ( "If True, discard the entire group when any of the n_samples " "rollouts fails or returns None. prepare_batch will automatically " "retry with a new prompt. This prevents partial groups from " "causing reward normalization group misalignment." ) }, ) score_centering_top_k: int = field( default=0, metadata={ "help": "SGLang sampler head width for score centering; 0 disables collection. Requires temperature=1 and untruncated sampling." }, ) # NOTE: to add new parameters, please correctly handle them in the `to_openai_args_dict` method. def new(self, **kwargs): args = asdict(self) args.update(kwargs) return GenerationHyperparameters(**args) def new_with_stop_and_pad_token_ids(self, tokenizer: "PreTrainedTokenizerFast"): """Create a new generation hyperparameters with stop and pad token ids added.""" new_stop_token_ids = self.stop_token_ids.copy() if tokenizer.pad_token_id not in new_stop_token_ids: new_stop_token_ids.append(tokenizer.pad_token_id) if tokenizer.eos_token_id not in new_stop_token_ids: new_stop_token_ids.append(tokenizer.eos_token_id) return self.new(stop_token_ids=new_stop_token_ids) def to_openai_completions_args_dict( self, exclude_args: list[str] | None = None ) -> dict[str, Any]: return self.to_openai_args_dict( exclude_args=exclude_args, api_format="completions" ) def to_openai_responses_args_dict( self, exclude_args: list[str] | None = None ) -> dict[str, Any]: return self.to_openai_args_dict( exclude_args=exclude_args, api_format="responses" ) def to_openai_agents_model_settings_dict( self, exclude_args: list[str] | None = None ) -> dict[str, Any]: return self.to_openai_args_dict( exclude_args=exclude_args, api_format="openai-agents" ) _OPENAI_UNSUPPORTED_ARGS: ClassVar[set[str]] = { "score_centering_top_k", "min_new_tokens", # Not supported by OpenAI "greedy", # Not directly supported by OpenAI "top_k", # Not supported by OpenAI "stop_token_ids", # Not supported by OpenAI "ignore_eos", # Not supported by OpenAI "skip_special_tokens", # Not supported by OpenAI "lora_name", # Not supported by OpenAI "use_beam_search", # Not supported by OpenAI "max_tokens", # deprecated by "completions", not used in "responses", should be `max_new_tokens` in "openai-agents" } # Workflow-layer flags, not generation arguments. Exclude silently from # OpenAI client kwargs even when users enable them. _WORKFLOW_ONLY_ARGS: ClassVar[set[str]] = { "reward_normalization", "reward_normalization_use_std", "drop_incomplete_group", } def to_openai_args_dict( self, exclude_args: list[str] | None = None, api_format: str = "completions" ) -> dict[str, Any]: """Convert the generation hyperparameters to a dictionary of arguments for OpenAI client.""" if self.score_centering_top_k: raise ValueError( "Score centering requires the native SGLang RLVR workflow, not OpenAI clients." ) final_exclude_args = set(exclude_args) if exclude_args is not None else set() final_exclude_args.update(self._OPENAI_UNSUPPORTED_ARGS) # TODO: move the excluded args into extra body, so they can be passed through the client request mapping = {"n_samples": "n"} if api_format == "completions": mapping["max_new_tokens"] = "max_completion_tokens" elif api_format == "responses": mapping["max_new_tokens"] = "max_output_tokens" elif api_format == "openai-agents": # NOTE: max_tokens in openai-agents means `max_new_tokens` in sglang/vllm. This is not a bug mapping["max_new_tokens"] = "max_tokens" else: raise ValueError(f"Unsupported API format: {api_format}") res = {} for k, v in asdict(self).items(): if k in self._WORKFLOW_ONLY_ARGS: continue if k in final_exclude_args: should_warn = False current_value = getattr(self, k) f = next(_field for _field in fields(self) if _field.name == k) # Check if equal to the default value if f.default is not dataclass_missing: if current_value != f.default: should_warn = True elif f.default_factory is not dataclass_missing: if current_value != f.default_factory(): should_warn = True if should_warn: logger.warning( f"Unsupported arg for openai format: `{k}` with value {current_value}" ) continue key = mapping.get(k, k) if key in res: logger.warning(f"Overriding key: {key} from {k} with value: {v}") res[key] = v return res # Train Engine Configs @dataclass class OptimizerConfig: """Configuration for model optimization during training.""" DEFAULT_WARMUP_STEPS_PROPORTION: ClassVar[float] = 0.001 type: str = field( default="adam", metadata={ "help": "Optimizer type. For FSDP Engine, adam_bf16 enables memory-efficient BF16 optimizer states. " "For Megatron Engine, adam_bf16 requires dtype=bfloat16 and is automatically converted to adam " "with precision-aware optimizer enabled.", "choices": ["adam", "sgd", "adam_bf16"], }, ) lr: float = field(default=1e-3, metadata={"help": "Learning rate"}) weight_decay: float = field(default=0.01, metadata={"help": "Weight decay"}) beta1: float = field( default=0.9, metadata={ "help": "Adam beta1 parameter. Only effective when optimizer_type is adam/adam_bf16" }, ) beta2: float = field( default=0.999, metadata={ "help": "Adam beta2 parameter. Only effective when optimizer_type is adam/adam_bf16" }, ) eps: float = field( default=1e-8, metadata={ "help": "Adam epsilon parameter. Only effective when optimizer_type is adam/adam_bf16" }, ) min_lr_ratio: float = field( default=0.0, metadata={ "help": "Minimum learning rate ratio after annealing", }, ) lr_scheduler_type: str = field( default="constant", metadata={ "help": "Learning rate scheduler type", "choices": ["linear", "cosine", "constant"], }, ) warmup_steps_proportion: float = field( default=DEFAULT_WARMUP_STEPS_PROPORTION, metadata={ "help": "Non-negative proportion of training steps for warmup. " "Ignored when warmup_steps is set. For Megatron, the resolved " "warmup steps must be less than the total training steps.", }, ) warmup_steps: int | None = field( default=None, metadata={ "help": "Fixed number of learning-rate scheduler steps for warmup. " "Must be non-negative. For Megatron, it must also be less than the " "total training steps. " "When both options are explicitly configured, warmup_steps takes " "precedence over warmup_steps_proportion and a warning is emitted.", }, ) initial_loss_scale: float = field( default=2**32, metadata={"help": "Initial loss scaling factor"} ) min_loss_scale: float = field( default=1.0, metadata={"help": "Minimum loss scaling factor"} ) loss_scale_window: float = field( default=5, metadata={"help": "Window size for loss scaling adjustment"} ) hysteresis: int = field( default=2, metadata={"help": "Hysteresis (scaling factor) for loss scaling"} ) gradient_clipping: float = field( default=1.0, metadata={"help": "Gradient clipping threshold"} ) def __post_init__(self) -> None: if ( self.warmup_steps is not None and self.warmup_steps_proportion != self.DEFAULT_WARMUP_STEPS_PROPORTION ): warnings.warn( "Both warmup_steps and warmup_steps_proportion are configured; " "warmup_steps takes precedence and warmup_steps_proportion is ignored.", UserWarning, stacklevel=2, ) @dataclass class FSDPWrapPolicy: """Policy configuration for FSDP model layer wrapping. None defaults to wrapping transformer decoder layers defined by transformers.""" transformer_layer_cls_to_wrap: list[str] | None = field( default=None, metadata={"help": "A list of transformer layer names for FSDP to wrap."}, ) @dataclass class FSDPEngineConfig: """Configuration for Fully Sharded Data Parallel (FSDP) training backend.""" wrap_policy: FSDPWrapPolicy | None = field( default=None, metadata={"help": "FSDP wrap policy, specifying model layers to wrap."}, ) offload_params: bool = field( default=False, metadata={"help": "Whether to offload FSDP parameters to CPU."}, ) memory_efficient_load: bool = field( default=False, metadata={ "help": "Enable memory-efficient model loading. When enabled, model weights " "are initialized on CPU and only rank 0 loads pretrained weights, which are " "then broadcast to all ranks after FSDP sharding. This reduces peak GPU memory " "during initialization for large models. Note: For VLMs, rank 0 broadcast is " "not used; each rank loads weights independently on CPU." }, ) per_layer_optim_step: bool = field( default=False, metadata={ "help": "Run Adam step on GPU by streaming optimizer states layer-by-layer " "with async prefetching, instead of running on CPU. Optimizer states are " "automatically managed on CPU by the per-layer wrapper regardless of " "offload_params setting. Requires optimizer type 'adam' (AdamW)." }, ) optim_step_prefetch_layers: int = field( default=1, metadata={"help": "Number of layers to prefetch during per-layer optim step."}, ) def __post_init__(self): if self.optim_step_prefetch_layers < 0: raise ValueError( f"optim_step_prefetch_layers must be >= 0, got {self.optim_step_prefetch_layers}" ) shard_vision_across_sp: bool = field( default=False, metadata={ "help": "Shard vision encoder across SP ranks by image. " "Only effective when context_parallel_size > 1." }, ) @dataclass class ArchonFP8Config: """Archon FP8 training configuration.""" mode: str = field( default="disabled", metadata={ "help": "FP8 precision mode. " "'disabled': FP8 training off (default). " "'blockwise': blockwise 128x128 FP8 e4m3fn matmuls (requires Hopper GPU).", "choices": ["disabled", "blockwise"], }, ) exclude_modules: list[str] = field( default_factory=lambda: ["output", "router", "score"], metadata={ "help": ( "FQN substrings of nn.Linear modules to keep in BF16 (not converted to FP8). " "Any module whose fully-qualified name contains one of these strings is skipped. " "Meaningful values for Archon models: " "'output' (LM head, logit precision sensitive), " "'router' (MoE router gate, routing stability sensitive), " "'score' (critic head, value precision sensitive). " "Note: nn.Embedding modules (e.g. tok_embeddings) are never converted " "regardless of this list. " "WARNING: Setting this in YAML replaces the entire default list " "(does not extend it). Include ALL modules you want to keep in BF16." ) }, ) include_experts: bool = field( default=False, metadata={ "help": "Apply FP8 to MoE expert computation. " "Uses per-expert blockwise FP8 matmuls via torchao." }, ) use_triton: bool = field( default=True, metadata={ "help": ( "Use Triton GEMM kernel for FP8 blockwise matmuls instead of cuBLAS. " "Currently must be True: torchao's blockwise FP8 is a prototype that uses " "mixed per-operand scaling (1x128 activations + 128x128 weights), which " "torch._scaled_mm does not support. The Triton kernel " "(triton_fp8_gemm_1x128_128x128) handles this natively. " "Revisit when torchao stabilizes mixed-mode cuBLAS dispatch." ), }, ) @property def enabled(self) -> bool: return self.mode != "disabled" def __post_init__(self): valid_modes = {"disabled", "blockwise"} if self.mode not in valid_modes: raise ValueError( f"fp8_config.mode must be one of {valid_modes}, got {self.mode!r}" ) if self.enabled and not self.use_triton: raise ValueError( "fp8_config.use_triton must be True when FP8 is enabled. " "torchao blockwise FP8 uses mixed per-operand scaling " "(1x128 activations + 128x128 weights) which " "torch._scaled_mm does not support." ) @dataclass class ArchonEngineConfig: """Configuration for Archon Engine training backend.""" # Attention backend attn_type: str = field( default="varlen", metadata={ "help": "Attention backend type. Use 'tree' for tree training.", "choices": ["varlen", "sdpa", "tree"], }, ) # CPU offloading for FSDP offload_params: bool = field( default=False, metadata={"help": "Whether to offload FSDP parameters to CPU."}, ) # Whether to enable torch.compile enable_compile: bool = field( default=False, metadata={"help": "Enable torch.compile for TransformerBlocks."}, ) # Activation Checkpointing (enabled when gradient_checkpointing=True) ac_mode: str = field( default="selective", metadata={ "help": "Activation checkpointing mode. " "'memory_budget' requires enable_compile=True.", "choices": ["none", "full", "selective", "memory_budget"], }, ) selective_ac_option: str = field( default="op", metadata={ "help": "Selective AC option: 'op' for op-level, " "or integer string (e.g., '2') for every Nth layer." }, ) ac_memory_budget: float = field( default=0.5, metadata={ "help": "Memory budget for 'memory_budget' AC mode. " "0.0 = minimum memory (max recompute), 1.0 = default behavior (no recompute)." }, ) ac_preserve_rng_state: bool = field( default=False, metadata={ "help": "Preserve RNG state during checkpointing for deterministic output. " "Enabling this may slow down training." }, ) ac_debug: bool = field( default=False, metadata={ "help": "(Testing only) Capture AC debug information. Will be slower." }, ) # Pipeline Parallel Schedule pp_schedule: str = field( default="Interleaved1F1B", metadata={ "help": "Pipeline parallel schedule type.", "choices": [ "1F1B", "Interleaved1F1B", "InterleavedZeroBubble", "ZBVZeroBubble", ], }, ) # NOTE: The following three PP layer distribution parameters are advanced options # that most users do not need to configure. The defaults work well for typical cases. # TODO: Consider simplifying or refactoring these parameters in the future. # Currently kept for consistency with Megatron's pipeline parallel configuration. pp_layers_per_stage: int | None = field( default=None, metadata={ "help": "Number of transformer layers per (virtual) pipeline stage. " "If set, num_virtual_stages is calculated from num_layers. " "If None, stages are inferred from schedule type " "(1 stage/rank for 1F1B, 2 stages/rank for Interleaved1F1B/InterleavedZeroBubble/ZBVZeroBubble).", }, ) pp_first_stage_less_layers: int = field( default=1, metadata={ "help": "Number of layers to reduce in the first pipeline stage. " "Accounts for embedding layer overhead.", }, ) pp_last_stage_less_layers: int = field( default=1, metadata={ "help": "Number of layers to reduce in the last pipeline stage. " "Accounts for output layer overhead.", }, ) # FSDP reshard policy after forward pass reshard_after_forward_policy: str = field( default="default", metadata={ "help": "FSDP reshard policy after forward pass. " "'default': reshard when pipeline parallelism is off; keep unsharded when on to avoid repeated all-gather per microbatch. " "'always': always reshard after forward (saves memory). " "'never': never reshard after forward.", "choices": ["default", "always", "never"], }, ) # FP8 Training fp8_config: ArchonFP8Config = field( default_factory=ArchonFP8Config, metadata={ "help": "FP8 training configuration. Set mode='blockwise' to enable." }, ) # Deterministic mode use_deterministic_algorithms: bool = field( default=False, metadata={ "help": "Enable deterministic algorithms for training reproducibility. " "Sets torch.use_deterministic_algorithms(True, warn_only=True), " "CUBLAS_WORKSPACE_CONFIG, NCCL_ALGO, and TORCH_COMPILE_DETERMINISTIC. " "May reduce performance.", }, ) # MoE moe_router_dtype: str | None = field( default="fp32", metadata={ "help": "Data type for MoE router gate GEMM computation. " "'fp32' runs gate linear in float32 for numerical stability. " "None uses model dtype (no override).", "choices": ["fp32", None], }, ) def __post_init__(self): if self.pp_layers_per_stage is not None and self.pp_layers_per_stage < 1: raise ValueError( f"pp_layers_per_stage must be >= 1, got {self.pp_layers_per_stage}" ) if self.pp_first_stage_less_layers < 0: raise ValueError( f"pp_first_stage_less_layers must be >= 0, " f"got {self.pp_first_stage_less_layers}" ) if self.pp_last_stage_less_layers < 0: raise ValueError( f"pp_last_stage_less_layers must be >= 0, " f"got {self.pp_last_stage_less_layers}" ) valid_reshard_policies = ("default", "always", "never") if self.reshard_after_forward_policy not in valid_reshard_policies: raise ValueError( f"reshard_after_forward_policy must be one of {valid_reshard_policies}, " f"got '{self.reshard_after_forward_policy}'" ) valid_router_dtypes = ("fp32", None) if self.moe_router_dtype not in valid_router_dtypes: raise ValueError( f"moe_router_dtype must be one of {valid_router_dtypes}, " f"got '{self.moe_router_dtype}'" ) # These configurations are used by Megatron Bridge to build Megatron models. @dataclass class DistributedDataParallelConfig: """Configuration for Megatron's DistributedDataParallel. Refer to Megatron-LM documentation for details. """ grad_reduce_in_fp32: bool = True overlap_grad_reduce: bool = False overlap_param_gather: bool = False align_param_gather: bool = False use_distributed_optimizer: bool = True check_for_nan_in_grad: bool = False bucket_size: int | None = None average_in_collective: bool = False fp8_param_gather: bool = False @dataclass class FP8EngineConfig: """Configuration for FP8 (8-bit floating point) training. This configuration encapsulates all FP8-related parameters and can be reused across different engines (e.g., Megatron, FSDP). When None in the parent config, FP8 training is disabled. """ mode: str = field( default="e4m3", metadata={ "help": "FP8 precision mode. Options: " "'e4m3' (uniform e4m3), " "'hybrid' (e4m3 for activations/weights, e5m2 for output activation gradients)." }, ) recipe: str = field( default="delayed", metadata={ "help": "FP8 scaling recipe. Options: 'tensorwise', 'delayed', 'mxfp8' (Blackwell only), 'blockwise'." }, ) param: bool = field( default=False, metadata={ "help": "Keep parameters in FP8 precision to save memory. " "Not all parameters will be converted to fp8; for example, biases will remain unchanged." }, ) margin: int = field( default=0, metadata={"help": "Margin for FP8 scaling factor computation."}, ) amax_history_len: int = field( default=1, metadata={ "help": "Length of amax history window for scaling factor computation." }, ) amax_compute_algo: str = field( default="most_recent", metadata={ "help": "Algorithm for choosing amax value. Options: 'max' (largest in history window), 'most_recent'." }, ) wgrad: bool = field( default=True, metadata={ "help": "When False, override FP8 config and compute weight gradients in higher precision." }, ) dot_product_attention: bool = field( default=False, metadata={"help": "Use FP8 implementation of Dot Product Attention."}, ) multi_head_attention: bool = field( default=False, metadata={"help": "Use FP8 implementation of Multi Head Attention."}, ) tp_only_amax_red: bool = field( default=False, metadata={"help": "Reduce FP8 AMAX only in TP or TP-CP domain."}, ) first_last_layers_bf16: bool = field( default=False, metadata={ "help": "Retain first and last N TransformerBlocks in BF16 instead of FP8." }, ) num_layers_at_start_in_bf16: int = field( default=1, metadata={ "help": "Number of layers at start to keep in BF16 when first_last_layers_bf16 is True." }, ) num_layers_at_end_in_bf16: int = field( default=1, metadata={ "help": "Number of layers at end to keep in BF16 when first_last_layers_bf16 is True." }, ) direct_convert: bool = field( default=True, metadata={ "help": "Whether to use direct FP8 conversion during weight updates and save/load. " "When True, FP8 parameters are directly converted between TE FP8 and PyTorch FP8 " "without intermediate dequantization/quantization." }, ) def apply_to(self, transformer_config: Any) -> None: """Apply the same precision settings to MCore and Bridge providers.""" unprefixed = { "tp_only_amax_red", "first_last_layers_bf16", "num_layers_at_start_in_bf16", "num_layers_at_end_in_bf16", } for option in fields(self): if option.name == "direct_convert": # AReaL's export policy, not a TransformerEngine option. continue target = ( "fp8" if option.name == "mode" else option.name if option.name in unprefixed else f"fp8_{option.name}" ) if hasattr(transformer_config, target): setattr(transformer_config, target, getattr(self, option.name)) else: logger.warning("Unknown FP8 field in TransformerConfig: %s", target) @dataclass class CPUStagedOffloadConfig: """Bounded GPU staging for CPU-resident Megatron optimizer state.""" enabled: bool = field( default=False, metadata={ "help": ( "Keep optimizer state in pinned CPU memory and stream bounded " "update units through GPU staging buffers. The optimizer algorithm " "is selected independently by optimizer.type. Megatron only." ) }, ) buffer_count: int = field( default=2, metadata={"help": "Number of reusable GPU optimizer staging buffers."}, ) bucket_size_mb: float = field( default=128.0, metadata={"help": "Maximum size in MiB of one GPU optimizer staging unit."}, ) def __post_init__(self) -> None: if self.buffer_count < 1: raise ValueError("cpu_staged_offload.buffer_count must be at least 1") if self.bucket_size_mb <= 0: raise ValueError("cpu_staged_offload.bucket_size_mb must be positive") @dataclass class MegatronEngineConfig: """Configuration for Megatron-LM training framework. Refer to Megatron-LM documentation for implementation details. """ # Distributed Training Configuration wrap_with_ddp: bool = True use_torch_fsdp2: bool = False # TODO: pending test use_custom_fsdp: bool = False # TODO: pending test ddp: DistributedDataParallelConfig = field( default_factory=DistributedDataParallelConfig ) virtual_pipeline_parallel_size: int = field( default=1, metadata={ "help": ( "Virtual pipeline parallel size for Megatron interleaved schedule. " "Set to >1 to enable VPP. Default is 1 (disabled)." ) }, ) language_model_only: bool = field( default=False, metadata={ "help": ( "For multimodal ModelScope bridges, build only the language model. " "Set True for text-only inputs and False for vision inputs." ) }, ) # Don't use MegatronOptimizerConfig here because OmegaConf # does not recognize the annotation "torch.dtype" overlap_param_gather_with_optimizer_step: bool = False # Precision Configuration use_precision_aware_optimizer: bool = field( default=False, metadata={ "help": "Enable precision-aware optimizer for Megatron. " "When using adam_bf16 optimizer type with Megatron Engine, " "this is automatically enabled with exp_avg_dtype=bfloat16 and exp_avg_sq_dtype=bfloat16." }, ) main_grads_dtype: str = "float32" main_params_dtype: str = "float32" exp_avg_dtype: str = "float32" exp_avg_sq_dtype: str = "float32" cpu_staged_offload: CPUStagedOffloadConfig = field( default_factory=CPUStagedOffloadConfig, metadata={ "help": ( "Keep the selected optimizer's state CPU-resident and use bounded " "GPU staging buffers for optimizer steps." ) }, ) # Checkpointing Configuration async_save: bool = field( default=False, metadata={ "help": ( "If True, Megatron checkpoint saves run in background processes and " "save_checkpoint() returns immediately after weights are durably " "staged off the GPU. Pending saves are drained before the next " "load_checkpoint() and during engine.destroy(). Reduces per-save " "sync wait on large MoE checkpoints." ), }, ) use_checkpoint_opt_param_scheduler: bool = True # Deterministic Option # NOTE: This option forces torch to use deterministic algorithms, # which makes sure that two forward passes with the same input # will produce the same output. However, it may have a performance impact. # It is recommended to set this option to True for RL training on MoE models for stability. use_deterministic_algorithms: bool = False # Gradient checkpointing options, only effective when gradient_checkpointing=True recompute_granularity: str | None = "full" recompute_method: str | None = "uniform" recompute_num_layers: int | None = 1 distribute_saved_activations: bool | None = None recompute_modules: list[str] | None = None # MoE moe_router_dtype: str | None = "fp32" moe_shared_expert_overlap: bool | None = field( default=None, metadata={ "help": "Enable overlapping between shared expert computations and dispatcher communications. " "Without this, the shared experts execute after the routed experts. " "None keeps the model bridge's own default." }, ) moe_enable_deepep: bool = False moe_token_dispatcher_type: str = field( default="alltoall", metadata={ "help": "Type of token dispatcher. Options: 'allgather','alltoall' and 'flex'." }, ) moe_permute_fusion: bool = field( default=False, metadata={"help": "Fuse token rearrangement ops during token dispatching."}, ) moe_router_fusion: bool = field( default=False, metadata={ "help": "Enable fusion for MoE TopK routing and aux-loss computation. " "Requires TransformerEngine >= 2.7.0.", }, ) enable_router_replay: bool = field( default=False, metadata={ "help": "Enable Megatron-Core native MoE router replay for R3 training." }, ) moe_router_bias_update_rate: float | None = field( default=None, metadata={ "help": "Update rate for auxiliary-loss-free MoE load balancing " "(DeepSeek V3 style). Controls how fast expert_bias adjusts. " "None keeps the model bridge's own default (AReaL bridges " "disable it or derive it from the checkpoint). Set 0.0 to " "disable explicitly; 1e-3 matches DeepSeek V3.", }, ) moe_z_loss_coeff: float | None = field( default=None, metadata={ "help": "Scaling coefficient for router z-loss. Complements " "auxiliary-loss-free load balancing for router stability. A starting " "value of 1e-3 is recommended. None disables z-loss.", }, ) # Precision & Loss enable_chunked_logits: bool = field( default=False, metadata={ "help": "Enable AReaL's CUDA-only chunked-logits path by replacing " "Megatron's native output layer with the vocab-parallel LM Head. " "NPU and tree training are unsupported." }, ) entropy_requires_grad: bool = field( default=False, metadata={ "help": "Whether the training loss requires entropy gradients. " "Defaults to False. With AReaL LM Head enabled, False permits " "destructive logits-storage reuse, so entropy is non-differentiable. " "Set True to use the differentiable fallback." }, ) lm_head_loss_chunk_size: int = field( default=0, metadata={ "help": "Sequence chunk size for AReaL's chunked LM Head loss. A " "positive value requires enable_chunked_logits=True and " "entropy_requires_grad=False, and computes LM Head logits and their " "backward one chunk at a time. Supports text actors and the " "Qwen3.5 model-owned / Qwen4Exp wrapper-owned THD multimodal paths " "without MTP; 0 disables it." }, ) enable_fp32_lm_head: bool = field( default=False, metadata={ "help": "Compute the lm_head projection with FP32 input and weight " "operands for numerical stability. With enable_chunked_logits=True, " "the local vocab-parallel weight is converted once per microbatch " "LM-head forward and reused across sequence chunks." }, ) cross_entropy_loss_fusion: bool = field( default=False, metadata={ "help": "Enable fused cross-entropy loss kernel for better performance." }, ) # FP8 Training Configuration fp8_config: FP8EngineConfig | None = None # Bridge backend used for HF<->Megatron conversion/model creation. bridge_type: str = field( default="mbridge", metadata={ "help": "Bridge backend for MegatronEngine. Choices: 'mbridge', 'megatron-bridge', or 'mcore-bridge'.", "choices": ["mbridge", "megatron-bridge", "mcore-bridge"], }, ) use_mbridge_save: bool = field( default=False, metadata={ "help": "Use mbridge's save method to save gpu memory when saving weights." }, ) use_bridge_for_update_weights: bool = field( default=False, metadata={ "help": "When True and bridge_type is 'megatron-bridge' or 'mcore-bridge', delegate live " "weight sync to bridge.export_hf_weights instead of the hand-rolled " "convert_to_hf registry. Required for models without a registry entry " "(e.g. Qwen3.5). FP8 paths fall back to the registry automatically.", }, ) disable_grad_buffers_cpu_backup: bool = field( default=False, metadata={ "help": ( "When offloading with torch_memory_saver, skip CPU backup for " "Megatron gradient buffers (they are recomputed each step). " ) }, ) enable_mtp: bool = field( default=False, metadata={ "help": "Keep the model's Multi-Token-Prediction (MTP) head " "(bridge_type=megatron-bridge only). Default False drops it.", }, ) freeze_vision_model: bool | None = field( default=None, metadata={ "help": ( "Freeze the vision encoder before DDP/optimizer construction " "(megatron-bridge only). Set freeze_vision_projection=True as well " "to freeze the complete visual feature path. None freezes " "Qwen3-VL/Qwen3.5 vision by default and preserves other providers' " "defaults. Set False explicitly to train the encoder." ) }, ) freeze_vision_projection: bool | None = field( default=None, metadata={ "help": ( "Freeze the vision projector/merger, including deepstack mergers, " "before DDP/optimizer construction (megatron-bridge only). " "None freezes Qwen3-VL/Qwen3.5 mergers by default and preserves " "other providers' defaults. Set False explicitly to train them." ) }, ) freeze_ple_table: bool = field( default=True, metadata={ "help": ( "Freeze the Qwen4-Exp PLE/N-gram table while keeping the PLE " "projection, norm, convolution, and token embedding trainable. " "Set False to train and export the full PLE table." ) }, ) enable_mtp_training: bool = field( default=False, metadata={ "help": "Train the Multi-Token-Prediction (MTP) head as an auxiliary " "objective (SFT/RL). Requires enable_mtp=True. The main forward keeps " "labels=None and returns logits; Megatron-Core derives MTP targets from " "input_ids and isolates MTP gradients from the backbone and LM head. " "bridge_type=megatron-bridge only; packed context parallel training is " "supported.", }, ) mtp_only: bool = field( default=False, metadata={ "help": "Freeze all non-MTP parameters before DDP/optimizer construction. " "Requires enable_mtp_training=True, bridge_type='megatron-bridge', " "and one native MTP layer. Pipeline stages without MTP stay frozen. " "Shared embeddings and output weights stay frozen. " "Not supported with LoRA, critic models, or FSDP wrappers. The main " "loss path is retained to trigger the auxiliary MTP backward.", }, ) mtp_loss_scaling_factor: float = field( default=0.1, metadata={ "help": "Weight of the auxiliary MTP loss relative to the main loss " "when enable_mtp_training=True (DeepSeek-V3 default: 0.1).", }, ) def __post_init__(self) -> None: if ( self.freeze_vision_model is not None or self.freeze_vision_projection is not None ) and self.bridge_type != "megatron-bridge": raise ValueError( "freeze_vision_model and freeze_vision_projection require " "bridge_type='megatron-bridge'; Qwen4-Exp with mcore-bridge " "already freezes its complete visual module by default" ) if self.cpu_staged_offload.enabled and self.async_save: raise ValueError( "megatron.async_save is not supported with " "megatron.cpu_staged_offload.enabled=true" ) if self.enable_mtp_training and not self.enable_mtp: raise ValueError("enable_mtp_training requires enable_mtp=True") if self.mtp_only: if not self.enable_mtp_training: raise ValueError("mtp_only requires enable_mtp_training=True") if self.bridge_type != "megatron-bridge": raise ValueError("mtp_only requires bridge_type='megatron-bridge'") if ( not math.isfinite(self.mtp_loss_scaling_factor) or self.mtp_loss_scaling_factor <= 0 ): raise ValueError( "mtp_only requires a finite, positive mtp_loss_scaling_factor" ) if self.use_custom_fsdp or self.use_torch_fsdp2: raise ValueError("mtp_only does not support FSDP wrappers") # Check the actual runtime: package metadata alone cannot establish # which cuDNN shared library PyTorch loads. from importlib.metadata import PackageNotFoundError import torch for package, minimum in ( ("megatron-core", "0.19.0"), ("megatron-bridge", "0.6.0"), ): try: installed = pkg_version.get_version(package) except PackageNotFoundError as exc: raise ValueError( f"mtp_only requires {package}>={minimum}; package not installed" ) from exc if pkg_version.compare_versions(installed, minimum) < 0: raise ValueError( f"mtp_only requires {package}>={minimum}; found {installed}" ) try: cudnn_version = torch.backends.cudnn.version() except RuntimeError as exc: raise ValueError( "mtp_only requires loaded cuDNN>=9.19.0; " "PyTorch could not load cuDNN" ) from exc if cudnn_version is None or cudnn_version < 91900: raise ValueError( "mtp_only requires loaded cuDNN>=9.19.0 (91900); " f"torch.backends.cudnn.version() returned {cudnn_version}. " "Ensure PyTorch loads the upgraded cuDNN shared libraries." ) if self.lm_head_loss_chunk_size < 0: raise ValueError( "lm_head_loss_chunk_size must be non-negative, got " f"{self.lm_head_loss_chunk_size}" ) if self.lm_head_loss_chunk_size > 0 and not self.enable_chunked_logits: raise ValueError( "lm_head_loss_chunk_size requires enable_chunked_logits=True" ) if self.lm_head_loss_chunk_size > 0 and self.entropy_requires_grad: raise ValueError( "lm_head_loss_chunk_size requires entropy_requires_grad=False" ) if self.lm_head_loss_chunk_size > 0 and self.enable_mtp: raise ValueError("lm_head_loss_chunk_size does not support enable_mtp=True") if self.lm_head_loss_chunk_size > 0 and self.enable_mtp_training: raise ValueError( "lm_head_loss_chunk_size does not support enable_mtp_training=True" ) class SchedulingStrategyType(str, Enum): separation = "separation" colocation = "colocation" @dataclass class SchedulingStrategy: type: str = field( default="separation", metadata={"choices": ["separation", "colocation"]}, ) target: str | None = field( default=None, metadata={"help": "The target role to be colocated with"} ) fork: bool = field( default=True, metadata={ "help": "When True with colocation, the target worker spawns a new " "process on the same node/GPUs instead of sharing its process. " "Provides process isolation while sharing GPU resources." }, ) @dataclass class SchedulingSpec: cpu: int = field( default=8, metadata={"help": "Number of CPU cores required per GPU"} ) gpu: int = field( default=0, metadata={ "help": "Number of GPU units required. Used only when allocating pods." }, ) mem: int = field( default=32, metadata={"help": "Amount of memory (GB) required per GPU"} ) port_count: int = field(default=2, metadata={"help": "Number of ports to expose"}) image: str = field( default="/storage/openpsi/images/areal-latest.sif", metadata={ "help": "Docker/Singularity container image to use. " "Currently only used by Slurm. Will be potentially used by Kubernetes in the future." }, ) task_type: str = field( default="worker", metadata={ "help": "Task type (e.g., worker, engine)", "choices": ["worker", "engine"], }, ) env_vars: dict[str, str] = field( default_factory=dict, metadata={"help": "Environment variables for the container"}, ) cmd: str | None = field( default=None, metadata={ "help": "Command to execute inside the container. Defaults to AReaL's RPC server." }, ) ray_runtime_env: dict[str, Any] = field( default_factory=dict, metadata={ "help": "Native Ray runtime environment for this role's launchers. " "Use image_uri or container.image for OCI images; container.run_options " "supplies Podman GPU and volume options. All specs in a role must " "use the same runtime environment and resources. Explicit env_vars " "override runtime_env.env_vars. Images must contain compatible Ray " "and Python versions and make AReaL importable." }, ) # Slurm specific options srun_additional_args: str = field( default="--unbuffered --mpi=pmi2 -K --chdir $PWD", metadata={ "help": "Additional arguments to pass to the srun command. Only used by slurm." }, ) additional_bash_cmds: list[str] | None = field( default=None, metadata={ "help": "Additional bash commands to setup the container before running " "the torchrun command. Only used by slurm." }, ) container_type: str = field( default="apptainer", metadata={ "help": "Type of containers used in slurm", "choices": ["apptainer", "none"], }, ) mount: str = field( default="/storage:/storage", metadata={"help": "Mount path for slurm."} ) nodelist: str | None = field( default=None, metadata={"help": "sbatch/srun's `--nodelist` option for slurm."} ) reservation: str | None = field( default=None, metadata={"help": "sbatch's `--reservation` option for slurm."}, ) exclusive: bool = field( default=False, metadata={ "help": "sbatch's `--exclusive` option for slurm. Ensures nodes are not shared with other jobs." }, ) exclude: str | None = field( default=None, metadata={"help": "sbatch/srun's `--exclude` option for slurm."} ) ray_placement_strategy: str | None = field( default=None, metadata={ "help": "Deprecated compatibility field for the legacy Ray scheduler. " "It is ignored by the current Ray scheduler.", }, ) def __post_init__(self): """Validate scheduling spec configuration.""" if self.ray_placement_strategy is not None: warnings.warn( "SchedulingSpec.ray_placement_strategy is deprecated and ignored by " "the current Ray scheduler.", DeprecationWarning, stacklevel=2, ) @dataclass class TrainEngineConfig: """Core configuration for model training, including optimization and backend settings.""" experiment_name: str = MISSING trial_name: str = MISSING path: str = field(default="", metadata={"help": "Path to HuggingFace checkpoint"}) attn_impl: str = field( default="flash_attention_2", metadata={ "help": "Attention implementation for huggingface transformers model. " "Accepts builtin transformers backends or a Hugging Face kernels repo ID " "formatted as org/repo[@revision][:entrypoint].", "choices": list(BUILTIN_ATTN_IMPLS), }, ) use_kernels: bool = field( default=False, metadata={ "help": "Enable Hugging Face kernels model kernelization after model creation." }, ) init_from_scratch: bool = field( default=False, metadata={"help": "Initialize model weights randomly"} ) is_critic: bool = field( default=False, metadata={"help": "Whether to use a critic/reward model"}, ) temperature: float = field( default=1.0, metadata={"help": "Temperature during generation."} ) logprobs_chunk_size: int = field( default=1024, metadata={ "help": "Maximum sequence chunk size used to compute log probabilities " "and entropy. Must be positive." }, ) # Runtime microbatch limit mb_spec: MicroBatchSpec = field(default_factory=MicroBatchSpec) pad_to_maximum: bool = field( default=False, metadata={ "help": ( "Whether to pad each microbatch to the length upper bound specified by mb_spec. " "Can reduce memory fragmentation but slows down training." ) }, ) # Training Backend Configuration disable_dropout: bool = field( default=False, metadata={"help": "Disable dropout layers during training"} ) gradient_checkpointing: bool = field( default=False, metadata={"help": "Enable gradient checkpointing"} ) dtype: str = field( default="bfloat16", metadata={"help": "Forward/backward compute dtype."}, ) grad_reduce_dtype: str = field( default="float32", metadata={"help": "Gradient reduction data type."} ) optimizer_dtype: str = field( default="float32", metadata={ "help": ( "Underlying parameter storage dtype, also the dtype of optimizer " "states (exp_avg, exp_avg_sq) since torch.optim.AdamW inherits " "dtype from model.parameters(). " "Default 'float32' maintains fp32 master weights matching " "DeepSpeed ZeRO-3 and Megatron precision-aware optimizer behavior. " "FSDP2's MixedPrecisionPolicy(param_dtype=`dtype`) will still " "cast forward/backward computation to `dtype` (e.g. bfloat16). " "Set to 'bfloat16' together with optimizer.type='adam_bf16' to " "reduce memory at the cost of needing Kahan summation for stability. " "Currently FSDP-only; Megatron uses use_precision_aware_optimizer " "instead and ignores this field." ) }, ) optimizer: OptimizerConfig | None = field( default=None, metadata={"help": "Optimizer configuration. None means no training."}, ) weight_update_mode: str = field( default="xccl", metadata={ "help": "Weight update backend type. 'awex' requires a Megatron actor " "and an SGLang rollout.", "choices": ["disk", "xccl", "awex"], }, ) enable_delta_weight_update: bool = field( default=False, metadata={"help": "Enable sparse delta weight updates for separation AWEX."}, ) weight_update_delta_method: str = field( default="adamw", metadata={ "help": "Change detection method used for delta weight transfer.", "choices": ["adamw"], }, ) weight_update_anchor_interval: int = field( default=0, metadata={ "help": "Force a full sync every N committed deltas. 0 disables " "periodic anchors." }, ) fsdp: FSDPEngineConfig = field(default_factory=FSDPEngineConfig) archon: ArchonEngineConfig = field(default_factory=ArchonEngineConfig) megatron: MegatronEngineConfig = field(default_factory=MegatronEngineConfig) # offload offload: bool = field( default=False, metadata={ "help": "Whether to offload model parameters and optimizer states to CPU. " }, ) # Lora use_lora: bool = field( default=False, metadata={ "help": "Whether to use LoRA. Only support FSDP. Note that should be enabled together with vLLM/SGLang." }, ) lora_rank: int = field(default=32, metadata={"help": "lora rank"}) lora_alpha: int = field(default=16, metadata={"help": "lora alpha"}) target_modules: list[str] = field( default_factory=list, metadata={"help": "lora target_modules."}, ) peft_type: str = field( default="lora", metadata={"help": "peft method type. Only LoRA is supported for now."}, ) # Tree training enable_tree_training: bool = field( default=False, metadata={"help": "Enable tree training with flex attention module."}, ) # Scheduling scheduling_spec: tuple[SchedulingSpec, ...] = field( default_factory=lambda: ( SchedulingSpec(cmd="python -m areal.infra.rpc.rpc_server"), ), metadata={ "help": "Train engine schedule specs. Can accept 1 or 2 SchedulingSpec: " "if 1 spec provided, it's used for both worker and engine, engine is embedded in the worker; " "if 2 specs provided, first one is for worker, second one is for engine. " "Currently only used by the TrainController." }, ) # Backend and parallelism (new per-engine config) backend: str = field( default=MISSING, metadata={ "help": "Backend and parallelism strategy. Must include an explicit backend prefix, " "e.g. 'fsdp:d4', 'megatron:d4t2p2', 'archon:d2'. Required." }, ) # v2 controller options _version: str = field( default="v1", metadata={ "help": "Train controller implementation version. Use 'v1' for legacy TrainController, 'v2' for GatewayTrainController.", "choices": ["v1", "v2"], }, ) admin_api_key: str = field( default="areal-admin-key", metadata={ "help": "Admin API key used by gateway/router/data-proxy in controller v2." }, ) log_level: str = field( default="warning", metadata={"help": "Gateway stack log level for controller v2."}, ) request_timeout: float = field( default=3600.0, metadata={"help": "Gateway request timeout in seconds for controller v2."}, ) setup_timeout: float = field( default=3600.0, metadata={"help": "Gateway setup timeout in seconds for controller v2."}, ) workers_ready_timeout: float = field( default=30.0, metadata={ "help": "Timeout (seconds) for initialize() to wait for guards to be ready." }, ) scheduling_strategy: SchedulingStrategy = field( default_factory=SchedulingStrategy, metadata={ "help": "The scheduling strategy of this TrainEngine, either separation or colocation. " "Currently only used by the TrainController." }, ) def __post_init__(self): """Validate scheduling_spec length and config combinations.""" if self.logprobs_chunk_size <= 0: raise ValueError( f"logprobs_chunk_size must be positive, got {self.logprobs_chunk_size}" ) if len(self.scheduling_spec) not in (1, 2): raise ValueError( f"scheduling_spec must contain 1 or 2 SchedulingSpec, " f"got {len(self.scheduling_spec)}" ) if not is_valid_attn_impl(self.attn_impl): raise ValueError(get_attn_impl_validation_error(self.attn_impl)) if self.fsdp.memory_efficient_load and self.init_from_scratch: raise ValueError( "memory_efficient_load cannot be used with init_from_scratch=True. " "memory_efficient_load is for loading pretrained weights on CPU, " "but init_from_scratch creates a model without loading any weights." ) if self._version not in ("v1", "v2"): raise ValueError( f"_version must be either 'v1' or 'v2', got '{self._version}'" ) if self.weight_update_mode == "awex" and not self.megatron.wrap_with_ddp: raise ValueError( "weight_update_mode='awex' requires megatron.wrap_with_ddp=true " "because AWEX offloads MCore DDP flat buffers" ) # Canonicalize common aliases so getattr(torch, ...) works at runtime. # Storage map omits fp16 since float16 is not a valid optimizer_dtype; # leaving "fp16" un-canonicalized makes the validation error below # echo what the user typed instead of a silently rewritten value. _compute_aliases = {"fp32": "float32", "bf16": "bfloat16", "fp16": "float16"} _storage_aliases = {"fp32": "float32", "bf16": "bfloat16"} if self.optimizer_dtype in _storage_aliases: self.optimizer_dtype = _storage_aliases[self.optimizer_dtype] if self.dtype in _compute_aliases: self.dtype = _compute_aliases[self.dtype] if self.optimizer_dtype not in ("float32", "bfloat16"): raise ValueError( f"optimizer_dtype must be 'float32' or 'bfloat16', " f"got {self.optimizer_dtype!r}" ) if self.dtype not in ("float32", "bfloat16", "float16"): raise ValueError( f"dtype must be one of float32/bfloat16/float16, got {self.dtype!r}" ) @dataclass class RejectionSamplingConfig: """Unified configuration for sample filtering based on policy divergence. Filters tokens/sequences where the divergence between proximal policy and behavior policy exceeds a threshold, via two action modes: - 'mask': zero out loss_mask (rejection, exclude from gradient) - 'clamp': clamp importance weight to bounds (truncation, bounded gradient) With score centering, token-ratio 'mask' instead zeros the sampled importance weight while retaining the centering correction and original normalization. Supports direct ratio bounds and KL divergence estimators (K1/K2/K3), at both token-level and sequence-level granularity. Replaces the removed ``behave_imp_weight_cap`` and ``behave_imp_weight_mode``. Attributes: level: Filtering granularity ('token' or 'sequence'). When ``level='sequence'`` and ``metric='ratio'``, both the filtering decision and the correction weight (behave_imp_weight) use the sequence-level geometric mean, matching the old ``sequence_mask``/``sequence_truncate`` semantics. action: Action mode ('mask' or 'clamp'). metric: Divergence metric ('ratio', 'kl_k1', 'kl_k2', 'kl_k3'). agg: Aggregation method for sequence-level ('sum', 'mean', 'max'). For 'ratio' metric, aggregation is performed in log space (geometric mean/sum) to avoid the "length trap" and match GSPO semantics. For KL metrics, aggregation is arithmetic. upper: Upper bound for filtering. lower: Lower bound for filtering (optional). """ level: str = field( default="token", metadata={ "help": "Filtering granularity. " "'token': per-token filtering (each token judged independently). " "'sequence': per-sequence filtering (all tokens in a sequence share the same fate). " "When metric='ratio', both the filtering decision and the correction weight " "(behave_imp_weight) operate at sequence level using the geometric mean.", "choices": ["token", "sequence"], }, ) action: str = field( default="mask", metadata={ "help": "Action to take when metric exceeds threshold. " "'mask': zero out loss_mask for filtered tokens/sequences (rejection, " "completely excludes from gradient computation). With score centering, only the " "sampled importance weight is zeroed; the centering correction remains active. " "'clamp': clamp importance weight to [lower, upper] bounds (truncation, " "tokens still participate in gradient but with bounded weight).", "choices": ["mask", "clamp"], }, ) metric: str = field( default="ratio", metadata={ "help": "Divergence metric for filtering. " "'ratio': direct importance ratio π_proximal/π_behave. " "'kl_k1': KL estimator k1 = log(r), forward KL unbiased estimator (can be negative). " "'kl_k2': KL estimator k2 = 0.5 * (log r)^2, non-negative quadratic approximation. " "'kl_k3': KL estimator k3 = r - log(r) - 1, non-negative exact forward KL estimator. " "'binary_kl': KPop (symmetric binary KL divergence) — masks tokens where either " "KL(proximal||behave) or KL(behave||proximal) exceeds the upper bound.", "choices": ["ratio", "kl_k1", "kl_k2", "kl_k3", "binary_kl"], }, ) agg: str = field( default="mean", metadata={ "help": "Aggregation method for sequence-level filtering. " "Only used when level='sequence'. " "For 'ratio' metric, aggregation is in log space: " "'sum' = exp(sum(log(r_i))), 'mean' = exp(mean(log(r_i))) = geometric mean " "(length-invariant, consistent with GSPO). " "For KL metrics, aggregation is arithmetic: " "'sum' = sum(kl_i), 'mean' = mean(kl_i). " "'max': max of per-token metric values (most conservative).", "choices": ["sum", "mean", "max"], }, ) upper: float = field( default=5.0, metadata={ "help": "Upper bound for filtering. " "Tokens/sequences with metric > upper are filtered out (loss_mask zeroed). " "For 'ratio' metric: must be > 1.0, typical values are 2.0 or 5.0. " "For 'kl_k2'/'kl_k3' metrics: typical values are 0.5-2.0." }, ) lower: float | None = field( default=None, metadata={ "help": "Lower bound for filtering (optional). " "None means no lower bound. " "For 'ratio' metric: typical value is 0.5 (filter out tokens where policy " "probability dropped significantly). Must be > 0 for masking; clamp also allows 0. " "For 'kl_k1' metric: can be used to filter negative KL estimates." }, ) def __post_init__(self): """Validate configuration.""" import warnings _VALID_LEVELS = ("token", "sequence") _VALID_ACTIONS = ("mask", "clamp") _VALID_METRICS = ("ratio", "kl_k1", "kl_k2", "kl_k3", "binary_kl") _VALID_AGGS = ("sum", "mean", "max") # Validate enum-like fields. if self.level not in _VALID_LEVELS: raise ValueError( f"level must be one of {_VALID_LEVELS}, got '{self.level}'" ) if self.action not in _VALID_ACTIONS: raise ValueError( f"action must be one of {_VALID_ACTIONS}, got '{self.action}'" ) if self.metric not in _VALID_METRICS: raise ValueError( f"metric must be one of {_VALID_METRICS}, got '{self.metric}'" ) if self.agg not in _VALID_AGGS: raise ValueError(f"agg must be one of {_VALID_AGGS}, got '{self.agg}'") # Validate lower <= upper when both are set. if self.lower is not None and self.lower > self.upper: raise ValueError( f"lower ({self.lower}) cannot be greater than upper ({self.upper})" ) # For ratio metric, upper must be > 1.0 (otherwise all non-identical policy tokens are filtered). if self.metric == "ratio": if self.upper <= 1.0: raise ValueError( f"upper must be > 1.0 for 'ratio' metric (otherwise all non-identical " f"policy tokens will be filtered), got {self.upper}" ) if self.lower is not None and ( self.lower < 0 or (self.lower == 0 and self.action != "clamp") ): raise ValueError( f"lower must be positive for 'ratio' metric (zero is allowed for clamp), got {self.lower}" ) # For KL metrics, upper must be > 0. # Note: kl_k1 is excluded because it is a forward KL unbiased estimator that # can produce negative values, so requiring upper > 0 would be too restrictive. if self.metric in ("kl_k2", "kl_k3", "binary_kl") and self.upper <= 0: raise ValueError( f"upper must be positive for '{self.metric}' metric, got {self.upper}" ) # Clamp action only supports ratio metric (direct importance weight truncation). if self.action == "clamp" and self.metric != "ratio": raise ValueError( f"action='clamp' only supports metric='ratio' (direct importance weight " f"truncation). Got metric='{self.metric}'. " f"Use action='mask' for KL-based filtering." ) # Clamp action defaults lower to 0.0 (consistent with old truncate behavior). if self.action == "clamp" and self.lower is None: self.lower = 0.0 # Validate sequence-level aggregation. if self.level == "token" and self.agg != "mean": warnings.warn( f"agg='{self.agg}' is ignored when level='token'. " "Aggregation is only used for sequence-level filtering.", UserWarning, stacklevel=2, ) @dataclass class AdvantageFunctionConfig: """Reference one built-in algorithm key or a dotted Python callable path.""" implementation: str = field( default="auto", metadata={ "help": "Built-in algorithm key or trusted dotted Python callable path." }, ) kwargs: dict[str, Any] = field( default_factory=dict, metadata={"help": "Keyword arguments forwarded to the resolved callable."}, ) def __post_init__(self) -> None: if not isinstance(self.implementation, str) or not self.implementation.strip(): raise ValueError("advantage function implementation must be non-empty") self.implementation = self.implementation.strip() if not isinstance(self.kwargs, (dict, DictConfig)): raise TypeError("advantage function kwargs must be a mapping") if isinstance(self.kwargs, DictConfig): self.kwargs = OmegaConf.to_container(self.kwargs, resolve=False) try: json.dumps(self.kwargs) except (TypeError, ValueError) as exc: raise TypeError( "advantage function kwargs must contain only serializable values" ) from exc @dataclass class AdvantageConfig: """Select the advantage function and its orthogonal built-in stages.""" compute: AdvantageFunctionConfig = field( default_factory=lambda: AdvantageFunctionConfig(implementation="default"), metadata={"help": "Default compute key or custom dotted compute path."}, ) outcome_reward_shaping: AdvantageFunctionConfig | None = field( default=None, metadata={ "help": "Transform task-result rewards before reward normalization; built-in key or dotted callable path." }, ) estimator: AdvantageFunctionConfig | None = field( default=None, metadata={"help": "Optional advantage-estimator key or dotted callable path."}, ) process_adv_shaping: AdvantageFunctionConfig | None = field( default=None, metadata={ "help": "Modify estimated advantages using process rewards; additive by default, or a built-in key or dotted callable path." }, ) def __post_init__(self) -> None: def normalize(value): if value is None or isinstance(value, AdvantageFunctionConfig): return value if isinstance(value, (dict, DictConfig)): return AdvantageFunctionConfig(**dict(value)) raise TypeError("advantage algorithm specifications must be mappings") self.compute = normalize(self.compute) if self.compute is None: raise ValueError("advantage.compute must not be None") for name in ( "outcome_reward_shaping", "estimator", "process_adv_shaping", ): setattr(self, name, normalize(getattr(self, name))) @dataclass class LossTermConfig: """One actor loss with its own support, reduction, and final coefficient. Implementations return unreduced token-shaped tensors. ``mask_key`` refers to a batch-only support mask; zero reward does not imply missing support. ``reduction=None`` inherits actor.loss_aggregation. Named loss mappings replace the implicit legacy objective, so include a policy term when desired. """ implementation: str = field( default="policy", metadata={ "help": "Built-in key (policy, entropy, mopd) or trusted dotted callable path." }, ) enabled: bool = field( default=True, metadata={ "help": "Disable a term without importing or evaluating its implementation." }, ) coefficient: float = field( default=1.0, metadata={"help": "Finite nonnegative multiplier applied once to this loss."}, ) reduction: _LossReduction | None = field( default=None, metadata={ "help": "None inherits policy reduction. token_mean averages supported tokens; turn_mean averages supported turns; prompt_mean averages rollout groups equally and tokens equally within each group. prompt_mean preserves response-level optimizer scheduling and normalizes by full-update active groups. seq_mean averages active response means; constant uses actor.loss_aggregation_divisor.", "choices": [ "token_mean", "seq_mean", "turn_mean", "prompt_mean", "constant", ], }, ) mask_key: str = field( default="loss_mask", metadata={ "help": "Prediction-aligned, batch-derived binary support mask key. Intersected with the post-advantage loss_mask; custom masks are not shifted automatically." }, ) kwargs: dict[str, Any] = field( default_factory=dict, metadata={ "help": "JSON-serializable algorithm kwargs for loss_fn(*, outputs, batch, mask, ...). Runtime arguments cannot be overridden. Built-in kwargs override actor defaults except pipeline-owned prox_logp_method/use_decoupled_loss, which must match the actor." }, ) def __post_init__(self) -> None: if not isinstance(self.implementation, str) or not self.implementation.strip(): raise ValueError("loss implementation must be a nonempty string") self.implementation = self.implementation.strip() if not isinstance(self.enabled, bool): raise ValueError("loss enabled must be boolean") if ( isinstance(self.coefficient, bool) or not isinstance(self.coefficient, (int, float)) or not math.isfinite(self.coefficient) or self.coefficient < 0 ): raise ValueError("loss coefficient must be finite and nonnegative") if self.reduction not in { None, "token_mean", "seq_mean", "turn_mean", "prompt_mean", "constant", }: raise ValueError("invalid loss reduction") if not isinstance(self.mask_key, str) or not self.mask_key: raise ValueError("loss mask_key must be a nonempty string") if isinstance(self.kwargs, DictConfig): self.kwargs = OmegaConf.to_container(self.kwargs, resolve=False) if not isinstance(self.kwargs, dict): raise ValueError("loss kwargs must be a mapping") try: json.dumps(self.kwargs, allow_nan=False) except (TypeError, ValueError) as exc: raise ValueError("loss kwargs must be JSON-serializable") from exc @dataclass class PPOActorConfig(TrainEngineConfig): """Configuration for PPO actor model, a subclass of a TrainEngine.""" advantage: AdvantageConfig = field( default_factory=AdvantageConfig, metadata={ "help": "Composable advantage computation; defaults preserve GAE/PRM settings." }, ) losses: dict[str, LossTermConfig] = field( default_factory=dict, metadata={ "help": "Named token losses with independent support and reduction. Empty preserves the existing objective; a nonempty mapping replaces it." }, ) # Core PPO/GRPO Parameters ppo_n_minibatches: int = field( default=4, metadata={ "help": "Number of minibatches for each PPO update. Separation DTE " "AdamW delta transfer currently requires 1." }, ) eps_clip: float = field( default=0.2, metadata={"help": "Clipping factor for policy ratio"} ) eps_clip_higher: float | None = field( default=None, metadata={ "help": "Clipping factor (higher value) for policy ratio. Default is None. When eps_clip_higher is set (decoupled), eps_clip will be used as the lower value." }, ) c_clip: float | None = field( default=None, metadata={ "help": "Dual-Clip PPO factor for policy ratio (Ye et al., AAAI 2020, https://arxiv.org/abs/1912.09729), must be > 1.0. None disables dual clipping." }, ) # M2PO m2_threshold: float | None = field( default=None, metadata={"help": "The second momentum threshold for M2PO."} ) # Reward reward_norm: NormConfig | None = field( default=None, metadata={"help": "Normalization configuration for rewards"}, ) reward_scaling: float = field( default=1.0, metadata={"help": "Reward scaling factor"} ) reward_bias: float = field(default=0.0, metadata={"help": "Reward bias"}) reward_clip: float = field( default=20.0, metadata={"help": "Maximum absolute value for reward clipping"} ) overlong_reward_penalty: bool = field( default=False, metadata={"help": "Penalty for overlong sequences. Used within DAPO."}, ) overlong_tokens: int | None = field( default=None, metadata={"help": "Number of tokens in the tail that will receive a penalty"}, ) overlong_penalty_factor: float | None = field( default=None, metadata={"help": "Penalty factor for tokens in the tail"}, ) mask_no_eos_with_zero: bool = field( default=False, metadata={ "help": "Mask truncated generations (no EOS token) and exclude from " "training. Incompatible with process-weighted PRM advantage shaping." }, ) # Advantage Estimation discount: float = field( default=1.0, metadata={"help": "Discount factor for future rewards"} ) gae_lambda: float | str = field( default=1.0, metadata={ "help": "Lambda parameter for GAE, either a static float or a dotted " "path to a batch-vectorized per-sample lambda function. The function " "receives a context dict containing effective_token_lengths, " "turn_counts, and timestep_lengths tensors and must return one lambda " "per local trajectory." }, ) gae_lambda_kwargs: dict[str, Any] = field( default_factory=dict, metadata={ "help": "Keyword arguments passed to a custom gae_lambda function. " "Ignored when gae_lambda is a float." }, ) # NOTE: not annotated as Literal["token", "turn"] because the pinned # OmegaConf version rejects Literal annotations in structured configs. # Validated in __post_init__ instead. gae_timestep_unit: str = field( default="token", metadata={ "help": "Timestep unit used by GAE. 'token' preserves standard " "token-level GAE; 'turn' applies discount and lambda once per " "generated turn.", "choices": ["token", "turn"], }, ) adv_norm: NormConfig | None = field( default=None, metadata={"help": "Normalization configuration for advantages."} ) token_rewards_as_adv: bool = field( default=True, metadata={ "help": "How per-token process rewards in 'token_rewards' enter the " "objective. True (default): shape advantages after GAE and advantage " "normalization according to rollout.agent.prm.advantage_shaping, " "keeping process rewards out of returns. False: add one uniform " "reward at each turn boundary before GAE, propagating it to preceding " "tokens and critic returns; this mode is incompatible with GVPO and " "process-weighted shaping." }, ) # Partial rollout groups min_usable_group_size: int | None = field( default=None, metadata={ "help": "Minimum usable rollout slots a prompt group must keep to stay " "trainable when some slots fail or are filtered. None derives the " "minimum from reward_norm/adv_norm: 2 when either uses group " "statistics (1 for a singleton target group), else 1." }, ) # KL Control kl_ctl: float = field(default=0.1, metadata={"help": "KL divergence coefficient"}) kl_estimator: str = field( default="k1", metadata={"help": "KL divergence estimator", "choices": ["k1", "k2", "k3"]}, ) # SAPO (Soft Adaptive Policy Optimization) - https://arxiv.org/abs/2511.20347 use_sapo_loss: bool = field( default=False, metadata={"help": "Use SAPO loss (mutually exclusive with PPO clipping)"}, ) sapo_tau_pos: float = field( default=1.0, metadata={"help": "SAPO temperature for positive advantages"}, ) sapo_tau_neg: float = field( default=1.05, metadata={"help": "SAPO temperature for negative advantages"}, ) # CISPO (Clipped IS-weight Policy Optimization) - MiniMax-M1 https://arxiv.org/abs/2506.13585 use_cispo_loss: bool = field( default=False, metadata={ "help": "Use CISPO loss: clip the importance-sampling weight under " "stop-gradient and keep gradient on every token's log pi (MiniMax-M1 " "Eq. 4-5). Mutually exclusive with SAPO. Uses token-level " "importance-sampling ratios. Requires eps_clip_higher > 0; recommended " "eps_clip=1.0 (single-sided, lower bound 0) with eps_clip_higher=4.0." }, ) loss_aggregation: str = field( default="token_mean", metadata={ "help": "Policy-gradient loss reduction. " "'token_mean': average over valid tokens. " "'seq_mean': average per-response token means. " "'prompt_mean': average per-prompt-group token means while preserving " "the response-level optimizer schedule; groups may span steps. " "'turn_mean': average token means over supported turns. " "'constant': sum of masked token losses divided by " "(n_active_responses * loss_aggregation_divisor). Tree-packed actor " "training currently supports only 'token_mean'.", "choices": [ "token_mean", "seq_mean", "prompt_mean", "constant", "turn_mean", ], }, ) loss_aggregation_divisor: float | None = field( default=None, metadata={ "help": "Positive fixed denominator L for any active constant loss reduction. " "Unused by other loss aggregation modes.", }, ) # Asynchronous RL recompute_logprob: bool = field( default=False, metadata={ "help": "Recompute log probability and replace the log probability returned by inference." }, ) use_decoupled_loss: bool = field( default=False, metadata={ "help": "Use the decoupled loss. Implicitly enables recompute_logprob." }, ) rejection_sampling: RejectionSamplingConfig | None = field( default=None, metadata={ "help": "Rejection sampling configuration for filtering stale samples. " "None disables filtering (equivalent to old behave_imp_weight_mode='disabled'). " "Effective with use_decoupled_loss=True, or with score centering for token-wise ratio TIS clamping (no lower bound) or IcePop masking. Score centering retains its correction and original normalization when a sampled token is rejected." }, ) importance_sampling_level: str = field( default="token", metadata={ "help": "Level at which to compute importance sampling ratios. 'token': per-token ratios (standard PPO). 'sequence': sequence-level geometric mean of per-token ratios (GSPO).", "choices": ["token", "sequence"], }, ) # Proximal Log-Probability Computation Method prox_logp_method: str = field( default=PROX_LOGP_METHOD_RECOMPUTE, metadata={ "help": "Method for computing proximal policy log-probabilities in decoupled PPO. " "Only effective when use_decoupled_loss=True. Options: " "'recompute' (default): Standard decoupled PPO, recompute proximal policy via forward pass. " "'loglinear': Use log-linear interpolation to approximate proximal policy (skip forward pass). " "'metrics': Like 'recompute', but also compute approximation metrics for evaluation. " "'reuse_train_logp': Reuse training forward-pass logprobs as the proximal " "logp (skip the extra forward; requires ppo_n_minibatches=1).", "choices": PROX_LOGP_METHODS_ALL, }, ) # Logging Agent Trajectories log_agent_stats: bool = field( default=False, metadata={"help": "Log statistics for agent trajectories"}, ) log_agent_stats_keys: list[str] = field( default_factory=lambda: [], metadata={"help": "Keys for logging agent trajectory statistics"}, ) # Others max_new_tokens: int = field( default=1024, metadata={"help": "Maximum number of new tokens to generate"}, ) use_score_centering: bool = field( default=False, metadata={ "help": "Use REINFORCE with head/tail score centering instead of clipped PPO. Support: SGLang, FSDP (TP/CP) or Megatron (TP/CP/PP), temperature=1, no sampling truncation." }, ) def _uses_group_statistics(self) -> bool: for normalization in (self.reward_norm, self.adv_norm): if normalization is None: continue if isinstance(normalization, (dict, DictConfig)): if ( normalization.get("mean_level") == "group" or normalization.get("std_level") == "group" ): return True elif normalization.uses_group_statistics: return True return False def resolve_min_usable_group_size(self, target_group_size: int) -> int: """Minimum usable rollout slots a group must keep to stay trainable. An explicit ``min_usable_group_size`` wins. Otherwise group-relative normalization needs at least two group members before partial groups become a hazard; a singleton target group is complete by definition, so it keeps the minimum of one. """ if self.min_usable_group_size is not None: return self.min_usable_group_size if self._uses_group_statistics(): return min(2, target_group_size) return 1 def should_compute_prox_logp(self) -> bool: """Determine if forward pass is needed for proximal log-probabilities. Returns: True if compute_logp() should be called, False to skip. """ from areal.utils.constants import ProxLogpMethod method = ProxLogpMethod(self.prox_logp_method) return (self.use_decoupled_loss and not method.skips_forward_pass()) or ( not self.use_decoupled_loss and self.recompute_logprob ) def __post_init__(self): """Validate PPO actor configuration.""" if not isinstance(self.losses, (dict, DictConfig)): raise ValueError("actor.losses must be a mapping") losses = {} for name, spec in self.losses.items(): if not isinstance(name, str) or not name.isidentifier(): raise ValueError("loss term names must be Python identifiers") if isinstance(spec, LossTermConfig): losses[name] = spec elif isinstance(spec, Mapping): losses[name] = LossTermConfig(**dict(spec)) else: raise ValueError( f"actor.losses.{name} must be a loss configuration mapping" ) self.losses = losses if self.losses and self.m2_threshold is not None: raise ValueError( "Named losses do not support forward-dependent M2PO selection" ) if self.losses and not any( s.enabled and s.coefficient > 0 for s in self.losses.values() ): raise ValueError("At least one active loss term is required") if isinstance(self.advantage, (dict, DictConfig)): self.advantage = AdvantageConfig(**dict(self.advantage)) if not isinstance(self.advantage, AdvantageConfig): raise ValueError("advantage must be an AdvantageConfig or mapping") if isinstance(self.gae_lambda, bool) or not isinstance( self.gae_lambda, int | float | str ): raise ValueError( "gae_lambda must be a float or dotted function path, got " f"{self.gae_lambda!r}" ) if isinstance(self.gae_lambda, str) and not self.gae_lambda: raise ValueError("gae_lambda function path must not be empty") if self.gae_timestep_unit not in {"token", "turn"}: raise ValueError( "gae_timestep_unit must be 'token' or 'turn', got " f"{self.gae_timestep_unit!r}" ) if self.min_usable_group_size is not None: if self.min_usable_group_size < 1: raise ValueError( "min_usable_group_size must be a positive integer, " f"got {self.min_usable_group_size}" ) if self.min_usable_group_size < 2 and self._uses_group_statistics(): raise ValueError( "min_usable_group_size must be at least 2 when reward_norm or " "adv_norm uses group statistics: a lone surviving rollout has " "no group peers to normalize against. Leave it unset to derive " "the minimum instead." ) reward_norm = self.reward_norm if isinstance(reward_norm, (dict, DictConfig)): reward_mean_level = reward_norm.get("mean_level") reward_group_size = reward_norm.get("group_size") else: reward_mean_level = getattr(reward_norm, "mean_level", None) reward_group_size = getattr(reward_norm, "group_size", None) if reward_mean_level == "group" and reward_group_size == 1: warnings.warn( "PPO reward_norm uses mean_level='group' with group_size=1: " "singleton group centering erases the task reward. Disable reward " "centering (mean_level=None) or use group_size >= 2.", UserWarning, stacklevel=2, ) from areal.utils.constants import ProxLogpMethod if ( ProxLogpMethod(self.prox_logp_method) == ProxLogpMethod.REUSE_TRAIN_LOGP and self.ppo_n_minibatches > 1 ): logger.warning( "prox_logp_method='reuse_train_logp' requires ppo_n_minibatches=1, " f"but got ppo_n_minibatches={self.ppo_n_minibatches}. " "With multiple minibatches, weights change between steps, so the " "training forward logprobs would differ from the original policy. " "Forcing ppo_n_minibatches=1; note this changes training dynamics " "to a single optimizer step per PPO update." ) self.ppo_n_minibatches = 1 if self.enable_delta_weight_update and self.ppo_n_minibatches != 1: raise ValueError( "actor.enable_delta_weight_update=true currently requires " "ppo_n_minibatches=1 because separation AdamW inversion " "supports exactly one optimizer step between weight updates; " f"got ppo_n_minibatches={self.ppo_n_minibatches}" ) # Warn if rejection_sampling is configured but use_decoupled_loss is False if ( not self.use_decoupled_loss and not self.use_score_centering and self.rejection_sampling is not None ): logger.warning( "rejection_sampling is configured but use_decoupled_loss=False. " "Filtering will be ignored. Set use_decoupled_loss=True to enable." ) # Warn if decoupled loss is enabled but no rejection sampling configured. # The old default (behave_imp_weight_cap=5.0, mode=token_mask) enabled # filtering implicitly; the new default (rejection_sampling=None) disables # it. This warning helps users who relied on the old defaults. if self.use_decoupled_loss and self.rejection_sampling is None: logger.warning( "use_decoupled_loss=True with rejection_sampling=None: " "staleness filtering is disabled. If you previously relied on " "the default behave_imp_weight_cap=5.0 with token_mask mode, " "restore equivalent behavior with:\n" " rejection_sampling:\n" " level: token\n" " action: mask\n" " metric: ratio\n" " upper: 5.0" ) # Validate SAPO configuration if self.use_sapo_loss: if self.sapo_tau_pos <= 0 or self.sapo_tau_neg <= 0: raise ValueError( f"SAPO temperatures (sapo_tau_pos, sapo_tau_neg) must be positive. " f"Got sapo_tau_pos={self.sapo_tau_pos}, sapo_tau_neg={self.sapo_tau_neg}." ) if self.use_decoupled_loss: raise ValueError( "SAPO is not compatible with `use_decoupled_loss=True`. " "Please set `actor.use_decoupled_loss=false` in your configuration." ) if self.loss_aggregation not in ( "token_mean", "seq_mean", "turn_mean", "prompt_mean", "constant", ): raise ValueError( "loss_aggregation must be 'token_mean', 'seq_mean', " f"'turn_mean', 'prompt_mean', or 'constant', got {self.loss_aggregation!r}." ) effective_reductions = ( { spec.reduction or self.loss_aggregation for spec in self.losses.values() if spec.enabled and spec.coefficient > 0 } if self.losses else {self.loss_aggregation} ) uses_constant = "constant" in effective_reductions if uses_constant: if ( self.loss_aggregation_divisor is None or not math.isfinite(self.loss_aggregation_divisor) or self.loss_aggregation_divisor <= 0 ): raise ValueError( "loss_aggregation_divisor must be a positive finite value " "when any active loss uses constant reduction." ) elif self.loss_aggregation_divisor is not None: raise ValueError( "loss_aggregation_divisor is only used when " "an active loss uses constant reduction." ) if self.enable_tree_training and effective_reductions != {"token_mean"}: raise ValueError( f"Loss reductions {sorted(effective_reductions)} need per-sequence " "boundaries, which tree-packed batches do not carry; tree " "training supports only 'token_mean'." ) # Validate CISPO configuration if self.use_cispo_loss: if self.use_sapo_loss: raise ValueError( "CISPO and SAPO are mutually exclusive surrogates. " "Set at most one of use_cispo_loss / use_sapo_loss." ) if self.eps_clip_higher is None or self.eps_clip_higher <= 0: raise ValueError( "CISPO requires a positive eps_clip_higher (the asymmetric " "upper clip is its defining knob, MiniMax-M1 Eq. 4-5). Got " f"eps_clip_higher={self.eps_clip_higher}." ) if self.importance_sampling_level != "token": raise ValueError( "CISPO only supports importance_sampling_level='token'. " "Sequence-level (GSPO-style) CISPO has no published surrogate." ) super().__post_init__() if self.use_score_centering: if ( not self.backend.startswith(("fsdp:", "megatron:")) or self.enable_tree_training or self.is_critic ): raise ValueError( "Score centering requires an ordinary FSDP or Megatron actor." ) if self.backend.startswith("megatron:") and ( self.megatron.lm_head_loss_chunk_size > 0 or self.megatron.enable_mtp_training or self.megatron.entropy_requires_grad ): raise ValueError( "Megatron score centering requires ordinary logits, no MTP, and diagnostic-only entropy." ) if self.temperature != 1.0 or self.recompute_logprob: raise ValueError( "Score centering requires temperature=1 and recompute_logprob=False to preserve sampler probabilities." ) if ( self.use_sapo_loss or self.use_cispo_loss or self.use_decoupled_loss or self.m2_threshold is not None or self.importance_sampling_level != "token" ): raise ValueError( "Score centering selects token-wise REINFORCE with optional TIS or IcePop; other policy surrogates are unsupported." ) from areal.utils.score_centering import resolve_score_centering_rejection resolve_score_centering_rejection(self.rejection_sampling) @dataclass class PPOCriticConfig(TrainEngineConfig): """Configuration for PPO critic model, a subclass of a TrainEngine.""" ppo_n_minibatches: int = field( default=4, metadata={"help": "Number of minibatches for each PPO update"} ) eps_clip: float = field( default=0.5, metadata={"help": "Clipping factor for value loss"} ) mask_no_eos_with_zero: bool = field( default=False, metadata={ "help": "Mask truncated generations (no EOS token) and exclude from training" }, ) def get_py_cmd(module: str, args: dict[str, Any]): # convert to flags cmd = ["python3", "-m", module] for k, v in args.items(): if v is None or v is False or v == "" or (isinstance(v, list) and not v): continue flag = f"--{k.replace('_', '-')}" if v is True: cmd.append(flag) elif isinstance(v, list): cmd.append(flag) cmd.extend(map(str, v)) else: cmd.append(flag) cmd.append(str(v)) return cmd @dataclass class vLLMConfig: """Configuration for vLLM runtime. Refer to: https://docs.vllm.ai/en/stable/api/index.html for detailed documentation. """ model: str = "" seed: int = 1 skip_tokenizer_init: bool = False enforce_eager: bool = False dtype: str = "bfloat16" distributed_executor_backend: str = "mp" # original max_num_seqs: int = 256 # kv_cache_type: str = "auto" block_size: int = 16 cpu_offload_gb: float = 0 disable_sliding_window: bool = True max_model_len: int | None = 32768 # NOTE: We use no_enable_* prefix (instead of enable_*) because get_py_cmd() # ignores parameters with False values. Setting enable_chunked_prefill=False # or enable_prefix_caching=False has NO effect - vLLM will use its default # values (True). Using no_enable_*=True correctly passes --no-enable-* flags # to vLLM, achieving enable_*=False behavior. # # IMPORTANT: vLLM V1 engine forces enable_chunked_prefill=True by default # for non-pooling tasks (generation tasks). And no_enable_chunked_prefill=True # has NO effect for generation tasks in vLLM v0.11.0. # no_enable_chunked_prefill: bool = False # NOTE: Disables prefix caching (vLLM default is enabled) because it will # make RL training corrupted in single controller mode. no_enable_prefix_caching: bool = True gpu_memory_utilization: float = 0.9 worker_extension_cls: str = ( "areal.engine.vllm_ext.vllm_worker_extension.VLLMWorkerExtension" ) enable_sleep_mode: bool = False uvicorn_log_level: str = "warning" # GDN prefill backend for hybrid models like Qwen3.5; "triton" avoids the # FlashInfer GDN-kernel hang (vLLM #38916). None leaves vLLM's default, so # no flag is emitted and non-GDN models are unaffected. gdn_prefill_backend: str | None = field( default=None, metadata={ "help": "GDN prefill backend for hybrid models like Qwen3.5.", "choices": ["triton", "flashinfer"], }, ) # lora enable_lora: bool = False max_lora_rank: int = 16 # vllm's default max_loras: int = 8 # override default lora_modules: list[str] | None = None # lora_modules is automatically filled @staticmethod def build_args( vllm_config: "vLLMConfig", tp_size: int, pp_size: int, host: str | None = None, port: int | None = None, dist_init_addr: str | None = None, n_nodes: int = 1, node_rank: int = 0, ): args: dict = conf_as_dict(vllm_config) args = dict( # Model and tokenizer tokenizer=vllm_config.model, load_format="auto", trust_remote_code=True, tensor_parallel_size=tp_size, pipeline_parallel_size=pp_size, **args, ) if port is not None: args["port"] = port if host is not None: args["host"] = host # Multi-node support if n_nodes > 1: args["nnodes"] = n_nodes args["node_rank"] = node_rank if dist_init_addr is not None: from areal.utils.network import split_hostport master_host, master_port = split_hostport(dist_init_addr) args["master_addr"] = master_host args["master_port"] = str(master_port) if node_rank > 0: args["headless"] = True return args @staticmethod def build_cmd_from_args(args: dict[str, Any]): return get_py_cmd("areal.engine.vllm_ext.areal_vllm_server", args) @staticmethod def build_cmd( vllm_config: "vLLMConfig", tp_size: int, pp_size: int, host: str | None = None, port: int | None = None, dist_init_addr: str | None = None, n_nodes: int = 1, node_rank: int = 0, ): args = vLLMConfig.build_args( vllm_config=vllm_config, tp_size=tp_size, pp_size=pp_size, host=host, port=port, dist_init_addr=dist_init_addr, n_nodes=n_nodes, node_rank=node_rank, ) return vLLMConfig.build_cmd_from_args(args) # Keep this list aligned with SGLang's deterministic inference documentation: # https://docs.sglang.ai/advanced_features/deterministic_inference.html _SGLANG_DETERMINISTIC_ATTENTION_BACKENDS = frozenset({"flashinfer", "fa3", "triton"}) @dataclass class SGLangConfig: """Configuration for SGLang runtime. Refer to: https://github.com/sgl-project/sglang for detailed documentation. """ model_path: str = "" random_seed: int = 1 skip_tokenizer_init: bool = False disable_cuda_graph: bool = False disable_prefill_cuda_graph: bool | None = field( default=None, metadata={ "help": "Disable only prefill CUDA graphs while retaining decode graphs. " "None omits the flag for compatibility with older SGLang versions; " "requires a runtime supporting --disable-prefill-cuda-graph." }, ) disable_radix_cache: bool = True disable_cuda_graph_padding: bool = False enable_nccl_nvls: bool = False disable_outlines_disk_cache: bool = False disable_custom_all_reduce: bool = False disable_overlap_schedule: bool = False enable_mixed_chunk: bool = False enable_dp_attention: bool = False enable_ep_moe: bool = False enable_torch_compile: bool = False torch_compile_max_bs: int = 32 cuda_graph_max_bs: int | None = None cuda_graph_bs: list[int] | None = None torchao_config: str = "" fp8_gemm_runner_backend: str | None = field( default=None, metadata={ "help": "SGLang FP8 GEMM backend. Qwen3.5 AWEX block-FP8 supports 'triton' or 'deep_gemm'; packed DeepGEMM scales are rebuilt after each weight update." }, ) enable_nan_detection: bool = False enable_p2p_check: bool = False triton_attention_reduce_in_fp32: bool = False triton_attention_num_kv_splits: int = 8 num_continuous_decode_steps: int = 1 load_format: str = "auto" enable_memory_saver: bool = False allow_auto_truncate: bool = False attention_backend: str | None = "fa3" mm_attention_backend: str | None = None linear_attn_decode_backend: str | None = field( default=None, kw_only=True, metadata={ "help": "Linear attention kernel backend for decode. None lets SGLang select the backend." }, ) linear_attn_prefill_backend: str | None = field( default=None, kw_only=True, metadata={ "help": "Linear attention kernel backend for prefill. None lets SGLang select the backend." }, ) enable_deterministic_inference: bool = False enable_multimodal: bool = False mm_process_config: dict[str, Any] | None = field( default=None, kw_only=True, metadata={ "help": "SGLang multimodal preprocessing options keyed by modality " "(image, video, or audio), such as min_pixels and max_pixels for " "images. None uses SGLang defaults." }, ) limit_mm_data_per_request: dict[str, int] | None = field( default=None, metadata={ "help": "Maximum image, video, and audio items per request. Values must " "be non-negative integers; zero disallows that modality. None uses " "SGLang defaults; an empty mapping disables its item-count limits.", }, ) sampling_backend: str | None = None context_length: int | None = 32768 mem_fraction_static: float | None = 0.9 max_running_requests: int | None = None # NOTE: chunked_prefill_size is by default 8192 on GPUs with 80GB mem in SGLang, # but we disable it to avoid precision issues chunked_prefill_size: int | None = -1 max_prefill_tokens: int = 32768 schedule_policy: str = "lpm" schedule_conservativeness: float = 1.0 cpu_offload_gb: int = 0 dtype: str = "bfloat16" kv_cache_dtype: str = "auto" mamba_scheduler_strategy: str | None = None mamba_ssm_dtype: str | None = None max_mamba_cache_size: int | None = None dp_size: int = 1 # only used for dp attention ep_size: int = 1 # lora enable_lora: bool | None = None max_lora_rank: int | None = None max_loaded_loras: int = 8 # override default lora_paths: list[str] | None = None # lora_paths is automatically filled lora_backend: str = "triton" # Speculative decoding (MTP / EAGLE / EAGLE3 / NEXTN). # All None by default so get_py_cmd() emits no flag and SGLang runs standard # decoding. Field names mirror SGLang ServerArgs; underscores -> CLI hyphens. speculative_algorithm: str | None = field( default=None, metadata={ "help": "Speculative decoding algorithm passed to SGLang. None disables " "spec decode. Use the target model's built-in MTP head via 'NEXTN'/'EAGLE', " "or an external draft model with 'EAGLE'/'EAGLE3' + " "speculative_draft_model_path.", "choices": ["EAGLE", "EAGLE3", "NEXTN", "STANDALONE"], }, ) speculative_num_steps: int | None = None speculative_eagle_topk: int | None = None speculative_num_draft_tokens: int | None = None speculative_draft_model_path: str | None = field( default=None, metadata={ "help": "Path to an external draft model. Leave None to use the target " "model's own MTP head (training side must set megatron.enable_mtp=True).", }, ) # Keep a CPU backup of draft weights so the server can start/run before the # MTP/draft weights are synced from training. Required for online MTP training # where draft weights arrive via weight-sync after the server launches. enable_draft_weights_cpu_backup: bool = False # logging log_level: str = "warning" log_level_http: str | None = "warning" log_requests: bool = False log_requests_level: int = 0 enable_cache_report: bool = False show_time_cost: bool = False enable_metrics: bool = True # Exports Prometheus-like metrics # The interval (in decoding iterations) to log throughput # and update prometheus metrics decode_log_interval: int = 1 # Extra loader arguments # NOTE: These arguments will be parsed into a dict json-string # and passed as `model_loader_extra_config` to SGLang. enable_multithread_load: bool = False # Internal field, not exposed to users. enable_return_routed_experts: bool = False def __post_init__(self) -> None: self._validate_mm_limits(self.limit_mm_data_per_request) @staticmethod def _validate_mm_limits(limits: dict[str, int] | None) -> None: if limits is None: return if not isinstance(limits, dict) or any( modality not in {"image", "video", "audio"} or type(limit) is not int or limit < 0 for modality, limit in limits.items() ): raise ValueError( "limit_mm_data_per_request must map image, video, or audio " "to non-negative integers" ) # Use staticmethod to make OmegaConf happy. @staticmethod def build_cmd( sglang_config: "SGLangConfig", tp_size, base_gpu_id, host: str | None = None, port: int | None = None, dist_init_addr: str | None = None, n_nodes: int = 1, node_rank: int = 0, pp_size: int = 1, ): args = SGLangConfig.build_args( sglang_config=sglang_config, tp_size=tp_size, base_gpu_id=base_gpu_id, host=host, port=port, dist_init_addr=dist_init_addr, n_nodes=n_nodes, node_rank=node_rank, pp_size=pp_size, ) return SGLangConfig.build_cmd_from_args(args) @staticmethod def build_cmd_from_args(args: dict[str, Any]): args = dict(args) # SGLang's CLI spelling differs from its ServerArgs field name. backend = args.pop("fp8_gemm_runner_backend", None) if backend is not None: args["fp8_gemm_backend"] = backend return get_py_cmd("areal.v2.inference_service.sglang.launch_server", args) @staticmethod def build_args( sglang_config: "SGLangConfig", tp_size: int, base_gpu_id: int, host: str | None = None, port: int | None = None, dist_init_addr: str | None = None, n_nodes: int = 1, node_rank: int = 0, pp_size: int = 1, ): attention_backend = sglang_config.attention_backend if ( sglang_config.enable_deterministic_inference and attention_backend is not None and attention_backend.lower() not in _SGLANG_DETERMINISTIC_ATTENTION_BACKENDS ): logger.warning( "SGLang deterministic inference is only documented for attention " "backends %s; configured attention_backend=%r may be non-deterministic.", sorted(_SGLANG_DETERMINISTIC_ATTENTION_BACKENDS), attention_backend, ) # Map "all-linear" to "all" args: dict = conf_as_dict(sglang_config) if sglang_config.fp8_gemm_runner_backend is None: args.pop("fp8_gemm_runner_backend") # build_args also accepts DictConfig, which bypasses __post_init__. limits = args.get("limit_mm_data_per_request") SGLangConfig._validate_mm_limits(limits) if limits is not None: args["limit_mm_data_per_request"] = json.dumps( limits, separators=(",", ":") ) mm_process_config = args.get("mm_process_config") if mm_process_config is not None: args["mm_process_config"] = json.dumps( mm_process_config, separators=(",", ":") ) if sglang_config.enable_multithread_load: model_loader_extra_config = dict( enable_multithread_load=sglang_config.enable_multithread_load, ) args["model_loader_extra_config"] = json.dumps( model_loader_extra_config, separators=(",", ":") ) args.pop("enable_multithread_load", None) args = dict( # Model and tokenizer tokenizer_path=sglang_config.model_path, tokenizer_mode="auto", trust_remote_code=True, is_embedding=False, # Other runtime options tp_size=tp_size, # Because we have set CUDA_VISIBLE_DEVICES to a single GPU in each process base_gpu_id=base_gpu_id, nnodes=n_nodes, node_rank=node_rank, # initialization addresses and ports dist_init_addr=dist_init_addr, **args, ) if pp_size > 1: args["pp_size"] = pp_size if host is not None: args["host"] = host if port is not None: args["port"] = port if not pkg_version.is_version_greater_or_equal("sglang", "0.5.10.post1"): raise RuntimeError("Needs sglang>=0.5.10.post1 to run the code.") return args @dataclass class PRMScorerConfig: """Declare one process-reward scorer loaded from a dotted Python path.""" path: str = field( default="", metadata={ "help": "Dotted path to a BaseScorer subclass, for example " "'my_project.scorers.MyScorer'." }, ) weight: float = field( default=1.0, metadata={ "help": "Multiplier applied to this scorer's reward contribution. " "Raw scorer metrics remain unweighted." }, ) enabled: bool = field( default=True, metadata={"help": "Skip this scorer when false."}, ) kwargs: dict = field( default_factory=dict, metadata={"help": "Additional keyword arguments for the scorer constructor."}, ) @dataclass class PRMAdvantageShapingConfig: """Control how direct process signals modify outcome advantages.""" mode: str = field( default="additive", metadata={ "help": "Advantage shaping strategy. 'additive' adds process rewards " "after GAE and advantage normalization. 'gvpo' treats negative process " "rewards as failed-token indicators and applies piecewise GVPO shaping. " "'process_weighted' expects process rewards in [0, 1], scales " "non-negative advantages by them, and replaces negative advantages " "with a positive process reward when one is present. It is incompatible " "with actor.mask_no_eos_with_zero=True.", "choices": ["additive", "gvpo", "process_weighted"], }, ) negative_scale: float = field( default=0.2, metadata={ "help": "GVPO multiplier b for failed tokens with negative outcome " "advantage; their result is (1 + b) * advantage." }, ) zero_penalty: float = field( default=0.4, metadata={ "help": "Negative magnitude assigned by GVPO to failed tokens whose " "outcome advantage is approximately zero." }, ) zero_eps: float = field( default=1e-6, metadata={ "help": "Absolute outcome-advantage tolerance for GVPO's zero branch." }, ) def __post_init__(self) -> None: if self.mode not in {"additive", "gvpo", "process_weighted"}: raise ValueError( "PRM advantage shaping mode must be 'additive', 'gvpo', or " f"'process_weighted', got {self.mode!r}" ) for name in ("negative_scale", "zero_penalty", "zero_eps"): value = getattr(self, name) if not math.isfinite(value) or value < 0: raise ValueError( f"PRM advantage shaping {name} must be finite and non-negative, " f"got {value}" ) @dataclass class PRMConfig: """Process-reward scoring and advantage shaping for agent rollouts.""" enabled: bool = field( default=True, metadata={ "help": "Enable process rewards when scorers are configured. An empty " "scorer list is always a no-op." }, ) advantage_shaping: PRMAdvantageShapingConfig = field( default_factory=PRMAdvantageShapingConfig, metadata={ "help": "How direct process signals are combined with outcome advantages." }, ) scorers: list[PRMScorerConfig] = field( default_factory=list, metadata={"help": "Process-reward scorers whose weighted outputs are summed."}, ) error_policy: str = field( default="reject", metadata={ "help": "On v1 proxy PRM errors: reject the trajectory or keep pre-scoring rewards.", "choices": ["reject", "keep_original"], }, ) def __post_init__(self) -> None: if self.error_policy not in {"reject", "keep_original"}: raise ValueError("PRM error_policy must be reject or keep_original") @dataclass class AgentConfig: """Configuration for agent workflows and the experimental agent service controller. Consolidates proxy settings (mode, parsers, export) with agent-service orchestration (scheduling, auth) into a single flat dataclass. """ agent_cls_path: str = field( default="", metadata={ "help": "Fully-qualified import path for the AgentRunnable implementation." }, ) admin_api_key: str = field( default="areal-admin-key", metadata={ "help": ( "Admin API key for the proxy server and agent-service inter-service auth. " "Used to authenticate management operations (grant_capacity, start_session). " "Cannot be used for chat completions. Each session gets a unique " "API key allocated via start_session. " "WARNING: Change this from the default for non-local deployments." ), }, ) scheduling_spec: tuple[SchedulingSpec, ...] = field( default_factory=lambda: ( SchedulingSpec( gpu=0, cmd="python -m areal.v2.agent_service.guard", ), ), metadata={ "help": "Scheduling spec for agent-service guard workers. Must contain exactly one SchedulingSpec. Use scheduling_spec[0].env_vars for child-process environment variables." }, ) # -- Proxy / workflow settings (formerly OpenAIProxyConfig) ---------------- mode: str = field( default="inline", metadata={ "help": ( "OpenAI proxy mode: 'inline' (in-process), 'subproc' (subprocess), " "or 'online' (external user sessions for online RL training). " "`inline` mode runs the provided agent workflow directly in the same process. " "`subproc` mode launches a separate process to run the agent. " "`online` mode waits for external users to complete sessions via " "the proxy gateway URL, enabling online RL training." ), "choices": ["inline", "subproc", "online"], }, ) tool_call_parser: str = field( default="qwen", metadata={"help": "Parser for tool calls in model output."}, ) tool_call_parser_allow_recovery: bool = field( default=True, metadata={ "help": "Allow permissive tool-call parsing (legacy behavior). " "False currently requires tool_call_parser='qwen3_coder': validate " "the complete XML call structure without tag repairs, partial-call " "extraction, or SGLang/vLLM fallback. Invalid tool protocol is " "cached as a model interaction and returned as non-retryable " "invalid_tool_json. Argument schema validation remains with the tool." }, ) reasoning_parser: str = field( default="qwen3", metadata={"help": "Parser for reasoning content ( tags)."}, ) chat_template_kwargs: dict[str, Any] = field( default_factory=dict, metadata={"help": "Default chat template arguments for proxy requests."}, ) chat_template_type: str = field( default="hf", metadata={ "help": "Chat template type: 'hf' (standard) or 'concat' (multi-turn concatenation).", "choices": ["hf", "concat"], }, ) engine_max_tokens: int | None = field( default=None, metadata={"help": "Maximum total tokens for the engine (prompt + completion)."}, ) turn_discount: float = field( default=1.0, metadata={ "help": "Discount factor for reward propagation in 'individual' " "export. Concat leaves keep their own branch-local outcome reward." }, ) export_style: str = field( default="individual", metadata={ "help": "Export style: 'individual' (all interactions) or 'concat' (leaf nodes only). " "The 'individual' style exports each interaction (input-output-reward) step separately, " "and treats them as independent samples to train the model. " "The 'concat' style exports every leaf as a full root-to-leaf trajectory, " "so branched histories produce multiple training samples. It requires " "token-compatible parent/child prompts (whether valid depends on the tokenizer).", "choices": ["individual", "concat"], }, ) message_preprocessors: list[str] = field( default_factory=list, metadata={ "help": ( "List of message preprocessor class paths applied, in order, to " "Anthropic-compatible `/v1/messages` requests after translating " "them to OpenAI-compatible requests. Native OpenAI " "`/chat/completions` and `/responses` requests are not " "preprocessed. Each entry is a dotted import path to a callable " "class." ), }, ) prefix_matcher: str | None = field( default=None, metadata={ "help": ( "Dotted import path to a custom prefix matcher function for " "InteractionCache parent-child matching. The function must accept " "two list[dict] arguments (candidate prefix, full messages) and " "return bool. When None, exact element-wise equality is used." ), }, ) subproc_max_workers: int = field( default=4, metadata={ "help": "Maximum number of worker processes for subprocess mode execution pool." }, ) drop_retry_orphans: bool = field( default=False, metadata={ "help": "Drop retry-orphan completions before export. " "When upstream Agent SDK times out and retries the same request, " "the proxy records both the orphan (never delivered to the agent) " "and the retry, producing extra leaves in concat-mode export " "(trajectory split). Enable this to discard orphans before reward " "discounting." }, ) session_timeout_seconds: int = field( default=3600, metadata={ "help": "Session timeout in seconds. Sessions inactive longer than this will be garbage collected." }, ) set_reward_finish_timeout: float = field( default=0.0, metadata={ "help": "Timeout in seconds to wait for additional reward updates before finalizing a session." }, ) prm: PRMConfig = field( default_factory=PRMConfig, metadata={ "help": "Process-reward shaping applied by the v1 or v2 proxy on concat " "trajectory export, before interactions are serialized." }, ) def __post_init__(self) -> None: if not self.agent_cls_path: raise ValueError("agent_cls_path must be a non-empty import path") if ( not self.tool_call_parser_allow_recovery and self.tool_call_parser != "qwen3_coder" ): raise ValueError( "tool_call_parser_allow_recovery=False requires " "tool_call_parser='qwen3_coder'" ) if len(self.scheduling_spec) != 1: raise ValueError( f"scheduling_spec must contain exactly 1 SchedulingSpec, got {len(self.scheduling_spec)}" ) if not self.admin_api_key or not self.admin_api_key.strip(): raise ValueError("admin_api_key must not be empty or whitespace-only") if self.set_reward_finish_timeout < 0: raise ValueError( "set_reward_finish_timeout must be non-negative, " f"got {self.set_reward_finish_timeout}" ) @dataclass class BackendRoutingConfig: """Optional v1 trajectory routing through the shared v2 Router service. Set `rollout.backend_routing.enabled: true` with SGLang in single-controller mode. The controller starts a router automatically unless `router_addr` and its `admin_api_key` are supplied through private configuration. New sessions choose a healthy backend with the fewest outstanding execution leases divided by capacity; ties are random. Subsequent requests retain the same backend for cache locality. This score counts in-flight requests, not tokens, active trajectories, or GPU utilization. Capacity normalizes the score without changing native concurrency. Generation goes directly to the backend; the router only handles metadata. Pausing fences admission and drains executions before native generation is paused for weight publication. An ambiguous execution fails the pool and requires restarting the experiment; it is never automatically replayed. """ enabled: bool = False """Route each generation execution; retain existing routing when false.""" router_addr: str | None = None """Reachable existing Router URL; None starts one on the Controller node.""" admin_api_key: str | None = None """Existing Router credential; generated per run for an owned subprocess.""" capacity: int | None = None """Positive normalization capacity; defaults to SGLang max_running_requests.""" metadata_timeout: float = 30.0 """Maximum seconds to resolve a metadata operation with idempotent retries.""" control_timeout: float = 1800.0 """Maximum seconds of backend unavailability or draining; excludes training pauses.""" def __post_init__(self): if self.capacity is not None and self.capacity <= 0: raise ValueError("backend_routing.capacity must be positive") if self.metadata_timeout <= 0 or self.control_timeout <= 0: raise ValueError("backend routing timeouts must be positive") if self.router_addr and not self.admin_api_key: raise ValueError("An existing backend Router requires admin_api_key") @dataclass class InferenceEngineConfig: """Configuration for inference servers, including offpolicyness control.""" experiment_name: str | None = None trial_name: str | None = None fileroot: str | None = field( default=None, metadata={"help": "Root directory for logs and trajectory dumps."}, ) max_concurrent_rollouts: None | int = field( default=None, metadata={ "help": "Maximum number of concurrent rollouts to " "the inference engine. Defaults to consumer_batch_size." }, ) queue_size: None | int = field( default=None, metadata={"help": "Input/Output queue size for async rollout."}, ) consumer_batch_size: int = field( default=1, metadata={"help": "Batch size for consuming rollouts from the queue."}, ) max_head_offpolicyness: int = field( default=0, metadata={ "help": "Maximum off-policyness for the head. " "If the current version is more than this many versions behind, " "the request will not be accepted.", }, ) mask_stale_tokens: bool = field( default=False, metadata={ "help": "Mask actor loss targets whose token policy-version age exceeds " "max_head_offpolicyness. Disabled by default to preserve existing " "training behavior; does not reject rollouts or change scheduling." }, ) enable_rollout_tracing: bool = field( default=False, metadata={ "help": "Whether to output verbose tracing messages for each generation request." }, ) deterministic_sampling: bool = field( default=False, metadata={ "help": "Use stable request seeds for internal OpenAI-proxy/data-proxy " "sessions, canonical group ordering, and task-ID ordering of completed " "rollout results. Concurrent SGLang generation also requires " "sglang.enable_deterministic_inference. End-to-end determinism is only " "supported with max_head_offpolicyness=0." }, ) serialize_group_samples: bool = field( default=False, metadata={ "help": "Run RolloutControllerV2 samples within each group sequentially " "instead of concurrently. This provides stable within-group member " "submission order at the cost of rollout throughput; it does not " "serialize requests across groups." }, ) check_trajectory_format: bool = field( default=False, metadata={ "help": "Whether to check the format of produced trajectories of a customized workflow. Useful when debugging the workflow in isolation. Should be False during RL training." }, ) tokenizer_path: str = field( default="", metadata={"help": "Path to tokenizer for trajectory text decoding."}, ) dump_to_file: bool = field( default=False, metadata={"help": "Whether to dump the trajectories to files under fileroot."}, ) setup_timeout: float = field( default=300.0, metadata={ "help": "Timeout in seconds of connecting to remote servers or launching local servers." }, ) workers_ready_timeout: float = field( default=30.0, metadata={ "help": "Timeout (seconds) for initialize() to wait for guards to be ready." }, ) request_timeout: float = field( default=3600, metadata={"help": "Timeout for HTTP requests."} ) request_retries: int = field( default=3, metadata={"help": "Number of retries for failed requests."} ) pause_grace_period: float = field( default=0.0, metadata={ "help": "The grace period after calling /pause_generation. Wait until all requests have been dropped." }, ) scheduling_spec: tuple[SchedulingSpec, ...] = field( default_factory=lambda: ( SchedulingSpec(cmd="python -m areal.infra.rpc.rpc_server"), ), metadata={ "help": "inference engine schedule specs. Can accept 1 or 2 SchedulingSpec: " "if 1 spec provided, it's used for both worker and engine, engine is embedded in the worker; " "if 2 specs provided, first one is for worker, second one is for engine. " "Currently only used by the RolloutController." }, ) # Backend and parallelism (new per-engine config) backend: str = field( default=MISSING, metadata={ "help": "Backend and parallelism strategy. Must include an explicit backend prefix, " "e.g. 'sglang:d4', 'vllm:d2t4'. Required." }, ) scheduling_strategy: SchedulingStrategy = field( default_factory=SchedulingStrategy, metadata={ "help": "The scheduling strategy of this InferenceEngine, either separation or colocation. " "Currently only used by the RolloutController." }, ) use_lora: bool = field( default=False, metadata={"help": "Whether to use LoRA. Should be same as actors LORA option."}, ) lora_name: str = field( default="", metadata={ "help": "LoRA adapter name the rollout backend serves. Generation " "requests select the adapter by this name (plus the weight version). " "Usually left empty and auto-filled from gconfig.lora_name by " "PPOConfig.__post_init__ so load and request sides stay in sync." }, ) agent: AgentConfig = field( default_factory=lambda: AgentConfig( agent_cls_path="areal.experimental.openai.proxy.online_agent._OnlineAgent" ), metadata={ "help": "Agent workflow configuration used by inference-service rollouts." }, ) return_routed_experts: bool = field( default=False, metadata={ "help": "Return routed expert indices for MoE models. Effective only when using SGLang engine with MoE models." }, ) # v2 controller options _version: str = field( default="v1", metadata={ "help": "Rollout controller implementation version. Use 'v1' for legacy RolloutController, 'v2' for RolloutControllerV2.", "choices": ["v1", "v2"], }, ) model: str = field( default="default", metadata={"help": "Model name exposed through the inference-service gateway."}, ) routing_strategy: str = field( default="round_robin", metadata={"help": "Routing strategy for the inference-service router."}, ) backend_routing: BackendRoutingConfig = field(default_factory=BackendRoutingConfig) """v1 direct generation routing; independent of DataProxy owner routing.""" poll_interval: float = field( default=5.0, metadata={ "help": "Health-poll interval in seconds for the inference-service router." }, ) admin_api_key: str = field( default="areal-admin-key", metadata={ "help": "Admin API key used by the inference-service gateway, router, and data proxies." }, ) api_url: str | None = field( default=None, metadata={ "help": "External OpenAI-compatible base URL for inference-service external model mode." }, ) provider_api_key: str | None = field( default=None, metadata={"help": "API key for the external OpenAI-compatible provider."}, ) def __post_init__(self): """Validate scheduling_spec length.""" if len(self.scheduling_spec) not in (1, 2): raise ValueError( f"scheduling_spec must contain 1 or 2 SchedulingSpec, " f"got {len(self.scheduling_spec)}" ) if self._version not in ("v1", "v2"): raise ValueError( f"_version must be either 'v1' or 'v2', got '{self._version}'" ) if not self.admin_api_key or not self.admin_api_key.strip(): raise ValueError("admin_api_key must not be empty or whitespace-only") if self.deterministic_sampling and self.max_head_offpolicyness > 0: logger.warning( "deterministic_sampling=True with max_head_offpolicyness=%d does " "not guarantee deterministic task-to-weight-version mapping; " "set max_head_offpolicyness=0 for end-to-end determinism.", self.max_head_offpolicyness, ) if ( self._version == "v2" and self.agent is not None and self.agent.admin_api_key != "areal-admin-key" ): logger.warning( "rollout.agent.admin_api_key is ignored by rollout controller v2; " "use rollout.admin_api_key instead." ) @dataclass class _Timer: experiment_name: str = MISSING trial_name: str = MISSING fileroot: str = MISSING freq_epochs: int | None = field( default=None, metadata={ "help": "Trigger frequency in epochs. None disables epoch-based saving." }, ) freq_steps: int | None = field( default=None, metadata={ "help": "Trigger frequency in steps. None disables step-based saving." }, ) freq_secs: int | None = field( default=None, metadata={ "help": "Trigger frequency in seconds. None disables time-based saving." }, ) @dataclass class EvaluatorConfig(_Timer): """Configuration for model evaluation scheduling and timing.""" eval_before_train: bool = field( default=False, metadata={ "help": "Run one evaluation before training begins, then continue with the configured evaluation frequency.", }, ) @dataclass class SaverConfig(_Timer): """Configuration for model checkpoint saving scheduling and timing.""" mode: str = field( default="auto", metadata={ "help": "Checkpoint save mode for HF saves. " "'auto': use async for Archon engine, sync for others (default). " "'sync': always synchronous. " "'async': always process-based async with pinned memory staging, " "extra CPU pinned memory " "proportional to per-rank model shard size " "(e.g., ~17.5GB/rank for 70B model on 8 GPUs). " "Non-Archon engines fall back to sync with a warning.", "choices": ["auto", "sync", "async"], }, ) def __post_init__(self): valid_modes = {"auto", "sync", "async"} if self.mode not in valid_modes: raise ValueError(f"Invalid mode '{self.mode}'. Valid: {valid_modes}") @dataclass class RecoverConfig(_Timer): """Configuration for experiment recovery and fault tolerance.""" mode: str = field( default="disabled", metadata={ "help": "Recovery mode for the launcher. " "Options: " "'on' or 'auto': Automatically recover from previous runs if recover info and checkpoints are available. " "'off' or 'disabled': Never recover from previous runs." }, ) retries: int = field( default=3, metadata={"help": "Number of recovery retries when recovery is enabled."}, ) no_save_optim: bool = field( default=False, metadata={ "help": "Do not save optimizer state in recovery checkpoints. " "Shrinks checkpoints and speeds up saving, but recovery then " "resumes with a freshly initialized optimizer (Adam moments " "reset), which can destabilize training. Leave this off unless " "the run never needs to resume optimizer state, e.g. profiling." }, ) no_load_optim: bool = field( default=False, metadata={ "help": "Do not load optimizer state when recovering from checkpoint. " "Same caveat as no_save_optim: training resumes with reset Adam " "moments." }, ) def __post_init__(self): valid_modes = {"on", "off", "auto", "disabled"} if self.mode not in valid_modes: raise ValueError( f"Invalid recover mode '{self.mode}'. " f"Valid options: {valid_modes}. " f"Note: 'fault' and 'resume' modes have been removed." ) @dataclass class WandBConfig: """Configuration for Weights & Biases experiment tracking.""" mode: str = field( default="disabled", metadata={ "help": "Tracking mode. One of 'online', 'offline', 'disabled', or 'shared'.", "choices": ["online", "offline", "disabled", "shared"], }, ) wandb_base_url: str = "" wandb_api_key: str = "" entity: str | None = None project: str | None = None name: str | None = None job_type: str | None = None group: str | None = None notes: str | None = None tags: list[str] | None = None config: dict | None = None id_suffix: str | None = "train" def __post_init__(self): """Validate WandB configuration.""" valid_modes = ("online", "offline", "disabled", "shared") if self.mode not in valid_modes: raise ValueError( f"Invalid wandb mode: '{self.mode}'. Must be one of: {', '.join(valid_modes)}." ) @dataclass class SwanlabConfig: """Configuration for SwanLab experiment tracking and monitoring.""" project: str | None = None name: str | None = None config: dict | None = None logdir: str | None = None mode: str = field( default="disabled", metadata={ "help": "Tracking mode. One of 'cloud', 'local', 'disabled', or 'offline'.", "choices": ["cloud", "local", "disabled", "offline"], }, ) # set None to prevent info-leak in docs api_key: str | None = None def __post_init__(self): """Validate SwanLab configuration.""" valid_modes = ("cloud", "local", "disabled", "offline") if self.mode not in valid_modes: raise ValueError( f"Invalid swanlab mode: '{self.mode}'. Must be one of: {', '.join(valid_modes)}." ) if self.api_key is None: self.api_key = os.getenv("SWANLAB_API_KEY") @dataclass class TensorBoardConfig: """Configuration for TensorBoard logging and visualization.""" path: str | None = None @dataclass class TrackioConfig: """Configuration for Trackio experiment tracking (Hugging Face). Trackio is a lightweight, local-first experiment tracking library with a wandb-compatible API. Dashboards can be viewed locally or deployed to Hugging Face Spaces. See: https://github.com/gradio-app/trackio """ mode: str = "disabled" """Tracking mode. One of "disabled", "online", or "local".""" project: str | None = None """Project name. Defaults to experiment_name if not set.""" name: str | None = None """Run name. Defaults to trial_name if not set.""" space_id: str | None = None """HF Space ID for remote dashboard deployment (e.g. "user/my-space"). When set, metrics are also pushed to the specified Hugging Face Space.""" def __post_init__(self): """Validate Trackio configuration.""" valid_modes = {"disabled", "online", "local"} if self.mode not in valid_modes: raise ValueError( f"Invalid trackio mode: '{self.mode}'. Must be one of {valid_modes}." ) @dataclass class StatsLoggerConfig: """Configuration for experiment statistics logging and tracking services.""" experiment_name: str = MISSING trial_name: str = MISSING fileroot: str = MISSING wandb: WandBConfig = field( default_factory=WandBConfig, metadata={"help": "Weights & Biases configuration."}, ) swanlab: SwanlabConfig = field( default_factory=SwanlabConfig, metadata={"help": "SwanLab configuration."}, ) tensorboard: TensorBoardConfig = field( default_factory=TensorBoardConfig, metadata={"help": "TensorBoard configuration. Only 'path' field required."}, ) trackio: TrackioConfig = field( default_factory=TrackioConfig, metadata={"help": "Trackio configuration (Hugging Face experiment tracking)."}, ) @dataclass class SessionTracerConfig: """Configuration for per-session lifecycle tracing.""" enabled: bool = field( default=False, metadata={ "help": ( "Enable per-session lifecycle tracing alongside perf events. " "When true, session metadata is captured to sessions.jsonl." ) }, ) flush_threshold: int = field( default=256, metadata={ "help": ( "Flush session trace records once this many entries are ready. " "Values <= 0 fall back to 1." ) }, ) @dataclass class MemoryProfilerConfig: """CUDA memory snapshot profiling configuration. Attributes: profile_steps: Steps at which to record memory snapshots. max_entries: Max entries for torch.cuda.memory._record_memory_history. """ profile_steps: list[int] = field( default_factory=lambda: [0, 1], metadata={"help": "List of global steps to capture memory snapshots."}, ) max_entries: int = field( default=100000, metadata={"help": "Max entries for memory history ring buffer."}, ) @dataclass class PerfTracerConfig: """Configuration for perf tracer emission.""" experiment_name: str = MISSING trial_name: str = MISSING fileroot: str = MISSING enabled: bool = field( default=False, metadata={ "help": ( "Explicitly enable or disable perf tracing. Set to true to capture perf traces." ) }, ) save_interval: int = field( default=1, metadata={ "help": ( "Flush trace events to disk every N calls to save(step=...). " "A value of 1 writes on every step; values <= 0 fall back to 1." ) }, ) profile_steps: list[int] | None = field( default=None, metadata={ "help": ( "List of step numbers at which to capture detailed profiling traces. " "If None, no detailed profiling traces are captured." ) }, ) session_tracer: SessionTracerConfig | None = field( default=None, metadata={"help": "Session tracing configuration."}, ) @dataclass class NameResolveConfig: """Configuration for distributed name resolution and service discovery.""" type: str = field( default="nfs", metadata={ "help": "Type of the distributed KV store for name resolving.", "choices": ["nfs", "etcd3", "ray"], }, ) nfs_record_root: str = field( default="/tmp/areal/name_resolve", metadata={ "help": "Record root for NFS name resolving. Should be available on all nodes." }, ) etcd3_addr: str = field( default="localhost:2379", metadata={"help": "Address of the ETCD3 server."} ) ray_actor_name: str = field( default="ray_kv_store", metadata={"help": "Name of the distributed Ray KV store."}, ) @dataclass class ClusterSpecConfig: """Configuration for cluster specification and distributed computing setup.""" name_resolve: NameResolveConfig = field( default_factory=NameResolveConfig, metadata={"help": "Name resolving configuration."}, ) cluster_name: str = field( default="local", metadata={"help": "Name of the cluster. Used to set specific environs."}, ) fileroot: str = field( default="/tmp/areal/", metadata={ "help": "Root for logs and checkpoints. Should be available on all nodes." }, ) n_nodes: int = field( default=32, metadata={ "help": "The size of the cluster. Used to decide slurm hostname suffix." }, ) n_gpus_per_node: int = field( default=8, metadata={"help": "Number of GPUs per node (physical)."}, ) ray_port: int = field( default=6379, metadata={ "help": "Port of the Ray head (GCS). Used by the in-package Ray " "bootstrap of the Ray launcher when assembling a multi-node " "cluster inside a platform job. Must be between 1 and 65535; " "dynamic port selection with 0 is not supported." }, ) ray_dashboard_port: int = field( default=8265, metadata={"help": "Port of the Ray dashboard on the head node."}, ) ray_bootstrap_timeout_seconds: int = field( default=900, metadata={ "help": "How long the Ray bootstrap head waits for all " "cluster.n_nodes nodes to join before failing." }, ) @staticmethod def validate_ray_port(ray_port: int) -> None: if ( not isinstance(ray_port, int) or isinstance(ray_port, bool) or not 1 <= ray_port <= 65535 ): raise ValueError( "cluster.ray_port must be an integer between 1 and 65535; " "0 is unsupported because workers cannot discover a " f"dynamically selected Ray head port. Got {ray_port!r}." ) def __post_init__(self) -> None: self.validate_ray_port(self.ray_port) @dataclass class SchedulerConfig: """Configuration for worker scheduling. Used in the single-controller mode. Experimental.""" type: str | None = field(default=None) endpoint: str = field(default="http://localhost:8081") deploy_mode: str = field(default="separation") functioncall_service_domain: str = field(default="http://localhost:8080") reward_functioncall_config: dict = field(default_factory=dict) reward_model_path: str = field(default="") reward_model_service_url: str = field(default="http://localhost:30000/classify") @dataclass class DatasetSourceConfig: """One source in a dataset mixture.""" path: str = field( default=MISSING, metadata={"help": "Local path or HuggingFace name for this dataset source."}, ) type: str = field( default=MISSING, metadata={"help": "Training data type, for example 'rl'."}, ) teacher_group: str | None = field( default=None, metadata={"help": "Optional MOPD teacher group applied to this entire source."}, ) split: str | None = field( default=None, metadata={"help": "Optional split override for this dataset source."}, ) max_length: int | None = field( default=None, metadata={"help": "Optional maximum sequence length for this source."}, ) dataset_kwargs: dict[str, Any] = field( default_factory=dict, metadata={"help": "Extra keyword arguments for this source's loader."}, ) def __post_init__(self) -> None: for name in ("path", "type"): value = getattr(self, name) if not isinstance(value, str) or not value.strip() or value == MISSING: raise ValueError(f"dataset source {name} must be a non-empty string") if self.teacher_group is not None and ( not isinstance(self.teacher_group, str) or not self.teacher_group.strip() ): raise ValueError( "dataset source teacher_group must be a non-empty string or null" ) @dataclass class _DatasetConfig: """Configuration for dataset loading and preprocessing.""" split: str = field( default="train", metadata={"help": "Dataset split to use, e.g., 'train', 'test'."}, ) path: str | None = field( default=None, metadata={"help": "Path to one dataset. Mutually exclusive with sources."}, ) type: str | None = field( default=None, metadata={ "help": "Training data type for path. Mutually exclusive with sources." }, ) sources: list[DatasetSourceConfig] = field( default_factory=list, metadata={ "help": "Dataset mixture sources. MOPD requires every source to declare " "a teacher_group." }, ) mixture_sampling_policy: str = field( default="proportional", metadata={ "help": ( "How a routed mixture represents sources in one epoch: " "'proportional' preserves source-size proportions; 'uniform' " "balances source counts by deterministically cycling shorter sources." ), "choices": ["proportional", "uniform"], }, ) batch_size: int = field( default=1, metadata={"help": "Batch size for the dataloader"} ) shuffle: bool = field( default=True, metadata={"help": "Whether to shuffle the dataset"} ) pin_memory: bool = field( default=False, metadata={ "help": "Pin memory for faster data loading (set True for GPU training)" }, ) num_workers: int = field( default=0, metadata={"help": "Number of worker processes for data loading"} ) num_dataset_workers: int = field( default=1, metadata={ "help": "Number of remote data-service worker processes to launch when using scheduling_spec." }, ) drop_last: bool = field( default=True, metadata={"help": "Drop the last incomplete batch"} ) max_length: int | None = field( default=None, metadata={ "help": "Maximum token length of sequences in dataset. Longer sequences are filtered out." }, ) dataset_kwargs: dict[str, Any] = field( default_factory=dict, metadata={ "help": "Additional keyword arguments for dataset loading. " "These are passed to the dataset loading function `get_custom_dataset`." }, ) scheduling_spec: SchedulingSpec | None = field( default_factory=lambda: SchedulingSpec( cpu=1, gpu=0, mem=10, cmd="python3 -m areal.infra.rpc.guard" ), metadata={ "help": "Scheduling spec for remote data loading workers. " "If set, dataset loading will be offloaded to a data service with remote workers." }, ) setup_timeout: float = field( default=120.0, metadata={ "help": "Timeout in seconds for the data service to load and register a dataset. " "Increase this value when loading large datasets for the first time " "(e.g. HuggingFace datasets that require downloading and preprocessing)." }, ) def __post_init__(self) -> None: if self.mixture_sampling_policy not in ("proportional", "uniform"): raise ValueError( "mixture_sampling_policy must be 'proportional' or 'uniform', " f"got {self.mixture_sampling_policy!r}" ) if self.sources and (self.path is not None or self.type is not None): raise ValueError("dataset path/type cannot be combined with sources") @dataclass class TrainDatasetConfig(_DatasetConfig): """Configuration for training dataset loading and preprocessing.""" @dataclass class ValidDatasetConfig(_DatasetConfig): """Configuration for validation dataset loading and preprocessing. It has different default values with `TrainDatasetConfig`. `shuffle` and `drop_last` default to False. """ split: str = field( default="test", metadata={"help": "Dataset split to use, e.g., 'train', 'test'."}, ) shuffle: bool = field( default=False, metadata={"help": "Whether to shuffle the dataset"} ) drop_last: bool = field( default=False, metadata={"help": "Drop the last incomplete batch"} ) @dataclass class BaseExperimentConfig: """Base configuration class for all experiment types with common settings.""" # NOTE: we need this unified config class because different experiments # have different config structures, e.g., GRPO has two engine configs, # but SFT only has a single one. We use subclasses to represent these structures. experiment_name: str = field( default=MISSING, metadata={"help": "Name of the experiment (no '_' or '/'). Required."}, ) trial_name: str = field( default=MISSING, metadata={"help": "Name of the trial (no '-' or '/'). Required."}, ) cluster: ClusterSpecConfig = field( default_factory=ClusterSpecConfig, metadata={"help": "Cluster specification. Mainly used by slurm."}, ) allocation_mode: str = field( default="", metadata={ "help": "DEPRECATED: Use per-engine 'backend' fields instead (e.g., actor.backend, rollout.backend). " "Legacy pattern-based GPU parallel strategy allocation mode. " "Only used by SPMD launchers (local/ray/slurm). Manual migration to per-engine 'backend' fields is required.", }, ) seed: int = field(default=1, metadata={"help": "Random seed for reproducibility."}) enable_offload: bool = field( default=False, metadata={ "help": "Whether to enable training offload using torch_memory_saver. " "This requires setting up the environment for TMS (e.g., via LD_PRELOAD)." }, ) total_train_epochs: int = field( default=1, metadata={"help": "Total number of epochs to train the model."} ) total_train_steps: int | None = field( default=None, metadata={ "help": "Terminate training after this number of steps. " "For benchmarking purposes only. None indicates normal training." }, ) total_train_n_seqs: int | None = field( default=None, metadata={ "help": "Terminate training after consuming this number of samples. " "For benchmarking purposes only. None indicates normal training." }, ) tokenizer_path: str = field( default="", metadata={"help": "Path to the tokenizer."}, ) train_dataset: TrainDatasetConfig = field(default_factory=TrainDatasetConfig) valid_dataset: ValidDatasetConfig | None = field(default=None) saver: SaverConfig = field(default_factory=SaverConfig) evaluator: EvaluatorConfig = field(default_factory=EvaluatorConfig) stats_logger: StatsLoggerConfig = field(default_factory=StatsLoggerConfig) perf_tracer: PerfTracerConfig | None = field( default=None, metadata={"help": "Performance tracer configuration. None means disabled."}, ) memory_profiler: MemoryProfilerConfig | None = field( default=None, metadata={ "help": "Memory snapshot profiler configuration. None means disabled." }, ) recover: RecoverConfig = field(default_factory=RecoverConfig) sglang: SGLangConfig = field(default_factory=SGLangConfig) vllm: vLLMConfig = field(default_factory=vLLMConfig) scheduler: SchedulerConfig = field(default_factory=SchedulerConfig) post_exit_hook: str = field( default="", metadata={ "help": "Shell command run after launcher shutdown. " "LOG_DIR is injected; failures are logged and ignored." }, ) def __post_init__(self): """Validate training configuration.""" if self.total_train_epochs <= 0: raise ValueError( f"total_train_epochs must be positive, got {self.total_train_epochs}" ) @dataclass class SFTConfig(BaseExperimentConfig): """Configuration for Supervised Fine-Tuning (SFT) experiments.""" actor: TrainEngineConfig = field(default_factory=TrainEngineConfig) @dataclass class RWConfig(BaseExperimentConfig): """Configuration for Reward Model (RW) training experiments.""" actor: TrainEngineConfig = field(default_factory=TrainEngineConfig) def __post_init__(self): super().__post_init__() if not getattr(self.actor, "is_critic", False): raise ValueError( "RWConfig requires actor.is_critic=True for reward modeling. " "Set 'actor.is_critic: true' in your YAML config." ) @dataclass class DPOEngineConfig(TrainEngineConfig): """Engine configuration for DPO training, extending TrainEngineConfig with DPO-specific fields.""" beta: float = field( default=0.1, metadata={"help": "KL penalty coefficient for DPO loss."}, ) loss_type: str = field( default="sigmoid", metadata={ "help": "DPO loss variant. " "'sigmoid': original DPO loss (Rafailov et al. 2023). " "'ipo': Identity Preference Optimization with per-token length normalization (Azar et al. 2023).", "choices": ["sigmoid", "ipo"], }, ) def __post_init__(self): super().__post_init__() _valid = {"sigmoid", "ipo"} if self.loss_type not in _valid: raise ValueError( f"Unsupported DPO loss_type '{self.loss_type}'. " f"Must be one of {sorted(_valid)}." ) @dataclass class DPOConfig(BaseExperimentConfig): """Configuration for Direct Preference Optimization (DPO) experiments.""" actor: DPOEngineConfig = field(default_factory=DPOEngineConfig) ref: DPOEngineConfig = field(default_factory=DPOEngineConfig) def __post_init__(self): super().__post_init__() if getattr(self.actor, "is_critic", False): raise ValueError( "DPOConfig requires a language model (is_critic=False). " "Remove 'actor.is_critic: true' from your YAML config." ) @dataclass class TeacherConfig: engine_type: str = field( default="rollout", metadata={ "help": "Teacher engine type. 'rollout' uses inference engine scoring; " "'train' uses the legacy train-engine teacher path.", "choices": ["rollout", "train"], }, ) rollout: InferenceEngineConfig | None = field(default=None) train: PPOActorConfig | None = field( default=None, metadata={ "help": "Legacy train-engine teacher config. Required when engine_type='train'." }, ) path: str = field( default="", metadata={ "help": "Teacher model path. If set, overrides shared rollout backend model path." }, ) offload: bool = field( default=False, metadata={"help": "Whether to offload teacher rollout model between steps"}, ) rl_loss_weight: float = field( default=1.0, metadata={"help": "RL loss weight"}, ) distill_loss_weight: float = field( default=0.005, metadata={"help": "Distillation loss weight"}, ) def __post_init__(self): if self.rollout is not None and self.train is not None: warnings.warn( "Both teacher.rollout and teacher.train are configured; " f"teacher.engine_type={self.engine_type!r} selects which one is used.", stacklevel=2, ) if self.engine_type == "rollout" and self.rollout is None: raise ValueError( "teacher.rollout must be provided when teacher.engine_type='rollout'." ) if self.engine_type == "train" and self.train is None: raise ValueError( "teacher.train must be provided when teacher.engine_type='train'." ) @dataclass class MOPDTeacherSpec: """Checkpoint specification for one MOPD teacher.""" path: str = field( default=MISSING, metadata={"help": "Local or shared-filesystem teacher checkpoint path."}, ) def __post_init__(self): if not isinstance(self.path, str) or not self.path.strip(): raise ValueError("MOPD teacher path must be a non-empty string") @dataclass class MOPDTeacherManagerConfig: """Checkpoint provider configuration for phase-scoped MOPD teachers.""" type: str = field( default="disk", metadata={ "help": "Teacher checkpoint provider.", "choices": ["disk", "local_memory"], }, ) staging_root: str = field( default="/dev/shm/areal-mopd", metadata={"help": "Node-local staging root for local_memory providers."}, ) min_free_bytes: int | None = field( default=None, metadata={ "help": "Optional minimum free space required after staging a checkpoint." }, ) def __post_init__(self): if self.type not in ("disk", "local_memory"): raise ValueError( "mopd.manager.type must be either 'disk' or 'local_memory', " f"got {self.type!r}" ) if not isinstance(self.staging_root, str) or not self.staging_root.strip(): raise ValueError("mopd.manager.staging_root must be a non-empty string") if self.min_free_bytes is not None and self.min_free_bytes < 0: raise ValueError( "mopd.manager.min_free_bytes must be non-negative or None, " f"got {self.min_free_bytes}" ) @dataclass class MOPDLossConfig: """Coefficients for joint RL and multi-teacher distillation training.""" rl_coefficient: float = field( default=0.0, metadata={"help": "Coefficient applied to the RL objective."}, ) distillation_coefficient: float = field( default=1.0, metadata={"help": "Coefficient applied to the MOPD objective."}, ) importance_ratio_cap: float = field( default=5.0, metadata={"help": "Positive cap applied to the behavior-policy ratio."}, ) def __post_init__(self): for name in ("rl_coefficient", "distillation_coefficient"): value = getattr(self, name) if not isinstance(value, (int, float)) or isinstance(value, bool): raise ValueError(f"mopd.loss.{name} must be a finite number") if not math.isfinite(value) or value < 0: raise ValueError( f"mopd.loss.{name} must be finite and non-negative, got {value}" ) if self.rl_coefficient == 0 and self.distillation_coefficient == 0: raise ValueError("MOPD loss coefficients cannot both be zero") if ( not isinstance(self.importance_ratio_cap, (int, float)) or isinstance(self.importance_ratio_cap, bool) or not math.isfinite(self.importance_ratio_cap) or self.importance_ratio_cap <= 0 ): raise ValueError( "mopd.loss.importance_ratio_cap must be finite and positive" ) @dataclass class MOPDTeacherEngineConfig(TrainEngineConfig): """Forward-only scoring engine configuration used by MOPD teachers.""" disable_dropout: bool = field( default=True, metadata={"help": "Disable dropout for deterministic teacher scoring."}, ) optimizer: OptimizerConfig | None = field( default=None, metadata={"help": "MOPD scoring teachers do not construct an optimizer."}, ) def __post_init__(self) -> None: super().__post_init__() if self.optimizer is not None: raise ValueError("MOPDTeacherEngineConfig.optimizer must be null") if not self.disable_dropout: raise ValueError("MOPDTeacherEngineConfig.disable_dropout must be true") @dataclass class MOPDConfig: """Configuration for multi-teacher on-policy distillation.""" teachers: dict[str, MOPDTeacherSpec] = field(default_factory=dict) teacher_groups: dict[str, dict[str, float]] = field(default_factory=dict) teacher_engine: MOPDTeacherEngineConfig = field( default_factory=MOPDTeacherEngineConfig ) manager: MOPDTeacherManagerConfig = field(default_factory=MOPDTeacherManagerConfig) loss: MOPDLossConfig = field(default_factory=MOPDLossConfig) def __post_init__(self): if not self.teachers: raise ValueError("mopd.teachers must not be empty") if not self.teacher_groups: raise ValueError("mopd.teacher_groups must not be empty") for teacher_id, teacher in self.teachers.items(): if not isinstance(teacher_id, str) or not teacher_id.strip(): raise ValueError("mopd teacher ids must be non-empty strings") if re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_.-]*", teacher_id) is None: raise ValueError( "mopd teacher ids must be filename-safe and match " "[A-Za-z0-9][A-Za-z0-9_.-]*" ) if not isinstance(teacher, MOPDTeacherSpec): raise ValueError( f"mopd.teachers[{teacher_id!r}] must be an MOPDTeacherSpec" ) for teacher_group, weights in self.teacher_groups.items(): if not isinstance(teacher_group, str) or not teacher_group.strip(): raise ValueError("mopd teacher group ids must be non-empty strings") if not weights: raise ValueError( f"mopd.teacher_groups[{teacher_group!r}] must not be empty" ) has_positive_weight = False for teacher_id, weight in weights.items(): if teacher_id not in self.teachers: raise ValueError( f"mopd.teacher_groups[{teacher_group!r}] references " f"unknown teacher {teacher_id!r}" ) if not isinstance(weight, (int, float)) or isinstance(weight, bool): raise ValueError( f"mopd.teacher_groups[{teacher_group!r}]" f"[{teacher_id!r}] must be a " "finite non-negative number" ) if not math.isfinite(weight) or weight < 0: raise ValueError( f"mopd.teacher_groups[{teacher_group!r}]" f"[{teacher_id!r}] must be finite " f"and non-negative, got {weight}" ) has_positive_weight = has_positive_weight or weight > 0 if not has_positive_weight: raise ValueError( f"mopd.teacher_groups[{teacher_group!r}] must contain at least " "one positive weight" ) @dataclass class PPOConfig(BaseExperimentConfig): """Configuration for Proximal Policy Optimization (PPO) reinforcement learning experiments.""" gconfig: GenerationHyperparameters = field( default_factory=GenerationHyperparameters ) eval_gconfig: GenerationHyperparameters | None = field( default=None, metadata={ "help": "Generation hyperparameters for evaluation. If None, use gconfig." }, ) rollout: InferenceEngineConfig = field(default_factory=InferenceEngineConfig) actor: PPOActorConfig = field(default_factory=PPOActorConfig) ref: PPOActorConfig | None = field(default=None) critic: PPOCriticConfig | None = field(default=None) teacher: TeacherConfig | None = field( default=None, metadata={ "help": ( "Optional teacher model configuration used for on-policy " "distillation during PPO training. If provided, the actor " "may be trained to match the teacher in addition to the " "standard PPO objective." ) }, ) mopd: MOPDConfig | None = field( default=None, metadata={"help": "Optional multi-teacher on-policy distillation config."}, ) dynamic_bs: bool = field( default=False, metadata={ "help": "Enable dynamic batch sizing in prepare_batch. When True, batch collection " "stops when (accepted + rejected) >= batch_size, returning only accepted results. " "This results in variable-sized batches of valid data." }, ) def __post_init__(self): """Validate the eval generation config.""" if self.teacher is not None and self.mopd is not None: raise ValueError("teacher and mopd cannot be configured at the same time") if self.mopd is not None: self._validate_mopd_config() if self.eval_gconfig is None: self.eval_gconfig = self.gconfig.new() if self.rollout.deterministic_sampling: for config_name, generation_config in ( ("gconfig", self.gconfig), ("eval_gconfig", self.eval_gconfig), ): if ( generation_config.n_samples > 1 and generation_config.seed is not None ): raise ValueError( "deterministic_sampling with grouped rollouts cannot use " f"a shared {config_name}.seed, because every sample would " "receive the same sampling seed. Set the seed to null to " "derive stable per-sample seeds, or set n_samples=1." ) if self.gconfig.reward_normalization and self.actor.reward_norm is not None: raise ValueError( "gconfig.reward_normalization (rollout-time, per-prompt) and " "actor.reward_norm (training-time, on collected batch) both apply " "reward normalization. Enable only one." ) # Propagate the LoRA adapter name to the rollout engine so the OpenAI-proxy # generation path requests the same adapter the trainer loads. The request # side (ArealOpenAI) cannot read gconfig.lora_name, so it must come from # the engine config. Single source of truth: gconfig.lora_name. if self.rollout.use_lora and not self.rollout.lora_name: self.rollout.lora_name = self.gconfig.lora_name prm = self.rollout.agent.prm # TODO(agent): Define an explicit scorer contract for converting dense # per-token rewards into whole-turn rewards before supporting folded PRM. # A dense vector may represent either independent token signals or a # normalized turn total, so the actor cannot infer the correct reduction. if prm.enabled and prm.scorers and not self.actor.token_rewards_as_adv: raise ValueError( "rollout.agent.prm scorers currently require " "actor.token_rewards_as_adv=True; folded process rewards are " "not supported yet" ) # Actor overrides own process semantics. Keep legacy validation only # when the default pipeline will actually consume the legacy selection. advantage = self.actor.advantage process = advantage.process_adv_shaping uses_legacy_process = advantage.compute.implementation in { "default", "areal.trainer.ppo.advantage.compute_advantages", } and (process is None or process.implementation == "auto") if ( prm.enabled and uses_legacy_process and prm.advantage_shaping.mode in {"gvpo", "process_weighted"} and not self.actor.token_rewards_as_adv ): raise ValueError( f"{prm.advantage_shaping.mode!r} advantage shaping requires " "actor.token_rewards_as_adv=True" ) if ( prm.enabled and uses_legacy_process and prm.advantage_shaping.mode == "process_weighted" and self.actor.mask_no_eos_with_zero ): raise ValueError( "'process_weighted' advantage shaping is incompatible with " "actor.mask_no_eos_with_zero=True" ) if prm.enabled and prm.scorers: if self.rollout._version not in {"v1", "v2"}: raise ValueError( "rollout.agent.prm requires rollout._version='v1' or 'v2'" ) if self.rollout._version == "v2" and self.rollout.api_url is not None: raise ValueError( "PRM scorers do not support v2 external-model mode " "(rollout.api_url); scoring requires token-backed interactions" ) if self.rollout.agent.export_style != "concat": raise ValueError( "rollout.agent.prm currently requires export_style='concat'" ) if self.rollout.agent.chat_template_type != "concat": raise ValueError( "rollout.agent.prm currently requires chat_template_type='concat'" ) super().__post_init__() def _validate_mopd_config(self): """Validate MOPD engine topology before any workers are created.""" from areal.api.alloc_mode import ModelAllocation, ParallelStrategy assert self.mopd is not None self._validate_mopd_dataset_sources("train_dataset", self.train_dataset) if self.valid_dataset is not None: self._validate_mopd_dataset_sources("valid_dataset", self.valid_dataset) if self.mopd.loss.distillation_coefficient == 0: # A pure-RL MOPD plan only scales the actor objective. It must not # require teacher workers, checkpoint compatibility, or colocated # actor/rollout infrastructure to initialize successfully. return teacher_engine = self.mopd.teacher_engine if not self.actor.backend.startswith("megatron:"): raise ValueError("mopd requires a Megatron actor backend") if not teacher_engine.backend.startswith("megatron:"): raise ValueError("mopd.teacher_engine backend must be Megatron") if teacher_engine._version != "v1": raise ValueError("mopd.teacher_engine currently requires _version='v1'") if not self.rollout.backend.startswith("sglang:"): raise ValueError("mopd requires an SGLang rollout backend") if self.actor.weight_update_mode != "awex": raise ValueError("mopd requires actor.weight_update_mode='awex'") if teacher_engine.optimizer is not None: raise ValueError("mopd.teacher_engine.optimizer must be null") if not teacher_engine.disable_dropout: raise ValueError("mopd.teacher_engine.disable_dropout must be true") teacher_schedule = teacher_engine.scheduling_strategy if ( teacher_schedule.type != SchedulingStrategyType.colocation.value or teacher_schedule.target != "actor" or not teacher_schedule.fork ): raise ValueError( "the current MOPD v1 runtime supports teacher colocation " "target='actor' with fork=true" ) rollout_schedule = self.rollout.scheduling_strategy if ( rollout_schedule.type != SchedulingStrategyType.colocation.value or rollout_schedule.target != "actor" or not rollout_schedule.fork ): raise ValueError( "the current MOPD v1 runtime supports rollout colocation " "target='actor' with fork=true" ) actor_worker_ports = self.actor.scheduling_spec[0].port_count if actor_worker_ports < 2: raise ValueError( "the current MOPD v1 runtime requires actor.scheduling_spec[0]." f"port_count >= 2, got {actor_worker_ports}" ) actor_alloc = ModelAllocation.from_str(self.actor.backend, name="actor") teacher_alloc = ModelAllocation.from_str( teacher_engine.backend, name="mopd_teacher" ) if not ParallelStrategy.parallelism_eq( actor_alloc.parallel, teacher_alloc.parallel ): raise ValueError( "mopd teacher and actor must use the same parallel strategy" ) if self.mopd.manager.type == "local_memory": if self.scheduler.type != "local": raise ValueError( "mopd local_memory provider requires scheduler.type='local' " "so controller and teacher workers share the same host" ) if actor_alloc.parallel.world_size > self.cluster.n_gpus_per_node: raise ValueError( "mopd local_memory provider only supports a single node; use " "disk for multi-node runs" ) def _validate_mopd_dataset_sources( self, name: str, dataset_config: TrainDatasetConfig | ValidDatasetConfig, ) -> None: """Require one configured teacher group for every MOPD dataset source.""" assert self.mopd is not None if not dataset_config.sources: raise ValueError(f"{name}.sources must not be empty when mopd is enabled") if dataset_config.path is not None or dataset_config.type is not None: raise ValueError( f"{name}.path/type cannot be used with {name}.sources in MOPD" ) for index, source in enumerate(dataset_config.sources): teacher_group = source.teacher_group if not isinstance(teacher_group, str) or not teacher_group.strip(): raise ValueError( f"{name}.sources[{index}].teacher_group must be configured " "when mopd is enabled" ) if teacher_group not in self.mopd.teacher_groups: raise ValueError( f"{name}.sources[{index}].teacher_group references unknown " f"MOPD teacher group {teacher_group!r}" ) @dataclass class GRPOConfig(PPOConfig): """A dummy place holder of GRPO config for backward compatibility.""" pass def parse_cli_args(argv: list[str]): parser = argparse.ArgumentParser() parser.add_argument( "--config", help="Path to the main configuration file", required=True ) # The first argument might be the path to a training script, # which should be ignored by the argument parser. if argv and argv[0].endswith(".py"): argv = argv[1:] args, overrides = parser.parse_known_args(argv) # Initialize hydra config config_file = Path(args.config).absolute() assert config_file.exists(), f"Config file {config_file} does not exist." # hydra only recognize relative paths relpath = Path(os.path.relpath(str(config_file), Path(__file__).parent.absolute())) if GlobalHydra.instance().is_initialized(): GlobalHydra.instance().clear() hydra_init(config_path=str(relpath.parent), job_name="app", version_base=None) cfg = hydra_compose( config_name=str(relpath.name).split(".yaml")[0], overrides=overrides, ) return cfg, config_file _LEGACY_REJECTION_SAMPLING_KEYS = { "behave_imp_weight_cap", "behave_imp_weight_mode", } _LEGACY_MIGRATION_MESSAGE = ( "Config keys 'behave_imp_weight_cap' and 'behave_imp_weight_mode' have been " "removed. Use 'rejection_sampling' sub-config instead.\n" "Migration mapping:\n" " behave_imp_weight_mode='disabled' -> rejection_sampling: null\n" " behave_imp_weight_mode='token_mask', behave_imp_weight_cap=X\n" " -> rejection_sampling: {level: token, action: mask, metric: ratio, upper: X}\n" " behave_imp_weight_mode='token_truncate', behave_imp_weight_cap=X\n" " -> rejection_sampling: {level: token, action: clamp, metric: ratio, upper: X}\n" ) def _migrate_legacy_rejection_sampling(cfg: DictConfig) -> DictConfig: """Intercept removed behave_imp_weight_* keys and raise actionable error.""" # Walk top-level and known nested actor/teacher configs for legacy keys. sections_to_check = ["actor", "teacher"] for section in sections_to_check: if not OmegaConf.is_missing(cfg, section) and section in cfg: sub = cfg[section] if sub is None or not isinstance(sub, DictConfig): continue found = _LEGACY_REJECTION_SAMPLING_KEYS.intersection(sub.keys()) if found: raise ValueError( f"Found removed config key(s) {found} under '{section}'. " + _LEGACY_MIGRATION_MESSAGE ) return cfg def to_structured_cfg(cfg, config_cls): # Intercept legacy config keys before merge to give actionable error. _migrate_legacy_rejection_sampling(cfg) # Merge with the default configuration. # The yaml and commandline can omit some default values defined in python dataclasses. default_cfg = OmegaConf.structured(config_cls) cfg = OmegaConf.merge(default_cfg, cfg) return cfg def load_expr_config(argv: list[str], config_cls: type[ConfigT]) -> tuple[ConfigT, str]: cfg, config_file = parse_cli_args(argv) cfg = to_structured_cfg(cfg, config_cls=config_cls) cfg = OmegaConf.to_object(cfg) assert isinstance(cfg, config_cls) # Setup environment name_resolve.reconfigure(cfg.cluster.name_resolve) from areal.utils.stats_logger import StatsLogger # Save configuration as yaml if os.getenv("RANK", "0") == "0": save_config(cfg, StatsLogger.get_log_path(cfg.stats_logger)) return cfg, str(config_file) def conf_as_dict(cfg): if isinstance(cfg, (OmegaConf, DictConfig)): return OmegaConf.to_container(cfg, resolve=True) return asdict(cfg) def save_config(cfg, log_dir): os.makedirs(log_dir, exist_ok=True) config_save_path = os.path.join(log_dir, "config.yaml") with open(config_save_path, "w") as f: config_dict: dict = redact_sensitive_config(asdict(cfg)) yaml.dump( config_dict, f, default_flow_style=False, sort_keys=False, )