feat: 8비트 양자화 옵션 추가 및 CPU 메모리 최적화 구현
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
from transformers import AutoModelForCausalLM, AutoTokenizer
|
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
||||||
from huggingface_hub import HfApi
|
from huggingface_hub import HfApi
|
||||||
|
|
||||||
def download_model(config_path='./config.json'):
|
def download_model(config_path='./config.json'):
|
||||||
@@ -12,6 +12,8 @@ def download_model(config_path='./config.json'):
|
|||||||
|
|
||||||
model_name = config['model_name']
|
model_name = config['model_name']
|
||||||
local_path = config['model_local_path']
|
local_path = config['model_local_path']
|
||||||
|
model_settings = config.get('model_settings', {})
|
||||||
|
use_quantization = model_settings.get('use_quantization', False)
|
||||||
|
|
||||||
if not os.path.exists(local_path):
|
if not os.path.exists(local_path):
|
||||||
os.makedirs(local_path)
|
os.makedirs(local_path)
|
||||||
@@ -19,10 +21,21 @@ def download_model(config_path='./config.json'):
|
|||||||
print(f"모델 {model_name}을 {local_path}에 다운로드 중...")
|
print(f"모델 {model_name}을 {local_path}에 다운로드 중...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# 양자화 설정 적용
|
||||||
|
if use_quantization:
|
||||||
|
print("8bit 양자화 적용")
|
||||||
|
quantization_config = BitsAndBytesConfig(
|
||||||
|
load_in_8bit=True,
|
||||||
|
llm_int8_enable_fp32_cpu_offload=True
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
quantization_config = None
|
||||||
|
|
||||||
model = AutoModelForCausalLM.from_pretrained(
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
model_name,
|
model_name,
|
||||||
cache_dir=local_path,
|
cache_dir=local_path,
|
||||||
device_map="auto", # GPU 자동 할당
|
quantization_config=quantization_config,
|
||||||
|
device_map="cpu", # 다운로드 시 CPU에 로드하여 메모리 절약
|
||||||
torch_dtype="auto"
|
torch_dtype="auto"
|
||||||
)
|
)
|
||||||
tokenizer = AutoTokenizer.from_pretrained(
|
tokenizer = AutoTokenizer.from_pretrained(
|
||||||
|
|||||||
Reference in New Issue
Block a user