mindspore_lite.ModelGroup
- class mindspore_lite.ModelGroup(flags=ModelGroupFlag.SHARE_WORKSPACE)[source]
The ModelGroup class is used to define a MindSpore model group, facilitating multiple models to share workspace memory or weights(including constants and variables) memory.
- Parameters
flags (ModelGroupFlag, optional) – Indicates the type of the model group. Default:
ModelGroupFlag.SHARE_WEIGHT
.
Examples
>>> # Multi models share workspace memory >>> import mindspore_lite as mslite >>> model_type=mslite.ModelType.MINDIR >>> path1 = "path_to_model1" >>> path2 = "path_to_model2" >>> model_group = mslite.ModelGroup() >>> model_group.add_model([path1, path2]) >>> model_group.cal_max_size_of_workspace(model_type, context) >>> >>> # Multi models share weights memory >>> import mindspore_lite as mslite >>> context = mslite.Context() >>> context.target = ["Ascend"] >>> context.ascend.device_id = 0 >>> context.ascend.rank_id = 0 >>> context.ascend.provider = "ge" >>> model_group = mslite.ModelGroup(mslite.ModelGroupFlag.SHARE_WEIGHT) >>> model0 = mslite.Model() >>> model1 = mslite.Model() >>> model_group.add_model([model0, model1]) >>> model0.build_from_file("seq_1024.mindir", mslite.ModelType.MINDIR, context, "config0.ini") >>> model1.build_from_file("seq_1.mindir", mslite.ModelType.MINDIR, context, "config.ini")
- add_model(models)[source]
Used to define MindSpore Lite model grouping information, which is used to share workspace memory or weight (including constants and variables) memory. This interface only supports weight memory sharing when the models is a tuple or list of Model objects, and only supports workspace memory sharing in other scenarios.
- Parameters
models (union[tuple/list(str), tuple/list(Model)]) – Define the list/tuple of model paths or Model objects.
- Raises
TypeError – models is not a list and tuple.
TypeError – models is a list or tuple, but the elements are not all str or Model.
RuntimeError – Failed to add model grouping information.
- cal_max_size_of_workspace(model_type, context)[source]
Calculate the max workspace of the added models. Only valid when the type of ModelGroup is
ModelGroupFlag.SHARE_WORKSPACE
.- Parameters
- Raises
TypeError – model_type is not a ModelType.
TypeError – context is a Context.
RuntimeError – cal max size of workspace failed.