Upload training_code/test_sft.py with huggingface_hub
Browse files- training_code/test_sft.py +148 -0
training_code/test_sft.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Quick test of model quality with diverse prompts."""
|
| 2 |
+
|
| 3 |
+
import os, sys, time, torch
|
| 4 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 5 |
+
from model.config import ModelConfig
|
| 6 |
+
from model.transformer import Transformer
|
| 7 |
+
from model.data import get_tokenizer
|
| 8 |
+
|
| 9 |
+
DPO_CKPT = "/jfs/deepak-kumar/checkpoints_dpo/dpo_final.pt"
|
| 10 |
+
SFT_CKPT = "/jfs/deepak-kumar/checkpoints_sft/sft_final.pt"
|
| 11 |
+
CHECKPOINT = DPO_CKPT if os.path.exists(DPO_CKPT) else SFT_CKPT
|
| 12 |
+
DEVICE = "cuda:0"
|
| 13 |
+
|
| 14 |
+
USER_START = "<|user|>\n"
|
| 15 |
+
ASST_START = "<|assistant|>\n"
|
| 16 |
+
TURN_END = "\n<|end|>\n"
|
| 17 |
+
|
| 18 |
+
TEST_PROMPTS = [
|
| 19 |
+
"Hi! How are you?",
|
| 20 |
+
"What is photosynthesis?",
|
| 21 |
+
"Explain gravity to a 5-year-old.",
|
| 22 |
+
"Write a short poem about the ocean.",
|
| 23 |
+
"What are the three states of matter?",
|
| 24 |
+
"How does a computer work?",
|
| 25 |
+
"What is the capital of France and why is it famous?",
|
| 26 |
+
"Give me 3 tips for learning a new language.",
|
| 27 |
+
"What is machine learning in simple terms?",
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@torch.no_grad()
|
| 32 |
+
def generate(model, tokenizer, prompt, max_new_tokens=256,
|
| 33 |
+
temperature=0.7, top_k=50, top_p=0.9, repetition_penalty=1.15):
|
| 34 |
+
input_ids = tokenizer.encode(prompt, add_special_tokens=False)
|
| 35 |
+
input_ids = torch.tensor([input_ids], dtype=torch.long, device=DEVICE)
|
| 36 |
+
generated = []
|
| 37 |
+
eos_id = tokenizer.eos_token_id
|
| 38 |
+
|
| 39 |
+
end_token_ids = tokenizer.encode("<|end|>", add_special_tokens=False)
|
| 40 |
+
end_id = end_token_ids[0] if end_token_ids else None
|
| 41 |
+
user_token_ids = tokenizer.encode("<|user|>", add_special_tokens=False)
|
| 42 |
+
user_id = user_token_ids[0] if user_token_ids else None
|
| 43 |
+
|
| 44 |
+
stop_ids = set()
|
| 45 |
+
if eos_id is not None:
|
| 46 |
+
stop_ids.add(eos_id)
|
| 47 |
+
if end_id is not None:
|
| 48 |
+
stop_ids.add(end_id)
|
| 49 |
+
if user_id is not None:
|
| 50 |
+
stop_ids.add(user_id)
|
| 51 |
+
|
| 52 |
+
for _ in range(max_new_tokens):
|
| 53 |
+
with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
|
| 54 |
+
logits, _ = model(input_ids)
|
| 55 |
+
|
| 56 |
+
logits = logits[:, -1, :].float()
|
| 57 |
+
|
| 58 |
+
if repetition_penalty != 1.0 and generated:
|
| 59 |
+
for tid in set(generated):
|
| 60 |
+
if logits[0, tid] > 0:
|
| 61 |
+
logits[0, tid] /= repetition_penalty
|
| 62 |
+
else:
|
| 63 |
+
logits[0, tid] *= repetition_penalty
|
| 64 |
+
|
| 65 |
+
logits = logits / max(temperature, 1e-5)
|
| 66 |
+
|
| 67 |
+
if top_k > 0:
|
| 68 |
+
topk_vals, _ = torch.topk(logits, min(top_k, logits.size(-1)))
|
| 69 |
+
logits[logits < topk_vals[:, -1:]] = float('-inf')
|
| 70 |
+
|
| 71 |
+
if top_p < 1.0:
|
| 72 |
+
sorted_logits, sorted_idx = torch.sort(logits, descending=True)
|
| 73 |
+
cumulative = torch.cumsum(torch.softmax(sorted_logits, dim=-1), dim=-1)
|
| 74 |
+
remove = cumulative - torch.softmax(sorted_logits, dim=-1) > top_p
|
| 75 |
+
sorted_logits[remove] = float('-inf')
|
| 76 |
+
logits = sorted_logits.scatter(1, sorted_idx, sorted_logits)
|
| 77 |
+
|
| 78 |
+
probs = torch.softmax(logits, dim=-1)
|
| 79 |
+
next_token = torch.multinomial(probs, 1)
|
| 80 |
+
token_id = next_token.item()
|
| 81 |
+
|
| 82 |
+
if token_id in stop_ids:
|
| 83 |
+
break
|
| 84 |
+
|
| 85 |
+
generated.append(token_id)
|
| 86 |
+
input_ids = torch.cat([input_ids, next_token], dim=1)
|
| 87 |
+
|
| 88 |
+
if input_ids.size(1) > 2048:
|
| 89 |
+
break
|
| 90 |
+
|
| 91 |
+
return tokenizer.decode(generated, skip_special_tokens=True)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def main():
|
| 95 |
+
ckpt_name = "DPO" if "dpo" in CHECKPOINT else "SFT"
|
| 96 |
+
print("=" * 70)
|
| 97 |
+
print(" " + ckpt_name + " MODEL TEST")
|
| 98 |
+
print("=" * 70)
|
| 99 |
+
|
| 100 |
+
tokenizer = get_tokenizer()
|
| 101 |
+
special_tokens = ["<|user|>", "<|assistant|>", "<|end|>"]
|
| 102 |
+
vocab = tokenizer.get_vocab()
|
| 103 |
+
new_tokens = [t for t in special_tokens if t not in vocab]
|
| 104 |
+
if new_tokens:
|
| 105 |
+
tokenizer.add_tokens(new_tokens, special_tokens=True)
|
| 106 |
+
|
| 107 |
+
config = ModelConfig()
|
| 108 |
+
config.vocab_size = len(tokenizer)
|
| 109 |
+
model = Transformer(config)
|
| 110 |
+
|
| 111 |
+
print("")
|
| 112 |
+
print("Loading checkpoint: " + CHECKPOINT)
|
| 113 |
+
ckpt = torch.load(CHECKPOINT, map_location="cpu", weights_only=False)
|
| 114 |
+
model.load_state_dict(ckpt["model"])
|
| 115 |
+
step = ckpt.get("step", "?")
|
| 116 |
+
del ckpt
|
| 117 |
+
|
| 118 |
+
model = model.to(DEVICE).bfloat16().eval()
|
| 119 |
+
print("Model loaded (" + ckpt_name + " step " + str(step) + ", vocab " + str(config.vocab_size) + ")")
|
| 120 |
+
mem = torch.cuda.max_memory_allocated(DEVICE) / 1e9
|
| 121 |
+
print("GPU memory: " + str(round(mem, 1)) + " GB")
|
| 122 |
+
print("-" * 70)
|
| 123 |
+
|
| 124 |
+
for i, question in enumerate(TEST_PROMPTS, 1):
|
| 125 |
+
prompt = USER_START + question + TURN_END + ASST_START
|
| 126 |
+
|
| 127 |
+
print("")
|
| 128 |
+
print("[Test " + str(i) + "/" + str(len(TEST_PROMPTS)) + "]")
|
| 129 |
+
print(" Q: " + question)
|
| 130 |
+
|
| 131 |
+
t0 = time.time()
|
| 132 |
+
response = generate(model, tokenizer, prompt)
|
| 133 |
+
dt = time.time() - t0
|
| 134 |
+
tokens = len(tokenizer.encode(response, add_special_tokens=False))
|
| 135 |
+
|
| 136 |
+
response = response.split("<|end|>")[0].split("<|user|>")[0].strip()
|
| 137 |
+
|
| 138 |
+
print(" A: " + response)
|
| 139 |
+
tps = int(tokens / max(dt, 0.01))
|
| 140 |
+
print(" [" + str(tokens) + " tokens, " + str(round(dt, 1)) + "s, " + str(tps) + " tok/s]")
|
| 141 |
+
print("-" * 70)
|
| 142 |
+
|
| 143 |
+
print("")
|
| 144 |
+
print("Done!")
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
if __name__ == "__main__":
|
| 148 |
+
main()
|