Skip to content
tridaoPublic

About

Rent a verified GPU box from Vast.ai, and make sure it is destroyed afterwards

Resources

Stars

8 stars

Watchers

0 watching

Forks

Repository files navigation

gpubox

Rent a GPU box from Vast.ai, RunPod, Lium, Verda or the VM clouds behind Shadeform, or take one from a Slurm cluster. gpubox checks that it works and manages its teardown. For CI jobs and dev boxes.

Marketplace hosts are unreliable: some never finish pulling the image, some come up with a GPU that is already busy. gpubox rents two at once, keeps the first that passes a health check, and destroys the other. Run gpubox down when done and schedule gpubox reap --namespace NS --destroy as a fallback. Some providers enforce expiry, but others rely on the reaper, as do rentals whose own lease timer never starts or is stopped by preemption.

The controller runs on Linux or macOS with Python 3.10+ and OpenSSH (ssh and ssh-keygen). No third-party Python dependencies. DESIGN.md explains how it works, what it assumes, and what was measured.

Install

pip install gpubox
export VASTAI_API_KEY=...

For a one-off CLI invocation, use uvx gpubox --help. In CI, pin a version (pip install gpubox==0.2.4): the job that runs gpubox holds your provider key. To install unreleased changes, pin a Git commit instead: pip install 'git+https://github.com/tridao/gpubox@<commit>'.

Keys are read from the environment (VASTAI_API_KEY, RUNPOD_API_KEY, LIUM_API_KEY, SHADEFORM_API_KEY, VERDA_CLIENT_ID and VERDA_CLIENT_SECRET), else from lines NAME=value in ~/.config/gpu-cloud-query/keys.env.

A dev box

gpubox up --gpu "RTX 4090" --max-dph-per-gpu 0.60 --image my/image:tag \
    --namespace dev --name mybox --ttl-min 480 --ssh-config
ssh mybox                      # also scp, rsync, editors
gpubox ssh -- nvidia-smi       # the same with the run's key, no ~/.ssh/config needed
gpubox renew --ttl-min 480     # the lease now ends 8 hours from now
gpubox down

--ssh-config adds a Host mybox entry to ~/.ssh/gpubox_config and one Include line at the top of ~/.ssh/config. down removes the entry.

On Vast, up --ttl-min 480 only tries offers that advertise at least eight hours of remaining lifetime. Pass the same --ttl-min to gpubox search to preview those offers without renting. renew moves gpubox's cleanup timer, not Vast's contract end time.

--gpu names a GPU model the same way on every provider, whatever the provider calls it: H100 is any H100, H100-SXM, H100-PCIe and H100-NVL one form of it (a different chip: other SMs, clocks, power and NVLink), and a memory can be added (A100 80GB). The same goes for A100-SXM/A100-PCIe, H200-SXM/H200-NVL, V100-SXM/V100-PCIe and RTXPRO6000-Server, -WS, -MaxQ; GeForce cards are RTX 4090 (not the 4090 D, nor the laptop chip), RTX 4070 Ti SUPER, and so on. The health gate checks the GPU the box has the same way. A name gpubox does not know (gpus.py) is matched word by word against each provider's names, and up says so. --gpu can be repeated to accept any of several models (the cheapest offer goes first). --min-cpus and --min-ram-gb are for the whole box; by default it gets max(16, 8 per GPU) CPUs and 32 GB of RAM per GPU. --min-cuda (default 13.0) is the lowest CUDA version the host's driver must support: a CUDA 13.x image runs on any 13.x driver but not on a 12.x one. A box whose driver turns out too old for the image's PyTorch fails without the machine being blamed, and offers with drivers as old are skipped.

A private image: --registry-user USER with the token in GPUBOX_REGISTRY_TOKEN (the registry comes from the image name). Vast and the host's docker daemon see the token, so make it a read-only pull token. Private pull reports omit the provider's raw status message and error body. Vast create refusals include recognized error codes: spend_limit_reached is a spending limit, not a shortage of offers.

--interruptible rents interruptible boxes: cheaper, but they can end at any time. On Vast it bids --bid-markup (1.25) times each offer's minimum bid; the price cap applies to the bid with the disk. An outbid box is stopped: its job dies, and the box keeps billing its disk, with its lease timer stopped too, until something destroys it. So run gpubox reap --namespace NS --destroy on a schedule, and expect wait to report a lost job (75). Two launches in different state directories can still bid on one machine at the same moment: the account's boxes are looked at once, when a launch starts. Machines the account already has a box on are skipped (our own bids would outbid each other), and a box that stops or never starts is not held against its machine. On Lium it also offers spot nodes (see below).

A large image needs a longer --startup-timeout than the default 300 s, which counts from the rental and so includes the image pull.

Jobs

gpubox run --script train.sh --env WANDB_API_KEY --timeout-min 600

run starts the script on the box in a session of its own and streams its output. The job does not depend on the connection: a dropped connection is picked up where it was (for up to --reconnect-min, 10), and Ctrl-C stops the streaming, not the job. --detach prints the job id and returns at once. Then:

gpubox logs -f [JOB]           # its output, until it ends; its exit code
gpubox wait [JOB]              # just the exit code
gpubox jobs                    # every job on the box: exit code, running or lost
gpubox kill [JOB]

(JOB defaults to the newest.) The box itself ends a job at --timeout-min (default 60; 0 for never), with exit 124. --destroy-after destroys the box when the script ends; a job can also run gpubox-destroy-self on the box whenever it is done.

The job sees the image's environment (Docker ENV, saved when the box starts; so do ssh sessions), then --env variables, then GPUBOX_RUN_ID, GPUBOX_JOB, GPUBOX_BOX_INDEX, GPUBOX_BOX_COUNT and GPUBOX_BOX_NAME. gpubox replaces the image's ENTRYPOINT to start its own sshd and timer: a job that needs the entrypoint's setup runs it itself (exec /entrypoint.sh python -m my.job).

Secrets. --env NAME (no =) takes the value from gpubox's own environment, so it is on no command line: op run -- gpubox run ..., a CI secret, Doppler and the like all work the same way. --env-file FILE reads NAME=VALUE lines. Values go over SSH into a file only root can read, which the job reads and deletes as it starts; they are never in Vast's instance settings. The host's operator has root on the machine, so send only credentials you can revoke: scoped, short-lived where possible.

Several boxes

gpubox up --count 4 --gpu "RTX 5090" --max-dph-per-gpu 0.80 --image my/image:tag \
    --namespace eval --name shard --ttl-min 240 --ssh-config
gpubox status                  # shard-0 ... shard-3: addresses, lease
ssh shard-2
gpubox run --box 2 --script job.sh
gpubox run --all --script shard.sh   # on every box: $GPUBOX_BOX_INDEX says which
gpubox down                    # all of them

All or nothing: up races count + --spare hosts and returns 0 only with --count healthy boxes; otherwise it destroys the ones it had. A run of one box is called NAME; a run of several, NAME-0, NAME-1, ...

RunPod

gpubox search --provider runpod --gpu "RTX 4090" --max-dph-per-gpu 0.80
gpubox up --provider runpod --gpu "RTX 4090" --max-dph-per-gpu 0.80 --image my/image:tag \
    --namespace dev --name mybox --ttl-min 240 --ssh-config

The same as on Vast, with RunPod's pods: the race, the gate, jobs, ssh, renew and the lease all work as they do there. RunPod chooses the machine: an offer is a GPU type in a data center that has it in stock, at RunPod's list price, and --gpu is read as everywhere (H100-SXM is RunPod's "NVIDIA H100 80GB HBM3"; without --gpu, any model under the price cap). A pod deletes itself when its lease ends, with the key RunPod gives each pod. By default, search and up consider both Secure and Community Cloud, cheapest first; ties prefer Secure. Use --runpod-cloud secure to avoid Community hosts, or --runpod-cloud community to restrict to them. Stock can change between search and launch. RunPod's API cannot require a public IP at launch, so gpubox waits for a usable direct SSH endpoint and does not hand over a pod without one. RunPod's basic SSH proxy is not a substitute for direct SSH and file transfer.

RunPod does not advertise a Vast-style contract end date. --ttl-min sets gpubox's cleanup timer, not a minimum uptime. The default disk is the pod's container disk; copy out anything you want to keep before stopping or deleting the pod. Use down to confirm teardown.

As everywhere, --min-cpus and --min-ram-gb default to max(16, 8 per GPU) CPUs and 32 GB per GPU: pass smaller ones to admit RunPod's smaller pods.

Not yet on RunPod: --interruptible, --registry-user (private images) and --exclude-country; and machines are not recorded (RunPod's API does not name them), so the blocklist does not apply. An image that cannot be pulled is not recognised as such: each pod is given up on after --stall-timeout.

Lium

gpubox search --provider lium --gpu "RTX 4090" --max-dph-per-gpu 0.60
gpubox up --provider lium --gpu "RTX 4090" --max-dph-per-gpu 0.60 --image my/image:tag \
    --namespace dev --name mybox --ttl-min 240 --ssh-config

Lium (LIUM_API_KEY) is a marketplace like Vast: an offer is one node (one machine's GPUs) at its own price per GPU, and a box is a pod on it, your image under gpubox's command. A node's CPUs, memory and disk are shared out in proportion to the GPUs rented, so --min-cpus, --min-ram-gb and --disk-gb are judged against that share; a node that rents only whole is offered only when --num-gpus is all its free GPUs. Nodes with no reliability score yet are left out. For a single consumer card, --min-cpus 4 --min-ram-gb 16 admits many more nodes.

Spot nodes are offered only with --interruptible, beside the others and cheapest first. A spot node's provider may close the rental at any time, with no credit: the pod is gone with its job and its files, and billing stops. There is no bid. A spot node needs no reliability score (most have none). Lium may rent at up to a cent per GPU-hour above the price offered, so that cent must fit under --max-dph-per-gpu too.

The sshd is Lium's, which it adds to the image if it has none, with the run's key (and without Lium's agent key; keys the image itself has are its own business). The lease is Lium's own: each pod's removal time, which renew moves; a pod cannot delete itself, so run --destroy-after is refused. Lium answers a repeated rent with the first one's answer, so a rent whose answer was lost is sent again rather than given up on. The pod's /root is its volume, with the image's files in it. A lease is at most 720 hours.

Use a key whose pod visibility is account (the default). A key with own visibility sees only the pods it rented, and nothing tells gpubox which kind a key is: down --run-id or reap with another such key would find nothing.

Not yet on Lium: --registry-user (private images), and machines are not recorded, so the blocklist does not apply.

Shadeform (VM clouds)

gpubox search --provider shadeform --gpu A6000 --max-dph-per-gpu 0.80
gpubox up --provider shadeform --gpu A6000 --max-dph-per-gpu 0.80 --image my/image:tag \
    --namespace dev --name mybox --ttl-min 120 --overall-timeout 1500
gpubox ssh                     # a shell in the box's container

Shadeform rents VMs on Hyperstack, Massed Compute, Lambda, Crusoe, Nebius and others with one key. A box there is your image run as a container on the VM: gpubox gives the VM a startup script that adds the run's key and starts the image, and reaches the box with SSH to the VM and docker exec, so the image needs no sshd (but, as everywhere, bash and python3). An offer is an instance type in a region, booted with Shadeform's own OS image with the newest CUDA, which must be at least --min-cuda (13.0 by default: pass --min-cuda 12.8 for an image built for CUDA 12). VMs take 5 to 8 minutes to boot; the start-up and stall limits count from when Shadeform expects the boot to end, but --overall-timeout should allow for it.

The lease is Shadeform's own: the VM is created with an auto-delete time, which renew moves. A VM cannot delete itself, so run --destroy-after is refused. Not yet: --ssh-config, --registry-user, --interruptible, --exclude-country, --account-id.

Hyperstack

--provider hyperstack (HYPERSTACK_API_KEY) rents Hyperstack's VMs directly, as boxes like Shadeform's: the image as a container on the VM, reached with SSH and docker exec, booted with Hyperstack's image "with Docker" of the newest CUDA in the region. Hyperstack keeps no expiry and a VM cannot delete itself, so up warns: only gpubox down, or a scheduled gpubox reap --provider hyperstack --namespace NS --destroy, ends a VM left behind. Hyperstack needs a key pair at the create: if the environment has none, the run's public key is imported once as gpubox. With --interruptible, its spot flavors are offered too, beside the others: Hyperstack deletes a spot VM without notice when it wants it back.

Massed Compute

--provider massed (MASSED_COMPUTE_API_KEY) rents Massed Compute's VMs directly, through its MCP server, as boxes like Shadeform's, on Massed's "Ubuntu Server 22.04 w/ drivers" (NVIDIA drivers and docker). Its 1-GPU products have 6 CPUs and 32 GB: pass --min-cpus 6. Massed cannot rename an instance, so the lease set by up --ttl-min cannot be moved (renew is refused), and as on Hyperstack only down or a scheduled reap ends a VM left behind.

Verda

gpubox search --provider verda --gpu H100 --max-dph-per-gpu 3 --interruptible
gpubox up --provider verda --gpu H100 --max-dph-per-gpu 3 --interruptible --image my/image:tag \
    --namespace dev --name mybox --ttl-min 120
gpubox ssh                     # a shell in the box's container

--provider verda rents Verda's (formerly DataCrunch) VMs in Finland, as boxes like Shadeform's: the image as a container on the VM, reached as ubuntu with SSH and docker exec, booted with Verda's image "+ Docker" of the newest CUDA the type supports. Its credentials are an OAuth client (VERDA_CLIENT_ID, VERDA_CLIENT_SECRET, from the console's Keys page). With --interruptible, spot VMs are offered too, at half the price, beside the others: Verda may evict one at any time, and it is then gone, its disk with it. A single H100, H200 or B200 is often to be had here.

Verda keeps no expiry and a VM cannot delete itself, so as on Hyperstack only gpubox down, or a scheduled gpubox reap --provider verda --namespace NS --destroy, ends a VM left behind. The OS disk (at least 50 GB) is billed apart from the VM, so the price cap does not cover it; down deletes it with the VM. Verda takes its money in pre-paid 10-minute steps and refunds what is unused later, so the balance can dip by more than a short run costs.

Lyceum

--provider lyceum (LYCEUM_API_KEY) rents Lyceum's VMs, as boxes like Shadeform's. Lyceum runs nothing of ours at boot, so the first probe that gets in sends the box's setup and starts it. Its region and disk (500 GB) are its own choice; as on Hyperstack only down or a scheduled reap ends a VM left behind.

A Slurm cluster

gpubox up --provider slurm --login mycluster --num-gpus 1 --gpu H100 \
    --namespace dev --name mybox --ttl-min 240
gpubox ssh                     # a shell in the allocation
gpubox run --script job.sh     # detached; follows its output
gpubox down

A box is one batch job holding an allocation (--gres=gpu:N, and --min-cpus, --min-ram-gb if given; otherwise the cluster's defaults). gpubox runs Slurm's commands on the login host over your own SSH configuration, so ssh mycluster squeue must work without a password prompt (keys, or a ControlMaster opened beforehand), and the login shell must be POSIX; Slurm must be 20.11 or later (srun --overlap). Everything runs in the nodes' own environment: --image is not used.

--partition, --slurm-account, --qos, --constraint and --sbatch-arg OPTION (repeatable) go to sbatch. A request the cluster can never satisfy (no such partition, too many GPUs, a time limit above the partition's) is a configuration error at once. Time in the queue is nobody's fault: --startup-timeout and --stall-timeout count from when the job starts, and only --overall-timeout bounds the wait. --spare is 0 by default here; a node that fails the gate is excluded from the jobs submitted after it.

The lease is kept twice: by the job's own script, which ends the job when the lease ends, and by Slurm's time limit, set two minutes past it. renew moves both; a cluster that does not let users extend a time limit refuses it, and the box then ends at its old one. Commands reach a box with srun --jobid=ID --overlap, so they get its GPUs (CUDA_VISIBLE_DEVICES), CPUs and environment; jobs started by run are started by the batch script, since Slurm ends whatever a step started when the step ends. The gate looks only at the allocation's GPUs. Not available: --ssh-config, search, and the machine blocklist and reputation store, which are about Vast's machines.

A CI job

- id: up
  run: >
    gpubox up --gpu "RTX 5090" --num-gpus 2 --max-dph-per-gpu 1.25
    --image my/ci-image:tag --namespace my-ci
    --name "${{ github.run_id }}-${{ github.run_attempt }}" --ttl-min 60
- run: gpubox run --script remote.sh --env "SHA=${{ github.sha }}" --timeout-min 40
- if: always() && steps.up.outputs.run_id != ''
  run: gpubox down --run-id "${{ steps.up.outputs.run_id }}"

and, from a schedule every half hour:

gpubox reap --namespace my-ci --destroy

up writes the run id as a step output before it rents anything. Passing it to down lets the teardown work even from a runner that has lost the state directory. Always run down after up, whatever up returned.

From Python

import gpubox

spec = gpubox.Spec(image="ghcr.io/org/img:1", gpus=("RTX 5090", "RTX 4090"),
                   max_dph_per_gpu=1.0, min_cpus=12, min_ram_gb=64, ttl_min=240)
with gpubox.up(spec, namespace="eval", name="shard", count=4) as run:
    jobs = run.start(open("shard.sh").read(), env={"TOKEN": token}, timeout_min=180)
    outcomes = gpubox.wait_all(jobs)   # per job: .code, or .error
# the boxes are destroyed when the block ends, however it ends (run.keep() to keep them)

If up fails or is interrupted after it began the run, it tears the run down before raising (and says how to retry if that fails too). A Run is a handle, not a lock: once the run is torn down or replaced in its state directory, whatever would touch its boxes raises StateError.

gpubox.attach(state_dir) picks up a run left in a state directory (also by the command line); run.boxes[i] has ssh_argv, sh, start, job, jobs; a Job has wait, logs and kill. gpubox.down(run_id=...) and gpubox.reap(namespace) are the teardowns. Failures are exceptions: ConfigError and StateError for our own mistakes, InfraError (NoBoxes, TeardownIncomplete, BoxLost, JobLost) for the provider's or a box's. gpubox.set_log(fn) sends what it says somewhere other than stdout, for the whole process. The command line is a thin layer over this (gpubox/api.py). Elsewhere than Vast, pass provider="runpod", or provider="slurm", options={"login": "mycluster", ...}, to up (and to down(run_id=...), reap and search); a run remembers where its boxes came from.

Commands

gpubox.receipt(state_dir, run_id) reads the durable rental attempts, keyed by tag. Each has an offer, machine, host, quoted dph, instance_id (when known) and outcome: unknown, refused, rented or gone. Use a persistent state directory per run. Vast's documented API requests too frequent response (HTTP 429) is a refusal. The launcher waits one poll interval before trying that offer again, if it still needs a box. A bare or unrecognized 429, like a timeout, stays unknown until its box is identified; down with that state directory reports incomplete even if the listing is empty. Retry down later. A missing receipt or teardown by tag alone is not proof that no rental was made. A receipt is a record of quoted compute cost, not an invoice or account-wide spending cap.

up rent boxes (--count, default 1) and wait until they pass the health gate; --provider runpod, shadeform or slurm for those
status show the run's boxes: names, addresses, lease
run start a local script on a box, detached, and stream its output
logs a job's output (-f: until it ends)
wait wait for a job to end
jobs the jobs on a box
kill stop a job
ssh a shell on a box, or -- CMD run there
renew move the end of the boxes' lease
down destroy everything the run rented
reap destroy boxes of a namespace whose lease has ended (without --destroy: report)
search show the offers up would try; rents nothing

Exit codes: 0 ok; 2 configuration (bad arguments, no API key, an image that cannot be pulled or does not start our sshd); 75 infrastructure (no healthy box, box lost mid-run, teardown not confirmed); 124 script timeout; 130 interrupted. run, wait and logs -f otherwise return the job's own exit code.

State lives in --state-dir (default .gpubox): run.json, an SSH key per run, and observations.jsonl.

How it works

  • Race. up rents --count + --spare hosts (2 by default), cheapest first, one machine per host. A box that fails is destroyed and replaced, up to --max-attempts rentals (default 2 * count + 4).
  • Health gate. GPU model, count and VRAM; required URLs reachable (--must-reach); the GPU idle before we touch it. Where the image has a CUDA build of PyTorch, also: CUDA works, a matmul on every GPU matches the CPU, and most of the VRAM can be allocated. It needs python3 in the image.
  • Your own checks. --gate-script FILE runs on each box after the gate passes (by its #! line, else bash; at most --gate-timeout seconds). Exit 0 admits the box; anything else fails it and another is tried. To have the machine blocklisted, print a last line {"ok": false, "reason": "no_network", "detail": "..."} with one of the gate's reason codes (policy.REASONS).
  • Tag. Every box is labelled <namespace>.<name>.<random>.a<n>.<expiry> when it is rented. Anything that knows the run id can find the run's boxes and destroy them.
  • Lease. The lease ends --ttl-min after up started. A timer on the box or the provider enforces it where supported. A scheduled reap destroys expired boxes when there is no timer or it could not run. renew moves the end on providers that support it.
  • Blocklist. A machine that failed in a launch that succeeded is skipped for 2 hours to 14 days, depending on why (observations.jsonl; cache it between CI runs). A launch that failed blames nobody.
  • Defaults from measurement. Hosts in mainland China are skipped (--exclude-country, default CN; none to skip none). A box whose loading status has not changed for --stall-timeout (300 s) is replaced.

What it promises, and what it does not

  • up exits 0 only with --count boxes that passed the gate, were still running when looked up afterwards, and are recorded in run.json. Before that, a failure or Ctrl-C rolls back what was rented. The rollback gets about ten seconds and is an attempt; down finishes it.
  • At most --count + --spare boxes are billable at once, as far as gpubox can know. A rental request that got no answer keeps its slot.
  • No offer above the price cap is rented, whatever the search returned.
  • down exits 0 only when every box it knows of is confirmed gone by id and two listings in a row show nothing with the run's tag. Otherwise it exits 75 and says what is left and what to do.
  • Not promised: a maximum lifetime. With no working lease timer or provider-enforced expiry, a box that down missed can keep billing until the reaper removes it. Nothing here finds a box that was renamed, or that the provider does not list. There is no spending cap across runs.

--account-id (or GPUBOX_ACCOUNT_ID) makes every command refuse an API key that belongs to another account.

Sharing what machines did

Optional. With GPUBOX_STORE_URL and GPUBOX_STORE_TOKEN set, observations of machines are shared through a small Cloudflare Worker (store/), so that a machine that failed for one client is skipped by the others. It is advisory: it costs a command a few seconds at most and never fails it.

Development checks

python3 -m pip install pytest pre-commit
pre-commit install         # lint and format staged Python files with Ruff
pre-commit run --all-files
(cd store && npm ci && npm test)  # Node 24 LTS; also enables the Python/Worker integration test
python3 -m pytest          # fake provider and fake SSH; rents nothing
python3 tests/mutations.py # breaks each behaviour in turn; a test named for it must fail

On PRs and pushes to main, GitHub Actions runs the hooks, tests Python 3.10 and current Python 3, and checks the metadata and installation of both the wheel and the source distribution. The current-Python job also tests the Worker and Python client against local D1. All jobs use GitHub-hosted Ubuntu runners; no GPUs or cloud credentials are needed. Mutation tests run weekly or on demand (Actions → Mutation tests).

About

Rent a verified GPU box from Vast.ai, and make sure it is destroyed afterwards

Resources

Stars

8 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages