diff --git a/facefusion/libraries/amd_smi.py b/facefusion/libraries/amd_smi.py index 8ca39886..f78c8708 100644 --- a/facefusion/libraries/amd_smi.py +++ b/facefusion/libraries/amd_smi.py @@ -16,12 +16,62 @@ def create_static_library() -> Optional[ctypes.CDLL]: library_file = resolve_library_file() if library_file: - amd_smi_library = ctypes.CDLL(library_file) - return init_ctypes(amd_smi_library) + library = ctypes.CDLL(library_file) + + if library: + return init_ctypes(library) return None +def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL: + library.amdsmi_init.argtypes = [ ctypes.c_uint64 ] + library.amdsmi_init.restype = ctypes.c_uint32 + + library.amdsmi_shut_down.argtypes = [] + library.amdsmi_shut_down.restype = ctypes.c_uint32 + + library.amdsmi_get_socket_handles.argtypes = [ ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p) ] + library.amdsmi_get_socket_handles.restype = ctypes.c_uint32 + + library.amdsmi_get_processor_handles.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p) ] + library.amdsmi_get_processor_handles.restype = ctypes.c_uint32 + + library.amdsmi_get_gpu_vram_usage.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ] + library.amdsmi_get_gpu_vram_usage.restype = ctypes.c_uint32 + + library.amdsmi_get_gpu_activity.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ] + library.amdsmi_get_gpu_activity.restype = ctypes.c_uint32 + + library.amdsmi_get_gpu_asic_info.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ] + library.amdsmi_get_gpu_asic_info.restype = ctypes.c_uint32 + + library.amdsmi_get_temp_metric.argtypes = [ ctypes.c_void_p, ctypes.c_uint32, ctypes.c_uint32, ctypes.POINTER(ctypes.c_int64) ] + library.amdsmi_get_temp_metric.restype = ctypes.c_uint32 + + return library + + +def find_device_handles(amd_smi_library : ctypes.CDLL) -> List[ctypes.c_void_p]: + device_handles : List[ctypes.c_void_p] = [] + + socket_count = ctypes.c_uint32() + amd_smi_library.amdsmi_get_socket_handles(ctypes.byref(socket_count), ctypes.POINTER(ctypes.c_void_p)()) + socket_handles = (ctypes.c_void_p * socket_count.value)() + amd_smi_library.amdsmi_get_socket_handles(ctypes.byref(socket_count), socket_handles) + + for socket_index in range(socket_count.value): + device_count = ctypes.c_uint32() + amd_smi_library.amdsmi_get_processor_handles(socket_handles[socket_index], ctypes.byref(device_count), ctypes.POINTER(ctypes.c_void_p)()) + processor_handles = (ctypes.c_void_p * device_count.value)() + amd_smi_library.amdsmi_get_processor_handles(socket_handles[socket_index], ctypes.byref(device_count), processor_handles) + + for device_index in range(device_count.value): + device_handles.append(ctypes.c_void_p(processor_handles[device_index])) + + return device_handles + + def define_product_info() -> ctypes.Structure: return type('AMDSMI_ASIC_INFO', (ctypes.Structure,), { @@ -70,51 +120,3 @@ def define_device_utilization() -> ctypes.Structure: ('reserved', ctypes.c_uint32 * 13) ] })() - - -def init_ctypes(amd_smi : ctypes.CDLL) -> ctypes.CDLL: - amd_smi.amdsmi_init.argtypes = [ ctypes.c_uint64 ] - amd_smi.amdsmi_init.restype = ctypes.c_uint32 - - amd_smi.amdsmi_shut_down.argtypes = [] - amd_smi.amdsmi_shut_down.restype = ctypes.c_uint32 - - amd_smi.amdsmi_get_socket_handles.argtypes = [ ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p) ] - amd_smi.amdsmi_get_socket_handles.restype = ctypes.c_uint32 - - amd_smi.amdsmi_get_processor_handles.argtypes = [ ctypes.c_void_p, ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p) ] - amd_smi.amdsmi_get_processor_handles.restype = ctypes.c_uint32 - - amd_smi.amdsmi_get_gpu_vram_usage.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ] - amd_smi.amdsmi_get_gpu_vram_usage.restype = ctypes.c_uint32 - - amd_smi.amdsmi_get_gpu_activity.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ] - amd_smi.amdsmi_get_gpu_activity.restype = ctypes.c_uint32 - - amd_smi.amdsmi_get_gpu_asic_info.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ] - amd_smi.amdsmi_get_gpu_asic_info.restype = ctypes.c_uint32 - - amd_smi.amdsmi_get_temp_metric.argtypes = [ ctypes.c_void_p, ctypes.c_uint32, ctypes.c_uint32, ctypes.POINTER(ctypes.c_int64) ] - amd_smi.amdsmi_get_temp_metric.restype = ctypes.c_uint32 - - return amd_smi - - -def find_device_handles(amd_smi_library : ctypes.CDLL) -> List[ctypes.c_void_p]: - device_handles : List[ctypes.c_void_p] = [] - - socket_count = ctypes.c_uint32() - amd_smi_library.amdsmi_get_socket_handles(ctypes.byref(socket_count), ctypes.POINTER(ctypes.c_void_p)()) - socket_handles = (ctypes.c_void_p * socket_count.value)() - amd_smi_library.amdsmi_get_socket_handles(ctypes.byref(socket_count), socket_handles) - - for socket_index in range(socket_count.value): - device_count = ctypes.c_uint32() - amd_smi_library.amdsmi_get_processor_handles(socket_handles[socket_index], ctypes.byref(device_count), ctypes.POINTER(ctypes.c_void_p)()) - processor_handles = (ctypes.c_void_p * device_count.value)() - amd_smi_library.amdsmi_get_processor_handles(socket_handles[socket_index], ctypes.byref(device_count), processor_handles) - - for device_index in range(device_count.value): - device_handles.append(ctypes.c_void_p(processor_handles[device_index])) - - return device_handles diff --git a/facefusion/libraries/datachannel.py b/facefusion/libraries/datachannel.py index e24f18b8..bd599579 100644 --- a/facefusion/libraries/datachannel.py +++ b/facefusion/libraries/datachannel.py @@ -7,6 +7,16 @@ from facefusion.filesystem import resolve_relative_path from facefusion.types import DownloadSet +def resolve_library_file() -> Optional[str]: + if is_linux(): + return 'linux-x64-openssl-h264-vp8-av1-opus-libdatachannel-0.24.1.so' + if is_macos(): + return 'macos-universal-openssl-h264-vp8-av1-opus-libdatachannel-0.24.1.dylib' + if is_windows(): + return 'windows-x64-openssl-h264-vp8-av1-opus-datachannel-0.24.1.dll' + return None + + @lru_cache def create_static_library_set() -> Dict[str, DownloadSet]: library_file = resolve_library_file() @@ -32,16 +42,78 @@ def create_static_library_set() -> Dict[str, DownloadSet]: } -def resolve_library_file() -> Optional[str]: - if is_linux(): - return 'linux-x64-openssl-h264-vp8-av1-opus-libdatachannel-0.24.1.so' - if is_macos(): - return 'macos-universal-openssl-h264-vp8-av1-opus-libdatachannel-0.24.1.dylib' - if is_windows(): - return 'windows-x64-openssl-h264-vp8-av1-opus-datachannel-0.24.1.dll' +@lru_cache +def create_static_library() -> Optional[ctypes.CDLL]: + library_path = create_static_library_set().get('sources').get('datachannel').get('path') + + if library_path: + library = ctypes.CDLL(library_path) + + if library: + return init_ctypes(library) + return None +def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL: + library.rtcInitLogger.argtypes = [ ctypes.c_int, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p) ] + library.rtcInitLogger.restype = None + library.rtcInitLogger(4, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p)(0)) + + library.rtcCreatePeerConnection.restype = ctypes.c_int + + library.rtcDeletePeerConnection.argtypes = [ ctypes.c_int ] + library.rtcDeletePeerConnection.restype = ctypes.c_int + + library.rtcSetLocalDescription.argtypes = [ ctypes.c_int, ctypes.c_char_p ] + library.rtcSetLocalDescription.restype = ctypes.c_int + + library.rtcSetRemoteDescription.argtypes = [ ctypes.c_int, ctypes.c_char_p, ctypes.c_char_p ] + library.rtcSetRemoteDescription.restype = ctypes.c_int + + library.rtcAddTrack.argtypes = [ ctypes.c_int, ctypes.c_char_p ] + library.rtcAddTrack.restype = ctypes.c_int + + library.rtcSendMessage.argtypes = [ ctypes.c_int, ctypes.c_void_p, ctypes.c_int ] + library.rtcSendMessage.restype = ctypes.c_int + + library.rtcSetVP8Packetizer.restype = ctypes.c_int + + library.rtcChainRtcpSrReporter.argtypes = [ ctypes.c_int ] + library.rtcChainRtcpSrReporter.restype = ctypes.c_int + + library.rtcSetTrackRtpTimestamp.argtypes = [ ctypes.c_int, ctypes.c_uint32 ] + library.rtcSetTrackRtpTimestamp.restype = ctypes.c_int + + library.rtcIsOpen.argtypes = [ ctypes.c_int ] + library.rtcIsOpen.restype = ctypes.c_bool + + library.rtcChainRtcpNackResponder.argtypes = [ ctypes.c_int, ctypes.c_uint ] + library.rtcChainRtcpNackResponder.restype = ctypes.c_int + + library.rtcGetLocalDescription.argtypes = [ ctypes.c_int, ctypes.c_char_p, ctypes.c_int ] + library.rtcGetLocalDescription.restype = ctypes.c_int + + library.rtcSetLocalDescription.argtypes = [ ctypes.c_int, ctypes.c_char_p ] + library.rtcSetLocalDescription.restype = ctypes.c_int + + library.rtcSetOpusPacketizer.restype = ctypes.c_int + + library.rtcSetUserPointer.argtypes = [ ctypes.c_int, ctypes.c_void_p ] + library.rtcSetUserPointer.restype = None + + library.rtcSetLocalDescriptionCallback.argtypes = [ ctypes.c_int, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_void_p) ] + library.rtcSetLocalDescriptionCallback.restype = ctypes.c_int + + library.rtcSetGatheringStateChangeCallback.argtypes = [ ctypes.c_int, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_int, ctypes.c_void_p) ] + library.rtcSetGatheringStateChangeCallback.restype = ctypes.c_int + + library.rtcSetStateChangeCallback.argtypes = [ ctypes.c_int, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_int, ctypes.c_void_p) ] + library.rtcSetStateChangeCallback.restype = ctypes.c_int + + return library + + def define_rtc_configuration() -> ctypes.Structure: return type('RTC_CONFIGURATION', (ctypes.Structure,), { @@ -79,73 +151,3 @@ def define_rtc_packetizer_init() -> ctypes.Structure: ('maxFragmentSize', ctypes.c_uint16) ] })() - - -@lru_cache -def create_static_library() -> Optional[ctypes.CDLL]: - library_path = create_static_library_set().get('sources').get('datachannel').get('path') - - if library_path: - datachannel_library = ctypes.CDLL(library_path) - return init_ctypes(datachannel_library) - - return None - - -def init_ctypes(datachannel_library : ctypes.CDLL) -> ctypes.CDLL: - datachannel_library.rtcInitLogger.argtypes = [ ctypes.c_int, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p) ] - datachannel_library.rtcInitLogger.restype = None - datachannel_library.rtcInitLogger(4, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p)(0)) - - datachannel_library.rtcCreatePeerConnection.restype = ctypes.c_int - - datachannel_library.rtcDeletePeerConnection.argtypes = [ ctypes.c_int ] - datachannel_library.rtcDeletePeerConnection.restype = ctypes.c_int - - datachannel_library.rtcSetLocalDescription.argtypes = [ ctypes.c_int, ctypes.c_char_p ] - datachannel_library.rtcSetLocalDescription.restype = ctypes.c_int - - datachannel_library.rtcSetRemoteDescription.argtypes = [ ctypes.c_int, ctypes.c_char_p, ctypes.c_char_p ] - datachannel_library.rtcSetRemoteDescription.restype = ctypes.c_int - - datachannel_library.rtcAddTrack.argtypes = [ ctypes.c_int, ctypes.c_char_p ] - datachannel_library.rtcAddTrack.restype = ctypes.c_int - - datachannel_library.rtcSendMessage.argtypes = [ ctypes.c_int, ctypes.c_void_p, ctypes.c_int ] - datachannel_library.rtcSendMessage.restype = ctypes.c_int - - datachannel_library.rtcSetVP8Packetizer.restype = ctypes.c_int - - datachannel_library.rtcChainRtcpSrReporter.argtypes = [ ctypes.c_int ] - datachannel_library.rtcChainRtcpSrReporter.restype = ctypes.c_int - - datachannel_library.rtcSetTrackRtpTimestamp.argtypes = [ ctypes.c_int, ctypes.c_uint32 ] - datachannel_library.rtcSetTrackRtpTimestamp.restype = ctypes.c_int - - datachannel_library.rtcIsOpen.argtypes = [ ctypes.c_int ] - datachannel_library.rtcIsOpen.restype = ctypes.c_bool - - datachannel_library.rtcChainRtcpNackResponder.argtypes = [ ctypes.c_int, ctypes.c_uint ] - datachannel_library.rtcChainRtcpNackResponder.restype = ctypes.c_int - - datachannel_library.rtcGetLocalDescription.argtypes = [ ctypes.c_int, ctypes.c_char_p, ctypes.c_int ] - datachannel_library.rtcGetLocalDescription.restype = ctypes.c_int - - datachannel_library.rtcSetLocalDescription.argtypes = [ ctypes.c_int, ctypes.c_char_p ] - datachannel_library.rtcSetLocalDescription.restype = ctypes.c_int - - datachannel_library.rtcSetOpusPacketizer.restype = ctypes.c_int - - datachannel_library.rtcSetUserPointer.argtypes = [ ctypes.c_int, ctypes.c_void_p ] - datachannel_library.rtcSetUserPointer.restype = None - - datachannel_library.rtcSetLocalDescriptionCallback.argtypes = [ ctypes.c_int, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_void_p) ] - datachannel_library.rtcSetLocalDescriptionCallback.restype = ctypes.c_int - - datachannel_library.rtcSetGatheringStateChangeCallback.argtypes = [ ctypes.c_int, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_int, ctypes.c_void_p) ] - datachannel_library.rtcSetGatheringStateChangeCallback.restype = ctypes.c_int - - datachannel_library.rtcSetStateChangeCallback.argtypes = [ ctypes.c_int, ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_int, ctypes.c_void_p) ] - datachannel_library.rtcSetStateChangeCallback.restype = ctypes.c_int - - return datachannel_library diff --git a/facefusion/libraries/nvidia_ml.py b/facefusion/libraries/nvidia_ml.py index 82a9b35a..32544cc2 100644 --- a/facefusion/libraries/nvidia_ml.py +++ b/facefusion/libraries/nvidia_ml.py @@ -18,12 +18,62 @@ def create_static_library() -> Optional[ctypes.CDLL]: library_file = resolve_library_file() if library_file: - nvml_library = ctypes.CDLL(library_file) - return init_ctypes(nvml_library) + library = ctypes.CDLL(library_file) + + if library: + return init_ctypes(library) return None +def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL: + library.nvmlInit_v2.argtypes = [] + library.nvmlInit_v2.restype = ctypes.c_int + + library.nvmlShutdown.argtypes = [] + library.nvmlShutdown.restype = ctypes.c_int + + library.nvmlDeviceGetCount_v2.argtypes = [ ctypes.POINTER(ctypes.c_uint) ] + library.nvmlDeviceGetCount_v2.restype = ctypes.c_int + + library.nvmlSystemGetDriverVersion.argtypes = [ ctypes.c_char_p, ctypes.c_uint ] + library.nvmlSystemGetDriverVersion.restype = ctypes.c_int + + library.nvmlSystemGetCudaDriverVersion.argtypes = [ ctypes.POINTER(ctypes.c_int) ] + library.nvmlSystemGetCudaDriverVersion.restype = ctypes.c_int + + library.nvmlDeviceGetHandleByIndex_v2.argtypes = [ ctypes.c_uint, ctypes.POINTER(ctypes.c_void_p) ] + library.nvmlDeviceGetHandleByIndex_v2.restype = ctypes.c_int + + library.nvmlDeviceGetName.argtypes = [ ctypes.c_void_p, ctypes.c_char_p, ctypes.c_uint ] + library.nvmlDeviceGetName.restype = ctypes.c_int + + library.nvmlDeviceGetMemoryInfo.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ] + library.nvmlDeviceGetMemoryInfo.restype = ctypes.c_int + + library.nvmlDeviceGetTemperature.argtypes = [ ctypes.c_void_p, ctypes.c_int, ctypes.POINTER(ctypes.c_uint) ] + library.nvmlDeviceGetTemperature.restype = ctypes.c_int + + library.nvmlDeviceGetUtilizationRates.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ] + library.nvmlDeviceGetUtilizationRates.restype = ctypes.c_int + + return library + + +def find_device_handles(nvidia_ml_library : ctypes.CDLL) -> List[ctypes.c_void_p]: + device_handles : List[ctypes.c_void_p] = [] + + device_count = ctypes.c_uint() + nvidia_ml_library.nvmlDeviceGetCount_v2(ctypes.byref(device_count)) + + for device_id in range(device_count.value): + device_handle = ctypes.c_void_p() + nvidia_ml_library.nvmlDeviceGetHandleByIndex_v2(device_id, ctypes.byref(device_handle)) + device_handles.append(device_handle) + + return device_handles + + def define_device_memory() -> ctypes.Structure: return type('NVML_MEMORY', (ctypes.Structure,), { @@ -45,51 +95,3 @@ def define_device_utilization() -> ctypes.Structure: ('memory', ctypes.c_uint) ] })() - - -def init_ctypes(nvidia_ml : ctypes.CDLL) -> ctypes.CDLL: - nvidia_ml.nvmlInit_v2.argtypes = [] - nvidia_ml.nvmlInit_v2.restype = ctypes.c_int - - nvidia_ml.nvmlShutdown.argtypes = [] - nvidia_ml.nvmlShutdown.restype = ctypes.c_int - - nvidia_ml.nvmlDeviceGetCount_v2.argtypes = [ ctypes.POINTER(ctypes.c_uint) ] - nvidia_ml.nvmlDeviceGetCount_v2.restype = ctypes.c_int - - nvidia_ml.nvmlSystemGetDriverVersion.argtypes = [ ctypes.c_char_p, ctypes.c_uint ] - nvidia_ml.nvmlSystemGetDriverVersion.restype = ctypes.c_int - - nvidia_ml.nvmlSystemGetCudaDriverVersion.argtypes = [ ctypes.POINTER(ctypes.c_int) ] - nvidia_ml.nvmlSystemGetCudaDriverVersion.restype = ctypes.c_int - - nvidia_ml.nvmlDeviceGetHandleByIndex_v2.argtypes = [ ctypes.c_uint, ctypes.POINTER(ctypes.c_void_p) ] - nvidia_ml.nvmlDeviceGetHandleByIndex_v2.restype = ctypes.c_int - - nvidia_ml.nvmlDeviceGetName.argtypes = [ ctypes.c_void_p, ctypes.c_char_p, ctypes.c_uint ] - nvidia_ml.nvmlDeviceGetName.restype = ctypes.c_int - - nvidia_ml.nvmlDeviceGetMemoryInfo.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ] - nvidia_ml.nvmlDeviceGetMemoryInfo.restype = ctypes.c_int - - nvidia_ml.nvmlDeviceGetTemperature.argtypes = [ ctypes.c_void_p, ctypes.c_int, ctypes.POINTER(ctypes.c_uint) ] - nvidia_ml.nvmlDeviceGetTemperature.restype = ctypes.c_int - - nvidia_ml.nvmlDeviceGetUtilizationRates.argtypes = [ ctypes.c_void_p, ctypes.c_void_p ] - nvidia_ml.nvmlDeviceGetUtilizationRates.restype = ctypes.c_int - - return nvidia_ml - - -def find_device_handles(nvidia_ml_library : ctypes.CDLL) -> List[ctypes.c_void_p]: - device_handles : List[ctypes.c_void_p] = [] - - device_count = ctypes.c_uint() - nvidia_ml_library.nvmlDeviceGetCount_v2(ctypes.byref(device_count)) - - for device_id in range(device_count.value): - device_handle = ctypes.c_void_p() - nvidia_ml_library.nvmlDeviceGetHandleByIndex_v2(device_id, ctypes.byref(device_handle)) - device_handles.append(device_handle) - - return device_handles diff --git a/facefusion/libraries/rocm_core.py b/facefusion/libraries/rocm_core.py index 04bfb66b..a6459b5c 100644 --- a/facefusion/libraries/rocm_core.py +++ b/facefusion/libraries/rocm_core.py @@ -16,14 +16,16 @@ def create_static_library() -> Optional[ctypes.CDLL]: library_file = resolve_library_file() if library_file: - rocm_core_library = ctypes.CDLL(library_file) - return init_ctypes(rocm_core_library) + library = ctypes.CDLL(library_file) + + if library: + return init_ctypes(library) return None -def init_ctypes(rocm_core : ctypes.CDLL) -> ctypes.CDLL: - rocm_core.getROCmVersion.argtypes = [ ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_uint) ] - rocm_core.getROCmVersion.restype = ctypes.c_int +def init_ctypes(library : ctypes.CDLL) -> ctypes.CDLL: + library.getROCmVersion.argtypes = [ ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(ctypes.c_uint) ] + library.getROCmVersion.restype = ctypes.c_int - return rocm_core + return library