entity_annotator_native_library.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_OPTIMIZATION_GUIDE_CORE_ENTITY_ANNOTATOR_NATIVE_LIBRARY_H_
  5. #define COMPONENTS_OPTIMIZATION_GUIDE_CORE_ENTITY_ANNOTATOR_NATIVE_LIBRARY_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/native_library.h"
  9. #include "components/optimization_guide/core/entity_metadata.h"
  10. #include "components/optimization_guide/core/model_info.h"
  11. namespace optimization_guide {
  12. // Enumerates the statuses possible when creating an entity annotator.
  13. //
  14. // Keep this in sync with
  15. // OptimizationGuidePageEntitiesModelExecutorCreationStatus in enums.xml.
  16. enum class EntityAnnotatorCreationStatus {
  17. kUnknown = 0,
  18. // The entity annotator was created successfully.
  19. kSuccess = 1,
  20. // The native library was loaded but invalid. Should not happen in the real
  21. // world.
  22. kLibraryInvalid = 2,
  23. // The entity annotator was requested to be created but no metadata for how to
  24. // create it was present.
  25. kMissingModelMetadata = 3,
  26. // The entity annotator was requested to be created but the metadata specific
  27. // to this model was not present.
  28. kMissingEntitiesModelMetadata = 4,
  29. // The entity annotator was requested to be created but no slices were
  30. // specified in the model metadata.
  31. kMissingEntitiesModelMetadataSliceSpecification = 5,
  32. // Expected files are missing.
  33. kMissingAdditionalEntitiesModelMetadataPath = 6,
  34. kMissingAdditionalWordEmbeddingsPath = 7,
  35. kMissingAdditionalNameFilterPath = 8,
  36. kMissingAdditionalNameTablePath = 9,
  37. kMissingAdditionalPrefixFilterPath = 10,
  38. kMissingAdditionalMetadataTablePath = 11,
  39. // All required files were present, but the creation failed for a different
  40. // reason.
  41. kInitializationFailure = 12,
  42. // New values go above here.
  43. kMaxValue = kInitializationFailure,
  44. };
  45. // Handles interactions with the native library that contains logic for the
  46. // entity annotator.
  47. class EntityAnnotatorNativeLibrary {
  48. public:
  49. // Creates an EntityAnnotatorNativeLibrary, which loads a native library and
  50. // relevant functions required. Will return nullptr if fails.
  51. // |should_provide_filter_path| dictates whether the filters used to optimize
  52. // the annotation should be provided.
  53. static std::unique_ptr<EntityAnnotatorNativeLibrary> Create(
  54. bool should_provide_filter_path);
  55. EntityAnnotatorNativeLibrary(const EntityAnnotatorNativeLibrary&) = delete;
  56. EntityAnnotatorNativeLibrary& operator=(const EntityAnnotatorNativeLibrary&) =
  57. delete;
  58. ~EntityAnnotatorNativeLibrary();
  59. // Returns whether this instance is valid (i.e. all necessary functions have
  60. // been loaded.)
  61. bool IsValid() const;
  62. // Gets the max supported feature from this native library.
  63. int32_t GetMaxSupportedFeatureFlag();
  64. // Creates an entity annotator from |model_info|.
  65. void* CreateEntityAnnotator(const ModelInfo& model_info);
  66. // Deletes |entity_annotator|.
  67. void DeleteEntityAnnotator(void* entity_annotator);
  68. // Uses |annotator| to annotate entities present in |text|.
  69. absl::optional<std::vector<ScoredEntityMetadata>> AnnotateText(
  70. void* annotator,
  71. const std::string& text);
  72. // Returns entity metadata from |annotator| for |entity_id|.
  73. absl::optional<EntityMetadata> GetEntityMetadataForEntityId(
  74. void* annotator,
  75. const std::string& entity_id);
  76. private:
  77. EntityAnnotatorNativeLibrary(base::NativeLibrary native_library,
  78. bool should_provide_filter_path);
  79. // Loads the functions exposed by the native library.
  80. void LoadFunctions();
  81. // Populates |options| based on |model_info|. Returns false if |model_info|
  82. // cannot construct a valid options object. Populates |status| with the
  83. // correct failure reason if a valid options object could not be constructed.
  84. bool PopulateEntityAnnotatorOptionsFromModelInfo(
  85. void* options,
  86. const ModelInfo& model_info,
  87. EntityAnnotatorCreationStatus* status);
  88. // Returns an entity metadata from the C-API representation.
  89. EntityMetadata GetEntityMetadataFromOptimizationGuideEntityMetadata(
  90. const void* og_entity_metadata);
  91. base::NativeLibrary native_library_;
  92. const bool should_provide_filter_path_ = true;
  93. // Functions exposed by native library.
  94. using GetMaxSupportedFeatureFlagFunc = int32_t (*)();
  95. GetMaxSupportedFeatureFlagFunc get_max_supported_feature_flag_func_ = nullptr;
  96. using CreateFromOptionsFunc = void* (*)(const void*);
  97. CreateFromOptionsFunc create_from_options_func_ = nullptr;
  98. using GetCreationErrorFunc = const char* (*)(const void*);
  99. GetCreationErrorFunc get_creation_error_func_ = nullptr;
  100. using DeleteFunc = void (*)(void*);
  101. DeleteFunc delete_func_ = nullptr;
  102. using AnnotateJobCreateFunc = void* (*)(void*);
  103. AnnotateJobCreateFunc annotate_job_create_func_ = nullptr;
  104. using AnnotateJobDeleteFunc = void (*)(void*);
  105. AnnotateJobDeleteFunc annotate_job_delete_func_ = nullptr;
  106. using RunAnnotateJobFunc = int32_t (*)(void*, const char*);
  107. RunAnnotateJobFunc run_annotate_job_func_ = nullptr;
  108. using AnnotateGetOutputMetadataAtIndexFunc = const void* (*)(void*, int32_t);
  109. AnnotateGetOutputMetadataAtIndexFunc
  110. annotate_get_output_metadata_at_index_func_ = nullptr;
  111. using AnnotateGetOutputMetadataScoreAtIndexFunc = float (*)(void*, int32_t);
  112. AnnotateGetOutputMetadataScoreAtIndexFunc
  113. annotate_get_output_metadata_score_at_index_func_ = nullptr;
  114. using EntityMetadataJobCreateFunc = void* (*)(void*);
  115. EntityMetadataJobCreateFunc entity_metadata_job_create_func_ = nullptr;
  116. using EntityMetadataJobDeleteFunc = void (*)(void*);
  117. EntityMetadataJobDeleteFunc entity_metadata_job_delete_func_ = nullptr;
  118. using RunEntityMetadataJobFunc = const void* (*)(void*, const char*);
  119. RunEntityMetadataJobFunc run_entity_metadata_job_func_ = nullptr;
  120. using OptionsCreateFunc = void* (*)();
  121. OptionsCreateFunc options_create_func_ = nullptr;
  122. using OptionsSetModelFilePathFunc = void (*)(void*, const char*);
  123. OptionsSetModelFilePathFunc options_set_model_file_path_func_ = nullptr;
  124. using OptionsSetModelMetadataFilePathFunc = void (*)(void*, const char*);
  125. OptionsSetModelMetadataFilePathFunc
  126. options_set_model_metadata_file_path_func_ = nullptr;
  127. using OptionsSetWordEmbeddingsFilePathFunc = void (*)(void*, const char*);
  128. OptionsSetWordEmbeddingsFilePathFunc
  129. options_set_word_embeddings_file_path_func_ = nullptr;
  130. using OptionsAddModelSliceFunc = void (*)(void*,
  131. const char*,
  132. const char*,
  133. const char*,
  134. const char*,
  135. const char*);
  136. OptionsAddModelSliceFunc options_add_model_slice_func_ = nullptr;
  137. using OptionsDeleteFunc = void (*)(void*);
  138. OptionsDeleteFunc options_delete_func_ = nullptr;
  139. using EntityMetadataGetEntityIdFunc = const char* (*)(const void*);
  140. EntityMetadataGetEntityIdFunc entity_metadata_get_entity_id_func_ = nullptr;
  141. using EntityMetadataGetHumanReadableNameFunc = const char* (*)(const void*);
  142. EntityMetadataGetHumanReadableNameFunc
  143. entity_metadata_get_human_readable_name_func_ = nullptr;
  144. using EntityMetadataGetHumanReadableCategoriesCountFunc =
  145. int32_t (*)(const void*);
  146. EntityMetadataGetHumanReadableCategoriesCountFunc
  147. entity_metadata_get_human_readable_categories_count_func_ = nullptr;
  148. using EntityMetadataGetHumanReadableCategoryNameAtIndexFunc =
  149. const char* (*)(const void*, int32_t);
  150. EntityMetadataGetHumanReadableCategoryNameAtIndexFunc
  151. entity_metadata_get_human_readable_category_name_at_index_func_ = nullptr;
  152. using EntityMetadataGetHumanReadableCategoryScoreAtIndexFunc =
  153. float (*)(const void*, int32_t);
  154. EntityMetadataGetHumanReadableCategoryScoreAtIndexFunc
  155. entity_metadata_get_human_readable_category_score_at_index_func_ =
  156. nullptr;
  157. using EntityMetadataGetHumanReadableAliasesCountFunc =
  158. int32_t (*)(const void*);
  159. EntityMetadataGetHumanReadableAliasesCountFunc
  160. entity_metadata_get_human_readable_aliases_count_func_ = nullptr;
  161. using EntityMetadataGetHumanReadableAliasAtIndexFunc =
  162. const char* (*)(const void*, int32_t);
  163. EntityMetadataGetHumanReadableAliasAtIndexFunc
  164. entity_metadata_get_human_readable_alias_at_index_func_ = nullptr;
  165. using EntityMetadataGetCollectionsCountFunc = int32_t (*)(const void*);
  166. EntityMetadataGetCollectionsCountFunc
  167. entity_metadata_get_collections_count_func_ = nullptr;
  168. using EntityMetadataGetCollectionAtIndexFunc = const char* (*)(const void*,
  169. int32_t);
  170. EntityMetadataGetCollectionAtIndexFunc
  171. entity_metadata_get_collection_at_index_func_ = nullptr;
  172. };
  173. } // namespace optimization_guide
  174. #endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_ENTITY_ANNOTATOR_NATIVE_LIBRARY_H_