compute_session_impl.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #ifndef NLP_SAFT_OPENSOURCE_DRAGNN_CORE_COMPUTE_SESSION_IMPL_H_
  2. #define NLP_SAFT_OPENSOURCE_DRAGNN_CORE_COMPUTE_SESSION_IMPL_H_
  3. #include <memory>
  4. #include "dragnn/components/util/bulk_feature_extractor.h"
  5. #include "dragnn/core/compute_session.h"
  6. #include "dragnn/core/index_translator.h"
  7. #include "dragnn/core/input_batch_cache.h"
  8. #include "dragnn/protos/data.pb.h"
  9. #include "dragnn/protos/spec.pb.h"
  10. #include "dragnn/protos/trace.pb.h"
  11. namespace syntaxnet {
  12. namespace dragnn {
  13. class ComputeSessionImpl : public ComputeSession {
  14. public:
  15. // Creates a ComputeSessionImpl with the provided component builder function.
  16. ComputeSessionImpl(
  17. int id,
  18. std::function<std::unique_ptr<Component>(const string &component_name,
  19. const string &backend_type)>
  20. component_builder);
  21. void Init(const MasterSpec &master_spec,
  22. const GridPoint &hyperparams) override;
  23. void InitializeComponentData(const string &component_name,
  24. int max_beam_size) override;
  25. int BatchSize(const string &component_name) const override;
  26. int BeamSize(const string &component_name) const override;
  27. const ComponentSpec &Spec(const string &component_name) const override;
  28. int SourceComponentBeamSize(const string &component_name,
  29. int channel_id) override;
  30. void AdvanceFromOracle(const string &component_name) override;
  31. void AdvanceFromPrediction(const string &component_name,
  32. const float score_matrix[],
  33. int score_matrix_length) override;
  34. int GetInputFeatures(const string &component_name,
  35. std::function<int32 *(int)> allocate_indices,
  36. std::function<int64 *(int)> allocate_ids,
  37. std::function<float *(int)> allocate_weights,
  38. int channel_id) const override;
  39. int BulkGetInputFeatures(const string &component_name,
  40. const BulkFeatureExtractor &extractor) override;
  41. std::vector<LinkFeatures> GetTranslatedLinkFeatures(
  42. const string &component_name, int channel_id) override;
  43. std::vector<std::vector<int>> EmitOracleLabels(
  44. const string &component_name) override;
  45. bool IsTerminal(const string &component_name) override;
  46. void FinalizeData(const string &component_name) override;
  47. std::vector<string> GetSerializedPredictions() override;
  48. std::vector<MasterTrace> GetTraceProtos() override;
  49. void SetInputData(const std::vector<string> &data) override;
  50. void ResetSession() override;
  51. void SetTracing(bool tracing_on) override;
  52. int Id() const override;
  53. string GetDescription(const string &component_name) const override;
  54. const std::vector<const IndexTranslator *> Translators(
  55. const string &component_name) const override;
  56. private:
  57. // Get a given component. Fails if the component is not found.
  58. Component *GetComponent(const string &component_name) const;
  59. // Get a given component. CHECK-fail if the component's IsReady method
  60. // returns false.
  61. Component *GetReadiedComponent(const string &component_name) const;
  62. // Get the index translators for the given component.
  63. const std::vector<IndexTranslator *> &GetTranslators(
  64. const string &component_name) const;
  65. // Create an index translator.
  66. std::unique_ptr<IndexTranslator> CreateTranslator(
  67. const LinkedFeatureChannel &channel, Component *start_component);
  68. // Perform initialization on the given Component.
  69. void InitComponent(Component *component);
  70. // Holds all of the components owned by this ComputeSession, associated with
  71. // their names in the MasterSpec.
  72. std::map<string, std::unique_ptr<Component>> components_;
  73. // Holds a vector of translators for each component, indexed by the name
  74. // of the component they belong to.
  75. std::map<string, std::vector<IndexTranslator *>> translators_;
  76. // Holds ownership of all the IndexTranslators for this compute session.
  77. std::vector<std::unique_ptr<IndexTranslator>> owned_translators_;
  78. // The predecessor component for every component.
  79. // If a component is not in this map, it has no predecessor component and
  80. // will have its beam initialized without any data from other components.
  81. std::map<Component *, Component *> predecessors_;
  82. // Holds the current input data for this ComputeSession.
  83. std::unique_ptr<InputBatchCache> input_data_;
  84. // Function that, given a string, will return a Component.
  85. std::function<std::unique_ptr<Component>(const string &component_name,
  86. const string &backend_type)>
  87. component_builder_;
  88. // The master spec for this compute session.
  89. MasterSpec spec_;
  90. // The hyperparameters for this compute session.
  91. GridPoint grid_point_;
  92. // Unique identifier, assigned at construction.
  93. int id_;
  94. // Whether or not to perform tracing.
  95. bool do_tracing_ = false;
  96. };
  97. } // namespace dragnn
  98. } // namespace syntaxnet
  99. #endif // NLP_SAFT_OPENSOURCE_DRAGNN_CORE_COMPUTE_SESSION_IMPL_H_