Views
No views yet
<s> [INST] Instruction [/INST] Model answer</s> [INST] Follow-up instruction [/INST]<s> and </s> are special tokens for beginning of string (BOS) and end of string (EOS) while [INST] and [/INST] are regular strings.1def tokenize(text):
2 return tok.encode(text, add_special_tokens=False)
3
4[BOS_ID] +
5tokenize("[INST]") + tokenize(USER_MESSAGE_1) + tokenize("[/INST]") +
6tokenize(BOT_MESSAGE_1) + [EOS_ID] +
7…
8tokenize("[INST]") + tokenize(USER_MESSAGE_N) + tokenize("[/INST]") +
9tokenize(BOT_MESSAGE_N) + [EOS_ID]tokenize method should not add a BOS or EOS token automatically, but should add a prefix space.1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
4tokenizer = AutoTokenizer.from_pretrained(model_id)
5
6model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
7
8messages = [
9 {"role": "user", "content": "What is your favourite condiment?"},
10 {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
11 {"role": "user", "content": "Do you have mayonnaise recipes?"}
12]
13
14inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
15
16outputs = model.generate(inputs, max_new_tokens=20)
17print(tokenizer.decode(outputs[0], skip_special_tokens=True))float16 precision only works on GPU devices1+ import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6
7+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
8
9messages = [
10 {"role": "user", "content": "What is your favourite condiment?"},
11 {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
12 {"role": "user", "content": "Do you have mayonnaise recipes?"}
13]
14
15input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
16
17outputs = model.generate(input_ids, max_new_tokens=20)
18print(tokenizer.decode(outputs[0], skip_special_tokens=True))bitsandbytes1+ import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6
7+ model = AutoModelForCausalLM.from_pretrained(model_id, load_in_4bit=True, device_map="auto")
8
9text = "Hello my name is"
10messages = [
11 {"role": "user", "content": "What is your favourite condiment?"},
12 {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
13 {"role": "user", "content": "Do you have mayonnaise recipes?"}
14]
15
16input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
17
18outputs = model.generate(input_ids, max_new_tokens=20)
19print(tokenizer.decode(outputs[0], skip_special_tokens=True))1+ import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6
7+ model = AutoModelForCausalLM.from_pretrained(model_id, use_flash_attention_2=True, device_map="auto")
8
9messages = [
10 {"role": "user", "content": "What is your favourite condiment?"},
11 {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
12 {"role": "user", "content": "Do you have mayonnaise recipes?"}
13]
14
15input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
16
17outputs = model.generate(input_ids, max_new_tokens=20)
18print(tokenizer.decode(outputs[0], skip_special_tokens=True))