|
2 | 2 | # Licensed under the Apache License, Version 2.0 (the "License"); |
3 | 3 | # http://www.apache.org/licenses/LICENSE-2.0 |
4 | 4 | # |
5 | | - |
6 | | -from typing import Optional, Tuple |
| 5 | +import os |
| 6 | +import tempfile |
| 7 | +from pathlib import Path |
| 8 | +from typing import TYPE_CHECKING, Optional, Tuple, Union |
7 | 9 |
|
8 | 10 | from lightning_sdk.api.teamspace_api import UploadedModelInfo |
9 | 11 | from lightning_sdk.teamspace import Teamspace |
10 | 12 | from lightning_sdk.utils import resolve as sdk_resolvers |
| 13 | +from lightning_utilities import module_available |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from torch.nn import Module |
| 17 | + |
| 18 | +if module_available("torch"): |
| 19 | + import torch |
| 20 | + from torch.nn import Module |
| 21 | +else: |
| 22 | + torch = None |
| 23 | + |
| 24 | +# if module_available("lightning"): |
| 25 | +# from lightning import LightningModule |
| 26 | +# elif module_available("pytorch_lightning"): |
| 27 | +# from pytorch_lightning import LightningModule |
| 28 | +# else: |
| 29 | +# LightningModule = None |
11 | 30 |
|
12 | 31 |
|
13 | 32 | def _parse_name(name: str) -> Tuple[str, str, str]: |
@@ -45,6 +64,48 @@ def _get_teamspace(name: str, organization: str) -> Teamspace: |
45 | 64 |
|
46 | 65 |
|
47 | 66 | def upload_model( |
| 67 | + model: Union[str, Path, "Module"], |
| 68 | + name: str, |
| 69 | + progress_bar: bool = True, |
| 70 | + cluster_id: Optional[str] = None, |
| 71 | + staging_dir: Optional[str] = None, |
| 72 | +) -> UploadedModelInfo: |
| 73 | + """Upload a checkpoint to the model store. |
| 74 | +
|
| 75 | + Args: |
| 76 | + model: The model to upload. Can be a path to a checkpoint file, a PyTorch model, or a Lightning model. |
| 77 | + name: Name tag of the model to upload. Must be in the format 'organization/teamspace/modelname' |
| 78 | + where entity is either your username or the name of an organization you are part of. |
| 79 | + progress_bar: Whether to show a progress bar for the upload. |
| 80 | + cluster_id: The name of the cluster to use. Only required if it can't be determined |
| 81 | + automatically. |
| 82 | + staging_dir: A directory where the model can be saved temporarily. If not provided, a temporary directory will |
| 83 | + be created and used. |
| 84 | +
|
| 85 | + """ |
| 86 | + if not staging_dir: |
| 87 | + staging_dir = tempfile.mkdtemp() |
| 88 | + # if LightningModule and isinstance(model, LightningModule): |
| 89 | + # path = os.path.join(staging_dir, f"{model.__class__.__name__}.ckpt") |
| 90 | + # model.save_checkpoint(path) |
| 91 | + if torch and isinstance(model, Module): |
| 92 | + path = os.path.join(staging_dir, f"{model.__class__.__name__}.pth") |
| 93 | + torch.save(model.state_dict(), path) |
| 94 | + elif isinstance(model, str): |
| 95 | + path = model |
| 96 | + elif isinstance(model, Path): |
| 97 | + path = str(model) |
| 98 | + else: |
| 99 | + raise ValueError(f"Unsupported model type {type(model)}") |
| 100 | + return upload_model_files( |
| 101 | + path=path, |
| 102 | + name=name, |
| 103 | + progress_bar=progress_bar, |
| 104 | + cluster_id=cluster_id, |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +def upload_model_files( |
48 | 109 | path: str, |
49 | 110 | name: str, |
50 | 111 | progress_bar: bool = True, |
|
0 commit comments