Skip to content

Commit e053703

Browse files
alabulei1juntao
authored andcommitted
Update llm_inference.md
Signed-off-by: alabulei1 <vivian.xiage@gmail.com>
1 parent e3cca88 commit e053703

File tree

1 file changed

+31
-57
lines changed

1 file changed

+31
-57
lines changed

docs/develop/rust/wasinn/llm_inference.md

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,11 @@
22
sidebar_position: 1
33
---
44

5-
# Llama 2 inference
6-
7-
WasmEdge now supports running open source models in Rust. We will use [this example project](https://github.com/second-state/LlamaEdge/tree/main/chat) to show how to make AI inferences with the llama2 model in WasmEdge and Rust.
8-
9-
WasmEdge now supports the following models:
10-
11-
1. Llama-2-7B-Chat
12-
1. Llama-2-13B-Chat
13-
1. CodeLlama-13B-Instruct
14-
1. Mistral-7B-Instruct-v0.1
15-
1. Mistral-7B-Instruct-v0.2
16-
1. MistralLite-7B
17-
1. OpenChat-3.5-0106
18-
1. OpenChat-3.5-1210
19-
1. OpenChat-3.5
20-
1. Wizard-Vicuna-13B-Uncensored-GGUF
21-
1. TinyLlama-1.1B-Chat-v1.0
22-
1. Baichuan2-13B-Chat
23-
1. OpenHermes-2.5-Mistral-7B
24-
1. Dolphin-2.2-Yi-34B
25-
1. Dolphin-2.6-Mistral-7B
26-
1. Samantha-1.2-Mistral-7B
27-
1. Samantha-1.11-CodeLlama-34B
28-
1. WizardCoder-Python-7B-V1.0
29-
1. Zephyr-7B-Alpha
30-
1. WizardLM-13B-V1.0-Uncensored
31-
1. Orca-2-13B
32-
1. Neural-Chat-7B-v3-1
33-
1. Yi-34B-Chat
34-
1. Starling-LM-7B-alpha
35-
1. DeepSeek-Coder-6.7B
36-
1. DeepSeek-LLM-7B-Chat
37-
1. SOLAR-10.7B-Instruct-v1.0
38-
1. Mixtral-8x7B-Instruct-v0.1
39-
1. Nous-Hermes-2-Mixtral-8x7B-DPO
40-
1. Nous-Hermes-2-Mixtral-8x7B-SFT
41-
42-
And more, please check [the supported models](https://github.com/second-state/LlamaEdge/blob/main/models.md) for details.
5+
# LLM inference
6+
7+
WasmEdge now supports running open-source Large Language Models (LLMs) in Rust. We will use [this example project](https://github.com/second-state/LlamaEdge/tree/main/chat) to show how to make AI inferences with the llama-3.1-8B model in WasmEdge and Rust.
8+
9+
Basically, WasmEdge can support any open-source LLMs. Please check [the supported models](https://github.com/second-state/LlamaEdge/blob/main/models.md) for details.
4310

4411
## Prerequisite
4512

@@ -55,23 +22,23 @@ First, get the latest llama-chat wasm application
5522
curl -LO https://github.com/LlamaEdge/LlamaEdge/releases/latest/download/llama-chat.wasm
5623
```
5724

58-
Next, let's get the model. In this example, we are going to use the llama2 7b chat model in GGUF format. You can also use other kinds of llama2 models, check out [here](https://github.com/second-state/llamaedge/blob/main/chat/README.md#get-model).
25+
Next, let's get the model. In this example, we are going to use the llama-3.1-8B model in GGUF format. You can also use other kinds of LLMs, check out [here](https://github.com/second-state/llamaedge/blob/main/chat/README.md#get-model).
5926

6027
```bash
61-
curl -LO https://huggingface.co/wasmedge/llama2/resolve/main/llama-2-7b-chat-q5_k_m.gguf
28+
curl -LO https://huggingface.co/second-state/Meta-Llama-3.1-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf
6229
```
6330

6431
Run the inference application in WasmEdge.
6532

6633
```bash
67-
wasmedge --dir .:. --nn-preload default:GGML:AUTO:llama-2-7b-chat-q5_k_m.gguf llama-chat.wasm
34+
wasmedge --dir .:. --nn-preload default:GGML:AUTO:Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf llama-chat.wasm -p llama-a-chat
6835
```
6936

7037
After executing the command, you may need to wait a moment for the input prompt to appear. You can enter your question once you see the `[USER]:` prompt:
7138

7239
```bash
7340
[USER]:
74-
I have two apples, each costing 5 dollars. What is the total cost of these apple
41+
I have two apples, each costing 5 dollars. What is the total cost of these apples?
7542
[ASSISTANT]:
7643
The total cost of the two apples is 10 dollars.
7744
[USER]:
@@ -95,19 +62,26 @@ Second, use `cargo` to build the example project.
9562
cargo build --target wasm32-wasi --release
9663
```
9764

98-
The output WASM file is `target/wasm32-wasi/release/llama-chat.wasm`. Next, use WasmEdge to load the llama-2-7b model and then ask the model to questions.
65+
The output WASM file is `target/wasm32-wasi/release/llama-chat.wasm`. Next, use WasmEdge to load the llama-3.1-8b model and then ask the model questions.
9966

10067
```bash
101-
wasmedge --dir .:. --nn-preload default:GGML:AUTO:llama-2-7b-chat-q5_k_m.gguf llama-chat.wasm
68+
wasmedge --dir .:. --nn-preload default:GGML:AUTO:Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf llama-chat.wasm -p llama-3-chat
10269
```
10370

104-
After executing the command, you may need to wait a moment for the input prompt to appear. You can enter your question once you see the `[USER]:` prompt:
71+
After executing the command, you may need to wait a moment for the input prompt to appear. You can enter your question once you see the `[You]:` prompt:
10572

10673
```bash
107-
[USER]:
108-
Who is Robert Oppenheimer?
109-
[ASSISTANT]:
110-
Robert Oppenheimer was an American theoretical physicist and director of the Manhattan Project, which developed the atomic bomb during World War II. He is widely regarded as one of the most important physicists of the 20th century and is known for his contributions to the development of quantum mechanics and the theory of the atomic nucleus. Oppenheimer was also a prominent figure in the post-war nuclear weapons debate and was a strong advocate for international cooperation on nuclear weapons control.
74+
[You]:
75+
Which one is greater? 9.11 or 9.8?
76+
77+
[Bot]:
78+
9.11 is greater.
79+
80+
[You]:
81+
why
82+
83+
[Bot]:
84+
11 is greater than 8.
11185
```
11286

11387
## Options
@@ -118,13 +92,13 @@ You can configure the chat inference application through CLI options.
11892
-m, --model-alias <ALIAS>
11993
Model alias [default: default]
12094
-c, --ctx-size <CTX_SIZE>
121-
Size of the prompt context [default: 4096]
95+
Size of the prompt context [default: 512]
12296
-n, --n-predict <N_PRDICT>
12397
Number of tokens to predict [default: 1024]
12498
-g, --n-gpu-layers <N_GPU_LAYERS>
12599
Number of layers to run on the GPU [default: 100]
126100
-b, --batch-size <BATCH_SIZE>
127-
Batch size for prompt processing [default: 4096]
101+
Batch size for prompt processing [default: 512]
128102
-r, --reverse-prompt <REVERSE_PROMPT>
129103
Halt generation at PROMPT, return control.
130104
-s, --system-prompt <SYSTEM_PROMPT>
@@ -148,8 +122,8 @@ The `--prompt-template` option is perhaps the most interesting. It allows the ap
148122
Furthermore, the following command tells WasmEdge to print out logs and statistics of the model at runtime.
149123

150124
```bash
151-
wasmedge --dir .:. --nn-preload default:GGML:AUTO:llama-2-7b-chat-q5_k_m.gguf \
152-
llama-chat.wasm --prompt-template llama-2-chat --log-stat
125+
wasmedge --dir .:. --nn-preload default:GGML:AUTO:Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf \
126+
llama-chat.wasm --prompt-template llama-3-chat --log-stat
153127
..................................................................................................
154128
llama_new_context_with_model: n_ctx = 512
155129
llama_new_context_with_model: freq_base = 10000.0
@@ -173,7 +147,7 @@ You can make the inference program run faster by AOT compiling the wasm file fir
173147
174148
```bash
175149
wasmedge compile llama-chat.wasm llama-chat.wasm
176-
wasmedge --dir .:. --nn-preload default:GGML:AUTO:llama-2-7b-chat-q5_k_m.gguf llama-chat.wasm
150+
wasmedge --dir .:. --nn-preload default:GGML:AUTO:Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf llama-chat.wasm
177151
```
178152
179153
## Understand the code
@@ -185,7 +159,7 @@ The [main.rs](https://github.com/second-state/llamaedge/blob/main/chat/src/main.
185159
curl -LO https://github.com/second-state/llamaedge/releases/latest/download/llama-simple.wasm
186160
187161
# Give it a prompt and ask it to use the model to complete it.
188-
wasmedge --dir .:. --nn-preload default:GGML:AUTO:llama-2-7b-chat-q5_k_m.gguf llama-simple.wasm \
162+
wasmedge --dir .:. --nn-preload default:GGML:AUTO:Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf llama-simple.wasm \
189163
--prompt 'Robert Oppenheimer most important achievement is ' --ctx-size 512
190164
191165
output: in 1942, when he led the team that developed the first atomic bomb, which was dropped on Hiroshima, Japan in 1945.
@@ -275,7 +249,7 @@ Next, execute the model inference.
275249
context.compute().expect("Failed to complete inference");
276250
```
277251
278-
After the inference is finished, extract the result from the computation context and lose invalid UTF8 sequences handled by converting the output to a string using `String::from_utf8_lossy`.
252+
After the inference is finished, extract the result from the computation context and losing invalid UTF8 sequences handled by converting the output to a string using `String::from_utf8_lossy`.
279253
280254
```rust
281255
let mut output_buffer = vec![0u8; *CTX_SIZE.get().unwrap()];
@@ -296,5 +270,5 @@ println!("\noutput: {}", output);
296270
## Resources
297271
298272
* If you're looking for multi-turn conversations with llama 2 models, please check out the above mentioned chat example source code [here](https://github.com/second-state/llamaedge/tree/main/chat).
299-
* If you want to construct OpenAI-compatible APIs specifically for any open-source LLMs, please check out the source code [for the API server](https://github.com/second-state/llamaedge/tree/main/api-server).
273+
* If you want to construct OpenAI-compatible APIs specifically for your llama2 model, or the Llama2 model itself, please check out the source code [for the API server](https://github.com/second-state/llamaedge/tree/main/api-server).
300274
* To learn more, please check out [this article](https://medium.com/stackademic/fast-and-portable-llama2-inference-on-the-heterogeneous-edge-a62508e82359).

0 commit comments

Comments
 (0)