Serverless
PyWorker
Overview
7 min
the vast pyworker is a python web server designed to run alongside a machine learning model instance, providing serverless compatibility it serves as the primary entry point for api requests, forwarding them to the model's api hosted on the same instance additionally, it monitors performance metrics and estimates current workload, reporting these metrics to the serverless system all of vast's serverless templates use the vast pyworker if you are using a recommended serverless template from vast, the pyworker is already integrated with the template and will automatically startup when a {{worker group}} is created pyworker diagram in the diagram's example, a user's client is attempting to infer from a machine learning model with vast's serverless setup, the client sends a /route/ post request to the serverless system this asks the system for a gpu instance to send the inference request the serverless system selects a ready and available worker instance from the user's endpoint and replies with a json object containing the url of the selected instance the client then constructs a new post request with it's payload, authentication data, and the url of the worker instance this is sent to the worker the pyworker running on that specific instance validates the request and extracts the payload it then sends the payload to the model inference server, which runs on the same instance as the pyworker the model generates it's output and returns the result to the pyworker the pyworker formats the model's response as needed, and sends the response back to the client independently and concurrently, the pyworker periodically sends it's operational metrics to the serverless system, which is used to make scaling decisions the vast pyworker repository https //github com/vast ai/pyworker/ gives examples that are useful for learning how to create a custom pyworker for your custom template and integrate with vast’s serverless system even with a custom pyworker, the pyworker code runs on your vast instance, and we automate its installation and activation during instance creation the graphic below shows how the files and entities for the serverless system are organized integration with model instance the vast pyworker wraps the backend code of the model instance you are running the pyworker calls the appropriate backend function when the pyworker's corresponding api endpoint is invoked for example, if you are running a text generation inference (tgi) server, your pyworker might receive the following json body from a /generate endpoint json { "auth data" { "signature" "a base64 encoded signature string from route endpoint", "cost" 256, "endpoint" "your tgi endpoint name", "reqnum" 1234567890, "url" "http //worker ip address\ port" }, "payload" { "inputs" "what is the answer to the universe?", "parameters" { "max new tokens" 256, "temperature" 0 7, "top p" 0 9, "do sample" true } } } when it receives this request, your pyworker will internally send the following to the tgi model sever json { "inputs" "what is the answer to the universe?", "parameters" { "max new tokens" 256, "temperature" 0 7, "top p" 0 9, "do sample" true } } your pyworker would similarily receive the output result from the tgi server, and forward a formatted version to the client communication with serverless if you are building a custom pyworker for your own use case, to be able to integrate with vast's serverless system, each backend must send a message to the serverless system when the backend server is ready (e g , after model installation) periodically send performance metrics to the serverless system to optimize usage and performance report any errors to the serverless system for example implementations, reference the vast pyworker repository creating a custom backend if you want to create your own backend and learn how to integrate with the serverless system, please refer to the following guides c reating new pyworkers vast supported backends vast has pre made templates for popular models such as t ext generation inference and comfy ui https //docs vast ai/serverless/comfy ui these templates allow you to use these models in api mode, automatically handling performance and error tracking, making them compatible with vast serverless with no additional code required to get started with vast supported backends, see the inside a severless gpu guide