aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/hlo_parser_test.cc
diff options
context:
space:
mode:
authorGravatar Mark Heffernan <meheff@google.com>2018-10-02 13:08:39 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-10-02 13:13:22 -0700
commit78e4ce52aeda5a10ddaf5e64ea8958f439a2f9f2 (patch)
tree096cb5b28777e227053a50e622313cfe66e56744 /tensorflow/compiler/xla/service/hlo_parser_test.cc
parent0a201955b47d484c6bfa149364c264a5b5f91be7 (diff)
Add proto serialization/deserialization testing to the HLO parser tests.
Many of the HLO parser tests verify that an text form of an HLO module preserves all information when running through ToString then parsing. It makes sense to also use these tests to exercise proto serialization/deserialization. This is done by adding additional instantiations of the parameterized parsing tests. This caught several bugs which are fixed in this CL: (1) Domain instructions were not being serialized properly. (2) Host send/recv instructions did not preserve the is_host_transfer bit. (3) Sparse literals could not be serialized or deserialized. PiperOrigin-RevId: 215445200
Diffstat (limited to 'tensorflow/compiler/xla/service/hlo_parser_test.cc')
-rw-r--r--tensorflow/compiler/xla/service/hlo_parser_test.cc85
1 files changed, 58 insertions, 27 deletions
diff --git a/tensorflow/compiler/xla/service/hlo_parser_test.cc b/tensorflow/compiler/xla/service/hlo_parser_test.cc
index 96db96bdb9..dd4ee780f0 100644
--- a/tensorflow/compiler/xla/service/hlo_parser_test.cc
+++ b/tensorflow/compiler/xla/service/hlo_parser_test.cc
@@ -1163,49 +1163,80 @@ ENTRY Sort {
// clang-format on
}
-class HloParserTest : public ::testing::Test,
- public ::testing::WithParamInterface<TestData> {
+// The test class for those tests defined above which round-trip through the
+// parser and ToString is templatized on two bool parameters:
+//
+// short_form : used for the "short" test cases which use the ShortParsable
+// output form.
+// proto_round_trip : whether the module should also be round-tripped through
+// HloProto form. This provides much better coverage for the proto
+// serialization/deserialization.
+//
+// The proto_round_trip=true case also technically covers the Parser->ToString
+// roundtrip as well, but separating out the Parser->ToString roundtrip as its
+// own test provides better isolation and could conceivably catch weirdo bugs
+// which are hidden by interaction between the textual and proto roundtripping.
+template <bool short_form, bool proto_round_trip>
+class HloParameterizedParserTest
+ : public ::testing::Test,
+ public ::testing::WithParamInterface<TestData> {
protected:
- static void ExpectHasSubstr(string_view s, string_view expected) {
- EXPECT_TRUE(absl::StrContains(s, expected))
- << "'" << s << "' does not contain '" << expected << "'";
- }
-
// Expects "ToString(ParseHloString(string)) == string", that is, parses the
// string, asserts that it succeeded, stringifies the parsed module, and
// checks that the it equals the original string.
void ExpectEqual() {
const string& original = GetParam().module_string;
- auto result = ParseHloString(original);
- TF_ASSERT_OK(result.status());
- EXPECT_EQ(original, result.ValueOrDie()->ToString(
- HloPrintOptions().set_print_large_constants(true)));
+ TF_ASSERT_OK_AND_ASSIGN(std::unique_ptr<HloModule> module,
+ ParseHloString(original));
+ if (proto_round_trip) {
+ TF_ASSERT_OK_AND_ASSIGN(module, HloModule::CreateFromProto(
+ module->ToProto(), module->config()));
+ }
+ if (short_form) {
+ EXPECT_EQ(original, module->ToString(HloPrintOptions::ShortParsable()));
+ } else {
+ EXPECT_EQ(
+ original,
+ module->ToString(HloPrintOptions().set_print_large_constants(true)));
+ }
}
};
-class HloParserShortTest : public HloParserTest {
- protected:
- void ExpectEqualShort() {
- const string& original = GetParam().module_string;
- auto result = ParseHloString(original);
- TF_ASSERT_OK(result.status());
- EXPECT_EQ(original,
- result.ValueOrDie()->ToString(HloPrintOptions::ShortParsable()));
- }
-};
+// These using shenanigans are required because the TEST_P macro doesn't like
+// template instantiations which contain commas.
+using HloParserTestLong = HloParameterizedParserTest<false, false>;
+using HloParserTestLongProto = HloParameterizedParserTest<false, true>;
+using HloParserTestShort = HloParameterizedParserTest<true, false>;
+using HloParserTestShortProto = HloParameterizedParserTest<true, true>;
-TEST_P(HloParserTest, Run) { ExpectEqual(); }
+TEST_P(HloParserTestLong, Run) { ExpectEqual(); }
+TEST_P(HloParserTestLongProto, Run) { ExpectEqual(); }
+TEST_P(HloParserTestShort, Run) { ExpectEqual(); }
+TEST_P(HloParserTestShortProto, Run) { ExpectEqual(); }
-TEST_P(HloParserShortTest, Run) { ExpectEqualShort(); }
-
-INSTANTIATE_TEST_CASE_P(HloParserTestSuccessInstantiation, HloParserTest,
+INSTANTIATE_TEST_CASE_P(HloParserTestSuccessInstantiation, HloParserTestLong,
::testing::ValuesIn(CreateTestCases()),
TestDataToString);
-
-INSTANTIATE_TEST_CASE_P(HloParserTestSuccessInstantiation, HloParserShortTest,
+INSTANTIATE_TEST_CASE_P(HloParserTestSuccessInstantiation,
+ HloParserTestLongProto,
+ ::testing::ValuesIn(CreateTestCases()),
+ TestDataToString);
+INSTANTIATE_TEST_CASE_P(HloParserTestSuccessInstantiation, HloParserTestShort,
+ ::testing::ValuesIn(CreateShortTestCases()),
+ TestDataToString);
+INSTANTIATE_TEST_CASE_P(HloParserTestSuccessInstantiation,
+ HloParserTestShortProto,
::testing::ValuesIn(CreateShortTestCases()),
TestDataToString);
+class HloParserTest : public ::testing::Test {
+ protected:
+ static void ExpectHasSubstr(string_view s, string_view expected) {
+ EXPECT_TRUE(absl::StrContains(s, expected))
+ << "'" << s << "' does not contain '" << expected << "'";
+ }
+};
+
TEST_F(HloParserTest, Empty) {
const string original = "";
auto result = ParseHloString(original);