Skip to content

Commit 8534f6d

Browse files
Require pre-started Ray cluster for experiments instead of auto-starting
Non-Colab environments now must run `rapidfireai start` before creating an experiment, raising a clear ConnectionError otherwise. Colab retains the auto-start fallback. Also reformats pgvector RAG tutorial notebook. Made-with: Cursor
1 parent 07684a0 commit 8534f6d

2 files changed

Lines changed: 691 additions & 687 deletions

File tree

rapidfireai/experiment.py

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,19 @@ def __init__(
7373

7474
def _init_ray(self) -> None:
7575
"""
76-
Initialize or connect to a shared Ray cluster.
76+
Connect to a running Ray cluster started by ``rapidfireai start``.
7777
78-
If Ray is already initialized in this process, reuses the existing connection.
79-
If a Ray cluster is already running externally (e.g., started via `rapidfireai start`),
80-
connects to it. Otherwise, starts a new local cluster.
78+
If Ray is already initialized in this process (e.g., a previous experiment),
79+
reuses the existing connection. On Colab, starts a local cluster automatically
80+
since there is no terminal to run the CLI.
8181
82-
After connecting, kills any stale actors from previous experiments to free
83-
GPU/CPU resources for the new experiment.
82+
Raises:
83+
ConnectionError: If no Ray cluster is running (non-Colab only).
8484
"""
8585
import ray
8686

8787
self._ray = ray
8888

89-
# Set env vars needed by both modes before any Ray interaction
9089
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
9190
os.environ.setdefault("VLLM_LOGGING_LEVEL", "ERROR")
9291
os.environ.setdefault("RAY_LOG_TO_STDERR", "0")
@@ -95,47 +94,52 @@ def _init_ray(self) -> None:
9594
os.environ["RAY_DEDUP_LOGS"] = "0"
9695
os.environ["RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO"] = "0"
9796

98-
already_initialized = ray.is_initialized()
99-
100-
if not already_initialized:
101-
ray_runtime_env = {
102-
"env_vars": {
103-
"CUDA_LAUNCH_BLOCKING": "0",
104-
"CUDA_MODULE_LOADING": "LAZY",
105-
"TF_CPP_MIN_LOG_LEVEL": "3",
106-
"PYTORCH_CUDA_ALLOC_CONF": "max_split_size_mb:512",
107-
"NCCL_NET": "Socket",
108-
"NCCL_IB_DISABLE": "1",
109-
}
97+
if ray.is_initialized():
98+
return
99+
100+
ray_runtime_env = {
101+
"env_vars": {
102+
"CUDA_LAUNCH_BLOCKING": "0",
103+
"CUDA_MODULE_LOADING": "LAZY",
104+
"TF_CPP_MIN_LOG_LEVEL": "3",
105+
"PYTORCH_CUDA_ALLOC_CONF": "max_split_size_mb:512",
106+
"NCCL_NET": "Socket",
107+
"NCCL_IB_DISABLE": "1",
110108
}
109+
}
111110

112-
try:
113-
ray.init(
114-
address="auto",
115-
ignore_reinit_error=True,
116-
runtime_env=ray_runtime_env,
117-
)
118-
except ConnectionError:
119-
ray.init(
120-
logging_level=logging.ERROR,
121-
log_to_driver=False,
122-
configure_logging=True,
123-
include_dashboard=True,
124-
dashboard_host=RayConfig.HOST,
125-
dashboard_port=RayConfig.PORT,
126-
_metrics_export_port=None,
127-
runtime_env=ray_runtime_env,
111+
try:
112+
ray.init(
113+
address="auto",
114+
ignore_reinit_error=True,
115+
runtime_env=ray_runtime_env,
116+
)
117+
except ConnectionError:
118+
if not ColabConfig.ON_COLAB:
119+
raise ConnectionError(
120+
"No running RapidFire cluster found.\n"
121+
"Please run `rapidfireai start` in a terminal before creating an experiment."
128122
)
123+
ray.init(
124+
logging_level=logging.ERROR,
125+
log_to_driver=False,
126+
configure_logging=True,
127+
include_dashboard=True,
128+
dashboard_host=RayConfig.HOST,
129+
dashboard_port=RayConfig.PORT,
130+
_metrics_export_port=None,
131+
runtime_env=ray_runtime_env,
132+
)
129133

130-
if ColabConfig.ON_COLAB:
131-
try:
132-
from google.colab.output import eval_js
134+
if ColabConfig.ON_COLAB:
135+
try:
136+
from google.colab.output import eval_js
133137

134-
proxy_url = eval_js(f"google.colab.kernel.proxyPort({RayConfig.PORT})")
135-
print(f"🌐 Google Colab detected. Ray dashboard URL: {proxy_url}")
136-
except Exception as e:
137-
if hasattr(self, "logger"):
138-
self.logger.warning(f"Colab detected but failed to get proxy URL: {e}")
138+
proxy_url = eval_js(f"google.colab.kernel.proxyPort({RayConfig.PORT})")
139+
print(f"🌐 Google Colab detected. Ray dashboard URL: {proxy_url}")
140+
except Exception as e:
141+
if hasattr(self, "logger"):
142+
self.logger.warning(f"Colab detected but failed to get proxy URL: {e}")
139143

140144
def _init_fit_mode(self) -> None:
141145
"""Initialize fit-specific components."""

0 commit comments

Comments
 (0)