lily_fast_api / fix_kanana_target_modules.py
gbrabbit's picture
Auto commit at 22-2025-08 16:26:25
149ff1e
#!/usr/bin/env python3
"""
Kanana ๋ชจ๋ธ์˜ ์ •ํ™•ํ•œ target modules ํŒจํ„ด ์ฐพ๊ธฐ
"""
import sys
import os
from pathlib import Path
# ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ๊ฒฝ๋กœ ์ถ”๊ฐ€
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
def find_exact_target_modules():
"""์ •ํ™•ํ•œ target modules ํŒจํ„ด ์ฐพ๊ธฐ"""
print("๐Ÿ” Kanana ๋ชจ๋ธ์˜ ์ •ํ™•ํ•œ target modules ํŒจํ„ด ์ฐพ๊ธฐ...")
try:
import torch
from transformers import AutoModelForVision2Seq
model_path = "./lily_llm_core/models/kanana_1_5_v_3b_instruct"
print(f"๐Ÿ“ฅ ๋ชจ๋ธ ๋กœ๋”ฉ ์ค‘: {model_path}")
# ๋ชจ๋ธ ๋กœ๋“œ
model = AutoModelForVision2Seq.from_pretrained(
model_path,
trust_remote_code=True,
local_files_only=True,
torch_dtype=torch.bfloat16
)
print(f"โœ… ๋ชจ๋ธ ๋กœ๋“œ ์„ฑ๊ณต!")
# language_model ๋ถ€๋ถ„์˜ ์ •ํ™•ํ•œ ๋ชจ๋“ˆ ์ด๋ฆ„ ์ฐพ๊ธฐ
print("\n๐ŸŽฏ Language Model ๋ชจ๋“ˆ ๊ฒ€์ƒ‰:")
target_candidates = []
for name, module in model.named_modules():
# language_model ๋ถ€๋ถ„๋งŒ ํ•„ํ„ฐ๋ง
if name.startswith("language_model.model.layers."):
if hasattr(module, 'weight') and module.weight is not None:
module_type = type(module).__name__
# LoRA์— ์ ํ•ฉํ•œ ๋ชจ๋“ˆ๋“ค ์ฐพ๊ธฐ
if any(pattern in name for pattern in ['q_proj', 'k_proj', 'v_proj', 'o_proj']):
target_candidates.append((name, module_type, "Attention"))
elif any(pattern in name for pattern in ['gate_proj', 'up_proj', 'down_proj']):
target_candidates.append((name, module_type, "MLP"))
# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
if target_candidates:
print(" โœ… ๋ฐœ๊ฒฌ๋œ target modules:")
for name, module_type, category in target_candidates:
print(f" - {name} ({module_type}) - {category}")
# ์‹ค์ œ ์‚ฌ์šฉํ•  target modules ์ถ”์ถœ
print("\n๐Ÿ“‹ ์‹ค์ œ ์‚ฌ์šฉํ•  target modules:")
target_modules = []
for name, _, _ in target_candidates:
target_modules.append(name)
print(f" '{name}',")
print(f"\n๐Ÿ”ข ์ด {len(target_modules)}๊ฐœ์˜ target modules ๋ฐœ๊ฒฌ")
else:
print(" โŒ language_model์—์„œ target modules๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ")
# ๋ชจ๋ธ ํ•ด์ œ
del model
import gc
gc.collect()
print("\nโœ… target modules ๊ฒ€์ƒ‰ ์™„๋ฃŒ!")
except Exception as e:
print(f"โŒ target modules ๊ฒ€์ƒ‰ ์‹คํŒจ: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
find_exact_target_modules()