Home | History | Annotate | Download | only in service
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/compiler/xla/service/zero_sized_hlo_elimination.h"
     17 
     18 #include <memory>
     19 #include <unordered_set>
     20 #include <vector>
     21 
     22 #include "tensorflow/compiler/xla/literal.h"
     23 #include "tensorflow/compiler/xla/service/hlo_computation.h"
     24 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
     25 #include "tensorflow/compiler/xla/service/hlo_module.h"
     26 #include "tensorflow/compiler/xla/service/hlo_opcode.h"
     27 #include "tensorflow/compiler/xla/service/shape_inference.h"
     28 #include "tensorflow/compiler/xla/shape_util.h"
     29 #include "tensorflow/compiler/xla/status_macros.h"
     30 #include "tensorflow/compiler/xla/test.h"
     31 #include "tensorflow/compiler/xla/test_helpers.h"
     32 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
     33 #include "tensorflow/compiler/xla/xla_data.pb.h"
     34 #include "tensorflow/core/platform/logging.h"
     35 
     36 namespace xla {
     37 namespace {
     38 class ZeroSizedHloEliminationTest : public HloTestBase {
     39  protected:
     40   ZeroSizedHloEliminationTest()
     41       : HloTestBase(),
     42         builder_("zero_sized_computation"),
     43         zero_sized_param_(
     44             builder_.AddInstruction(HloInstruction::CreateParameter(
     45                 0, ShapeUtil::MakeShape(F32, {3, 0}), "zero sized param"))) {}
     46 
     47   StatusOr<bool> RunZeroSizedElimination() {
     48     auto module =
     49         CreateNewUnverifiedModule("zero_sized_elimination_test_module");
     50     module->AddEntryComputation(builder_.Build());
     51     return ZeroSizedHloElimination{}.Run(module.get());
     52   }
     53 
     54   HloComputation::Builder builder_;
     55   HloInstruction* zero_sized_param_;
     56 };
     57 
     58 TEST_F(ZeroSizedHloEliminationTest, EliminatedZeroSizedOp) {
     59   builder_.AddInstruction(HloInstruction::CreateUnary(
     60       zero_sized_param_->shape(), HloOpcode::kTanh, zero_sized_param_));
     61   TF_ASSERT_OK_AND_ASSIGN(bool changed, RunZeroSizedElimination());
     62   EXPECT_TRUE(changed);
     63 }
     64 
     65 TEST_F(ZeroSizedHloEliminationTest, DoesNotEliminateParameter) {
     66   TF_ASSERT_OK_AND_ASSIGN(bool changed, RunZeroSizedElimination());
     67   EXPECT_FALSE(changed);
     68 }
     69 
     70 TEST_F(ZeroSizedHloEliminationTest, DoesNotEliminateSideEffects) {
     71   auto token = builder_.AddInstruction(HloInstruction::CreateToken());
     72   builder_.AddInstruction(
     73       HloInstruction::CreateSend(zero_sized_param_, token, 0));
     74   TF_ASSERT_OK_AND_ASSIGN(bool changed, RunZeroSizedElimination());
     75   EXPECT_FALSE(changed);
     76 }
     77 
     78 TEST_F(ZeroSizedHloEliminationTest, DoesNotEliminateConstant) {
     79   builder_.AddInstruction(
     80       HloInstruction::CreateConstant(LiteralUtil::CreateR1({})));
     81   TF_ASSERT_OK_AND_ASSIGN(bool changed, RunZeroSizedElimination());
     82   EXPECT_FALSE(changed);
     83 }
     84 
     85 TEST_F(ZeroSizedHloEliminationTest, ZeroSizedInstructionWithoutLayoutFolded) {
     86   Shape op_shape = ShapeUtil::MakeShape(F32, {4, 0});
     87   op_shape.clear_layout();
     88   HloInstruction* param1 = builder_.AddInstruction(
     89       HloInstruction::CreateParameter(1, op_shape, "zero sized param 1"));
     90   HloInstruction* param2 = builder_.AddInstruction(
     91       HloInstruction::CreateParameter(2, op_shape, "zero sized param 2"));
     92   builder_.AddInstruction(
     93       HloInstruction::CreateBinary(op_shape, HloOpcode::kAdd, param1, param2));
     94   TF_ASSERT_OK_AND_ASSIGN(bool changed, RunZeroSizedElimination());
     95   EXPECT_TRUE(changed);
     96 }
     97 
     98 }  // namespace
     99 }  // namespace xla
    100