ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
| 1 | #include <ggml.h> |
| 2 | #include <ggml-cpu.h> |
| 3 | #include <ggml-alloc.h> |
| 4 | #include <ggml-backend.h> |
| 5 | #include <ggml-cpp.h> |
| 6 | |
| 7 | #include <cassert> |
| 8 | #include <cmath> |
| 9 | #include <cstdio> |
| 10 | #include <cstring> |
| 11 | #include <array> |
| 12 | #include <numeric> |
| 13 | #include <vector> |
| 14 | |
| 15 | int64_t wrap(int64_t i, int64_t ne) { |
| 16 | if (i < 0) { |
| 17 | return i + ne; |
| 18 | } else if (i >= ne) { |
| 19 | return i - ne; |
| 20 | } |
| 21 | return i; |
| 22 | } |
| 23 | |
| 24 | std::vector<float> roll_reference( |
| 25 | const float * src, std::array<int64_t, 4> ne, std::array<int, 4> shift) { |
| 26 | |
| 27 | const int64_t ne0 = ne[0], ne1 = ne[1], ne2 = ne[2], ne3 = ne[3]; |
| 28 | std::vector<float> dst(ne0 * ne1 * ne2 * ne3); |
| 29 | |
| 30 | for (int64_t i3 = 0; i3 < ne3; ++i3) { |
| 31 | for (int64_t i2 = 0; i2 < ne2; ++i2) { |
| 32 | for (int64_t i1 = 0; i1 < ne1; ++i1) { |
| 33 | for (int64_t i0 = 0; i0 < ne0; ++i0) { |
| 34 | const int64_t i03 = wrap(i3 - shift[3], ne3); |
| 35 | const int64_t i02 = wrap(i2 - shift[2], ne2); |
| 36 | const int64_t i01 = wrap(i1 - shift[1], ne1); |
| 37 | const int64_t i00 = wrap(i0 - shift[0], ne0); |
| 38 | |
| 39 | dst[i3 * (ne2*ne1*ne0) + i2 * (ne1*ne0) + i1 * ne0 + i0] = |
| 40 | src[i03 * (ne2*ne1*ne0) + i02 * (ne1*ne0) + i01 * ne0 + i00]; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | return dst; |
| 46 | } |
| 47 | |
| 48 | std::vector<float> f32_range(int64_t n) { |
| 49 | std::vector<float> values(n); |
| 50 | std::iota(values.begin(), values.end(), 0.f); |
| 51 | return values; |
| 52 | } |
| 53 | |
| 54 | bool check_equal(const std::vector<float> & result, const std::vector<float> & expected) { |
| 55 | if (result.size() != expected.size()) { |
| 56 | printf("result.size() = %d, expected.size() = %d\n", (int)result.size(), (int)expected.size()); |
| 57 | return false; |
| 58 | } |
| 59 | for (int i = 0; i < result.size(); i++) { |
| 60 | if(std::abs(result[i] - expected[i]) > 1e-5) { |
| 61 | printf("result[%d] %f != %f expected[%d]\n", i, result[i], expected[i], i); |
| 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | bool test_roll(std::array<int64_t, 4> ne, std::array<int, 4> shift, bool permute) { |
| 69 | ggml_time_init(); |
| 70 | |
| 71 | ggml_init_params params { |
| 72 | /*.mem_size =*/ 64 * ggml_tensor_overhead() + ggml_graph_overhead(), |
| 73 | /*.mem_buffer =*/ NULL, |
| 74 | /*.no_alloc =*/ true |
| 75 | }; |
| 76 | |
| 77 | ggml_context_ptr ctx_ptr{ggml_init(params)}; |
| 78 | ggml_context * ctx = ctx_ptr.get(); |
| 79 | ggml_cgraph * gf = ggml_new_graph(ctx); |
| 80 | |
| 81 | // Build graph |
| 82 | ggml_tensor * src = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne.data()); |
| 83 | ggml_tensor * res; |
| 84 | if (!permute) { |
| 85 | res = ggml_roll(ctx, src, shift[0], shift[1], shift[2], shift[3]); |
| 86 | } else { |
| 87 | ggml_tensor * p = ggml_permute(ctx, src, 0, 3, 1, 2); |
| 88 | res = ggml_roll(ctx, p, shift[0], shift[2], shift[3], shift[1]); |
| 89 | res = ggml_cont(ctx, ggml_permute(ctx, res, 0, 2, 3, 1)); |
| 90 | } |
| 91 | ggml_build_forward_expand(gf, res); |
| 92 | |
| 93 | // Create backend & allocate buffers |
| 94 | ggml_backend_ptr backend_ptr{ggml_backend_cpu_init()}; |
| 95 | ggml_backend_t backend = backend_ptr.get(); |
| 96 | ggml_backend_cpu_set_n_threads(backend, 2); |
| 97 | ggml_backend_buffer_ptr buffer{ggml_backend_alloc_ctx_tensors(ctx, backend)}; |
| 98 | |
| 99 | std::vector<float> src_values = f32_range(ggml_nelements(src)); |
| 100 | ggml_backend_tensor_set(src, src_values.data(), 0, ggml_nbytes(src)); |
| 101 | |
| 102 | // Execute and compare results |
| 103 | ggml_backend_graph_compute(backend, gf); |
| 104 | |
| 105 | std::vector<float> res_values(ggml_nelements(res)); |
| 106 | ggml_backend_tensor_get(res, res_values.data(), 0, ggml_nbytes(res)); |
| 107 | |
| 108 | std::vector<float> expected = roll_reference(src_values.data(), ne, shift); |
| 109 | |
| 110 | bool passed = check_equal(res_values, expected); |
| 111 | |
| 112 | printf("ggml_roll(%d(%d), %d(%d), %d(%d), %d(%d), %s): %s\n", |
| 113 | int(ne[0]), int(shift[0]), |
| 114 | int(ne[1]), int(shift[1]), |
| 115 | int(ne[2]), int(shift[2]), |
| 116 | int(ne[3]), int(shift[3]), |
| 117 | permute ? "permuted" : "contiguous", |
| 118 | passed ? "\033[32mPASSED\033[0m" : "\033[31mFAILED\033[0m"); |
| 119 | return passed; |
| 120 | } |
| 121 | |
| 122 | int main() { |
| 123 | bool passed = true; |
| 124 | passed &= test_roll({3, 7, 4, 2}, {1, 0, -1, 0}, false); |
| 125 | passed &= test_roll({37, 42, 59, 2}, {-4, 3, -7, 1}, false); |
| 126 | passed &= test_roll({37, 42, 59, 2}, {-4, 3, -7, 1}, true); |
| 127 | return passed ? 0 : 1; |
| 128 | } |