Spaces:
Sleeping
Sleeping
Update facefusion/inference_manager.py
Browse files- facefusion/inference_manager.py +96 -95
facefusion/inference_manager.py
CHANGED
|
@@ -1,95 +1,96 @@
|
|
| 1 |
-
import importlib
|
| 2 |
-
import random
|
| 3 |
-
from time import sleep, time
|
| 4 |
-
from typing import List
|
| 5 |
-
|
| 6 |
-
from onnxruntime import InferenceSession
|
| 7 |
-
|
| 8 |
-
from facefusion import logger, process_manager, state_manager, translator
|
| 9 |
-
from facefusion.app_context import detect_app_context
|
| 10 |
-
from facefusion.common_helper import is_windows
|
| 11 |
-
from facefusion.execution import create_inference_session_providers, has_execution_provider
|
| 12 |
-
from facefusion.exit_helper import fatal_exit
|
| 13 |
-
from facefusion.filesystem import get_file_name, is_file
|
| 14 |
-
from facefusion.time_helper import calculate_end_time
|
| 15 |
-
from facefusion.types import DownloadSet, ExecutionProvider, InferencePool, InferencePoolSet
|
| 16 |
-
|
| 17 |
-
INFERENCE_POOL_SET : InferencePoolSet =\
|
| 18 |
-
{
|
| 19 |
-
'cli': {},
|
| 20 |
-
'ui': {}
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def get_inference_pool(module_name : str, model_names : List[str], model_source_set : DownloadSet) -> InferencePool:
|
| 25 |
-
while process_manager.is_checking():
|
| 26 |
-
sleep(0.5)
|
| 27 |
-
execution_device_ids = state_manager.get_item('execution_device_ids')
|
| 28 |
-
execution_providers = resolve_execution_providers(module_name)
|
| 29 |
-
app_context = detect_app_context()
|
| 30 |
-
|
| 31 |
-
for execution_device_id in execution_device_ids:
|
| 32 |
-
inference_context = get_inference_context(module_name, model_names, execution_device_id, execution_providers)
|
| 33 |
-
|
| 34 |
-
if app_context == 'cli' and INFERENCE_POOL_SET.get('ui').get(inference_context):
|
| 35 |
-
INFERENCE_POOL_SET['cli'][inference_context] = INFERENCE_POOL_SET.get('ui').get(inference_context)
|
| 36 |
-
if app_context == 'ui' and INFERENCE_POOL_SET.get('cli').get(inference_context):
|
| 37 |
-
INFERENCE_POOL_SET['ui'][inference_context] = INFERENCE_POOL_SET.get('cli').get(inference_context)
|
| 38 |
-
if not INFERENCE_POOL_SET.get(app_context).get(inference_context):
|
| 39 |
-
INFERENCE_POOL_SET[app_context][inference_context] = create_inference_pool(model_source_set, execution_device_id, execution_providers)
|
| 40 |
-
|
| 41 |
-
current_inference_context = get_inference_context(module_name, model_names, random.choice(execution_device_ids), execution_providers)
|
| 42 |
-
return INFERENCE_POOL_SET.get(app_context).get(current_inference_context)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def create_inference_pool(model_source_set : DownloadSet, execution_device_id : int, execution_providers : List[ExecutionProvider]) -> InferencePool:
|
| 46 |
-
inference_pool : InferencePool = {}
|
| 47 |
-
|
| 48 |
-
for model_name in model_source_set.keys():
|
| 49 |
-
model_path = model_source_set.get(model_name).get('path')
|
| 50 |
-
if is_file(model_path):
|
| 51 |
-
inference_pool[model_name] = create_inference_session(model_path, execution_device_id, execution_providers)
|
| 52 |
-
|
| 53 |
-
return inference_pool
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def clear_inference_pool(module_name : str, model_names : List[str]) -> None:
|
| 57 |
-
execution_device_ids = state_manager.get_item('execution_device_ids')
|
| 58 |
-
execution_providers = resolve_execution_providers(module_name)
|
| 59 |
-
app_context = detect_app_context()
|
| 60 |
-
|
| 61 |
-
if is_windows() and has_execution_provider('directml'):
|
| 62 |
-
INFERENCE_POOL_SET[app_context].clear()
|
| 63 |
-
|
| 64 |
-
for execution_device_id in execution_device_ids:
|
| 65 |
-
inference_context = get_inference_context(module_name, model_names, execution_device_id, execution_providers)
|
| 66 |
-
if INFERENCE_POOL_SET.get(app_context).get(inference_context):
|
| 67 |
-
del INFERENCE_POOL_SET[app_context][inference_context]
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
def create_inference_session(model_path : str, execution_device_id : int, execution_providers : List[ExecutionProvider]) -> InferenceSession:
|
| 71 |
-
model_file_name = get_file_name(model_path)
|
| 72 |
-
start_time = time()
|
| 73 |
-
|
| 74 |
-
try:
|
| 75 |
-
inference_session_providers = create_inference_session_providers(execution_device_id, execution_providers)
|
| 76 |
-
inference_session = InferenceSession(model_path, providers = inference_session_providers)
|
| 77 |
-
logger.debug(translator.get('loading_model_succeeded').format(model_name = model_file_name, seconds = calculate_end_time(start_time)), __name__)
|
| 78 |
-
return inference_session
|
| 79 |
-
|
| 80 |
-
except Exception:
|
| 81 |
-
logger.error(translator.get('loading_model_failed').format(model_name = model_file_name), __name__)
|
| 82 |
-
fatal_exit(1)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
def get_inference_context(module_name : str, model_names : List[str], execution_device_id : int, execution_providers : List[ExecutionProvider]) -> str:
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
| 1 |
+
import importlib
|
| 2 |
+
import random
|
| 3 |
+
from time import sleep, time
|
| 4 |
+
from typing import List
|
| 5 |
+
|
| 6 |
+
from onnxruntime import InferenceSession
|
| 7 |
+
|
| 8 |
+
from facefusion import logger, process_manager, state_manager, translator
|
| 9 |
+
from facefusion.app_context import detect_app_context
|
| 10 |
+
from facefusion.common_helper import is_windows
|
| 11 |
+
from facefusion.execution import create_inference_session_providers, has_execution_provider
|
| 12 |
+
from facefusion.exit_helper import fatal_exit
|
| 13 |
+
from facefusion.filesystem import get_file_name, is_file
|
| 14 |
+
from facefusion.time_helper import calculate_end_time
|
| 15 |
+
from facefusion.types import DownloadSet, ExecutionProvider, InferencePool, InferencePoolSet
|
| 16 |
+
|
| 17 |
+
INFERENCE_POOL_SET : InferencePoolSet =\
|
| 18 |
+
{
|
| 19 |
+
'cli': {},
|
| 20 |
+
'ui': {}
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def get_inference_pool(module_name : str, model_names : List[str], model_source_set : DownloadSet) -> InferencePool:
|
| 25 |
+
while process_manager.is_checking():
|
| 26 |
+
sleep(0.5)
|
| 27 |
+
execution_device_ids = state_manager.get_item('execution_device_ids')
|
| 28 |
+
execution_providers = resolve_execution_providers(module_name)
|
| 29 |
+
app_context = detect_app_context()
|
| 30 |
+
|
| 31 |
+
for execution_device_id in execution_device_ids:
|
| 32 |
+
inference_context = get_inference_context(module_name, model_names, execution_device_id, execution_providers)
|
| 33 |
+
|
| 34 |
+
if app_context == 'cli' and INFERENCE_POOL_SET.get('ui').get(inference_context):
|
| 35 |
+
INFERENCE_POOL_SET['cli'][inference_context] = INFERENCE_POOL_SET.get('ui').get(inference_context)
|
| 36 |
+
if app_context == 'ui' and INFERENCE_POOL_SET.get('cli').get(inference_context):
|
| 37 |
+
INFERENCE_POOL_SET['ui'][inference_context] = INFERENCE_POOL_SET.get('cli').get(inference_context)
|
| 38 |
+
if not INFERENCE_POOL_SET.get(app_context).get(inference_context):
|
| 39 |
+
INFERENCE_POOL_SET[app_context][inference_context] = create_inference_pool(model_source_set, execution_device_id, execution_providers)
|
| 40 |
+
|
| 41 |
+
current_inference_context = get_inference_context(module_name, model_names, random.choice(execution_device_ids), execution_providers)
|
| 42 |
+
return INFERENCE_POOL_SET.get(app_context).get(current_inference_context)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def create_inference_pool(model_source_set : DownloadSet, execution_device_id : int, execution_providers : List[ExecutionProvider]) -> InferencePool:
|
| 46 |
+
inference_pool : InferencePool = {}
|
| 47 |
+
|
| 48 |
+
for model_name in model_source_set.keys():
|
| 49 |
+
model_path = model_source_set.get(model_name).get('path')
|
| 50 |
+
if is_file(model_path):
|
| 51 |
+
inference_pool[model_name] = create_inference_session(model_path, execution_device_id, execution_providers)
|
| 52 |
+
|
| 53 |
+
return inference_pool
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def clear_inference_pool(module_name : str, model_names : List[str]) -> None:
|
| 57 |
+
execution_device_ids = state_manager.get_item('execution_device_ids')
|
| 58 |
+
execution_providers = resolve_execution_providers(module_name)
|
| 59 |
+
app_context = detect_app_context()
|
| 60 |
+
|
| 61 |
+
if is_windows() and has_execution_provider('directml'):
|
| 62 |
+
INFERENCE_POOL_SET[app_context].clear()
|
| 63 |
+
|
| 64 |
+
for execution_device_id in execution_device_ids:
|
| 65 |
+
inference_context = get_inference_context(module_name, model_names, execution_device_id, execution_providers)
|
| 66 |
+
if INFERENCE_POOL_SET.get(app_context).get(inference_context):
|
| 67 |
+
del INFERENCE_POOL_SET[app_context][inference_context]
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def create_inference_session(model_path : str, execution_device_id : int, execution_providers : List[ExecutionProvider]) -> InferenceSession:
|
| 71 |
+
model_file_name = get_file_name(model_path)
|
| 72 |
+
start_time = time()
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
inference_session_providers = create_inference_session_providers(execution_device_id, execution_providers)
|
| 76 |
+
inference_session = InferenceSession(model_path, providers = inference_session_providers)
|
| 77 |
+
logger.debug(translator.get('loading_model_succeeded').format(model_name = model_file_name, seconds = calculate_end_time(start_time)), __name__)
|
| 78 |
+
return inference_session
|
| 79 |
+
|
| 80 |
+
except Exception:
|
| 81 |
+
logger.error(translator.get('loading_model_failed').format(model_name = model_file_name), __name__)
|
| 82 |
+
fatal_exit(1)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def get_inference_context(module_name : str, model_names : List[str], execution_device_id : int, execution_providers : List[ExecutionProvider]) -> str:
|
| 86 |
+
valid_model_names = [name for name in model_names if name]
|
| 87 |
+
inference_context = '.'.join([ module_name ] + valid_model_names + [ str(execution_device_id) ] + list(execution_providers))
|
| 88 |
+
return inference_context
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def resolve_execution_providers(module_name : str) -> List[ExecutionProvider]:
|
| 92 |
+
module = importlib.import_module(module_name)
|
| 93 |
+
|
| 94 |
+
if hasattr(module, 'resolve_execution_providers'):
|
| 95 |
+
return getattr(module, 'resolve_execution_providers')()
|
| 96 |
+
return state_manager.get_item('execution_providers')
|