Skip to content

Commit 76e9f01

Browse files
committed
[Example] ggml: update gemma readme
Signed-off-by: hydai <z54981220@gmail.com>
1 parent 9292ea5 commit 76e9f01

File tree

3 files changed

+128
-3
lines changed

3 files changed

+128
-3
lines changed

‎wasmedge-ggml/gemma/README.md‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# `gemma`
2+
3+
## Get Model
4+
5+
```console
6+
wget https://huggingface.co/second-state/Gemma-2b-it-GGUF/resolve/main/gemma-2b-it-Q4_K_M.gguf
7+
```
8+
9+
## Execute
10+
11+
````console
12+
$ wasmedge --dir .:. \
13+
--nn-preload default:GGML:AUTO:gemma-2b-it-Q4_K_M.gguf \
14+
./wasmedge-ggml-gemma.wasm default
15+
16+
USER:
17+
Write a merge sort in Rust for me
18+
ASSISTANT:
19+
```rust
20+
fn merge_sort(arr: &mut [i32]) {
21+
if arr.len() <= 1 {
22+
return;
23+
}
24+
let mid = arr.len() / 2;
25+
let (left, right) = arr.split_at_mut(mid);
26+
27+
merge_sort(left);
28+
merge_sort(right);
29+
30+
merge(arr, left, right);
31+
}
32+
33+
fn merge(arr: &mut [i32], left: &mut [i32], right: &mut [i32]) {
34+
let mut merged = vec![];
35+
36+
for i in 0..left.len() {
37+
merged.push(*left[i]);
38+
}
39+
40+
for j in 0..right.len() {
41+
merged.push(*right[j]);
42+
}
43+
44+
arr.clear();
45+
for item in merged {
46+
arr.push(item);
47+
}
48+
}
49+
```
50+
51+
**Usage:**
52+
53+
```rust
54+
let mut numbers = vec![5, 2, 8, 3, 1, 9, 4];
55+
merge_sort(&mut numbers);
56+
println!("{:?}", numbers); // Output: [1, 2, 3, 4, 5, 8, 9]
57+
```
58+
59+
**Explanation:**
60+
61+
The `merge` function takes three slices as input and merges them into a single sorted slice. It works by iterating over the two input slices and adding each element to the merged slice until one of the slices is empty.
62+
63+
* `left` contains the first half of the elements.
64+
* `right` contains the second half of the elements.
65+
* The merged slice is stored in `arr`.
66+
67+
**Time complexity:**
68+
69+
Merge sort has a time complexity of O(n log n), where n is the length of the input vector. This is because the function divides the problem into smaller subproblems until each subproblem contains only one element.
70+
USER:
71+
How about in C++?
72+
ASSISTANT:
73+
```c++
74+
#include <iostream>
75+
#include <vector>
76+
77+
using namespace std;
78+
79+
void merge(int arr[], int left[], int right[]) {
80+
int n = sizeof(arr) / 2;
81+
int i = 0, j = 0, k = 0;
82+
83+
while (i < n && j < left[n]) {
84+
if (left[i] <= right[j]) {
85+
arr[k++] = left[i++];
86+
} else {
87+
arr[k++] = right[j++];
88+
}
89+
}
90+
91+
while (i < n) {
92+
arr[k++] = left[i++];
93+
}
94+
95+
while (j < left[n]) {
96+
arr[k++] = right[j++];
97+
}
98+
}
99+
100+
void merge_sort(vector<int>& arr) {
101+
if (arr.size() <= 1) {
102+
return;
103+
}
104+
int mid = arr.size() / 2;
105+
merge_sort(arr.data() + mid);
106+
merge_sort(arr.data() + mid * 2);
107+
merge(arr.data(), arr.data() + mid, arr.data() + 2 * mid);
108+
}
109+
110+
int main() {
111+
vector<int> numbers = {5, 2, 8, 3, 1, 9, 4};
112+
merge_sort(numbers);
113+
cout << "{:?}", numbers) << endl;
114+
return 0;
115+
}
116+
```
117+
118+
**Output:**
119+
120+
```
121+
{:?}", [1, 2, 3, 4, 5, 8, 9]
122+
```
123+
````

‎wasmedge-ggml/gemma/src/main.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ fn get_options_from_env() -> Value {
2323
}
2424
if let Ok(val) = env::var("ctx_size") {
2525
options["ctx-size"] = serde_json::from_str(val.as_str()).unwrap()
26+
} else {
27+
options["ctx-size"] = serde_json::from_str("4096").unwrap()
2628
}
2729
if let Ok(val) = env::var("n_gpu_layers") {
2830
options["n-gpu-layers"] = serde_json::from_str(val.as_str()).unwrap()
31+
} else {
32+
options["n-gpu-layers"] = serde_json::from_str("100").unwrap()
2933
}
30-
if let Ok(val) = env::var("stream_output") {
31-
options["stream-stdout"] = serde_json::from_str(val.as_str()).unwrap()
32-
}
34+
options["stream-stdout"] = serde_json::from_str("true").unwrap();
3335

3436
options
3537
}
-138 KB
Binary file not shown.

0 commit comments

Comments
 (0)