aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/hlo_ordering_test.cc
diff options
context:
space:
mode:
authorGravatar Mark Heffernan <meheff@google.com>2017-01-25 14:49:28 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2017-01-25 15:03:50 -0800
commit239493a6825f33c96d64b6a36be6616fbb41e42b (patch)
tree4d1dac37af8ebd79b79741a44dc144ae7b1afc72 /tensorflow/compiler/xla/service/hlo_ordering_test.cc
parent59757b7afd5dd08d5651ca966f03511bb2aad7bd (diff)
Break out HloOrdering classes into separate files.
Add CreateMemoryMinimizingSequence which constructs a sequence of the instructions in an HLO module that heuristically minimizes the total size of live buffers containing HLO outputs. Change: 145599747
Diffstat (limited to 'tensorflow/compiler/xla/service/hlo_ordering_test.cc')
-rw-r--r--tensorflow/compiler/xla/service/hlo_ordering_test.cc83
1 files changed, 83 insertions, 0 deletions
diff --git a/tensorflow/compiler/xla/service/hlo_ordering_test.cc b/tensorflow/compiler/xla/service/hlo_ordering_test.cc
new file mode 100644
index 0000000000..425bee601a
--- /dev/null
+++ b/tensorflow/compiler/xla/service/hlo_ordering_test.cc
@@ -0,0 +1,83 @@
+/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+==============================================================================*/
+
+#include "tensorflow/compiler/xla/service/hlo_ordering.h"
+
+#include <memory>
+#include <string>
+
+#include "tensorflow/compiler/xla/service/hlo_computation.h"
+#include "tensorflow/compiler/xla/service/hlo_instruction.h"
+#include "tensorflow/compiler/xla/service/hlo_opcode.h"
+#include "tensorflow/compiler/xla/shape_util.h"
+#include "tensorflow/compiler/xla/tests/hlo_test_base.h"
+#include "tensorflow/compiler/xla/types.h"
+#include "tensorflow/compiler/xla/xla_data.pb.h"
+
+namespace xla {
+namespace {
+
+class HloOrderingTest : public HloTestBase {};
+
+TEST_F(HloOrderingTest, LastUseScheduledFirst) {
+ // Tests scheduling of the following HLO code:
+ //
+ // %ab = abs(%param)
+ // %exp = exp(%param)
+ // %add = add(%ab, %exp)
+ // %negate = negate(%exp)
+ // %sub = subtract(%add, %negate)
+ //
+ // %add should be scheduled before %negate because %add is the last (and only)
+ // use of %ab. Scheduling %add first then frees up %ab's buffer.
+ const Shape vec = ShapeUtil::MakeShape(xla::F32, {42});
+ auto builder = HloComputation::Builder(TestName());
+ auto param =
+ builder.AddInstruction(HloInstruction::CreateParameter(0, vec, "param"));
+ auto ab = builder.AddInstruction(
+ HloInstruction::CreateUnary(vec, HloOpcode::kAbs, param));
+ auto exp = builder.AddInstruction(
+ HloInstruction::CreateUnary(vec, HloOpcode::kExp, param));
+
+ auto add = builder.AddInstruction(
+ HloInstruction::CreateBinary(vec, HloOpcode::kAdd, ab, exp));
+ auto negate = builder.AddInstruction(
+ HloInstruction::CreateUnary(vec, HloOpcode::kNegate, exp));
+ auto sub = builder.AddInstruction(
+ HloInstruction::CreateBinary(vec, HloOpcode::kSubtract, add, negate));
+
+ HloModule module(TestName());
+ module.AddEntryComputation(builder.Build());
+
+ TF_ASSIGN_OR_ASSERT_OK(
+ SequentialHloOrdering::HloModuleSequence sequence,
+ CreateMemoryMinimizingSequence(module, [](const LogicalBuffer& buffer) {
+ return ShapeUtil::ByteSizeOf(buffer.shape());
+ }));
+ // Verify that all instructions are in the sequence.
+ EXPECT_EQ(module.entry_computation()->instruction_count(),
+ sequence.at(module.entry_computation()).size());
+
+ // The first instruction should be the parameter and the last the root "sub".
+ EXPECT_EQ(param, sequence.at(module.entry_computation()).front());
+ EXPECT_EQ(sub, sequence.at(module.entry_computation()).back());
+
+ SequentialHloOrdering ordering(&module, sequence);
+ EXPECT_TRUE(ordering.ExecutesBefore(add, negate));
+}
+
+} // namespace
+
+} // namespace xla