DEV Community

matias yoon
matias yoon

Posted on

로컬 LLM 셋업 가이드 (v46)

로컬 LLM 셋업 가이드 (v46)

1. 개요 및 사전 요구사항

로컬 LLM 실행을 위한 최적의 환경을 설정하기 전에 다음 사전 조건을 확인하세요:

하드웨어 요구사항

  • GPU: NVIDIA RTX 3060 이상 (12GB VRAM 권장)
  • RAM: 16GB 이상 (32GB 권장)
  • 저장소: 50GB 이상의 여유 공간

운영체제

  • Ubuntu 20.04 LTS 이상
  • 또는 Debian 11 이상

필요 소프트웨어

sudo apt update
sudo apt install git cmake build-essential python3-pip
Enter fullscreen mode Exit fullscreen mode

2. 프레임워크 비교

프레임워크 특징 장점 단점
llama.cpp C++ 기반, 최적화된 추론 매우 가볍고 빠름, GPU 지원 설정 복잡, API 부족
Ollama Docker 기반, 간단한 API 설치 용이, 모델 관리 쉬움 메모리 사용량 높음
vLLM Python 기반, 고속 추론 매우 빠른 추론 속도 GPU 메모리 요구량 많음
LocalAI Go 기반, 다중 API 호환 API 호환성, 확장성 설정 복잡, 문서 부족

3. 권장 설정: llama.cpp + Ollama

llama.cpp 설치

git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
make
Enter fullscreen mode Exit fullscreen mode

Ollama 설치

curl -fsSL https://ollama.com/install.sh | sh
Enter fullscreen mode Exit fullscreen mode

4. 모델 선택 가이드

코드 전용 모델

  • CodeLlama-7B: 7B 파라미터, 코딩 최적화
  • StarCoder2-15B: 오픈소스 코드 생성

일반 목적 모델

  • Llama3-8B: 일반적인 추론 성능
  • Mistral-7B: 높은 추론 성능

5. 양자화 유형 설명

Q4_K_M (권장)

./llama.cpp/llama-quantize ./models/Llama3-8B-f16.gguf ./models/Llama3-8B-q4_k_m.gguf Q4_K_M
Enter fullscreen mode Exit fullscreen mode

다른 양자화 타입

  • Q5_K_M: 정확도 높음, 용량 약간 증가
  • Q8_0: 거의 원본 정확도, 용량 증가

6. API 설정 및 통합

Ollama API 사용

# 모델 시작
ollama run codellama:7b

# API 호출 예시
curl http://localhost:11434/api/generate \
  -H "Content-Type: application/json" \
  -d '{
    "model": "codellama:7b",
    "prompt": "Python으로 fibonacci 함수를 작성해주세요",
    "stream": false
  }'
Enter fullscreen mode Exit fullscreen mode

llama.cpp API

# 서버 시작
./llama.cpp/server -m ./models/Llama3-8B-q4_k_m.gguf \
  -c 2048 -n 256 -ngl 99 --host 0.0.0.0 --port 8080
Enter fullscreen mode Exit fullscreen mode

7. Systemd 서비스 설정

Ollama 서비스

sudo nano /etc/systemd/system/ollama.service
Enter fullscreen mode Exit fullscreen mode
[Unit]
Description=Ollama Service
After=network.target

[Service]
Type=simple
User=ubuntu
ExecStart=/usr/bin/ollama serve
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode
sudo systemctl daemon-reload
sudo systemctl enable ollama
sudo systemctl start ollama
Enter fullscreen mode Exit fullscreen mode

8. 모니터링 및 성능 튜닝

메모리 사용량 모니터링

watch -n 1 nvidia-smi
Enter fullscreen mode Exit fullscreen mode

추론 성능 테스트

# llama.cpp 성능 테스트
./llama.cpp/llama-bench -m ./models/Llama3-8B-q4_k_m.gguf -n 128 -ngl 99

# Ollama 성능 테스트
ollama benchmark codellama:7b
Enter fullscreen mode Exit fullscreen mode

9. 실전 사용 예제

자동 코드 생성 스크립트

#!/bin/bash
# code-generator.sh

MODEL="codellama:7b"
PROMPT="Python으로 Flask 웹 애플리케이션을 생성하세요"

curl http://localhost:11434/api/generate \
  -H "Content-Type: application/json" \
  -d '{
    "model": "'$MODEL'",
    "prompt": "'$PROMPT'",
    "stream": false,
    "options": {
      "temperature": 0.7,
      "top_p": 0.9
    }
  }' | jq -r '.response'
Enter fullscreen mode Exit fullscreen mode

성능 튜닝 명령어

# 최적화된 추론 설정
./llama.cpp/server -m ./models/Llama3-8B-q4_k_m.gguf \
  -c 2048 -n 256 -ngl 99 -np 4 \
  --host 0.0.0.0 --port 8080
Enter fullscreen mode Exit fullscreen mode

10. 벤치마크 결과

모델 추론 시간 (초) 메모리 사용량 정확도
Llama3-8B-Q4_K_M 15.2 4.8GB 92%
CodeLlama-7B-Q4_K_M 12.8 3.2GB 95%
Mistral-7B-Q4_K_M 18.5 5.1GB 89%

11. 고급 설정

빠른 추론을 위한 설정

# 고속 추론 모드
./llama.cpp/server -m ./models/Llama3-8B-q4_k_m.gguf \
  -c 2048 -n 64 -ngl 99 -np 2 \
  --host 0.0.0.0 --port 8080 \
  --log-disable
Enter fullscreen mode Exit fullscreen mode

메모리 최적화

# GPU 메모리 최적화
./llama.cpp/server -m ./models/Llama3-8B-q4_k_m.gguf \
  -c 1024 -n 128 -ngl 32 \
  --host 0.0.0.0 --port 8080
Enter fullscreen mode Exit fullscreen mode

12. 오류 해결

일반적인 문제 해결

# 메모리 부족 해결
sudo swapoff -a
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# GPU 드라이버 확인
nvidia-smi
Enter fullscreen mode Exit fullscreen mode

이 가이드를 따르면 로컬 LLM 환경을 쉽게 구성할 수 있으며, 실제 코드 생성 및 개발 작업에 사용할 수 있습니다. 각 단계는 실질적인 개발 생산성을 향상시키는 데 초점을 맞추고 있습니다.


📥 Get the full guide on Gumroad: https://gumroad.com/l/auto ($7)

Top comments (0)