-
Notifications
You must be signed in to change notification settings - Fork 886
[RFC] Add third party caching support in TorchServe #2465
Description
🚀 The feature
Proposal
This proposal is for adding third party caching support in TorchServe.
Conceptually, we can add caching support in either frontend or backend
Frontend Caching Support
Backend Caching Support
From a design point of view, though it would make sense to add caching support in the frontend, the proposal is to add caching support for the backend for 2 reasons:
- Easy of use with Python
- Ability to extend the base class and customize the caching logic.
We start by adding support for Redis.
The implementation is based on the example proposed by @GavinPHR in #1952
Design Considerations
Two main considerations when designing this solution are
- How do you specify the config needed to connect to RedisCache
- Where do we plugin Caching class in TorchServe
Specify Cache Config
We could pass the config using config.properties, model-config.yaml, extra-files.
Since we are adding cache support for Python backend, it makes sense to add the config in model-config.yaml
This also gives users the flexibility to use different caches for different model.
The necessary config to access your Redis Cache is defined in the model-config.yaml
cache:
host: localhost
port: 6379
db: 0
TorchServe Implementation
The basic idea is to wrap the handle method with the RedisCache
class.
We use the context as the key , hash the context and use this as a key to store the inference result
Where do we wrap the handle method?
Option A
In model_loader.py, load method
User specifies config in yaml file. Based on the config, the entry_point is wrapped with RedicCache
Pro:
Minimal effort
Con:
Spaghetti code
Option B
In custom_handler.py , initialize method
Pro:
User can customize easily
Con:
User needs to define the initialize method
The below code is with Option B
To use Redis Cache with TorchServe, include the following in your handler
from ts.handler_utils.cache.redis import RedisCache
and define your handler as follows for an Image Classifier.
The key idea is to wrap the handle method with the class RedisCache
from ts.torch_handler.image_classifier import ImageClassifier
class CacheHandler(ImageClassifier):
def __init__(self):
super(CacheHandler, self).__init__()
self.initialized = False
def initialize(self, ctx):
super().initialize(ctx)
self.handle = RedisCache(ctx)(self.handle)
self.initialized = True
Motivation, pitch
Implementing caching support will result in cost savings for customers in inference costs.
Alternatives
No response
Additional context
No response