TorchServe – Hamel’s Blog - Hamel Husain
Хэмел Хусейн делится впечатлениями от экспериментов с TorchServe — инструментом для развёртывания моделей PyTorch. Среди плюсов: автоматическая батчёвка запросов, версионирование моделей, встроенное логирование и метрики. Однако отладка затруднена из-за гибрида Java и Python, а кривая обучения круче, чем у TF Serving, поскольку приходится разбираться в исходниках класса BaseHandler. Автор рекомендует использовать кастомные обработчики вместо «магических» дефолтных и отмечает скрытые ограничения вроде обязательного расширения .pt для torchscript. Пре- и постобработка в TorchServe проще, чем в TFServing, где приходится менять сигнатуру модели через DSL. При этом TF Serving выигрывает в управлении версиями за счёт алиасов вроде «staging» и «production», которых в TorchServe без обратного прокси не реализовать.
Experiments With Torch Serve
Why Use Torch Serve
Impressions
torch-model-archiver work together unless you study BaseHandler. For example: - the help docs of
torch-model-archiverstate that--model-fileis mandatory for eager mode models, which is not entirely true because you can load the model in the handler instead of implementing an interface that torch serve knows how to load (e.g. a class with aload_state_dictmethod like this example. Furthermore, this model file can only contain one class definition extended fromtorch.nn.moduleswhich is an odd constraint. When you search the internet for using torch-serve, many people are ignoring the prescribed interface and are loading models inside a custom handler instead. Here is an example of doing this with fastai and HuggingFace. I believe it is a better idea to use a custom handler because it is more transparent and easier to understand. The default handlers in the getting started guides are a bit too magical and I think they cause confusion for newcomers. I would argue that the default handlers should only be used after you understandBaseHandlerand write a few custom handlers. - If you want to use torchscript, your file extension must be
.pt(not.pth). This is not documented anywhere, and is an example of something you can only learn from theBaseHandlersource code (that particular file extension is hardcoded!).