-
Notifications
You must be signed in to change notification settings - Fork 4k
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 1 commit
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
263 changes: 263 additions & 0 deletions
263
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,263 @@ | ||
| # 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 | ||
| import onnx | ||
|
|
||
| from onnx import TensorProto, helper | ||
|
|
||
| import onnxruntime as ort | ||
|
|
||
|
|
||
| class TestWhitelistedData(unittest.TestCase): | ||
| def test_huggingface_hub_symlink(self): | ||
| # working directory structure (simulate huggingface hub local cache): | ||
| # temp_dir/ | ||
|
tianleiwu marked this conversation as resolved.
Outdated
|
||
| # blobs/ | ||
| # guid1 | ||
| # guid2 | ||
| # snapshots/version/onnx/ | ||
| # 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", "onnx") | ||
| 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) | ||
| # Note: The model proto will just say "data.bin". | ||
| # When loaded from snapshots/onnx/model.onnx, ORT looks for snapshots/onnx/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") | ||
| onnx.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}") | ||
|
|
||
| print(f"Model symlink: {model_symlink_path} -> {os.readlink(model_symlink_path)}") | ||
| print(f"Data symlink: {data_symlink_path} -> {os.readlink(data_symlink_path)}") | ||
|
|
||
|
tianleiwu marked this conversation as resolved.
Outdated
|
||
| 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/onnx/ | ||
| # 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", "onnx") | ||
| 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) | ||
| # Note: The model proto will just say "data.bin". | ||
| # When loaded from snapshots/onnx/model.onnx, ORT looks for snapshots/onnx/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") | ||
| onnx.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}") | ||
|
|
||
| print(f"Model symlink: {model_symlink_path} -> {os.readlink(model_symlink_path)}") | ||
| print(f"Data symlink: {data_symlink_path} -> {os.readlink(data_symlink_path)}") | ||
|
|
||
|
tianleiwu marked this conversation as resolved.
Outdated
|
||
| 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/onnx/ | ||
| # model.onnx -> ../../model/guid1 | ||
| # data.bin -> ../../data/guid2 | ||
|
|
||
| self.temp_dir = tempfile.mkdtemp() | ||
|
tianleiwu marked this conversation as 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", "onnx") | ||
| 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) | ||
| # Note: The model proto will just say "data.bin". | ||
| # When loaded from snapshots/onnx/model.onnx, ORT looks for snapshots/onnx/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") | ||
| onnx.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}") | ||
|
|
||
| print(f"Model symlink: {model_symlink_path} -> {os.readlink(model_symlink_path)}") | ||
| print(f"Data symlink: {data_symlink_path} -> {os.readlink(data_symlink_path)}") | ||
|
|
||
|
tianleiwu marked this conversation as resolved.
Outdated
|
||
| with self.assertRaises(Exception) as cm: | ||
| ort.InferenceSession(model_symlink_path, providers=["CPUExecutionProvider"]) | ||
|
|
||
| # We expect an error about external data not being in whitelisted directories | ||
| self.assertIn("External data path validation failed", str(cm.exception)) | ||
| finally: | ||
| shutil.rmtree(self.temp_dir) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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.