# GPU sandboxes
URL: https://docs.munito.ai/sandboxes/gpu

Run Python code on one or more GPUs in an isolated EU sandbox. More powerful GPUs are coming.

## Hello, GPU [#hello-gpu]

Save this as `hello_gpu.py`. The decorator requests a GPU. Calling `.remote()` runs the function in the sandbox and returns its PyTorch version. [Hello world on GitHub](https://github.com/munitoai/demo/blob/b72da1fbdd932e9126c63143840848651ebbe915/sandboxes/gpu/python/hello_gpu.py)

```python
from munitoai import App

app = App(region="eu")


@app.function(gpu="L4")
def torch_version():
    import torch

    return torch.__version__


print(torch_version.remote())
```

Set `MUNITO_API_KEY` to a [workspace key](https://console.munito.ai/keys) for your organization. Run the file with [uv](https://docs.astral.sh/uv/).

```sh
uv run --python 3.12 --with munitoai==0.1.0 hello_gpu.py
```

The SDK releases the GPU when the function succeeds.

## Ray-trace a glass VI [#ray-trace-a-glass-vi]

<video
  controls
  loop
  muted
  playsInline
  preload="none"
  poster="/examples/vi/poster.jpg"
  width="480"
  height="480"
  aria-label="Studio lights sweep across a beveled glass VI and its reflection on a dark surface"
>
  <source src="/examples/vi/preview.mp4" type="video/mp4" />
</video>

This example renders 120 frames on one GPU. The [render helper](https://github.com/munitoai/demo/blob/b72da1fbdd932e9126c63143840848651ebbe915/sandboxes/gpu/python/glass_vi.py) uses the SDK to send the scene to the sandbox, retrieve the frames, and release the GPU. It encodes the MP4 locally.

```python
from munitoai import Munito

from glass_vi import render_video

with Munito(region="eu") as client:
    print(render_video(client, frames=120))
```

Clone the [public demo repository](https://github.com/munitoai/demo/tree/b72da1fbdd932e9126c63143840848651ebbe915/sandboxes/gpu/python) and run [the rendering example on GitHub](https://github.com/munitoai/demo/blob/b72da1fbdd932e9126c63143840848651ebbe915/sandboxes/gpu/python/render_vi.py). The command prints the path to `vi.mp4`.

```sh
git clone https://github.com/munitoai/demo.git
cd demo/sandboxes/gpu/python
uv run render_vi.py
```

The GPU sandbox produced the MP4 in **21.4–23.2 seconds**, including sandbox creation, file transfers, cleanup, and encoding. A ready eight-vCPU sandbox took **141.3–146.4 seconds**. [Benchmark results](https://github.com/munitoai/demo/blob/b72da1fbdd932e9126c63143840848651ebbe915/sandboxes/gpu/python/timings.json)

## Run PyTorch on two GPUs [#run-pytorch-on-two-gpus]

Request two GPUs on one host and print the device names that PyTorch sees. [Two-GPU example on GitHub](https://github.com/munitoai/demo/blob/b72da1fbdd932e9126c63143840848651ebbe915/sandboxes/gpu/python/two_gpus.py)

```python
from munitoai import App

app = App(region="eu")


@app.function(gpu="L4:2")
def gpu_names():
    import torch

    return [torch.cuda.get_device_name(i) for i in range(torch.cuda.device_count())]


print(gpu_names.remote())
```

Run it from the demo checkout.

```sh
uv run two_gpus.py
```

### Sum a tensor across both GPUs [#sum-a-tensor-across-both-gpus]

The launcher starts one worker per GPU. Each worker contributes its rank plus one. After `all_reduce`, both hold the sum, and the first worker prints `GPUs: 2, sum: 3`. [Worker on GitHub](https://github.com/munitoai/demo/blob/b72da1fbdd932e9126c63143840848651ebbe915/sandboxes/gpu/python/worker.py) · [Launcher on GitHub](https://github.com/munitoai/demo/blob/b72da1fbdd932e9126c63143840848651ebbe915/sandboxes/gpu/python/all_reduce.py)

```python
import os

import torch
import torch.distributed as dist

rank = int(os.environ["LOCAL_RANK"])
torch.cuda.set_device(rank)
dist.init_process_group("nccl")
try:
    value = torch.tensor([rank + 1], device=f"cuda:{rank}")
    dist.all_reduce(value)
    if rank == 0:
        print(f"GPUs: {dist.get_world_size()}, sum: {value.item()}")
finally:
    dist.destroy_process_group()
```

```sh
uv run all_reduce.py
```

## Train a tiny language model [#train-a-tiny-language-model]

Train a one-layer GPT to predict the next byte in a French sentence. The sandbox includes PyTorch and Transformers. [Training example on GitHub](https://github.com/munitoai/demo/blob/b72da1fbdd932e9126c63143840848651ebbe915/sandboxes/gpu/python/train_tiny_gpt.py)

```python
from munitoai import App

with App(region="eu") as app:
    @app.function(gpu="L4")
    def train():
        import torch
        from transformers import GPT2Config, GPT2LMHeadModel

        torch.manual_seed(7)
        tokens = torch.tensor([list(b"Paris est en France.")], device="cuda")
        model = GPT2LMHeadModel(GPT2Config(vocab_size=256, n_positions=32, n_embd=64, n_layer=1, n_head=2)).cuda()
        optimizer = torch.optim.AdamW(model.parameters(), lr=0.003)
        losses = []
        for _ in range(32):
            optimizer.zero_grad()
            loss = model(tokens, labels=tokens).loss
            loss.backward()
            optimizer.step()
            losses.append(loss.item())
        return [round(losses[0], 3), round(losses[-1], 3)]

    print(train.remote())
```

```sh
uv run train_tiny_gpt.py
```

In 32 updates, the loss on the training sentence fell from **5.623** to **0.345**.

## Workspace and limits [#workspace-and-limits]

A sandbox supports one to eight GPUs on the same machine. If the requested GPUs are unavailable, the API returns `503 no_capacity`. Retry after the interval in `Retry-After`, or request fewer GPUs.

GPU sandboxes have no outbound internet access. Custom images, private networks, and the pause, resume, and fork operations are unavailable.

## Hardware and pricing [#hardware-and-pricing]

Each NVIDIA L4 has 24 GB of GPU memory and costs **$0.78 per GPU-hour**, plus CPU and RAM. With one GPU and the default 2 vCPUs and 4 GiB of RAM, the total is **$0.798 per hour**. The usual sandbox plan and trial terms apply. Billing measures elapsed run time even when the GPU is idle. Delete the sandbox or let its timeout expire to end the charge. See [sandbox prices and limits](/sandboxes/pricing).
