|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +# language governing permissions and limitations under the License. |
| 14 | +from __future__ import absolute_import |
| 15 | +from sagemaker.workflow import _repack_model |
| 16 | + |
| 17 | +from pathlib import Path |
| 18 | +import shutil |
| 19 | +import tarfile |
| 20 | +import os |
| 21 | +import pytest |
| 22 | +import time |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.skip( |
| 26 | + reason="""This test operates on the root file system |
| 27 | + and will likely fail due to permission errors. |
| 28 | + Temporarily remove this skip decorator and run |
| 29 | + the test after making changes to _repack_model.py""" |
| 30 | +) |
| 31 | +def test_repack_entry_point_only(tmp): |
| 32 | + model_name = "xg-boost-model" |
| 33 | + fake_model_path = os.path.join(tmp, model_name) |
| 34 | + |
| 35 | + # create a fake model |
| 36 | + open(fake_model_path, "w") |
| 37 | + |
| 38 | + # create model.tar.gz |
| 39 | + model_tar_name = "model-%s.tar.gz" % time.time() |
| 40 | + model_tar_location = os.path.join(tmp, model_tar_name) |
| 41 | + with tarfile.open(model_tar_location, mode="w:gz") as t: |
| 42 | + t.add(fake_model_path, arcname=model_name) |
| 43 | + |
| 44 | + # move model.tar.gz to /opt/ml/input/data/training |
| 45 | + Path("/opt/ml/input/data/training").mkdir(parents=True, exist_ok=True) |
| 46 | + shutil.move(model_tar_location, os.path.join("/opt/ml/input/data/training", model_tar_name)) |
| 47 | + |
| 48 | + # create files that will be added to model.tar.gz |
| 49 | + create_file_tree( |
| 50 | + "/opt/ml/code", |
| 51 | + [ |
| 52 | + "inference.py", |
| 53 | + ], |
| 54 | + ) |
| 55 | + |
| 56 | + # repack |
| 57 | + _repack_model.repack(inference_script="inference.py", model_archive=model_tar_name) |
| 58 | + |
| 59 | + # /opt/ml/model should now have the original model and the inference script |
| 60 | + assert os.path.exists(os.path.join("/opt/ml/model", model_name)) |
| 61 | + assert os.path.exists(os.path.join("/opt/ml/model/code", "inference.py")) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.skip( |
| 65 | + reason="""This test operates on the root file system |
| 66 | + and will likely fail due to permission errors. |
| 67 | + Temporarily remove this skip decorator and run |
| 68 | + the test after making changes to _repack_model.py""" |
| 69 | +) |
| 70 | +def test_repack_with_dependencies(tmp): |
| 71 | + model_name = "xg-boost-model" |
| 72 | + fake_model_path = os.path.join(tmp, model_name) |
| 73 | + |
| 74 | + # create a fake model |
| 75 | + open(fake_model_path, "w") |
| 76 | + |
| 77 | + # create model.tar.gz |
| 78 | + model_tar_name = "model-%s.tar.gz" % time.time() |
| 79 | + model_tar_location = os.path.join(tmp, model_tar_name) |
| 80 | + with tarfile.open(model_tar_location, mode="w:gz") as t: |
| 81 | + t.add(fake_model_path, arcname=model_name) |
| 82 | + |
| 83 | + # move model.tar.gz to /opt/ml/input/data/training |
| 84 | + Path("/opt/ml/input/data/training").mkdir(parents=True, exist_ok=True) |
| 85 | + shutil.move(model_tar_location, os.path.join("/opt/ml/input/data/training", model_tar_name)) |
| 86 | + |
| 87 | + # create files that will be added to model.tar.gz |
| 88 | + create_file_tree( |
| 89 | + "/opt/ml/code", |
| 90 | + ["inference.py", "dependencies/a", "bb", "dependencies/some/dir/b"], |
| 91 | + ) |
| 92 | + |
| 93 | + # repack |
| 94 | + _repack_model.repack( |
| 95 | + inference_script="inference.py", |
| 96 | + model_archive=model_tar_name, |
| 97 | + dependencies=["dependencies/a", "bb", "dependencies/some/dir"], |
| 98 | + ) |
| 99 | + |
| 100 | + # /opt/ml/model should now have the original model and the inference script |
| 101 | + assert os.path.exists(os.path.join("/opt/ml/model", model_name)) |
| 102 | + assert os.path.exists(os.path.join("/opt/ml/model/code", "inference.py")) |
| 103 | + assert os.path.exists(os.path.join("/opt/ml/model/code/lib", "a")) |
| 104 | + assert os.path.exists(os.path.join("/opt/ml/model/code/lib", "bb")) |
| 105 | + assert os.path.exists(os.path.join("/opt/ml/model/code/lib/dir", "b")) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.skip( |
| 109 | + reason="""This test operates on the root file system |
| 110 | + and will likely fail due to permission errors. |
| 111 | + Temporarily remove this skip decorator and run |
| 112 | + the test after making changes to _repack_model.py""" |
| 113 | +) |
| 114 | +def test_repack_with_source_dir_and_dependencies(tmp): |
| 115 | + model_name = "xg-boost-model" |
| 116 | + fake_model_path = os.path.join(tmp, model_name) |
| 117 | + |
| 118 | + # create a fake model |
| 119 | + open(fake_model_path, "w") |
| 120 | + |
| 121 | + # create model.tar.gz |
| 122 | + model_tar_name = "model-%s.tar.gz" % time.time() |
| 123 | + model_tar_location = os.path.join(tmp, model_tar_name) |
| 124 | + with tarfile.open(model_tar_location, mode="w:gz") as t: |
| 125 | + t.add(fake_model_path, arcname=model_name) |
| 126 | + |
| 127 | + # move model.tar.gz to /opt/ml/input/data/training |
| 128 | + Path("/opt/ml/input/data/training").mkdir(parents=True, exist_ok=True) |
| 129 | + shutil.move(model_tar_location, os.path.join("/opt/ml/input/data/training", model_tar_name)) |
| 130 | + |
| 131 | + # create files that will be added to model.tar.gz |
| 132 | + create_file_tree( |
| 133 | + "/opt/ml/code", |
| 134 | + [ |
| 135 | + "inference.py", |
| 136 | + "dependencies/a", |
| 137 | + "bb", |
| 138 | + "dependencies/some/dir/b", |
| 139 | + "sourcedir/foo.py", |
| 140 | + "sourcedir/some/dir/a", |
| 141 | + ], |
| 142 | + ) |
| 143 | + |
| 144 | + # repack |
| 145 | + _repack_model.repack( |
| 146 | + inference_script="inference.py", |
| 147 | + model_archive=model_tar_name, |
| 148 | + dependencies=["dependencies/a", "bb", "dependencies/some/dir"], |
| 149 | + source_dir="sourcedir", |
| 150 | + ) |
| 151 | + |
| 152 | + # /opt/ml/model should now have the original model and the inference script |
| 153 | + assert os.path.exists(os.path.join("/opt/ml/model", model_name)) |
| 154 | + assert os.path.exists(os.path.join("/opt/ml/model/code", "inference.py")) |
| 155 | + assert os.path.exists(os.path.join("/opt/ml/model/code/lib", "a")) |
| 156 | + assert os.path.exists(os.path.join("/opt/ml/model/code/lib", "bb")) |
| 157 | + assert os.path.exists(os.path.join("/opt/ml/model/code/lib/dir", "b")) |
| 158 | + assert os.path.exists(os.path.join("/opt/ml/model/code/", "foo.py")) |
| 159 | + assert os.path.exists(os.path.join("/opt/ml/model/code/some/dir", "a")) |
| 160 | + |
| 161 | + |
| 162 | +def create_file_tree(root, tree): |
| 163 | + for file in tree: |
| 164 | + try: |
| 165 | + os.makedirs(os.path.join(root, os.path.dirname(file))) |
| 166 | + except: # noqa: E722 Using bare except because p2/3 incompatibility issues. |
| 167 | + pass |
| 168 | + with open(os.path.join(root, file), "a") as f: |
| 169 | + f.write(file) |
| 170 | + |
| 171 | + |
| 172 | +@pytest.fixture() |
| 173 | +def tmp(tmpdir): |
| 174 | + yield str(tmpdir) |
0 commit comments