-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IR] Improve external data handling (#2020)
1. Add an `external_data` option to `ir.save`. This will save initializers as external tensors. It is robust against data loss when overwriting, and is idempotent when the current model does not contain external tensors already referencing the same path. 1. Expose `ir.external_data` module as a public module users can use to manipulate external data. 1. It defines the following methods ```py [ "set_base_dir", "unload_from_model", "load_to_model", "convert_tensors_to_external", "convert_tensors_from_external", ] ``` I renamed `to_external_data` to `unload_from_model` for clarity. **Reviewers please let me know if the naming sounds good.** 1. Support setting a threshold `size_threshold_bytes` to control which tensors are offloaded. 1. Simplified torch_apis logic by leveraging to updated `ir.save` method. 1. Updated the to_external_data function to always load data to memory, iff the tensor references an external data file that is being written to. This simplifies the logic and avoids creating and managing temporary files. 1. Implemented a polyfill of the `zip()` function's strict mode to support Python<=3.9 > [!NOTE] > We **do not** need to add external data options to `ir.load`. The external data is always loaded lazily in the IR. If users want to transfer the data to memory at loading, they can use `ir.external_data.load_to_model()`. ## Example usage ```py ir.save(model, "model.onnx", external_data="model.onnx.data") # Can save many times ir.save(model, "model_copy.onnx", external_data="model_copy.onnx.data") ``` --------- Co-authored-by: Copilot <[email protected]>
- Loading branch information
1 parent
0447822
commit b8d3179
Showing
9 changed files
with
670 additions
and
434 deletions.
There are no files selected for viewing
This file contains 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 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 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.