[Lite Interpreter] Enable __setstate__ #33294
[Lite Interpreter] Enable __setstate__ #33294iseeyuan wants to merge 13 commits intogh/iseeyuan/47/basefrom
Conversation
💊 CircleCI build failures summary and remediationsAs of commit 7d2d498 (more details on the Dr. CI page): Commit 7d2d498 was recently pushed. Waiting for builds... This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions on the GitHub issue tracker. This comment has been revised 38 times. |
| } | ||
|
|
||
| // operators | ||
| std::vector<IValue> operators; |
There was a problem hiding this comment.
Given we know the vector size, we can just reserve that here?
| }; | ||
| } | ||
|
|
||
| static IValue Tup(std::vector<IValue> ivalues) { |
There was a problem hiding this comment.
Would it make sense to have a version of this that accepts rvalue ref? I see call-sites for this where passed in vector is not used afterwards and we still end up copying it during the call to this function.
There was a problem hiding this comment.
I assume Tup() is a simple helper function. Currently the vector is not used later but not guaranteed. In addition, it's in the export process where I don't think the perf saving (if there is any) with rvalue ref would be noticeable. To keep it simple I'd prefer one API for the helper function.
|
Can you import this to phabricator so we can see the moved code detection? |
| push(*stack, ss.str()); | ||
| } | ||
|
|
||
| void to_dtype_kernal(const c10::OperatorHandle& op, Stack* stack) { |
There was a problem hiding this comment.
Typo, kernal vs kernel.
| norm_index > static_cast<int64_t>(tuple->elements().size())) { | ||
| throw std::out_of_range("Tuple list index out of range"); | ||
| } | ||
| stack->emplace_back(tuple->elements()[norm_index]); |
There was a problem hiding this comment.
Is there a reason to not use pack variant here?
kimishpatel
left a comment
There was a problem hiding this comment.
Left some comments. BTW, for quantized models to work we will also have to add non dynamic versions of linear_run and linear_prepack etc. ops. It is orthogonal to this PR but just wanted to bring it up.
1. Serialize bytecode of __setstate__ and run it when loading the model. 2. One use case is quantization. To test this use case a few operators are registered temporarily for lite interpreter. The "_" prefix registration will be removed when the operators are all migrated to mobile. Differential Revision: [D20162898](https://our.internmc.facebook.com/intern/diff/D20162898)
1. Serialize bytecode of __setstate__ and run it when loading the model. 2. One use case is quantization. To test this use case a few operators are registered temporarily for lite interpreter. The "_" prefix registration will be removed when the operators are all migrated to mobile. Differential Revision: [D20162898](https://our.internmc.facebook.com/intern/diff/D20162898)
1. Serialize bytecode of __setstate__ and run it when loading the model. 2. One use case is quantization. To test this use case a few operators are registered temporarily for lite interpreter. The "_" prefix registration will be removed when the operators are all migrated to mobile. Differential Revision: [D20162898](https://our.internmc.facebook.com/intern/diff/D20162898)
zdevito
left a comment
There was a problem hiding this comment.
Thanks -- see the inline comment about how to find all the setstate methods, I believe this will miss some.
| auto userObj = pop(stack).toObject(); | ||
| // Mobile only: since the number of slots is not known, resize the numAttributes | ||
| // before setSlot. | ||
| while (userObj->type()->numAttributes() <= inst.X) { |
There was a problem hiding this comment.
The combination of while, and v.type() is not correct. I think it would be best to just set all member types to AnyType rather than give them a wrong type.
There was a problem hiding this comment.
There's exception when I use AnyType. Should I disable checkNoAny()?
There was a problem hiding this comment.
I have changed it to NonType, which may be close to "neglecting type in loaded obj" as @zdevito suggested. I feel it's easier than introducing another #ifdef inside setslot(), where some codes may be hidden from unit test.
| methods_.emplace_back(std::move(fn)); | ||
| } | ||
|
|
||
| Function* CompilationUnit::find_method_by_qn(const c10::QualifiedName& qn) { |
There was a problem hiding this comment.
_by_qn feels redundant and they are Funcitons not methods, so maybe just find_function.
| elements.push_back(Tup({func.qualname().qualifiedName(), table})); | ||
| } | ||
| writeArchive("bytecode", Tup(std::move(elements))); | ||
| // top-level methods |
There was a problem hiding this comment.
This approach will not find all the setstate methods that need to be saved. For instance, if there is a custom class as an attribute of a Module, this will not save it. The correct approach is already implemented for standard serialization: it collects all the classes/functions that have been referenced and makes sure to save them. This code should reuse the result of that analysis to know what setstate methods to produce.
There was a problem hiding this comment.
Good catch! I've changed it the similar way as in pickler, to check ivalue of the module and its slots. checkHasValidSetGetState() is reused.
1. Serialize bytecode of __setstate__ and run it when loading the model. 2. One use case is quantization. To test this use case a few operators are registered temporarily for lite interpreter. The "_" prefix registration will be removed when the operators are all migrated to mobile. Differential Revision: [D20162898](https://our.internmc.facebook.com/intern/diff/D20162898)
1. Serialize bytecode of __setstate__ and run it when loading the model. 2. One use case is quantization. To test this use case a few operators are registered temporarily for lite interpreter. The "_" prefix registration will be removed when the operators are all migrated to mobile. Differential Revision: [D20162898](https://our.internmc.facebook.com/intern/diff/D20162898)
1. Serialize bytecode of __setstate__ and run it when loading the model. 2. One use case is quantization. To test this use case a few operators are registered temporarily for lite interpreter. The "_" prefix registration will be removed when the operators are all migrated to mobile. Differential Revision: [D20162898](https://our.internmc.facebook.com/intern/diff/D20162898)
1. Serialize bytecode of __setstate__ and run it when loading the model. 2. One use case is quantization. To test this use case a few operators are registered temporarily for lite interpreter. The "_" prefix registration will be removed when the operators are all migrated to mobile. Differential Revision: [D20162898](https://our.internmc.facebook.com/intern/diff/D20162898)
1. Serialize bytecode of __setstate__ and run it when loading the model. 2. One use case is quantization. To test this use case a few operators are registered temporarily for lite interpreter. The "_" prefix registration will be removed when the operators are all migrated to mobile. Differential Revision: [D20162898](https://our.internmc.facebook.com/intern/diff/D20162898)
1. Serialize bytecode of __setstate__ and run it when loading the model. 2. One use case is quantization. To test this use case a few operators are registered temporarily for lite interpreter. The "_" prefix registration will be removed when the operators are all migrated to mobile. Differential Revision: [D20162898](https://our.internmc.facebook.com/intern/diff/D20162898)
ghstack-source-id: b1e18a5 Pull Request resolved: pytorch/pytorch#33294
Summary: Pull Request resolved: pytorch#33294 1. Serialize bytecode of __setstate__ and run it when loading the model. 2. One use case is quantization. To test this use case a few operators are registered temporarily for lite interpreter. The "_" prefix registration will be removed when the operators are all migrated to mobile. Test Plan: Imported from OSS Differential Revision: D20162898 Pulled By: iseeyuan fbshipit-source-id: 7a3180807bf38fbce594d86993896861f12bb58c
Stack from ghstack:
Differential Revision: D20162898