로컬 LLM 셋업 가이드 (v20)
1. 개요 & 사전 요구사항
로컬 LLM 실행은 GPU가 있는 시스템에서 최적의 성능을 제공하지만, CPU만으로도 실행할 수 있습니다. 성능은 하드웨어에 따라 크게 달라집니다.
사전 요구사항:
- Linux 시스템 (Ubuntu 20.04 이상 권장)
- RAM: 최소 8GB, 권장 16GB 이상
- GPU: NVIDIA GTX 10xx 이상 (CUDA 지원), AMD 또는 CPU 환경에서도 가능
- 디스크 공간: 최소 5GB, 모델 다운로드 및 저장을 위해 10-20GB 권장
하드웨어 성능 기준:
- CPU 모드: 100-200 tokens/초
- GPU (RTX 3060): 500-1000 tokens/초
- GPU (RTX 4090): 1000+ tokens/초
2. 프레임워크 비교
| 프레임워크 | 장점 | 단점 | 권장 사용 사례 |
|---|---|---|---|
| llama.cpp | 높은 호환성, 최소 의존성, C++ 기반 | 설정이 복잡, 명령줄 중심 | 실시간 LLM 서버 구축 |
| Ollama | 쉬운 설치, Docker 기반 | 최적화가 제한적 | 개발 테스트, 빠른 프로토타이핑 |
| vLLM | 높은 성능, 대용량 모델 지원 | 설치 복잡, 메모리 요구량 큼 | 실시간 API 서버 |
| LocalAI | 다양한 API 호환성, RESTful API | 복잡한 구성 | 프로덕션 LLM API 서비스 |
3. 추천 설정 - llama.cpp 기반 설치
1) 필요 패키지 설치:
sudo apt update
sudo apt install -y build-essential git cmake python3-pip
2) llama.cpp 클론:
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
3) 빌드:
make clean
make
4) 모델 다운로드 및 준비:
mkdir -p models
cd models
wget https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF/resolve/main/llama-2-7b-chat.Q4_K_M.gguf
4. 모델 선택 가이드
| 사용 사례 | 권장 모델 | 크기 | 성능 |
|---|---|---|---|
| 일반 채팅 | Llama-2-7B-Chat | 4GB | 높음 |
| 코드 생성 | CodeLlama-7B | 4GB | 높음 |
| 번역 | Mistral-7B | 4GB | 중간 |
| 최대 성능 | Llama-3-8B | 5GB | 높음 |
| CPU 실행용 | Phi-3-mini | 2GB | 중간 |
5. 양자화 유형 설명
# 양자화 유형별 품질 및 크기
Q4_K_M: 4.5GB, 최고 품질
Q5_K_M: 5.0GB, 품질 우수
Q6_K: 6.0GB, 품질 높음
Q8_0: 8.0GB, 최대 품질
F16: 16GB, 원본 품질
성능 비교:
# 예제 명령어
./main -m models/llama-2-7b-chat.Q4_K_M.gguf -p "Hello, how are you?"
# Q4_K_M: 150 tokens/sec, 4.5GB
# F16: 80 tokens/sec, 16GB
6. API 설정 및 통합
1) 기본 서버 실행:
./main -m models/llama-2-7b-chat.Q4_K_M.gguf -p "You are a helpful assistant." --host 0.0.0.0 --port 8080
2) API 호출 예제:
curl -X POST http://localhost:8080/completion \
-H "Content-Type: application/json" \
-d '{"prompt": "Write a Python function to calculate Fibonacci numbers.", "n_predict": 100}'
3) 프로젝트 통합:
# Python API 사용 예시
import requests
def call_local_llm(prompt):
response = requests.post(
"http://localhost:8080/completion",
json={
"prompt": prompt,
"n_predict": 100
}
)
return response.json()['content']
7. Systemd 서비스 설정
1) 서비스 파일 생성:
sudo nano /etc/systemd/system/local-llm.service
2) 서비스 내용:
[Unit]
Description=Local LLM Server
After=network.target
[Service]
Type=simple
User=your-user
WorkingDirectory=/home/your-user/llama.cpp
ExecStart=/home/your-user/llama.cpp/main -m /home/your-user/llama.cpp/models/llama-2-7b-chat.Q4_K_M.gguf --host 0.0.0.0 --port 8080
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
3) 서비스 시작:
sudo systemctl daemon-reload
sudo systemctl enable local-llm.service
sudo systemctl start local-llm.service
8. 모니터링 및 성능 최적화
1) 시스템 모니터링:
# GPU 사용량 확인
nvidia-smi
# CPU 및 메모리 사용량
htop
# 성능 테스트
./main -m models/llama-2-7b-chat.Q4_K_M.gguf -p "Test prompt" -n 100
2) 메모리 최적화:
# 최대 1GB 메모리 사용
./main -m models/llama-2-7b-chat.Q4_K_M.gguf --ctx 1024 --n-gpu-layers 32
3) CPU 스레드 최적화:
# CPU 코어 수에 따라 스레드 수 조정
./main -m models/llama-2-7b-chat.Q4_K_M.gguf -t 8
9. 실제 예제 및 성능 벤치마크
1) 기본 성능 테스트:
# 단일 응답 시간 측정
time ./main -m models/llama-2-7b-chat.Q4_K_M.gguf -p "Explain quantum computing in simple terms."
2) 병렬 처리 테스트:
# 병렬 요청 테스트
ab -n 10 -c 5 http://localhost:8080/completion
3) 성능 비교:
- Q4_K_M: 150 tokens/sec (4.5GB)
- Q5_K_M: 120 tokens/sec (5.0GB)
- F16: 80 tokens/sec (16GB)
이 가이드를 따라하면 로컬에서 빠르고 효율적으로 LLM을 실행할 수 있습니다. 최소한의 리소스로도 고성능 LLM 서버를 구축할 수 있으며, Systemd 서비스를 통해 24/7 지속 실행이 가능합니다.
📥 Get the full guide on Gumroad: https://gumroad.com/l/auto ($7)
Top comments (0)