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.h"
19 #include "tensorflow/compiler/xla/literal_util.h"
20 #include "tensorflow/core/framework/numeric_types.h"
21 #include "tensorflow/core/framework/tensor_testutil.h"
22 #include "tensorflow/core/platform/test.h"
23
24 namespace tensorflow {
25 namespace {
26
TEST(LiteralUtil,LiteralToHostTensor)27 TEST(LiteralUtil, LiteralToHostTensor) {
28 // int64 literal can only be converted to an int64 host tensor.
29 std::vector<int64> int64_values = {1, 2, 3};
30 xla::Literal int64_values_literal =
31 xla::LiteralUtil::CreateR1(absl::Span<const int64>(int64_values));
32 Tensor host_tensor;
33 EXPECT_EQ("Cannot convert literal of type S64 to tensor of type int32",
34 LiteralToHostTensor(int64_values_literal, DT_INT32, &host_tensor)
35 .error_message());
36 EXPECT_EQ("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).ok());
41 test::ExpectTensorEqual<int64>(host_tensor,
42 test::AsTensor<int64>(int64_values));
43 }
44
45 template <class T>
46 using LiteralUtilTest = ::testing::Test;
47 using Types =
48 ::testing::Types<std::pair<int8, qint8>, std::pair<uint8, quint8>,
49 std::pair<int16, qint16>, std::pair<uint16, quint16>,
50 std::pair<int32, qint32>>;
51
52 TYPED_TEST_SUITE(LiteralUtilTest, Types);
53
TYPED_TEST(LiteralUtilTest,LiteralToQuantizedHostTensor)54 TYPED_TEST(LiteralUtilTest, LiteralToQuantizedHostTensor) {
55 using int_type = typename TypeParam::first_type;
56 using qint_type = typename TypeParam::second_type;
57
58 Tensor host_tensor;
59 std::vector<int_type> int_values = {10, 11};
60 xla::Literal int_values_literal =
61 xla::LiteralUtil::CreateR1(absl::Span<const int_type>(int_values));
62 EXPECT_TRUE(LiteralToHostTensor(int_values_literal,
63 DataTypeToEnum<int_type>::value, &host_tensor)
64 .ok());
65 test::ExpectTensorEqual<int_type>(host_tensor,
66 test::AsTensor<int_type>(int_values));
67
68 EXPECT_TRUE(LiteralToHostTensor(int_values_literal,
69 DataTypeToEnum<qint_type>::value,
70 &host_tensor)
71 .ok());
72 std::vector<qint_type> qint_values = {10, 11};
73 test::ExpectTensorEqual<qint_type>(host_tensor,
74 test::AsTensor<qint_type>(qint_values));
75
76 EXPECT_EQ(
77 error::INVALID_ARGUMENT,
78 LiteralToHostTensor(int_values_literal, DT_INT64, &host_tensor).code());
79 }
80
81 } // namespace
82 } // namespace tensorflow
83