Download madlad400_fp16_flash.py from aoiandroid/MADLAD400-3B-to-coreml-python: direct link, hf CLI and curl.
- Browser
- Download file 3.79 kB
-
https://huggingface.co/aoiandroid/MADLAD400-3B-to-coreml-python/resolve/main/madlad400_fp16_flash.py
- Command line
-
hf download hf://aoiandroid/MADLAD400-3B-to-coreml-python/madlad400_fp16_flash.py
-
curl -L -o madlad400_fp16_flash.py https://huggingface.co/aoiandroid/MADLAD400-3B-to-coreml-python/resolve/main/madlad400_fp16_flash.py
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") | |