Skip to content

[Lite Interpreter] Enable __setstate__ #33294

Closed
iseeyuan wants to merge 13 commits intogh/iseeyuan/47/basefrom
gh/iseeyuan/47/head
Closed

[Lite Interpreter] Enable __setstate__ #33294
iseeyuan wants to merge 13 commits intogh/iseeyuan/47/basefrom
gh/iseeyuan/47/head

Conversation

@iseeyuan
Copy link
Copy Markdown
Contributor

@iseeyuan iseeyuan commented Feb 13, 2020

Stack from ghstack:

  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

@iseeyuan iseeyuan requested a review from apaszke as a code owner February 13, 2020 16:19
@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label Feb 13, 2020
@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented Feb 13, 2020

💊 CircleCI build failures summary and remediations

As 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.

iseeyuan pushed a commit that referenced this pull request Feb 27, 2020
ghstack-source-id: 351e4ec
Pull Request resolved: #33294

[Lite Interpreter] Serialize/deserialize __setstate__

ghstack-source-id: 351e4ec
Pull Request resolved: #33295
@iseeyuan iseeyuan changed the title register ops for quantized linear. [Lite Interpreter] Enable __setstate__ Feb 27, 2020
}

// operators
std::vector<IValue> operators;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we know the vector size, we can just reserve that here?

};
}

static IValue Tup(std::vector<IValue> ivalues) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dreiss
Copy link
Copy Markdown
Contributor

dreiss commented Feb 28, 2020

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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to not use pack variant here?

Copy link
Copy Markdown
Contributor

@kimishpatel kimishpatel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
iseeyuan pushed a commit that referenced this pull request Feb 29, 2020
ghstack-source-id: 646caa4
Pull Request resolved: #33294

[Lite Interpreter] Serialize/deserialize __setstate__

ghstack-source-id: 646caa4
Pull Request resolved: #33295
@iseeyuan iseeyuan requested a review from kimishpatel February 29, 2020 00:56
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)
Copy link
Copy Markdown
Contributor

@kimishpatel kimishpatel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

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)
iseeyuan pushed a commit that referenced this pull request Mar 2, 2020
ghstack-source-id: ae93780
Pull Request resolved: #33294

[Lite Interpreter] Serialize/deserialize __setstate__

ghstack-source-id: ae93780
Pull Request resolved: #33295
Copy link
Copy Markdown
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's exception when I use AnyType. Should I disable checkNoAny()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread torch/csrc/jit/mobile/module.cpp Outdated
methods_.emplace_back(std::move(fn));
}

Function* CompilationUnit::find_method_by_qn(const c10::QualifiedName& qn) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
iseeyuan pushed a commit that referenced this pull request Mar 2, 2020
ghstack-source-id: 21a1969
Pull Request resolved: #33294

[Lite Interpreter] Serialize/deserialize __setstate__

ghstack-source-id: 21a1969
Pull Request resolved: #33295
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)
iseeyuan pushed a commit that referenced this pull request Mar 3, 2020
ghstack-source-id: 1b08abf
Pull Request resolved: #33294

[Lite Interpreter] Serialize/deserialize __setstate__

ghstack-source-id: 1b08abf
Pull Request resolved: #33295
@iseeyuan iseeyuan requested a review from zdevito March 3, 2020 23:49
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)
iseeyuan pushed a commit that referenced this pull request Mar 5, 2020
ghstack-source-id: a9e159b
Pull Request resolved: #33294

[Lite Interpreter] Serialize/deserialize __setstate__

ghstack-source-id: a9e159b
Pull Request resolved: #33295
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@iseeyuan merged this pull request in ccf4d69.

@facebook-github-bot facebook-github-bot deleted the gh/iseeyuan/47/head branch March 9, 2020 14:16
karansachdev-1012 pushed a commit to karansachdev-1012/pytorch that referenced this pull request Feb 17, 2026
ghstack-source-id: b1e18a5
Pull Request resolved: pytorch/pytorch#33294
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants