Skip to content

Commit

Permalink
[SR] Release reference to JIT module if possible (pytorch#67911)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: pytorch#67911

If we can remove `self` from the graph inputs, there is no need for `StaticModule` to hold onto its `Module` reference anymore.

Test Plan: `buck test caffe2/benchmarks/static_runtime:static_runtime_cpptest`

Reviewed By: hlu1

Differential Revision: D32190755

fbshipit-source-id: 9c4649a63b6e68c7d2e47395a23572985d2babb1
  • Loading branch information
Mike Iovine authored and facebook-github-bot committed Nov 9, 2021
1 parent 9ae3f39 commit 5e19fb6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
3 changes: 1 addition & 2 deletions torch/csrc/jit/runtime/static/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,11 +657,10 @@ StaticModule::StaticModule(
// handle schema
if (module_.has_value()) {
Method method = module_->get_method("forward");
schema_ = method.function().getSchema();
if (RemoveSelfFromGraphInput(graph_)) {
schema_ = RemoveSelfFromSchema(method.function().getSchema());
module_ = c10::nullopt;
} else {
first_input_is_self_ = true;
schema_ = method.function().getSchema();
}
}
Expand Down
4 changes: 2 additions & 2 deletions torch/csrc/jit/runtime/static/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class TORCH_API StaticModule {
}

const Module& module() const {
DCHECK(module_.has_value());
return *module_;
}

Expand Down Expand Up @@ -229,14 +230,13 @@ class TORCH_API StaticModule {
}

bool first_input_is_self() const {
return first_input_is_self_;
return module_.has_value();
}

StaticRuntime& runtime();

private:
StaticModuleOptions opts_;
bool first_input_is_self_{false};
std::shared_ptr<torch::jit::Graph> graph_;
c10::optional<torch::jit::Module> module_;
c10::optional<c10::FunctionSchema> schema_;
Expand Down

0 comments on commit 5e19fb6

Please sign in to comment.