Views
No views yet


mistral-common1from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
2from mistral_common.protocol.instruct.messages import UserMessage
3from mistral_common.protocol.instruct.request import ChatCompletionRequest
4
5mistral_models_path = "MISTRAL_MODELS_PATH"
6
7tokenizer = MistralTokenizer.v1()
8
9completion_request = ChatCompletionRequest(messages=[UserMessage(content="Explain Machine Learning to me in a nutshell.")])
10
11tokens = tokenizer.encode_chat_completion(completion_request).tokens1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3model_id = "RedHatAI/Mixtral-8x7B-Instruct-v0.1"
4number_gpus = 4
5sampling_params = SamplingParams(temperature=0.7, top_p=0.8, max_tokens=256)
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7prompt = "Give me a short introduction to large language model."
8llm = LLM(model=model_id, tensor_parallel_size=number_gpus)
9outputs = llm.generate(prompt, sampling_params)
10generated_text = outputs[0].outputs[0].text
11print(generated_text)1podman run --rm -it --device nvidia.com/gpu=all -p 8000:8000 \
2 --ipc=host \
3--env "HUGGING_FACE_HUB_TOKEN=$HF_TOKEN" \
4--env "HF_HUB_OFFLINE=0" -v ~/.cache/vllm:/home/vllm/.cache \
5--name=vllm \
6registry.access.redhat.com/rhaiis/rh-vllm-cuda \
7vllm serve \
8--tensor-parallel-size 8 \
9--max-model-len 32768 \
10--enforce-eager --model RedHatAI/Mixtral-8x7B-Instruct-v0.11# Download model from Red Hat Registry via docker
2# Note: This downloads the model to ~/.cache/instructlab/models unless --model-dir is specified.
3ilab model download --repository docker://registry.redhat.io/rhelai1/mixtral-8x7b-instruct-v0-1:1.41# Serve model via ilab
2ilab model serve --model-path ~/.cache/instructlab/models/mixtral-8x7b-instruct-v0-1
3
4# Chat with model
5ilab model chat --model ~/.cache/instructlab/models/mixtral-8x7b-instruct-v0-11# Setting up vllm server with ServingRuntime
2# Save as: vllm-servingruntime.yaml
3apiVersion: serving.kserve.io/v1alpha1
4kind: ServingRuntime
5metadata:
6 name: vllm-cuda-runtime # OPTIONAL CHANGE: set a unique name
7 annotations:
8 openshift.io/display-name: vLLM NVIDIA GPU ServingRuntime for KServe
9 opendatahub.io/recommended-accelerators: '["nvidia.com/gpu"]'
10 labels:
11 opendatahub.io/dashboard: 'true'
12spec:
13 annotations:
14 prometheus.io/port: '8080'
15 prometheus.io/path: '/metrics'
16 multiModel: false
17 supportedModelFormats:
18 - autoSelect: true
19 name: vLLM
20 containers:
21 - name: kserve-container
22 image: quay.io/modh/vllm:rhoai-2.20-cuda # CHANGE if needed. If AMD: quay.io/modh/vllm:rhoai-2.20-rocm
23 command:
24 - python
25 - -m
26 - vllm.entrypoints.openai.api_server
27 args:
28 - "--port=8080"
29 - "--model=/mnt/models"
30 - "--served-model-name={{.Name}}"
31 env:
32 - name: HF_HOME
33 value: /tmp/hf_home
34 ports:
35 - containerPort: 8080
36 protocol: TCP1# Attach model to vllm server. This is an NVIDIA template
2# Save as: inferenceservice.yaml
3apiVersion: serving.kserve.io/v1beta1
4kind: InferenceService
5metadata:
6 annotations:
7 openshift.io/display-name: Mixtral-8x7B-Instruct-v0.1 # OPTIONAL CHANGE
8 serving.kserve.io/deploymentMode: RawDeployment
9 name: Mixtral-8x7B-Instruct-v0.1 # specify model name. This value will be used to invoke the model in the payload
10 labels:
11 opendatahub.io/dashboard: 'true'
12spec:
13 predictor:
14 maxReplicas: 1
15 minReplicas: 1
16 model:
17 modelFormat:
18 name: vLLM
19 name: ''
20 resources:
21 limits:
22 cpu: '2' # this is model specific
23 memory: 8Gi # this is model specific
24 nvidia.com/gpu: '1' # this is accelerator specific
25 requests: # same comment for this block
26 cpu: '1'
27 memory: 4Gi
28 nvidia.com/gpu: '1'
29 runtime: vllm-cuda-runtime # must match the ServingRuntime name above
30 storageUri: oci://registry.redhat.io/rhelai1/modelcar-mixtral-8x7b-instruct-v0-1:1.4
31 tolerations:
32 - effect: NoSchedule
33 key: nvidia.com/gpu
34 operator: Exists1# make sure first to be in the project where you want to deploy the model
2# oc project <project-name>
3# apply both resources to run model
4# Apply the ServingRuntime
5oc apply -f vllm-servingruntime.yaml
6# Apply the InferenceService
7oc apply -f qwen-inferenceservice.yaml1# Replace <inference-service-name> and <cluster-ingress-domain> below:
2# - Run `oc get inferenceservice` to find your URL if unsure.
3# Call the server using curl:
4curl https://<inference-service-name>-predictor-default.<domain>/v1/chat/completions
5 -H "Content-Type: application/json" \
6 -d '{
7 "model": "Mixtral-8x7B-Instruct-v0.1",
8 "stream": true,
9 "stream_options": {
10 "include_usage": true
11 },
12 "max_tokens": 1,
13 "messages": [
14 {
15 "role": "user",
16 "content": "How can a bee fly when its wings are so small?"
17 }
18 ]
19}'mistral_inference1from mistral_inference.transformer import Transformer
2from mistral_inference.generate import generate
3
4model = Transformer.from_folder(mistral_models_path)
5out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
6
7result = tokenizer.decode(out_tokens[0])
8
9print(result)transformers1from transformers import AutoModelForCausalLM
2
3model = AutoModelForCausalLM.from_pretrained("mistralai/Mixtral-8x7B-Instruct-v0.1")
4model.to("cuda")
5
6generated_ids = model.generate(tokens, max_new_tokens=1000, do_sample=True)
7
8# decode with mistral tokenizer
9result = tokenizer.decode(generated_ids[0].tolist())
10print(result)[!TIP] PRs to correct the transformers tokenizer so that it gives 1-to-1 the same results as the mistral-common reference implementation are very welcome!
<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))