2023-02-14 05:02:16 -07:00
|
|
|
import os
|
|
|
|
import torch
|
|
|
|
|
|
|
|
from datetime import timedelta
|
2023-06-26 04:32:54 -06:00
|
|
|
from loguru import logger
|
2024-06-25 05:20:57 -06:00
|
|
|
from text_generation_server.utils.import_utils import SYSTEM
|
2023-02-14 05:02:16 -07:00
|
|
|
|
2023-07-24 03:43:58 -06:00
|
|
|
# Tensor Parallelism settings
|
|
|
|
RANK = int(os.getenv("RANK", "0"))
|
|
|
|
WORLD_SIZE = int(os.getenv("WORLD_SIZE", "1"))
|
|
|
|
|
|
|
|
# CUDA memory fraction
|
|
|
|
MEMORY_FRACTION = float(os.getenv("CUDA_MEMORY_FRACTION", "1.0"))
|
|
|
|
|
2023-02-14 05:02:16 -07:00
|
|
|
|
2023-06-08 06:51:52 -06:00
|
|
|
class FakeBarrier:
|
|
|
|
def wait(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class FakeGroup:
|
|
|
|
def __init__(self, rank, size):
|
|
|
|
self._rank = rank
|
|
|
|
self._size = size
|
|
|
|
|
|
|
|
def allreduce(self, *args, **kwargs):
|
|
|
|
return FakeBarrier()
|
|
|
|
|
|
|
|
def allgather(self, inputs, local_tensor, **kwargs):
|
|
|
|
assert (
|
|
|
|
len(inputs[0]) == len(local_tensor) == 1
|
|
|
|
), f"{len(inputs[0])} != {len(local_tensor)} != 1, and the FakeGroup is supposed to join on simple tensors"
|
|
|
|
for input_ in inputs:
|
|
|
|
input_[0].data = local_tensor[0].data
|
|
|
|
return FakeBarrier()
|
|
|
|
|
|
|
|
def barrier(self, *args, **kwargs):
|
|
|
|
return FakeBarrier()
|
|
|
|
|
|
|
|
def size(self):
|
|
|
|
return self._size
|
|
|
|
|
|
|
|
def rank(self):
|
|
|
|
return self._rank
|
|
|
|
|
|
|
|
|
2023-02-14 05:02:16 -07:00
|
|
|
def initialize_torch_distributed():
|
|
|
|
if torch.cuda.is_available():
|
|
|
|
from torch.distributed import ProcessGroupNCCL
|
|
|
|
|
|
|
|
# Set the device id.
|
2023-07-24 03:43:58 -06:00
|
|
|
assert WORLD_SIZE <= torch.cuda.device_count(), "Each process is one gpu"
|
|
|
|
device = RANK % torch.cuda.device_count()
|
2023-02-14 05:02:16 -07:00
|
|
|
torch.cuda.set_device(device)
|
2023-07-24 03:43:58 -06:00
|
|
|
torch.cuda.set_per_process_memory_fraction(MEMORY_FRACTION, device)
|
2023-02-14 05:02:16 -07:00
|
|
|
backend = "nccl"
|
|
|
|
options = ProcessGroupNCCL.Options()
|
|
|
|
options.is_high_priority_stream = True
|
|
|
|
options._timeout = timedelta(seconds=60)
|
|
|
|
else:
|
2024-06-25 04:21:29 -06:00
|
|
|
backend = "gloo"
|
2023-02-14 05:02:16 -07:00
|
|
|
options = None
|
|
|
|
|
2023-07-24 03:43:58 -06:00
|
|
|
if WORLD_SIZE == 1:
|
|
|
|
return FakeGroup(RANK, WORLD_SIZE), RANK, WORLD_SIZE
|
2023-06-08 06:51:52 -06:00
|
|
|
else:
|
|
|
|
if os.getenv("DEBUG", None) == "1":
|
2023-07-24 03:43:58 -06:00
|
|
|
return FakeGroup(RANK, WORLD_SIZE), RANK, WORLD_SIZE
|
2023-06-26 04:32:54 -06:00
|
|
|
|
|
|
|
if not torch.distributed.is_initialized():
|
|
|
|
# Call the init process.
|
2024-06-25 05:20:57 -06:00
|
|
|
if SYSTEM == "ipex":
|
2024-06-25 04:21:29 -06:00
|
|
|
import intel_extension_for_pytorch as ipex
|
|
|
|
|
|
|
|
ipex.distributed.init_process_group(
|
|
|
|
backend="ccl",
|
|
|
|
world_size=WORLD_SIZE,
|
|
|
|
rank=RANK,
|
|
|
|
timeout=timedelta(seconds=60),
|
|
|
|
pg_options=options,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
torch.distributed.init_process_group(
|
|
|
|
backend=backend,
|
|
|
|
world_size=WORLD_SIZE,
|
|
|
|
rank=RANK,
|
|
|
|
timeout=timedelta(seconds=60),
|
|
|
|
pg_options=options,
|
|
|
|
)
|
2023-06-26 04:32:54 -06:00
|
|
|
else:
|
|
|
|
logger.warning("torch.distributed is already initialized.")
|
2023-02-14 05:02:16 -07:00
|
|
|
|
2023-07-24 03:43:58 -06:00
|
|
|
return torch.distributed.group.WORLD, RANK, WORLD_SIZE
|