Skip to content
项目

关于

🐍 | 用于 Runpod API 和无服务器工作器 SDK 的 Python 库。

人工智能与机器学习

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
0
1个月
+2

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

Runpod Python

Define GPU functions in Python. Run them in the cloud with one decorator.

PyPI Package Downloads CI | Unit Tests License

Documentation • Examples • Discord

Using an AI agent? Point it to https://docs.runpod.io/agent-setup.md to install the Runpod skills and MCP server.

Installation

pip install runpod    # or: uv add runpod
rp login              # authenticate once

Requires Python 3.11+. Installing the package also installs the rp CLI.

Example

import runpod
from runpod import App, Model, NetworkVolume, Secret

app = App("inference")

checkpoints = NetworkVolume("checkpoints", size=100)
llama = Model("meta-llama/Llama-3.1-8B-Instruct")


# an autoscaling job queue on cloud H100s: weights pre-cached,
# dependencies vendored at deploy time, scale-to-zero when idle
@app.queue(
    gpu="H100",
    workers=(0, 3),
    dependencies=["vllm"],
    model=llama,
    env={"HF_TOKEN": Secret("hf-token")},
)
def chat(prompt: str):
    import vllm

    llm = vllm.LLM(model=str(llama.path))   # weights already on disk
    result = llm.generate([prompt])[0]
    return result.outputs[0].text           # results must be JSON values


# one ephemeral pod per call: provisions, runs to completion, terminates
@app.task(gpu="H100", mounts={"/checkpoints": checkpoints})
def finetune(steps: int = 1000):
    if steps < 1:
        raise ValueError("steps must be a positive integer")

    import torch

    model = torch.nn.Linear(1, 1).cuda()
    optimizer = torch.optim.SGD(model.parameters(), lr=0.05)
    x = torch.randn(1024, 1, device="cuda")
    y = 3 * x + 1
    for _ in range(steps):
        loss = ((model(x) - y) ** 2).mean()
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    torch.save(model.state_dict(), checkpoints.path / "model.pt")
    return {"loss": loss.item()}


@runpod.local_entrypoint
def main():
    print(chat.remote("why is the sky blue?"))   # blocks for the result
    print(finetune.remote(steps=500))            # pod terminates on return
rp secret add hf-token  # store your Hugging Face token once
rp flash dev main.py    # live dev session: edit, re-run, logs stream back
rp flash deploy         # deploy production endpoints

Recurring schedules are not supported by runpod.apps. For periodic jobs, use an external scheduler to invoke a deployed queue function with .spawn().

Contributing

Pull requests and issues are welcome — see the contributing guide to get started.

git clone https://github.com/runpod/runpod-python.git
cd runpod-python
make setup
make test

⚡ | Serverless Worker (SDK)

This python package can also be used to create a serverless worker that can be deployed to Runpod as a custom endpoint API.

Quick Start

Create a python script in your project that contains your model definition and the Runpod worker start code. Run this python code as your default container start command:

# my_worker.py

import runpod

def is_even(job):

    job_input = job["input"]
    the_number = job_input["number"]

    if not isinstance(the_number, int):
        return {"error": "Silly human, you need to pass an integer."}

    if the_number % 2 == 0:
        return True

    return False

runpod.serverless.start({"handler": is_even})

Make sure that this file is ran when your container starts. This can be accomplished by calling it in the docker command when you set up a template at console.runpod.io/serverless/user/templates or by setting it as the default command in your Dockerfile.

See our blog post for creating a basic Serverless API, or view the details docs for more information.

Local Test Worker

You can also test your worker locally before deploying it to Runpod. This is useful for debugging and testing.

python my_worker.py --rp_serve_api

📁 | Directory

.
├── docs               # Documentation
├── examples           # Examples
├── runpod             # Package source code
│   ├── api            # rest api v2 wrapper
│   ├── cli            # Command Line Interface Functions
│   ├── endpoint       # Language library - Endpoints
│   └── serverless     # SDK - Serverless Worker
└── tests              # Package tests

Discord

探索相关项目

加载中…