sdk overview

run your code on gpu machines from a python script.

castform compute runs your code on gpu machines from a python script. you describe an image and a container, call run(), and read the logs. the sdk handles machines, builds and restarts.

install and log in

the sdk ships as the castform python package (python 3.10 or later), which also provides the castform command.

pip install castform
castform login
python train_job.py

castform login opens a browser to approve the session and picks your workspace. your script runs locally; the commands it launches run remotely.

a first run

import castform

container = castform.Container(
    name="hello",
    image=castform.Image.from_registry("python:3.12-slim"),
    gpu="H100",
)
process = container.run(["python", "-c", "print('hello from the container')"])

for line in process.logs(follow=True):
    print(line, end="")
print("exit status", process.wait())

run() creates and starts the container if needed, launches the command, and returns a Process once it has launched. it does not wait for the program to finish: process.wait() does, and returns the exit status.

images

an Image is a recipe. each method returns a new image, so recipes chain.

methodwhat it adds
Image.from_registry(ref)a base image, e.g. "python:3.12"
.pip_install(*packages)python packages
.apt_install(*packages)os packages
.run_commands(*commands)shell commands run during the build
.add_local_dir(path, destination)a local directory, uploaded at start
.add_local_file(path, destination)a local file, uploaded at start
image = (
    castform.Image.from_registry("python:3.12")
    .pip_install("torch", "transformers")
    .add_local_dir("./src", "/workspace/src")
)

local files are uploaded when the container is created, not built into the image, so editing them doesn’t rebuild; container.sync() copies new versions into a running container. pass copy=True to build them into the image instead. an unchanged registry image starts without a build.

containers

container = castform.Container(
    name="sft-run",
    image=image,
    gpu="H100:8",
    env={"WANDB_MODE": "offline"},
    working_dir="/workspace/src",
)
argumentmeaning
nameunique among your active containers
imagethe image recipe to run
gpu"MODEL:COUNT", e.g. "H100:8" or "A100-80GB:4"
envenvironment variables for every command
working_dirwhere commands start

lifecycle

callwhat it does
container.start()provision a machine and wait until the container is ready
container.run(command)launch a command, starting the container first if needed
container.stop()stop the machine and everything running on it; keep the definition
container.delete()delete the container
container.refresh().statethe current state, e.g. "ready"
Container.get(name)reconnect to an existing container from another script

states are stopped, provisioning, starting, ready, stopping, failed and deleted. start(), stop() and delete() wait for their target state and accept a timeout.

a command is a string, run with sh -c so pipes and && work, or a list such as ["python", "train.py"], run as is. commands can run side by side in the same container.

run() returns a Process:

callwhat it does
process.wait()wait for the command to exit and return its exit status
process.poll()the exit status if it has exited, else None; doesn’t wait
process.logs(follow=True)this command’s output lines; ends when it exits
process.returncodethe exit status once seen, else None

an exit status of 128+N means signal N ended the command (137 is often out of memory).

logs

for text in container.logs(follow=True):
    print(text, end="")

logs() yields the output of your commands, image builds and lifecycle events (machine acquired, container ready, and so on). narrow it with sources="command", "build" or "lifecycle". follow=True keeps waiting for new lines until you stop reading. since="30m" (or "2h", "1d", a datetime) skips older lines.

log_events() returns the same records with their source, timestamp and a cursor you can pass back later to resume where you left off.

errors

every error derives from castform.CastformError. the ones you are most likely to handle:

errorwhen
AuthenticationErrornot logged in, or the credentials expired
AlreadyExistsErrora container with that name already exists
BuildFailedErrorthe image build failed; the build logs say why
ContainerStartErrorthe container could not start
QuotaExceededErrorthe request is over your workspace’s quota
CommandLostErrorthe command ended without an exit status, e.g. its machine was preempted
WaitTimeoutErroryour timeout ran out; the work goes on

external sandboxes

training jobs sometimes rely on external sandboxes (modal, e2b, daytona, etc.) to execute tool calls generated by the llm being trained. those connections have to survive preemptions too: add @cr.rollout (from castform.recovery) to your rollout function, and after a preemption it replays any rollouts that were in flight in new sandboxes and cleans up the old ones.

see external sandboxes to set it up.

running without a browser

for ci, set CASTFORM_WORKSPACE and CASTFORM_API_KEY in the environment instead of running castform login.