Home | History | Annotate | Download | only in cpu
      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/cpu/cpu_hlo_support_checker.h"
     17 #include "tensorflow/compiler/xla/shape_util.h"
     18 #include "tensorflow/compiler/xla/test.h"
     19 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
     20 #include "tensorflow/core/lib/core/error_codes.pb.h"
     21 #include "tensorflow/core/lib/core/status_test_util.h"
     22 
     23 namespace xla {
     24 namespace {
     25 
     26 using ::testing::HasSubstr;
     27 
     28 class CpuHloSupportCheckerTest : public HloTestBase {
     29  protected:
     30   CpuHloSupportChecker& checker() { return checker_; }
     31 
     32  private:
     33   CpuHloSupportChecker checker_;
     34 };
     35 
     36 TEST_F(CpuHloSupportCheckerTest, Add) {
     37   HloComputation::Builder builder(TestName());
     38   const Shape scalar_shape = ShapeUtil::MakeShape(F32, {});
     39   HloInstruction* param0 = builder.AddInstruction(
     40       HloInstruction::CreateParameter(0, scalar_shape, "param0"));
     41   HloInstruction* param1 = builder.AddInstruction(
     42       HloInstruction::CreateParameter(1, scalar_shape, "param1"));
     43   builder.AddInstruction(HloInstruction::CreateBinary(
     44       scalar_shape, HloOpcode::kAdd, param0, param1));
     45   auto module = CreateNewModule();
     46   module->AddEntryComputation(builder.Build());
     47 
     48   TF_ASSERT_OK(checker().Run(module.get()).status());
     49 }
     50 
     51 TEST_F(CpuHloSupportCheckerTest, SparseUnimplemented) {
     52   HloComputation::Builder builder(TestName());
     53   const Shape sparse_shape = ShapeUtil::MakeShapeWithSparseLayout(F32, {10}, 2);
     54   HloInstruction* param0 = builder.AddInstruction(
     55       HloInstruction::CreateParameter(0, sparse_shape, "param0"));
     56   HloInstruction* param1 = builder.AddInstruction(
     57       HloInstruction::CreateParameter(1, sparse_shape, "param1"));
     58   builder.AddInstruction(HloInstruction::CreateBinary(
     59       sparse_shape, HloOpcode::kAdd, param0, param1));
     60   auto module = CreateNewModule();
     61   module->AddEntryComputation(builder.Build());
     62 
     63   Status status = checker().Run(module.get()).status();
     64   ASSERT_EQ(status.code(), tensorflow::error::UNIMPLEMENTED);
     65   EXPECT_THAT(status.error_message(),
     66               HasSubstr("CPU backend does not support"));
     67   EXPECT_THAT(status.error_message(),
     68               HasSubstr(ShapeUtil::HumanStringWithLayout(sparse_shape)));
     69 }
     70 
     71 }  // namespace
     72 }  // namespace xla
     73