Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
4e1c05e
[trainer, cfg] feat: Add AlgorithmConfig dataclass for type-safe algo…
openhands-agent Jun 20, 2025
9ed10fa
Complete algorithm config dataclass migration
openhands-agent Jun 21, 2025
646a1e7
Remove temporary test files
openhands-agent Jun 21, 2025
d7aa12b
Revert changes and rename algorithm config to algo config
openhands-agent Jun 21, 2025
109340d
Update compute_advantage type annotations and fix linting
openhands-agent Jun 21, 2025
89e4b34
Update all core_algos.py functions to use AlgoConfig type annotations
openhands-agent Jun 21, 2025
f0f406f
Fix compute_grpo_outcome_advantage function signature to include Algo…
openhands-agent Jun 21, 2025
637a358
Merge main into feat/algorithm-config-dataclass
openhands-agent Jun 22, 2025
9eeab2e
init frozen adaptor
eric-haibin-lin Jun 29, 2025
1b85290
move to profiler folder
eric-haibin-lin Jun 30, 2025
ba93223
backward compat namespace move
eric-haibin-lin Jun 30, 2025
da8d771
fix lint
eric-haibin-lin Jun 30, 2025
0b1cb62
remove omega_conf_to_dataclass type
eric-haibin-lin Jun 30, 2025
2c25c76
Refactor algorithm config classes to use frozen dataclasses and BaseC…
devin-ai-integration[bot] Jun 30, 2025
520b23d
Revert documentation changes and fix omega_conf_to_dataclass call
devin-ai-integration[bot] Jun 30, 2025
80685b4
Fix config.get() call in compute_advantage function
devin-ai-integration[bot] Jun 30, 2025
2df1773
Merge main branch and resolve conflicts
devin-ai-integration[bot] Jun 30, 2025
52c62b3
Fix lint issues after merge
devin-ai-integration[bot] Jun 30, 2025
562a111
Fix type annotation and docstring coverage issues
devin-ai-integration[bot] Jun 30, 2025
81d7edf
Add test_base_config_on_cpu.py to allow list and update omega_conf_to…
devin-ai-integration[bot] Jun 30, 2025
a6df414
fix test
eric-haibin-lin Jun 30, 2025
6e743a5
fix litn
eric-haibin-lin Jun 30, 2025
ffa8d77
convert to dataclass upfront
eric-haibin-lin Jun 30, 2025
12c22b8
Merge branch 'feat/algorithm-config-dataclass' of code.byted.org:data…
eric-haibin-lin Jun 30, 2025
e2fac2c
update import stmt
eric-haibin-lin Jun 30, 2025
969a734
merge with main
eric-haibin-lin Jun 30, 2025
69a1a17
fix lint
eric-haibin-lin Jun 30, 2025
f1f4047
add _target_ to megatron config
eric-haibin-lin Jun 30, 2025
7bcd0fe
fix ranks init
eric-haibin-lin Jun 30, 2025
0eacb9f
adjust line-len
eric-haibin-lin Jul 1, 2025
ac19891
adjust len=120
eric-haibin-lin Jul 1, 2025
c907607
merge with main
eric-haibin-lin Jul 1, 2025
e63bbb0
fix lint
eric-haibin-lin Jul 1, 2025
8bce67d
merge with master
eric-haibin-lin Jul 3, 2025
fb93f20
merge with main
eric-haibin-lin Jul 4, 2025
c195f00
Merge remote-tracking branch 'oss/main' into feat/algorithm-config-da…
eric-haibin-lin Jul 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
backward compat namespace move
  • Loading branch information
eric-haibin-lin committed Jun 30, 2025
commit ba93223028372808f00c06c3e86449df7f543cbb
42 changes: 42 additions & 0 deletions tests/test_base_config_on_cpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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.

import pytest

from verl.base_config import BaseConfig


@pytest.fixture
def base_config_mock():
"""Fixture to create a mock BaseConfig instance with test attributes."""
mock_config = BaseConfig()
mock_config.test_attr = "test_value"
return mock_config


def test_getitem_success(base_config_mock):
"""Test __getitem__ with existing attribute (happy path)."""
assert base_config_mock["test_attr"] == "test_value"


def test_getitem_nonexistent_attribute(base_config_mock):
"""Test __getitem__ with non-existent attribute (exception path 1)."""
with pytest.raises(AttributeError):
_ = base_config_mock["nonexistent_attr"]


def test_getitem_invalid_key_type(base_config_mock):
"""Test __getitem__ with invalid key type (exception path 2)."""
with pytest.raises(TypeError):
_ = base_config_mock[123] # type: ignore
12 changes: 8 additions & 4 deletions tests/utils/test_nvtx_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,14 @@ def test_config_init(self):
self.assertEqual(profiler_config.ranks, config.ranks)
assert isinstance(profiler_config, ProfilerConfig)
with self.assertRaises(AttributeError):
profiler_config.non_existing_key
assert config.get('non_existing_key') == profiler_config.get('non_existing_key')
assert config.get('non_existing_key', 1) == profiler_config.get('non_existing_key', 1)
assert config['discrete'] == profiler_config['discrete']
_ = profiler_config.non_existing_key
assert config.get("non_existing_key") == profiler_config.get("non_existing_key")
assert config.get("non_existing_key", 1) == profiler_config.get("non_existing_key", 1)
assert config["discrete"] == profiler_config["discrete"]
from dataclasses import FrozenInstanceError

with self.assertRaises(FrozenInstanceError):
profiler_config.discrete = False


if __name__ == "__main__":
Expand Down
47 changes: 43 additions & 4 deletions verl/base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,62 @@
# limitations under the License.

import collections
from dataclasses import asdict
from dataclasses import fields # Import the fields function to inspect dataclass fields
from typing import Any


# BaseConfig class inherits from collections.abc.Mapping, which means it can act like a dictionary
class BaseConfig(collections.abc.Mapping):
"""The BaseConfig provides omegaconf DictConfig-like interface for a dataclass config.

def get(self, key, default=None):
The BaseConfig class implements the Mapping Abstract Base Class.
This allows instances of this class to be used like dictionaries.
"""

def get(self, key: str, default: Any = None):
"""Get the value associated with the given key. If the key does not exist, return the default value.

Args:
key (str): The attribute name to retrieve.
default (Any, optional): The value to return if the attribute does not exist. Defaults to None.

Returns:
Any: The value of the attribute or the default value.
"""
try:
return getattr(self, key)
except AttributeError as e:
except AttributeError:
return default

def __getitem__(self, key):
def __getitem__(self, key: str):
"""Implement the [] operator for the class. Allows accessing attributes like dictionary items.

Args:
key (str): The attribute name to retrieve.

Returns:
Any: The value of the attribute.

Raises:
AttributeError: If the attribute does not exist.
TypeError: If the key type is not string
"""
return getattr(self, key)

def __iter__(self):
"""Implement the iterator protocol. Allows iterating over the attribute names of the instance.

Yields:
str: The name of each field in the dataclass.
"""
for f in fields(self):
yield f.name

def __len__(self):
"""
Return the number of fields in the dataclass.

Returns:
int: The number of fields in the dataclass.
"""
return len(fields(self))
15 changes: 3 additions & 12 deletions verl/utils/debug/__init__.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding a warning when importing this module?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now we can mention it as deprecated in #2270. we cannot print warning each time debug is imported because there are still other functions defined in the .debug submodule

Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from ..import_utils import is_nvtx_available
from .performance import GPUMemoryLogger, log_gpu_memory_usage, log_print, simple_timer
from .profile import DistProfilerExtension, ProfilerConfig

if is_nvtx_available():
from .nvtx_profile import NsightSystemsProfiler as DistProfiler
from .nvtx_profile import mark_annotate, mark_end_range, mark_start_range, marked_timer
else:
from .performance import marked_timer
from .profile import DistProfiler, mark_annotate, mark_end_range, mark_start_range

__all__ = ["GPUMemoryLogger", "log_gpu_memory_usage", "log_print", "mark_start_range", "mark_end_range", "mark_annotate", "DistProfiler", "DistProfilerExtension", "ProfilerConfig", "simple_timer", "marked_timer"]
# APIs kept for backward compatibility purpose
# For new features please develop in verl/utils/profiler/
from ..profiler import * # noqa
26 changes: 26 additions & 0 deletions verl/utils/profiler/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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.

from ..import_utils import is_nvtx_available
from .performance import GPUMemoryLogger, log_gpu_memory_usage, simple_timer
from .profile import DistProfilerExtension, ProfilerConfig

if is_nvtx_available():
from .nvtx_profile import NsightSystemsProfiler as DistProfiler
from .nvtx_profile import mark_annotate, mark_end_range, mark_start_range, marked_timer
else:
from .performance import marked_timer
from .profile import DistProfiler, mark_annotate, mark_end_range, mark_start_range

__all__ = ["GPUMemoryLogger", "log_gpu_memory_usage", "mark_start_range", "mark_end_range", "mark_annotate", "DistProfiler", "DistProfilerExtension", "ProfilerConfig", "simple_timer", "marked_timer"]
49 changes: 49 additions & 0 deletions verl/utils/profiler/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.

from dataclasses import dataclass, field

from verl.base_config import BaseConfig


@dataclass(frozen=True)
class ProfilerConfig(BaseConfig):
"""Worker profiler config. Currently only support Nsight system profiler."""

# True for each task has its own database, False for all tasks in one training step share one database.
discrete: bool = False

# Whether to profile all ranks.
all_ranks: bool = False

# The ranks that will be profiled. [] or [0,1,...]
ranks: list[int] = field(default_factory=list)

def union(self, other: "ProfilerConfig") -> "ProfilerConfig":
return ProfilerConfig(
all_ranks=self.all_ranks or other.all_ranks,
ranks=list(set(self.ranks or []) | set(other.ranks or [])),
discrete=self.discrete or other.discrete,
)

def intersect(self, other: "ProfilerConfig") -> "ProfilerConfig":
return ProfilerConfig(
all_ranks=self.all_ranks and other.all_ranks,
ranks=list(set(self.ranks or []) & set(other.ranks or [])),
discrete=self.discrete and other.discrete,
)

def __post_init__(self) -> None:
"""config validation logics go here"""
assert isinstance(self.ranks, (set, list, tuple))
6 changes: 1 addition & 5 deletions verl/workers/megatron_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import os
import time
import warnings
from typing import Union
from typing import Optional, Union

import torch
import torch.distributed
Expand Down Expand Up @@ -335,8 +335,6 @@ def init_model(self):

importlib.import_module(self.config.model.external_lib)

from omegaconf import OmegaConf

from verl.utils.torch_dtypes import PrecisionType

override_model_config = OmegaConf.to_container(self.config.model.get("override_config", OmegaConf.create()))
Expand Down Expand Up @@ -720,7 +718,6 @@ def megatron_critic_model_provider(pre_process, post_process):
@register(dispatch_mode=Dispatch.ONE_TO_ALL)
def init_model(self):
# create critic
from omegaconf import OmegaConf

from verl.utils.torch_dtypes import PrecisionType

Expand Down Expand Up @@ -926,7 +923,6 @@ def megatron_rm_model_provider(pre_process, post_process):
@register(dispatch_mode=Dispatch.ONE_TO_ALL)
def init_model(self):
# create critic
from omegaconf import OmegaConf

from verl.utils.torch_dtypes import PrecisionType

Expand Down