Skip to content

Commit 00092ce

Browse files
committed
[HWORKS-1037] Save model files in /Models/name/version/Files dir
1 parent 4b577e8 commit 00092ce

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

python/hopsworks_common/constants.py

+2
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ class MODEL:
163163

164164
class MODEL_REGISTRY:
165165
HOPSFS_MOUNT_PREFIX = "/hopsfs/"
166+
ARTIFACTS_DIR_NAME = "Artifacts"
167+
MODEL_FILES_DIR_NAME = "Files"
166168

167169

168170
class MODEL_SERVING:

python/hsml/engine/model_engine.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ def _upload_additional_resources(self, model_instance):
8080
return model_instance
8181

8282
def _copy_or_move_hopsfs_model_item(
83-
self, item_attr, to_model_version_path, keep_original_files
83+
self, item_attr, to_model_files_path, keep_original_files
8484
):
8585
"""Copy or move model item from a hdfs path to the model version folder in the Models dataset. It works with files and folders."""
8686
path = item_attr["path"]
87-
to_hdfs_path = os.path.join(to_model_version_path, os.path.basename(path))
87+
to_hdfs_path = os.path.join(to_model_files_path, os.path.basename(path))
8888
if keep_original_files:
8989
self._engine.copy(path, to_hdfs_path)
9090
else:
@@ -93,7 +93,7 @@ def _copy_or_move_hopsfs_model_item(
9393
def _copy_or_move_hopsfs_model(
9494
self,
9595
from_hdfs_model_path,
96-
to_model_version_path,
96+
to_model_files_path,
9797
keep_original_files,
9898
update_upload_progress,
9999
):
@@ -122,7 +122,7 @@ def _copy_or_move_hopsfs_model(
122122
)["items"]:
123123
path_attr = entry["attributes"]
124124
self._copy_or_move_hopsfs_model_item(
125-
path_attr, to_model_version_path, keep_original_files
125+
path_attr, to_model_files_path, keep_original_files
126126
)
127127
if path_attr.get("dir", False):
128128
n_dirs += 1
@@ -132,7 +132,7 @@ def _copy_or_move_hopsfs_model(
132132
else:
133133
# if path is a file, copy/move it
134134
self._copy_or_move_hopsfs_model_item(
135-
model_path_attr, to_model_version_path, keep_original_files
135+
model_path_attr, to_model_files_path, keep_original_files
136136
)
137137
n_files += 1
138138
update_upload_progress(n_dirs=n_dirs, n_files=n_files)
@@ -156,7 +156,7 @@ def _download_model_from_hopsfs_recursive(
156156

157157
if path_attr.get("dir", False):
158158
# otherwise, make a recursive call for the folder
159-
if basename == "Artifacts":
159+
if basename == constants.MODEL_REGISTRY.ARTIFACTS_DIR_NAME:
160160
continue # skip Artifacts subfolder
161161
local_folder_path = os.path.join(to_local_path, basename)
162162
os.mkdir(local_folder_path)
@@ -195,11 +195,11 @@ def _download_model_from_hopsfs(
195195
def _upload_local_model(
196196
self,
197197
from_local_model_path,
198-
to_model_version_path,
198+
to_model_files_path,
199199
update_upload_progress,
200200
upload_configuration=None,
201201
):
202-
"""Copy or upload model files from a local path to the model version folder in the Models dataset."""
202+
"""Copy or upload model files from a local path to the model files folder in the Models dataset."""
203203
n_dirs, n_files = 0, 0
204204
if os.path.isdir(from_local_model_path):
205205
# if path is a dir, upload files and folders iteratively
@@ -210,7 +210,7 @@ def _upload_local_model(
210210
# - files is the list of file names present in the root dir
211211
# we need to replace the local path prefix with the hdfs path prefix (i.e., /srv/hops/....../root with /Projects/.../)
212212
remote_base_path = root.replace(
213-
from_local_model_path, to_model_version_path
213+
from_local_model_path, to_model_files_path
214214
)
215215
for d_name in dirs:
216216
self._engine.mkdir(remote_base_path + "/" + d_name)
@@ -228,7 +228,7 @@ def _upload_local_model(
228228
# if path is a file, upload file
229229
self._engine.upload(
230230
from_local_model_path,
231-
to_model_version_path,
231+
to_model_files_path,
232232
upload_configuration=upload_configuration,
233233
)
234234
n_files += 1
@@ -249,14 +249,14 @@ def _save_model_from_local_or_hopsfs_mount(
249249
from_hdfs_model_path=model_path.replace(
250250
constants.MODEL_REGISTRY.HOPSFS_MOUNT_PREFIX, ""
251251
),
252-
to_model_version_path=model_instance.version_path,
252+
to_model_files_path=model_instance.files_path,
253253
keep_original_files=keep_original_files,
254254
update_upload_progress=update_upload_progress,
255255
)
256256
else:
257257
self._upload_local_model(
258258
from_local_model_path=model_path,
259-
to_model_version_path=model_instance.version_path,
259+
to_model_files_path=model_instance.files_path,
260260
update_upload_progress=update_upload_progress,
261261
upload_configuration=upload_configuration,
262262
)
@@ -365,6 +365,7 @@ def save(
365365
if step["id"] == 0:
366366
# Create folders
367367
self._engine.mkdir(model_instance.version_path)
368+
self._engine.mkdir(model_instance.files_path)
368369
if step["id"] == 1:
369370

370371
def update_upload_progress(n_dirs=0, n_files=0, step=step):
@@ -374,7 +375,7 @@ def update_upload_progress(n_dirs=0, n_files=0, step=step):
374375

375376
update_upload_progress(n_dirs=0, n_files=0)
376377

377-
# Upload Model files from local path to /Models/{model_instance._name}/{model_instance._version}
378+
# Upload Model files from local path to /Models/{model_instance._name}/{model_instance._version}/Files
378379
# check local absolute
379380
if os.path.isabs(model_path) and os.path.exists(model_path):
380381
self._save_model_from_local_or_hopsfs_mount(
@@ -401,7 +402,7 @@ def update_upload_progress(n_dirs=0, n_files=0, step=step):
401402
): # check hdfs relative and absolute
402403
self._copy_or_move_hopsfs_model(
403404
from_hdfs_model_path=model_path,
404-
to_model_version_path=model_instance.version_path,
405+
to_model_files_path=model_instance.files_path,
405406
keep_original_files=keep_original_files,
406407
update_upload_progress=update_upload_progress,
407408
)
@@ -446,7 +447,7 @@ def update_download_progress(n_dirs, n_files, done=False):
446447
)
447448

448449
try:
449-
from_hdfs_model_path = model_instance.version_path
450+
from_hdfs_model_path = model_instance.files_path
450451
if from_hdfs_model_path.startswith("hdfs:/"):
451452
projects_index = from_hdfs_model_path.find("/Projects", 0)
452453
from_hdfs_model_path = from_hdfs_model_path[projects_index:]

python/hsml/engine/serving_engine.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import uuid
2020
from typing import Dict, List, Union
2121

22-
from hsml import util
22+
from hsml import util, constants
2323
from hsml.client.exceptions import ModelServingException, RestAPIError
2424
from hsml.client.istio.utils.infer_type import InferInput
2525
from hsml.constants import (
@@ -322,7 +322,7 @@ def download_artifact(self, deployment_instance):
322322
str(uuid.uuid4()),
323323
deployment_instance.model_name,
324324
str(deployment_instance.model_version),
325-
"Artifacts",
325+
constants.MODEL_REGISTRY.ARTIFACTS_DIR_NAME,
326326
)
327327
to_artifact_version_path = (
328328
to_artifacts_path + "/" + str(deployment_instance.artifact_version)

python/hsml/model.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import humps
2424
from hopsworks_common import usage
25-
from hsml import client, util
25+
from hsml import client, util, constants
2626
from hsml.constants import ARTIFACT_VERSION
2727
from hsml.constants import INFERENCE_ENDPOINTS as IE
2828
from hsml.core import explicit_provenance
@@ -544,6 +544,11 @@ def model_path(self):
544544
def version_path(self):
545545
"""path of the model including version folder. Resolves to /Projects/{project_name}/Models/{name}/{version}"""
546546
return "{}/{}".format(self.model_path, str(self.version))
547+
548+
@property
549+
def files_path(self):
550+
"""path of the model files including version and files folder. Resolves to /Projects/{project_name}/Models/{name}/{version}/Files"""
551+
return "{}/{}/{}".format(self.model_path, str(self.version), constants.MODEL_REGISTRY.MODEL_FILES_DIR_NAME)
547552

548553
@property
549554
def shared_registry_project_name(self):

0 commit comments

Comments
 (0)