전문가 병렬
전문가 병렬 (Expert parallelism)
전문가 병렬(Expert parallelism)은 mixture-of-experts (MoE) 모델을 위한 병렬화 전략이에요. 각 전문가의 feedforward 레이어는 서로 다른 하드웨어 가속기에 배치돼요. 라우터(router)가 토큰을 알맞은 전문가에게 배정하고 결과를 모아줘요. 이 방식은 계산 비용을 늘리지 않으면서도 모델을 훨씬 더 큰 파라미터 수로 확장할 수 있게 해줘요. 각 토큰이 몇 개의 전문가만 활성화하기 때문이에요.
출처: 문서
본문
DistributedConfig[[transformers.DistributedConfig]]
DistributedConfig 클래스와 enable_expert_parallel 인자로 전문가 병렬을 활성화할 수 있어요.
import os
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.distributed.configuration_utils import DistributedConfig
distributed_config = DistributedConfig(
tp_size=int(os.environ["WORLD_SIZE"]),
enable_expert_parallel=True,
)
model = AutoModelForCausalLM.from_pretrained(
"openai/gpt-oss-120b",
distributed_config=distributed_config,
)
[!TIP] 전문가 병렬은 attention 레이어에 대해 tensor parallelism을 자동으로 활성화해요.
이 인자는 각 MoE 모델의 config 파일에 정의된 ep_plan(전문가 병렬 플랜)으로 전환해줘요. GroupedGemmParallel 클래스는 전문가 가중치를 분할해서 각 디바이스가 자신의 로컬 전문가만 로드하게 해줘요. ep_router는 토큰을 전문가에게 라우팅하고, all-reduce 연산이 전문가들의 출력을 합쳐줘요.
torchrun으로 추론 스크립트를 실행하고 사용할 디바이스 수를 지정해요. 디바이스 수는 전체 전문가 수를 정확히 나눌 수 있어야 해요.
torchrun --nproc-per-node 8 your_script.py
transformers.DistributedConfig[[transformers.DistributedConfig]]
transformers.DistributedConfig(tp_size: int | None = None, tp_plan: typing.Union[dict[str, str], typing.Literal['auto'], NoneType] = None, enable_sequence_parallel: bool = False, enable_expert_parallel: bool = False, fsdp_size: int | None = None, fsdp_cpu_offload: bool = False, fsdp_mixed_precision: bool = False, pp_size: int | None = None)
Parameters:
tp_size (int, optional) : tensor parallelism에 사용할 디바이스 수예요. None이고 tp_plan이 설정된 경우 WORLD_SIZE // (other_parallel_size)로 기본 설정돼요. None이고 tp_plan이 없는 경우에는 기본값이 1이에요.
tp_plan (dict[str, str] or "auto", optional) : tensor parallel 샤딩 플랜이에요. 모델이 미리 정의한 base_model_tp_plan을 사용하려면 *"auto"*를 전달하거나, tp_size가 설정된 경우 None으로 남겨두면 돼요. 미리 정의된 플랜을 덮어쓰려면 사전(dictionary)을 전달해요.
enable_sequence_parallel (bool, optional, defaults to False) : sequence parallelism을 위해 예약된 인자예요. 아직 연결(와이어링)되지 않았어요.
enable_expert_parallel (bool, optional, defaults to False) : MoE 모델을 전문가 병렬 경로(base_model_ep_plan)로 라우팅해요.
fsdp_size (int, optional) : FSDP(데이터 병렬)에 사용할 디바이스 수예요. None이고 tp_size가 설정된 경우 기본값이 1이에요.
fsdp_cpu_offload (bool, optional, defaults to False) : FSDP2에 대해 CPU offloading을 활성화할지 여부예요.
fsdp_mixed_precision (bool, optional, defaults to False) : FSDP2에 대해 혼합 정밀도(mixed precision)를 활성화할지 여부예요.
pp_size (int, optional) : pipeline parallelism에 사용할 디바이스 수예요. None이고 다른 병렬 모드가 설정된 경우 기본값이 1이에요.
tensor, pipeline, 또는 FSDP2 병렬을 사용한 네이티브 분산 추론과 학습을 위한 구성이에요.
더 알아보기 (Learn more)
- expert parallelism에 대한 자세한 튜토리얼을 확인해 보세요: ultrascale-playbook