huggingface-zerogpu
Skill này làm gì
Skill này giúp AI xây dựng các demo machine learning chạy trên phần cứng ZeroGPU dùng chung của Hugging Face. Skill dành cho lập trình viên muốn Space của họ dùng GPU mà không gặp lỗi.
Khi nào nên dùng
- ✓Khi bạn viết hoặc rà soát mã dùng decorator GPU trên một ZeroGPU Space.
- ✓Khi bạn cần đặt phiên bản Python hoặc requirements cho một ZeroGPU Space.
- ✓Khi một demo ZeroGPU lỗi vì giới hạn thời gian, hạn mức hoặc quy tắc tiến trình.
Liên kết skill
https://github.com/huggingface/skills/blob/main/skills/huggingface-zerogpu/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Space ZeroGPU của tôi báo thời lượng GPU không hợp lệ. Hãy sửa decorator để demo chạy được.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
AI áp dụng các quy tắc của ZeroGPU, như cô lập tiến trình, cài đặt thời lượng GPU và giới hạn bản build CUDA. AI kiểm tra decorator, cách xử lý trạng thái và tính đồng thời để sửa lỗi.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Hugging Face ZeroGPU
Rules and patterns for ML demos on Hugging Face Spaces with ZeroGPU hardware. Covers @spaces.GPU, duration and quota tuning, process isolation, the CUDA availability model, concurrency safety, and CUDA build constraints.
Scope
This skill is for Gradio SDK Spaces using ZeroGPU hardware. Docker and Static Spaces cannot schedule onto ZeroGPU, and Streamlit apps now run as Docker Spaces — so this skill applies only to Gradio. For general Gradio coding (components, layouts, event listeners), see the huggingface-gradio skill in this repo. The authoritative ZeroGPU docs live at https://huggingface.co/docs/hub/spaces-zerogpu — refer to them for the current backing GPU, runtime version lists, and tier thresholds, all of which change over time.
Reference Files
| Reference | When to read |
|---|---|
references/concurrency.md |
Always read alongside SKILL.md when writing ZeroGPU code — handlers run in parallel by default |
references/how-zerogpu-works.md |
When reasoning about cold-starts, worker reuse, why module-scope warmup does not carry to requests, or why returning CUDA tensors hangs |
references/how-quota-works.md |
When choosing duration values, debugging illegal duration vs quota exceeded errors, or explaining why default 60s blocks short tasks |
references/cuda-and-deps.md |
When installing CUDA-dependent packages (e.g. flash-attn), pinning torch side-cars, or reading wheel filename tags |
Hardware
ZeroGPU exposes two GPU sizes that map to a fraction of the backing card:
size |
Slice of backing GPU | Quota cost |
|---|---|---|
large (default) |
Half | 1x |
xlarge |
Full | 2x |
Default large gives half a physical GPU, so memory bandwidth and compute are significantly lower than the full card's specs. Use xlarge only when the workload genuinely needs the extra memory or compute.
Backing GPU changes without notice. ZeroGPU has already migrated across GPU generations several times; older write-ups may name A100 or H200, but those are outdated. For the current backing GPU and exact per-size VRAM, always check the ZeroGPU docs before sizing workloads.
Basic Pattern
import spaces
import torch
from transformers import pipeline
pipe = pipeline("text-generation", model="...", device="cuda")
@spaces.GPU
def generate(prompt: str) -> str:
return pipe(prompt, max_new_tokens=100)[0]["generated_text"]
Key rules:
- Instantiate models at module scope and call
.to("cuda")eagerly. ZeroGPU handles the actual device mapping transparently (see CUDA availability model below). - Decorate GPU functions with
@spaces.GPU. The decorator is a no-op outside ZeroGPU, so it is safe to keep in all environments. - Set
durationto match the realistic worst-case workload (default 60s). The platform pre-checksrequested durationagainst the user'sremaining quota— not against the actual run time — so a 10-second task left at the 60s default fails withquota exceededas soon as the user's remaining quota drops below 60s. Smaller declareddurationalso ranks higher in the node-level queue. See "Duration and Quota" below. torch.compileis NOT supported. Use PyTorch ahead-of-time compilation (AoTI) (torch 2.8+) instead.- Use
size="xlarge"sparingly. It allocates the full backing GPU, but costs 2x quota and tends to queue longer.
@spaces.GPU(duration=120)
def generate_image(prompt: str):
return pipe(prompt).images[0]
CUDA Availability Model
Real GPU access is only available inside @spaces.GPU-decorated functions. Outside those functions, the GPU is not attached to the process.
However, import spaces monkey-patches torch so that:
torch.cuda.is_available()returnsTrueglobally..to("cuda")/device="cuda"calls at module scope succeed without error.
This is intentional. Module-scope model.to("cuda") calls register tensors with the ZeroGPU backend, which writes them to a disk offload directory at a startup "pack" step and frees the corresponding RAM. When a @spaces.GPU call lands, a forked GPU worker process streams those weights from disk into VRAM via a pinned-memory pipeline. Warm workers (reused across requests on the same GPU slot) keep weights resident on the GPU and skip the disk → V
Câu hỏi thường gặp
Lỗi ZeroGPU phổ biến là gì?
Một lỗi giới hạn thời gian, như thời lượng không hợp lệ, là một trường hợp phổ biến mà skill giải thích.
Skill có bao quát mọi việc lập trình Gradio không?
Không. Công việc Gradio chung được bao quát bởi một skill Gradio riêng.
Skill có hoạt động với Docker Spaces không?
Không. Skill chỉ áp dụng cho Gradio Spaces trên phần cứng ZeroGPU.
Chi tiết
Mô tả gốc của tác giả
AI demos and GPU compute with Gradio Spaces and Hugging Face Spaces ZeroGPU. Use when writing or reviewing code that uses `@spaces.GPU`, configuring `python_version` or `requirements.txt` for a ZeroGPU Space, or handling ZeroGPU-specific code constraints — pickle-based process isolation, `gr.State` semantics across the worker boundary, no `torch.compile` (use AoTI instead), CUDA wheel-only builds (no `nvcc` at build or runtime), large vs xlarge sizing, and dynamic duration callables. Make sure to use this skill whenever the user mentions ZeroGPU, `@spaces.GPU`, or the `spaces` Python package,
repo: huggingface/skills
Độ phổ biến
- Sao GitHub
- 11k
- Vị trí xếp hạng
- #11780
- Giấy phép
- Apache-2.0
- Cập nhật
- 30/10/2026
Bình luận
- …
Thêm skill từ kho mã này
hf-cli
11kGiúp agent của bạn dùng công cụ dòng lệnh Hugging Face để tải xuống, tải lên và quản lý model, dataset, Spaces, repo và bộ nhớ đệm cục bộ.
hf-cloud-aws-context-discovery
11kGiúp agent của bạn tìm AWS profile, region và thông tin tài khoản trước khi làm bất kỳ việc gì trên cloud, để không phải đoán tài khoản nào sẽ dùng.
hf-cloud-python-env-setup
11kGiúp agent của bạn thiết lập môi trường Python sạch với phiên bản Python phù hợp và thư viện mới nhất trước khi chạy SageMaker hoặc tự động hóa AWS.
hf-cloud-sagemaker-deployment-planner
11kGiúp agent của bạn lên kế hoạch triển khai model lên Amazon SageMaker, chọn lưu trữ thời gian thực, serverless, async hoặc batch với mặc định hợp lý.
Skill tương tự
api-connector-builder
280kGiúp agent của bạn xây dựng kết nối API mới theo đúng mẫu đã có, dùng khi bạn cần thêm một tích hợp mà không tạo cách làm mới.
api-design
280kGiúp agent của bạn thiết kế REST API theo các thực hành tốt nhất, hữu ích để định nghĩa cách API giao tiếp, hiển thị lỗi và tổ chức dữ liệu.
backend-patterns
280kGiúp agent của bạn với các mẫu kiến trúc phía máy chủ và tối ưu cơ sở dữ liệu, hữu ích khi xây dựng hoặc rà soát code chạy trên máy chủ.