test_unity_cpp.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import ggml
  2. import ctypes
  3. import torch
  4. import pytest
  5. import numpy as np
  6. from pathlib import Path
  7. from typing import Iterator
  8. from ggml import NativeObj
  9. Ctx = ggml.ggml_context_p
  10. UNITY_MODELS = Path(__file__).parent / "examples/unity/models"
  11. PARAMS_16MB = ggml.ggml_init_params(mem_size=16 * 1024 * 1024, mem_buffer=None)
  12. @pytest.fixture(name="ctx")
  13. def _ctx() -> Iterator[Ctx]:
  14. """Allocate a new context with 16 MB of memory"""
  15. try:
  16. ctx = ggml.ggml_init(params=PARAMS_16MB)
  17. yield ctx
  18. finally:
  19. ggml.ggml_free(ctx)
  20. def test_ggml_bindings_work(ctx: Ctx) -> None:
  21. # Instantiate tensors
  22. x = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 1)
  23. a = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 1)
  24. b = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 1)
  25. # Use ggml operations to build a computational graph
  26. x2 = ggml.ggml_mul(ctx, x, x)
  27. f = ggml.ggml_add(ctx, ggml.ggml_mul(ctx, a, x2), b)
  28. gf = ggml.ggml_build_forward(f)
  29. # Set the input values
  30. ggml.ggml_set_f32(x, 2.0)
  31. ggml.ggml_set_f32(a, 3.0)
  32. ggml.ggml_set_f32(b, 4.0)
  33. # Compute the graph
  34. ggml.ggml_graph_compute_with_ctx(ctx, ctypes.pointer(gf), 1)
  35. # Get the output value
  36. output = ggml.ggml_get_f32_1d(f, 0)
  37. assert output == 16.0
  38. def test_shape_works(ctx: Ctx) -> None:
  39. a = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 10)
  40. assert ggml.shape(a) == (10,)
  41. b = ggml.ggml_new_tensor_2d(ctx, ggml.GGML_TYPE_F32, 11, 21)
  42. assert ggml.shape(b) == (11, 21)
  43. c = ggml.ggml_new_tensor_3d(ctx, ggml.GGML_TYPE_F32, 12, 22, 32)
  44. assert ggml.shape(c) == (12, 22, 32)
  45. @pytest.mark.xfail(
  46. reason="TODO: understand diff between ggml strides and numpy strides"
  47. )
  48. def test_strides_works(ctx: Ctx) -> None:
  49. a = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 10)
  50. assert ggml.strides(a) == np.ones((10,), dtype=np.float32).strides
  51. b = ggml.ggml_new_tensor_2d(ctx, ggml.GGML_TYPE_F32, 11, 21)
  52. assert ggml.strides(b) == np.ones((11, 21), dtype=np.float32).strides
  53. c = ggml.ggml_new_tensor_3d(ctx, ggml.GGML_TYPE_F32, 12, 22, 32)
  54. assert ggml.strides(c) == np.ones((12, 22, 32), dtype=np.float32).strides
  55. def test_to_numpy_works_with_f32(ctx: Ctx) -> None:
  56. a = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F32, 10)
  57. a = ggml.ggml_set_f32(a, 2.14)
  58. assert np.allclose(ggml.to_numpy(a), np.ones((10,)) * 2.14)
  59. b = ggml.ggml_new_tensor_2d(ctx, ggml.GGML_TYPE_F32, 11, 21)
  60. assert np.allclose(ggml.to_numpy(b), np.zeros((11, 21)))
  61. c = ggml.ggml_new_tensor_3d(ctx, ggml.GGML_TYPE_F32, 12, 22, 32)
  62. assert np.allclose(ggml.to_numpy(c), np.zeros((12, 22, 32)))
  63. def test_from_numpy_works_with_f32(ctx: Ctx) -> None:
  64. a = np.random.normal(size=(10,)).astype(dtype=np.float32)
  65. ga = ggml.from_numpy(ctx, a)
  66. assert np.allclose(a, ggml.to_numpy(ga))
  67. a = np.random.normal(size=(11, 21)).astype(dtype=np.float32)
  68. ga = ggml.from_numpy(ctx, a)
  69. assert np.allclose(a, ggml.to_numpy(ga))
  70. a = np.random.normal(size=(12, 22, 32)).astype(dtype=np.float32)
  71. ga = ggml.from_numpy(ctx, a)
  72. assert np.allclose(a, ggml.to_numpy(ga))
  73. def test_to_numpy_works_with_f16(ctx: Ctx) -> None:
  74. # We explicitly fill the tensor otherwise they might have non-zero values in them.
  75. a = ggml.ggml_new_tensor_1d(ctx, ggml.GGML_TYPE_F16, 10)
  76. a = ggml.ggml_set_f32(a, 2.14)
  77. assert np.allclose(ggml.to_numpy(a), np.ones((10,), dtype=np.float16) * 2.14)
  78. b = ggml.ggml_new_tensor_2d(ctx, ggml.GGML_TYPE_F16, 11, 21)
  79. b = ggml.ggml_set_f32(b, 4.18)
  80. assert np.allclose(ggml.to_numpy(b), np.ones((11, 21), dtype=np.float16) * 4.18)
  81. c = ggml.ggml_new_tensor_3d(ctx, ggml.GGML_TYPE_F16, 12, 22, 32)
  82. c = ggml.ggml_set_f32(c, 3.16)
  83. assert np.allclose(ggml.to_numpy(c), np.ones((12, 22, 32), dtype=np.float16) * 3.16)
  84. def test_from_numpy_works_with_f16(ctx: Ctx) -> None:
  85. a = np.random.normal(size=(10,)).astype(dtype=np.float16)
  86. ga = ggml.from_numpy(ctx, a)
  87. assert np.allclose(a, ggml.to_numpy(ga))
  88. a = np.random.normal(size=(11, 21)).astype(dtype=np.float16)
  89. ga = ggml.from_numpy(ctx, a)
  90. assert np.allclose(a, ggml.to_numpy(ga))
  91. a = np.random.normal(size=(12, 22, 32)).astype(dtype=np.float16)
  92. ga = ggml.from_numpy(ctx, a)
  93. assert np.allclose(a, ggml.to_numpy(ga))
  94. def test_unity_model_load(ctx: Ctx) -> None:
  95. model, vocab = ggml.unity_model_load(
  96. UNITY_MODELS / "unity-large/ggml-model.bin"
  97. )
  98. print(model, vocab)
  99. example = ggml.from_file(ctx, UNITY_MODELS / "unity-large/seqs_before_conformer_block.bin", (1024, 137))
  100. with ggml.MeasureArena() as arena:
  101. graph = ggml.unity_audio_encoder_graph(model, example)
  102. # TODO: why the extra memory ?
  103. mem_size = ggml.ggml_allocr_alloc_graph(arena.ptr, graph) + ggml.GGML_MEM_ALIGN
  104. with ggml.FixedSizeArena(mem_size) as allocr:
  105. print(f"unity_audio_encoder_graph: compute buffer size: {mem_size/1024/1024} MB")
  106. eval_res_ptr = ggml.unity_eval(allocr, model, example, 1)
  107. eval_res = eval_res_ptr.contents
  108. inpL = ggml.to_numpy(eval_res.nodes[eval_res.n_nodes - 1])
  109. expected_raw = "-0.1308,0.0346,-0.2656,0.2873,-0.0104,0.0574,0.4033,-0.1125,-0.0460,-0.0496"
  110. expected = map(float, expected_raw.split(","))
  111. assert np.allclose(inpL[0, :10], list(expected), atol=1e-4)
  112. # def test_unity_model_load2(ctx: Ctx) -> None:
  113. # model = ggml.unity_model_load(
  114. # UNITY_MODELS / "unity-large/ggml-model.bin"
  115. # )
  116. # print(model, vocab)
  117. #
  118. #