Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions skyrl/backends/skyrl_train_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,18 @@ def _ensure_inference_engines(self):
self._render_server.shutdown()
self._render_server = None

def initialize_base_inference(self) -> None:
"""Start base-model inference before the first training model is created."""
if self._inference_engines_initialized:
return
self._cfg = _build_skyrl_train_config(self.base_model, self.config)
if not ray.is_initialized():
logger.info("Initializing Ray for base-model inference")
initialize_ray(self._cfg)
self._colocate_pg = self._create_colocate_pg() if self._cfg.trainer.placement.colocate_all else None

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like initialize_base_inference() is only ever called in non-colocated mode. What is the purpose of having this check here?

self._create_new_inference_client()
self._inference_engines_initialized = True

def _lora_signature_from(self, lora_config: types.LoraConfig) -> tuple:
# Tinker's public LoraConfig only exposes rank + alpha (plus
# seed/train_attn/train_mlp/train_unembed) - pending support https://github.com/NovaSky-AI/SkyRL/issues/1632.
Expand Down Expand Up @@ -497,6 +509,9 @@ def create_model(self, model_id: str, lora_config: types.LoraConfig, model_role:

logger.info("Building models.")
self._build_policy(PolicyWorker, model_id=model_id)
if self._inference_engines_initialized:
self._dispatch.set_inference_engine_client(self._inference_engine_client)
self.init_weight_sync_state()
Comment on lines +512 to +514

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The call to self._dispatch.set_inference_engine_client(self._inference_engine_client) is redundant here. self._dispatch is instantiated inside self._build_policy (called on the line immediately preceding this block) where self._inference_engine_client is already passed to the WorkerDispatch constructor. Removing this redundant call simplifies the initialization flow.

Suggested change
if self._inference_engines_initialized:
self._dispatch.set_inference_engine_client(self._inference_engine_client)
self.init_weight_sync_state()
if self._inference_engines_initialized:
self.init_weight_sync_state()

if is_lora:
self._base_lora_signature = self._lora_signature_from(lora_config)
elif model_role == "critic":
Expand Down
9 changes: 9 additions & 0 deletions skyrl/tinker/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ def __init__(
if hasattr(self.backend, "set_inference_state_publisher"):
self.backend.set_inference_state_publisher(self._write_inference_state_to_db)

is_colocated = bool(config.backend_config.get("trainer.placement.colocate_all", True))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using bool() directly on a configuration value retrieved from config.backend_config can lead to unexpected behavior if the value is parsed as a string (e.g., from command-line overrides or environment variables). In Python, bool("False") or bool("false") evaluates to True because any non-empty string is truthy. It is safer to explicitly check against common falsey string and boolean representations.

        colocate_val = config.backend_config.get("trainer.placement.colocate_all", True)
        is_colocated = colocate_val not in (False, "False", "false", 0, "0")

if not is_colocated and hasattr(self.backend, "initialize_base_inference"):
self.backend.initialize_base_inference()

# Track last cleanup time for periodic stale session cleanup
self._last_cleanup_time: float = time.time()

Expand All @@ -295,6 +299,11 @@ def _write_inference_state_to_db(self, proxy_url: str | None) -> None:
row.updated_at = datetime.now(timezone.utc)
session.add(row)
session.commit()
if proxy_url is not None:
logger.info(
'SKYRL_DEPLOYMENT_EVENT {"event":"inference_proxy_published","model":"%s"}',
self.config.base_model,
)

@contextmanager
def _checkpoint_status_context(self, model_id: str, checkpoint_id: str, checkpoint_type: types.CheckpointType):
Expand Down
22 changes: 22 additions & 0 deletions tests/tinker/skyrl_train/test_text_only_batch_no_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,25 @@ def shutdown(self):
assert fake_self._renderer is None
assert render_server.shutdown_called
assert fake_self._render_server is None


def test_base_inference_initializes_without_training_dispatch(monkeypatch):
cfg = SimpleNamespace(trainer=SimpleNamespace(placement=SimpleNamespace(colocate_all=False)))
calls = []
fake_self = SimpleNamespace(
_inference_engines_initialized=False,
base_model="model_test",
config=object(),
_cfg=None,
_colocate_pg=None,
_create_new_inference_client=lambda: calls.append("inference"),
)
monkeypatch.setattr(skyrl_train_backend, "_build_skyrl_train_config", lambda *_args: cfg)
monkeypatch.setattr(skyrl_train_backend.ray, "is_initialized", lambda: True)

skyrl_train_backend.SkyRLTrainBackend.initialize_base_inference(fake_self)

assert fake_self._cfg is cfg
assert not hasattr(fake_self, "_dispatch")
assert fake_self._inference_engines_initialized
assert calls == ["inference"]
Loading