Ray Distributed Computing Cheat Sheet
Scale Python workloads across clusters with Ray core tasks and actors plus Ray Train, Tune, and Serve for distributed ML workflows.
3 PagesAdvancedFeb 12, 2026
Tasks and Actors
Turn a plain function into a distributed task and a class into a stateful actor.
python
import rayray.init() # or ray.init(address="auto") to join an existing cluster@ray.remotedef square(x): return x * xfutures = [square.remote(i) for i in range(10)]results = ray.get(futures)@ray.remoteclass Counter: def __init__(self): self.n = 0 def incr(self): self.n += 1 return self.ncounter = Counter.remote()ray.get([counter.incr.remote() for _ in range(5)]) # -> 5
Distributed Training with Ray Train
Scale a PyTorch training loop across multiple GPUs/nodes with minimal code changes.
python
from ray.train.torch import TorchTrainerfrom ray.train import ScalingConfigdef train_loop_per_worker(config): model = build_model() model = ray.train.torch.prepare_model(model) for epoch in range(config["epochs"]): loss = train_one_epoch(model) ray.train.report({"loss": loss})trainer = TorchTrainer( train_loop_per_worker, train_loop_config={"epochs": 10}, scaling_config=ScalingConfig(num_workers=4, use_gpu=True),)result = trainer.fit()
Hyperparameter Search with Ray Tune
Run a distributed hyperparameter sweep with an early-stopping scheduler.
python
from ray import tunefrom ray.tune.schedulers import ASHASchedulerdef objective(config): for step in range(20): acc = train_step(config["lr"], config["batch_size"]) tune.report({"accuracy": acc})tuner = tune.Tuner( objective, param_space={"lr": tune.loguniform(1e-4, 1e-1), "batch_size": tune.choice([16, 32, 64])}, tune_config=tune.TuneConfig(scheduler=ASHAScheduler(metric="accuracy", mode="max"), num_samples=50),)results = tuner.fit()print(results.get_best_result().config)
Serve a Model with Ray Serve
Deploy a Python class as an autoscaling HTTP inference endpoint.
python
from ray import serve@serve.deployment(num_replicas=2, ray_actor_options={"num_gpus": 0.5})class Predictor: def __init__(self): self.model = load_model() async def __call__(self, request): data = await request.json() return {"prediction": self.model.predict(data["input"])}serve.run(Predictor.bind(), route_prefix="/predict")
Cluster CLI Essentials
Commands for launching and managing a Ray cluster.
- ray start --head- starts the head node of a Ray cluster on the local machine
- ray start --address=<head_ip>:6379- joins a worker node to an existing cluster
- ray status- shows current cluster resource usage and node count
- ray dashboard- opens the web UI for tasks, actors, and logs
- ray.init(address="auto")- connects a script to a running cluster instead of starting one locally
Pro Tip
Set ray_actor_options={"num_gpus": 0.5} in Ray Serve deployments to pack two lightweight model replicas onto a single GPU — fractional resource requests are honored by Ray's scheduler, not just documented as a nice idea.
Was this cheat sheet helpful?
Explore Topics
#RayDistributedComputing#RayDistributedComputingCheatSheet#DataScience#Advanced#TasksAndActors#Distributed#Training#Ray#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance