Skip to content

Commit c522b63

Browse files
Mikhail Zolotukhinfacebook-github-bot
authored andcommitted
Add 'optimized_graph' to Function. (pytorch#26488)
Summary: Pull Request resolved: pytorch#26488 Currently the main use case for this graph is inlining and that's the only optimization we perform. We probably should run more cleanups on this graph in future. Test Plan: Imported from OSS Differential Revision: D17485745 Pulled By: ZolotukhinM fbshipit-source-id: 7b30c9ba47b4e5fff3591a0063560bfeb68f2164
1 parent c034f97 commit c522b63

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

torch/csrc/jit/function.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <torch/csrc/jit/function.h>
2+
#include <torch/csrc/jit/passes/inliner.h>
23

34
#include <torch/csrc/jit/script/error_report.h>
45

@@ -52,5 +53,12 @@ const FunctionSchema& Function::getSchema() const {
5253
}
5354
return *schema_;
5455
}
56+
57+
void preoptimizeGraph(std::shared_ptr<Graph>& graph) {
58+
// TODO: Invoke cleanup passes before and after inlining to reduce amount of
59+
// code we're copying.
60+
Inline(*graph);
61+
}
62+
5563
} // namespace jit
5664
} // namespace torch

torch/csrc/jit/function.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace jit {
99

1010
using Kwargs = std::unordered_map<std::string, IValue>;
1111

12+
TORCH_API void preoptimizeGraph(std::shared_ptr<Graph>& graph);
13+
1214
// A Function is a pure Graph with no implicit `self` object bound.
1315
// It contains schema information, and the executor that manages the
1416
// execution of the function. script::Method is a wrapper around a
@@ -40,6 +42,16 @@ struct TORCH_API Function {
4042
return graph_;
4143
}
4244

45+
std::shared_ptr<Graph> optimized_graph() const {
46+
if (optimized_graph_) {
47+
return *optimized_graph_;
48+
}
49+
std::lock_guard<std::mutex> lock(compile_mutex);
50+
optimized_graph_ = graph_->copy();
51+
preoptimizeGraph(*optimized_graph_);
52+
return *optimized_graph_;
53+
}
54+
4355
const c10::QualifiedName& qualname() const {
4456
return name_;
4557
}
@@ -99,10 +111,17 @@ struct TORCH_API Function {
99111

100112
private:
101113
c10::QualifiedName name_;
114+
// The original, non-optimized graph
102115
std::shared_ptr<Graph> graph_; // for debugging and for inlining
103116

117+
// Optimized graph, computed lazily. Used for inlining.
118+
// Note: this graph is not specialized, only generic optimizations are applied
119+
// here.
120+
mutable c10::optional<std::shared_ptr<Graph>> optimized_graph_;
121+
104122
// Functions are invokable from multiple threads, so this lock needs to be
105-
// held when we're initializing graph executor for the first time.
123+
// held when we're initializing graph executor for the first time or computing
124+
// the optimized graph.
106125
mutable std::mutex compile_mutex;
107126

108127
GraphExecutor executor_; // for execution

0 commit comments

Comments
 (0)