1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16// Test for parse_flags_from_env.cc
17
18#include "tensorflow/compiler/xla/legacy_flags/debug_options_parsers.h"
19
20#include <unordered_map>
21#include <vector>
22
23#include "tensorflow/core/lib/strings/str_util.h"
24#include "tensorflow/core/platform/test.h"
25
26namespace xla {
27namespace legacy_flags {
28
29// Test that the xla_backend_extra_options flag is parsed correctly.
30TEST(DebugOptionsFlags, ParseXlaBackendExtraOptions) {
31  std::unordered_map<string, string> test_map;
32  string test_string = "aa=bb,cc,dd=,ee=ff=gg";
33  impl::parse_xla_backend_extra_options(&test_map, test_string);
34  EXPECT_EQ(test_map.size(), 4);
35  EXPECT_EQ(test_map.at("aa"), "bb");
36  EXPECT_EQ(test_map.at("cc"), "");
37  EXPECT_EQ(test_map.at("dd"), "");
38  EXPECT_EQ(test_map.at("ee"), "ff=gg");
39}
40
41// Test that the xla_reduce_precision flag is parsed correctly.
42TEST(DebugOptionsFlags, ParseXlaReducePrecisionOptionNoStrings) {
43  HloReducePrecisionOptions proto;
44  string test_string = "OP_OUTPUTS=5,10:add,dot";
45  EXPECT_TRUE(impl::parse_xla_reduce_precision_option(&proto, test_string));
46  EXPECT_EQ(proto.location(), HloReducePrecisionOptions::OP_OUTPUTS);
47  EXPECT_EQ(proto.exponent_bits(), 5);
48  EXPECT_EQ(proto.mantissa_bits(), 10);
49  EXPECT_EQ(proto.opcodes_to_suffix_size(), 2);
50  EXPECT_EQ(static_cast<HloOpcode>(proto.opcodes_to_suffix(0)),
51            HloOpcode::kAdd);
52  EXPECT_EQ(static_cast<HloOpcode>(proto.opcodes_to_suffix(1)),
53            HloOpcode::kDot);
54  EXPECT_EQ(proto.opname_substrings_to_suffix_size(), 0);
55}
56
57TEST(DebugOptionsFlags, ParseXlaReducePrecisionOptionNoStringsSemicolon) {
58  HloReducePrecisionOptions proto;
59  string test_string = "OP_OUTPUTS=5,10:add,dot;";
60  EXPECT_TRUE(impl::parse_xla_reduce_precision_option(&proto, test_string));
61  EXPECT_EQ(proto.location(), HloReducePrecisionOptions::OP_OUTPUTS);
62  EXPECT_EQ(proto.exponent_bits(), 5);
63  EXPECT_EQ(proto.mantissa_bits(), 10);
64  EXPECT_EQ(proto.opcodes_to_suffix_size(), 2);
65  EXPECT_EQ(static_cast<HloOpcode>(proto.opcodes_to_suffix(0)),
66            HloOpcode::kAdd);
67  EXPECT_EQ(static_cast<HloOpcode>(proto.opcodes_to_suffix(1)),
68            HloOpcode::kDot);
69  EXPECT_EQ(proto.opname_substrings_to_suffix_size(), 0);
70}
71
72TEST(DebugOptionsFlags, ParseXlaReducePrecisionOptionNoOpcodes) {
73  HloReducePrecisionOptions proto;
74  string test_string = "UNFUSED_OP_OUTPUTS=5,10:;foo,bar/baz";
75  EXPECT_TRUE(impl::parse_xla_reduce_precision_option(&proto, test_string));
76  EXPECT_EQ(proto.location(), HloReducePrecisionOptions::UNFUSED_OP_OUTPUTS);
77  EXPECT_EQ(proto.exponent_bits(), 5);
78  EXPECT_EQ(proto.mantissa_bits(), 10);
79  EXPECT_EQ(proto.opcodes_to_suffix_size(), HloOpcodeCount());
80  EXPECT_EQ(proto.opname_substrings_to_suffix_size(), 2);
81  EXPECT_EQ(proto.opname_substrings_to_suffix(0), "foo");
82  EXPECT_EQ(proto.opname_substrings_to_suffix(1), "bar/baz");
83}
84
85TEST(DebugOptionsFlags, ParseXlaReducePrecisionOptionBoth) {
86  HloReducePrecisionOptions proto;
87  string test_string = "UNFUSED_OP_OUTPUTS=5,10:subtract;foo,bar/baz";
88  EXPECT_TRUE(impl::parse_xla_reduce_precision_option(&proto, test_string));
89  EXPECT_EQ(proto.location(), HloReducePrecisionOptions::UNFUSED_OP_OUTPUTS);
90  EXPECT_EQ(proto.exponent_bits(), 5);
91  EXPECT_EQ(proto.mantissa_bits(), 10);
92  EXPECT_EQ(proto.opcodes_to_suffix_size(), 1);
93  EXPECT_EQ(static_cast<HloOpcode>(proto.opcodes_to_suffix(0)),
94            HloOpcode::kSubtract);
95  EXPECT_EQ(proto.opname_substrings_to_suffix_size(), 2);
96  EXPECT_EQ(proto.opname_substrings_to_suffix(0), "foo");
97  EXPECT_EQ(proto.opname_substrings_to_suffix(1), "bar/baz");
98}
99
100}  // namespace legacy_flags
101}  // namespace xla
102
103int main(int argc, char* argv[]) {
104  testing::InitGoogleTest(&argc, argv);
105  return RUN_ALL_TESTS();
106}
107