Lium SDK
Rent GPUs from Python with an API key. The lium.io package ships the Python SDK and the CLI. Pods are billed per second.
Signatures, models and exceptions: Open SDK Reference
Set up
Install
pip install lium.io
Sign in
lium init
This saves your API key to ~/.lium/config.ini. Then Lium() signs in by itself:
from lium.sdk import Lium
lium = Lium()
In CI, set LIUM_API_KEY instead. It wins over the config file:
export LIUM_API_KEY="sk_..."
Two ways to run work
@lium.machine: run one Python function on a GPU.Lium(): manage pods yourself.
@lium.machine decorator
Call the function like any other. The SDK rents the cheapest matching node, runs the function there, returns the result and removes the pod (or keeps it warm).
import lium
@lium.machine(machine="RTX4090", requirements=["transformers", "accelerate"], keep_warm=300)
def infer(prompt: str) -> str:
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
model_id = "HuggingFaceTB/SmolLM2-135M-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16, device_map="cuda")
text = tokenizer.apply_chat_template([{"role": "user", "content": prompt}], tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to("cuda")
out = model.generate(**inputs, max_new_tokens=64, do_sample=False, pad_token_id=tokenizer.eos_token_id)
return tokenizer.decode(out[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True).strip()
print(infer("Who discovered penicillin?")) # cold: rent + boot + install, ~1-2 min
print(infer("And who first mass-produced it?")) # warm pod, cached environment: ~20 s
infer.close() # remove the warm pod (otherwise: 300 s + 2 min later)
machineis"<count>x<gpu>"or"<gpu>", for example"1xH200"or"RTX4090".- Import inside the function and pass everything else as arguments. See what can travel.
timeoutdefaults to 1 hour.f.map(items)runs a batch on one pod.f.local(...)runs on your machine.
Full guide: Serverless functions with @lium.machine.
Lium() client
List nodes, start a pod, wait for it, run a command, remove it:
from lium.sdk import Lium
lium = Lium()
ready = None
try:
executor = lium.ls(gpu_type="A100", gpu_count=1)[0]
pod = lium.up(executor_id=executor.id, name="demo")
ready = lium.wait_ready(pod, timeout=600)
if ready is None:
raise RuntimeError("Pod did not become ready before the timeout")
print(lium.exec(ready, command="nvidia-smi")["stdout"])
finally:
if ready is not None:
lium.down(ready)
wait_ready returns None if the pod is still starting at the timeout. It raises PodStartError if the pod failed.
Full guide: Pod Lifecycle with Lium().
The account event log
Lium.events() lists what happened on the account, newest first: rents, reboots, deletions and key changes, with who did it.
from datetime import datetime, timedelta, timezone
from lium.sdk import Lium
lium = Lium()
for e in lium.events(since=datetime.now(timezone.utc) - timedelta(hours=24)):
actor = e.get("actor") or {}
by = actor.get("api_key_name") or actor.get("auth") or "platform" # key name · "session" · the platform itself
print(e["created_at"], e.get("pod_name"), e.get("sub_event_type"), by)
The CLI equivalent is lium audit.