Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] create a complete document #571

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
update get-start.md
  • Loading branch information
Cyberhan123 committed Oct 12, 2023
commit b0926db3d4bea845d2c020921d7e1b9f9964854b
95 changes: 54 additions & 41 deletions docs/guide/get-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The above configuration declares a dependency on ggml which is downloaded from G

You can add ggml as a submodule of your project. In your project repository:

```bash
```sh
git submodule add https://github.com/ggerganov/ggml ggml
```

Expand All @@ -58,25 +58,27 @@ With ggml declared as a dependency, you can use ggml code within your own projec
As an example, create a file named hello_ggml.cpp in your project directory with the following contents:

```c++
#include <vector>
#include <cstdio>

#include "ggml/ggml.h"
#include "ggml/ggml-alloc.h"
#include "ggml/ggml-backend.h"

const struct ggml_init_params params = {
/*.mem_size =*/ ggml_tensor_overhead() * 10240,
/*.mem_buffer =*/ nullptr,
/*.no_alloc =*/ true
/*.mem_size =*/ ggml_tensor_overhead() * 10240,
/*.mem_buffer =*/ nullptr,
/*.no_alloc =*/ true
};
int main(int argc, char ** argv) {
// this get time
// init ggml_time total
ggml_time_init();
const auto t_main_start_us = ggml_time_us();
// create ggml ctx
const auto ctx = ggml_init(params);
// create ggml cpu backend
const auto backend = ggml_backend_cpu_init();
// create a 1d tensor
// create 1d tensor
const auto tensor_a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
const auto tensor_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
auto tensor_c = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
Expand All @@ -90,54 +92,65 @@ int main(int argc, char ** argv) {

// set value
{

const auto alloc = ggml_allocr_new_from_buffer(tensor_1d_buffer);
// this updates the pointers in the tensors to point to the correct location in the buffer
// this is necessary since the ggml_context is .no_alloc == true
// note that the buffer can actually be a device buffer, depending on the backend
ggml_allocr_alloc(alloc, tensor_a);
ggml_allocr_alloc(alloc, tensor_b);

// in cpu we also can do
// tensor_a->data = &tensor_data_a.data();
// tensor_b->data = &tensor_data_b.data();
ggml_backend_tensor_set(tensor_a, tensor_data_a.data(), 0, ggml_nbytes(tensor_a));
ggml_backend_tensor_set(tensor_b, tensor_data_b.data(), 0, ggml_nbytes(tensor_b));
ggml_allocr_free(alloc);
const auto alloc = ggml_allocr_new_from_buffer(tensor_1d_buffer);
// this updates the pointers in the tensors to point to the correct location in the buffer
// this is necessary since the ggml_context is .no_alloc == true
// note that the buffer can actually be a device buffer, depending on the backend
ggml_allocr_alloc(alloc, tensor_a);
ggml_allocr_alloc(alloc, tensor_b);

// in cpu we also can do
// tensor_a->data = &tensor_data_a.data();
// tensor_b->data = &tensor_data_b.data();
ggml_backend_tensor_set(tensor_a, tensor_data_a.data(), 0, ggml_nbytes(tensor_a));
ggml_backend_tensor_set(tensor_b, tensor_data_b.data(), 0, ggml_nbytes(tensor_b));
ggml_allocr_free(alloc);
}

// compute
{
const auto compute_tensor_buffer = ggml_backend_alloc_buffer(backend, 656480);
const auto allocr = ggml_allocr_new_from_buffer(tensor_1d_buffer);
const auto gf = ggml_new_graph(ctx);

// creat forward
tensor_c = ggml_add(ctx, tensor_a, tensor_b);
ggml_build_forward_expand(gf, tensor_c);

// allocate tensors
ggml_allocr_alloc_graph(allocr, gf);

if (ggml_backend_is_cpu(backend)) {
ggml_backend_cpu_set_n_threads(backend, 1);
}
const auto compute_tensor_buffer = ggml_backend_alloc_buffer(backend, 656480);
const auto allocr = ggml_allocr_new_from_buffer(compute_tensor_buffer);
const auto gf = ggml_new_graph(ctx);
// creat forward
tensor_c = ggml_add(ctx, tensor_a, tensor_b);
ggml_build_forward_expand(gf, tensor_c);
// allocate tensors
ggml_allocr_alloc_graph(allocr, gf);
if (ggml_backend_is_cpu(backend)) {
ggml_backend_cpu_set_n_threads(backend, 1);
}

ggml_backend_graph_compute(backend, gf);
ggml_backend_graph_compute(backend, gf);

tensor_data_c.resize(1);
ggml_backend_tensor_get(gf->nodes[gf->n_nodes - 1], tensor_data_c.data(), 0,
tensor_data_c.size() * sizeof(float));
printf("result is = %p \n", tensor_data_c.data());
tensor_data_c.resize(1);
ggml_backend_tensor_get(gf->nodes[gf->n_nodes - 1], tensor_data_c.data(), 0,
tensor_data_c.size() * sizeof(float));
printf("result is = [");
for (auto i = tensor_data_c.begin(); i != tensor_data_c.end(); ++i) {
printf("%f ", *i);
}
printf("]\n");
ggml_allocr_free(allocr);
}

const auto t_main_end_us = ggml_time_us();

printf("total time = %8.2f ms\n", (t_main_end_us - t_main_start_us) / 1000.0f);


ggml_free(ctx);
ggml_backend_buffer_free(tensor_1d_buffer);
ggml_backend_free(backend);
}
```
```

The result is 3.000000
```sh
result is = [3.000000]
total time = 10.93 ms
Process finished with exit code 0
```