HiSparse: 계층형 희소 어텐션
HiSparse: 계층형 희소 어텐션 (Hierarchical Sparse Attention)
HiSparse는 decode 단계에서 요청당 GPU 메모리 소비를 줄이기 위해, GPU에는 작은 "hot" KV 버퍼만 유지하면서 전체 KV 데이터는 CPU pinned memory에 두는 기법입니다. PD disaggregation과 결합하면 decode 동시성을 크게 높일 수 있어요.
출처: 문서
본문
HiSparse는 decode 단계에서 요청당 GPU 메모리 소비를 줄입니다. GPU에는 작은 "hot" KV 버퍼만 유지하고, 전체 KV 데이터는 CPU pinned memory에 둡니다. PD disaggregation과 결합하면 decode 동시성을 훨씬 높일 수 있어요.
전제 조건 (Prerequisites): HiSparse는 DeepSeek Sparse Attention (DSA) 아키텍처(예: DeepSeek-V3.2, GLM-5.1), DeepSeek V4, MiniMax M3 모델에서 동작합니다. 이 모델들은 원래 어텐션에 토큰의 일부만 선택하므로, 정확도 손실 없이 GPU에는 top-k KV만 두고 전체 KV는 호스트 메모리에 저장할 수 있어요. 또한 HiSparse는 현재 PD disaggregation 모드가 필요하며 decode 인스턴스에서만 활성화됩니다.
왜 HiSparse인가 (Why HiSparse?)
긴 컨텍스트 LLM 추론에서 각 디코딩 요청은 전체 길이의 KV 캐시를 GPU에 보유해, decode 인스턴스가 서빙할 수 있는 동시 요청 수를 제한합니다. HiSparse는 다음으로 이를 해결합니다:
- 요청당 GPU 메모리 절감 (Reducing GPU memory per request): 각 요청은 전체 시퀀스 길이 대신 고정 크기 디바이스 버퍼(예: 4KB 토큰)만 차지함
- 주문형 스왑인 (On-demand swap-in): CUDA 커널이 어텐션 점수를 기반으로 호스트 메모리에서 top-k의 가장 관련성 높은 KV 항목을 동적으로 로드함
- 프리필에 투명 (Transparent to prefill): HiSparse는 순전히 decode-측 최적화이며, prefill 인스턴스에는 변경이 필요 없음
설계 개요 (Design Overview)
Decode 워크플로우 (Decode Workflow)
각 decode 단계는 다음 흐름을 따릅니다:
- Forward decode — 다음 토큰 생성
- Top-k 선택 (Top-k selection) — 어텐션 점수로 가장 관련성 높은 토큰 위치 선택
- 스왑인 (Swap-in) — CUDA 커널이 top-k KV 항목을 호스트에서 디바이스 버퍼로 로드:
- 짧은 시퀀스 (
seq_len ≤ device_buffer_size): fast path, 모든 KV가 이미 버퍼에 있음 - 긴 시퀀스: hit detection → LRU 재정렬 → miss 처리 (호스트 → 디바이스 복사)
- 짧은 시퀀스 (
- Decode 어텐션 (Decode attention) — top-k 디바이스 위치로 어텐션 계산
- Eager backup — 이전 토큰의 KV를 디바이스에서 호스트로 비동기 복사
PD Disaggregation 통합 (Direct-to-Host)
PD disaggregation 모드에서 prefill 인스턴스는 RDMA를 통해 decode 인스턴스의 호스트 풀로 KV 캐시를 직접 전송하며, decode 측 GPU는 완전히 우회합니다. 이는 KV 전송 중 일시적인 GPU 메모리 스파이크를 제거하고 스테이징 DMA 단계를 없앱니다.
Prefill GPU ──RDMA──▶ Decode Host Pool (CPU pinned memory)
│
▼
alloc device buffer (4KB)
│
▼
swap-in kernel (on-demand top-k)
DeepSeek V4의 경우 direct-to-host 경로는 C4 KV만 decode 호스트 풀에 씁니다. c4_indexer와 C128 KV는 디바이스 간 전송으로 유지됩니다.
서버 인자 (Server Arguments)
| Argument | Type / Default | Description |
|---|---|---|
--enable-hisparse |
flag; default: disabled | decode 인스턴스에서 HiSparse 활성화 |
--hisparse-config |
JSON string | HiSparse 설정 (아래 참고) |
HiSparse 설정 파라미터 (HiSparse Config Parameters)
--hisparse-config로 JSON 문자열을 전달합니다:
| Parameter | Type / Default | Description |
|---|---|---|
top_k |
int | topk 항목 수 |
device_buffer_size |
int | 요청별 GPU 디바이스 버퍼의 토큰 슬롯 수 |
host_to_device_ratio |
int | 논리 풀 크기 대비 디바이스 풀 크기 비율, 호스트 메모리 용량 결정 |
swap_in_block_size |
int / 960 | HiSparse swap-in 커널의 CUDA thread-block 크기 |
예: --hisparse-config='{"top_k": 2048, "device_buffer_size": 6144, "host_to_device_ratio": 10, "swap_in_block_size": 960}'
공유 인덱스 프리페치 (Shared-index prefetch, automatic)
모델이 하나의 anchor 레이어의 top-k 선택을 일련의 후속 "skip" 레이어에서 재사용할 때(GLM-5.2에서 IndexShare로 네이티브한 DSA index_topk_freq / index_topk_pattern), anchor의 인덱스가 계산되는 순간 모든 skip 레이어의 working set이 알려집니다. HiSparse는 이를 자동으로 활용합니다. anchor의 swap-in 커널은 miss 플랜(어느 호스트 슬롯이 어느 디바이스 버퍼 슬롯으로 가는지)을 기록하고, 각 skip 레이어는 그 플랜을 side stream에서 미리 발행된 copy-only 커널로 재생해, skip 레이어의 host→device I/O가 decode critical path에 얹히는 대신 사이 레이어의 compute와 겹치게 합니다. replayed 커널은 겹치는 동안 SM footprint를 낮게 유지하도록 작은 고정 그리드를 사용합니다.
프리페치는 적격 모델(파이프라인 병렬·추측 디코딩이 없는)에서 자동으로 활성화되며, SGLANG_DISABLE_HISPARSE_PREFETCH=1로 A/B 비교용으로 끌 수 있습니다.
배포 (Deployment)
HiSparse는 현재 PD disaggregation 모드가 필요하며 decode 인스턴스에서만 활성화됩니다.
Prefill 인스턴스
python3 -m sglang.launch_server \
--model-path /path/to/model \
--trust-remote-code \
--port 8000 --host 0.0.0.0 \
--context-length 81920 \
--chunked-prefill-size 65536 \
--tp-size 8 --dp-size 8 --enable-dp-attention \
--mem-fraction-static 0.85 \
--disaggregation-mode prefill \
--disaggregation-ib-device mlx5_0,mlx5_1,mlx5_2,mlx5_3 \
--nnodes 1 --node-rank 0
Decode 인스턴스 (HiSparse 포함)
python3 -m sglang.launch_server \
--model-path /path/to/model \
--trust-remote-code \
--port 8000 --host 0.0.0.0 \
--context-length 81920 \
--tp-size 8 --dp-size 8 --enable-dp-attention \
--mem-fraction-static 0.85 \
--disable-radix-cache \
--disaggregation-mode decode \
--disaggregation-ib-device mlx5_0,mlx5_1,mlx5_2,mlx5_3 \
--dist-init-addr 127.0.0.1:5757 \
--nnodes 1 --node-rank 0 \
--enable-hisparse \
--hisparse-config='{"top_k": 2048, "device_buffer_size": 6144, "host_to_device_ratio": 10, "swap_in_block_size": 960}'
참고 (Note): DSA 모델의 경우
--kv-cache-dtype기본값은auto이며, SM100+ (Blackwell)에서는fp8_e4m3, 그 이전 아키텍처에서는bfloat16으로 결정됩니다. DSA decode 백엔드는 KV dtype에 따라 자동 선택됩니다(bfloat16→flashmla_sparse,fp8_e4m3→flashmla_kv). 단,fp8_e4m3로 SM120/SM121에서 실행하는 GLM DSA 모델은flashinfer_sparse_mla를 사용합니다. DSA 백엔드 플래그는 DSA 모델에만 적용되며, DeepSeek V4는 자체dsv4어텐션 백엔드를 사용합니다.
MiniMax M3
Dense 레이어 K/V와 인덱스 K는 GPU에 유지하고, sparse 레이어 K/V는 호스트 메모리와 GPU working set을 사용합니다.
- TP 4 이상을 사용하고, 두 PD 인스턴스에서 동일한 TP 크기와 PP 1을 사용하세요.
--attention-backend triton,--mm-attention-backend triton_attn,--disable-prefill-cuda-graph,--disable-radix-cache를 사용하세요.--hisparse-config에서device_buffer_size를 최소 2048로 설정하세요.top_k는 모델의 선택 폭을 오버라이드하지 않습니다.- PD retraction backup은 지원되지 않습니다. 예상 출력 길이만큼 용량을 예약하려면
--num-reserved-decode-tokens를 사용하세요.
벤치마크 (Benchmark)
python3 -m sglang.bench_serving \
--backend sglang \
--dataset-path /path/to/ShareGPT_V3_unfiltered_cleaned_split.json \
--dataset-name random \
--random-input 40000 \
--random-output 20000 \
--num-prompts 200 \
--max-concurrency 200 \
--request-rate 40 \
--random-range-ratio 1.0 \
--host 127.0.0.1 \
--port 20000 \
--model /path/to/model \
--flush-cache \
핵심 참고 사항 (Key Notes)
- prefill 인스턴스는
--enable-hisparse가 필요하지 않습니다. HiSparse를 인지하지 못해요. - decode 인스턴스에는 HiSparse를 위해
--enable-hisparse와--hisparse-config가 필요합니다. - DSA 모델의 경우
--kv-cache-dtype bfloat16은flashmla_sparse를,--kv-cache-dtype fp8_e4m3는flashmla_kv를 사용합니다. - SM120/SM121(예: RTX PRO 6000, RTX 5090)에서 GLM DSA 모델과
--kv-cache-dtype fp8_e4m3를 쓰면 두 DSA 백엔드 모두flashinfer_sparse_mla로 해석됩니다. 이는 해당 아키텍처에서 유일한 DSA 커널이에요. HiSparse는 거기서 이를 수용하며 추가 플래그가 필요 없습니다. - DeepSeek V4에는 DSA 백엔드 플래그가 적용되지 않습니다. DeepSeek V4는
dsv4어텐션 백엔드와 기본fp8_e4m3KV 캐시를 사용합니다. host_to_device_ratio는 호스트 머신의 가용 메모리에 따라 구성해야 합니다. 예:- ~1 TB 호스트 메모리 →
host_to_device_ratio: 5 - ~2 TB 호스트 메모리 →
host_to_device_ratio: 10
- ~1 TB 호스트 메모리 →
감사의 말 (Acknowledgments)
구현과 아낌없는 지원을 준 SGLang 팀과 커뮤니티에 감사합니다. 특히 Zhiqiang Xie, Zhangheng Huang, Tingwei Huang, Shangming Cai, Teng Ma와 많은 분들께 감사드립니다. Alibaba Cloud TairKVCache 팀과 AntGroup SCT Inference 팀의 소중한 기여도 감사합니다.