본문으로 건너뛰기

[sglang] GC Threshold 인자 추가: Python 가비지 컬렉션 주기 튜닝 지원

PR 링크: sgl-project/sglang#21481 상태: Merged | 변경: +21 / -0

들어가며

Python의 GC(Garbage Collection)는 순환 참조를 해제하기 위해 주기적으로 실행됩니다. LLM 추론 서버처럼 대량의 텐서 객체를 다루는 환경에서는 GC가 예상치 못한 시점에 발생하여 지연(latency spike)을 유발할 수 있습니다. 이번 PR은 --gc-threshold 인자를 추가하여, 운영자가 GC 수집 빈도를 직접 튜닝할 수 있도록 합니다.

핵심 코드 분석

1. 서버 인자 정의

# server_args.py
gc_threshold: Optional[List[int]] = None

parser.add_argument(
    "--gc-threshold",
    type=int,
    nargs="+",
    help="Set the garbage collection thresholds (the collection frequency). "
         "Accepts 1 to 3 integers.",
)

Python의 gc.set_threshold(threshold0, threshold1, threshold2)는 1~3개의 정수를 받습니다. threshold0은 generation 0 수집 빈도, threshold1threshold2는 각각 generation 1, 2의 수집 빈도를 제어합니다.

2. GC 설정 적용

# engine.py
def _set_gc(server_args: ServerArgs):
    if gc_threshold := server_args.gc_threshold:
        import gc
        gc.set_threshold(*gc_threshold)

서버 시작 시 _launch_subprocesses에서 _set_gc를 호출합니다.

3. 유효성 검증

if self.gc_threshold:
    if not (1 <= len(self.gc_threshold) <= 3):
        raise ValueError(
            "When setting gc_threshold, it must contain 1 to 3 integers."
        )

왜 이게 좋은가

  1. 지연 제어: --gc-threshold 50000 같이 높은 값을 설정하면 GC 빈도가 줄어 latency spike가 감소합니다.
  2. 메모리 트레이드오프: GC를 덜 자주 하면 메모리 사용량이 증가할 수 있지만, LLM 서버에서는 대부분의 메모리가 GPU에 있으므로 영향이 적습니다.
  3. 운영 유연성: 워크로드 특성에 따라 GC 전략을 조정할 수 있습니다.

정리

21줄 추가의 간결한 기능 PR이지만, 프로덕션 LLM 서버의 tail latency 최적화에 중요한 도구를 제공합니다. Python GC 튜닝은 고처리량 서버에서 흔히 사용되는 테크닉입니다.

참고 자료

⚠️ 알림: 이 분석은 AI가 실제 코드 diff를 기반으로 작성했습니다.

댓글

관련 포스트

PR Analysis 의 다른글