Add files using upload-large-folder tool
Browse filesFiles changed (3)
- README_madlad400_setup.mdAdded+85
- madlad400_awq_int8.pyAdded+152
- madlad400_fp16_flash.pyAdded+93
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485# MADLAD400-3B-MT ๆ้ฉๅใปใใใขใใใฌใคใ
## ๅไฝ็ฐๅข- Windows 11, Python 3.10+- RTX 4070 Ti (12GB VRAM)- CUDA 11.8 ไปฅไธ
---
## ใปใใใขใใๆ้
### Step 1: ไปฎๆณ็ฐๅขใไฝใ๏ผๆจๅฅจ๏ผ```batpython -m venv madlad_envmadlad_env\Scripts\activate```
### Step 2: ๅ
ฑ้ใใใฑใผใธใฎใคใณในใใผใซ```batpip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118pip install transformers accelerate sentencepiece```
### Step 3a: FP16 + Flash Attention 2 ใไฝฟใๅ ดๅ```batpip install flash-attn --no-build-isolationpython madlad400_fp16_flash.py```
> โ ๏ธ flash-attnใฎใใซใใซใฏๆฐๅใใใใพใ๏ผVisual C++ Build Toolsใๅฟ
่ฆ๏ผ> ใคใณในใใผใซใ้ฃใใๅ ดๅใฏ attn_implementation="flash_attention_2" ใฎ่กใๅ้คใใฆใFP16ใฏๅไฝใใพใ
### Step 3b: AWQ INT8 ใไฝฟใๅ ดๅ```batpip install autoawqpython madlad400_awq_int8.py```
> โ ๏ธ ๅๅๅฎ่กๆใฏใญใฃใชใใฌใผใทใงใณใฎใใ20ใ60ๅใใใใพใ> 2ๅ็ฎไปฅ้ใฏไฟๅญๆธใฟใขใใซใ่ชๅใงใญใผใใใพใ
---
## VRAMไฝฟ็จ้ใฎ็ฎๅฎ
| ๆๆณ | VRAM | ็ฒพๅบฆ | ้ๅบฆ ||---------------|----------|----------|------------|| FP32๏ผๅ
๏ผ | ~12 GB | 100% | baseline || FP16 | ~6 GB | ~100% | 1.5-2x || FP16+FlashAttn| ~5.5 GB | ~100% | 2-3x || AWQ INT8 | ~3.5 GB | ~98-99% | 2-3x |
---
## ใใฉใใซใทใฅใผใใฃใณใฐ
**flash-attnใใคใณในใใผใซใงใใชใๅ ดๅ**- `madlad400_fp16_flash.py` ใฎ `attn_implementation="flash_attention_2"` ใฎ่กใๅ้ค- FP16ใฎใฟใงใๅๅใช้ซ้ๅใซใชใใพใ
**OOM๏ผOut of Memory๏ผใจใฉใผใๅบใๅ ดๅ**- `num_beams=4` ใ `num_beams=1` ใซๅคๆด๏ผใฐใชใผใใฃใใณใผใ๏ผ- `max_new_tokens` ใๅฐใใใใ
**autoawqใSeq2Seqใขใใซใใตใใผใใใฆใใชใใจใใใจใฉใผใใงใๅ ดๅ**- `pip install autoawq --upgrade` ใงๆๆฐ็ใ่ฉฆใ- ใพใใฏ FP16 + Flash Attention 2 ใฎๆนใไฝฟใ๏ผ็ฒพๅบฆใฏใปใผๅใ๏ผ
---
## ๅฏพๅฟ่จ่ชใณใผใไพ
| ใณใผใ | ่จ่ช ||--------|------------|| ja | ๆฅๆฌ่ช || en | ่ฑ่ช || zh | ไธญๅฝ่ช || ko | ้ๅฝ่ช || fr | ใใฉใณใน่ช || de | ใใคใ่ช || es | ในใใคใณ่ช || ar | ใขใฉใใข่ช |
MADLAD400ใฏ400่จ่ชไปฅไธใซๅฏพๅฟใใฆใใพใใ่จ่ชใณใผใใฎๅฎๅ
จใชในใ: https://huggingface.co/google/madlad400-3b-mt123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152"""MADLAD400-3B-MT: AWQ INT8 ้ๅญๅใใฉใณในๅใป็ฒพๅบฆใไฟใกใชใใVRAMๅๆธในใฏใชใใ
ๅฟ
่ฆใชใใใฑใผใธ: pip install transformers torch accelerate sentencepiece pip install autoawq # AWQ้ๅญๅใฉใคใใฉใช
ๆณจๆ: - ๅๅๅฎ่กๆใซใญใฃใชใใฌใผใทใงใณใ่ตฐใใๆฐๅๅใใใๅ ดๅใใใใพใ - ้ๅญๅๆธใฟใขใใซใฏ madlad400-3b-awq-int8/ ใซไฟๅญใใใพใ - 2ๅ็ฎไปฅ้ใฏไฟๅญๆธใฟใขใใซใ็ดๆฅใญใผใใใฆใใ ใใ"""
import timeimport torchfrom pathlib import Path
MODEL_ID = "google/madlad400-3b-mt"SAVE_PATH = "./madlad400-3b-awq-int8"
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# 1. ้ๅญๅ่จญๅฎ# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโQUANT_CONFIG = { "zero_point": True, # ใผใญ็น่ฃๆญฃใใ๏ผ็ฒพๅบฆๅไธ๏ผ "q_group_size": 128, # ใฐใซใผใใตใคใบ๏ผๅฐใใใปใฉ็ฒพๅบฆโใ้ๅบฆโ๏ผ "w_bit": 8, # INT8 ๏ผINT4ใซใใใจใใใซ่ปฝ้ใ ใ็ฒพๅบฆไฝไธ๏ผ "version": "GEMM", # GEMM or GEMV๏ผใใใๅฆ็ใซใฏGEMMๆจๅฅจ๏ผ}
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# 2. ้ๅญๅๅฎ่ก or ไฟๅญๆธใฟใขใใซใใญใผใ# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโif Path(SAVE_PATH).exists(): # โโ ไฟๅญๆธใฟใขใใซใใญใผใ๏ผ2ๅ็ฎไปฅ้๏ผโโ print(f"Loading quantized model from {SAVE_PATH}...") from awq import AutoAWQForSeq2SeqLM from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(SAVE_PATH) model = AutoAWQForSeq2SeqLM.from_quantized( SAVE_PATH, fuse_layers=True, # ใฌใคใคใผfusionใงๆจ่ซ้ซ้ๅ device_map="auto", )
else: # โโ ๅๅ๏ผ้ๅญๅใใฆใใไฟๅญ โโ print("First run: quantizing model (this may take 20-60 min)...") from awq import AutoAWQForSeq2SeqLM from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) model = AutoAWQForSeq2SeqLM.from_pretrained( MODEL_ID, device_map="auto", safetensors=True, )
# ใญใฃใชใใฌใผใทใงใณใใผใฟ๏ผ็ฟป่จณใฟในใฏใซๅใใใใตใณใใซ๏ผ calib_data = [ "<2ja> Hello, this is a test sentence for calibration.", "<2ja> Machine translation systems have improved significantly.", "<2en> ใใใฏใญใฃใชใใฌใผใทใงใณ็จใฎใตใณใใซใใญในใใงใใ", "<2ja> The model needs diverse examples to calibrate properly.", "<2zh> Natural language processing is a key area of AI research.", "<2ko> Quantization reduces memory usage while preserving accuracy.", "<2ja> Large language models require substantial computational resources.", "<2en> ๅค่จ่ช็ฟป่จณใขใใซใฏ่คๆฐใฎ่จ่ชใๅๆใซๅฆ็ใงใใพใใ", "<2fr> This sentence will help calibrate the quantization process.", "<2ja> Flash attention reduces memory complexity from quadratic to linear.", ]
print("Running quantization with calibration data...") model.quantize( tokenizer, quant_config=QUANT_CONFIG, calib_data=calib_data, )
print(f"Saving quantized model to {SAVE_PATH}...") model.save_quantized(SAVE_PATH) tokenizer.save_pretrained(SAVE_PATH) print("Saved!")
model.eval()print(f"Device: {next(model.parameters()).device}")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# 3. ็ฟป่จณ้ขๆฐ# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโdef translate(text: str, target_lang: str = "ja", max_new_tokens: int = 256) -> str: 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)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# 4. ใใณใใใผใฏ๏ผFP16ใจๆฏ่ผใงใใใใๅไธใในใใฑใผใน๏ผ# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโtest_cases = [ ("Hello, how are you?", "ja"), ("The weather is beautiful today.", "ja"), ("Machine learning is transforming industries.", "ja"), ("ใใใซใกใฏใๅ
ๆฐใงใใ๏ผ", "en"),]
print("\n" + "="*60)print("AWQ INT8 ใใณใใใผใฏ")print("="*60)
total_time = 0.0for 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")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# 5. 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")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ# 6. FP16ใจใฎๆฏ่ผใกใข# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโprint("""ใๆๅพ
ใใใๆฏ่ผ็ตๆ๏ผ็ฎๅฎ๏ผใ FP16 AWQ INT8VRAMไฝฟ็จ้ : ~6.0 GB ~3.5 GBๆจ่ซ้ๅบฆ : baseline ~1.2-1.5x faster็ฟป่จณ็ฒพๅบฆ : 100% ~98-99%""")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293"""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 timeimport torchfrom 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.0for 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")README_madlad400_setup.mdAdded+85
madlad400_awq_int8.pyAdded+152
madlad400_fp16_flash.pyAdded+93