-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Enable Robust Symlink Support for External Data (HF Cache Support) #27374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
250 changes: 250 additions & 0 deletions
250
onnxruntime/test/python/onnxruntime_test_python_symlink_data.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| import os | ||
| import shutil | ||
| import struct | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| import numpy as np | ||
| from onnx import TensorProto, helper, save | ||
|
|
||
| import onnxruntime as ort | ||
|
|
||
|
|
||
| class TestSymLinkOnnxModelExternalData(unittest.TestCase): | ||
| def test_symlink_model_and_data_under_same_directory(self): | ||
| # The following directory structure simulates huggingface hub local cache: | ||
| # temp_dir/ (This corresponds to .cache/huggingface/hub/model_id/) | ||
| # blobs/ | ||
| # guid1 | ||
| # guid2 | ||
| # snapshots/version/ | ||
| # model.onnx -> ../../blobs/guid1 | ||
| # data.bin -> ../../blobs/guid2 | ||
|
|
||
| self.temp_dir = tempfile.mkdtemp() | ||
| try: | ||
| blobs_dir = os.path.join(self.temp_dir, "blobs") | ||
| os.makedirs(blobs_dir) | ||
|
|
||
| snapshots_dir = os.path.join(self.temp_dir, "snapshots", "version") | ||
| os.makedirs(snapshots_dir) | ||
|
|
||
| # Create real files in blobs | ||
| # We'll use the helper to create the model, but we need to control where files end up. | ||
| # Let's manually create the data file in blobs | ||
| data_blob_path = os.path.join(blobs_dir, "guid2") | ||
| vals = [float(i) for i in range(10)] | ||
| with open(data_blob_path, "wb") as f: | ||
| f.writelines(struct.pack("f", v) for v in vals) | ||
|
|
||
| # Create model in blobs (referencing "data.bin" as external data) | ||
| # When loaded from snapshots/version/model.onnx, ORT looks for snapshots/version/data.bin | ||
|
|
||
| input_ = helper.make_tensor_value_info("input", TensorProto.FLOAT, [10]) | ||
| output = helper.make_tensor_value_info("output", TensorProto.FLOAT, [10]) | ||
| tensor = helper.make_tensor("external_data", TensorProto.FLOAT, [10], vals) | ||
| tensor.data_location = TensorProto.EXTERNAL | ||
| tensor.ClearField("float_data") | ||
| tensor.ClearField("raw_data") | ||
|
|
||
| k = tensor.external_data.add() | ||
| k.key = "location" | ||
| k.value = "data.bin" # Relative path | ||
|
|
||
| offset = tensor.external_data.add() | ||
| offset.key = "offset" | ||
| offset.value = "0" | ||
|
|
||
| length = tensor.external_data.add() | ||
| length.key = "length" | ||
| length.value = str(len(vals) * 4) | ||
|
|
||
| const_node = helper.make_node("Constant", [], ["const_out"], value=tensor) | ||
| add_node = helper.make_node("Add", ["input", "const_out"], ["output"]) | ||
| graph = helper.make_graph([const_node, add_node], "test_graph", [input_], [output]) | ||
| model = helper.make_model(graph) | ||
|
|
||
| model_blob_path = os.path.join(blobs_dir, "guid1") | ||
| save(model, model_blob_path) | ||
|
|
||
| # Now create symlinks in snapshots | ||
| model_symlink_path = os.path.join(snapshots_dir, "model.onnx") | ||
| data_symlink_path = os.path.join(snapshots_dir, "data.bin") | ||
|
|
||
| try: | ||
| os.symlink(model_blob_path, model_symlink_path) | ||
| os.symlink(data_blob_path, data_symlink_path) | ||
| except (OSError, NotImplementedError) as e: | ||
| self.skipTest(f"Skipping symlink test: symlink creation is not supported in this environment: {e}") | ||
|
|
||
| sess = ort.InferenceSession(model_symlink_path, providers=["CPUExecutionProvider"]) | ||
|
|
||
| input_data = np.zeros(10, dtype=np.float32) | ||
| res = sess.run(["output"], {"input": input_data}) | ||
| expected = np.array([float(i) for i in range(10)], dtype=np.float32) | ||
| np.testing.assert_allclose(res[0], expected) | ||
|
|
||
| finally: | ||
| shutil.rmtree(self.temp_dir) | ||
|
|
||
| def test_symlink_with_data_in_model_sub_dir(self): | ||
| # working directory structure (data is in model sub directory): | ||
| # temp_dir/ | ||
| # blobs/ | ||
| # guid1 | ||
| # data/guid2 | ||
| # snapshots/version/ | ||
| # model.onnx -> ../../blobs/guid1 | ||
| # data.bin -> ../../blobs/data/guid2 | ||
|
|
||
| self.temp_dir = tempfile.mkdtemp() | ||
| try: | ||
| blobs_dir = os.path.join(self.temp_dir, "blobs") | ||
| os.makedirs(blobs_dir) | ||
| data_dir = os.path.join(blobs_dir, "data") | ||
| os.makedirs(data_dir) | ||
|
|
||
| snapshots_dir = os.path.join(self.temp_dir, "snapshots", "version") | ||
| os.makedirs(snapshots_dir) | ||
|
|
||
| # Create real files in blobs | ||
| # We'll use the helper to create the model, but we need to control where files end up. | ||
| # Let's manually create the data file in blobs | ||
| data_blob_path = os.path.join(data_dir, "guid2") | ||
| vals = [float(i) for i in range(10)] | ||
| with open(data_blob_path, "wb") as f: | ||
| f.writelines(struct.pack("f", v) for v in vals) | ||
|
|
||
| # Create model in blobs (referencing "data.bin" as external data) | ||
| # When loaded from snapshots/version/model.onnx, ORT looks for snapshots/version/data.bin | ||
|
|
||
| input_ = helper.make_tensor_value_info("input", TensorProto.FLOAT, [10]) | ||
| output = helper.make_tensor_value_info("output", TensorProto.FLOAT, [10]) | ||
| tensor = helper.make_tensor("external_data", TensorProto.FLOAT, [10], vals) | ||
| tensor.data_location = TensorProto.EXTERNAL | ||
| tensor.ClearField("float_data") | ||
| tensor.ClearField("raw_data") | ||
|
|
||
| k = tensor.external_data.add() | ||
| k.key = "location" | ||
| k.value = "data.bin" # Relative path | ||
|
|
||
| offset = tensor.external_data.add() | ||
| offset.key = "offset" | ||
| offset.value = "0" | ||
|
|
||
| length = tensor.external_data.add() | ||
| length.key = "length" | ||
| length.value = str(len(vals) * 4) | ||
|
|
||
| const_node = helper.make_node("Constant", [], ["const_out"], value=tensor) | ||
| add_node = helper.make_node("Add", ["input", "const_out"], ["output"]) | ||
| graph = helper.make_graph([const_node, add_node], "test_graph", [input_], [output]) | ||
| model = helper.make_model(graph) | ||
|
|
||
| model_blob_path = os.path.join(blobs_dir, "guid1") | ||
| save(model, model_blob_path) | ||
|
|
||
| # Now create symlinks in snapshots | ||
| model_symlink_path = os.path.join(snapshots_dir, "model.onnx") | ||
| data_symlink_path = os.path.join(snapshots_dir, "data.bin") | ||
|
|
||
| try: | ||
| os.symlink(model_blob_path, model_symlink_path) | ||
| os.symlink(data_blob_path, data_symlink_path) | ||
| except (OSError, NotImplementedError) as e: | ||
| self.skipTest(f"Skipping symlink test: symlink creation is not supported in this environment: {e}") | ||
|
|
||
| sess = ort.InferenceSession(model_symlink_path, providers=["CPUExecutionProvider"]) | ||
|
|
||
| input_data = np.zeros(10, dtype=np.float32) | ||
| res = sess.run(["output"], {"input": input_data}) | ||
| expected = np.array([float(i) for i in range(10)], dtype=np.float32) | ||
| np.testing.assert_allclose(res[0], expected) | ||
|
|
||
| finally: | ||
| shutil.rmtree(self.temp_dir) | ||
|
|
||
| def test_symlink_with_data_not_in_model_sub_dir(self): | ||
| # working directory structure (data is not in model directory or its sub directories): | ||
| # temp_dir/ | ||
| # model/ | ||
| # guid1 | ||
| # data/ | ||
| # guid2 | ||
| # snapshots/version/ | ||
| # model.onnx -> ../../model/guid1 | ||
| # data.bin -> ../../data/guid2 | ||
|
|
||
| self.temp_dir = tempfile.mkdtemp() | ||
tianleiwu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try: | ||
| model_dir = os.path.join(self.temp_dir, "model") | ||
| os.makedirs(model_dir) | ||
| data_dir = os.path.join(self.temp_dir, "data") | ||
| os.makedirs(data_dir) | ||
|
|
||
| snapshots_dir = os.path.join(self.temp_dir, "snapshots", "version") | ||
| os.makedirs(snapshots_dir) | ||
|
|
||
| # Create real files in data_dir | ||
| # We'll use the helper to create the model, but we need to control where files end up. | ||
| # Let's manually create the data file in data_dir | ||
| data_blob_path = os.path.join(data_dir, "guid2") | ||
| vals = [float(i) for i in range(10)] | ||
| with open(data_blob_path, "wb") as f: | ||
| f.writelines(struct.pack("f", v) for v in vals) | ||
|
|
||
| # Create model in model_dir (referencing "data.bin" as external data) | ||
| # When loaded from snapshots/version/model.onnx, ORT looks for snapshots/version/data.bin | ||
|
|
||
| input_ = helper.make_tensor_value_info("input", TensorProto.FLOAT, [10]) | ||
| output = helper.make_tensor_value_info("output", TensorProto.FLOAT, [10]) | ||
| tensor = helper.make_tensor("external_data", TensorProto.FLOAT, [10], vals) | ||
| tensor.data_location = TensorProto.EXTERNAL | ||
| tensor.ClearField("float_data") | ||
| tensor.ClearField("raw_data") | ||
|
|
||
| k = tensor.external_data.add() | ||
| k.key = "location" | ||
| k.value = "data.bin" # Relative path | ||
|
|
||
| offset = tensor.external_data.add() | ||
| offset.key = "offset" | ||
| offset.value = "0" | ||
|
|
||
| length = tensor.external_data.add() | ||
| length.key = "length" | ||
| length.value = str(len(vals) * 4) | ||
|
|
||
| const_node = helper.make_node("Constant", [], ["const_out"], value=tensor) | ||
| add_node = helper.make_node("Add", ["input", "const_out"], ["output"]) | ||
| graph = helper.make_graph([const_node, add_node], "test_graph", [input_], [output]) | ||
| model = helper.make_model(graph) | ||
|
|
||
| model_blob_path = os.path.join(model_dir, "guid1") | ||
| save(model, model_blob_path) | ||
|
|
||
| # Now create symlinks in snapshots | ||
| model_symlink_path = os.path.join(snapshots_dir, "model.onnx") | ||
| data_symlink_path = os.path.join(snapshots_dir, "data.bin") | ||
|
|
||
| try: | ||
| os.symlink(model_blob_path, model_symlink_path) | ||
| os.symlink(data_blob_path, data_symlink_path) | ||
| except (OSError, NotImplementedError) as e: | ||
| self.skipTest(f"Skipping symlink test: symlink creation is not supported in this environment: {e}") | ||
|
|
||
| with self.assertRaises(Exception) as cm: | ||
| ort.InferenceSession(model_symlink_path, providers=["CPUExecutionProvider"]) | ||
|
|
||
| # We expect an error about external data not under model directory or the real model directory. | ||
| self.assertIn("External data path validation failed", str(cm.exception)) | ||
| finally: | ||
| shutil.rmtree(self.temp_dir) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.