-
Notifications
You must be signed in to change notification settings - Fork 17
/
DerivativeUtils.cpp
530 lines (466 loc) · 15.7 KB
/
DerivativeUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include <iterator>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "CSE.h"
#include "DerivativeUtils.h"
#include "ExprUsesVar.h"
#include "FindCalls.h"
#include "IREquality.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "IRVisitor.h"
#include "Monotonic.h"
#include "RealizationOrder.h"
#include "Simplify.h"
#include "Solve.h"
#include "Substitute.h"
namespace Halide {
namespace Internal {
using std::map;
using std::pair;
using std::set;
using std::string;
using std::vector;
class StripLets : public IRGraphMutator2 {
public:
using IRGraphMutator2::visit;
Expr visit(const Let *op) override {
return mutate(op->body);
}
};
vector<string> gather_variables(const Expr &expr,
const vector<string> &filter) {
class GatherVariables : public IRGraphVisitor {
public:
using IRGraphVisitor::visit;
void visit(const Variable *op) override {
for (const auto &pv : filter) {
if (op->name == pv) {
variables.push_back(op->name);
}
}
}
GatherVariables(const vector<string> &f)
: filter(f) {
}
vector<string> variables;
const vector<string> &filter;
} gatherer(filter);
expr.accept(&gatherer);
return gatherer.variables;
}
vector<string> gather_variables(const Expr &expr,
const vector<Var> &filter) {
vector<string> str_filter;
str_filter.reserve(filter.size());
for (const auto &var : filter) {
str_filter.push_back(var.name());
}
return gather_variables(expr, str_filter);
}
map<string, ReductionVariableInfo> gather_rvariables(Tuple tuple) {
class GatherRVars : public IRGraphVisitor {
public:
using IRGraphVisitor::visit;
void visit(const Variable *op) override {
if (op->reduction_domain.defined()) {
const vector<ReductionVariable> &domain =
op->reduction_domain.domain();
for (int i = 0; i < (int) domain.size(); i++) {
const ReductionVariable &rv = domain[i];
if (rv.var == op->name) {
rvar_map[op->name] = ReductionVariableInfo{
rv.min, rv.extent, i, op->reduction_domain, op->name
};
return;
}
}
internal_error << "Unknown reduction variable encountered";
}
}
map<string, ReductionVariableInfo> rvar_map;
} gatherer;
for (const auto &expr : tuple.as_vector()) {
expr.accept(&gatherer);
}
return gatherer.rvar_map;
}
map<string, ReductionVariableInfo> gather_rvariables(Expr expr) {
return gather_rvariables(Tuple(expr));
}
Expr add_let_expression(const Expr &expr,
const map<string, Expr> &let_var_mapping,
const vector<string> &let_variables) {
// TODO: find a faster way to do this
Expr ret = StripLets().mutate(expr);
bool changed = true;
vector<bool> injected(let_variables.size(), false);
while (changed) {
changed = false;
for (size_t i = 0; i < let_variables.size(); i++) {
const auto &let_variable = let_variables[i];
if (!injected[i] && expr_uses_var(ret, let_variable)) {
auto value = let_var_mapping.find(let_variable)->second;
ret = Let::make(let_variable, value, ret);
injected[i] = true;
changed = true;
}
}
}
return ret;
}
/** Gather the expression DAG and sort them in topological order
*/
class ExpressionSorter : public IRGraphVisitor {
public:
using IRGraphVisitor::include;
using IRGraphVisitor::visit;
vector<Expr> sort(const Expr &expr);
void visit(const Call *op) override;
void visit(const Let *op) override;
void visit(const Variable *op) override;
void visit(const Select *op) override;
protected:
void include(const Expr &e) override;
private:
set<const IRNode *> visited_exprs;
vector<Expr> expr_list;
map<string, Expr> let_var_mapping;
};
vector<Expr> ExpressionSorter::sort(const Expr &e) {
e.accept(this);
expr_list.push_back(e);
return expr_list;
}
void ExpressionSorter::visit(const Call *op) {
// No point visiting the arguments of a Halide func or an image
if (op->call_type == Call::Halide || op->call_type == Call::Image) {
return;
}
for (const auto &arg : op->args) {
include(arg);
}
}
void ExpressionSorter::visit(const Let *op) {
internal_assert(let_var_mapping.find(op->name) == let_var_mapping.end());
let_var_mapping[op->name] = op->value;
include(op->body);
}
void ExpressionSorter::visit(const Select *op) {
// Ignore the condition since the derivative is zero
include(op->true_value);
include(op->false_value);
}
void ExpressionSorter::visit(const Variable *op) {
auto it = let_var_mapping.find(op->name);
if (it != let_var_mapping.end()) {
include(it->second);
}
}
void ExpressionSorter::include(const Expr &e) {
IRGraphVisitor::include(e);
if (visited_exprs.count(e.get()) == 0) {
visited_exprs.insert(e.get());
expr_list.push_back(e);
}
}
vector<Expr> sort_expressions(const Expr &expr) {
ExpressionSorter sorter;
return sorter.sort(expr);
}
map<string, Box> inference_bounds(const vector<Func> &funcs,
const vector<Box> &output_bounds) {
internal_assert(funcs.size() == output_bounds.size());
// Obtain all dependencies
vector<Function> functions;
functions.reserve(funcs.size());
for (const auto &func : funcs) {
functions.push_back(func.function());
}
map<string, Function> env;
for (const auto &func : functions) {
map<string, Function> local_env = find_transitive_calls(func);
env.insert(local_env.begin(), local_env.end());
}
// Reduction variable scopes
Scope<Interval> scope;
for (const auto &it : env) {
Func func = Func(it.second);
for (int i = 0; i < func.num_update_definitions(); i++) {
map<string, ReductionVariableInfo> rvars =
gather_rvariables(func.update_values(i));
for (const auto &it : rvars) {
Interval interval(it.second.min, it.second.min + it.second.extent - 1);
scope.push(it.first, interval);
}
}
}
// Sort functions
vector<string> order = realization_order(functions, env).first;
map<string, Box> bounds;
// Set up bounds for outputs
for (int i = 0; i < (int) funcs.size(); i++) {
const Func &func = funcs[i];
bounds[func.name()] = output_bounds[i];
}
// Traverse from the consumers to the producers
for (auto it = order.rbegin(); it != order.rend(); it++) {
Func func = Func(env[*it]);
// We should already have the bounds of this function
internal_assert(bounds.find(*it) != bounds.end());
const Box ¤t_bounds = bounds[*it];
internal_assert(func.args().size() == current_bounds.size());
// We know the range for each argument of this function
for (int i = 0; i < (int) current_bounds.size(); i++) {
string arg = func.args()[i].name();
scope.push(arg, current_bounds[i]);
}
// Propagate the bounds
for (int update_id = -1; update_id < func.num_update_definitions(); update_id++) {
// For each rhs expression
Tuple tuple = update_id == -1 ? func.values() : func.update_values(update_id);
for (const auto &expr : tuple.as_vector()) {
// For all the immediate dependencies of this expression,
// find the required ranges
map<string, Box> update_bounds =
boxes_required(expr, scope);
// Loop over the dependencies
for (const auto &it : update_bounds) {
// Update the bounds, if not exists then create a new one
auto found = bounds.find(it.first);
if (found == bounds.end()) {
bounds[it.first] = it.second;
} else {
Box new_box = box_union(found->second, it.second);
bounds[it.first] = new_box;
}
}
}
}
for (int i = 0; i < (int) current_bounds.size(); i++) {
scope.pop(func.args()[i].name());
}
}
for (auto &it : bounds) {
auto &bound = it.second;
for (int i = 0; i < (int) bound.size(); i++) {
bound[i].min = common_subexpression_elimination(simplify(bound[i].min));
bound[i].max = common_subexpression_elimination(simplify(bound[i].max));
}
}
return bounds;
}
map<string, Box> inference_bounds(const Func &func,
const Box &output_bounds) {
return inference_bounds(vector<Func>{ func },
vector<Box>{ output_bounds });
}
vector<pair<Expr, Expr>> box_to_vector(const Box &bounds) {
vector<pair<Expr, Expr>> ret;
ret.reserve(bounds.size());
for (const auto &b : bounds.bounds) {
ret.push_back({ b.min, b.max - b.min + 1 });
}
return ret;
}
bool equal(const RDom &bounds0, const RDom &bounds1) {
if (bounds0.domain().domain().size() != bounds1.domain().domain().size()) {
return false;
}
for (int bid = 0; bid < (int) bounds0.domain().domain().size(); bid++) {
if (!equal(bounds0[bid].min(), bounds1[bid].min()) ||
!equal(bounds0[bid].extent(), bounds1[bid].extent())) {
return false;
}
}
return true;
}
vector<string> vars_to_strings(const vector<Var> &vars) {
vector<string> ret;
ret.reserve(vars.size());
for (const auto &var : vars) {
ret.push_back(var.name());
}
return ret;
}
class RDomExtractor : public IRGraphVisitor {
public:
using IRGraphVisitor::visit;
ReductionDomain gather(const Expr &expr) {
expr.accept(this);
return rdom;
}
void visit(const Variable *op) override {
if (op->reduction_domain.defined()) {
rdom = op->reduction_domain;
}
}
private:
ReductionDomain rdom;
};
ReductionDomain extract_rdom(const Expr &expr) {
RDomExtractor extractor;
return extractor.gather(expr);
}
pair<bool, Expr> solve_inverse(Expr expr,
const string &new_var,
const string &var) {
expr = substitute_in_all_lets(simplify(expr));
Interval interval = solve_for_outer_interval(expr, var);
if (!interval.is_bounded()) {
return { false, Expr() };
}
Expr rmin = simplify(interval.min);
Expr rmax = simplify(interval.max);
Expr rextent = simplify(rmax - rmin + 1);
const int64_t *extent_int = as_const_int(rextent);
if (extent_int == nullptr) {
return { false, Expr() };
}
// For some reason interval.is_single_point() doesn't work
if (extent_int != nullptr && *extent_int == 1) {
return { true, rmin };
}
// Create a RDom to loop over the interval
RDom r(0, int(*extent_int));
Expr cond = substitute(var, rmin + r.x, expr.as<EQ>()->b);
cond = substitute(new_var, Var(var), cond) == Var(var);
r.where(cond);
return { true, rmin + r.x };
}
struct BufferDimensionsFinder : public IRGraphVisitor {
public:
using IRGraphVisitor::visit;
map<string, BufferInfo> find(const Func &func) {
buffer_calls.clear();
vector<Expr> vals = func.values().as_vector();
for (Expr val : vals) {
val.accept(this);
}
for (int update_id = 0; update_id < func.num_update_definitions(); update_id++) {
vals = func.update_values(update_id).as_vector();
for (Expr val : vals) {
val.accept(this);
}
}
return buffer_calls;
}
void visit(const Call *op) override {
IRGraphVisitor::visit(op);
if (op->call_type == Call::Image) {
if (op->image.defined()) {
buffer_calls[op->name] = BufferInfo{
op->image.dimensions(),
op->type
};
} else {
internal_assert(op->param.defined());
buffer_calls[op->name] = BufferInfo{
op->param.dimensions(),
op->type
};
}
}
}
map<string, BufferInfo> buffer_calls;
};
map<string, BufferInfo> find_buffer_calls(const Func &func) {
BufferDimensionsFinder finder;
return finder.find(func);
}
struct ImplicitVariablesFinder : public IRGraphVisitor {
public:
using IRGraphVisitor::visit;
set<string> find(Expr expr) {
implicit_variables.clear();
expr.accept(this);
return implicit_variables;
}
void visit(const Variable *op) override {
IRGraphVisitor::visit(op);
if (Var::is_implicit(op->name)) {
implicit_variables.insert(op->name);
}
}
set<string> implicit_variables;
};
set<string> find_implicit_variables(Expr expr) {
ImplicitVariablesFinder finder;
return finder.find(expr);
}
Expr substitute_rdom_predicate(
const string &name, const Expr &replacement, const Expr &expr) {
Expr substituted = substitute(name, replacement, expr);
map<string, ReductionVariableInfo> rvars =
gather_rvariables(substituted);
set<ReductionDomain, ReductionDomain::Compare> rdoms_set;
for (const auto &it : rvars) {
rdoms_set.insert(it.second.domain);
}
vector<ReductionDomain> rdoms;
copy(rdoms_set.begin(), rdoms_set.end(), std::back_inserter(rdoms));
for (auto &r : rdoms) {
Expr predicate = r.predicate();
predicate = substitute(name, replacement, predicate);
r.set_predicate(predicate);
}
return substituted;
}
struct FunctionCallFinder : public IRGraphVisitor {
public:
using IRGraphVisitor::visit;
bool find(const string &func_name_,
const Expr &expr,
const map<string, Expr> &let_var_mapping_) {
func_name = func_name_;
let_var_mapping = &let_var_mapping_;
found = false;
expr.accept(this);
return found;
}
bool find(const Expr &expr,
const map<string, Expr> &let_var_mapping_) {
func_name = "";
let_var_mapping = &let_var_mapping_;
found = false;
expr.accept(this);
return found;
}
void visit(const Variable *var) override {
if (!found) {
auto it = let_var_mapping->find(var->name);
if (it != let_var_mapping->end()) {
found = find(func_name, it->second, *let_var_mapping);
}
}
}
void visit(const Call *op) override {
if (op->call_type == Call::Image || op->call_type == Call::Halide) {
if (func_name == "" || op->name == func_name) {
found = true;
}
}
if (!found) {
IRGraphVisitor::visit(op);
}
}
string func_name;
map<string, Expr> const *let_var_mapping;
bool found;
};
bool is_calling_function(
const string &func_name, const Expr &expr,
const map<string, Expr> &let_var_mapping) {
FunctionCallFinder finder;
return finder.find(func_name, expr, let_var_mapping);
}
bool is_calling_function(
const Expr &expr,
const map<string, Expr> &let_var_mapping) {
FunctionCallFinder finder;
return finder.find(expr, let_var_mapping);
}
} // namespace Internal
} // namespace Halide