πŸ€– AI/ML

Ray

Ray

A distributed ML framework. Parallel training, hyperparameter tuning.

πŸ“– Detailed Explanation

Ray is an open-source framework for distributed computing that makes it easy to run machine learning workloads in parallel across multiple machines. Developed at UC Berkeley's RISELab, it is popular among ML engineers because Python code can be run in a distributed environment with only minimal modifications.

Ray started at UC Berkeley in 2017, and in 2019 the commercial service Anyscale was founded. Existing distributed frameworks such as Spark and Dask are optimized for specific workloads, but Ray aims at general-purpose distributed computing and supports a variety of ML tasks such as reinforcement learning, hyperparameter tuning, and model serving.

The core of Ray is a distributed programming model based on Tasks (functions) and Actors (classes). A single @ray.remote decorator can turn a function into a distributed task, and Ray provides ML-specific libraries such as Ray Tune (hyperparameter tuning), Ray Train (distributed training), and Ray Serve (model serving).

In practice, Ray is becoming the standard for large-scale ML training pipelines. It is used by global companies such as OpenAI, Uber, and Shopify, and is especially active in LLM fine-tuning and reinforcement learning. With Anyscale's managed service, you can easily operate Ray clusters in the cloud.

πŸ’» Code Example

This is an example of distributed hyperparameter tuning using Ray.

# pip install ray[tune] torch
import ray
from ray import tune
from ray.tune.schedulers import ASHAScheduler

# Ray μ΄ˆκΈ°ν™”
ray.init()

# ν•™μŠ΅ ν•¨μˆ˜ μ •μ˜
def train_model(config):
    """ν•˜μ΄νΌνŒŒλΌλ―Έν„° config둜 λͺ¨λΈ ν•™μŠ΅"""
    import torch
    import torch.nn as nn

    model = nn.Sequential(
        nn.Linear(10, config["hidden"]),
        nn.ReLU(),
        nn.Linear(config["hidden"], 1)
    )

    optimizer = torch.optim.Adam(model.parameters(), lr=config["lr"])

    for epoch in range(10):
        loss = torch.rand(1).item()  # μ‹€μ œλ‘œλŠ” ν•™μŠ΅ 둜직
        tune.report({"loss": loss, "epoch": epoch})

# ν•˜μ΄νΌνŒŒλΌλ―Έν„° 검색 곡간
search_space = {
    "lr": tune.loguniform(1e-4, 1e-2),
    "hidden": tune.choice([32, 64, 128, 256]),
}

# ASHA μŠ€μΌ€μ€„λŸ¬ (μ‘°κΈ° μ’…λ£Œ)
scheduler = ASHAScheduler(max_t=10, grace_period=1, reduction_factor=2)

# λΆ„μ‚° ν•˜μ΄νΌνŒŒλΌλ―Έν„° νŠœλ‹ μ‹€ν–‰
tuner = tune.Tuner(
    train_model,
    param_space=search_space,
    tune_config=tune.TuneConfig(
        metric="loss",
        mode="min",
        num_samples=20,  # 20개 μ‹œλ„
        scheduler=scheduler,
    ),
)
results = tuner.fit()

print(f"Best config: {results.get_best_result().config}")

πŸ“Š Performance & Cost

As of January 2025, here is the pricing information for Ray and Anyscale.

Option Price Features
Ray OSS (Open Source) Free Self-hosted, community support
Anyscale Platform ~$0.50/hour~ Managed service, Pay-as-you-go
Anyscale Enterprise Custom quote SLA, dedicated support, governance

Cost-saving tips:

  • Using Spot Instances can save up to 60% on costs
  • Minimize idle resources with auto scaling
  • $100 in credits provided on new sign-up

πŸ—£οΈ Say It Like This in Practice

πŸ’¬ In a Meeting
"Training is taking too long, so it would be good to set up distributed training with Ray. If we scale up to 10 nodes, we can cut the training time to 1/8."
πŸ’¬ In an Interview
"Ray provides a distributed programming model based on Tasks and Actors. You can easily convert a function into a distributed task with the @ray.remote decorator."
πŸ’¬ In a Technical Discussion
"If we standardize on Ray Tune for hyperparameter tuning, Ray Train for distributed training, and Ray Serve for serving, we can manage the entire ML pipeline consistently."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Common Mistakes & Cautions

❌
Distributing small tasks excessively: Because tasks have overhead, distributing tasks that are too small actually makes things slower. Batch them together before distributing.
❌
Ignoring Object Store memory: Ray stores objects in shared memory. Transferring large amounts of data can cause out-of-memory errors, so use ray.put().
βœ…
Using the Dashboard: Use the Ray Dashboard (localhost:8265) to monitor task status, memory usage, and bottlenecks in real time.

πŸ”— Related Terms

πŸ“š Learn More