2023-05-03 15:32:04 +08:00
|
|
|
from typing import Dict, List, Union, Tuple, Optional
|
2023-03-22 04:45:42 +08:00
|
|
|
|
2023-04-30 15:42:17 +08:00
|
|
|
try:
|
|
|
|
import ray
|
|
|
|
except ImportError:
|
|
|
|
ray = None
|
2023-02-23 09:32:19 +00:00
|
|
|
|
2023-05-09 15:30:12 -07:00
|
|
|
from cacheflow.core.scheduler import Scheduler
|
2023-03-10 09:58:21 -08:00
|
|
|
from cacheflow.sequence import SequenceGroupInputs
|
2023-02-23 09:32:19 +00:00
|
|
|
from cacheflow.worker.worker import Worker
|
|
|
|
|
|
|
|
|
2023-03-22 04:45:42 +08:00
|
|
|
DeviceID = Tuple[int, str, int] # rank, node resource (node IP), device id
|
|
|
|
|
|
|
|
|
2023-02-23 09:32:19 +00:00
|
|
|
class Controller:
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2023-03-22 04:45:42 +08:00
|
|
|
stage_id: int,
|
|
|
|
stage_devices: List[DeviceID],
|
|
|
|
world_size: int,
|
|
|
|
tensor_parallel_size: int,
|
|
|
|
pipeline_parallel_size: int,
|
|
|
|
distributed_init_method: str,
|
2023-02-23 09:32:19 +00:00
|
|
|
model_name: str,
|
|
|
|
block_size: int,
|
|
|
|
num_gpu_blocks: int,
|
|
|
|
num_cpu_blocks: int,
|
2023-03-10 09:58:21 -08:00
|
|
|
dtype: str,
|
|
|
|
seed: int,
|
2023-05-03 15:32:04 +08:00
|
|
|
cache_dir: Optional[str],
|
2023-04-08 23:36:12 -07:00
|
|
|
use_dummy_weights: bool,
|
2023-05-03 15:32:04 +08:00
|
|
|
use_np_cache: bool,
|
2023-04-05 11:16:57 -07:00
|
|
|
max_num_batched_tokens: int,
|
2023-04-30 15:42:17 +08:00
|
|
|
use_ray: bool,
|
2023-02-23 09:32:19 +00:00
|
|
|
) -> None:
|
2023-03-22 04:45:42 +08:00
|
|
|
self.stage_id = stage_id
|
|
|
|
self.stage_devices = stage_devices
|
2023-02-23 09:32:19 +00:00
|
|
|
self.model_name = model_name
|
|
|
|
self.block_size = block_size
|
|
|
|
self.num_gpu_blocks = num_gpu_blocks
|
|
|
|
self.num_cpu_blocks = num_cpu_blocks
|
2023-04-30 15:42:17 +08:00
|
|
|
self.use_ray = use_ray
|
2023-02-23 09:32:19 +00:00
|
|
|
|
|
|
|
# Which pipeline stage is this node assigned to?
|
2023-03-22 04:45:42 +08:00
|
|
|
self.is_first_stage = stage_id == 0
|
2023-02-23 09:32:19 +00:00
|
|
|
self.is_last_stage = False
|
|
|
|
|
|
|
|
self.workers: List[Worker] = []
|
2023-03-22 04:45:42 +08:00
|
|
|
for rank, node_resource, device_id in stage_devices:
|
2023-04-30 15:42:17 +08:00
|
|
|
if self.use_ray:
|
|
|
|
worker_cls = ray.remote(num_cpus=0,
|
|
|
|
num_gpus=1,
|
|
|
|
resources={node_resource: 1e-5})(Worker).remote
|
|
|
|
else:
|
|
|
|
worker_cls = Worker
|
|
|
|
worker = worker_cls(
|
2023-02-23 09:32:19 +00:00
|
|
|
model_name=model_name,
|
|
|
|
block_size=block_size,
|
|
|
|
num_gpu_blocks=num_gpu_blocks,
|
|
|
|
num_cpu_blocks=num_cpu_blocks,
|
2023-02-23 21:31:39 +00:00
|
|
|
dtype=dtype,
|
2023-03-10 09:58:21 -08:00
|
|
|
seed=seed,
|
2023-03-22 04:45:42 +08:00
|
|
|
distributed_init_method=distributed_init_method,
|
|
|
|
rank=rank,
|
|
|
|
world_size=world_size,
|
|
|
|
tensor_parallel_size=tensor_parallel_size,
|
|
|
|
pipeline_parallel_size=pipeline_parallel_size,
|
2023-05-03 15:32:04 +08:00
|
|
|
cache_dir=cache_dir,
|
2023-04-08 23:36:12 -07:00
|
|
|
use_dummy_weights=use_dummy_weights,
|
2023-05-03 15:32:04 +08:00
|
|
|
use_np_cache=use_np_cache,
|
2023-04-05 11:16:57 -07:00
|
|
|
max_num_batched_tokens=max_num_batched_tokens,
|
2023-02-23 09:32:19 +00:00
|
|
|
)
|
|
|
|
self.workers.append(worker)
|
|
|
|
|
|
|
|
def set_next(
|
|
|
|
self,
|
|
|
|
next_node: Union['Controller', 'Scheduler'],
|
|
|
|
) -> None:
|
|
|
|
self.next_node = next_node
|
|
|
|
self.is_last_stage = isinstance(next_node, Scheduler)
|
|
|
|
|
|
|
|
def execute_stage(
|
|
|
|
self,
|
2023-03-10 09:58:21 -08:00
|
|
|
input_seq_groups: List[SequenceGroupInputs],
|
2023-02-23 09:32:19 +00:00
|
|
|
blocks_to_swap_in: Dict[int, int],
|
|
|
|
blocks_to_swap_out: Dict[int, int],
|
2023-03-10 09:58:21 -08:00
|
|
|
blocks_to_copy: Dict[int, List[int]],
|
2023-02-23 09:32:19 +00:00
|
|
|
) -> None:
|
2023-04-30 15:42:17 +08:00
|
|
|
all_outputs = []
|
2023-03-22 04:45:42 +08:00
|
|
|
for worker in self.workers:
|
2023-04-30 15:42:17 +08:00
|
|
|
executor = (worker.execute_stage.remote
|
|
|
|
if self.use_ray else worker.execute_stage)
|
|
|
|
output = executor(
|
2023-03-22 04:45:42 +08:00
|
|
|
input_seq_groups,
|
|
|
|
blocks_to_swap_in,
|
|
|
|
blocks_to_swap_out,
|
|
|
|
blocks_to_copy,
|
|
|
|
)
|
2023-04-30 15:42:17 +08:00
|
|
|
all_outputs.append(output)
|
|
|
|
|
|
|
|
if self.use_ray:
|
|
|
|
all_outputs = ray.get(all_outputs)
|
2023-03-22 04:45:42 +08:00
|
|
|
|
|
|
|
# Make sure all workers have the same results.
|
|
|
|
output = all_outputs[0]
|
|
|
|
for other_output in all_outputs[1:]:
|
|
|
|
assert output == other_output
|
2023-02-23 09:32:19 +00:00
|
|
|
|
|
|
|
if self.is_last_stage:
|
|
|
|
self.next_node.post_step(output)
|
|
|
|
else:
|
|
|
|
# TODO: Support pipeline parallelism.
|
|
|
|
assert False
|