Loads delegate from the shared library. Args: library: Shared library name. options: Dictionary of options that are required to load the delegate. All keys and values in the dictionary should be serializable. Consult the documentation of the specific delegate for req
(self, library, options=None)
| 68 | """ |
| 69 | |
| 70 | def __init__(self, library, options=None): |
| 71 | """Loads delegate from the shared library. |
| 72 | |
| 73 | Args: |
| 74 | library: Shared library name. |
| 75 | options: Dictionary of options that are required to load the delegate. All |
| 76 | keys and values in the dictionary should be serializable. Consult the |
| 77 | documentation of the specific delegate for required and legal options. |
| 78 | (default None) |
| 79 | Raises: |
| 80 | RuntimeError: This is raised if the Python implementation is not CPython. |
| 81 | """ |
| 82 | |
| 83 | # TODO(b/136468453): Remove need for __del__ ordering needs of CPython |
| 84 | # by using explicit closes(). See implementation of Interpreter __del__. |
| 85 | if platform.python_implementation() != 'CPython': |
| 86 | raise RuntimeError('Delegates are currently only supported into CPython' |
| 87 | 'due to missing immediate reference counting.') |
| 88 | |
| 89 | self._library = ctypes.pydll.LoadLibrary(library) |
| 90 | self._library.tflite_plugin_create_delegate.argtypes = [ |
| 91 | ctypes.POINTER(ctypes.c_char_p), |
| 92 | ctypes.POINTER(ctypes.c_char_p), ctypes.c_int, |
| 93 | ctypes.CFUNCTYPE(None, ctypes.c_char_p) |
| 94 | ] |
| 95 | self._library.tflite_plugin_create_delegate.restype = ctypes.c_void_p |
| 96 | |
| 97 | # Convert the options from a dictionary to lists of char pointers. |
| 98 | options = options or {} |
| 99 | options_keys = (ctypes.c_char_p * len(options))() |
| 100 | options_values = (ctypes.c_char_p * len(options))() |
| 101 | for idx, (key, value) in enumerate(options.items()): |
| 102 | options_keys[idx] = str(key).encode('utf-8') |
| 103 | options_values[idx] = str(value).encode('utf-8') |
| 104 | |
| 105 | class ErrorMessageCapture(object): |
| 106 | |
| 107 | def __init__(self): |
| 108 | self.message = '' |
| 109 | |
| 110 | def report(self, x): |
| 111 | self.message += x |
| 112 | |
| 113 | capture = ErrorMessageCapture() |
| 114 | error_capturer_cb = ctypes.CFUNCTYPE(None, ctypes.c_char_p)(capture.report) |
| 115 | # Do not make a copy of _delegate_ptr. It is freed by Delegate's finalizer. |
| 116 | self._delegate_ptr = self._library.tflite_plugin_create_delegate( |
| 117 | options_keys, options_values, len(options), error_capturer_cb) |
| 118 | if self._delegate_ptr is None: |
| 119 | raise ValueError(capture.message) |
| 120 | |
| 121 | def __del__(self): |
| 122 | # __del__ can be called multiple times, so if the delegate is destroyed. |
nothing calls this directly
no test coverage detected