|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import os |
| 5 | +import shutil |
| 6 | +import struct |
| 7 | +import tempfile |
| 8 | +import unittest |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +from onnx import TensorProto, helper, save |
| 12 | + |
| 13 | +import onnxruntime as ort |
| 14 | + |
| 15 | + |
| 16 | +class TestSymLinkOnnxModelExternalData(unittest.TestCase): |
| 17 | + def test_symlink_model_and_data_under_same_directory(self): |
| 18 | + # The following directory structure simulates huggingface hub local cache: |
| 19 | + # temp_dir/ (This corresponds to .cache/huggingface/hub/model_id/) |
| 20 | + # blobs/ |
| 21 | + # guid1 |
| 22 | + # guid2 |
| 23 | + # snapshots/version/ |
| 24 | + # model.onnx -> ../../blobs/guid1 |
| 25 | + # data.bin -> ../../blobs/guid2 |
| 26 | + |
| 27 | + self.temp_dir = tempfile.mkdtemp() |
| 28 | + try: |
| 29 | + blobs_dir = os.path.join(self.temp_dir, "blobs") |
| 30 | + os.makedirs(blobs_dir) |
| 31 | + |
| 32 | + snapshots_dir = os.path.join(self.temp_dir, "snapshots", "version") |
| 33 | + os.makedirs(snapshots_dir) |
| 34 | + |
| 35 | + # Create real files in blobs |
| 36 | + # We'll use the helper to create the model, but we need to control where files end up. |
| 37 | + # Let's manually create the data file in blobs |
| 38 | + data_blob_path = os.path.join(blobs_dir, "guid2") |
| 39 | + vals = [float(i) for i in range(10)] |
| 40 | + with open(data_blob_path, "wb") as f: |
| 41 | + f.writelines(struct.pack("f", v) for v in vals) |
| 42 | + |
| 43 | + # Create model in blobs (referencing "data.bin" as external data) |
| 44 | + # When loaded from snapshots/version/model.onnx, ORT looks for snapshots/version/data.bin |
| 45 | + |
| 46 | + input_ = helper.make_tensor_value_info("input", TensorProto.FLOAT, [10]) |
| 47 | + output = helper.make_tensor_value_info("output", TensorProto.FLOAT, [10]) |
| 48 | + tensor = helper.make_tensor("external_data", TensorProto.FLOAT, [10], vals) |
| 49 | + tensor.data_location = TensorProto.EXTERNAL |
| 50 | + tensor.ClearField("float_data") |
| 51 | + tensor.ClearField("raw_data") |
| 52 | + |
| 53 | + k = tensor.external_data.add() |
| 54 | + k.key = "location" |
| 55 | + k.value = "data.bin" # Relative path |
| 56 | + |
| 57 | + offset = tensor.external_data.add() |
| 58 | + offset.key = "offset" |
| 59 | + offset.value = "0" |
| 60 | + |
| 61 | + length = tensor.external_data.add() |
| 62 | + length.key = "length" |
| 63 | + length.value = str(len(vals) * 4) |
| 64 | + |
| 65 | + const_node = helper.make_node("Constant", [], ["const_out"], value=tensor) |
| 66 | + add_node = helper.make_node("Add", ["input", "const_out"], ["output"]) |
| 67 | + graph = helper.make_graph([const_node, add_node], "test_graph", [input_], [output]) |
| 68 | + model = helper.make_model(graph) |
| 69 | + |
| 70 | + model_blob_path = os.path.join(blobs_dir, "guid1") |
| 71 | + save(model, model_blob_path) |
| 72 | + |
| 73 | + # Now create symlinks in snapshots |
| 74 | + model_symlink_path = os.path.join(snapshots_dir, "model.onnx") |
| 75 | + data_symlink_path = os.path.join(snapshots_dir, "data.bin") |
| 76 | + |
| 77 | + try: |
| 78 | + os.symlink(model_blob_path, model_symlink_path) |
| 79 | + os.symlink(data_blob_path, data_symlink_path) |
| 80 | + except (OSError, NotImplementedError) as e: |
| 81 | + self.skipTest(f"Skipping symlink test: symlink creation is not supported in this environment: {e}") |
| 82 | + |
| 83 | + sess = ort.InferenceSession(model_symlink_path, providers=["CPUExecutionProvider"]) |
| 84 | + |
| 85 | + input_data = np.zeros(10, dtype=np.float32) |
| 86 | + res = sess.run(["output"], {"input": input_data}) |
| 87 | + expected = np.array([float(i) for i in range(10)], dtype=np.float32) |
| 88 | + np.testing.assert_allclose(res[0], expected) |
| 89 | + |
| 90 | + finally: |
| 91 | + shutil.rmtree(self.temp_dir) |
| 92 | + |
| 93 | + def test_symlink_with_data_in_model_sub_dir(self): |
| 94 | + # working directory structure (data is in model sub directory): |
| 95 | + # temp_dir/ |
| 96 | + # blobs/ |
| 97 | + # guid1 |
| 98 | + # data/guid2 |
| 99 | + # snapshots/version/ |
| 100 | + # model.onnx -> ../../blobs/guid1 |
| 101 | + # data.bin -> ../../blobs/data/guid2 |
| 102 | + |
| 103 | + self.temp_dir = tempfile.mkdtemp() |
| 104 | + try: |
| 105 | + blobs_dir = os.path.join(self.temp_dir, "blobs") |
| 106 | + os.makedirs(blobs_dir) |
| 107 | + data_dir = os.path.join(blobs_dir, "data") |
| 108 | + os.makedirs(data_dir) |
| 109 | + |
| 110 | + snapshots_dir = os.path.join(self.temp_dir, "snapshots", "version") |
| 111 | + os.makedirs(snapshots_dir) |
| 112 | + |
| 113 | + # Create real files in blobs |
| 114 | + # We'll use the helper to create the model, but we need to control where files end up. |
| 115 | + # Let's manually create the data file in blobs |
| 116 | + data_blob_path = os.path.join(data_dir, "guid2") |
| 117 | + vals = [float(i) for i in range(10)] |
| 118 | + with open(data_blob_path, "wb") as f: |
| 119 | + f.writelines(struct.pack("f", v) for v in vals) |
| 120 | + |
| 121 | + # Create model in blobs (referencing "data.bin" as external data) |
| 122 | + # When loaded from snapshots/version/model.onnx, ORT looks for snapshots/version/data.bin |
| 123 | + |
| 124 | + input_ = helper.make_tensor_value_info("input", TensorProto.FLOAT, [10]) |
| 125 | + output = helper.make_tensor_value_info("output", TensorProto.FLOAT, [10]) |
| 126 | + tensor = helper.make_tensor("external_data", TensorProto.FLOAT, [10], vals) |
| 127 | + tensor.data_location = TensorProto.EXTERNAL |
| 128 | + tensor.ClearField("float_data") |
| 129 | + tensor.ClearField("raw_data") |
| 130 | + |
| 131 | + k = tensor.external_data.add() |
| 132 | + k.key = "location" |
| 133 | + k.value = "data.bin" # Relative path |
| 134 | + |
| 135 | + offset = tensor.external_data.add() |
| 136 | + offset.key = "offset" |
| 137 | + offset.value = "0" |
| 138 | + |
| 139 | + length = tensor.external_data.add() |
| 140 | + length.key = "length" |
| 141 | + length.value = str(len(vals) * 4) |
| 142 | + |
| 143 | + const_node = helper.make_node("Constant", [], ["const_out"], value=tensor) |
| 144 | + add_node = helper.make_node("Add", ["input", "const_out"], ["output"]) |
| 145 | + graph = helper.make_graph([const_node, add_node], "test_graph", [input_], [output]) |
| 146 | + model = helper.make_model(graph) |
| 147 | + |
| 148 | + model_blob_path = os.path.join(blobs_dir, "guid1") |
| 149 | + save(model, model_blob_path) |
| 150 | + |
| 151 | + # Now create symlinks in snapshots |
| 152 | + model_symlink_path = os.path.join(snapshots_dir, "model.onnx") |
| 153 | + data_symlink_path = os.path.join(snapshots_dir, "data.bin") |
| 154 | + |
| 155 | + try: |
| 156 | + os.symlink(model_blob_path, model_symlink_path) |
| 157 | + os.symlink(data_blob_path, data_symlink_path) |
| 158 | + except (OSError, NotImplementedError) as e: |
| 159 | + self.skipTest(f"Skipping symlink test: symlink creation is not supported in this environment: {e}") |
| 160 | + |
| 161 | + sess = ort.InferenceSession(model_symlink_path, providers=["CPUExecutionProvider"]) |
| 162 | + |
| 163 | + input_data = np.zeros(10, dtype=np.float32) |
| 164 | + res = sess.run(["output"], {"input": input_data}) |
| 165 | + expected = np.array([float(i) for i in range(10)], dtype=np.float32) |
| 166 | + np.testing.assert_allclose(res[0], expected) |
| 167 | + |
| 168 | + finally: |
| 169 | + shutil.rmtree(self.temp_dir) |
| 170 | + |
| 171 | + def test_symlink_with_data_not_in_model_sub_dir(self): |
| 172 | + # working directory structure (data is not in model directory or its sub directories): |
| 173 | + # temp_dir/ |
| 174 | + # model/ |
| 175 | + # guid1 |
| 176 | + # data/ |
| 177 | + # guid2 |
| 178 | + # snapshots/version/ |
| 179 | + # model.onnx -> ../../model/guid1 |
| 180 | + # data.bin -> ../../data/guid2 |
| 181 | + |
| 182 | + self.temp_dir = tempfile.mkdtemp() |
| 183 | + try: |
| 184 | + model_dir = os.path.join(self.temp_dir, "model") |
| 185 | + os.makedirs(model_dir) |
| 186 | + data_dir = os.path.join(self.temp_dir, "data") |
| 187 | + os.makedirs(data_dir) |
| 188 | + |
| 189 | + snapshots_dir = os.path.join(self.temp_dir, "snapshots", "version") |
| 190 | + os.makedirs(snapshots_dir) |
| 191 | + |
| 192 | + # Create real files in data_dir |
| 193 | + # We'll use the helper to create the model, but we need to control where files end up. |
| 194 | + # Let's manually create the data file in data_dir |
| 195 | + data_blob_path = os.path.join(data_dir, "guid2") |
| 196 | + vals = [float(i) for i in range(10)] |
| 197 | + with open(data_blob_path, "wb") as f: |
| 198 | + f.writelines(struct.pack("f", v) for v in vals) |
| 199 | + |
| 200 | + # Create model in model_dir (referencing "data.bin" as external data) |
| 201 | + # When loaded from snapshots/version/model.onnx, ORT looks for snapshots/version/data.bin |
| 202 | + |
| 203 | + input_ = helper.make_tensor_value_info("input", TensorProto.FLOAT, [10]) |
| 204 | + output = helper.make_tensor_value_info("output", TensorProto.FLOAT, [10]) |
| 205 | + tensor = helper.make_tensor("external_data", TensorProto.FLOAT, [10], vals) |
| 206 | + tensor.data_location = TensorProto.EXTERNAL |
| 207 | + tensor.ClearField("float_data") |
| 208 | + tensor.ClearField("raw_data") |
| 209 | + |
| 210 | + k = tensor.external_data.add() |
| 211 | + k.key = "location" |
| 212 | + k.value = "data.bin" # Relative path |
| 213 | + |
| 214 | + offset = tensor.external_data.add() |
| 215 | + offset.key = "offset" |
| 216 | + offset.value = "0" |
| 217 | + |
| 218 | + length = tensor.external_data.add() |
| 219 | + length.key = "length" |
| 220 | + length.value = str(len(vals) * 4) |
| 221 | + |
| 222 | + const_node = helper.make_node("Constant", [], ["const_out"], value=tensor) |
| 223 | + add_node = helper.make_node("Add", ["input", "const_out"], ["output"]) |
| 224 | + graph = helper.make_graph([const_node, add_node], "test_graph", [input_], [output]) |
| 225 | + model = helper.make_model(graph) |
| 226 | + |
| 227 | + model_blob_path = os.path.join(model_dir, "guid1") |
| 228 | + save(model, model_blob_path) |
| 229 | + |
| 230 | + # Now create symlinks in snapshots |
| 231 | + model_symlink_path = os.path.join(snapshots_dir, "model.onnx") |
| 232 | + data_symlink_path = os.path.join(snapshots_dir, "data.bin") |
| 233 | + |
| 234 | + try: |
| 235 | + os.symlink(model_blob_path, model_symlink_path) |
| 236 | + os.symlink(data_blob_path, data_symlink_path) |
| 237 | + except (OSError, NotImplementedError) as e: |
| 238 | + self.skipTest(f"Skipping symlink test: symlink creation is not supported in this environment: {e}") |
| 239 | + |
| 240 | + with self.assertRaises(Exception) as cm: |
| 241 | + ort.InferenceSession(model_symlink_path, providers=["CPUExecutionProvider"]) |
| 242 | + |
| 243 | + # We expect an error about external data not under model directory or the real model directory. |
| 244 | + self.assertIn("External data path validation failed", str(cm.exception)) |
| 245 | + finally: |
| 246 | + shutil.rmtree(self.temp_dir) |
| 247 | + |
| 248 | + |
| 249 | +if __name__ == "__main__": |
| 250 | + unittest.main() |
0 commit comments