|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Export functions for NeMo Run.""" |
| 17 | + |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +from nemo.collections.llm.api import export_ckpt |
| 21 | +from nemo.utils import logging |
| 22 | + |
| 23 | + |
| 24 | +def export_most_recent_ckpt(directory: str, output_path: str): |
| 25 | + """Export most recent checkpoint from a NeMo Run experiment directory.""" |
| 26 | + most_recent_ckpt = _get_most_recent_ckpt(directory) |
| 27 | + modelopt_kwargs = {"export_extra_modules": True} |
| 28 | + logging.info(f"Exporting most recent NeMo Run checkpoint: {most_recent_ckpt}") |
| 29 | + export_ckpt( |
| 30 | + most_recent_ckpt, |
| 31 | + "hf", |
| 32 | + output_path=output_path, |
| 33 | + overwrite=True, |
| 34 | + modelopt_export_kwargs=modelopt_kwargs, |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +def _get_most_recent_subdir(directory: Path): |
| 39 | + # Get all subdirectories |
| 40 | + subdirs = [d for d in directory.iterdir() if d.is_dir()] |
| 41 | + if not subdirs: |
| 42 | + raise ValueError(f"No subdirectories found in {directory}") |
| 43 | + |
| 44 | + # Sort by modification time (most recent first) |
| 45 | + most_recent = max(subdirs, key=lambda x: x.stat().st_mtime) |
| 46 | + |
| 47 | + return most_recent |
| 48 | + |
| 49 | + |
| 50 | +def _get_most_recent_ckpt(directory: str): |
| 51 | + """Find the most recent checkpoint subdirectory in a given NeMo Run experiment directory. |
| 52 | +
|
| 53 | + Args: |
| 54 | + directory (str): Path to the directory to search in. |
| 55 | +
|
| 56 | + Returns: |
| 57 | + str: Path to the most recent subdirectory. |
| 58 | + """ |
| 59 | + exp_dir = Path(directory) / "default" |
| 60 | + assert exp_dir.exists(), f"Experiment directory {exp_dir} does not exist" |
| 61 | + |
| 62 | + checkpoint_dir = exp_dir / "checkpoints" |
| 63 | + if checkpoint_dir.exists(): |
| 64 | + most_recent = _get_most_recent_subdir(checkpoint_dir) |
| 65 | + else: |
| 66 | + most_recent = _get_most_recent_subdir(exp_dir) |
| 67 | + checkpoint_dir = most_recent / "checkpoints" |
| 68 | + assert checkpoint_dir.exists(), f"Checkpoint directory {checkpoint_dir} does not exist" |
| 69 | + most_recent = _get_most_recent_subdir(checkpoint_dir) |
| 70 | + |
| 71 | + return str(most_recent) |
0 commit comments