-
Notifications
You must be signed in to change notification settings - Fork 112
K8s/XCalibur Support #579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sjayaram-nv
wants to merge
11
commits into
NVIDIA-NeMo:main
Choose a base branch
from
sjayaram-nv:sjayaram/k8s-excalibur
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
K8s/XCalibur Support #579
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
51b1502
K8s/XCalibur Support
sjayaram-nv 03043fd
Support for KAI scheduler in XCalibur executor
sjayaram-nv 7d43922
Fix detached runs for XCalibur jobs
sjayaram-nv d22d483
Merge branch 'NVIDIA-NeMo:main' into sjayaram/k8s-excalibur
sjayaram-nv 38010c7
Merge branch 'NVIDIA-NeMo:main' into sjayaram/k8s-excalibur
sjayaram-nv 03831d3
Added tests to improve test coverage
sjayaram-nv 6725d3d
Add Renovate configuration file (#593)
balasaajay a8c4314
chore: add build constraint for setuptools version in pyproject.toml …
balasaajay fe5eab5
Rename XCalibur to Nvcre
sjayaram-nv c3fe84c
Merge branch 'main' into sjayaram/k8s-excalibur
sjayaram-nv 75387b8
Addressed review comments and cleaned up redundant code
sjayaram-nv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,298 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import json | ||
| import logging | ||
| import os | ||
| import shutil | ||
| import tempfile | ||
| from dataclasses import dataclass | ||
| from datetime import datetime | ||
| from pathlib import Path | ||
| from typing import Any, Iterable, Optional | ||
|
|
||
| import fiddle as fdl | ||
| import fiddle._src.experimental.dataclasses as fdl_dc | ||
| import yaml | ||
| from torchx.schedulers.api import ( | ||
| AppDryRunInfo, | ||
| DescribeAppResponse, | ||
| ListAppResponse, | ||
| Scheduler, | ||
| Stream, | ||
| ) | ||
| from torchx.specs import AppDef, AppState, ReplicaStatus, Role, RoleStatus, runopts | ||
|
|
||
| from nemo_run.config import get_nemorun_home | ||
| from nemo_run.core.execution.base import Executor | ||
| from nemo_run.core.execution.nvcre import NvcreExecutor, NvcrePhase | ||
| from nemo_run.core.serialization.zlib_json import ZlibJSONSerializer | ||
| from nemo_run.run.torchx_backend.schedulers.api import SchedulerMixin | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| NVCRE_JOB_DIRS = os.path.join(get_nemorun_home(), ".nvcre_jobs.json") | ||
|
|
||
| NVCRE_STATES: dict[NvcrePhase, AppState] = { | ||
| NvcrePhase.PENDING: AppState.PENDING, | ||
| NvcrePhase.IN_PROGRESS: AppState.RUNNING, | ||
| NvcrePhase.SUCCEEDED: AppState.SUCCEEDED, | ||
| NvcrePhase.FAILED: AppState.FAILED, | ||
| NvcrePhase.UNKNOWN: AppState.UNKNOWN, | ||
| } | ||
|
|
||
|
|
||
| @dataclass | ||
| class NvcreRequest: | ||
| """Wraps the AppDef and NvcreExecutor for dryrun/schedule.""" | ||
|
|
||
| app: AppDef | ||
| executor: NvcreExecutor | ||
| cmd: list[str] | ||
| name: str | ||
|
|
||
|
|
||
| class NvcreScheduler(SchedulerMixin, Scheduler[dict]): # type: ignore | ||
| def __init__(self, session_name: str) -> None: | ||
| super().__init__("nvcre", session_name) | ||
|
|
||
| def _run_opts(self) -> runopts: | ||
| opts = runopts() | ||
| opts.add("job_dir", type_=str, help="Directory for job outputs.") | ||
| return opts | ||
|
|
||
| def _submit_dryrun(self, app: AppDef, cfg: Executor) -> AppDryRunInfo[NvcreRequest]: | ||
| assert isinstance(cfg, NvcreExecutor), ( | ||
| f"{cfg.__class__} is not supported by NvcreScheduler." | ||
| ) | ||
| executor = cfg | ||
| assert len(app.roles) == 1, "NvcreScheduler only supports single-role apps." | ||
|
|
||
| role = app.roles[0] | ||
| values = executor.macro_values() | ||
| if values: | ||
| role = values.apply(role) | ||
|
|
||
| # Merge role-level env into executor env | ||
| executor.env_vars.update(role.env) | ||
|
|
||
| cmd = [role.entrypoint] + role.args | ||
|
|
||
| # Wrap with torchrun so that torch.distributed is initialised correctly | ||
| # across all nodes. Nvcre injects PET_* rendezvous env vars per-pod | ||
| # (via the JobSet downward-API); torchrun reads them via --nnodes / | ||
| # --nproc_per_node / --node_rank / --master_addr / --master_port and | ||
| # sets the standard RANK, WORLD_SIZE, LOCAL_RANK, MASTER_ADDR vars that | ||
| # Megatron-Bridge's common_utils.py expects. Without this wrapper each | ||
| # replica starts as a lone python process (WORLD_SIZE=1) and fails the | ||
| # parallelism divisibility check. | ||
| if executor.use_torchrun and cmd and cmd[0] == "python": | ||
| script_and_args = cmd[1:] # drop the "python" token; torchrun runs the script directly | ||
| cmd = [ | ||
| "torchrun", | ||
| "--nnodes=$(PET_NNODES)", | ||
| "--nproc_per_node=$(PET_NPROC_PER_NODE)", | ||
| "--node_rank=$(PET_NODE_RANK)", | ||
| "--master_addr=$(PET_MASTER_ADDR)", | ||
| "--master_port=$(PET_MASTER_PORT)", | ||
| ] + script_and_args | ||
|
|
||
| req = NvcreRequest(app=app, executor=executor, cmd=cmd, name=role.name) | ||
|
|
||
| def _wl_cmd(r: NvcreRequest) -> list[str]: | ||
| if r.executor.workdir_pvc: | ||
| return ["/bin/bash", f"{r.executor.code_dir}/launch.sh"] | ||
| return r.cmd | ||
|
|
||
| return AppDryRunInfo( | ||
| req, | ||
| lambda r: yaml.dump(r.executor.build_workloadrun_yaml(_wl_cmd(r))), | ||
| ) | ||
|
|
||
| def schedule(self, dryrun_info: AppDryRunInfo[NvcreRequest]) -> str: | ||
| req = dryrun_info.request | ||
| executor = req.executor | ||
|
|
||
| os.makedirs(executor.job_dir, exist_ok=True) | ||
|
|
||
| if executor.workdir_pvc: | ||
| # Write launch.sh with the actual training command and sync to PVC. | ||
| executor.materialize_launch_script(req.cmd, max_retries=executor.retries) | ||
| executor.package(executor.packager, job_name=executor.job_name) | ||
| wl_cmd = ["/bin/bash", f"{executor.code_dir}/launch.sh"] | ||
| else: | ||
| # No PVC: code is assumed to be in the container image. | ||
| # Run the training command directly; env vars are injected via the | ||
| # WorkloadRun spec rather than through a launch.sh wrapper. | ||
| nsys_prefix = executor.get_launcher_prefix() | ||
| wl_cmd = (["nsys"] + nsys_prefix + req.cmd) if nsys_prefix else req.cmd | ||
|
|
||
| # Write WorkloadRun YAML | ||
| yaml_path = os.path.join(executor.job_dir, "workloadrun.yaml") | ||
| manifest = executor.build_workloadrun_yaml(wl_cmd) | ||
| with open(yaml_path, "w") as f: | ||
| yaml.dump(manifest, f, default_flow_style=False) | ||
|
|
||
| # Submit | ||
| workloadrun_name = executor.submit(yaml_path) | ||
|
|
||
| experiment_id = getattr(executor, "experiment_id", "nvcre_experiment") | ||
| app_id = f"{experiment_id}___{req.name}___{workloadrun_name}" | ||
|
|
||
| _save_job(app_id, workloadrun_name, executor) | ||
| return app_id | ||
|
|
||
| def describe(self, app_id: str) -> Optional[DescribeAppResponse]: | ||
| stored = _get_jobs() | ||
| job_info = stored.get(app_id) | ||
| if not job_info: | ||
| return None | ||
|
|
||
| parts = app_id.split("___") | ||
| role_name = parts[1] if len(parts) > 1 else app_id | ||
| workloadrun_name = job_info.get("workloadrun_name") or ( | ||
| parts[-1] if len(parts) > 2 else app_id | ||
| ) | ||
|
|
||
| executor: Optional[NvcreExecutor] = job_info.get("executor") | ||
| if not executor: | ||
| return None | ||
|
|
||
| phase = executor.status(workloadrun_name) | ||
| app_state = NVCRE_STATES.get(phase, AppState.UNKNOWN) | ||
|
|
||
| roles = [Role(name=role_name, image="", num_replicas=executor.num_nodes)] | ||
| roles_statuses = [ | ||
| RoleStatus( | ||
| role_name, | ||
| replicas=[ | ||
| ReplicaStatus(id=i, role=role_name, state=app_state, hostname="") | ||
| for i in range(executor.num_nodes) | ||
| ], | ||
| ) | ||
| ] | ||
|
|
||
| return DescribeAppResponse( | ||
| app_id=app_id, | ||
| roles=roles, | ||
| roles_statuses=roles_statuses, | ||
| state=app_state, | ||
| msg="", | ||
| ) | ||
|
|
||
| def log_iter( | ||
|
sjayaram-nv marked this conversation as resolved.
|
||
| self, | ||
| app_id: str, | ||
| role_name: str, | ||
| k: int = 0, | ||
| regex: Optional[str] = None, | ||
| since: Optional[datetime] = None, | ||
| until: Optional[datetime] = None, | ||
| should_tail: bool = False, | ||
| streams: Optional[Stream] = None, | ||
| ) -> Iterable[str]: | ||
| stored = _get_jobs() | ||
| job_info = stored.get(app_id) | ||
| if not job_info: | ||
| return [] | ||
|
|
||
| parts = app_id.split("___") | ||
| workloadrun_name = job_info.get("workloadrun_name") or ( | ||
| parts[-1] if len(parts) > 2 else app_id | ||
| ) | ||
| executor: Optional[NvcreExecutor] = job_info.get("executor") | ||
| if not executor: | ||
| return [] | ||
|
|
||
| # job_dir is an init=False field that doesn't survive fiddle serialisation; | ||
| # restore it from the explicitly saved value so fetch_logs can write the | ||
| # streaming log to the correct experiment directory. | ||
| job_dir = job_info.get("job_dir", "") | ||
| if job_dir and not executor.job_dir: | ||
| executor.job_dir = job_dir | ||
|
|
||
| return executor.fetch_logs(workloadrun_name, stream=should_tail) | ||
|
|
||
| def _cancel_existing(self, app_id: str) -> None: | ||
| stored = _get_jobs() | ||
| job_info = stored.get(app_id) | ||
| if not job_info: | ||
| return | ||
|
|
||
| parts = app_id.split("___") | ||
| workloadrun_name = job_info.get("workloadrun_name") or ( | ||
| parts[-1] if len(parts) > 2 else app_id | ||
| ) | ||
| executor: Optional[NvcreExecutor] = job_info.get("executor") | ||
| if executor: | ||
| executor.cancel(workloadrun_name) | ||
|
|
||
| def list(self) -> list[ListAppResponse]: | ||
| return [] | ||
|
|
||
| def _validate(self, app: AppDef, scheduler: str) -> None: | ||
| pass | ||
|
|
||
|
|
||
| def create_scheduler(session_name: str, **kwargs: Any) -> NvcreScheduler: | ||
| return NvcreScheduler(session_name=session_name) | ||
|
|
||
|
|
||
| def _save_job(app_id: str, workloadrun_name: str, executor: NvcreExecutor) -> None: | ||
| original_apps: dict = {} | ||
| os.makedirs(os.path.dirname(NVCRE_JOB_DIRS), exist_ok=True) | ||
| if not os.path.isfile(NVCRE_JOB_DIRS): | ||
| Path(NVCRE_JOB_DIRS).touch() | ||
|
|
||
| serializer = ZlibJSONSerializer() | ||
| with open(NVCRE_JOB_DIRS, "r+") as f: | ||
| try: | ||
| original_apps = json.load(f) | ||
| except Exception: | ||
| original_apps = {} | ||
|
|
||
| entry = { | ||
| "workloadrun_name": workloadrun_name, | ||
| "job_dir": executor.job_dir, | ||
| "executor": serializer.serialize( | ||
| fdl_dc.convert_dataclasses_to_configs(executor, allow_post_init=True) | ||
| ), | ||
| } | ||
| original_apps[app_id] = entry | ||
|
|
||
| with tempfile.NamedTemporaryFile(mode="w+", delete=False) as fp: | ||
| json.dump(original_apps, fp) | ||
| temp_path = fp.name | ||
|
|
||
| f.close() | ||
| shutil.move(temp_path, NVCRE_JOB_DIRS) | ||
|
|
||
|
|
||
| def _get_jobs() -> dict[str, dict]: | ||
| if not os.path.isfile(NVCRE_JOB_DIRS): | ||
| return {} | ||
| with open(NVCRE_JOB_DIRS) as f: | ||
| try: | ||
| data = json.load(f) | ||
| except Exception: | ||
| return {} | ||
|
|
||
| serializer = ZlibJSONSerializer() | ||
| for entry in data.values(): | ||
| try: | ||
| entry["executor"] = fdl.build(serializer.deserialize(entry["executor"])) | ||
| except Exception as e: | ||
| logger.debug("Failed to deserialize Nvcre executor: %s", e) | ||
| return data | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.