Home | History | Annotate | Download | only in tf2xla
      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/tf2xla/literal_util.h"
     17 
     18 #include "tensorflow/compiler/xla/literal_util.h"
     19 #include "tensorflow/core/framework/numeric_types.h"
     20 #include "tensorflow/core/framework/tensor_testutil.h"
     21 #include "tensorflow/core/platform/test.h"
     22 
     23 namespace tensorflow {
     24 
     25 TEST(LiteralUtil, LiteralToHostTensor) {
     26   // int64 literal can only be converted to an int64 host tensor.
     27   {
     28     std::vector<int64> int64_values = {1, 2, 3};
     29     std::unique_ptr<xla::Literal> int64_values_literal =
     30         xla::Literal::CreateR1(gtl::ArraySlice<int64>(int64_values));
     31     Tensor host_tensor;
     32     EXPECT_EQ("Cannot convert literal of type S64 to tensor of type int32",
     33               LiteralToHostTensor(*int64_values_literal, DT_INT32, &host_tensor)
     34                   .error_message());
     35     EXPECT_EQ(
     36         "Cannot convert literal of type S64 to tensor of type qint32",
     37         LiteralToHostTensor(*int64_values_literal, DT_QINT32, &host_tensor)
     38             .error_message());
     39     EXPECT_TRUE(
     40         LiteralToHostTensor(*int64_values_literal, DT_INT64, &host_tensor)
     41             .ok());
     42     test::ExpectTensorEqual<int64>(host_tensor,
     43                                    test::AsTensor<int64>(int64_values));
     44   }
     45 
     46   {
     47     // Repeat tests with int32.
     48     Tensor host_tensor;
     49     std::vector<int32> int32_values = {10, 11};
     50     std::unique_ptr<xla::Literal> int32_values_literal =
     51         xla::Literal::CreateR1(gtl::ArraySlice<int32>(int32_values));
     52     EXPECT_TRUE(
     53         LiteralToHostTensor(*int32_values_literal, DT_INT32, &host_tensor)
     54             .ok());
     55     test::ExpectTensorEqual<int32>(host_tensor,
     56                                    test::AsTensor<int32>(int32_values));
     57 
     58     EXPECT_TRUE(
     59         LiteralToHostTensor(*int32_values_literal, DT_QINT32, &host_tensor)
     60             .ok());
     61     std::vector<qint32> qint32_values = {10, 11};
     62     test::ExpectTensorEqual<qint32>(host_tensor,
     63                                     test::AsTensor<qint32>(qint32_values));
     64 
     65     EXPECT_EQ("Cannot convert literal of type S32 to tensor of type int64",
     66               LiteralToHostTensor(*int32_values_literal, DT_INT64, &host_tensor)
     67                   .error_message());
     68   }
     69 }
     70 
     71 }  // namespace tensorflow
     72