Skip to content

Commit

Permalink
Handle SIGABRT from GGML (#30)
Browse files Browse the repository at this point in the history
* Handle SIGABRT from GGML

* Add comment

* Move handler to utils

---------

Co-authored-by: Andrei Betlen <[email protected]>
  • Loading branch information
dmille and abetlen authored Feb 11, 2024
1 parent a40058b commit d9d5ab6
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions ggml/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"""Utility functions for ggml-python.
"""

import enum
import ctypes
import signal
import platform
import traceback

from typing import Any, Optional, Sequence, Tuple

Expand Down Expand Up @@ -52,7 +56,7 @@ def to_numpy(
ctypes_type = ctypes.c_uint16
else:
ctypes_type = np.ctypeslib.as_ctypes_type(GGML_TYPE_TO_NUMPY_DTYPE[ggml_type])

data = ggml.ggml_get_data(tensor)
if data is None:
raise ValueError("tensor data is None")
Expand All @@ -61,7 +65,7 @@ def to_numpy(
shape = tuple(reversed(tensor.contents.ne[:n_dims]))
output = np.ctypeslib.as_array(array, shape=shape)
if ggml_type == GGML_TYPE.F16:
output.dtype = np.float16 # type: ignore
output.dtype = np.float16 # type: ignore
return np.lib.stride_tricks.as_strided(
output, strides=tuple(reversed(tensor.contents.nb[:n_dims]))
)
Expand Down Expand Up @@ -312,3 +316,18 @@ def slice_tensor(
f"ggml tensors with {ndims} dimensions are not supported"
)


def setup_sigabrt_handler():
if platform.system() == "Windows":
c_globals = ctypes.windll.kernel32
signal_type = signal.CTRL_C_EVENT
else:
c_globals = ctypes.CDLL(None) # POSIX
signal_type = signal.SIGABRT

@ctypes.CFUNCTYPE(None, ctypes.c_int)
def sigabrt_handler(sig):
traceback.print_stack()
raise Exception("GGML SIGABRT")

c_globals.signal(signal_type, sigabrt_handler)

0 comments on commit d9d5ab6

Please sign in to comment.