跳到正文
Skillpedia
部署与云(DevOps)51k · #3499

modal

它能做什么

Modal 是一个云平台,让你无需管理服务器,就能使用强大的计算机(包括 GPU)运行 Python 代码,尤其适合 AI 和机器学习任务。

适用场景

  • ✓当你需要用高性能 GPU 训练或运行 AI 模型时。
  • ✓当你想创建能响应请求的在线服务,比如网站或应用时。
  • ✓当你想把需要定期运行的重复性任务放到云端自动执行时。

技能链接

https://github.com/K-Dense-AI/scientific-agent-skills/blob/main/skills/modal/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/K-Dense-AI/scientific-agent-skills/blob/main/skills/modal/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我想用一块 Nvidia H100 GPU 训练一个电影推荐 AI 模型,并把结果保存到持久化存储中。”

每周 5 个新技能,1 分钟读完。

工作原理

你先编写 Python 代码,然后用 Modal 的工具指定所需资源(比如 GPU 或存储)。Modal 会负责在云服务器上运行你的代码,并根据需要自动扩容。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Modal

Overview

Modal is a cloud platform for running Python code serverlessly, with a focus on AI/ML workloads. Key capabilities:

  • GPU compute on demand (including H100, H200, B200 and B300)
  • Serverless functions with autoscaling from zero to thousands of containers
  • Custom container images built entirely in Python code
  • Persistent storage via Volumes for model weights and datasets
  • Web endpoints for serving models and APIs
  • Scheduled jobs via cron or fixed intervals
  • Container reuse and warm pools to reduce cold starts (model loading still takes time)

Modal Apps and Images are defined in Python; existing Dockerfiles are also supported.

When to Use This Skill

Use this skill when:

  • Deploy or serve AI/ML models in the cloud
  • Run GPU-accelerated computations (training, inference, fine-tuning)
  • Create serverless web APIs or endpoints
  • Scale batch processing jobs in parallel
  • Schedule recurring tasks (data pipelines, retraining, scraping)
  • Need persistent cloud storage for model weights or datasets
  • Want to run code in custom container environments
  • Build job queues or async task processing systems

Installation and Authentication

Install

uv pip install "modal==1.6.0"

Reviewed against SDK 1.6.0 and current release notes. Local SDK construction and selected handlers were tested; remote builds, deployments, GPU inference and cloud limits were not executed. Snippets using model/data placeholders or third-party workloads are illustrative and require their stated dependencies.

Authenticate

Prefer existing credentials before creating new ones. Only the two Modal-specific variables below are relevant — do not read, load, or expose any other environment variables or .env file contents:

  1. Reuse an existing Modal profile, or check whether both MODAL_TOKEN_ID and MODAL_TOKEN_SECRET are set; report presence only.
  2. If not, look up only those two keys in a local .env file (ignore all other entries) and load them if appropriate for the workflow.
  3. Only fall back to interactive modal setup if no usable profile or token pair exists. The SDK supports profiles in .modal.toml; environment tokens are optional when a profile authenticates.
modal setup

This opens a browser for authentication. For CI/CD or headless environments, use environment variables:

export MODAL_TOKEN_ID=<your-token-id>
export MODAL_TOKEN_SECRET=<your-token-secret>

If neither an existing profile nor a token pair is available, create credentials at https://modal.com/settings

Check current pricing and workspace quotas before sizing a run. GPU use requires a payment method even when credits remain.

Reference: See references/getting-started.md for detailed setup and first app walkthrough.

Core Concepts

App and Functions

A Modal App groups related functions. Functions decorated with @app.function() run remotely in the cloud:

import modal

app = modal.App("my-app")

@app.function()
def square(x):
    return x ** 2

@app.local_entrypoint()
def main():
    # .remote() runs in the cloud
    print(square.remote(42))

Run with modal run script.py. Deploy with modal deploy script.py.

Reference: See references/functions.md for lifecycle hooks, classes, .map(), .spawn(), and more.

Container Images

Modal builds container images from Python code. The recommended package installer is uv:

image = (
    modal.Image.debian_slim(python_version="3.11")
    .uv_pip_install("torch==2.12.0", "transformers==5.9.0", "accelerate==1.13.0")
    .apt_install("git")
)

@app.function(image=image, gpu="L40S")
def inference(prompt):
    from transformers import pipeline
    pipe = pipeline("text-generation", model="openai-community/gpt2", device=0)
    return pipe(prompt)

Key image methods:

  • .uv_pip_install() — Install Python packages with uv (recommended)
  • .pip_install() — Install with pip (fallback)
  • .apt_install() — Install system packages
  • .run_commands() — Run shell commands during build
  • .run_function() — Run Python during build (e.g., download model weights)
  • .add_local_python_source() — Add local modules
  • .env() — Set environment variables

Reference: See references/images.md for Dockerfiles, micromamba, caching, GPU build steps.

GPU Compute

Request GPUs via the gpu parameter:


…
常见问题

我该如何设置 Modal?

你可以用 `uv pip install "modal==1.6.0"` 安装 Modal,然后在终端中运行 `modal setup` 进行身份验证,它会打开浏览器;你也可以通过环境变量设置你的令牌来完成验证。

什么是“无服务器函数”?

它们是由 Modal 在云端按需运行的代码片段。你不需要操心服务器的开启或关闭,代码需要运行时,Modal 会自动处理。

我能使用自己的 Docker 容器吗?

可以。Modal 支持使用你自己的 Docker 容器,也可以直接在 Python 代码中构建新的容器,这样就能很方便地为应用配置运行环境。

详情

作者的原始描述

Modal is a serverless cloud platform for running Python on demand, including on-demand GPUs. Use when deploying or serving AI/ML models, running GPU-accelerated workloads (training, fine-tuning, inference), serving web endpoints, scheduling batch jobs, or scaling Python code to cloud containers with the Modal SDK.

repo: K-Dense-AI/scientific-agent-skills

热度

GitHub 星标
51k
排名
#3499
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能