TimeGPT on Ray

Ray is an open-source unified compute framework that helps scale Python workloads for distributed computing. In this tutorial, you will learn how to distribute TimeGPT forecasting jobs on top of Ray.

This guide uses Fugue to easily run code across various distributed computing frameworks, including Ray.

Overview

Below is an outline of what we’ll cover:

  1. Installation
  2. Load Your Data
  3. Initialize Ray
  4. Use TimeGPT on Ray
  5. Shutdown Ray
1

1. Installation

Install Ray using Fugue. Fugue provides an easy-to-use interface for distributed computation. It lets you run Python code on several distributed computing frameworks, including Ray.

Fugue Ray Installation
pip install fugue[ray]

When executing on a distributed Ray cluster, ensure the nixtla library is installed on all workers.

2

2. Load Your Data

Load your dataset into a pandas DataFrame. This tutorial uses hourly electricity prices from various markets:

Load Dataset Example
import pandas as pd

df = pd.read_csv(
    'https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short.csv',
    parse_dates=['ds'],
)
df.head()

Preview of the first few rows of data

3

3. Initialize Ray

Here, we’re spinning up a Ray cluster locally by creating a head node. You can scale this to multiple machines in a real cluster environment.

Ray Cluster Initialization
import ray
from ray.cluster_utils import Cluster

ray_cluster = Cluster(
    initialize_head=True,
    head_node_args={"num_cpus": 2}
)

ray.init(address=ray_cluster.address, ignore_reinit_error=True)

# Convert your DataFrame to Ray format:
ray_df = ray.data.from_pandas(df)
ray_df
4

4. Use TimeGPT on Ray

With Ray, you can run TimeGPT similar to a standard (non-distributed) local environment. Operations such as forecast still apply directly to Ray Dataset objects.

5

5. Shutdown Ray

Always shut down Ray after you finish your tasks to free up resources.

Shutdown Ray Example
ray.shutdown()

Congratulations! You’ve successfully used TimeGPT on Ray for distributed forecasting.