|
| 1 | +# Copyright The PyTorch Lightning team. |
| 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 os |
| 16 | +import socket |
| 17 | + |
| 18 | +from pytorch_lightning import _logger as log |
| 19 | +from pytorch_lightning.plugins.environments import ClusterEnvironment |
| 20 | + |
| 21 | + |
| 22 | +class LSFEnvironment(ClusterEnvironment): |
| 23 | + """ |
| 24 | + An environment for running on clusters managed by the LSF resource manager. |
| 25 | +
|
| 26 | + It is expected that any execution using this ClusterEnvironment was executed |
| 27 | + using the Job Step Manager i.e. ``jsrun``. |
| 28 | +
|
| 29 | + This plugin expects the following environment variables. |
| 30 | +
|
| 31 | + LSB_JOBID: |
| 32 | + The LSF assigned job ID |
| 33 | +
|
| 34 | + LSB_HOSTS: |
| 35 | + The hosts used in the job. This string is expected to have the format "batch <rank_0_host> ...." |
| 36 | +
|
| 37 | + JSM_NAMESPACE_LOCAL_RANK: |
| 38 | + The node local rank for the task. This environment variable is set by jsrun |
| 39 | +
|
| 40 | + JSM_NAMESPACE_SIZE: |
| 41 | + The world size for the task. This environment variable is set by jsrun |
| 42 | + """ |
| 43 | + |
| 44 | + def __init__(self): |
| 45 | + self._master_address = self._get_master_address() |
| 46 | + self._master_port = self._get_master_port() |
| 47 | + log.debug(f"MASTER_ADDR: {self._master_address}") |
| 48 | + log.debug(f"MASTER_PORT: {self._master_port}") |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def is_using_lsf() -> bool: |
| 52 | + """ Returns ``True`` if the current process was launched using the jsrun command. """ |
| 53 | + required_env_vars = ( |
| 54 | + "LSB_JOBID", |
| 55 | + "LSB_HOSTS", |
| 56 | + "JSM_NAMESPACE_LOCAL_RANK", |
| 57 | + "JSM_NAMESPACE_SIZE", |
| 58 | + ) |
| 59 | + return all(v in os.environ for v in required_env_vars) |
| 60 | + |
| 61 | + def creates_children(self) -> bool: |
| 62 | + return True |
| 63 | + |
| 64 | + def master_address(self): |
| 65 | + """ The master address is read from a list of hosts contained in the environment variable `LSB_HOSTS`. """ |
| 66 | + return self._master_address |
| 67 | + |
| 68 | + def master_port(self): |
| 69 | + """ THe master port gets calculated from the LSF job ID. """ |
| 70 | + return self._master_port |
| 71 | + |
| 72 | + def world_size(self): |
| 73 | + """ The world size is read from the environment variable `JSM_NAMESPACE_SIZE`. """ |
| 74 | + var = "JSM_NAMESPACE_SIZE" |
| 75 | + world_size = os.environ.get(var) |
| 76 | + if world_size is None: |
| 77 | + raise ValueError( |
| 78 | + f"Cannot determine world size from environment variable {var}." |
| 79 | + " Make sure you run your executable with `jsrun`" |
| 80 | + ) |
| 81 | + return int(world_size) |
| 82 | + |
| 83 | + def set_world_size(self, size: int) -> None: |
| 84 | + log.debug("LSFEnvironment.set_world_size was called, but setting world size is not allowed. Ignored.") |
| 85 | + |
| 86 | + def global_rank(self): |
| 87 | + """ The world size is read from the environment variable `JSM_NAMESPACE_RANK`. """ |
| 88 | + var = "JSM_NAMESPACE_RANK" |
| 89 | + global_rank = os.environ.get(var) |
| 90 | + if global_rank is None: |
| 91 | + raise ValueError( |
| 92 | + f"Cannot determine global rank from environment variable {var}." |
| 93 | + " Make sure you run your executable with `jsrun`" |
| 94 | + ) |
| 95 | + return int(global_rank) |
| 96 | + |
| 97 | + def set_global_rank(self, rank: int) -> None: |
| 98 | + log.debug("LSFEnvironment.set_global_rank was called, but setting global rank is not allowed. Ignored.") |
| 99 | + |
| 100 | + def local_rank(self): |
| 101 | + """ The local rank is read from the environment variable `JSM_NAMESPACE_LOCAL_RANK`. """ |
| 102 | + var = "JSM_NAMESPACE_LOCAL_RANK" |
| 103 | + local_rank = os.environ.get(var) |
| 104 | + if local_rank is None: |
| 105 | + raise ValueError( |
| 106 | + f"Cannot determine local rank from environment variable {var}." |
| 107 | + " Make sure you run your executable with `jsrun`" |
| 108 | + ) |
| 109 | + return int(local_rank) |
| 110 | + |
| 111 | + def node_rank(self): |
| 112 | + """ |
| 113 | + The node rank is determined by the position of the current hostname in the list of hosts stored in |
| 114 | + the environment variable `LSB_HOSTS`. |
| 115 | + """ |
| 116 | + hosts = self._read_hosts() |
| 117 | + count = dict() |
| 118 | + for host in hosts: |
| 119 | + if "batch" in host or "login" in host: |
| 120 | + continue |
| 121 | + if host not in count: |
| 122 | + count[host] = len(count) |
| 123 | + return count[socket.gethostname()] |
| 124 | + |
| 125 | + @staticmethod |
| 126 | + def _read_hosts(): |
| 127 | + hosts = os.environ.get("LSB_HOSTS") |
| 128 | + if not hosts: |
| 129 | + raise ValueError("Could not find hosts in environment variable LSB_HOSTS") |
| 130 | + hosts = hosts.split() |
| 131 | + if len(hosts) < 2: |
| 132 | + raise ValueError( |
| 133 | + "Cannot parse hosts from LSB_HOSTS environment variable." |
| 134 | + " Expected format: \"batch <rank_0_host> ...\"" |
| 135 | + ) |
| 136 | + return hosts |
| 137 | + |
| 138 | + def _get_master_address(self): |
| 139 | + hosts = self._read_hosts() |
| 140 | + return hosts[1] |
| 141 | + |
| 142 | + @staticmethod |
| 143 | + def _get_master_port(): |
| 144 | + """ |
| 145 | + A helper function for accessing the master port. |
| 146 | + Uses the LSF job ID so all ranks can compute the master port. |
| 147 | + """ |
| 148 | + # check for user-specified master port |
| 149 | + port = os.environ.get("MASTER_PORT") |
| 150 | + if not port: |
| 151 | + jobid = os.environ.get("LSB_JOBID") |
| 152 | + if not jobid: |
| 153 | + raise ValueError("Could not find job id in environment variable LSB_JOBID") |
| 154 | + port = int(jobid) |
| 155 | + # all ports should be in the 10k+ range |
| 156 | + port = int(port) % 1000 + 10000 |
| 157 | + log.debug(f"calculated LSF master port: {port}") |
| 158 | + else: |
| 159 | + log.debug(f"using externally specified master port: {port}") |
| 160 | + return int(port) |
0 commit comments