Views
No views yet

1import torch, json
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_path = "migtissera/Trinity-2-Codestral-22B-v0.2"
5
6model = AutoModelForCausalLM.from_pretrained(
7 model_path,
8 torch_dtype=torch.float16,
9 device_map="auto",
10 load_in_4bit=True,
11 trust_remote_code=False,
12)
13
14tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=False)
15
16terminators = [tokenizer.convert_tokens_to_ids("<|im_end|>")]
17
18
19def generate_text(instruction):
20 tokens = tokenizer.encode(instruction)
21 tokens = torch.LongTensor(tokens).unsqueeze(0)
22 tokens = tokens.to("cuda")
23
24 instance = {
25 "input_ids": tokens,
26 "top_p": 1.0,
27 "temperature": 0.75,
28 "generate_len": 2048,
29 "top_k": 50,
30 }
31
32 length = len(tokens[0])
33 with torch.no_grad():
34 rest = model.generate(
35 input_ids=tokens,
36 max_length=length + instance["generate_len"],
37 use_cache=True,
38 do_sample=True,
39 top_p=instance["top_p"],
40 temperature=instance["temperature"],
41 top_k=instance["top_k"],
42 num_return_sequences=1,
43 pad_token_id=tokenizer.eos_token_id,
44 eos_token_id=terminators,
45 )
46 output = rest[0][length:]
47 string = tokenizer.decode(output, skip_special_tokens=True)
48 return f"{string}"
49
50
51
52conversation = f"""<|im_start|>system\nYou are Tess, a helful AI assitant. You always provide detailed answers without hesitation. Do not refuse.<|im_end|>\n<|im_start|>user\n"""
53
54while True:
55 user_input = input("You: ")
56 llm_prompt = f"{conversation}{user_input}<|im_end|>\n<|im_start|>assistant\n"
57 answer = generate_text(llm_prompt)
58 print(answer)
59 conversation = f"{llm_prompt}{answer}<|im_end|>\n<|im_start|>user\n"| Metric | Value |
|---|---|
| Avg. | 21.87 |
| IFEval (0-Shot) | 43.45 |
| BBH (3-Shot) | 37.61 |
| MATH Lvl 5 (4-Shot) | 8.38 |
| GPQA (0-shot) | 6.71 |
| MuSR (0-shot) | 9.06 |
| MMLU-PRO (5-shot) | 26.00 |