Add files using upload-large-folder tool
Browse files- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--115--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--164--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--189--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--192--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--195--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--292--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--322--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--399--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--405--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--462--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--504--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--549--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--567--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--kvasir_seg--cju17r8il13910799dr2wme2e--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--kvasir_seg--cju45ty6zn9oz0850qy4qnck1--2d_none--0000_001.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--kvasir_seg--cju886ryxnsl50801r93jai7q--2d_none--0000_001.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--kvasir_seg--cju8b542nr81x0871uxnkm9ih--2d_none--0000_000.png +3 -0
- benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--kvasir_seg--cju8bj2ssrmlm0871gc2ug2rs--2d_none--0000_000.png +3 -0
- 之前的代码版本/注意力用于sam的prompt的版本/MedEvalKit/LLaVA-NeXT/llava-critic-r1/EasyR1/verl/utils/fsdp_utils.py +140 -0
- 之前的代码版本/注意力用于sam的prompt的版本/MedEvalKit/LLaVA-NeXT/llava-critic-r1/EasyR1/verl/utils/logger/logger.py +165 -0
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--115--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--164--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--189--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--192--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--195--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--292--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--322--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--399--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--405--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--462--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--504--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--549--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--cvc_clinicdb--567--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--kvasir_seg--cju17r8il13910799dr2wme2e--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--kvasir_seg--cju45ty6zn9oz0850qy4qnck1--2d_none--0000_001.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--kvasir_seg--cju886ryxnsl50801r93jai7q--2d_none--0000_001.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--kvasir_seg--cju8b542nr81x0871uxnkm9ih--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
benchmarks/VGMED/image_folder/SAMed2D_v1/endoscopy/masks/endoscopy_00--kvasir_seg--cju8bj2ssrmlm0871gc2ug2rs--2d_none--0000_000.png
ADDED
|
Git LFS Details
|
之前的代码版本/注意力用于sam的prompt的版本/MedEvalKit/LLaVA-NeXT/llava-critic-r1/EasyR1/verl/utils/fsdp_utils.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2024 Bytedance Ltd. and/or its affiliates
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import gc
|
| 16 |
+
from collections import defaultdict
|
| 17 |
+
from functools import partial
|
| 18 |
+
from typing import Callable, Union
|
| 19 |
+
|
| 20 |
+
import torch
|
| 21 |
+
from torch import nn
|
| 22 |
+
from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
|
| 23 |
+
from torch.distributed.fsdp._runtime_utils import _lazy_init
|
| 24 |
+
from torch.distributed.fsdp.wrap import transformer_auto_wrap_policy
|
| 25 |
+
from torch.optim import Optimizer
|
| 26 |
+
from transformers import PreTrainedModel
|
| 27 |
+
from transformers.trainer_pt_utils import get_module_class_from_name
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def get_init_fn(model: nn.Module, device: Union[str, torch.device]) -> Callable[[nn.Module], None]:
|
| 31 |
+
param_occurrence = defaultdict(int)
|
| 32 |
+
for _, param in model.named_parameters(remove_duplicate=False):
|
| 33 |
+
param_occurrence[param] += 1
|
| 34 |
+
|
| 35 |
+
duplicated_params = {param for param in param_occurrence.keys() if param_occurrence[param] > 1}
|
| 36 |
+
materialized_params = {}
|
| 37 |
+
|
| 38 |
+
def init_fn(module: nn.Module):
|
| 39 |
+
for name, param in module.named_parameters(recurse=False):
|
| 40 |
+
if param in duplicated_params:
|
| 41 |
+
module._parameters[name] = materialized_params.setdefault(
|
| 42 |
+
param, nn.Parameter(torch.empty_like(param.data, device=device), requires_grad=param.requires_grad)
|
| 43 |
+
)
|
| 44 |
+
else:
|
| 45 |
+
module._parameters[name] = nn.Parameter(
|
| 46 |
+
torch.empty_like(param.data, device=device), requires_grad=param.requires_grad
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
return init_fn
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def get_fsdp_wrap_policy(model: PreTrainedModel):
|
| 53 |
+
"""Get FSDP wrap policy for the model.
|
| 54 |
+
|
| 55 |
+
Args:
|
| 56 |
+
module: The module to get wrap policy for
|
| 57 |
+
"""
|
| 58 |
+
transformer_cls_to_wrap = set()
|
| 59 |
+
for module in model._no_split_modules:
|
| 60 |
+
transformer_cls = get_module_class_from_name(model, module)
|
| 61 |
+
if transformer_cls is None:
|
| 62 |
+
raise Exception(f"Cannot find {module} in pretrained model.")
|
| 63 |
+
else:
|
| 64 |
+
transformer_cls_to_wrap.add(transformer_cls)
|
| 65 |
+
|
| 66 |
+
return partial(transformer_auto_wrap_policy, transformer_layer_cls=transformer_cls_to_wrap)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@torch.no_grad()
|
| 70 |
+
def offload_fsdp_model(model: FSDP, empty_cache: bool = True):
|
| 71 |
+
# lazy init FSDP model
|
| 72 |
+
_lazy_init(model, model)
|
| 73 |
+
assert model._is_root, "Only support root model offloading to CPU"
|
| 74 |
+
for handle in model._all_handles:
|
| 75 |
+
if handle._offload_params:
|
| 76 |
+
continue
|
| 77 |
+
|
| 78 |
+
flat_param = handle.flat_param
|
| 79 |
+
assert (
|
| 80 |
+
flat_param.data.data_ptr() == flat_param._local_shard.data_ptr()
|
| 81 |
+
and id(flat_param.data) != id(flat_param._local_shard)
|
| 82 |
+
and flat_param.data.size() == flat_param._local_shard.size()
|
| 83 |
+
)
|
| 84 |
+
handle.flat_param_to("cpu", non_blocking=True)
|
| 85 |
+
# the following still keeps id(._local_shard) != id(.data)
|
| 86 |
+
flat_param._local_shard = flat_param.data
|
| 87 |
+
assert id(flat_param._local_shard) != id(flat_param.data)
|
| 88 |
+
|
| 89 |
+
if empty_cache:
|
| 90 |
+
torch.cuda.empty_cache()
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
@torch.no_grad()
|
| 94 |
+
def load_fsdp_model(model: FSDP, empty_cache: bool = True):
|
| 95 |
+
# lazy init FSDP model
|
| 96 |
+
_lazy_init(model, model)
|
| 97 |
+
assert model._is_root, "Only support root model loading to GPU"
|
| 98 |
+
for handle in model._all_handles:
|
| 99 |
+
if handle._offload_params:
|
| 100 |
+
continue
|
| 101 |
+
|
| 102 |
+
flat_param = handle.flat_param
|
| 103 |
+
handle.flat_param_to("cuda", non_blocking=True)
|
| 104 |
+
# the following still keeps id(._local_shard) != id(.data)
|
| 105 |
+
flat_param._local_shard = flat_param.data
|
| 106 |
+
|
| 107 |
+
if empty_cache:
|
| 108 |
+
gc.collect()
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
@torch.no_grad()
|
| 112 |
+
def offload_fsdp_optimizer(optimizer: Optimizer, empty_cache: bool = True):
|
| 113 |
+
if not optimizer.state:
|
| 114 |
+
return
|
| 115 |
+
|
| 116 |
+
for param_group in optimizer.param_groups:
|
| 117 |
+
for param in param_group["params"]:
|
| 118 |
+
state = optimizer.state[param]
|
| 119 |
+
for key, value in state.items():
|
| 120 |
+
if isinstance(value, torch.Tensor):
|
| 121 |
+
state[key] = value.to("cpu", non_blocking=True)
|
| 122 |
+
|
| 123 |
+
if empty_cache:
|
| 124 |
+
torch.cuda.empty_cache()
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
@torch.no_grad()
|
| 128 |
+
def load_fsdp_optimizer(optimizer: Optimizer, empty_cache: bool = True):
|
| 129 |
+
if not optimizer.state:
|
| 130 |
+
return
|
| 131 |
+
|
| 132 |
+
for param_group in optimizer.param_groups:
|
| 133 |
+
for param in param_group["params"]:
|
| 134 |
+
state = optimizer.state[param]
|
| 135 |
+
for key, value in state.items():
|
| 136 |
+
if isinstance(value, torch.Tensor):
|
| 137 |
+
state[key] = value.to("cuda", non_blocking=True)
|
| 138 |
+
|
| 139 |
+
if empty_cache:
|
| 140 |
+
gc.collect()
|
之前的代码版本/注意力用于sam的prompt的版本/MedEvalKit/LLaVA-NeXT/llava-critic-r1/EasyR1/verl/utils/logger/logger.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2024 Bytedance Ltd. and/or its affiliates
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""
|
| 15 |
+
A unified tracking interface that supports logging data to different backend
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
import os
|
| 19 |
+
from abc import ABC, abstractmethod
|
| 20 |
+
from typing import Any, Dict, List, Optional, Tuple, Union
|
| 21 |
+
|
| 22 |
+
import torch
|
| 23 |
+
|
| 24 |
+
from ..py_functional import convert_dict_to_str, flatten_dict, is_package_available, unflatten_dict
|
| 25 |
+
from .gen_logger import AggregateGenerationsLogger
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
if is_package_available("mlflow"):
|
| 29 |
+
import mlflow # type: ignore
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
if is_package_available("tensorboard"):
|
| 33 |
+
from torch.utils.tensorboard import SummaryWriter
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
if is_package_available("wandb"):
|
| 37 |
+
import wandb # type: ignore
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
if is_package_available("swanlab"):
|
| 41 |
+
import swanlab # type: ignore
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class Logger(ABC):
|
| 45 |
+
@abstractmethod
|
| 46 |
+
def __init__(self, config: Dict[str, Any]) -> None: ...
|
| 47 |
+
|
| 48 |
+
@abstractmethod
|
| 49 |
+
def log(self, data: Dict[str, Any], step: int) -> None: ...
|
| 50 |
+
|
| 51 |
+
def finish(self) -> None:
|
| 52 |
+
pass
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class ConsoleLogger(Logger):
|
| 56 |
+
def __init__(self, config: Dict[str, Any]) -> None:
|
| 57 |
+
print("Config\n" + convert_dict_to_str(config))
|
| 58 |
+
|
| 59 |
+
def log(self, data: Dict[str, Any], step: int) -> None:
|
| 60 |
+
print(f"Step {step}\n" + convert_dict_to_str(unflatten_dict(data)))
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class MlflowLogger(Logger):
|
| 64 |
+
def __init__(self, config: Dict[str, Any]) -> None:
|
| 65 |
+
mlflow.start_run(run_name=config["trainer"]["experiment_name"])
|
| 66 |
+
mlflow.log_params(flatten_dict(config))
|
| 67 |
+
|
| 68 |
+
def log(self, data: Dict[str, Any], step: int) -> None:
|
| 69 |
+
mlflow.log_metrics(metrics=data, step=step)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class SwanlabLogger(Logger):
|
| 73 |
+
def __init__(self, config: Dict[str, Any]) -> None:
|
| 74 |
+
swanlab_key = os.getenv("SWANLAB_API_KEY")
|
| 75 |
+
swanlab_dir = os.getenv("SWANLAB_DIR", "swanlab_log")
|
| 76 |
+
swanlab_mode = os.getenv("SWANLAB_MODE", "cloud")
|
| 77 |
+
if swanlab_key:
|
| 78 |
+
swanlab.login(swanlab_key)
|
| 79 |
+
|
| 80 |
+
swanlab.init(
|
| 81 |
+
project=config["trainer"]["project_name"],
|
| 82 |
+
experiment_name=config["trainer"]["experiment_name"],
|
| 83 |
+
config={"UPPERFRAMEWORK": "EasyR1", "FRAMEWORK": "veRL", **config},
|
| 84 |
+
logdir=swanlab_dir,
|
| 85 |
+
mode=swanlab_mode,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
def log(self, data: Dict[str, Any], step: int) -> None:
|
| 89 |
+
swanlab.log(data=data, step=step)
|
| 90 |
+
|
| 91 |
+
def finish(self) -> None:
|
| 92 |
+
swanlab.finish()
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class TensorBoardLogger(Logger):
|
| 96 |
+
def __init__(self, config: Dict[str, Any]) -> None:
|
| 97 |
+
tensorboard_dir = os.getenv("TENSORBOARD_DIR", "tensorboard_log")
|
| 98 |
+
os.makedirs(tensorboard_dir, exist_ok=True)
|
| 99 |
+
print(f"Saving tensorboard log to {tensorboard_dir}.")
|
| 100 |
+
self.writer = SummaryWriter(tensorboard_dir)
|
| 101 |
+
config_dict = {}
|
| 102 |
+
for key, value in flatten_dict(config).items():
|
| 103 |
+
if isinstance(value, (int, float, str, bool, torch.Tensor)):
|
| 104 |
+
config_dict[key] = value
|
| 105 |
+
else:
|
| 106 |
+
config_dict[key] = str(value)
|
| 107 |
+
|
| 108 |
+
self.writer.add_hparams(hparam_dict=config_dict, metric_dict={"placeholder": 0})
|
| 109 |
+
|
| 110 |
+
def log(self, data: Dict[str, Any], step: int) -> None:
|
| 111 |
+
for key, value in data.items():
|
| 112 |
+
self.writer.add_scalar(key, value, step)
|
| 113 |
+
|
| 114 |
+
def finish(self):
|
| 115 |
+
self.writer.close()
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
class WandbLogger(Logger):
|
| 119 |
+
def __init__(self, config: Dict[str, Any]) -> None:
|
| 120 |
+
wandb.init(
|
| 121 |
+
project=config["trainer"]["project_name"],
|
| 122 |
+
name=config["trainer"]["experiment_name"],
|
| 123 |
+
config=config,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
def log(self, data: Dict[str, Any], step: int) -> None:
|
| 127 |
+
wandb.log(data=data, step=step)
|
| 128 |
+
|
| 129 |
+
def finish(self) -> None:
|
| 130 |
+
wandb.finish()
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
LOGGERS = {
|
| 134 |
+
"console": ConsoleLogger,
|
| 135 |
+
"mlflow": MlflowLogger,
|
| 136 |
+
"swanlab": SwanlabLogger,
|
| 137 |
+
"tensorboard": TensorBoardLogger,
|
| 138 |
+
"wandb": WandbLogger,
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
class Tracker:
|
| 143 |
+
def __init__(self, loggers: Union[str, List[str]] = "console", config: Optional[Dict[str, Any]] = None):
|
| 144 |
+
if isinstance(loggers, str):
|
| 145 |
+
loggers = [loggers]
|
| 146 |
+
|
| 147 |
+
self.loggers: List[Logger] = []
|
| 148 |
+
for logger in loggers:
|
| 149 |
+
if logger not in LOGGERS:
|
| 150 |
+
raise ValueError(f"{logger} is not supported.")
|
| 151 |
+
|
| 152 |
+
self.loggers.append(LOGGERS[logger](config))
|
| 153 |
+
|
| 154 |
+
self.gen_logger = AggregateGenerationsLogger(loggers)
|
| 155 |
+
|
| 156 |
+
def log(self, data: Dict[str, Any], step: int) -> None:
|
| 157 |
+
for logger in self.loggers:
|
| 158 |
+
logger.log(data=data, step=step)
|
| 159 |
+
|
| 160 |
+
def log_generation(self, samples: List[Tuple[str, str, str, float]], step: int) -> None:
|
| 161 |
+
self.gen_logger.log(samples, step)
|
| 162 |
+
|
| 163 |
+
def __del__(self):
|
| 164 |
+
for logger in self.loggers:
|
| 165 |
+
logger.finish()
|