Skip to content

Commit

Permalink
feat: add option to allow to load a single module
Browse files Browse the repository at this point in the history
Since we are deprecating loading several models (#128, #129, #132) we
need to ensure that users are able to load only a single module, even if
several are present.
  • Loading branch information
alvarolopez committed Mar 25, 2024
1 parent eee055e commit dce1ab5
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 43 deletions.
9 changes: 5 additions & 4 deletions deepaas/cmd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
from oslo_log import log

# from deepaas import config
from deepass import config
from deepaas.model import loading
from deepaas.model.v2 import wrapper as v2_wrapper

CONF = config.CONF

debug_cli = False

Expand Down Expand Up @@ -152,15 +154,14 @@ def _get_model_name(model_name=None):
sys.stderr.write(
"[ERROR]: There are several models available ({}).\n"
"You have to choose one and set it in the DEEPAAS_V2_MODEL "
"environment setting.\n".format(list(models.keys()))
"environment variable or using the --mode-name option"
".\n".format(list(models.keys()))
)
sys.exit(1)


# Get the model name
model_name = None
if "DEEPAAS_V2_MODEL" in os.environ:
model_name = os.environ["DEEPAAS_V2_MODEL"]
model_name = CONF.model_name

model_name, model_obj = _get_model_name(model_name)

Expand Down
11 changes: 0 additions & 11 deletions deepaas/cmd/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@
from deepaas.model.v2.wrapper import UploadedFile

cli_opts = [
cfg.StrOpt(
"model-name",
help="""
Add the name of the model from which you want
to obtain the prediction.
If there are multiple models installed and youd don't
specify the name of the one you want to use the program will fail.
If there is only one model installed, that will be used
to make the prediction.
""",
),
cfg.StrOpt(
"input-file",
short="i",
Expand Down
14 changes: 14 additions & 0 deletions deepaas/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# under the License.

import logging
import os
import warnings

from oslo_config import cfg
Expand Down Expand Up @@ -85,6 +86,19 @@
Pre-warm the modules (eg. load models, do preliminary checks, etc). You might
want to disable this option if DEEPaaS is loading more than one module because
you risk getting out of memory errors.
""",
),
cfg.StrOpt(
"model-name",
default=os.environ.get("DEEPAAS_V2_MODEL", ""),
help="""
Specify the model to be used. If not specified, DEEPaaS will serve all the models that
are available. If specified, DEEPaaS will serve only the specified model. You can also
use the DEEPAAS_V2_MODEL environment variable.
WARNING: Serving multiple models is deprecated and will be removed in the future,
therefore it is strongly suggested that you specify the model you want to
or that you ensure that only one model is available.
""",
),
]
Expand Down
19 changes: 19 additions & 0 deletions deepaas/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-

# Copyright 2018 Spanish National Research Council (CSIC)
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


class ModuleNotFoundError(Exception):
"""Module not found error."""
24 changes: 24 additions & 0 deletions deepaas/model/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,37 @@
# License for the specific language governing permissions and limitations
# under the License.

from deepaas import exceptions

import stevedore

NAMESPACES = {
"v2": "deepaas.v2.model",
}


def get_model_by_name(name, version):
"""Get a model by its name.
:param name: The name of the model.
:type name: str
:param version: The version of the model.
:type version: str
:returns: The model.
:rtype: object
"""
mgr = stevedore.NamedExtensionManager(
namespace=NAMESPACES.get(version),
names=[name],
)
if name not in mgr.names():
raise exceptions.ModuleNotFoundError(
"Model '%s' not found in namespace '%s'" % (name, NAMESPACES.get(version))
)
return mgr[name].plugin


def get_available_model_names(version):
"""Get the names of all the models that are available on the system.
Expand Down
24 changes: 22 additions & 2 deletions deepaas/model/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@

from oslo_log import log

from deepaas import config
from deepaas import exceptions
from deepaas.model import loading
from deepaas.model.v2 import test
from deepaas.model.v2 import wrapper

LOG = log.getLogger(__name__)

CONF = config.CONF

# Model registry
MODELS = {}
MODELS_LOADED = False
Expand All @@ -37,10 +41,26 @@ def register_models(app):
return

try:
for name, model in loading.get_available_models("v2").items():
MODELS[name] = wrapper.ModelWrapper(name, model, app)
if CONF.model_name:
MODELS[CONF.model_name] = wrapper.ModelWrapper(
CONF.model_name,
loading.get_model_by_name(CONF.model_name, "v2"),
app,
)
else:
for name, model in loading.get_available_models("v2").items():
MODELS[name] = wrapper.ModelWrapper(name, model, app)
except exceptions.ModuleNotFoundError:
LOG.error("Model not found: %s", CONF.model_name)
raise
except Exception as e:
# We do not raise here, as we have not yet removed the deprecated loading of the
# test module... but we should remove it as soon as the code below is deprecated
LOG.warning("Error loading models: %s", e)
warnings.warn(
"Error loading models, using test model. This will be deprecated soon.",
DeprecationWarning,
)

if MODELS:
if len(MODELS) > 1:
Expand Down
36 changes: 23 additions & 13 deletions doc/source/cli/deepaas-cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ Synopsis
Description
===========

:program:`deepaas-cli` Command line interface (CLI) to DEEPaaS models
:program:`deepaas-cli` Command line interface (CLI) to DEEPaaS models
that are loaded through the ``deepaas.v2.models`` entrypoint API.
One gets access to the same get_metadata, warm, predict, and train
One gets access to the same get_metadata, warm, predict, and train
methods with all corresponding options as with DEEPaaS REST API.
To get available for the method options, one has to call
To get available for the method options, one has to call
``deepaas-cli <method> --help``.
Additional parameters are provided:
``--deepaas_method_output`` is to store the output in a pre-defined
by a user file;
``--deepaas_with_multiprocessing`` is to activate multiprocessing
``--deepaas_method_output`` is to store the output in a pre-defined
by a user file;
``--deepaas_with_multiprocessing`` is to activate multiprocessing
support, default is True.
oslo_log package is used for logging information, which provides
oslo_log package is used for logging information, which provides
additional options for the script.
If several models are available for loading, one has to provide
If several models are available for loading, one has to provide
which one to load via DEEPAAS_V2_MODEL environment setting.

Options
Expand All @@ -42,21 +42,31 @@ Options

Calls predict() method. The output can be stored via
--deepaas_method_output.

.. option:: train

Calls train() method. The output can be stored via
--deepaas_method_output.

.. option:: --deepaas_method_output
.. option:: --deepaas_method_output

To save the results to a local file, if needed. Available for
To save the results to a local file, if needed. Available for
get_metadata, predict, train methods.

.. option:: --deepaas_with_multiprocessing
.. option:: --deepaas_with_multiprocessing

To activate multiprocessing support, default is True.


.. option:: --model-name MODEL_NAME

Specify the model to be used. If not specified, DEEPaaS will serve all the models
that are available. If specified, DEEPaaS will serve only the specified model. You
can also use the DEEPAAS_V2_MODEL environment variable.

WARNING: Serving multiple models is deprecated and will be removed in the future,
therefore it is strongly suggested that you specify the model you want to or that
you ensure that only one model is available.

Files
=====

Expand Down
25 changes: 13 additions & 12 deletions doc/source/cli/deepaas-predict.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Synopsis
Description
===========

:program:`deepaas-predict` It is a command that allows you to obtain,
through the command line, the prediction of a file or the url of
:program:`deepaas-predict` It is a command that allows you to obtain,
through the command line, the prediction of a file or the url of
a file, of the models that are loaded through the ``deepaas.v2.models``
entrypoint API.

Expand All @@ -28,23 +28,24 @@ Options
option must be available in the model used.
(by default application/json).

.. option:: --model-name CONTENT_TYPE, -c CONTENT_TYPE
.. option:: --model-name MODEL_NAME

Specify the model to be used. If not specified, DEEPaaS will serve all the models
that are available. If specified, DEEPaaS will serve only the specified model. You
can also use the DEEPAAS_V2_MODEL environment variable.

WARNING: Serving multiple models is deprecated and will be removed in the future,
therefore it is strongly suggested that you specify the model you want to or that
you ensure that only one model is available.

Add the name of the model from which you want
to obtain the prediction.
If there are multiple models installed and youd don't
specify the name of the one you want to use the program will fail.
If there is only one model installed, that will be used
to make the prediction.

.. option:: --output OUTPUT_DIR, -o OUTPUT_DIR

Save the result to a local file. This option is required.

.. option:: --url, -u
.. option:: --url, -u

If set to true, the input file is the url o a file to predict.

Files
=====

Expand Down
10 changes: 10 additions & 0 deletions doc/source/cli/deepaas-run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ Description
Options
=======

.. option:: --model-name MODEL_NAME

Specify the model to be used. If not specified, DEEPaaS will serve all the models
that are available. If specified, DEEPaaS will serve only the specified model. You
can also use the DEEPAAS_V2_MODEL environment variable.

WARNING: Serving multiple models is deprecated and will be removed in the future,
therefore it is strongly suggested that you specify the model you want to or that
you ensure that only one model is available.

.. option:: --debug, -d

If set to true, the logging level will be set to DEBUG instead of the
Expand Down
6 changes: 6 additions & 0 deletions doc/source/user/v2-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ loaded to offer the model functionality through the API. This allows you to
offer several models using a single DEEPaaS instance, by defining different
entry points for the different models.

.. warning::
Serving multiple models is marked as deprecated, and will be removed in a
future major version of the API. Please ensure that you start using the `model-name`
configuration option in your configuration file or the `--model-name` command line
option as soon as possible.

.. _Setuptools: https://setuptools.readthedocs.io/en/latest/setuptools.html

When the DEEPaaS API is spawned it will look for the ``deepaas.v2.model``
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ prelude: >
deprecations:
- Loading the default deepaas-test model if no models are available is now
marked as deprecated and will be removed in the next major version.
- Loading several models is now marked as deprecated.
- Loading several models is now marked as deprecated. If you have several models
installed, please use the `model-name` configuration option or the
`--model-name` CLI option.

0 comments on commit dce1ab5

Please sign in to comment.