A (distributed) worker.
| 79 | |
| 80 | # we assume that in each WorkerGroup, there is a Master Worker |
| 81 | class Worker(WorkerHelper): |
| 82 | """A (distributed) worker.""" |
| 83 | |
| 84 | def __new__(cls, *args, **kwargs): |
| 85 | instance = super().__new__(cls) |
| 86 | |
| 87 | # note that here we use int to distinguish |
| 88 | disable_worker_init = int(os.environ.get('DISABLE_WORKER_INIT', 0)) |
| 89 | if disable_worker_init: |
| 90 | return instance |
| 91 | |
| 92 | rank = os.environ.get("RANK", None) |
| 93 | worker_group_prefix = os.environ.get("WG_PREFIX", None) |
| 94 | |
| 95 | # when decorator @ray.remote applies, __new__ will be called while we don't want to apply _configure_before_init |
| 96 | if None not in [rank, worker_group_prefix] and 'ActorClass(' not in cls.__name__: |
| 97 | instance._configure_before_init(f"{worker_group_prefix}_register_center", int(rank)) |
| 98 | |
| 99 | return instance |
| 100 | |
| 101 | def _configure_before_init(self, register_center_name: str, rank: int): |
| 102 | assert isinstance(rank, int), f"rank must be int, instead of {type(rank)}" |
| 103 | |
| 104 | if rank == 0: |
| 105 | master_addr, master_port = self.get_availale_master_addr_port() |
| 106 | rank_zero_info = { |
| 107 | "MASTER_ADDR": master_addr, |
| 108 | "MASTER_PORT": master_port, |
| 109 | } |
| 110 | |
| 111 | if os.getenv("WG_BACKEND", None) == "ray": |
| 112 | from verl.single_controller.base.register_center.ray import create_worker_group_register_center |
| 113 | self.register_center = create_worker_group_register_center(name=register_center_name, |
| 114 | info=rank_zero_info) |
| 115 | |
| 116 | os.environ.update(rank_zero_info) |
| 117 | |
| 118 | def __init__(self, cuda_visible_devices=None) -> None: |
| 119 | # construct a meta from envrionment variable. Note that the import must be inside the class because it is executed remotely |
| 120 | import os |
| 121 | |
| 122 | ### |
| 123 | # [SUPPORT AMD: torch] |
| 124 | import torch |
| 125 | ### |
| 126 | |
| 127 | ### |
| 128 | # [SUPPORT AMD: torch] |
| 129 | if "AMD" in torch.cuda.get_device_name(): |
| 130 | os.environ['CUDA_VISIBLE_DEVICES'] = os.environ.get('ROCR_VISIBLE_DEVICES') |
| 131 | os.environ['LOCAL_RANK'] = os.environ.get('RAY_LOCAL_RANK') |
| 132 | ### |
| 133 | |
| 134 | world_size = int(os.environ['WORLD_SIZE']) |
| 135 | rank = int(os.environ['RANK']) |
| 136 | self._rank = rank |
| 137 | self._world_size = world_size |
| 138 |
nothing calls this directly
no outgoing calls
no test coverage detected