test_unity_cpp.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import ggml
  2. import ctypes
  3. def test_ggml_bindings_work() -> None:
  4. # Allocate a new context with 16 MB of memory
  5. params = ggml.ggml_init_params(mem_size=16 * 1024 * 1024, mem_buffer=None)
  6. ctx = ggml.ggml_init(params=params)
  7. # Instantiate tensors
  8. x = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 1)
  9. a = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 1)
  10. b = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 1)
  11. # Use ggml operations to build a computational graph
  12. x2 = ggml.ggml_mul(ctx, x, x)
  13. f = ggml.ggml_add(ctx, ggml.ggml_mul(ctx, a, x2), b)
  14. gf = ggml.ggml_build_forward(f)
  15. # Set the input values
  16. ggml.ggml_set_f32(x, 2.0)
  17. ggml.ggml_set_f32(a, 3.0)
  18. ggml.ggml_set_f32(b, 4.0)
  19. # Compute the graph
  20. ggml.ggml_graph_compute_with_ctx(ctx, ctypes.pointer(gf), 1)
  21. # Get the output value
  22. output = ggml.ggml_get_f32_1d(f, 0)
  23. assert output == 16.0
  24. # Free the context
  25. ggml.ggml_free(ctx)
  26. def test_unity_model_load() -> None:
  27. model, vocab = ggml.unity_model_load(
  28. "examples/unity/models/unity-large/ggml-model.bin"
  29. )
  30. print(model, vocab)