component.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #ifndef NLP_SAFT_OPENSOURCE_DRAGNN_CORE_INTERFACES_COMPONENT_H_
  16. #define NLP_SAFT_OPENSOURCE_DRAGNN_CORE_INTERFACES_COMPONENT_H_
  17. #include <vector>
  18. #include "dragnn/components/util/bulk_feature_extractor.h"
  19. #include "dragnn/core/input_batch_cache.h"
  20. #include "dragnn/core/interfaces/transition_state.h"
  21. #include "dragnn/protos/spec.pb.h"
  22. #include "dragnn/protos/trace.pb.h"
  23. #include "syntaxnet/registry.h"
  24. namespace syntaxnet {
  25. namespace dragnn {
  26. class Component : public RegisterableClass<Component> {
  27. public:
  28. virtual ~Component() {}
  29. // Initializes this component from the spec.
  30. virtual void InitializeComponent(const ComponentSpec &spec) = 0;
  31. // Provides the previous beam to the component.
  32. virtual void InitializeData(
  33. const std::vector<std::vector<const TransitionState *>> &states,
  34. int max_beam_size, InputBatchCache *input_data) = 0;
  35. // Returns true if the component has had InitializeData called on it since
  36. // the last time it was reset.
  37. virtual bool IsReady() const = 0;
  38. // Initializes the component for tracing execution, resetting any existing
  39. // traces. This will typically have the side effect of slowing down all
  40. // subsequent Component calculations and storing a trace in memory that can be
  41. // returned by GetTraceProtos().
  42. virtual void InitializeTracing() = 0;
  43. // Disables tracing, freeing any associated traces and avoiding triggering
  44. // additional computation in the future.
  45. virtual void DisableTracing() = 0;
  46. // Returns the string name of this component.
  47. virtual string Name() const = 0;
  48. // Returns the current batch size of the component's underlying data.
  49. virtual int BatchSize() const = 0;
  50. // Returns the maximum beam size of this component.
  51. virtual int BeamSize() const = 0;
  52. // Returns the number of steps taken by this component so far.
  53. virtual int StepsTaken(int batch_index) const = 0;
  54. // Return the beam index of the item which is currently at index
  55. // 'index', when the beam was at step 'step', for batch element 'batch'.
  56. virtual int GetBeamIndexAtStep(int step, int current_index,
  57. int batch) const = 0;
  58. // Return the source index of the item which is currently at index 'index'
  59. // for batch element 'batch'. This index is into the final beam of the
  60. // Component that this Component was initialized from.
  61. virtual int GetSourceBeamIndex(int current_index, int batch) const = 0;
  62. // Request a translation function based on the given method string.
  63. // The translation function will be called with arguments (beam, batch, value)
  64. // and should return the step index corresponding to the given value, for the
  65. // data in the given beam and batch.
  66. virtual std::function<int(int, int, int)> GetStepLookupFunction(
  67. const string &method) = 0;
  68. // Advances this component from the given transition matrix.
  69. virtual void AdvanceFromPrediction(const float transition_matrix[],
  70. int transition_matrix_length) = 0;
  71. // Advances this component from the state oracles.
  72. virtual void AdvanceFromOracle() = 0;
  73. // Returns true if all states within this component are terminal.
  74. virtual bool IsTerminal() const = 0;
  75. // Returns the current batch of beams for this component.
  76. virtual std::vector<std::vector<const TransitionState *>> GetBeam() = 0;
  77. // Extracts and populates the vector of FixedFeatures for the specified
  78. // channel. Each functor allocates storage space for the indices, the IDs, and
  79. // the weights (respectively).
  80. virtual int GetFixedFeatures(
  81. std::function<int32 *(int num_elements)> allocate_indices,
  82. std::function<int64 *(int num_elements)> allocate_ids,
  83. std::function<float *(int num_elements)> allocate_weights,
  84. int channel_id) const = 0;
  85. // Extracts and populates all FixedFeatures for all channels, advancing this
  86. // component via the oracle until it is terminal. This call uses a
  87. // BulkFeatureExtractor object to contain the functors and other information.
  88. virtual int BulkGetFixedFeatures(const BulkFeatureExtractor &extractor) = 0;
  89. // Extracts and returns the vector of LinkFeatures for the specified
  90. // channel. Note: these are NOT translated.
  91. virtual std::vector<LinkFeatures> GetRawLinkFeatures(
  92. int channel_id) const = 0;
  93. // Returns a vector of oracle labels for each element in the beam and
  94. // batch.
  95. virtual std::vector<std::vector<int>> GetOracleLabels() const = 0;
  96. // Annotate the underlying data object with the results of this Component's
  97. // calculation.
  98. virtual void FinalizeData() = 0;
  99. // Reset this component.
  100. virtual void ResetComponent() = 0;
  101. // Get a vector of all traces managed by this component.
  102. virtual std::vector<std::vector<ComponentTrace>> GetTraceProtos() const = 0;
  103. // Add the translated link features (done outside the component) to the traces
  104. // managed by this component.
  105. virtual void AddTranslatedLinkFeaturesToTrace(
  106. const std::vector<LinkFeatures> &features, int channel_id) = 0;
  107. };
  108. } // namespace dragnn
  109. } // namespace syntaxnet
  110. #endif // NLP_SAFT_OPENSOURCE_DRAGNN_CORE_INTERFACES_COMPONENT_H_