File: bounds_inference.cpp

package info (click to toggle)
pytorch 1.7.1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 80,340 kB
  • sloc: cpp: 670,830; python: 343,991; ansic: 67,845; asm: 5,503; sh: 2,924; java: 2,888; xml: 266; makefile: 244; ruby: 148; yacc: 144; objc: 51; lex: 44
file content (263 lines) | stat: -rw-r--r-- 7,846 bytes parent folder | download
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
#include <torch/csrc/jit/tensorexpr/bounds_inference.h>
#include <torch/csrc/jit/tensorexpr/expr.h>
#include <torch/csrc/jit/tensorexpr/ir.h>
#include <torch/csrc/jit/tensorexpr/ir_printer.h>
#include <torch/csrc/jit/tensorexpr/ir_simplifier.h>
#include <torch/csrc/jit/tensorexpr/ir_visitor.h>
#include <torch/csrc/jit/tensorexpr/stmt.h>

namespace torch {
namespace jit {
namespace tensorexpr {

class BoundsInference : public IRVisitor {
 public:
  void visit(const FunctionCall* v) override;
  void visit(const Load* v) override;
  void visit(const Store* v) override;
  void visit(const For* v) override;
  void visit(const Block* v) override;

  BoundsInfo accesses() const {
    return accesses_;
  }

 private:
  BoundsInfo accesses_;
};

void BoundsInference::visit(const Load* v) {
  accesses_[v->buf()].push_back({kLoad, v->indices(), v->indices()});
}

void BoundsInference::visit(const FunctionCall* v) {
  accesses_[v->tensor()->buf()].push_back({kLoad, v->params(), v->params()});
}

void BoundsInference::visit(const Store* v) {
  accesses_[v->buf()].push_back({kStore, v->indices(), v->indices()});
  IRVisitor::visit(v);
}

void BoundsInference::visit(const For* v) {
  v->body()->accept(this);
  for (auto& pair : accesses_) {
    for (TensorAccessBoundsInfo& access : pair.second) {
      for (size_t j = 0; j < access.start.size(); j++) {
        // TODO: This function assumes that all indices grow monotonically and
        // thus for the loop:
        //   for i in A..B:
        //     buf[i] = i
        // the range for i is [A, B). It should be generalized to correctly
        // handle all cases.
        const Expr* old_start = access.start[j];
        const Expr* old_stop = access.stop[j];
        const Expr* new_start = Substitute(old_start, {{v->var(), v->start()}});
        const Expr* new_stop = Substitute(
            old_stop, {{v->var(), new Sub(v->stop(), new IntImm(1))}});

        access.start[j] = IRSimplifier::simplify(new_start);
        access.stop[j] = IRSimplifier::simplify(new_stop);
      }
    }
  }
}

void BoundsInference::visit(const Block* v) {
  BoundsInfo res;
  for (auto s : *v) {
    s->accept(this);
    for (auto& pair : accesses_) {
      res[pair.first].insert(
          res[pair.first].end(), pair.second.begin(), pair.second.end());
    }
  }
  accesses_ = res;
}

void printBoundsInfo(const BoundsInfo& v) {
  std::cerr << "Access vector {\n";
  for (auto& pair : v) {
    std::cerr << *pair.first << " in [";
    bool first = true;
    for (const auto& b : pair.second) {
      if (!first) {
        std::cerr << ", ";
      }
      std::cerr << ((b.kind == kLoad) ? "LOAD" : "STORE") << "(";
      int i = 0;
      if (b.start.empty()) {
        std::cerr << "0";
      }
      for (const auto& s : b.start) {
        if (i != 0) {
          std::cerr << ", ";
        }
        std::cerr << *s;
        i++;
      }
      std::cerr << "; ";
      i = 0;
      if (b.stop.empty()) {
        std::cerr << "0";
      }
      for (const auto& s : b.stop) {
        if (i != 0) {
          std::cerr << ", ";
        }
        std::cerr << *s;
        i++;
      }
      std::cerr << ")";
      first = false;
    }
    std::cerr << "]\n";
  }
  std::cerr << "}\n";
}

bool equalExprs(const Expr* A, const Expr* B) {
  const Expr* diff = IRSimplifier::simplify(new Sub(B, A));
  return diff->isConstant() && immediateEquals(diff, 0);
}

// returns the bounds of an overlapping range, or {nullptr, nullptr} if the
// ranges don't overlap.
std::pair<const Expr*, const Expr*> rangeOverlap(
    const Expr* s1,
    const Expr* e1,
    const Expr* s2,
    const Expr* e2) {
  // If they're equal they're equal.
  if (equalExprs(s1, s2) && equalExprs(e1, e2)) {
    return {s1, e1};
  }

  std::pair<const Expr*, const Expr*> noOverlap = {nullptr, nullptr};
  std::pair<const Expr*, const Expr*> overlap = {
      IRSimplifier::simplify(new Min(s1, s2, true)),
      IRSimplifier::simplify(new Max(e1, e2, true))};

  const Expr* lowDiff = IRSimplifier::simplify(new Sub(s1, e2));
  const Expr* highDiff = IRSimplifier::simplify(new Sub(s2, e1));
  if (lowDiff->isConstant() && highDiff->isConstant()) {
    // No overlap.
    if (!(immediateAs<int>(lowDiff) <= 1 || immediateAs<int>(highDiff) >= 1)) {
      return noOverlap;
    }

    return overlap;
  }

  // Can still merge if we can infer adjacency without knowing static values:
  // If we know one side, we can use the fact that each eX >= sX.
  if (highDiff->isConstant() && abs(immediateAs<int>(highDiff)) <= 1) {
    return {s1, e2};
  }

  if (lowDiff->isConstant() && abs(immediateAs<int>(lowDiff)) <= 1) {
    return {s2, e1};
  }

  const Expr* diffs = IRSimplifier::simplify(new Sub(s2, s1));
  const Expr* diffe = IRSimplifier::simplify(new Sub(e2, e1));

  // If one side fully encloses the other, they're adjacent.
  if (diffs->isConstant() && diffe->isConstant()) {
    int ds_i = immediateAs<int>(diffs);
    int de_i = immediateAs<int>(diffe);
    if ((ds_i <= 0 && de_i >= 0) || (ds_i >= 0 && de_i <= 0)) {
      return overlap;
    }
  }

  // If either the start or end is 1 element apart from it's pair, they must
  // be adjacent.
  if (diffs->isConstant() && abs(immediateAs<int>(diffs)) <= 1) {
    return overlap;
  }

  if (diffe->isConstant() && abs(immediateAs<int>(diffe)) <= 1) {
    return overlap;
  }

  return noOverlap;
}

/*
 * Go through the given BoundsInfo vector and merge entries corresponding to
 * the same buf. E.g. given
 *    [{a, kLoad, 0, 100}, {b, kStore, 0, 100}, {a, kLoad, 10, 110}]
 * produce:
 *    [{a, kLoad, 0, 110}, {b, kStore, 0, 100}]
 */
BoundsInfo mergeTensorAccesses(const BoundsInfo& unmerged) {
  BoundsInfo res;
  // For each buf in the BoundsInfo:
  for (auto& pair : unmerged) {
    const std::vector<TensorAccessBoundsInfo>& new_vec = pair.second;
    std::vector<TensorAccessBoundsInfo>& existing_vec = res[pair.first];

    // For each bound pair in the unmerged set:
    for (const auto& new_bound : new_vec) {
      bool found = false;
      // For each already merged bound pair:
      for (auto& existing_bound : existing_vec) {
        // Only merge the same kind of access.
        if (existing_bound.kind != new_bound.kind) {
          continue;
        }

        // Sanity check the buf indices have the same dimensionality.
        TORCH_INTERNAL_ASSERT(new_bound.start.size() == new_bound.stop.size());
        TORCH_INTERNAL_ASSERT(
            existing_bound.start.size() == existing_bound.stop.size());
        TORCH_INTERNAL_ASSERT(
            new_bound.start.size() == existing_bound.start.size());

        std::vector<const Expr*> start;
        std::vector<const Expr*> stop;
        bool fail = false;
        // For each dimension:
        for (size_t i = 0; i < new_bound.start.size(); ++i) {
          // The range of the new bound must overlap the existing bound.
          // TODO(nickg): we allow all dimensions to partially overlap,
          // which will overstate the bounds.
          auto pair = rangeOverlap(
              new_bound.start[i],
              new_bound.stop[i],
              existing_bound.start[i],
              existing_bound.stop[i]);
          if (pair.first == nullptr) {
            fail = true;
            break;
          }
          start.push_back(pair.first);
          stop.push_back(pair.second);
        }
        if (fail) {
          continue;
        }
        found = true;
        // Update the existing bound.
        existing_bound.start = start;
        existing_bound.stop = stop;
      }
      if (!found) {
        existing_vec.push_back(new_bound);
      }
    }
  }

  return res;
}

BoundsInfo inferBounds(Stmt* s) {
  BoundsInference ac;
  s->accept(&ac);
  return mergeTensorAccesses(ac.accesses());
}

} // namespace tensorexpr
} // namespace jit
} // namespace torch