stateless_component_test.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2017 Google Inc. 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. #include "dragnn/core/component_registry.h"
  16. #include "dragnn/core/input_batch_cache.h"
  17. #include "dragnn/core/test/generic.h"
  18. #include "dragnn/core/test/mock_transition_state.h"
  19. #include "dragnn/io/sentence_input_batch.h"
  20. #include "syntaxnet/base.h"
  21. #include "syntaxnet/sentence.pb.h"
  22. #include "tensorflow/core/lib/core/errors.h"
  23. #include "tensorflow/core/lib/core/status.h"
  24. #include "tensorflow/core/lib/io/path.h"
  25. #include "tensorflow/core/platform/env.h"
  26. #include "tensorflow/core/platform/protobuf.h"
  27. #include "tensorflow/core/platform/test.h"
  28. namespace syntaxnet {
  29. namespace dragnn {
  30. namespace {
  31. const char kSentence0[] = R"(
  32. token {
  33. word: "Sentence" start: 0 end: 7 tag: "NN" category: "NOUN" label: "ROOT"
  34. break_level: NO_BREAK
  35. }
  36. token {
  37. word: "0" start: 9 end: 9 head: 0 tag: "CD" category: "NUM" label: "num"
  38. break_level: SPACE_BREAK
  39. }
  40. token {
  41. word: "." start: 10 end: 10 head: 0 tag: "." category: "." label: "punct"
  42. break_level: NO_BREAK
  43. }
  44. )";
  45. const char kSentence1[] = R"(
  46. token {
  47. word: "Sentence" start: 0 end: 7 tag: "NN" category: "NOUN" label: "ROOT"
  48. break_level: NO_BREAK
  49. }
  50. token {
  51. word: "1" start: 9 end: 9 head: 0 tag: "CD" category: "NUM" label: "num"
  52. break_level: SPACE_BREAK
  53. }
  54. token {
  55. word: "." start: 10 end: 10 head: 0 tag: "." category: "." label: "punct"
  56. break_level: NO_BREAK
  57. }
  58. )";
  59. const char kLongSentence[] = R"(
  60. token {
  61. word: "Sentence" start: 0 end: 7 tag: "NN" category: "NOUN" label: "ROOT"
  62. break_level: NO_BREAK
  63. }
  64. token {
  65. word: "1" start: 9 end: 9 head: 0 tag: "CD" category: "NUM" label: "num"
  66. break_level: SPACE_BREAK
  67. }
  68. token {
  69. word: "2" start: 10 end: 10 head: 0 tag: "CD" category: "NUM" label: "num"
  70. break_level: SPACE_BREAK
  71. }
  72. token {
  73. word: "3" start: 11 end: 11 head: 0 tag: "CD" category: "NUM" label: "num"
  74. break_level: SPACE_BREAK
  75. }
  76. token {
  77. word: "." start: 12 end: 12 head: 0 tag: "." category: "." label: "punct"
  78. break_level: NO_BREAK
  79. }
  80. )";
  81. const char kMasterSpec[] = R"(
  82. component {
  83. name: "test"
  84. transition_system {
  85. registered_name: "shift-only"
  86. }
  87. linked_feature {
  88. name: "prev"
  89. fml: "input.focus"
  90. embedding_dim: 32
  91. size: 1
  92. source_component: "prev"
  93. source_translator: "identity"
  94. source_layer: "last_layer"
  95. }
  96. backend {
  97. registered_name: "StatelessComponent"
  98. }
  99. }
  100. )";
  101. } // namespace
  102. using testing::Return;
  103. class StatelessComponentTest : public ::testing::Test {
  104. public:
  105. std::unique_ptr<Component> CreateParser(
  106. int beam_size,
  107. const std::vector<std::vector<const TransitionState *>> &states,
  108. const std::vector<string> &data) {
  109. MasterSpec master_spec;
  110. CHECK(TextFormat::ParseFromString(kMasterSpec, &master_spec));
  111. data_.reset(new InputBatchCache(data));
  112. // Create a parser component with the specified beam size.
  113. std::unique_ptr<Component> parser_component(
  114. Component::Create("StatelessComponent"));
  115. parser_component->InitializeComponent(master_spec.component(0));
  116. parser_component->InitializeData(states, beam_size, data_.get());
  117. return parser_component;
  118. }
  119. std::unique_ptr<InputBatchCache> data_;
  120. };
  121. TEST_F(StatelessComponentTest, ForwardsTransitionStates) {
  122. MockTransitionState mock_state_1, mock_state_2, mock_state_3;
  123. const std::vector<std::vector<const TransitionState *>> parent_states = {
  124. {}, {&mock_state_1}, {&mock_state_2, &mock_state_3}};
  125. std::vector<string> data;
  126. for (const string &textproto : {kSentence0, kSentence1, kLongSentence}) {
  127. Sentence sentence;
  128. CHECK(TextFormat::ParseFromString(textproto, &sentence));
  129. data.emplace_back();
  130. CHECK(sentence.SerializeToString(&data.back()));
  131. }
  132. CHECK_EQ(parent_states.size(), data.size());
  133. const int kBeamSize = 2;
  134. auto test_parser = CreateParser(kBeamSize, parent_states, data);
  135. EXPECT_TRUE(test_parser->IsReady());
  136. EXPECT_TRUE(test_parser->IsTerminal());
  137. EXPECT_EQ(kBeamSize, test_parser->BeamSize());
  138. EXPECT_EQ(data.size(), test_parser->BatchSize());
  139. EXPECT_TRUE(test_parser->GetTraceProtos().empty());
  140. for (int batch_index = 0; batch_index < parent_states.size(); ++batch_index) {
  141. EXPECT_EQ(0, test_parser->StepsTaken(batch_index));
  142. const auto &beam = parent_states[batch_index];
  143. for (int beam_index = 0; beam_index < beam.size(); ++beam_index) {
  144. // Expect an identity mapping.
  145. EXPECT_EQ(beam_index,
  146. test_parser->GetSourceBeamIndex(beam_index, batch_index));
  147. }
  148. }
  149. const auto forwarded_states = test_parser->GetBeam();
  150. EXPECT_EQ(parent_states, forwarded_states);
  151. }
  152. } // namespace dragnn
  153. } // namespace syntaxnet