Spaces:
Running
Running
Auto commit at 22-2025-08 16:26:25
Browse files- fix_kanana_target_modules.py +83 -0
- inspect_kanana_model.py +95 -0
- lily_llm_api/app_v2.py +138 -107
- lily_llm_api/models/kanana_1_5_v_3b_instruct.py +37 -17
- lily_llm_api/models/polyglot_ko_1_3b_chat.py +4 -0
- lily_llm_api/models/polyglot_ko_5_8b_chat.py +5 -1
- lily_llm_core/lora_manager.py +13 -0
- lily_llm_core/rag_processor.py +4 -4
- test_lora_integration.py +93 -0
- test_model_selection.py +86 -0
- test_model_type_fix.py +90 -0
- test_rag_integration.py +1 -0
fix_kanana_target_modules.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Kanana ๋ชจ๋ธ์ ์ ํํ target modules ํจํด ์ฐพ๊ธฐ
|
| 4 |
+
"""
|
| 5 |
+
import sys
|
| 6 |
+
import os
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
# ํ๋ก์ ํธ ๋ฃจํธ ๊ฒฝ๋ก ์ถ๊ฐ
|
| 10 |
+
project_root = Path(__file__).parent
|
| 11 |
+
sys.path.insert(0, str(project_root))
|
| 12 |
+
|
| 13 |
+
def find_exact_target_modules():
|
| 14 |
+
"""์ ํํ target modules ํจํด ์ฐพ๊ธฐ"""
|
| 15 |
+
print("๐ Kanana ๋ชจ๋ธ์ ์ ํํ target modules ํจํด ์ฐพ๊ธฐ...")
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
import torch
|
| 19 |
+
from transformers import AutoModelForVision2Seq
|
| 20 |
+
|
| 21 |
+
model_path = "./lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 22 |
+
|
| 23 |
+
print(f"๐ฅ ๋ชจ๋ธ ๋ก๋ฉ ์ค: {model_path}")
|
| 24 |
+
|
| 25 |
+
# ๋ชจ๋ธ ๋ก๋
|
| 26 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 27 |
+
model_path,
|
| 28 |
+
trust_remote_code=True,
|
| 29 |
+
local_files_only=True,
|
| 30 |
+
torch_dtype=torch.bfloat16
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
print(f"โ
๋ชจ๋ธ ๋ก๋ ์ฑ๊ณต!")
|
| 34 |
+
|
| 35 |
+
# language_model ๋ถ๋ถ์ ์ ํํ ๋ชจ๋ ์ด๋ฆ ์ฐพ๊ธฐ
|
| 36 |
+
print("\n๐ฏ Language Model ๋ชจ๋ ๊ฒ์:")
|
| 37 |
+
|
| 38 |
+
target_candidates = []
|
| 39 |
+
|
| 40 |
+
for name, module in model.named_modules():
|
| 41 |
+
# language_model ๋ถ๋ถ๋ง ํํฐ๋ง
|
| 42 |
+
if name.startswith("language_model.model.layers."):
|
| 43 |
+
if hasattr(module, 'weight') and module.weight is not None:
|
| 44 |
+
module_type = type(module).__name__
|
| 45 |
+
|
| 46 |
+
# LoRA์ ์ ํฉํ ๋ชจ๋๋ค ์ฐพ๊ธฐ
|
| 47 |
+
if any(pattern in name for pattern in ['q_proj', 'k_proj', 'v_proj', 'o_proj']):
|
| 48 |
+
target_candidates.append((name, module_type, "Attention"))
|
| 49 |
+
elif any(pattern in name for pattern in ['gate_proj', 'up_proj', 'down_proj']):
|
| 50 |
+
target_candidates.append((name, module_type, "MLP"))
|
| 51 |
+
|
| 52 |
+
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
| 53 |
+
if target_candidates:
|
| 54 |
+
print(" โ
๋ฐ๊ฒฌ๋ target modules:")
|
| 55 |
+
for name, module_type, category in target_candidates:
|
| 56 |
+
print(f" - {name} ({module_type}) - {category}")
|
| 57 |
+
|
| 58 |
+
# ์ค์ ์ฌ์ฉํ target modules ์ถ์ถ
|
| 59 |
+
print("\n๐ ์ค์ ์ฌ์ฉํ target modules:")
|
| 60 |
+
target_modules = []
|
| 61 |
+
for name, _, _ in target_candidates:
|
| 62 |
+
target_modules.append(name)
|
| 63 |
+
print(f" '{name}',")
|
| 64 |
+
|
| 65 |
+
print(f"\n๐ข ์ด {len(target_modules)}๊ฐ์ target modules ๋ฐ๊ฒฌ")
|
| 66 |
+
|
| 67 |
+
else:
|
| 68 |
+
print(" โ language_model์์ target modules๋ฅผ ์ฐพ์ ์ ์์")
|
| 69 |
+
|
| 70 |
+
# ๋ชจ๋ธ ํด์
|
| 71 |
+
del model
|
| 72 |
+
import gc
|
| 73 |
+
gc.collect()
|
| 74 |
+
|
| 75 |
+
print("\nโ
target modules ๊ฒ์ ์๋ฃ!")
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
print(f"โ target modules ๊ฒ์ ์คํจ: {e}")
|
| 79 |
+
import traceback
|
| 80 |
+
traceback.print_exc()
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
find_exact_target_modules()
|
inspect_kanana_model.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Kanana ๋ชจ๋ธ ๊ตฌ์กฐ ํ์ธ ์คํฌ๋ฆฝํธ
|
| 4 |
+
"""
|
| 5 |
+
import sys
|
| 6 |
+
import os
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
# ํ๋ก์ ํธ ๋ฃจํธ ๊ฒฝ๋ก ์ถ๊ฐ
|
| 10 |
+
project_root = Path(__file__).parent
|
| 11 |
+
sys.path.insert(0, str(project_root))
|
| 12 |
+
|
| 13 |
+
def inspect_kanana_model():
|
| 14 |
+
"""Kanana ๋ชจ๋ธ์ ๊ตฌ์กฐ๋ฅผ ํ์ธํ์ฌ target modules ์ฐพ๊ธฐ"""
|
| 15 |
+
print("๐ Kanana ๋ชจ๋ธ ๊ตฌ์กฐ ํ์ธ ์์...")
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
import torch
|
| 19 |
+
from transformers import AutoModelForVision2Seq, AutoProcessor
|
| 20 |
+
|
| 21 |
+
model_path = "./lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 22 |
+
|
| 23 |
+
print(f"๐ฅ ๋ชจ๋ธ ๋ก๋ฉ ์ค: {model_path}")
|
| 24 |
+
|
| 25 |
+
# ํ๋ก์ธ์ ๋ก๋
|
| 26 |
+
processor = AutoProcessor.from_pretrained(
|
| 27 |
+
model_path,
|
| 28 |
+
trust_remote_code=True,
|
| 29 |
+
local_files_only=True
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# ๋ชจ๋ธ ๋ก๋
|
| 33 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 34 |
+
model_path,
|
| 35 |
+
trust_remote_code=True,
|
| 36 |
+
local_files_only=True,
|
| 37 |
+
torch_dtype=torch.bfloat16
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
print(f"โ
๋ชจ๋ธ ๋ก๋ ์ฑ๊ณต!")
|
| 41 |
+
print(f"๐ ๋ชจ๋ธ ํ์
: {type(model).__name__}")
|
| 42 |
+
print(f"๐ ๋ชจ๋ธ ๊ตฌ์กฐ:")
|
| 43 |
+
|
| 44 |
+
# ๋ชจ๋ธ์ ๋ชจ๋ named_modules ํ์ธ
|
| 45 |
+
print("\n๐ ๋ชจ๋ named_modules:")
|
| 46 |
+
for name, module in model.named_modules():
|
| 47 |
+
if hasattr(module, 'weight') and module.weight is not None:
|
| 48 |
+
print(f" - {name}: {type(module).__name__}")
|
| 49 |
+
|
| 50 |
+
# ์ผ๋ฐ์ ์ธ LoRA target modules ํจํด ์ฐพ๊ธฐ
|
| 51 |
+
print("\n๐ฏ LoRA target modules ํ๋ณด:")
|
| 52 |
+
target_candidates = []
|
| 53 |
+
|
| 54 |
+
for name, module in model.named_modules():
|
| 55 |
+
module_type = type(module).__name__
|
| 56 |
+
|
| 57 |
+
# ์ผ๋ฐ์ ์ธ LoRA target modules ํจํด
|
| 58 |
+
if any(pattern in name.lower() for pattern in ['q_proj', 'k_proj', 'v_proj', 'o_proj']):
|
| 59 |
+
target_candidates.append((name, module_type, "Attention"))
|
| 60 |
+
elif any(pattern in name.lower() for pattern in ['gate_proj', 'up_proj', 'down_proj']):
|
| 61 |
+
target_candidates.append((name, module_type, "MLP"))
|
| 62 |
+
elif any(pattern in name.lower() for pattern in ['query_key_value', 'dense_h_to_4h', 'dense_4h_to_h']):
|
| 63 |
+
target_candidates.append((name, module_type, "GPTNeoX"))
|
| 64 |
+
elif any(pattern in name.lower() for pattern in ['fc1', 'fc2', 'proj']):
|
| 65 |
+
target_candidates.append((name, module_type, "Linear"))
|
| 66 |
+
|
| 67 |
+
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
| 68 |
+
if target_candidates:
|
| 69 |
+
print(" โ
๋ฐ๊ฒฌ๋ target modules:")
|
| 70 |
+
for name, module_type, category in target_candidates:
|
| 71 |
+
print(f" - {name} ({module_type}) - {category}")
|
| 72 |
+
else:
|
| 73 |
+
print(" โ ์ผ๋ฐ์ ์ธ ํจํด์ ์ฐพ์ ์ ์์")
|
| 74 |
+
|
| 75 |
+
# ๋ชจ๋ธ์ ์ฒซ ๋ฒ์งธ ๋ ์ด์ด ๊ตฌ์กฐ ์์ธํ ๋ณด๊ธฐ
|
| 76 |
+
print("\n๐ ์ฒซ ๋ฒ์งธ ๋ ์ด์ด ๊ตฌ์กฐ:")
|
| 77 |
+
for name, module in list(model.named_modules())[:20]:
|
| 78 |
+
if hasattr(module, 'weight') and module.weight is not None:
|
| 79 |
+
print(f" - {name}: {type(module).__name__} (shape: {module.weight.shape})")
|
| 80 |
+
|
| 81 |
+
# ๋ชจ๋ธ ํด์
|
| 82 |
+
del model
|
| 83 |
+
del processor
|
| 84 |
+
import gc
|
| 85 |
+
gc.collect()
|
| 86 |
+
|
| 87 |
+
print("\nโ
๋ชจ๋ธ ๊ตฌ์กฐ ํ์ธ ์๋ฃ!")
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"โ ๋ชจ๋ธ ๊ตฌ์กฐ ํ์ธ ์คํจ: {e}")
|
| 91 |
+
import traceback
|
| 92 |
+
traceback.print_exc()
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
inspect_kanana_model()
|
lily_llm_api/app_v2.py
CHANGED
|
@@ -54,6 +54,11 @@ from lily_llm_core.hybrid_rag_processor import hybrid_rag_processor
|
|
| 54 |
# ์ปจํ
์คํธ ๊ด๋ฆฌ์ ๋ฐ LoRA ๊ด๋ฆฌ์ ์ถ๊ฐ
|
| 55 |
from lily_llm_core.context_manager import get_context_manager, context_manager
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# LoRA ๊ด๋ฆฌ์ import (์ ํ์ )
|
| 58 |
try:
|
| 59 |
from lily_llm_core.lora_manager import get_lora_manager, lora_manager
|
|
@@ -65,6 +70,124 @@ except ImportError as e:
|
|
| 65 |
lora_manager = None
|
| 66 |
get_lora_manager = None
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# ===== lifespan ์ปจํ
์คํธ ๋งค๋์ (์๋ฒ ์์/์ข
๋ฃ ์ด๋ฒคํธ) =====
|
| 69 |
from contextlib import asynccontextmanager
|
| 70 |
|
|
@@ -81,9 +204,9 @@ async def lifespan(app: FastAPI):
|
|
| 81 |
except Exception as e:
|
| 82 |
logger.error(f"โ CPU ์ค๋ ๋ ์ค์ ์คํจ: {e}")
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
selected_model_id =
|
| 86 |
-
logger.info(f"๐ ์๋ฒ ์์ ์
|
| 87 |
|
| 88 |
try:
|
| 89 |
await load_model_async(selected_model_id)
|
|
@@ -110,47 +233,8 @@ async def lifespan(app: FastAPI):
|
|
| 110 |
except Exception as e:
|
| 111 |
logger.warning(f"โ ๏ธ ๊ณ ๊ธ ์ปจํ
์คํธ ๊ด๋ฆฌ์ ์ค์ ์คํจ: {e}")
|
| 112 |
|
| 113 |
-
# LoRA ์๋
|
| 114 |
-
|
| 115 |
-
try:
|
| 116 |
-
logger.info("๐ง ์๋ฒ ์์ ํ LoRA ์๋ ์ค์ ์์...")
|
| 117 |
-
|
| 118 |
-
# ๋ชจ๋ธ ๊ฒฝ๋ก ์ค์
|
| 119 |
-
current_model_path = "./lily_llm_core/models/polyglot_ko_1_3b_chat"
|
| 120 |
-
logger.info(f"๐ LoRA ๋ชจ๋ธ ๊ฒฝ๋ก: {current_model_path}")
|
| 121 |
-
|
| 122 |
-
# LoRA ๊ธฐ๋ณธ ๋ชจ๋ธ ๋ก๋
|
| 123 |
-
logger.info("๐ง LoRA ๊ธฐ๋ณธ ๋ชจ๋ธ ๋ก๋ ์์...")
|
| 124 |
-
success = lora_manager.load_base_model(current_model_path, "causal_lm")
|
| 125 |
-
if success:
|
| 126 |
-
logger.info("โ
LoRA ๊ธฐ๋ณธ ๋ชจ๋ธ ๋ก๋ ์ฑ๊ณต")
|
| 127 |
-
|
| 128 |
-
# LoRA ์ค์ ์์ฑ
|
| 129 |
-
logger.info("๐ง LoRA ์ค์ ์์ฑ ์์...")
|
| 130 |
-
lora_config = lora_manager.create_lora_config(
|
| 131 |
-
r=16,
|
| 132 |
-
lora_alpha=32,
|
| 133 |
-
lora_dropout=0.1,
|
| 134 |
-
bias="none",
|
| 135 |
-
task_type="CAUSAL_LM",
|
| 136 |
-
target_modules=["query_key_value", "mlp.dense_h_to_4h", "mlp.dense_4h_to_h"]
|
| 137 |
-
)
|
| 138 |
-
logger.info("โ
LoRA ์ค์ ์์ฑ ์๋ฃ")
|
| 139 |
-
|
| 140 |
-
# LoRA ์ด๋ํฐ ์ ์ฉ
|
| 141 |
-
logger.info("๐ง LoRA ์ด๋ํฐ ์ ์ฉ ์์...")
|
| 142 |
-
adapter_success = lora_manager.apply_lora_to_model("auto_adapter")
|
| 143 |
-
if adapter_success:
|
| 144 |
-
logger.info("โ
LoRA ์ด๋ํฐ ์ ์ฉ ์๋ฃ: auto_adapter")
|
| 145 |
-
logger.info("๐ ์๋ฒ ์์ ์ LoRA ์๋ ์ค์ ์๋ฃ!")
|
| 146 |
-
else:
|
| 147 |
-
logger.error("โ LoRA ์ด๋ํฐ ์ ์ฉ ์คํจ")
|
| 148 |
-
else:
|
| 149 |
-
logger.error("โ LoRA ๊ธฐ๋ณธ ๋ชจ๋ธ ๋ก๋ ์คํจ")
|
| 150 |
-
except Exception as e:
|
| 151 |
-
logger.error(f"โ LoRA ์๋ ์ค์ ์ค ์ค๋ฅ: {e}")
|
| 152 |
-
else:
|
| 153 |
-
logger.warning("โ ๏ธ LoRA๊ฐ ์ฌ์ฉ ๋ถ๊ฐ๋ฅํ์ฌ ์๋ ์ค์ ๊ฑด๋๋")
|
| 154 |
|
| 155 |
except Exception as e:
|
| 156 |
logger.error(f"โ ๋ชจ๋ธ ๋ก๋์ ์คํจํ์ต๋๋ค: {e}", exc_info=True)
|
|
@@ -325,14 +409,14 @@ def select_model_interactive():
|
|
| 325 |
print(f"{i:2d}. {model_info['name']} ({model_info['model_id']})")
|
| 326 |
while True:
|
| 327 |
try:
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
selected_model = available_models[1]
|
| 331 |
print(f"\nโ
'{selected_model['name']}' ๋ชจ๋ธ์ ์ ํํ์ต๋๋ค.")
|
| 332 |
return selected_model['model_id']
|
| 333 |
except (ValueError, IndexError):
|
| 334 |
print(f"โ 1์์ {len(available_models)} ์ฌ์ด์ ์ซ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.")
|
| 335 |
-
except KeyboardInterrupt:
|
|
|
|
| 336 |
|
| 337 |
# @app.on_event("startup") - FastAPI ์ต์ ๋ฒ์ ์์ ์๋ํ์ง ์์
|
| 338 |
# startup_event ํจ์๋ lifespan์ผ๋ก ์ด๋๋จ
|
|
@@ -358,7 +442,7 @@ async def load_model_endpoint(model_id: str):
|
|
| 358 |
|
| 359 |
def load_model_sync(model_id: str):
|
| 360 |
"""๋ชจ๋ธ ๋ฐ ๊ด๋ จ ํ๋ก์ธ์๋ฅผ ๋๊ธฐ์ ์ผ๋ก ๋ก๋ฉ (์ต์ข
์์ ๋ณธ)"""
|
| 361 |
-
global model, tokenizer, processor, current_profile
|
| 362 |
|
| 363 |
try:
|
| 364 |
if model is not None:
|
|
@@ -377,6 +461,9 @@ def load_model_sync(model_id: str):
|
|
| 377 |
# ์ด์ load_model์ (model, processor)๋ฅผ ๋ฐํํฉ๋๋ค.
|
| 378 |
model, processor = current_profile.load_model()
|
| 379 |
|
|
|
|
|
|
|
|
|
|
| 380 |
# processor์์ tokenizer๋ฅผ ๊บผ๋ด ์ ์ญ ๋ณ์์ ํ ๋นํฉ๋๋ค.
|
| 381 |
if hasattr(processor, 'tokenizer'):
|
| 382 |
tokenizer = processor.tokenizer
|
|
@@ -386,64 +473,8 @@ def load_model_sync(model_id: str):
|
|
| 386 |
|
| 387 |
logger.info(f"โ
'{current_profile.display_name}' ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
| 388 |
|
| 389 |
-
# LoRA ๊ธฐ๋ณธ ๋ชจ๋ธ ์๋ ๋ก๋
|
| 390 |
-
|
| 391 |
-
if LORA_AVAILABLE and lora_manager:
|
| 392 |
-
# ํ์ฌ ๋ก๋๋ ๋ชจ๋ธ ๊ฒฝ๋ก ํ์ธ
|
| 393 |
-
current_model_path = None
|
| 394 |
-
if hasattr(current_profile, 'model_path') and current_profile.model_path:
|
| 395 |
-
current_model_path = current_profile.model_path
|
| 396 |
-
logger.info(f"๐ ๋ชจ๋ธ ๊ฒฝ๋ก ์ง์ ์ฌ์ฉ: {current_model_path}")
|
| 397 |
-
elif hasattr(current_profile, 'model_id') and current_profile.model_id:
|
| 398 |
-
# ๋ชจ๋ธ ID๋ฅผ ๊ฒฝ๋ก๋ก ๋ณํ
|
| 399 |
-
model_id = current_profile.model_id
|
| 400 |
-
logger.info(f"๐ ๋ชจ๋ธ ID ๊ฐ์ง: {model_id}")
|
| 401 |
-
|
| 402 |
-
if model_id == "polyglot-ko-1.3b-chat":
|
| 403 |
-
current_model_path = "./lily_llm_core/models/polyglot_ko_1_3b_chat"
|
| 404 |
-
elif model_id == "kanana-1.5-v-3b-instruct":
|
| 405 |
-
current_model_path = "./lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 406 |
-
elif model_id == "polyglot-ko-5.8b-chat":
|
| 407 |
-
current_model_path = "./lily_llm_core/models/polyglot_ko_5_8b_chat"
|
| 408 |
-
|
| 409 |
-
logger.info(f"๐ ๋ณํ๋ ๋ชจ๋ธ ๊ฒฝ๋ก: {current_model_path}")
|
| 410 |
-
|
| 411 |
-
if current_model_path:
|
| 412 |
-
logger.info(f"๐ง LoRA ๊ธฐ๋ณธ ๋ชจ๋ธ ์๋ ๋ก๋ ์์: {current_model_path}")
|
| 413 |
-
success = lora_manager.load_base_model(current_model_path, "causal_lm")
|
| 414 |
-
if success:
|
| 415 |
-
logger.info(f"โ
LoRA ๊ธฐ๋ณธ ๋ชจ๋ธ ์๋ ๋ก๋ ์ฑ๊ณต: {current_model_path}")
|
| 416 |
-
|
| 417 |
-
# LoRA ์ค์ ์๋ ์์ฑ
|
| 418 |
-
try:
|
| 419 |
-
logger.info("๐ง LoRA ์ค์ ์๋ ์์ฑ ์์...")
|
| 420 |
-
lora_config = lora_manager.create_lora_config(
|
| 421 |
-
r=16,
|
| 422 |
-
lora_alpha=32,
|
| 423 |
-
lora_dropout=0.1,
|
| 424 |
-
bias="none",
|
| 425 |
-
task_type="CAUSAL_LM",
|
| 426 |
-
target_modules=["query_key_value", "mlp.dense_h_to_4h", "mlp.dense_4h_to_h"]
|
| 427 |
-
)
|
| 428 |
-
logger.info("โ
LoRA ์ค์ ์๋ ์์ฑ ์๋ฃ")
|
| 429 |
-
|
| 430 |
-
# LoRA ์ด๋ํฐ ์๋ ์ ์ฉ
|
| 431 |
-
logger.info("๐ง LoRA ์ด๋ํฐ ์๋ ์ ์ฉ ์์...")
|
| 432 |
-
adapter_success = lora_manager.apply_lora_to_model("auto_adapter")
|
| 433 |
-
if adapter_success:
|
| 434 |
-
logger.info("โ
LoRA ์ด๋ํฐ ์๋ ์ ์ฉ ์๋ฃ: auto_adapter")
|
| 435 |
-
else:
|
| 436 |
-
logger.error("โ LoRA ์ด๋ํฐ ์๋ ์ ์ฉ ์คํจ")
|
| 437 |
-
except Exception as e:
|
| 438 |
-
logger.error(f"โ LoRA ๏ฟฝ๏ฟฝ๏ฟฝ์ /์ด๋ํฐ ์๋ ์์ฑ ์คํจ: {e}")
|
| 439 |
-
else:
|
| 440 |
-
logger.error(f"โ LoRA ๊ธฐ๋ณธ ๋ชจ๋ธ ์๋ ๋ก๋ ์คํจ: {current_model_path}")
|
| 441 |
-
else:
|
| 442 |
-
logger.warning("โ ๏ธ ํ์ฌ ๋ชจ๋ธ์ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ ์ ์์ด LoRA ์๋ ๋ก๋ ๊ฑด๋๋")
|
| 443 |
-
else:
|
| 444 |
-
logger.info("โ ๏ธ LoRA๊ฐ ์ฌ์ฉ ๋ถ๊ฐ๋ฅํ์ฌ ์๋ ๋ก๋ ๊ฑด๋๋")
|
| 445 |
-
except Exception as e:
|
| 446 |
-
logger.error(f"โ LoRA ์๋ ๋ก๋ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 447 |
|
| 448 |
except Exception as e:
|
| 449 |
logger.error(f"โ load_model_sync ์คํจ: {e}")
|
|
|
|
| 54 |
# ์ปจํ
์คํธ ๊ด๋ฆฌ์ ๋ฐ LoRA ๊ด๋ฆฌ์ ์ถ๊ฐ
|
| 55 |
from lily_llm_core.context_manager import get_context_manager, context_manager
|
| 56 |
|
| 57 |
+
# ์ ์ญ ๋ณ์๋ค
|
| 58 |
+
current_model = None # ๐ ํ์ฌ ๋ก๋๋ ๋ชจ๋ธ ์ธ์คํด์ค
|
| 59 |
+
current_profile = None # ๐ ํ์ฌ ์ ํ๋ ๋ชจ๋ธ ํ๋กํ
|
| 60 |
+
model_loaded = False # ๐ ๋ชจ๋ธ ๋ก๋ ์ํ
|
| 61 |
+
|
| 62 |
# LoRA ๊ด๋ฆฌ์ import (์ ํ์ )
|
| 63 |
try:
|
| 64 |
from lily_llm_core.lora_manager import get_lora_manager, lora_manager
|
|
|
|
| 70 |
lora_manager = None
|
| 71 |
get_lora_manager = None
|
| 72 |
|
| 73 |
+
# ===== ๊ณตํต LoRA ์ค์ ํจ์ =====
|
| 74 |
+
def setup_lora_for_model(profile, lora_manager):
|
| 75 |
+
"""๋ชจ๋ธ ํ๋กํ์ ๋ฐ๋ฅธ LoRA ์ค์ (๊ณตํต ํจ์)"""
|
| 76 |
+
if not LORA_AVAILABLE or not lora_manager:
|
| 77 |
+
logger.warning("โ ๏ธ LoRA๊ฐ ์ฌ์ฉ ๋ถ๊ฐ๋ฅํ์ฌ ์๋ ์ค์ ๊ฑด๋๋")
|
| 78 |
+
return False
|
| 79 |
+
|
| 80 |
+
try:
|
| 81 |
+
logger.info("๐ง LoRA ์๋ ์ค์ ์์...")
|
| 82 |
+
|
| 83 |
+
# ๐ ๋ชจ๋ธ ํ๋กํ์์ ๊ฒฝ๋ก ๋ฐ ํ์
์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
|
| 84 |
+
current_model_path = None
|
| 85 |
+
model_type = "causal_lm" # ๊ธฐ๋ณธ๊ฐ
|
| 86 |
+
|
| 87 |
+
# ๐ ๋ชจ๋ธ ํ๋กํ์์ ๊ฒฝ๋ก ๋ฐ ํ์
์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
|
| 88 |
+
if hasattr(profile, 'local_path') and profile.local_path:
|
| 89 |
+
# ๋ก์ปฌ ํ๊ฒฝ: ๋ก์ปฌ ๊ฒฝ๋ก ์ฌ์ฉ
|
| 90 |
+
current_model_path = profile.local_path
|
| 91 |
+
# ๐ local_path ์ฌ์ฉ ์์๋ model_type ์ค์ ํ์
|
| 92 |
+
if hasattr(profile, 'model_id') and profile.model_id:
|
| 93 |
+
model_id = profile.model_id
|
| 94 |
+
if model_id == "kanana-1.5-v-3b-instruct":
|
| 95 |
+
model_type = "vision2seq" # ๐ kanana๋ vision2seq ํ์
|
| 96 |
+
else:
|
| 97 |
+
model_type = "causal_lm" # ๊ธฐ๋ณธ๊ฐ
|
| 98 |
+
logger.info(f"๐ ๋ชจ๋ธ ํ๋กํ์์ ๋ก์ปฌ ๊ฒฝ๋ก ์ฌ์ฉ: {current_model_path}")
|
| 99 |
+
logger.info(f"๐ ๊ฒฐ์ ๋ ๋ชจ๋ธ ํ์
: {model_type}")
|
| 100 |
+
elif hasattr(profile, 'model_id') and profile.model_id:
|
| 101 |
+
# ๋ชจ๋ธ ID๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ฒฝ๋ก ๊ฒฐ์
|
| 102 |
+
model_id = profile.model_id
|
| 103 |
+
logger.info(f"๐ ๋ชจ๋ธ ID ๊ธฐ๋ฐ ๊ฒฝ๋ก ๊ฒฐ์ : {model_id}")
|
| 104 |
+
|
| 105 |
+
# ๐ ํ๊ฒฝ์ ๋ฐ๋ฅธ ๊ฒฝ๋ก ๊ฒฐ์
|
| 106 |
+
if hasattr(profile, 'is_local') and profile.is_local:
|
| 107 |
+
# ๋ก์ปฌ ํ๊ฒฝ: ๋ก์ปฌ ๊ฒฝ๋ก ์ฌ์ฉ
|
| 108 |
+
if model_id == "polyglot-ko-1.3b-chat":
|
| 109 |
+
current_model_path = "./lily_llm_core/models/polyglot_ko_1_3b_chat"
|
| 110 |
+
model_type = "causal_lm"
|
| 111 |
+
elif model_id == "kanana-1.5-v-3b-instruct":
|
| 112 |
+
current_model_path = "./lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 113 |
+
model_type = "vision2seq" # ๐ kanana๋ vision2seq ํ์
|
| 114 |
+
elif model_id == "polyglot-ko-5.8b-chat":
|
| 115 |
+
current_model_path = "./lily_llm_core/models/polyglot_ko_5_8b_chat"
|
| 116 |
+
model_type = "causal_lm"
|
| 117 |
+
else:
|
| 118 |
+
# ๋ฐฐํฌ ํ๊ฒฝ: HF ๋ชจ๋ธ๋ช
์ฌ์ฉ (๋ก์ปฌ ๊ฒฝ๋ก ์์)
|
| 119 |
+
current_model_path = None
|
| 120 |
+
logger.info(f"๐ ๋ฐฐํฌ ํ๊ฒฝ: LoRA ์ค์ ๊ฑด๋๋ (HF ๋ชจ๋ธ)")
|
| 121 |
+
return False
|
| 122 |
+
|
| 123 |
+
logger.info(f"๐ ๊ฒฐ์ ๋ ๋ชจ๋ธ ๊ฒฝ๋ก: {current_model_path}")
|
| 124 |
+
logger.info(f"๐ ๊ฒฐ์ ๋ ๋ชจ๋ธ ํ์
: {model_type}")
|
| 125 |
+
|
| 126 |
+
if not current_model_path:
|
| 127 |
+
logger.warning("โ ๏ธ ํ์ฌ ๋ชจ๋ธ์ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ ์ ์์ด LoRA ์๋ ๋ก๋ ๊ฑด๋๋")
|
| 128 |
+
return False
|
| 129 |
+
|
| 130 |
+
logger.info(f"๐ LoRA ๋ชจ๋ธ ๊ฒฝ๋ก: {current_model_path}")
|
| 131 |
+
logger.info(f"๐ LoRA ๋ชจ๋ธ ํ์
: {model_type}")
|
| 132 |
+
|
| 133 |
+
# ๐ ์ด๋ฏธ ๋ก๋๋ ๋ฉ์ธ ๋ชจ๋ธ์ LoRA์ ์ง์ ์ ์ฉ (์ค๋ณต ๋ก๋ ๋ฐฉ์ง)
|
| 134 |
+
logger.info("๐ง ๊ธฐ์กด ๋ฉ์ธ ๋ชจ๋ธ์ LoRA ์ง์ ์ ์ฉ ์์...")
|
| 135 |
+
|
| 136 |
+
# ๐ lora_manager์ ๊ธฐ์กด ๋ฉ์ธ ๋ชจ๋ธ ์ค์
|
| 137 |
+
if hasattr(lora_manager, 'base_model') and lora_manager.base_model is None:
|
| 138 |
+
# ์ ์ญ ๋ณ์์์ ๋ฉ์ธ ๋ชจ๋ธ ๊ฐ์ ธ์ค๊ธฐ
|
| 139 |
+
from lily_llm_api.app_v2 import current_model
|
| 140 |
+
if current_model is not None:
|
| 141 |
+
lora_manager.base_model = current_model
|
| 142 |
+
logger.info("โ
๊ธฐ์กด ๋ฉ์ธ ๋ชจ๋ธ์ LoRA ๊ด๋ฆฌ์์ ์ค์ ์๋ฃ")
|
| 143 |
+
else:
|
| 144 |
+
logger.warning("โ ๏ธ ๋ฉ์ธ ๋ชจ๋ธ์ ์ฐพ์ ์ ์์ด LoRA ์ค์ ๊ฑด๋๋")
|
| 145 |
+
return False
|
| 146 |
+
|
| 147 |
+
# LoRA ์ค์ ์์ฑ
|
| 148 |
+
logger.info("๐ง LoRA ์ค์ ์์ฑ ์์...")
|
| 149 |
+
|
| 150 |
+
# ๐ ๋ชจ๋ธ๋ณ target modules ์ค์
|
| 151 |
+
if model_type == "vision2seq" and "kanana" in profile.model_id:
|
| 152 |
+
# Kanana ๋ชจ๋ธ: Llama ๊ธฐ๋ฐ language model ์ฌ์ฉ (์ฒซ ๋ฒ์งธ ๋ ์ด์ด๋ง ์ฌ์ฉ)
|
| 153 |
+
target_modules = [
|
| 154 |
+
"language_model.model.layers.0.self_attn.q_proj",
|
| 155 |
+
"language_model.model.layers.0.self_attn.k_proj",
|
| 156 |
+
"language_model.model.layers.0.self_attn.v_proj",
|
| 157 |
+
"language_model.model.layers.0.self_attn.o_proj",
|
| 158 |
+
"language_model.model.layers.0.mlp.gate_proj",
|
| 159 |
+
"language_model.model.layers.0.mlp.up_proj",
|
| 160 |
+
"language_model.model.layers.0.mlp.down_proj"
|
| 161 |
+
]
|
| 162 |
+
else:
|
| 163 |
+
# ๊ธฐ์กด ๋ชจ๋ธ๋ค: GPTNeoX ๊ธฐ๋ฐ
|
| 164 |
+
target_modules = ["query_key_value", "mlp.dense_h_to_4h", "mlp.dense_4h_to_h"]
|
| 165 |
+
|
| 166 |
+
lora_config = lora_manager.create_lora_config(
|
| 167 |
+
r=16,
|
| 168 |
+
lora_alpha=32,
|
| 169 |
+
lora_dropout=0.1,
|
| 170 |
+
bias="none",
|
| 171 |
+
task_type="CAUSAL_LM" if model_type == "causal_lm" else "VISION_2_SEQ",
|
| 172 |
+
target_modules=target_modules
|
| 173 |
+
)
|
| 174 |
+
logger.info("โ
LoRA ์ค์ ์์ฑ ์๋ฃ")
|
| 175 |
+
|
| 176 |
+
# LoRA ์ด๋ํฐ ์ ์ฉ (๊ธฐ์กด ๋ฉ์ธ ๋ชจ๋ธ์ ์ง์ )
|
| 177 |
+
logger.info("๐ง LoRA ์ด๋ํฐ ์ ์ฉ ์์...")
|
| 178 |
+
adapter_success = lora_manager.apply_lora_to_model("auto_adapter")
|
| 179 |
+
if adapter_success:
|
| 180 |
+
logger.info("โ
LoRA ์ด๋ํฐ ์ ์ฉ ์๋ฃ: auto_adapter")
|
| 181 |
+
logger.info("๐ LoRA ์๋ ์ค์ ์๋ฃ!")
|
| 182 |
+
return True
|
| 183 |
+
else:
|
| 184 |
+
logger.error("โ LoRA ์ด๋ํฐ ์ ์ฉ ์คํจ")
|
| 185 |
+
return False
|
| 186 |
+
|
| 187 |
+
except Exception as e:
|
| 188 |
+
logger.error(f"โ LoRA ์๋ ์ค์ ์ค ์ค๋ฅ: {e}")
|
| 189 |
+
return False
|
| 190 |
+
|
| 191 |
# ===== lifespan ์ปจํ
์คํธ ๋งค๋์ (์๋ฒ ์์/์ข
๋ฃ ์ด๋ฒคํธ) =====
|
| 192 |
from contextlib import asynccontextmanager
|
| 193 |
|
|
|
|
| 204 |
except Exception as e:
|
| 205 |
logger.error(f"โ CPU ์ค๋ ๋ ์ค์ ์คํจ: {e}")
|
| 206 |
|
| 207 |
+
# ๐ ๋ชจ๋ธ ์ ํ ๋ณต์: ์ฌ์ฉ์๊ฐ ๋ชจ๋ธ์ ์ ํํ ์ ์๋๋ก
|
| 208 |
+
selected_model_id = select_model_interactive()
|
| 209 |
+
logger.info(f"๐ ์๋ฒ ์์ ์ ์ ํ๋ ๋ชจ๋ธ: {selected_model_id}")
|
| 210 |
|
| 211 |
try:
|
| 212 |
await load_model_async(selected_model_id)
|
|
|
|
| 233 |
except Exception as e:
|
| 234 |
logger.warning(f"โ ๏ธ ๊ณ ๊ธ ์ปจํ
์คํธ ๊ด๋ฆฌ์ ์ค์ ์คํจ: {e}")
|
| 235 |
|
| 236 |
+
# ๐ LoRA ์๋ ์ค์ ์ load_model_async ๋ด๋ถ์์ ์ด๋ฏธ ์ฒ๋ฆฌ๋จ
|
| 237 |
+
# setup_lora_for_model(current_profile, lora_manager) # ์ค๋ณต ํธ์ถ ์ ๊ฑฐ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
except Exception as e:
|
| 240 |
logger.error(f"โ ๋ชจ๋ธ ๋ก๋์ ์คํจํ์ต๋๋ค: {e}", exc_info=True)
|
|
|
|
| 409 |
print(f"{i:2d}. {model_info['name']} ({model_info['model_id']})")
|
| 410 |
while True:
|
| 411 |
try:
|
| 412 |
+
choice = input(f"\n๐ ์ฌ์ฉํ ๋ชจ๋ธ ๋ฒํธ๋ฅผ ์ ํํ์ธ์ (1-{len(available_models)}): ")
|
| 413 |
+
selected_model = available_models[int(choice) - 1]
|
|
|
|
| 414 |
print(f"\nโ
'{selected_model['name']}' ๋ชจ๋ธ์ ์ ํํ์ต๋๋ค.")
|
| 415 |
return selected_model['model_id']
|
| 416 |
except (ValueError, IndexError):
|
| 417 |
print(f"โ 1์์ {len(available_models)} ์ฌ์ด์ ์ซ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.")
|
| 418 |
+
except KeyboardInterrupt:
|
| 419 |
+
sys.exit("\n\n๐ ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.")
|
| 420 |
|
| 421 |
# @app.on_event("startup") - FastAPI ์ต์ ๋ฒ์ ์์ ์๋ํ์ง ์์
|
| 422 |
# startup_event ํจ์๋ lifespan์ผ๋ก ์ด๋๋จ
|
|
|
|
| 442 |
|
| 443 |
def load_model_sync(model_id: str):
|
| 444 |
"""๋ชจ๋ธ ๋ฐ ๊ด๋ จ ํ๋ก์ธ์๋ฅผ ๋๊ธฐ์ ์ผ๋ก ๋ก๋ฉ (์ต์ข
์์ ๋ณธ)"""
|
| 445 |
+
global model, tokenizer, processor, current_profile, current_model
|
| 446 |
|
| 447 |
try:
|
| 448 |
if model is not None:
|
|
|
|
| 461 |
# ์ด์ load_model์ (model, processor)๋ฅผ ๋ฐํํฉ๋๋ค.
|
| 462 |
model, processor = current_profile.load_model()
|
| 463 |
|
| 464 |
+
# ๐ ์ ์ญ ๋ณ์์ ๋ชจ๋ธ ์ค์ (LoRA์์ ์ฌ์ฉ)
|
| 465 |
+
current_model = model
|
| 466 |
+
|
| 467 |
# processor์์ tokenizer๋ฅผ ๊บผ๋ด ์ ์ญ ๋ณ์์ ํ ๋นํฉ๋๋ค.
|
| 468 |
if hasattr(processor, 'tokenizer'):
|
| 469 |
tokenizer = processor.tokenizer
|
|
|
|
| 473 |
|
| 474 |
logger.info(f"โ
'{current_profile.display_name}' ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
| 475 |
|
| 476 |
+
# ๐ LoRA ๊ธฐ๋ณธ ๋ชจ๋ธ ์๋ ๋ก๋ (๊ณตํต ํจ์ ์ฌ์ฉ)
|
| 477 |
+
setup_lora_for_model(current_profile, lora_manager)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
|
| 479 |
except Exception as e:
|
| 480 |
logger.error(f"โ load_model_sync ์คํจ: {e}")
|
lily_llm_api/models/kanana_1_5_v_3b_instruct.py
CHANGED
|
@@ -25,16 +25,21 @@ class Kanana15V3bInstructProfile:
|
|
| 25 |
# ํ๊ฒฝ ๊ฐ์ง
|
| 26 |
self.is_local = self._detect_local_environment()
|
| 27 |
|
| 28 |
-
# ๋ชจ๋ธ ๊ฒฝ๋ก ์ค์
|
|
|
|
|
|
|
| 29 |
if self.is_local:
|
| 30 |
-
|
| 31 |
self.local_path = "./lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 32 |
self.display_name = "kanana-1.5-v-3b-instruct"
|
| 33 |
else:
|
| 34 |
-
|
| 35 |
-
self.local_path = None
|
| 36 |
self.display_name = "kanana-1.5-v-3b-instruct"
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
self.description = "์นด์นด์ค ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ (3.6B) - Math RAG ํนํ"
|
| 39 |
self.language = "ko"
|
| 40 |
self.model_size = "3.6B"
|
|
@@ -97,15 +102,19 @@ class Kanana15V3bInstructProfile:
|
|
| 97 |
logger.error(f"โ ํ๊ฒฝ๋ณ์ ๋ก๋ ์คํจ: {e}")
|
| 98 |
|
| 99 |
def load_model(self) -> Tuple[Any, Any]:
|
| 100 |
-
"""๋ชจ๋ธ ๋ก๋ (
|
| 101 |
-
logger.info(f"๐ฅ {self.display_name} ๋ชจ๋ธ ๋ก๋
|
| 102 |
|
| 103 |
-
#
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
try:
|
| 111 |
from transformers import AutoModelForVision2Seq, AutoProcessor
|
|
@@ -119,16 +128,17 @@ class Kanana15V3bInstructProfile:
|
|
| 119 |
processor = AutoProcessor.from_pretrained(
|
| 120 |
model_path,
|
| 121 |
trust_remote_code=True,
|
| 122 |
-
local_files_only=use_local
|
|
|
|
| 123 |
)
|
| 124 |
|
| 125 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 126 |
|
| 127 |
# dtype ์ค์ ์ต์ ํ - CPU์์๋ float32 ์ฌ์ฉ
|
| 128 |
if device == 'cuda':
|
| 129 |
-
selected_dtype = torch.
|
| 130 |
else:
|
| 131 |
-
selected_dtype = torch.
|
| 132 |
|
| 133 |
logger.info(f"๐ง ์ ํ๋ dtype: {selected_dtype} (device: {device})")
|
| 134 |
|
|
@@ -163,8 +173,16 @@ class Kanana15V3bInstructProfile:
|
|
| 163 |
"top_p": 0.95,
|
| 164 |
"repetition_penalty": 1.1,
|
| 165 |
"no_repeat_ngram_size": 3,
|
| 166 |
-
"pad_token_id":
|
| 167 |
-
"eos_token_id":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
}
|
| 169 |
|
| 170 |
def extract_response(self, full_text: str, formatted_prompt: str = None, **kwargs) -> str:
|
|
@@ -220,7 +238,9 @@ class Kanana15V3bInstructProfile:
|
|
| 220 |
# ์ผ๋ฐ์ ์ธ ํ๋กฌํํธ ํจํด ์ ๊ฑฐ ์๋
|
| 221 |
patterns_to_remove = [
|
| 222 |
"<|im_start|>user\n",
|
|
|
|
| 223 |
"<|im_end|>",
|
|
|
|
| 224 |
"<image>",
|
| 225 |
"user\n",
|
| 226 |
"assistant\n"
|
|
|
|
| 25 |
# ํ๊ฒฝ ๊ฐ์ง
|
| 26 |
self.is_local = self._detect_local_environment()
|
| 27 |
|
| 28 |
+
# ๐ ๋ชจ๋ธ ๊ฒฝ๋ก ์ค์ (๋ก์ปฌ/๋ฐฐํฌ ํ๊ฒฝ ๋ชจ๋ ์ง์)
|
| 29 |
+
self.model_name = "kakaocorp/kanana-1.5-v-3b-instruct"
|
| 30 |
+
|
| 31 |
if self.is_local:
|
| 32 |
+
# ๋ก์ปฌ ํ๊ฒฝ: ๋ก์ปฌ ๊ฒฝ๋ก ์ฐ์ , ์์ผ๋ฉด HF์์ ๋ค์ด๋ก๋
|
| 33 |
self.local_path = "./lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 34 |
self.display_name = "kanana-1.5-v-3b-instruct"
|
| 35 |
else:
|
| 36 |
+
# ๋ฐฐํฌ ํ๊ฒฝ: HF ๋ชจ๋ธ๋ช
์ฌ์ฉ, ๋ก์ปฌ ๊ฒฝ๋ก๋ None
|
| 37 |
+
self.local_path = None
|
| 38 |
self.display_name = "kanana-1.5-v-3b-instruct"
|
| 39 |
|
| 40 |
+
# ๐ ๋ชจ๋ธ ID ์ถ๊ฐ (LoRA ๋ฐ ๊ธฐํ ์ค์ ์์ ์ฌ์ฉ)
|
| 41 |
+
self.model_id = "kanana-1.5-v-3b-instruct"
|
| 42 |
+
|
| 43 |
self.description = "์นด์นด์ค ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ (3.6B) - Math RAG ํนํ"
|
| 44 |
self.language = "ko"
|
| 45 |
self.model_size = "3.6B"
|
|
|
|
| 102 |
logger.error(f"โ ํ๊ฒฝ๋ณ์ ๋ก๋ ์คํจ: {e}")
|
| 103 |
|
| 104 |
def load_model(self) -> Tuple[Any, Any]:
|
| 105 |
+
"""๋ชจ๋ธ ๋ก๋ (๋ก์ปฌ/๋ฐฐํฌ ํ๊ฒฝ ๋ชจ๋ ์ง์)"""
|
| 106 |
+
logger.info(f"๐ฅ {self.display_name} ๋ชจ๋ธ ๋ก๋ ์ค...")
|
| 107 |
|
| 108 |
+
# ๐ ํ๊ฒฝ์ ๋ฐ๋ฅธ ๋ชจ๋ธ ๊ฒฝ๋ก ๊ฒฐ์
|
| 109 |
+
if self.is_local and self.local_path:
|
| 110 |
+
# ๋ก์ปฌ ํ๊ฒฝ: ๋ก์ปฌ ๊ฒฝ๋ก ํ์ธ
|
| 111 |
+
absolute_model_path = os.path.abspath(self.local_path)
|
| 112 |
+
use_local = Path(absolute_model_path).exists() and any(Path(absolute_model_path).iterdir())
|
| 113 |
+
model_path = absolute_model_path if use_local else self.model_name
|
| 114 |
+
else:
|
| 115 |
+
# ๋ฐฐํฌ ํ๊ฒฝ: HF ๋ชจ๋ธ๋ช
์ฌ์ฉ
|
| 116 |
+
use_local = False
|
| 117 |
+
model_path = self.model_name
|
| 118 |
|
| 119 |
try:
|
| 120 |
from transformers import AutoModelForVision2Seq, AutoProcessor
|
|
|
|
| 128 |
processor = AutoProcessor.from_pretrained(
|
| 129 |
model_path,
|
| 130 |
trust_remote_code=True,
|
| 131 |
+
local_files_only=use_local,
|
| 132 |
+
use_fast=True # ๐ ๋น ๋ฅธ ์ด๋ฏธ์ง ํ๋ก์ธ์ ์ฌ์ฉ (๊ฒฝ๊ณ ์ ๊ฑฐ)
|
| 133 |
)
|
| 134 |
|
| 135 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 136 |
|
| 137 |
# dtype ์ค์ ์ต์ ํ - CPU์์๋ float32 ์ฌ์ฉ
|
| 138 |
if device == 'cuda':
|
| 139 |
+
selected_dtype = torch.bfloat16 # GPU์์๋ float16์ผ๋ก ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ
|
| 140 |
else:
|
| 141 |
+
selected_dtype = torch.bfloat16 # CPU์์๋ float32๋ก ์์ ์ฑ ํ๋ณด
|
| 142 |
|
| 143 |
logger.info(f"๐ง ์ ํ๋ dtype: {selected_dtype} (device: {device})")
|
| 144 |
|
|
|
|
| 173 |
"top_p": 0.95,
|
| 174 |
"repetition_penalty": 1.1,
|
| 175 |
"no_repeat_ngram_size": 3,
|
| 176 |
+
"pad_token_id": 128001,
|
| 177 |
+
"eos_token_id": 128009,
|
| 178 |
+
"bos_token_id": 128000,
|
| 179 |
+
"use_cache": True,
|
| 180 |
+
# "early_stopping": False,
|
| 181 |
+
# "num_beams": 1,
|
| 182 |
+
# "num_return_sequences": 1,
|
| 183 |
+
# "return_full_text": False,
|
| 184 |
+
# "return_dict": False,
|
| 185 |
+
# "return_dict_in_generate": False,
|
| 186 |
}
|
| 187 |
|
| 188 |
def extract_response(self, full_text: str, formatted_prompt: str = None, **kwargs) -> str:
|
|
|
|
| 238 |
# ์ผ๋ฐ์ ์ธ ํ๋กฌํํธ ํจํด ์ ๊ฑฐ ์๋
|
| 239 |
patterns_to_remove = [
|
| 240 |
"<|im_start|>user\n",
|
| 241 |
+
"<|im_start|>assistant\n",
|
| 242 |
"<|im_end|>",
|
| 243 |
+
"<|im_in_end|>",
|
| 244 |
"<image>",
|
| 245 |
"user\n",
|
| 246 |
"assistant\n"
|
lily_llm_api/models/polyglot_ko_1_3b_chat.py
CHANGED
|
@@ -24,6 +24,10 @@ class PolyglotKo13bChatProfile:
|
|
| 24 |
self.model_name = "heegyu/polyglot-ko-1.3b-chat"
|
| 25 |
self.local_path = "./lily_llm_core/models/polyglot_ko_1_3b_chat"
|
| 26 |
self.display_name = "Polyglot-ko-1.3b-chat"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
self.description = "ํ๊ตญ์ด ์ฑํ
์ ์ฉ ๊ฒฝ๋ ๋ชจ๋ธ (1.3B)"
|
| 28 |
self.language = "ko"
|
| 29 |
self.model_size = "1.3B"
|
|
|
|
| 24 |
self.model_name = "heegyu/polyglot-ko-1.3b-chat"
|
| 25 |
self.local_path = "./lily_llm_core/models/polyglot_ko_1_3b_chat"
|
| 26 |
self.display_name = "Polyglot-ko-1.3b-chat"
|
| 27 |
+
|
| 28 |
+
# ๐ ๋ชจ๋ธ ID ์ถ๊ฐ (LoRA ๋ฐ ๊ธฐํ ์ค์ ์์ ์ฌ์ฉ)
|
| 29 |
+
self.model_id = "polyglot-ko-1.3b-chat"
|
| 30 |
+
|
| 31 |
self.description = "ํ๊ตญ์ด ์ฑํ
์ ์ฉ ๊ฒฝ๋ ๋ชจ๋ธ (1.3B)"
|
| 32 |
self.language = "ko"
|
| 33 |
self.model_size = "1.3B"
|
lily_llm_api/models/polyglot_ko_5_8b_chat.py
CHANGED
|
@@ -21,6 +21,10 @@ class PolyglotKo58bChatProfile:
|
|
| 21 |
self.model_name = "heegyu/polyglot-ko-5.8b-chat"
|
| 22 |
self.local_path = "./lily_llm_core/models/polyglot_ko_5_8b_chat"
|
| 23 |
self.display_name = "Polyglot-ko-5.8b-chat"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
self.description = "ํ๊ตญ์ด ์ฑํ
์ ์ฉ ๊ณ ์ฑ๋ฅ ๋ชจ๋ธ (5.8B)"
|
| 25 |
self.language = "ko"
|
| 26 |
self.model_size = "5.8B"
|
|
@@ -85,7 +89,7 @@ class PolyglotKo58bChatProfile:
|
|
| 85 |
|
| 86 |
# CPU์์๋ float32๊ฐ ๋ ์์ ์ , CUDA์์๋ float16 ์ฌ์ฉ
|
| 87 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 88 |
-
selected_dtype = torch.
|
| 89 |
|
| 90 |
model = AutoModelForCausalLM.from_pretrained(
|
| 91 |
model_path,
|
|
|
|
| 21 |
self.model_name = "heegyu/polyglot-ko-5.8b-chat"
|
| 22 |
self.local_path = "./lily_llm_core/models/polyglot_ko_5_8b_chat"
|
| 23 |
self.display_name = "Polyglot-ko-5.8b-chat"
|
| 24 |
+
|
| 25 |
+
# ๐ ๋ชจ๋ธ ID ์ถ๊ฐ (LoRA ๋ฐ ๊ธฐํ ์ค์ ์์ ์ฌ์ฉ)
|
| 26 |
+
self.model_id = "polyglot-ko-5.8b-chat"
|
| 27 |
+
|
| 28 |
self.description = "ํ๊ตญ์ด ์ฑํ
์ ์ฉ ๊ณ ์ฑ๋ฅ ๋ชจ๋ธ (5.8B)"
|
| 29 |
self.language = "ko"
|
| 30 |
self.model_size = "5.8B"
|
|
|
|
| 89 |
|
| 90 |
# CPU์์๋ float32๊ฐ ๋ ์์ ์ , CUDA์์๋ float16 ์ฌ์ฉ
|
| 91 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 92 |
+
selected_dtype = torch.bfloat16 if device == 'cuda' else torch.bfloat16
|
| 93 |
|
| 94 |
model = AutoModelForCausalLM.from_pretrained(
|
| 95 |
model_path,
|
lily_llm_core/lora_manager.py
CHANGED
|
@@ -156,6 +156,16 @@ class LoRAManager:
|
|
| 156 |
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
| 157 |
device_map="auto" if self.device == "cuda" else None
|
| 158 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
else:
|
| 160 |
raise ValueError(f"์ง์ํ์ง ์๋ ๋ชจ๋ธ ํ์
: {model_type}")
|
| 161 |
|
|
@@ -190,6 +200,9 @@ class LoRAManager:
|
|
| 190 |
# ์ง์ TaskType ์ฌ์ฉ (๋ฌธ์์ด ๋ณํ ์ ๊ฑฐ)
|
| 191 |
if task_type == "CAUSAL_LM":
|
| 192 |
task_type_enum = TaskType.CAUSAL_LM
|
|
|
|
|
|
|
|
|
|
| 193 |
elif task_type == "SEQ_2_SEQ_LM":
|
| 194 |
task_type_enum = TaskType.SEQ_2_SEQ_LM
|
| 195 |
elif task_type == "SEQUENCE_CLASSIFICATION":
|
|
|
|
| 156 |
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
| 157 |
device_map="auto" if self.device == "cuda" else None
|
| 158 |
)
|
| 159 |
+
elif model_type == "vision2seq":
|
| 160 |
+
# ๐ Vision2Seq ๋ชจ๋ธ ์ง์ ์ถ๊ฐ (kanana ๋ฑ)
|
| 161 |
+
from transformers import AutoModelForVision2Seq
|
| 162 |
+
self.base_model = AutoModelForVision2Seq.from_pretrained(
|
| 163 |
+
str(model_path),
|
| 164 |
+
trust_remote_code=True,
|
| 165 |
+
local_files_only=True,
|
| 166 |
+
torch_dtype=torch.bfloat16 if self.device == "cuda" else torch.bfloat16,
|
| 167 |
+
device_map="auto" if self.device == "cuda" else None
|
| 168 |
+
)
|
| 169 |
else:
|
| 170 |
raise ValueError(f"์ง์ํ์ง ์๋ ๋ชจ๋ธ ํ์
: {model_type}")
|
| 171 |
|
|
|
|
| 200 |
# ์ง์ TaskType ์ฌ์ฉ (๋ฌธ์์ด ๋ณํ ์ ๊ฑฐ)
|
| 201 |
if task_type == "CAUSAL_LM":
|
| 202 |
task_type_enum = TaskType.CAUSAL_LM
|
| 203 |
+
elif task_type == "VISION_2_SEQ":
|
| 204 |
+
# ๐ Vision2Seq ๋ชจ๋ธ ์ง์ ์ถ๊ฐ
|
| 205 |
+
task_type_enum = TaskType.SEQ_2_SEQ_LM # Vision2Seq๋ SEQ_2_SEQ_LM๊ณผ ์ ์ฌ
|
| 206 |
elif task_type == "SEQ_2_SEQ_LM":
|
| 207 |
task_type_enum = TaskType.SEQ_2_SEQ_LM
|
| 208 |
elif task_type == "SEQUENCE_CLASSIFICATION":
|
lily_llm_core/rag_processor.py
CHANGED
|
@@ -245,7 +245,7 @@ class RAGProcessor:
|
|
| 245 |
"context": "",
|
| 246 |
"sources": []
|
| 247 |
}
|
| 248 |
-
|
| 249 |
def _generate_text_response(self, query: str, text_docs: List[Document],
|
| 250 |
llm_model, image_files: List[str]) -> Dict[str, Any]:
|
| 251 |
"""ํ
์คํธ ๊ธฐ๋ฐ ์๋ต ์์ฑ"""
|
|
@@ -255,8 +255,8 @@ class RAGProcessor:
|
|
| 255 |
|
| 256 |
# ํ๋กฌํํธ ์์ฑ
|
| 257 |
prompt = f"""
|
| 258 |
-
|
| 259 |
-
|
| 260 |
์ฐธ๊ณ ๋ฌธ์:
|
| 261 |
{text_context}
|
| 262 |
|
|
@@ -397,7 +397,7 @@ class RAGProcessor:
|
|
| 397 |
"document_id": document_id,
|
| 398 |
"error": str(e)
|
| 399 |
}
|
| 400 |
-
|
| 401 |
def get_performance_stats(self) -> Dict[str, Any]:
|
| 402 |
"""์ฑ๋ฅ ํต๊ณ ๋ฐํ"""
|
| 403 |
try:
|
|
|
|
| 245 |
"context": "",
|
| 246 |
"sources": []
|
| 247 |
}
|
| 248 |
+
|
| 249 |
def _generate_text_response(self, query: str, text_docs: List[Document],
|
| 250 |
llm_model, image_files: List[str]) -> Dict[str, Any]:
|
| 251 |
"""ํ
์คํธ ๊ธฐ๋ฐ ์๋ต ์์ฑ"""
|
|
|
|
| 255 |
|
| 256 |
# ํ๋กฌํํธ ์์ฑ
|
| 257 |
prompt = f"""
|
| 258 |
+
์ง๋ฌธ: {query}
|
| 259 |
+
|
| 260 |
์ฐธ๊ณ ๋ฌธ์:
|
| 261 |
{text_context}
|
| 262 |
|
|
|
|
| 397 |
"document_id": document_id,
|
| 398 |
"error": str(e)
|
| 399 |
}
|
| 400 |
+
|
| 401 |
def get_performance_stats(self) -> Dict[str, Any]:
|
| 402 |
"""์ฑ๋ฅ ํต๊ณ ๋ฐํ"""
|
| 403 |
try:
|
test_lora_integration.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
LoRA ํตํฉ ๋ฐ ๋ชจ๋ธ ํ์
์ง์ ํ
์คํธ ์คํฌ๋ฆฝํธ
|
| 4 |
+
"""
|
| 5 |
+
import sys
|
| 6 |
+
import os
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
# ํ๋ก์ ํธ ๋ฃจํธ ๊ฒฝ๋ก ์ถ๊ฐ
|
| 10 |
+
project_root = Path(__file__).parent
|
| 11 |
+
sys.path.insert(0, str(project_root))
|
| 12 |
+
|
| 13 |
+
def test_lora_integration():
|
| 14 |
+
"""LoRA ํตํฉ ํ
์คํธ"""
|
| 15 |
+
print("๐ LoRA ํตํฉ ํ
์คํธ ์์...")
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
from lily_llm_api.models import get_model_profile, list_available_models
|
| 19 |
+
|
| 20 |
+
available_models = list_available_models()
|
| 21 |
+
print(f"๐ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ธ: {len(available_models)}๊ฐ")
|
| 22 |
+
|
| 23 |
+
for model_info in available_models:
|
| 24 |
+
model_id = model_info['model_id']
|
| 25 |
+
print(f"\n๐ ๋ชจ๋ธ: {model_info['name']} ({model_id})")
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
profile = get_model_profile(model_id)
|
| 29 |
+
print(f" โ
ํ๋กํ ๋ก๋ ์ฑ๊ณต")
|
| 30 |
+
print(f" - display_name: {getattr(profile, 'display_name', 'N/A')}")
|
| 31 |
+
print(f" - model_id: {getattr(profile, 'model_id', 'N/A')}")
|
| 32 |
+
print(f" - local_path: {getattr(profile, 'local_path', 'N/A')}")
|
| 33 |
+
print(f" - is_local: {getattr(profile, 'is_local', 'N/A')}")
|
| 34 |
+
print(f" - multimodal: {getattr(profile, 'multimodal', 'N/A')}")
|
| 35 |
+
|
| 36 |
+
# LoRA ๊ฒฝ๋ก ์๋ฎฌ๋ ์ด์
|
| 37 |
+
print(f" ๐ LoRA ๊ฒฝ๋ก ์๋ฎฌ๋ ์ด์
:")
|
| 38 |
+
|
| 39 |
+
if hasattr(profile, 'local_path') and profile.local_path:
|
| 40 |
+
current_model_path = profile.local_path
|
| 41 |
+
print(f" - ๋ก์ปฌ ๊ฒฝ๋ก ์ง์ ์ฌ์ฉ: {current_model_path}")
|
| 42 |
+
elif hasattr(profile, 'model_id') and profile.model_id:
|
| 43 |
+
model_id = profile.model_id
|
| 44 |
+
print(f" - ๋ชจ๋ธ ID ๊ธฐ๋ฐ: {model_id}")
|
| 45 |
+
|
| 46 |
+
if hasattr(profile, 'is_local') and profile.is_local:
|
| 47 |
+
# ๋ก์ปฌ ํ๊ฒฝ ์๋ฎฌ๋ ์ด์
|
| 48 |
+
if model_id == "polyglot-ko-1.3b-chat":
|
| 49 |
+
current_model_path = "./lily_llm_core/models/polyglot_ko_1_3b_chat"
|
| 50 |
+
model_type = "causal_lm"
|
| 51 |
+
elif model_id == "kanana-1.5-v-3b-instruct":
|
| 52 |
+
current_model_path = "./lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 53 |
+
model_type = "vision2seq"
|
| 54 |
+
elif model_id == "polyglot-ko-5.8b-chat":
|
| 55 |
+
current_model_path = "./lily_llm_core/models/polyglot_ko_5_8b_chat"
|
| 56 |
+
model_type = "causal_lm"
|
| 57 |
+
|
| 58 |
+
print(f" - ๋ก์ปฌ ํ๊ฒฝ ๊ฒฝ๋ก: {current_model_path}")
|
| 59 |
+
print(f" - ๋ชจ๋ธ ํ์
: {model_type}")
|
| 60 |
+
else:
|
| 61 |
+
print(f" - ๋ฐฐํฌ ํ๊ฒฝ: LoRA ์ค์ ๊ฑด๋๋")
|
| 62 |
+
current_model_path = None
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print(f" โ ํ๋กํ ๋ก๋ ์คํจ: {e}")
|
| 66 |
+
|
| 67 |
+
print("\n" + "="*50)
|
| 68 |
+
print("๐ฏ ๊ณตํต LoRA ์ค์ ํจ์ ํ
์คํธ")
|
| 69 |
+
print("="*50)
|
| 70 |
+
|
| 71 |
+
# ๊ณตํต ํจ์ ํ
์คํธ
|
| 72 |
+
try:
|
| 73 |
+
from lily_llm_api.app_v2 import setup_lora_for_model
|
| 74 |
+
print("โ
๊ณตํต LoRA ์ค์ ํจ์ import ์ฑ๊ณต")
|
| 75 |
+
|
| 76 |
+
# ์ฒซ ๋ฒ์งธ ๋ชจ๋ธ๋ก ํ
์คํธ
|
| 77 |
+
if available_models:
|
| 78 |
+
test_model_id = available_models[0]['model_id']
|
| 79 |
+
test_profile = get_model_profile(test_model_id)
|
| 80 |
+
print(f"๐ ํ
์คํธ ๋ชจ๋ธ: {test_profile.display_name}")
|
| 81 |
+
|
| 82 |
+
# LoRA ๋งค๋์ ๊ฐ ์๋ ์ํ์์ ํ
์คํธ
|
| 83 |
+
result = setup_lora_for_model(test_profile, None)
|
| 84 |
+
print(f"๐ LoRA ๋งค๋์ ์์ ํ
์คํธ ๊ฒฐ๊ณผ: {result}")
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"โ ๊ณตํต ํจ์ ํ
์คํธ ์คํจ: {e}")
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"โ ์ ์ฒด ํ
์คํธ ์คํจ: {e}")
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
test_lora_integration()
|
test_model_selection.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
๋ชจ๋ธ ์ ํ ๋ฐ LoRA ๊ฒฝ๋ก ์ค์ ํ
์คํธ ์คํฌ๋ฆฝํธ
|
| 4 |
+
"""
|
| 5 |
+
import sys
|
| 6 |
+
import os
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
# ํ๋ก์ ํธ ๋ฃจํธ ๊ฒฝ๋ก ์ถ๊ฐ
|
| 10 |
+
project_root = Path(__file__).parent
|
| 11 |
+
sys.path.insert(0, str(project_root))
|
| 12 |
+
|
| 13 |
+
from lily_llm_api.models import get_model_profile, list_available_models
|
| 14 |
+
|
| 15 |
+
def test_model_profiles():
|
| 16 |
+
"""๋ชจ๋ธ ํ๋กํ๋ค์ด ์ฌ๋ฐ๋ฅธ ์์ฑ์ ๊ฐ์ง๊ณ ์๋์ง ํ
์คํธ"""
|
| 17 |
+
print("๐ ๋ชจ๋ธ ํ๋กํ ํ
์คํธ ์์...")
|
| 18 |
+
|
| 19 |
+
available_models = list_available_models()
|
| 20 |
+
print(f"๐ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ธ: {len(available_models)}๊ฐ")
|
| 21 |
+
|
| 22 |
+
for model_info in available_models:
|
| 23 |
+
model_id = model_info['model_id']
|
| 24 |
+
print(f"\n๐ ๋ชจ๋ธ: {model_info['name']} ({model_id})")
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
profile = get_model_profile(model_id)
|
| 28 |
+
print(f" โ
ํ๋กํ ๋ก๋ ์ฑ๊ณต")
|
| 29 |
+
print(f" - display_name: {getattr(profile, 'display_name', 'N/A')}")
|
| 30 |
+
print(f" - model_id: {getattr(profile, 'model_id', 'N/A')}")
|
| 31 |
+
print(f" - local_path: {getattr(profile, 'local_path', 'N/A')}")
|
| 32 |
+
print(f" - multimodal: {getattr(profile, 'multimodal', 'N/A')}")
|
| 33 |
+
|
| 34 |
+
# ํ์ ์์ฑ ํ์ธ
|
| 35 |
+
required_attrs = ['model_id', 'local_path', 'display_name']
|
| 36 |
+
missing_attrs = [attr for attr in required_attrs if not hasattr(profile, attr)]
|
| 37 |
+
|
| 38 |
+
if missing_attrs:
|
| 39 |
+
print(f" โ ๋๋ฝ๋ ์์ฑ: {missing_attrs}")
|
| 40 |
+
else:
|
| 41 |
+
print(f" โ
๋ชจ๋ ํ์ ์์ฑ ์กด์ฌ")
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f" โ ํ๋กํ ๋ก๋ ์คํจ: {e}")
|
| 45 |
+
|
| 46 |
+
print("\n" + "="*50)
|
| 47 |
+
print("๐ฏ ๋ชจ๋ธ ์ ํ ์๋ฎฌ๋ ์ด์
")
|
| 48 |
+
print("="*50)
|
| 49 |
+
|
| 50 |
+
# ๋ชจ๋ธ ์ ํ ์๋ฎฌ๋ ์ด์
|
| 51 |
+
for i, model_info in enumerate(available_models, 1):
|
| 52 |
+
print(f"{i:2d}. {model_info['name']} ({model_info['model_id']})")
|
| 53 |
+
|
| 54 |
+
# ์ฒซ ๋ฒ์งธ ๋ชจ๋ธ ์ ํ ์๋ฎฌ๋ ์ด์
|
| 55 |
+
if available_models:
|
| 56 |
+
selected_model = available_models[0]
|
| 57 |
+
selected_model_id = selected_model['model_id']
|
| 58 |
+
print(f"\n๐ ์ ํ๋ ๋ชจ๋ธ: {selected_model['name']} ({selected_model_id})")
|
| 59 |
+
|
| 60 |
+
# LoRA ๊ฒฝ๋ก ๊ฒฐ์ ์๋ฎฌ๋ ์ด์
|
| 61 |
+
profile = get_model_profile(selected_model_id)
|
| 62 |
+
current_model_path = None
|
| 63 |
+
|
| 64 |
+
if hasattr(profile, 'local_path') and profile.local_path:
|
| 65 |
+
current_model_path = profile.local_path
|
| 66 |
+
print(f"๐ ๋ชจ๋ธ ํ๋กํ์์ ๊ฒฝ๋ก ์ง์ ์ฌ์ฉ: {current_model_path}")
|
| 67 |
+
elif hasattr(profile, 'model_id') and profile.model_id:
|
| 68 |
+
model_id = profile.model_id
|
| 69 |
+
print(f"๐ ๋ชจ๋ธ ID ๊ธฐ๋ฐ ๊ฒฝ๋ก ๊ฒฐ์ : {model_id}")
|
| 70 |
+
|
| 71 |
+
if model_id == "polyglot-ko-1.3b-chat":
|
| 72 |
+
current_model_path = "./lily_llm_core/models/polyglot_ko_1_3b_chat"
|
| 73 |
+
elif model_id == "kanana-1.5-v-3b-instruct":
|
| 74 |
+
current_model_path = "./lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 75 |
+
elif model_id == "polyglot-ko-5.8b-chat":
|
| 76 |
+
current_model_path = "./lily_llm_core/models/polyglot_ko_5_8b_chat"
|
| 77 |
+
|
| 78 |
+
print(f"๐ ๊ฒฐ์ ๋ ๋ชจ๋ธ ๊ฒฝ๋ก: {current_model_path}")
|
| 79 |
+
|
| 80 |
+
if current_model_path:
|
| 81 |
+
print(f"โ
LoRA ๊ฒฝ๋ก ๊ฒฐ์ ์ฑ๊ณต: {current_model_path}")
|
| 82 |
+
else:
|
| 83 |
+
print(f"โ LoRA ๊ฒฝ๋ก ๊ฒฐ์ ์คํจ")
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
test_model_profiles()
|
test_model_type_fix.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
๋ชจ๋ธ ํ์
์ค์ ํ
์คํธ ์คํฌ๋ฆฝํธ
|
| 4 |
+
"""
|
| 5 |
+
import sys
|
| 6 |
+
import os
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
# ํ๋ก์ ํธ ๋ฃจํธ ๊ฒฝ๋ก ์ถ๊ฐ
|
| 10 |
+
project_root = Path(__file__).parent
|
| 11 |
+
sys.path.insert(0, str(project_root))
|
| 12 |
+
|
| 13 |
+
def test_model_type_detection():
|
| 14 |
+
"""๋ชจ๋ธ ํ์
๊ฐ์ง ํ
์คํธ"""
|
| 15 |
+
print("๐ ๋ชจ๋ธ ํ์
๊ฐ์ง ํ
์คํธ ์์...")
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
from lily_llm_api.models import get_model_profile, list_available_models
|
| 19 |
+
|
| 20 |
+
available_models = list_available_models()
|
| 21 |
+
print(f"๐ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ธ: {len(available_models)}๊ฐ")
|
| 22 |
+
|
| 23 |
+
for model_info in available_models:
|
| 24 |
+
model_id = model_info['model_id']
|
| 25 |
+
print(f"\n๐ ๋ชจ๋ธ: {model_info['name']} ({model_id})")
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
profile = get_model_profile(model_id)
|
| 29 |
+
print(f" โ
ํ๋กํ ๋ก๋ ์ฑ๊ณต")
|
| 30 |
+
print(f" - display_name: {getattr(profile, 'display_name', 'N/A')}")
|
| 31 |
+
print(f" - model_id: {getattr(profile, 'model_id', 'N/A')}")
|
| 32 |
+
print(f" - local_path: {getattr(profile, 'local_path', 'N/A')}")
|
| 33 |
+
print(f" - is_local: {getattr(profile, 'is_local', 'N/A')}")
|
| 34 |
+
|
| 35 |
+
# ๐ ๋ชจ๋ธ ํ์
๊ฐ์ง ์๋ฎฌ๋ ์ด์
|
| 36 |
+
print(f" ๐ ๋ชจ๋ธ ํ์
๊ฐ์ง ์๋ฎฌ๋ ์ด์
:")
|
| 37 |
+
|
| 38 |
+
current_model_path = None
|
| 39 |
+
model_type = "causal_lm" # ๊ธฐ๋ณธ๊ฐ
|
| 40 |
+
|
| 41 |
+
if hasattr(profile, 'local_path') and profile.local_path:
|
| 42 |
+
# ๋ก์ปฌ ํ๊ฒฝ: ๋ก์ปฌ ๊ฒฝ๋ก ์ฌ์ฉ
|
| 43 |
+
current_model_path = profile.local_path
|
| 44 |
+
# ๐ local_path ์ฌ์ฉ ์์๋ model_type ์ค์ ํ์
|
| 45 |
+
if hasattr(profile, 'model_id') and profile.model_id:
|
| 46 |
+
model_id = profile.model_id
|
| 47 |
+
if model_id == "kanana-1.5-v-3b-instruct":
|
| 48 |
+
model_type = "vision2seq" # ๐ kanana๋ vision2seq ํ์
|
| 49 |
+
else:
|
| 50 |
+
model_type = "causal_lm" # ๊ธฐ๋ณธ๊ฐ
|
| 51 |
+
print(f" - ๋ก์ปฌ ๊ฒฝ๋ก ์ฌ์ฉ: {current_model_path}")
|
| 52 |
+
print(f" - ๊ฒฐ์ ๋ ๋ชจ๋ธ ํ์
: {model_type}")
|
| 53 |
+
|
| 54 |
+
elif hasattr(profile, 'model_id') and profile.model_id:
|
| 55 |
+
# ๋ชจ๋ธ ID๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ฒฝ๋ก ๊ฒฐ์
|
| 56 |
+
model_id = profile.model_id
|
| 57 |
+
print(f" - ๋ชจ๋ธ ID ๊ธฐ๋ฐ: {model_id}")
|
| 58 |
+
|
| 59 |
+
if hasattr(profile, 'is_local') and profile.is_local:
|
| 60 |
+
# ๋ก์ปฌ ํ๊ฒฝ: ๋ก์ปฌ ๊ฒฝ๋ก ์ฌ์ฉ
|
| 61 |
+
if model_id == "polyglot-ko-1.3b-chat":
|
| 62 |
+
current_model_path = "./lily_llm_core/models/polyglot_ko_1_3b_chat"
|
| 63 |
+
model_type = "causal_lm"
|
| 64 |
+
elif model_id == "kanana-1.5-v-3b-instruct":
|
| 65 |
+
current_model_path = "./lily_llm_core/models/kanana_1_5_v_3b_instruct"
|
| 66 |
+
model_type = "vision2seq"
|
| 67 |
+
elif model_id == "polyglot-ko-5.8b-chat":
|
| 68 |
+
current_model_path = "./lily_llm_core/models/polyglot_ko_5_8b_chat"
|
| 69 |
+
model_type = "causal_lm"
|
| 70 |
+
|
| 71 |
+
print(f" - ๋ก์ปฌ ํ๊ฒฝ ๊ฒฝ๋ก: {current_model_path}")
|
| 72 |
+
print(f" - ๋ชจ๋ธ ํ์
: {model_type}")
|
| 73 |
+
else:
|
| 74 |
+
print(f" - ๋ฐฐํฌ ํ๊ฒฝ: LoRA ์ค์ ๊ฑด๋๋")
|
| 75 |
+
current_model_path = None
|
| 76 |
+
|
| 77 |
+
# ์ต์ข
๊ฒฐ๊ณผ
|
| 78 |
+
if current_model_path:
|
| 79 |
+
print(f" โ
์ต์ข
๊ฒฐ๊ณผ: ๊ฒฝ๋ก={current_model_path}, ํ์
={model_type}")
|
| 80 |
+
else:
|
| 81 |
+
print(f" โ ์ต์ข
๊ฒฐ๊ณผ: ๊ฒฝ๋ก ์์")
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f" โ ํ๋กํ ๋ก๋ ์คํจ: {e}")
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"โ ์ ์ฒด ํ
์คํธ ์คํจ: {e}")
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
test_model_type_detection()
|
test_rag_integration.py
CHANGED
|
@@ -267,3 +267,4 @@ if __name__ == "__main__":
|
|
| 267 |
print("\nํ
์คํธ ์๋ฃ! ๐")
|
| 268 |
|
| 269 |
|
|
|
|
|
|
| 267 |
print("\nํ
์คํธ ์๋ฃ! ๐")
|
| 268 |
|
| 269 |
|
| 270 |
+
|