example_add_quant.py 853 B

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