DEV Community

matias yoon
matias yoon

Posted on

로컬 LLM 셋업 가이드 (v26)

로컬 LLM 셋업 가이드 (v26)

개요 & 전제 조건

로컬에서 LLM을 실행하는 것은 데이터 프라이버시, 성능, 비용 절감을 위해 필수적인 접근 방식입니다. 이 가이드는 소비자 하드웨어에서 최적화된 로컬 LLM 인퍼런스를 설정하는 실용적인 방법을 제공합니다.

전제 조건:

  • Linux 시스템 (Ubuntu 20.04+ 권장)
  • 최소 16GB RAM (32GB 이상 권장)
  • NVIDIA GPU (CUDA 지원) 또는 AMD GPU (ROCm 지원) 권장
  • 최소 20GB 여유 디스크 공간
  • git, curl, build-essential, python3, pip 설치 필요

프레임워크 비교: llama.cpp vs Ollama vs vLLM vs LocalAI

프레임워크 장점 단점 추천 사용 사례
llama.cpp 빠른 성능, 최적화된 C++ 구현, 가볍고 독립적 설치 복잡함, API 지원 제한 고성능 요구, 커스터마이즈 필요
Ollama 쉬운 설치, 간단한 API, 자동 모델 다운로드 최적화 부족, 더 많은 메모리 소모 빠른 실험, 개발용
vLLM 초고속 추론, 다중 GPU 지원, 고성능 복잡한 설치, 메모리 요구량 높음 프로덕션, 대규모 배포
LocalAI 다양한 백엔드 지원, REST API, 쉬운 통합 리소스 소모, 최적화 제한 다양한 도구와 연동 필요

추천: 이 가이드는 llama.cpp 기반 설치를 중심으로 설명합니다. 이유는 최적화된 성능과 낮은 리소스 소모, 그리고 독립적인 실행이 가능하다는 점입니다.

단계별 설치 가이드 (llama.cpp 기반)

  1. 필요한 패키지 설치:
sudo apt update
sudo apt install git curl build-essential python3 python3-pip
Enter fullscreen mode Exit fullscreen mode
  1. llama.cpp 클론 및 빌드:
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
make
Enter fullscreen mode Exit fullscreen mode
  1. 모델 다운로드 (예: Llama-3 8B):
mkdir models
cd models
wget https://huggingface.co/meta-llama/Llama-3-8B/resolve/main/original/consolidated.00.pth
wget https://huggingface.co/meta-llama/Llama-3-8B/resolve/main/original/params.json
Enter fullscreen mode Exit fullscreen mode
  1. 모델 변환:
cd ../
python3 convert.py models/
Enter fullscreen mode Exit fullscreen mode
  1. Quantization (Q4_K_M) 실행:
./llama-main -m models/llama-3-8b.Q4_K_M.gguf -p "Hello, how are you?" -n 100
Enter fullscreen mode Exit fullscreen mode

모델 선택 가이드

모델 추천 사용 사례 성능 메모리 요구
Llama-3 8B Q4_K_M 일반 개발, 채팅, 코드 생성 높음 8GB
Mixtral 8x7B 복잡한 작업, 대규모 코드 생성 매우 높음 16GB+
Phi-3 Mini 빠른 추론, 가벼운 작업 중간 4GB
Mistral 7B 코드 리뷰, 학습, 분석 높음 8GB

양자화 유형 설명 (Q4_K_M, Q5_K_M 등)

유형 품질 메모리 사용량 성능
Q4_K_M 4비트 양자화, 최적화된 Kahan Sum 4GB 높음
Q5_K_M 5비트 양자화, Kahan Sum 최적화 5GB 높음
Q6_K 6비트 양자화 6GB 중간
Q8_0 8비트 양자화 8GB 보통

API 설정 및 기존 도구 통합

  1. llama.cpp API 서버 시작:
./server -m models/llama-3-8b.Q4_K_M.gguf -c 2048 --host 0.0.0.0 --port 8080
Enter fullscreen mode Exit fullscreen mode
  1. API 호출 예제 (curl):
curl http://localhost:8080/completion -H "Content-Type: application/json" -d '{
  "prompt": "Write a Python function to calculate Fibonacci numbers.",
  "n_predict": 100,
  "temperature": 0.7
}'
Enter fullscreen mode Exit fullscreen mode
  1. Python SDK 예제:
import requests

def call_llm(prompt):
    response = requests.post(
        'http://localhost:8080/completion',
        json={
            'prompt': prompt,
            'n_predict': 100,
            'temperature': 0.7
        }
    )
    return response.json()['content']

result = call_llm("Explain quantum computing in simple terms")
print(result)
Enter fullscreen mode Exit fullscreen mode

Systemd 서비스 설정 (24/7 운영)

  1. 서비스 파일 생성:
sudo nano /etc/systemd/system/llm.service
Enter fullscreen mode Exit fullscreen mode
  1. 내용 입력:
[Unit]
Description=Local LLM Server
After=network.target

[Service]
Type=simple
User=your_username
WorkingDirectory=/home/your_username/llama.cpp
ExecStart=/home/your_username/llama.cpp/server -m /home/your_username/models/llama-3-8b.Q4_K_M.gguf -c 2048 --host 0.0.0.0 --port 8080
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode
  1. 서비스 시작:
sudo systemctl daemon-reload
sudo systemctl enable llm
sudo systemctl start llm
sudo systemctl status llm
Enter fullscreen mode Exit fullscreen mode

모니터링 및 성능 튜닝

  1. 메모리 사용량 확인:
watch -n 1 nvidia-smi  # NVIDIA GPU
htop                 # 전체 시스템
Enter fullscreen mode Exit fullscreen mode
  1. 성능 벤치마크:
# 단일 요청 시간 측정
time ./llama-main -m models/llama-3-8b.Q4_K_M.gguf -p "Test prompt" -n 50

# 일괄 요청 성능
ab -n 100 -c 10 http://localhost:8080/completion
Enter fullscreen mode Exit fullscreen mode
  1. CPU 및 GPU 최적화 설정:
# CPU 스레드 수 조정 (기본 8개)
./server -m models/llama-3-8b.Q4_K_M.gguf -t 16

# GPU 메모리 사용량 제한 (NVIDIA)
CUDA_VISIBLE_DEVICES=0 ./server -m models/llama-3-8b.Q4_K_M.gguf
Enter fullscreen mode Exit fullscreen mode

실용적 명령어 예제

1. 모델 다운로드 및 변환

# 모델 다운로드
wget https://huggingface.co/TheBloke/Llama-3-8B-Chat-GGUF/resolve/main/llama-3-8b-chat.Q4_K_M.gguf

# 양자화 실행
./llama-main -m llama-3-8b-chat.Q4_K_M.gguf -p "Explain quantum computing" -n 100

# API 서버 실행
./server -m llama-3-8b-chat.Q4_K_M.gguf -c 2048 --host 0.0.0.0 --port 8080
Enter fullscreen mode Exit fullscreen mode

2. 성능 최적화


bash
# 메모리 최적화 실행
./llama-main -m llama-3-8b-chat.Q4_K_M.gguf -t 8 -p "Test" -n 50 --temp 0.7

# GPU 메모리 관리
CUDA_VISIBLE_DEVICES=0 ./server -m llama-3-8b-chat.Q4_K_M.gguf -c 4096 --host 0.0.0.0 --port 808

---

📥 **Get the full guide on Gumroad**: https://gumroad.com/l/auto ($7)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)