2020-10-13 11:18:07 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# 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.
|
2020-10-26 10:47:09 +00:00
|
|
|
|
2020-10-04 11:31:19 +00:00
|
|
|
import os
|
2021-01-15 22:44:27 +00:00
|
|
|
|
2020-10-04 11:31:19 +00:00
|
|
|
from pytorch_lightning import _logger as log
|
|
|
|
from pytorch_lightning.cluster_environments.cluster_environment import ClusterEnvironment
|
2021-01-15 22:44:27 +00:00
|
|
|
from pytorch_lightning.utilities import rank_zero_warn
|
2020-10-04 11:31:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TorchElasticEnvironment(ClusterEnvironment):
|
|
|
|
|
2020-10-04 12:48:46 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2020-10-04 11:31:19 +00:00
|
|
|
|
|
|
|
def master_address(self):
|
|
|
|
if "MASTER_ADDR" not in os.environ:
|
|
|
|
rank_zero_warn(
|
|
|
|
"MASTER_ADDR environment variable is not defined. Set as localhost"
|
|
|
|
)
|
|
|
|
os.environ["MASTER_ADDR"] = "127.0.0.1"
|
|
|
|
log.debug(f"MASTER_ADDR: {os.environ['MASTER_ADDR']}")
|
|
|
|
master_address = os.environ.get('MASTER_ADDR')
|
|
|
|
return master_address
|
|
|
|
|
|
|
|
def master_port(self):
|
|
|
|
if "MASTER_PORT" not in os.environ:
|
|
|
|
rank_zero_warn(
|
|
|
|
"MASTER_PORT environment variable is not defined. Set as 12910"
|
|
|
|
)
|
|
|
|
os.environ["MASTER_PORT"] = "12910"
|
|
|
|
log.debug(f"MASTER_PORT: {os.environ['MASTER_PORT']}")
|
|
|
|
|
|
|
|
port = os.environ.get('MASTER_PORT')
|
|
|
|
return port
|
|
|
|
|
|
|
|
def world_size(self):
|
2020-10-26 10:47:09 +00:00
|
|
|
return os.environ.get('WORLD_SIZE')
|
2020-11-08 19:28:55 +00:00
|
|
|
|
|
|
|
def local_rank(self):
|
|
|
|
return int(os.environ['LOCAL_RANK'])
|