Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/tests/test-arange.cpp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / tests / test-arange.cpp` 100 lines ยท 2627 bytes Raw Blame History 1 #include "ggml.h" 2 #include "ggml-cpu.h" 3 #include "ggml-alloc.h" 4 #include "ggml-backend.h" 5 6 #ifdef GGML_USE_CUDA 7 #include "ggml-cuda.h" 8 #endif 9 10 #ifdef GGML_USE_METAL 11 #include "ggml-metal.h" 12 #endif 13 14 #include 15 #include 16 #include 17 18 int main(int /*argc*/, const char** /*argv*/) { 19 { 20 bool use_gpu = true; 21 GGML_UNUSED(use_gpu); 22 23 ggml_backend_t backend = NULL; 24 //ggml_backend_buffer_t buffer; 25 26 #ifdef GGML_USE_CUDA 27 if (use_gpu) { 28 fprintf(stderr, "%s: using CUDA backend\n", __func__); 29 backend = ggml_backend_cuda_init(0); 30 if (!backend) { 31 fprintf(stderr, "%s: ggml_backend_cuda_init() failed\n", __func__); 32 } 33 } 34 #endif 35 36 #ifdef GGML_USE_METAL 37 if (!backend) { 38 fprintf(stderr, "%s: using Metal backend\n", __func__); 39 backend = ggml_backend_metal_init(); 40 if (!backend) { 41 fprintf(stderr, "%s: ggml_backend_metal_init() failed\n", __func__); 42 } 43 } 44 #endif 45 46 const int num_tensors = 2; 47 48 struct ggml_init_params params = { 49 /*.mem_size =*/ ggml_tensor_overhead() * num_tensors + 2 * 1024 * 1024, 50 /*.mem_size =*/ NULL, 51 /*.mem_size =*/ true, 52 }; 53 54 if (!backend) { 55 // fallback to CPU backend 56 backend = ggml_backend_cpu_init(); 57 } 58 59 // create context 60 struct ggml_context* ctx = ggml_init(params); 61 struct ggml_tensor * t = ggml_arange(ctx, 0, 3, 1); 62 63 GGML_ASSERT(t->ne[0] == 3); 64 65 ggml_gallocr_t galloc = ggml_gallocr_new(ggml_backend_get_default_buffer_type(backend)); 66 67 struct ggml_cgraph * graph = ggml_new_graph(ctx); 68 ggml_build_forward_expand(graph, t); 69 70 // allocate tensors 71 ggml_gallocr_alloc_graph(galloc, graph); 72 73 int n_threads = 4; 74 75 if (ggml_backend_is_cpu(backend)) { 76 ggml_backend_cpu_set_n_threads(backend, n_threads); 77 } 78 79 ggml_backend_graph_compute(backend, graph); 80 81 float * output = new float[ggml_nelements(t)]; 82 ggml_backend_tensor_get(t, output, 0, ggml_nbytes(t)); 83 84 for (int i = 0; i < t->ne[0]; i++) { 85 printf("%.2f ", output[i]); 86 } 87 printf("\n"); 88 89 GGML_ASSERT(output[0] == 0); 90 GGML_ASSERT(output[1] == 1); 91 GGML_ASSERT(output[2] == 2); 92 93 delete[] output; 94 ggml_free(ctx); 95 ggml_gallocr_free(galloc); 96 ggml_backend_free(backend); 97 } 98 99 return 0; 100 }