config.py 3.89 KB
Newer Older
chenych's avatar
chenych committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Copyright 2024 Bytedance Ltd. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
PPO config
"""

import os
from dataclasses import asdict, dataclass, field, fields, is_dataclass
from typing import Optional, Tuple

chenych's avatar
chenych committed
22
from ..workers.config import WorkerConfig
chenych's avatar
chenych committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38


def recursive_post_init(dataclass_obj):
    if hasattr(dataclass_obj, "post_init"):
        dataclass_obj.post_init()

    for attr in fields(dataclass_obj):
        if is_dataclass(getattr(dataclass_obj, attr.name)):
            recursive_post_init(getattr(dataclass_obj, attr.name))


@dataclass
class DataConfig:
    train_files: str = ""
    val_files: str = ""
    prompt_key: str = "prompt"
chenych's avatar
chenych committed
39
40
    answer_key: str = "answer"
    image_key: str = "images"
chenych's avatar
chenych committed
41
42
43
    max_prompt_length: int = 512
    max_response_length: int = 512
    rollout_batch_size: int = 512
chenych's avatar
chenych committed
44
    val_batch_size: int = -1
chenych's avatar
Update  
chenych committed
45
    format_prompt: Optional[str] = None
chenych's avatar
chenych committed
46
47
48
49
    shuffle: bool = True
    seed: int = 1
    max_pixels: int = 4194304
    min_pixels: int = 262144
chenych's avatar
update  
chenych committed
50
51
52
53
54
55
56
57
    filter_overlong_prompts: bool = True

    def post_init(self):
        if self.format_prompt is not None:
            if os.path.exists(self.format_prompt):
                self.format_prompt = os.path.abspath(self.format_prompt)
            else:
                self.format_prompt = None
chenych's avatar
chenych committed
58
59
60
61
62
63


@dataclass
class AlgorithmConfig:
    gamma: float = 1.0
    lam: float = 1.0
chenych's avatar
chenych committed
64
65
66
    adv_estimator: str = "grpo"
    disable_kl: bool = False
    use_kl_loss: bool = False
chenych's avatar
chenych committed
67
68
    kl_penalty: str = "kl"
    kl_coef: float = 1e-3
chenych's avatar
chenych committed
69
    kl_type: str = "fixed"
chenych's avatar
chenych committed
70
71
72
73
74
75
76
77
78
79
80
81
82
    kl_horizon: float = 0.0
    kl_target: float = 0.0


@dataclass
class TrainerConfig:
    total_episodes: int = 10
    max_steps: Optional[int] = None
    project_name: str = "easy_r1"
    experiment_name: str = "demo"
    logger: Tuple[str] = ("console", "wandb")
    nnodes: int = 1
    n_gpus_per_node: int = 8
chenych's avatar
chenych committed
83
84
    critic_warmup: int = 0
    val_freq: int = -1
chenych's avatar
chenych committed
85
86
    val_before_train: bool = True
    val_only: bool = False
chenych's avatar
chenych committed
87
88
89
    val_generations_to_log: int = 0
    save_freq: int = -1
    save_limit: int = -1
chenych's avatar
chenych committed
90
    save_checkpoint_path: Optional[str] = None
chenych's avatar
chenych committed
91
    load_checkpoint_path: Optional[str] = None
chenych's avatar
chenych committed
92
93
94
95
96

    def post_init(self):
        if self.save_checkpoint_path is None:
            self.save_checkpoint_path = os.path.join("checkpoints", self.project_name, self.experiment_name)

chenych's avatar
update  
chenych committed
97
98
99
100
        self.save_checkpoint_path = os.path.abspath(self.save_checkpoint_path)
        if self.load_checkpoint_path is not None:
            self.load_checkpoint_path = os.path.abspath(self.load_checkpoint_path)

chenych's avatar
chenych committed
101
102
103
104
105
106
107
108
109
110
111

@dataclass
class PPOConfig:
    data: DataConfig = field(default_factory=DataConfig)
    worker: WorkerConfig = field(default_factory=WorkerConfig)
    algorithm: AlgorithmConfig = field(default_factory=AlgorithmConfig)
    trainer: TrainerConfig = field(default_factory=TrainerConfig)

    def post_init(self):
        self.worker.rollout.prompt_length = self.data.max_prompt_length
        self.worker.rollout.response_length = self.data.max_response_length
chenych's avatar
update  
chenych committed
112
        self.worker.rollout.trust_remote_code = self.worker.actor.model.trust_remote_code
chenych's avatar
chenych committed
113
114
115
116
        self.worker.actor.disable_kl = self.algorithm.disable_kl
        self.worker.actor.use_kl_loss = self.algorithm.use_kl_loss
        self.worker.actor.kl_penalty = self.algorithm.kl_penalty
        self.worker.actor.kl_coef = self.algorithm.kl_coef
chenych's avatar
chenych committed
117
118
119
120
121
122

    def deep_post_init(self):
        recursive_post_init(self)

    def to_dict(self):
        return asdict(self)