Skip to content

Commit 8a0ebae

Browse files
authored
Unify usage of /tmp folder and .tmp extension for writing temp files (aws#163)
* unify-tmp-path * ignore tmp files * fix incorrect path bug * renamed SAGEMAKER_TEMP_PATH_SUFFIX to SMDEBUG_TEMP_PATH_SUFFIX
1 parent 4cc7ab9 commit 8a0ebae

File tree

4 files changed

+9
-24
lines changed

4 files changed

+9
-24
lines changed

smdebug/core/access_layer/file.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
# First Party
66
from smdebug.core.logger import get_logger
7-
from smdebug.core.sagemaker_utils import is_sagemaker_job
87

98
# Local
109
from .base import TSAccessBase
1110

12-
NON_SAGEMAKER_TEMP_PATH_PREFIX = "/tmp"
13-
SAGEMAKER_TEMP_PATH_SUFFIX = ".tmp"
11+
SMDEBUG_TEMP_PATH_SUFFIX = ".tmp"
1412

1513

1614
def ensure_dir(file_path, is_file=True):
@@ -23,13 +21,7 @@ def ensure_dir(file_path, is_file=True):
2321

2422

2523
def get_temp_path(file_path):
26-
directory = os.path.dirname(file_path)
27-
if is_sagemaker_job():
28-
temp_path = file_path + SAGEMAKER_TEMP_PATH_SUFFIX
29-
else:
30-
if len(file_path) > 0 and file_path[0] == "/":
31-
file_path = file_path[1:]
32-
temp_path = os.path.join(NON_SAGEMAKER_TEMP_PATH_PREFIX, file_path)
24+
temp_path = file_path + SMDEBUG_TEMP_PATH_SUFFIX
3325
return temp_path
3426

3527

smdebug/core/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,10 @@ def list_files_in_directory(directory, file_regex=None):
8484

8585

8686
def list_collection_files_in_directory(directory):
87-
collections_directory = get_path_to_collections(directory)
8887
import re
8988

90-
collections_file_regex = re.compile(".*_?collections.json")
91-
return list_files_in_directory(collections_directory, file_regex=collections_file_regex)
89+
collections_file_regex = re.compile(".*_?collections.json$")
90+
return list_files_in_directory(directory, file_regex=collections_file_regex)
9291

9392

9493
def serialize_tf_device(device: str) -> str:

smdebug/trials/local_trial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# First Party
55
from smdebug.core.collection_manager import CollectionManager
66
from smdebug.core.index_reader import LocalIndexReader
7-
from smdebug.core.utils import get_path_to_collections, list_files_in_directory
7+
from smdebug.core.utils import get_path_to_collections, list_collection_files_in_directory
88

99
# Local
1010
from .trial import Trial
@@ -37,7 +37,7 @@ def __init__(
3737
self._load_tensors()
3838

3939
def _get_collection_files(self) -> list:
40-
return list_files_in_directory(get_path_to_collections(self.path))
40+
return list_collection_files_in_directory(get_path_to_collections(self.path))
4141

4242
def _load_tensors_from_index_tensors(self, index_tensors_dict):
4343
for tname in index_tensors_dict:

tests/core/test_paths.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55

66
# First Party
77
import smdebug.pytorch as smd
8-
from smdebug.core.access_layer.file import (
9-
NON_SAGEMAKER_TEMP_PATH_PREFIX,
10-
SAGEMAKER_TEMP_PATH_SUFFIX,
11-
get_temp_path,
12-
)
8+
from smdebug.core.access_layer.file import SMDEBUG_TEMP_PATH_SUFFIX, get_temp_path
139
from smdebug.core.access_layer.utils import training_has_ended
1410
from smdebug.core.hook_utils import verify_and_get_out_dir
1511
from smdebug.core.utils import SagemakerSimulator, ScriptSimulator
@@ -93,14 +89,12 @@ def test_temp_paths():
9389
"/opt/ml/output/tensors/events/a/b",
9490
]:
9591
temp_path = get_temp_path(path)
96-
assert temp_path.endswith(SAGEMAKER_TEMP_PATH_SUFFIX)
97-
assert not temp_path.startswith(NON_SAGEMAKER_TEMP_PATH_PREFIX)
92+
assert temp_path.endswith(SMDEBUG_TEMP_PATH_SUFFIX)
9893

9994
with ScriptSimulator() as sim:
10095
for path in ["/a/b/c", "/opt/ml/output/a", "a/b/c"]:
10196
temp_path = get_temp_path(path)
102-
assert not SAGEMAKER_TEMP_PATH_SUFFIX in temp_path
103-
assert temp_path.startswith(NON_SAGEMAKER_TEMP_PATH_PREFIX)
97+
assert temp_path.endswith(SMDEBUG_TEMP_PATH_SUFFIX)
10498

10599

106100
def test_s3_path_that_exists_without_end_of_job():

0 commit comments

Comments
 (0)