|
| 1 | +# Spaces ZeroGPU: Dynamic GPU Allocation for Spaces |
| 2 | + |
| 3 | +<img src="https://cdn-uploads.huggingface.co/production/uploads/5f17f0a0925b9863e28ad517/naVZI-v41zNxmGlhEhGDJ.gif" style="max-width: 440px; width: 100%" alt="ZeroGPU schema" /> |
| 4 | + |
| 5 | +ZeroGPU is a shared infrastructure that optimizes GPU usage for AI models and demos on Hugging Face Spaces. It dynamically allocates and releases NVIDIA A100 GPUs as needed, offering: |
| 6 | + |
| 7 | +1. **Free GPU Access**: Enables cost-effective GPU usage for Spaces. |
| 8 | +2. **Multi-GPU Support**: Allows Spaces to leverage multiple GPUs concurrently on a single application. |
| 9 | + |
| 10 | +Unlike traditional single-GPU allocations, ZeroGPU's efficient system lowers barriers for developers, researchers, and organizations to deploy AI models by maximizing resource utilization and power efficiency. |
| 11 | + |
| 12 | +## Using and hosting ZeroGPU Spaces |
| 13 | + |
| 14 | +- **Using existing ZeroGPU Spaces** |
| 15 | + - ZeroGPU Spaces are available to use for free to all users. (Visit [the curated list](https://huggingface.co/spaces/enzostvs/zero-gpu-spaces)). |
| 16 | + - [PRO users](https://huggingface.co/subscribe/pro) get x5 more daily usage quota and highest priority in GPU queues when using any ZeroGPU Spaces. |
| 17 | +- **Hosting your own ZeroGPU Spaces** |
| 18 | + - Personal accounts: [Subscribe to PRO](https://huggingface.co/settings/billing/subscription) to access ZeroGPU in the hardware options when creating a new Gradio SDK Space. |
| 19 | + - Organizations: [Subscribe to the Enterprise Hub](https://huggingface.co/enterprise) to enable ZeroGPU Spaces for all organization members. |
| 20 | + |
| 21 | +## Technical Specifications |
| 22 | + |
| 23 | +- **GPU Type**: Nvidia A100 |
| 24 | +- **Available VRAM**: 40GB per workload |
| 25 | + |
| 26 | +## Compatibility |
| 27 | + |
| 28 | +ZeroGPU Spaces are designed to be compatible with most PyTorch-based GPU Spaces. While compatibility is enhanced for high-level Hugging Face libraries like `transformers` and `diffusers`, users should be aware that: |
| 29 | + |
| 30 | +- Currently, ZeroGPU Spaces are exclusively compatible with the **Gradio SDK**. |
| 31 | +- ZeroGPU Spaces may have limited compatibility compared to standard GPU Spaces. |
| 32 | +- Unexpected issues may arise in some scenarios. |
| 33 | + |
| 34 | +### Supported Versions |
| 35 | + |
| 36 | +- Gradio: 4+ |
| 37 | +- PyTorch: 2.0.1, 2.1.2, 2.2.2, 2.4.0 (Note: 2.3.x is not supported due to a [PyTorch bug](https://github.com/pytorch/pytorch/issues/122085)) |
| 38 | +- Python: 3.10.13 |
| 39 | + |
| 40 | +## Getting started with ZeroGPU |
| 41 | + |
| 42 | +To utilize ZeroGPU in your Space, follow these steps: |
| 43 | + |
| 44 | +1. Make sure the ZeroGPU hardware is selected in your Space settings. |
| 45 | +2. Import the `spaces` module. |
| 46 | +3. Decorate GPU-dependent functions with `@spaces.GPU`. |
| 47 | + |
| 48 | +This decoration process allows the Space to request a GPU when the function is called and release it upon completion. |
| 49 | + |
| 50 | +### Example Usage |
| 51 | + |
| 52 | +```python |
| 53 | +import spaces |
| 54 | +from diffusers import DiffusionPipeline |
| 55 | + |
| 56 | +pipe = DiffusionPipeline.from_pretrained(...) |
| 57 | +pipe.to('cuda') |
| 58 | + |
| 59 | +@spaces.GPU |
| 60 | +def generate(prompt): |
| 61 | + return pipe(prompt).images |
| 62 | + |
| 63 | +gr.Interface( |
| 64 | + fn=generate, |
| 65 | + inputs=gr.Text(), |
| 66 | + outputs=gr.Gallery(), |
| 67 | +).launch() |
| 68 | +``` |
| 69 | + |
| 70 | +Note: The `@spaces.GPU` decorator is designed to be effect-free in non-ZeroGPU environments, ensuring compatibility across different setups. |
| 71 | + |
| 72 | +## Duration Management |
| 73 | + |
| 74 | +For functions expected to exceed the default 60-second of GPU runtime, you can specify a custom duration: |
| 75 | + |
| 76 | +```python |
| 77 | +@spaces.GPU(duration=120) |
| 78 | +def generate(prompt): |
| 79 | + return pipe(prompt).images |
| 80 | +``` |
| 81 | + |
| 82 | +This sets the maximum function runtime to 120 seconds. Specifying shorter durations for quicker functions will improve queue priority for Space visitors. |
| 83 | + |
| 84 | +## Hosting Limitations |
| 85 | + |
| 86 | +- **Personal accounts ([PRO subscribers](https://huggingface.co/subscribe/pro))**: Maximum of 10 ZeroGPU Spaces. |
| 87 | +- **Organization accounts ([Enterprise Hub](https://huggingface.co/enterprise))**: Maximum of 50 ZeroGPU Spaces. |
| 88 | + |
| 89 | +By leveraging ZeroGPU, developers can create more efficient and scalable Spaces, maximizing GPU utilization while minimizing costs. |
| 90 | + |
| 91 | +## Feedback |
| 92 | + |
| 93 | +You can share your feedback on Spaces ZeroGPU directly on the HF Hub: https://huggingface.co/spaces/zero-gpu-explorers/README/discussions |
0 commit comments