Skip to content

Commit 9fd4632

Browse files
authored
Bananas (#22)
2 parents ab00f82 + 5bf3460 commit 9fd4632

File tree

10 files changed

+2382
-1671
lines changed

10 files changed

+2382
-1671
lines changed

docs/serverless/workers/handlers/handler-additional-controls.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Additional controls"
3-
sidebar_position: 5
3+
sidebar_position: 6
44
---
55

66
## Update progress
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"position": 1,
3+
"label": "Banana",
4+
"link": {
5+
"type": "generated-index",
6+
"title": "Bananas"
7+
},
8+
"customProps": {
9+
"description": "Migrating from another service to RunPod, see our Overview page to get started."
10+
}
11+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
title: Migrate Your Banana Images to RunPod
3+
sidebar_label: Migrate images
4+
draft: true
5+
---
6+
7+
Migrating your AI models and applications from one cloud service to another can often present a challenge, especially when the two platforms operate differently. This tutorial aims to streamline the process of moving from Banana Dev to RunPod, focusing on transferring Docker-based applications and AI models. Whether you're shifting due to preferences in service offerings, pricing, or performance, this guide will help you through the transition smoothly.
8+
9+
## Introduction
10+
11+
Banana Dev provides an environment for deploying machine learning models easily, while RunPod offers robust and scalable serverless solutions. Transitioning between these platforms involves adapting your application to the new environment's requirements and deploying it effectively.
12+
13+
Below, we'll walk through how to adapt a Python application from Banana Dev to RunPod, including necessary changes to your Dockerfile and deployment configurations.
14+
15+
## Step 1: Understand Your Application
16+
17+
First, take a comprehensive look at your current Banana Dev application. Our example application uses the `potassium` framework for serving a machine learning model:
18+
19+
```python
20+
from io import BytesIO
21+
from potassium import Potassium, Request, Response
22+
from diffusers import DiffusionPipeline, DDPMScheduler
23+
import torch
24+
import base64
25+
26+
# create a new Potassium app
27+
app = Potassium("my_app")
28+
29+
# @app.init runs at startup, and loads models into the app's context
30+
@app.init
31+
def init():
32+
repo_id="Meina/MeinaUnreal_V3"
33+
34+
ddpm = DDPMScheduler.from_pretrained(repo_id, subfolder="scheduler")
35+
36+
model = DiffusionPipeline.from_pretrained(
37+
repo_id,
38+
use_safetensors=True,
39+
torch_dtype=torch.float16,
40+
scheduler=ddpm
41+
).to("cuda")
42+
43+
context = {
44+
"model": model,
45+
}
46+
47+
return context
48+
49+
# @app.handler runs for every call
50+
@app.handler()
51+
def handler(context: dict, request: Request) -> Response:
52+
model = context.get("model")
53+
54+
prompt = request.json.get("prompt")
55+
negative_prompt = "(worst quality, low quality:1.4), monochrome, zombie, (interlocked fingers), cleavage, nudity, naked, nude"
56+
57+
image = model(
58+
prompt=prompt,
59+
negative_prompt=negative_prompt,
60+
guidance_scale=7,
61+
num_inference_steps=request.json.get("steps", 30),
62+
generator=torch.Generator(device="cuda").manual_seed(request.json.get("seed")) if request.json.get("seed") else None,
63+
width=512,
64+
height=512,
65+
).images[0]
66+
67+
buffered = BytesIO()
68+
image.save(buffered, format="JPEG", quality=80)
69+
img_str = base64.b64encode(buffered.getvalue())
70+
71+
return Response(
72+
json = {"output": str(img_str, "utf-8")},
73+
status=200
74+
)
75+
76+
if __name__ == "__main__":
77+
app.serve()
78+
```
79+
80+
This application initializes a BERT model for fill-mask tasks and serves it over HTTP.
81+
82+
---
83+
84+
## Step 2: Adapt Your Code for RunPod
85+
86+
In RunPod, applications can be adapted to run in a serverless manner, which involves modifying your application logic to fit into the RunPod's handler function format. Below is an example modification that adapts our initial Banana Dev application to work with RunPod, using the `diffusers` library for AI model inference:
87+
88+
```python
89+
import runpod
90+
from diffusers import AutoPipelineForText2Image
91+
import base64
92+
import io
93+
import time
94+
95+
# If your handler runs inference on a model, load the model here.
96+
# You will want models to be loaded into memory before starting serverless.
97+
98+
try:
99+
pipe = AutoPipelineForText2Image.from_pretrained("meina/meinaunreal_v3")
100+
pipe.to("cuda")
101+
except RuntimeError:
102+
quit()
103+
104+
def handler(job):
105+
""" Handler function that will be used to process jobs. """
106+
job_input = job['input']
107+
prompt = job_input['prompt']
108+
109+
time_start = time.time()
110+
image = pipe(prompt=prompt, num_inference_steps=1, guidance_scale=0.0).images[0]
111+
print(f"Time taken: {time.time() - time_start}")
112+
113+
buffer = io.BytesIO()
114+
image.save(buffer, format="PNG")
115+
image_bytes = buffer.getvalue()
116+
117+
return base64.b64encode(image_bytes).decode('utf-8')
118+
119+
120+
runpod.serverless.start({"handler": handler})
121+
```
122+
123+
---
124+
125+
This modification involves initializing your model outside of the handler function to ensure it's loaded into memory before processing jobs, a crucial step for efficient serverless execution.
126+
127+
## Step 3: Update Your Dockerfile
128+
129+
The Dockerfile must also be adapted for RunPod's environment. Here's a comparison between a typical Banana Dev Dockerfile and the adapted version for RunPod:
130+
131+
### Banana Dev Dockerfile
132+
133+
```dockerfile
134+
FROM pytorch/pytorch:1.11.0-cuda11.3-cudnn8-runtime
135+
...
136+
CMD python3 -u app.py
137+
```
138+
139+
---
140+
141+
### RunPod Dockerfile
142+
143+
```dockerfile
144+
FROM runpod/base:0.4.0-cuda11.8.0
145+
146+
CMD python3.11 -u /handler.py
147+
```
148+
149+
---
150+
151+
The key differences include the base image and the execution command, reflecting RunPod's requirements and Python version specifics.
152+
153+
## Step 4: Deploy to RunPod
154+
155+
Once your code and Dockerfile are ready, the next steps involve building your Docker image and deploying it on RunPod. This process typically involves:
156+
157+
- Building your Docker image with the adapted Dockerfile.
158+
- Pushing the image to a container registry (e.g., DockerHub).
159+
- Creating a serverless function on RunPod and configuring it to use your Docker image.
160+
161+
## Testing and Verification
162+
163+
After deployment, thoroughly test your application to ensure it operates as expected within the RunPod environment. This may involve sending requests to your serverless endpoint and verifying the output.
164+
165+
## Conclusion
166+
167+
Migrating from Banana Dev to RunPod involves several key steps: adapting your application code, updating the Dockerfile, and deploying the adapted application on RunPod. By following this guide, you can make the transition smoother and take advantage of RunPod's serverless capabilities for your AI applications.
168+
169+
Remember to review RunPod's documentation for specific details on serverless deployment and configuration options to optimize your application's performance and cost-efficiency.
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
---
2+
title: Overview
3+
sidebar_position: 1
4+
description: Migrating from Banana to RunPod? Checkout our migration page to get started.
5+
---
6+
7+
import Tabs from '@theme/Tabs';
8+
import TabItem from '@theme/TabItem';
9+
10+
To get started with RunPod:
11+
12+
- [Create a RunPod account](/get-started/manage-accounts)
13+
- [Add funds](/get-started/billing-information)
14+
- [Use the RunPod SDK](#setting-up-your-project) to build and connect with your Serverless Endpoints
15+
16+
<details>
17+
<summary>
18+
19+
**Quick migration with Docker**
20+
21+
</summary>
22+
23+
Transitioning from Banana to RunPod doesn't have to be a lengthy process.
24+
For users seeking a swift migration path while maintaining Banana's dependencies for the interim, the Docker approach provides an efficient solution.
25+
This method allows you to leverage Docker to encapsulate your environment, simplifying the migration process and enabling a smoother transition to RunPod.
26+
27+
**Why consider the Dockerfile approach?**
28+
29+
Utilizing a Dockerfile for migration offers a bridge between Banana and RunPod, allowing for immediate deployment of existing projects without the need to immediately discard Banana's dependencies. This approach is particularly beneficial for those looking to test or move their applications to RunPod with minimal initial adjustments.
30+
31+
**Dockerfile**
32+
33+
The provided Dockerfile outlines a straightforward process for setting up your application on RunPod.
34+
35+
Add this Dockerfile to your project.
36+
37+
```dockerfile
38+
FROM runpod/banana:peel as bread
39+
FROM repo/image:tag
40+
41+
RUN pip install runpod
42+
43+
COPY --from=bread /handler.py .
44+
COPY --from=bread /start.sh .
45+
46+
RUN chmod +x start.sh
47+
CMD ["./start.sh"]
48+
```
49+
50+
**Building and deploying**
51+
52+
After creating your Dockerfile, build your Docker image and deploy it to RunPod.
53+
This process involves using Docker commands to build the image and then deploying it to RunPod.
54+
55+
**Advantages and considerations**
56+
57+
This Dockerfile approach expedites the migration process, allowing you to leverage RunPod's powerful features with minimal initial changes to your project.
58+
It's an excellent way to quickly transition and test your applications on RunPod.
59+
60+
However, while this method facilitates a quick start on RunPod, it's advisable to plan for a future migration away from Banana's dependencies, as there is overhead to building Banana's dependencies and deploying them to RunPod.
61+
62+
Gradually adapting your project to utilize RunPod's native features and services will optimize your application's performance and scalability.
63+
64+
**Moving forward**
65+
66+
Once you've migrated your application using the Docker approach, consider exploring RunPod's full capabilities.
67+
Transitioning away from Banana's dependencies and fully integrating with RunPod's services will allow you to take full advantage of what RunPod has to offer.
68+
69+
This quick migration guide is just the beginning.
70+
Continue with the rest of our tutorial to learn how to leverage RunPod's features to their fullest and ensure your project is fully adapted to its new environment.
71+
72+
</details>
73+
74+
The rest of this guide will help you set up a RunPod project.
75+
76+
## Setting up your project
77+
78+
Just like with Banana, RunPod provides a Python SDK to run your projects.
79+
80+
To get started, install setup a virtual environment then install the SDK library.
81+
82+
<Tabs>
83+
<TabItem value="macos" label="macOS" default>
84+
85+
Create a Python virtual environment with venv:
86+
87+
```command
88+
python3 -m venv env
89+
source env/bin/activate
90+
```
91+
92+
</TabItem>
93+
<TabItem value="windows" label="Windows">
94+
95+
Create a Python virtual environment with venv:
96+
97+
```command
98+
python -m venv env
99+
env\Scripts\activate
100+
```
101+
102+
</TabItem>
103+
104+
</Tabs>
105+
106+
To install the SDK, run the following command from the terminal.
107+
108+
```command
109+
python -m pip install runpod
110+
```
111+
112+
## Project examples
113+
114+
RunPod provides a [repository of templates for your project](https://github.com/runpod-workers).
115+
116+
You can use the template to get started with your project.
117+
118+
```command
119+
gh repo clone runpod-workers/worker-template
120+
```
121+
122+
Now that you've got a basic RunPod Worker template created:
123+
124+
- Continue reading to see how you'd migrate from Banana to RunPod
125+
- See [Generate SDXL Turbo](/tutorials/serverless/generate-sdxl-turbo) for a general approach on deploying your first Serverless Endpoint with RunPod.
126+
127+
## Project structure
128+
129+
When beginning to migrate your Banana monorepo to RunPod, you will need to understand the structure of your project.
130+
131+
<Tabs>
132+
133+
<TabItem value="banana" label="Banana" default>
134+
135+
Banana is a monorepo that contains multiple services. The basic structure for Banana projects is aligned with the RunPod Serverless projects for consistency:
136+
137+
```text
138+
.
139+
├── Dockerfile # Docker configuration
140+
├── README.md # Project documentation
141+
├── banana_config.json # Configuration settings
142+
├── requirements.txt # Dependencies
143+
└── src
144+
├── app.py # Main application code
145+
└── download.py # Download script
146+
```
147+
148+
</TabItem>
149+
<TabItem value="runpod" label="RunPod">
150+
151+
RunPod Serverless is a monorepo that contains multiple services.
152+
153+
```text
154+
.
155+
├── Dockerfile # Docker configuration
156+
├── LICENSE # License information
157+
├── README.md # Project documentation
158+
├── builder
159+
│ ├── requirements.txt # Dependencies
160+
│ └── setup.sh # Setup script
161+
└── src
162+
└── handler.py # Main handler code
163+
```
164+
165+
</TabItem>
166+
</Tabs>
167+
168+
Both project setups at a minimum contain:
169+
170+
- `Dockerfile`: Defines the container for running the application.
171+
- Application code: The executable code within the container.
172+
173+
Optional files included in both setups:
174+
175+
- `requirements.txt`: Lists dependencies needed for the application.
176+
177+
### Banana Configuration settings
178+
179+
Banana configuration settings are stored in a `banana_config.json` file.
180+
181+
Banana uses a `banana_config.json` file which contains things like Idle Timeout, Inference Timeout, and Max Replicas.
182+
183+
**Idle Timeout**
184+
185+
RunPod allows you to set an [Idle Timeout](/serverless/references/endpoint-configurations#idle-timeout) when creating the Endpoint.
186+
The default value is 5 seconds.
187+
188+
**Inference Timeout**
189+
190+
RunPod has a similar concept to Inference Timeout.
191+
For runs that are take less than 30 seconds to execute, you should use the `run_sync` handler.
192+
For runs that take longer than 30 seconds to execute, you should use the `sync` handler.
193+
194+
**Max Replicas**
195+
196+
When creating a Worker in RunPod, you can set the max Workers that will scale up depending on the amount of Worker sent to your Endpoint.
197+
For more informaiton, see [Scale Type](/serverless/references/endpoint-configurations#scale-type)
198+
199+
:::note
200+
201+
When creating a Worker, select the **Flashboot** option to optimize your startup time.
202+
203+
:::

0 commit comments

Comments
 (0)