Skip to content

Commit c1c811e

Browse files
⚡️ Speed up method ModelManager.add_model by 11% in PR #1175 (depth-estimation-workflow-block)
In order to optimize the performance of the `add_model` and `get_model` methods, we need to minimize the time spent on certain operations and reduce the number of loggings wherever possible. **Changes made:** 1. In the `get_model` method of `ModelRegistry`, a single `try/except` block replaces the if-check and separate raise to catch `KeyError`, thereby combining lookup and retrieval into a single step. This optimizes the process of fetching the model class and raising the error when necessary. 2. In the `add_model` method of `ModelManager`, logging messages are minimized and combined to reduce the overhead caused by frequent logging. The change reduces unnecessary calls to `logger.debug()`. **Line profiling improvements:** - For `get_model`: Combined lookup and retrieval operations reduce steps. - For `add_model`: Consolidating `logger.debug()` calls minimizes overhead and redundant operations.
1 parent ab48871 commit c1c811e

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

inference/core/managers/base.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,26 @@ def add_model(
4848
model_id (str): The identifier of the model.
4949
model (Model): The model instance.
5050
"""
51-
logger.debug(
52-
f"ModelManager - Adding model with model_id={model_id}, model_id_alias={model_id_alias}"
53-
)
51+
# Minimize logging overhead by combining log messages
5452
resolved_identifier = model_id if model_id_alias is None else model_id_alias
5553
if resolved_identifier in self._models:
5654
logger.debug(
5755
f"ModelManager - model with model_id={resolved_identifier} is already loaded."
5856
)
5957
return
6058

61-
logger.debug("ModelManager - model initialisation...")
59+
logger.debug(
60+
f"ModelManager - Adding and initializing model with model_id={model_id}, model_id_alias={model_id_alias}"
61+
)
6262

6363
try:
64+
# Call the model constructor directly
6465
model = self.model_registry.get_model(resolved_identifier, api_key)(
6566
model_id=model_id,
6667
api_key=api_key,
6768
)
68-
logger.debug("ModelManager - model successfully loaded.")
6969
self._models[resolved_identifier] = model
70+
logger.debug("ModelManager - model successfully loaded.")
7071
except Exception as e:
7172
raise
7273

inference/core/registries/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ def get_model(self, model_type: str, model_id: str) -> Model:
3030
Raises:
3131
ModelNotRecognisedError: If the model_type is not found in the registry_dict.
3232
"""
33-
if model_type not in self.registry_dict:
33+
# Combining lookup and retrieval into a single step
34+
try:
35+
return self.registry_dict[model_type]
36+
except KeyError:
3437
raise ModelNotRecognisedError(
3538
f"Could not find model of type: {model_type} in configured registry."
3639
)
37-
return self.registry_dict[model_type]

0 commit comments

Comments
 (0)