This is an example of using Ray actor in Ray Serve to dynamically update the models. In the screencast below, I demostrated that you can dynamically register new model or update existing one with Python API. The updated model is immediately reflected in API call.
Created
January 19, 2021 00:13
-
-
Save simon-mo/8c08249f3ed9d64b68e70e5f197514ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ray | |
| from ray import serve | |
| @ray.remote | |
| class RegistryActor: | |
| def __init__(self): | |
| self.registry = dict() | |
| def put(self, key, value): | |
| self.registry[key] = value | |
| def get(self, key): | |
| return self.registry.get(key) | |
| class DynmaicModel: | |
| def __init__(self): | |
| self.registry_actor = ray.get_actor("registry") | |
| async def __call__(self, http_request): | |
| name = http_request.query_params["model_name"] | |
| model_weights = await self.registry_actor.get.remote(name) | |
| # for this example, we will just return the model weights. | |
| return model_weights | |
| def user_api_add_model(model_name, model_weights): | |
| # retrieve the registry actor | |
| handle = ray.get_actor("registry") | |
| # add the model to actor | |
| ray.get(handle.put.remote(model_name, model_weights)) | |
| def deploy_registry_actor(): | |
| # create a global singleton actor | |
| RegistryActor.options(lifetime="detached", name="registry").remote() | |
| def deploy_serve(): | |
| client = serve.start(detached=True) | |
| client.create_backend("model", DynmaicModel) | |
| client.create_endpoint("model", backend="model", route="/predict") | |
| if __name__ == "__main__": | |
| ray.init(address="auto") | |
| deploy_registry_actor() | |
| deploy_serve() | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Execution log: