Views
No views yet
1# Install ExLLamaV2
2!git clone https://github.com/turboderp/exllamav2
3!pip install -e exllamav21from huggingface_hub import login, HfApi, create_repo
2from torch import bfloat16
3import locale
4import torch
5import os1# Define the model ID for the desired model
2model_id = "alokabhishek/Mistral-7B-Instruct-v0.2-8.0-bpw-exl2"
3BPW = 8.0
4
5# define variables
6model_name = model_id.split("/")[-1]
71!git-lfs install
2# download the model to loacl directory
3!git clone https://{username}:{HF_TOKEN}@huggingface.co/{model_id} {model_name}1# Run model
2!python exllamav2/test_inference.py -m {model_name}/ -p "Tell me a funny joke about Large Language Models meeting a Blackhole in an intergalactic Bar."1import sys, os
2
3sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
4
5from exllamav2 import (
6 ExLlamaV2,
7 ExLlamaV2Config,
8 ExLlamaV2Cache,
9 ExLlamaV2Tokenizer,
10)
11
12from exllamav2.generator import ExLlamaV2BaseGenerator, ExLlamaV2Sampler
13
14import time
15
16# Initialize model and cache
17
18model_directory = "/model_path/Mistral-7B-Instruct-v0.2-8.0-bpw-exl2/"
19print("Loading model: " + model_directory)
20
21config = ExLlamaV2Config(model_directory)
22model = ExLlamaV2(config)
23cache = ExLlamaV2Cache(model, lazy=True)
24model.load_autosplit(cache)
25tokenizer = ExLlamaV2Tokenizer(config)
26
27# Initialize generator
28
29generator = ExLlamaV2BaseGenerator(model, cache, tokenizer)
30
31# Generate some text
32
33settings = ExLlamaV2Sampler.Settings()
34settings.temperature = 0.85
35settings.top_k = 50
36settings.top_p = 0.8
37settings.token_repetition_penalty = 1.01
38settings.disallow_tokens(tokenizer, [tokenizer.eos_token_id])
39
40prompt = "Tell me a funny joke about Large Language Models meeting a Blackhole in an intergalactic Bar."
41
42max_new_tokens = 512
43
44generator.warmup()
45time_begin = time.time()
46
47output = generator.generate_simple(prompt, settings, max_new_tokens, seed=1234)
48
49time_end = time.time()
50time_total = time_end - time_begin
51
52print(output)
53print()
54print(f"Response generated in {time_total:.2f} seconds")
55
56[INST] and [/INST] tokens. The very first instruction should begin with a begin of sentence id. The next instructions should not. The assistant generation will be ended by the end-of-sentence token id.text = "<s>[INST] What is your favourite condiment? [/INST]"
"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!</s> "
"[INST] Do you have mayonnaise recipes? [/INST]"apply_chat_template() method:1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3device = "cuda" # the device to load the model onto
4
5model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
6tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
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
14encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt")
15
16model_inputs = encodeds.to(device)
17model.to(device)
18
19generated_ids = model.generate(model_inputs, max_new_tokens=1000, do_sample=True)
20decoded = tokenizer.batch_decode(generated_ids)
21print(decoded[0])Traceback (most recent call last):
File "", line 1, in
File "/transformers/models/auto/auto_factory.py", line 482, in from_pretrained
config, kwargs = AutoConfig.from_pretrained(
File "/transformers/models/auto/configuration_auto.py", line 1022, in from_pretrained
config_class = CONFIG_MAPPING[config_dict["model_type"]]
File "/transformers/models/auto/configuration_auto.py", line 723, in getitem
raise KeyError(key)
KeyError: 'mistral'