ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / examples / python / test_tensor.py
| 1 | import pytest |
| 2 | from pytest import raises |
| 3 | |
| 4 | from ggml import lib, ffi |
| 5 | from ggml.utils import init, copy, numpy |
| 6 | import numpy as np |
| 7 | import numpy.testing as npt |
| 8 | |
| 9 | @pytest.fixture() |
| 10 | def ctx(): |
| 11 | print("setup") |
| 12 | yield init(mem_size=10*1024*1024) |
| 13 | print("teardown") |
| 14 | |
| 15 | class TestNumPy: |
| 16 | |
| 17 | # Single element |
| 18 | |
| 19 | def test_set_get_single_i32(self, ctx): |
| 20 | i = lib.ggml_new_i32(ctx, 42) |
| 21 | assert lib.ggml_get_i32_1d(i, 0) == 42 |
| 22 | assert numpy(i) == np.array([42], dtype=np.int32) |
| 23 | |
| 24 | def test_set_get_single_f32(self, ctx): |
| 25 | i = lib.ggml_new_f32(ctx, 4.2) |
| 26 | |
| 27 | epsilon = 0.000001 # Not sure why so large a difference?? |
| 28 | pytest.approx(lib.ggml_get_f32_1d(i, 0), 4.2, epsilon) |
| 29 | pytest.approx(numpy(i), np.array([4.2], dtype=np.float32), epsilon) |
| 30 | |
| 31 | def _test_copy_np_to_ggml(self, a: np.ndarray, t: ffi.CData): |
| 32 | a2 = a.copy() # Clone original |
| 33 | copy(a, t) |
| 34 | npt.assert_array_equal(numpy(t), a2) |
| 35 | |
| 36 | # I32 |
| 37 | |
| 38 | def test_copy_np_to_ggml_1d_i32(self, ctx): |
| 39 | t = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_I32, 10) |
| 40 | a = np.arange(10, dtype=np.int32) |
| 41 | self._test_copy_np_to_ggml(a, t) |
| 42 | |
| 43 | def test_copy_np_to_ggml_2d_i32(self, ctx): |
| 44 | t = lib.ggml_new_tensor_2d(ctx, lib.GGML_TYPE_I32, 2, 3) |
| 45 | a = np.arange(2 * 3, dtype=np.int32).reshape((2, 3)) |
| 46 | self._test_copy_np_to_ggml(a, t) |
| 47 | |
| 48 | def test_copy_np_to_ggml_3d_i32(self, ctx): |
| 49 | t = lib.ggml_new_tensor_3d(ctx, lib.GGML_TYPE_I32, 2, 3, 4) |
| 50 | a = np.arange(2 * 3 * 4, dtype=np.int32).reshape((2, 3, 4)) |
| 51 | self._test_copy_np_to_ggml(a, t) |
| 52 | |
| 53 | def test_copy_np_to_ggml_4d_i32(self, ctx): |
| 54 | t = lib.ggml_new_tensor_4d(ctx, lib.GGML_TYPE_I32, 2, 3, 4, 5) |
| 55 | a = np.arange(2 * 3 * 4 * 5, dtype=np.int32).reshape((2, 3, 4, 5)) |
| 56 | self._test_copy_np_to_ggml(a, t) |
| 57 | |
| 58 | def test_copy_np_to_ggml_4d_n_i32(self, ctx): |
| 59 | dims = [2, 3, 4, 5] # GGML_MAX_DIMS is 4, going beyond would crash |
| 60 | pdims = ffi.new('int64_t[]', len(dims)) |
| 61 | for i, d in enumerate(dims): pdims[i] = d |
| 62 | t = lib.ggml_new_tensor(ctx, lib.GGML_TYPE_I32, len(dims), pdims) |
| 63 | a = np.arange(np.prod(dims), dtype=np.int32).reshape(tuple(pdims)) |
| 64 | self._test_copy_np_to_ggml(a, t) |
| 65 | |
| 66 | # F32 |
| 67 | |
| 68 | def test_copy_np_to_ggml_1d_f32(self, ctx): |
| 69 | t = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F32, 10) |
| 70 | a = np.arange(10, dtype=np.float32) |
| 71 | self._test_copy_np_to_ggml(a, t) |
| 72 | |
| 73 | def test_copy_np_to_ggml_2d_f32(self, ctx): |
| 74 | t = lib.ggml_new_tensor_2d(ctx, lib.GGML_TYPE_F32, 2, 3) |
| 75 | a = np.arange(2 * 3, dtype=np.float32).reshape((2, 3)) |
| 76 | self._test_copy_np_to_ggml(a, t) |
| 77 | |
| 78 | def test_copy_np_to_ggml_3d_f32(self, ctx): |
| 79 | t = lib.ggml_new_tensor_3d(ctx, lib.GGML_TYPE_F32, 2, 3, 4) |
| 80 | a = np.arange(2 * 3 * 4, dtype=np.float32).reshape((2, 3, 4)) |
| 81 | self._test_copy_np_to_ggml(a, t) |
| 82 | |
| 83 | def test_copy_np_to_ggml_4d_f32(self, ctx): |
| 84 | t = lib.ggml_new_tensor_4d(ctx, lib.GGML_TYPE_F32, 2, 3, 4, 5) |
| 85 | a = np.arange(2 * 3 * 4 * 5, dtype=np.float32).reshape((2, 3, 4, 5)) |
| 86 | self._test_copy_np_to_ggml(a, t) |
| 87 | |
| 88 | def test_copy_np_to_ggml_4d_n_f32(self, ctx): |
| 89 | dims = [2, 3, 4, 5] # GGML_MAX_DIMS is 4, going beyond would crash |
| 90 | pdims = ffi.new('int64_t[]', len(dims)) |
| 91 | for i, d in enumerate(dims): pdims[i] = d |
| 92 | t = lib.ggml_new_tensor(ctx, lib.GGML_TYPE_F32, len(dims), pdims) |
| 93 | a = np.arange(np.prod(dims), dtype=np.float32).reshape(tuple(pdims)) |
| 94 | self._test_copy_np_to_ggml(a, t) |
| 95 | |
| 96 | # F16 |
| 97 | |
| 98 | def test_copy_np_to_ggml_1d_f16(self, ctx): |
| 99 | t = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F16, 10) |
| 100 | a = np.arange(10, dtype=np.float16) |
| 101 | self._test_copy_np_to_ggml(a, t) |
| 102 | |
| 103 | def test_copy_np_to_ggml_2d_f16(self, ctx): |
| 104 | t = lib.ggml_new_tensor_2d(ctx, lib.GGML_TYPE_F16, 2, 3) |
| 105 | a = np.arange(2 * 3, dtype=np.float16).reshape((2, 3)) |
| 106 | self._test_copy_np_to_ggml(a, t) |
| 107 | |
| 108 | def test_copy_np_to_ggml_3d_f16(self, ctx): |
| 109 | t = lib.ggml_new_tensor_3d(ctx, lib.GGML_TYPE_F16, 2, 3, 4) |
| 110 | a = np.arange(2 * 3 * 4, dtype=np.float16).reshape((2, 3, 4)) |
| 111 | self._test_copy_np_to_ggml(a, t) |
| 112 | |
| 113 | def test_copy_np_to_ggml_4d_f16(self, ctx): |
| 114 | t = lib.ggml_new_tensor_4d(ctx, lib.GGML_TYPE_F16, 2, 3, 4, 5) |
| 115 | a = np.arange(2 * 3 * 4 * 5, dtype=np.float16).reshape((2, 3, 4, 5)) |
| 116 | self._test_copy_np_to_ggml(a, t) |
| 117 | |
| 118 | def test_copy_np_to_ggml_4d_n_f16(self, ctx): |
| 119 | dims = [2, 3, 4, 5] # GGML_MAX_DIMS is 4, going beyond would crash |
| 120 | pdims = ffi.new('int64_t[]', len(dims)) |
| 121 | for i, d in enumerate(dims): pdims[i] = d |
| 122 | t = lib.ggml_new_tensor(ctx, lib.GGML_TYPE_F16, len(dims), pdims) |
| 123 | a = np.arange(np.prod(dims), dtype=np.float16).reshape(tuple(pdims)) |
| 124 | self._test_copy_np_to_ggml(a, t) |
| 125 | |
| 126 | # Mismatching shapes |
| 127 | |
| 128 | def test_copy_mismatching_shapes_1d(self, ctx): |
| 129 | t = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F32, 10) |
| 130 | a = np.arange(10, dtype=np.float32) |
| 131 | copy(a, t) # OK |
| 132 | |
| 133 | a = a.reshape((5, 2)) |
| 134 | with raises(AssertionError): copy(a, t) |
| 135 | with raises(AssertionError): copy(t, a) |
| 136 | |
| 137 | def test_copy_mismatching_shapes_2d(self, ctx): |
| 138 | t = lib.ggml_new_tensor_2d(ctx, lib.GGML_TYPE_F32, 2, 3) |
| 139 | a = np.arange(6, dtype=np.float32) |
| 140 | copy(a.reshape((2, 3)), t) # OK |
| 141 | |
| 142 | a = a.reshape((3, 2)) |
| 143 | with raises(AssertionError): copy(a, t) |
| 144 | with raises(AssertionError): copy(t, a) |
| 145 | |
| 146 | def test_copy_mismatching_shapes_3d(self, ctx): |
| 147 | t = lib.ggml_new_tensor_3d(ctx, lib.GGML_TYPE_F32, 2, 3, 4) |
| 148 | a = np.arange(24, dtype=np.float32) |
| 149 | copy(a.reshape((2, 3, 4)), t) # OK |
| 150 | |
| 151 | a = a.reshape((2, 4, 3)) |
| 152 | with raises(AssertionError): copy(a, t) |
| 153 | with raises(AssertionError): copy(t, a) |
| 154 | |
| 155 | def test_copy_mismatching_shapes_4d(self, ctx): |
| 156 | t = lib.ggml_new_tensor_4d(ctx, lib.GGML_TYPE_F32, 2, 3, 4, 5) |
| 157 | a = np.arange(24*5, dtype=np.float32) |
| 158 | copy(a.reshape((2, 3, 4, 5)), t) # OK |
| 159 | |
| 160 | a = a.reshape((2, 3, 5, 4)) |
| 161 | with raises(AssertionError): copy(a, t) |
| 162 | with raises(AssertionError): copy(t, a) |
| 163 | |
| 164 | def test_copy_f16_to_f32(self, ctx): |
| 165 | t = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F32, 1) |
| 166 | a = np.array([123.45], dtype=np.float16) |
| 167 | copy(a, t) |
| 168 | np.testing.assert_allclose(lib.ggml_get_f32_1d(t, 0), 123.45, rtol=1e-3) |
| 169 | |
| 170 | def test_copy_f32_to_f16(self, ctx): |
| 171 | t = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F16, 1) |
| 172 | a = np.array([123.45], dtype=np.float32) |
| 173 | copy(a, t) |
| 174 | np.testing.assert_allclose(lib.ggml_get_f32_1d(t, 0), 123.45, rtol=1e-3) |
| 175 | |
| 176 | def test_copy_f16_to_Q5_K(self, ctx): |
| 177 | n = 256 |
| 178 | t = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_Q5_K, n) |
| 179 | a = np.arange(n, dtype=np.float16) |
| 180 | copy(a, t) |
| 181 | np.testing.assert_allclose(a, numpy(t, allow_copy=True), rtol=0.05) |
| 182 | |
| 183 | def test_copy_Q5_K_to_f16(self, ctx): |
| 184 | n = 256 |
| 185 | t = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_Q5_K, n) |
| 186 | copy(np.arange(n, dtype=np.float32), t) |
| 187 | a = np.arange(n, dtype=np.float16) |
| 188 | copy(t, a) |
| 189 | np.testing.assert_allclose(a, numpy(t, allow_copy=True), rtol=0.05) |
| 190 | |
| 191 | def test_copy_i16_f32_mismatching_types(self, ctx): |
| 192 | t = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F32, 1) |
| 193 | a = np.arange(1, dtype=np.int16) |
| 194 | with raises(NotImplementedError): copy(a, t) |
| 195 | with raises(NotImplementedError): copy(t, a) |
| 196 | |
| 197 | class TestTensorCopy: |
| 198 | |
| 199 | def test_copy_self(self, ctx): |
| 200 | t = lib.ggml_new_i32(ctx, 42) |
| 201 | copy(t, t) |
| 202 | assert lib.ggml_get_i32_1d(t, 0) == 42 |
| 203 | |
| 204 | def test_copy_1d(self, ctx): |
| 205 | t1 = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F32, 10) |
| 206 | t2 = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F32, 10) |
| 207 | a = np.arange(10, dtype=np.float32) |
| 208 | copy(a, t1) |
| 209 | copy(t1, t2) |
| 210 | assert np.allclose(a, numpy(t2)) |
| 211 | assert np.allclose(numpy(t1), numpy(t2)) |
| 212 | |
| 213 | class TestGraph: |
| 214 | |
| 215 | def test_add(self, ctx): |
| 216 | n = 256 |
| 217 | ta = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F32, n) |
| 218 | tb = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F32, n) |
| 219 | tsum = lib.ggml_add(ctx, ta, tb) |
| 220 | assert tsum.type == lib.GGML_TYPE_F32 |
| 221 | |
| 222 | gf = ffi.new('struct ggml_cgraph*') |
| 223 | lib.ggml_build_forward_expand(gf, tsum) |
| 224 | |
| 225 | a = np.arange(0, n, dtype=np.float32) |
| 226 | b = np.arange(n, 0, -1, dtype=np.float32) |
| 227 | copy(a, ta) |
| 228 | copy(b, tb) |
| 229 | |
| 230 | lib.ggml_graph_compute_with_ctx(ctx, gf, 1) |
| 231 | |
| 232 | assert np.allclose(numpy(tsum, allow_copy=True), a + b) |
| 233 | |
| 234 | class TestQuantization: |
| 235 | |
| 236 | def test_quantized_add(self, ctx): |
| 237 | n = 256 |
| 238 | ta = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_Q5_K, n) |
| 239 | tb = lib.ggml_new_tensor_1d(ctx, lib.GGML_TYPE_F32, n) |
| 240 | tsum = lib.ggml_add(ctx, ta, tb) |
| 241 | assert tsum.type == lib.GGML_TYPE_Q5_K |
| 242 | |
| 243 | gf = ffi.new('struct ggml_cgraph*') |
| 244 | lib.ggml_build_forward_expand(gf, tsum) |
| 245 | |
| 246 | a = np.arange(0, n, dtype=np.float32) |
| 247 | b = np.arange(n, 0, -1, dtype=np.float32) |
| 248 | copy(a, ta) |
| 249 | copy(b, tb) |
| 250 | |
| 251 | lib.ggml_graph_compute_with_ctx(ctx, gf, 1) |
| 252 | |
| 253 | unquantized_sum = a + b |
| 254 | sum = numpy(tsum, allow_copy=True) |
| 255 | |
| 256 | diff = np.linalg.norm(unquantized_sum - sum, np.inf) |
| 257 | assert diff > 4 |
| 258 | assert diff < 5 |