aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/heap_simulator.h
diff options
context:
space:
mode:
authorGravatar Yuanzhong Xu <yuanzx@google.com>2018-09-21 13:10:39 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-09-21 13:14:08 -0700
commitf22484881fe1895ee77ec62b6493e015ca40e71a (patch)
tree08b6d926b66127bb32af1831aff6b29e5f10cd45 /tensorflow/compiler/xla/service/heap_simulator.h
parentef630974578b2c1185d4c3848836839d91cb3963 (diff)
[XLA] Add a global decreasing size best-fit buffer allocation algorithm, which sorts buffers by size regardless of their alloc/free time. It uses a interval tree to avoid conflicting allocations.
Also changed to choose the best result from the new algorithm and the old one. PiperOrigin-RevId: 214032637
Diffstat (limited to 'tensorflow/compiler/xla/service/heap_simulator.h')
-rw-r--r--tensorflow/compiler/xla/service/heap_simulator.h62
1 files changed, 62 insertions, 0 deletions
diff --git a/tensorflow/compiler/xla/service/heap_simulator.h b/tensorflow/compiler/xla/service/heap_simulator.h
index ffbf947d5a..7d6dcc0dc9 100644
--- a/tensorflow/compiler/xla/service/heap_simulator.h
+++ b/tensorflow/compiler/xla/service/heap_simulator.h
@@ -351,6 +351,68 @@ class LazyBestFitHeap : public HeapAlgorithm {
std::set<Chunk, OrderChunkByIncreasingSize> free_;
};
+// GlobalDecreasingSizeBestFitHeap collects the live intervals of all buffers,
+// then allocates them in decreasing sizes regardless of the alloc/free time. It
+// internally tracks the allocated buffers and their live intervals; when
+// allocating a buffer, it finds the best-fit free chunk during its live
+// interval.
+class GlobalDecreasingSizeBestFitHeap : public HeapAlgorithm {
+ public:
+ GlobalDecreasingSizeBestFitHeap(int64 alignment) : alignment_(alignment) {}
+ ~GlobalDecreasingSizeBestFitHeap() override {}
+
+ void Alloc(const BufferValue* buffer, int64 size) override;
+ void Free(const BufferValue* buffer, int64 size) override;
+ Result Finish() override;
+
+ private:
+ int64 alignment_;
+ Result result_;
+
+ // The current time represented as an integer. It increments by 1 at each
+ // Alloc or Free call.
+ int64 current_time_ = 0;
+
+ // BufferInterval stores a buffer's size and time interval.
+ struct BufferInterval {
+ const BufferValue* buffer;
+ int64 size;
+ // Alloc time of the buffer.
+ int64 start;
+ // Free time of the buffer.
+ int64 end;
+ };
+ tensorflow::gtl::FlatMap<const BufferValue*, BufferInterval>
+ buffer_intervals_;
+};
+
+// A heap algorithm that chooses the best results from other algorithms added to
+// it.
+class ChooseBestHeapAlgorithm : public HeapAlgorithm {
+ public:
+ ChooseBestHeapAlgorithm(
+ std::unique_ptr<std::vector<std::unique_ptr<HeapAlgorithm>>> algorithms)
+ : algorithms_(std::move(*algorithms)) {}
+ ~ChooseBestHeapAlgorithm() override {}
+
+ void Alloc(const BufferValue* buffer, int64 size) override {
+ for (auto& algorithm : algorithms_) {
+ algorithm->Alloc(buffer, size);
+ }
+ }
+
+ void Free(const BufferValue* buffer, int64 size) override {
+ for (auto& algorithm : algorithms_) {
+ algorithm->Free(buffer, size);
+ }
+ }
+
+ Result Finish() override;
+
+ private:
+ std::vector<std::unique_ptr<HeapAlgorithm>> algorithms_;
+};
+
} // namespace xla
#endif // TENSORFLOW_COMPILER_XLA_SERVICE_HEAP_SIMULATOR_H_