TorchScript¶
关于本页
这是 BentoML 中 TorchScript 的 API 参考。您可以在官方文档中找到更多关于 TorchScript 的信息。
注意
您可以在我们的 bentoml/examples 目录中找到更多 TorchScript 的示例。
- bentoml.torchscript.save_model(name: Tag | str, model: torch.ScriptModule, *, signatures: ModelSignaturesType | None = None, labels: t.Dict[str, str] | None = None, custom_objects: t.Dict[str, t.Any] | None = None, external_modules: t.List[ModuleType] | None = None, metadata: t.Dict[str, t.Any] | None = None, _framework_name: str = 'torchscript', _module_name: str = 'bentoml.torchscript', _extra_files: dict[str, t.Any] | None = None) bentoml.Model [源代码]¶
将模型实例保存到 BentoML 模型库。
- 参数:
name (
str
) – 给定模型的名称。这应该通过 Python 标识符检查。model (torch.ScriptModule) – 要保存的模型实例
signatures (
dict
, 可选) – 方法名称及其对应签名的字典。labels (
Dict[str, str]
, 可选, 默认为None
) – 用于管理模型的用户定义标签,例如 team=nlp, stage=devcustom_objects (
Dict[str, Any]]
, 可选, 默认为None
) – 用户定义的其他 Python 对象,与模型一起保存,例如 tokenizer 实例、预处理器函数、模型配置 jsonexternal_modules (
List[ModuleType]
, 可选, 默认为None
) – 用户定义的其他 Python 模块,与模型或 custom objects 一起保存,例如 tokenizer 模块、预处理器模块、模型配置模块metadata (
Dict[str, Any]
, 可选, 默认为None
) – 给定模型的自定义元数据。
- 返回:
格式为 name:version 的
tag
,其中 name 是用户定义的模型名称,version 是由 BentoML 生成的版本。- 返回类型:
Tag
示例
import bentoml import torch
- bentoml.torchscript.load_model(bentoml_model: str | Tag | Model, device_id: str | None = None, *, _extra_files: dict[str, t.Any] | None = None) torch.ScriptModule | tuple[torch.ScriptModule, dict[str, t.Any]] [源代码]¶
从 BentoML 本地模型库中加载给定名称的模型。
- 参数:
tag – BentoML 本地模型库中已保存模型的标签。
device_id – 可选的将给定模型放置在哪个设备上。参考 https://pytorch.ac.cn/docs/stable/tensor_attributes.html#torch.torch.device
_extra_files – 文件名和空字符串的字典。参见 https://pytorch.ac.cn/docs/stable/generated/torch.jit.load.html。
- 返回:
从 BentoML 模型库获取
torch.ScriptModule
的实例。- 返回类型:
torch.ScriptModule
示例
import bentoml lit = bentoml.torchscript.load_model('lit_classifier:latest', device_id="cuda:0")