interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / examples / python / example_add_quant.py

25 lines · 853 bytes Raw Blame History
1from ggml import lib, ffi
2from ggml.utils import init, copy, numpy
3import numpy as np
4
5ctx = init(mem_size=12*1024*1024) # automatically freed when pointer is GC'd
6n = 256
7n_threads = 4
8
9a = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_Q5_K, n)
10b = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F32, n) # can't both be quantized
11sum = lib.ggml_add(ctx, a, b) # all zeroes for now. Will be quantized too!
12
13# See cffi's doc on how to allocate native memory: it's very simple!
14# https://cffi.readthedocs.io/en/latest/ref.html#ffi-interface
15gf = ffi.new('struct ggml_cgraph*')
16lib.ggml_build_forward_expand(gf, sum)
17
18copy(np.array([i for i in range(n)], np.float32), a)
19copy(np.array([i*100 for i in range(n)], np.float32), b)
20
21lib.ggml_graph_compute_with_ctx(ctx, gf, n_threads)
22
23print(numpy(a, allow_copy=True))
24print(numpy(b))
25print(numpy(sum, allow_copy=True))