-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Enhance Model Export with Custom Saver Functionality #7835
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
Conversation
Signed-off-by: Han123su <popsmall212@gmail.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
for more information, see https://pre-commit.ci
Signed-off-by: Han123su <popsmall212@gmail.com>
…to fix-issue-6375
Signed-off-by: Han123su <107395380+Han123su@users.noreply.github.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
…to fix-issue-6375
for more information, see https://pre-commit.ci
Signed-off-by: Han123su <107395380+Han123su@users.noreply.github.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
…to fix-issue-6375
Signed-off-by: Han123su <107395380+Han123su@users.noreply.github.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
…'monai.bundle.scripts' Signed-off-by: Han123su <popsmall212@gmail.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
for more information, see https://pre-commit.ci
Signed-off-by: Han123su <popsmall212@gmail.com>
Signed-off-by: Han123su <popsmall212@gmail.com>
ericspod
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Han123su, thanks for the submission and your work but I'm sorry to say that I don't really see the value of this change. I have made a number of inline comments that I hope you'll understand are meant to be constructive and positive, it's hard to convey tone through text alone I realise!
I feel the intent here was to create a more abstract interface for saving models in different ways that we could easily extend later to include other means. I appreciate that objective but I don't see these changes doing it. The _export function can be parameterised by a callable if we wanted to separate out the actual saving functionality from the other things that are being done, but I wouldn't do this by defining new classes like CkptSaver. Since we can use callables of any sort as parameters in Python, I would use partial instead to wrap pre-defined arguments with a function and then pass that as a parameter, so there's no need to make things class-based.
In terms of common functionality there isn't much between ONNX saving and Torch/TRT saving which is why in our original code these were done separately rather than calling _export in onnx_export. By making _export more general the code that was loading weights and putting the extra_files dictionary together gets duplicated in ckpt_export and trt_export. Duplication is generally bad despite how often I and others are guilty of it, so I would avoid this and leave the code in _export.
To accomplish the goal of making this more generic and parametric, I would suggest that _export should be changed much less with the code for loading checkpoints and putting extra_files together put back in. Instead of calling save_net_with_metadata this function would instead call a callable object which would expect the same named parameters. For Torch/TRT saving that would be just save_net_with_metadata, but for ONNX this would be a partial-wrapped function or a closure function which calls onnx.save itself. This would keep most of the functionality unchanged and reduce duplication (eg. the checkpoint loading in onnx_export is duplicate and could be factored out this way).
I hope that helps and thanks again!
|
Hi, @ericspod , Currently, These are my thoughts on the modifications. Thank you for reading. Maybe I misunderstood the meaning of this issue, or perhaps the author just wanted |
|
Hi @Han123su what I would say we can do here is to do two refactorings:
All this avoid defining new classes and introduces one new function only (the |
for more information, see https://pre-commit.ci
…to fix-issue-6375
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
…to fix-issue-6375
for more information, see https://pre-commit.ci
Ok, I understand your suggestions, and I’ll try to make the modifications accordingly and provide feedback. Thanks again for your help! |
Fixes #6375 . ### Description Changes to be made based on the [previous discussion #7835](#7835). Modify the `_export` function to call the `saver` parameter for saving different models. Rewrite the `onnx_export` function using the updated `_export` to achieve consistency in model format conversion and saving. * Rewrite `onnx_export` to call `_export` with `convert_to_onnx` and appropriate `kwargs`. * Add a `saver: Callable` parameter to `_export`, replacing `save_net_with_metadata`. * Pass `save_net_with_metadata` function wrapped with `partial` to set parameters like `include_config_vals` and `append_timestamp`. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Han123su <popsmall212@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Fixes #6375 .
Description
Make the
_exportfunction focus on model conversion and saving, and add the saver parameter so that different model conversions in the future can also call the_exportfunction, instead of making special behaviors due to specific conditions, but maintaining a consistent behavior pattern.saver_type.pywith various Saver types and Handles specific model parameters using closures._exportfunction to focus on model conversion and saving, adding a saver parameter for flexibility.onnx_export、ckpt_export、trt_exportfunction to utilize the updated_exportfunction.save_net_with_metadatafor better compatibility.Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.