Skip to content

Commit 261de53

Browse files
authored
[Collage] CombinerRule and CandidatePartition::EstimateCost (#12078)
* [Collage] CombinerRule and CandidatePartition::EstimateCost See https://github.com/apache/tvm-rfcs/blob/main/rfcs/0062-collage.md. We complete the PartitionRule sub-class hierarchy with the addition of CombinePartitionRule, which allows disjoint candidate partitions to be unioned based on simple rules. - By TOpPattern kind, eg a kOutElemwiseFusable and kBroadcast. - A tuple argument with injective fields. - The projection from an injective group (obviously of tuple type) - Combinations of the above. These let us mimic many common fusion strategies, including TVMs, so that the candidates explored during Collage search are as large as possible to expose possible fusion opportunities but no larger. Also completes CandidatePartition with the EstimateCost method, which is used during search to construct a stand-alone IRModule for latency estimation. Finish units tests for PartitionRule and CandidatePartition. * - fix relay.collage ffi prefix.
1 parent 4b5dd13 commit 261de53

15 files changed

+2167
-92
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*!
21+
* \file src/relay/collage/candidate_function_cache.cc
22+
* \brief A cache of the unique global name and costs for partitioned functions.
23+
*/
24+
25+
#include "./candidate_function_cache.h"
26+
27+
namespace tvm {
28+
namespace relay {
29+
namespace collage {
30+
31+
CandidateFunctionCache::Entry& CandidateFunctionCache::GetEntry(const std::string& label,
32+
const Function& function) {
33+
auto itr = cache_.find(function);
34+
if (itr == cache_.end()) {
35+
String compiler = function->GetAttr<String>(attr::kCompiler, String("tvm")).value();
36+
std::string global_symbol_name = name_supply_->Fresh({compiler, label});
37+
GlobalVar global_symbol(std::move(global_symbol_name), function->checked_type());
38+
itr = cache_.emplace(function, Entry(std::move(global_symbol))).first;
39+
}
40+
return itr->second;
41+
}
42+
43+
GlobalVar CandidateFunctionCache::GetGlobalSymbol(const Function& function) {
44+
return GetEntry(/*label=*/"", function).global_symbol;
45+
}
46+
47+
} // namespace collage
48+
} // namespace relay
49+
} // namespace tvm
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*!
21+
* \file src/relay/collage/candidate_function_cache.h
22+
* \brief A cache of the unique global symbol name and cost for partitioned functions.
23+
*/
24+
25+
#ifndef TVM_RELAY_COLLAGE_CANDIDATE_FUNCTION_CACHE_H_
26+
#define TVM_RELAY_COLLAGE_CANDIDATE_FUNCTION_CACHE_H_
27+
28+
#include <tvm/relay/function.h>
29+
30+
#include <memory>
31+
#include <string>
32+
#include <unordered_map>
33+
#include <utility>
34+
35+
#include "../transforms/compiler_function_utils.h"
36+
#include "./cost.h"
37+
#include "./name_supply.h"
38+
39+
namespace tvm {
40+
namespace relay {
41+
namespace collage {
42+
43+
/*!
44+
* \brief A cache of the unique global symbol and cost for functions extracted to represent
45+
* partitions. If two functions are structurally equal (which includes equality of their "Compiler"
46+
* attributes) then they will share the same global symbol and estimated cost. We rely on the
47+
* function's attributes to distinguish partitions which are structurally the same graph but
48+
* intended for different targets.
49+
*/
50+
class CandidateFunctionCache : public transform::GlobalSymbolCache {
51+
public:
52+
explicit CandidateFunctionCache(std::shared_ptr<NameSupply> name_supply)
53+
: name_supply_(std::move(name_supply)) {}
54+
55+
struct Entry {
56+
GlobalVar global_symbol;
57+
Cost cost = Cost::Unknown(); // Filled in when have estimated cost.
58+
59+
explicit Entry(GlobalVar global_symbol) : global_symbol(std::move(global_symbol)) {}
60+
};
61+
62+
/*!
63+
* \brief Returns the unique entry for \p function. If no such entry already exists, create it
64+
* and assign it a unique global symbol name.
65+
*/
66+
Entry& GetEntry(const std::string& label, const Function& function);
67+
68+
GlobalVar GetGlobalSymbol(const Function& function) final;
69+
70+
private:
71+
std::shared_ptr<NameSupply> name_supply_;
72+
std::unordered_map<Function, Entry, StructuralHash, StructuralEqual> cache_;
73+
};
74+
75+
} // namespace collage
76+
} // namespace relay
77+
} // namespace tvm
78+
79+
#endif // TVM_RELAY_COLLAGE_CANDIDATE_FUNCTION_CACHE_H_

src/relay/collage/candidate_partition.cc

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424

2525
#include "./candidate_partition.h"
2626

27+
#include <tvm/relay/analysis.h>
2728
#include <tvm/relay/attrs/memory.h>
29+
#include <tvm/relay/transform.h>
2830

31+
#include "../transforms/compiler_function_utils.h"
32+
#include "./candidate_function_cache.h"
2933
#include "./candidate_set.h"
3034
#include "./partition_rule.h"
3135
#include "./partition_spec.h"
@@ -106,6 +110,102 @@ std::string CandidatePartitionNode::ToString() const {
106110
return os.str();
107111
}
108112

113+
namespace {
114+
/*!
115+
* \brief If function's body is a call to an inlined "Primitive" function, return it.
116+
* Otherwise return function directly.
117+
*/
118+
Function GetPrimitiveFunction(const Function& function) {
119+
if (const auto* call_node = function->body.as<CallNode>()) {
120+
if (const auto* function_node = call_node->op.as<FunctionNode>()) {
121+
if (function_node->HasNonzeroAttr(attr::kPrimitive)) {
122+
return GetRef<Function>(function_node);
123+
}
124+
}
125+
}
126+
return function;
127+
}
128+
129+
/*!
130+
* \brief Eta-expand any tuple arguments of \p function. Ie rewrite:
131+
* \code
132+
* f(x: (t1, t2)) { ... x ... }
133+
* \endcode
134+
* to
135+
* \code
136+
* f(x_1: t1, x_2: t2) { ... (x_1, x_2) ... }
137+
* \endcode
138+
*/
139+
Function EtaExpandTuples(const Function& function) {
140+
Map<Var, Expr> subst;
141+
Array<Var> new_params;
142+
for (const auto& param : function->params) {
143+
std::vector<TensorType> tensor_types = FlattenTupleType(param->type_annotation);
144+
if (tensor_types.size() == 1) {
145+
new_params.push_back(param);
146+
} else {
147+
Array<Expr> fields;
148+
for (size_t i = 0; i < tensor_types.size(); ++i) {
149+
Var new_param(param->name_hint() + "_" + std::to_string(i), tensor_types[i], param->span);
150+
new_param->checked_type_ = tensor_types[i];
151+
new_params.push_back(new_param);
152+
fields.push_back(new_param);
153+
}
154+
Tuple new_tuple(fields);
155+
subst.Set(param, new_tuple);
156+
}
157+
}
158+
if (subst.empty()) {
159+
return function;
160+
}
161+
return WithFields(function, new_params, Bind(function->body, subst));
162+
}
163+
164+
} // namespace
165+
166+
Cost CandidatePartitionNode::EstimatedCost(
167+
const DataflowGraph& dataflow_graph, const CostEstimator& cost_estimator,
168+
const std::shared_ptr<CandidateFunctionCache>& cache) const {
169+
if (cost_.is_unknown()) {
170+
VLOG_CONTEXT << "spec " << partition_spec_name();
171+
Function extracted_function = sub_graph_->ExtractAsFunction(dataflow_graph);
172+
VLOG(2) << "Extracted function:" << std::endl << PrettyPrint(extracted_function);
173+
extracted_function = EtaExpandTuples(extracted_function);
174+
VLOG(2) << "Validating function:" << std::endl << PrettyPrint(extracted_function);
175+
String error = partition_spec()->validate_sub_graph_func_(extracted_function);
176+
if (!error.empty()) {
177+
cost_ = Cost::Invalid();
178+
VLOG(1) << "Unable to rewrite function: " << error;
179+
} else {
180+
// The extracted function may be the eta-expansion of a "Primitive" function.
181+
// If so we want the cached external name and cost to be w.r.t. that function
182+
// rather than the outer so that we'll get a cache hit when we outline functions
183+
// in the final program.
184+
Function primitive_function = GetPrimitiveFunction(extracted_function);
185+
CandidateFunctionCache::Entry& entry =
186+
cache->GetEntry(sub_graph_->label_, primitive_function);
187+
if (entry.cost.is_unknown()) {
188+
IRModule mod = IRModule::FromExpr(extracted_function);
189+
VLOG(1) << "Outlining:" << std::endl << PrettyPrint(mod);
190+
mod = OutlineCompilerFunctions(cache)(mod);
191+
VLOG(1) << "Estimating cost of:" << std::endl
192+
<< PrettyPrint(mod) << std::endl
193+
<< "using target " << target()->ToDebugString();
194+
entry.cost = cost_estimator->Estimate(mod, target(),
195+
/*needs_tvm_tuning=*/!target().IsExternalCodegen());
196+
VLOG(1) << "Measured cost as " << entry.cost.ToString();
197+
} else {
198+
VLOG(1) << "Reusing cost " << entry.cost.ToString()
199+
<< " cached in candidate function cache";
200+
}
201+
cost_ = entry.cost;
202+
}
203+
} else {
204+
VLOG(1) << "Reusing cost " << cost_.ToString() << " cached in candidate";
205+
}
206+
return cost_;
207+
}
208+
109209
CandidatePartition::CandidatePartition(String rule_name, SubGraph sub_graph,
110210
ObjectRef /* actually PartitionSpec */ spec, Cost cost) {
111211
auto node = runtime::make_object<CandidatePartitionNode>();

src/relay/collage/candidate_partition.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
#include <string>
3333
#include <vector>
3434

35+
#include "./candidate_function_cache.h"
3536
#include "./cost.h"
37+
#include "./cost_estimator.h"
38+
#include "./name_supply.h"
3639
#include "./sub_graph.h"
3740

3841
namespace tvm {
@@ -93,6 +96,13 @@ class CandidatePartitionNode : public Object {
9396
*/
9497
Target target() const;
9598

99+
/*!
100+
* \brief Return the estimated cost of the candidate partition, using \p cost_estimator and
101+
* \p cache.
102+
*/
103+
Cost EstimatedCost(const DataflowGraph& dataflow_graph, const CostEstimator& cost_estimator,
104+
const std::shared_ptr<CandidateFunctionCache>& cache) const;
105+
96106
/*!
97107
* \brief Returns a brief description of candidate suitable for debugging output.
98108
*/

0 commit comments

Comments
 (0)