MADLAD400-3B-to-coreml-python / madlad400_fp16_flash.py
aoiandroid's picture
Add files using upload-large-folder tool
9cd4efe verified
Raw History Blame Contribute Delete
3.79 kB
"""
MADLAD400-3B-MT: FP16 + Flash Attention 2
็ฒพๅบฆๆœ€ๅ„ชๅ…ˆใƒป้ซ˜้€ŸๅŒ–ใ‚นใ‚ฏใƒชใƒ—ใƒˆ
ๅฟ…่ฆใชใƒ‘ใƒƒใ‚ฑใƒผใ‚ธ:
pip install transformers torch accelerate sentencepiece
pip install flash-attn --no-build-isolation # Flash Attention 2๏ผˆ่ฆCUDA๏ผ‰
ๅ‹•ไฝœ็ขบ่ช็’ฐๅขƒ:
RTX 4070 Ti (12GB VRAM), Python 3.10+, CUDA 11.8+
"""
import time
import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
MODEL_ID = "google/madlad400-3b-mt"
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 1. ใƒขใƒ‡ใƒซใƒปใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎใƒญใƒผใƒ‰
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
print("Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
print("Loading model (FP16 + Flash Attention 2)...")
model = AutoModelForSeq2SeqLM.from_pretrained(
MODEL_ID,
torch_dtype=torch.float16, # FP16ใง่ชญใฟ่พผใฟ๏ผˆVRAM็ด„6GB๏ผ‰
device_map="auto", # GPU่‡ชๅ‹•ๅ‰ฒใ‚Šๅฝ“ใฆ
attn_implementation="flash_attention_2", # Flash Attention 2ใ‚’ๆœ‰ๅŠนๅŒ–
)
model.eval()
print(f"Device: {next(model.parameters()).device}")
print(f"Dtype: {next(model.parameters()).dtype}")
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 2. ็ฟป่จณ้–ขๆ•ฐ
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def translate(text: str, target_lang: str = "ja", max_new_tokens: int = 256) -> str:
"""
target_lang: ISO 639-1ใ‚ณใƒผใƒ‰
ไพ‹: "ja"=ๆ—ฅๆœฌ่ชž, "en"=่‹ฑ่ชž, "zh"=ไธญๅ›ฝ่ชž, "ko"=้Ÿ“ๅ›ฝ่ชž, "fr"=ใƒ•ใƒฉใƒณใ‚น่ชž
"""
# MADLAD400ใฏใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚นใง่จ€่ชžใ‚’ๆŒ‡ๅฎšใ™ใ‚‹
input_text = f"<2{target_lang}> {text}"
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
num_beams=4, # ใƒ“ใƒผใƒ ใ‚ตใƒผใƒ๏ผˆ็ฒพๅบฆ้‡่ฆ–๏ผ‰
early_stopping=True,
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 3. ใƒ™ใƒณใƒใƒžใƒผใ‚ฏ๏ผˆ้€Ÿๅบฆใƒป็ฒพๅบฆ็ขบ่ช๏ผ‰
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
test_cases = [
("Hello, how are you?", "ja"),
("The weather is beautiful today.", "ja"),
("Machine learning is transforming industries.", "ja"),
("ใ“ใ‚“ใซใกใฏใ€ๅ…ƒๆฐ—ใงใ™ใ‹๏ผŸ", "en"),
]
print("\n" + "="*60)
print("FP16 + Flash Attention 2 ใƒ™ใƒณใƒใƒžใƒผใ‚ฏ")
print("="*60)
total_time = 0.0
for src, tgt in test_cases:
start = time.perf_counter()
result = translate(src, target_lang=tgt)
elapsed = time.perf_counter() - start
total_time += elapsed
print(f"\n[{tgt}] {src}")
print(f" โ†’ {result}")
print(f" โฑ {elapsed:.3f}s")
print(f"\nๅนณๅ‡็ฟป่จณๆ™‚้–“: {total_time / len(test_cases):.3f}s")
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 4. VRAMใฎไฝฟ็”จ้‡็ขบ่ช
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
if torch.cuda.is_available():
allocated = torch.cuda.memory_allocated() / 1e9
reserved = torch.cuda.memory_reserved() / 1e9
print(f"\nVRAMไฝฟ็”จ้‡: {allocated:.2f} GB allocated / {reserved:.2f} GB reserved")