Source code for mindspore_serving.server.worker.distributed.register

# Copyright 2021 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
"""Serving, distributed worker register"""

from mindspore_serving import log as logger
from mindspore_serving.server.common import check_type
from mindspore_serving.server.register.utils import get_servable_dir
from mindspore_serving.server.register.model import append_declared_model
from mindspore_serving._mindspore_serving import ModelMeta_, ServableRegister_


[docs]def declare_servable(rank_size, stage_size, with_batch_dim=True, without_batch_dim_inputs=None): """declare distributed servable in servable_config.py. Args: rank_size (int): Te rank size of the distributed model. stage_size (int): The stage size of the distributed model. with_batch_dim (bool, optional): Whether the first shape dim of the inputs and outputs of model is batch. Default: True. without_batch_dim_inputs (Union[int, tuple[int], list[int]], optional): Index of inputs that without batch dim when with_batch_dim is True. Default: None. Raises: RuntimeError: The type or value of the parameters are invalid. Return: Model, identification of this model, used as input of add_stage. Examples: >>> from mindspore_serving.server import distributed >>> model = distributed.declare_servable(rank_size=8, stage_size=1) """ check_type.check_bool('with_batch_dim', with_batch_dim) meta = ModelMeta_() meta.common_meta.servable_name = get_servable_dir() meta.common_meta.model_key = get_servable_dir() # used to identify model meta.common_meta.with_batch_dim = with_batch_dim if without_batch_dim_inputs: without_batch_dim_inputs = check_type.check_and_as_int_tuple_list('without_batch_dim_inputs', without_batch_dim_inputs, 0) meta.common_meta.without_batch_dim_inputs = without_batch_dim_inputs # init distributed servable meta info check_type.check_int("rank_size", rank_size, 1) check_type.check_int("stage_size", stage_size, 1) meta.distributed_meta.rank_size = rank_size meta.distributed_meta.stage_size = stage_size ServableRegister_.declare_distributed_model(meta) logger.info(f"Declare distributed servable, servable name: {meta.common_meta.model_key} " f", rank_size: {rank_size} , stage_size: {stage_size}, with_batch_dim: {with_batch_dim} " f", without_batch_dim_inputs: {without_batch_dim_inputs}") return append_declared_model(meta.common_meta.model_key)