|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "../core/types.h" |
| 4 | +#include "../core/tokenizer.h" |
| 5 | +#include "lora_adapter.h" |
| 6 | +#include <string> |
| 7 | +#include <memory> |
| 8 | +#include <mutex> |
| 9 | +#include <unordered_map> |
| 10 | + |
| 11 | +namespace openvino_sr { |
| 12 | +namespace classifiers { |
| 13 | + |
| 14 | +/** |
| 15 | + * @brief Task types for LoRA multi-task classification |
| 16 | + */ |
| 17 | +enum class TaskType { |
| 18 | + Intent, |
| 19 | + PII, |
| 20 | + Security, |
| 21 | + Classification |
| 22 | +}; |
| 23 | + |
| 24 | +/** |
| 25 | + * @brief Token-level prediction for token classification models |
| 26 | + */ |
| 27 | +struct TokenPrediction { |
| 28 | + std::string token; // The token text |
| 29 | + int class_id; // Predicted class ID |
| 30 | + float confidence; // Confidence score (0.0 to 1.0) |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * @brief Detected entity from BIO tagging |
| 35 | + */ |
| 36 | +struct DetectedEntity { |
| 37 | + std::string type; // Entity type (e.g., "EMAIL_ADDRESS", "PERSON") |
| 38 | + std::string text; // The detected entity text |
| 39 | + int start_token; // Start token index |
| 40 | + int end_token; // End token index (inclusive) |
| 41 | + float confidence; // Average confidence of tokens in entity |
| 42 | +}; |
| 43 | + |
| 44 | +/** |
| 45 | + * @brief Token classification result |
| 46 | + */ |
| 47 | +struct TokenClassificationResult { |
| 48 | + std::vector<TokenPrediction> token_predictions; // Per-token predictions |
| 49 | + std::vector<DetectedEntity> entities; // Detected entities (aggregated from BIO tags) |
| 50 | + float processing_time_ms; // Processing time in milliseconds |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * @brief LoRA-enabled classifier for BERT and ModernBERT |
| 55 | + * |
| 56 | + * Supports multi-task classification with parameter-efficient LoRA adapters. |
| 57 | + * Each task has its own LoRA adapter and classification head. |
| 58 | + */ |
| 59 | +class LoRAClassifier { |
| 60 | +public: |
| 61 | + LoRAClassifier() = default; |
| 62 | + |
| 63 | + /** |
| 64 | + * @brief Initialize LoRA classifier with base model and adapters |
| 65 | + * @param base_model_path Path to base BERT/ModernBERT model (.xml file) |
| 66 | + * @param lora_adapters_path Path to directory containing LoRA adapter models |
| 67 | + * @param task_configs Map of task types to number of classes |
| 68 | + * @param device Device name ("CPU", "GPU", etc.) |
| 69 | + * @param model_type "bert" or "modernbert" |
| 70 | + * @return true if successful |
| 71 | + */ |
| 72 | + bool initialize( |
| 73 | + const std::string& base_model_path, |
| 74 | + const std::string& lora_adapters_path, |
| 75 | + const std::unordered_map<TaskType, int>& task_configs, |
| 76 | + const std::string& device = "CPU", |
| 77 | + const std::string& model_type = "bert" |
| 78 | + ); |
| 79 | + |
| 80 | + /** |
| 81 | + * @brief Classify text for a specific task (sequence classification) |
| 82 | + * @param text Input text |
| 83 | + * @param task Task type |
| 84 | + * @return Classification result |
| 85 | + */ |
| 86 | + core::ClassificationResult classifyTask(const std::string& text, TaskType task); |
| 87 | + |
| 88 | + /** |
| 89 | + * @brief Classify tokens for token-level classification (e.g., NER, PII detection) |
| 90 | + * @param text Input text |
| 91 | + * @param task Task type (should be PII or similar token classification task) |
| 92 | + * @return Token classification result with per-token predictions and detected entities |
| 93 | + */ |
| 94 | + TokenClassificationResult classifyTokens(const std::string& text, TaskType task); |
| 95 | + |
| 96 | + /** |
| 97 | + * @brief Check if initialized |
| 98 | + */ |
| 99 | + bool isInitialized() const { |
| 100 | + return base_model_ && base_model_->compiled_model != nullptr; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @brief Get supported tasks |
| 105 | + */ |
| 106 | + std::vector<TaskType> getSupportedTasks() const; |
| 107 | + |
| 108 | +private: |
| 109 | + /** |
| 110 | + * @brief Get pooled output from base model |
| 111 | + */ |
| 112 | + ov::Tensor getPooledOutput(const std::string& text); |
| 113 | + |
| 114 | + /** |
| 115 | + * @brief Apply task-specific LoRA adapter and classification head |
| 116 | + */ |
| 117 | + core::ClassificationResult applyLoRAAndClassify( |
| 118 | + const ov::Tensor& pooled_output, |
| 119 | + TaskType task |
| 120 | + ); |
| 121 | + |
| 122 | + /** |
| 123 | + * @brief Load task-specific LoRA adapter and classification head |
| 124 | + */ |
| 125 | + bool loadTaskAdapter( |
| 126 | + const std::string& lora_adapters_path, |
| 127 | + TaskType task, |
| 128 | + int num_classes, |
| 129 | + const std::string& device |
| 130 | + ); |
| 131 | + |
| 132 | + /** |
| 133 | + * @brief Get task name as string |
| 134 | + */ |
| 135 | + std::string getTaskName(TaskType task) const; |
| 136 | + |
| 137 | + /** |
| 138 | + * @brief Get maximum sequence length for the model type |
| 139 | + * @return Max sequence length (8192 for ModernBERT, 512 for BERT) |
| 140 | + */ |
| 141 | + int getMaxSequenceLength() const; |
| 142 | + |
| 143 | + /** |
| 144 | + * @brief Aggregate BIO tags into detected entities |
| 145 | + * @param original_text The original input text |
| 146 | + * @param tokens Vector of token strings |
| 147 | + * @param predictions Vector of token predictions |
| 148 | + * @param labels Map of class IDs to label names |
| 149 | + * @return Vector of detected entities |
| 150 | + */ |
| 151 | + std::vector<DetectedEntity> aggregateBIOTags( |
| 152 | + const std::string& original_text, |
| 153 | + const std::vector<std::string>& tokens, |
| 154 | + const std::vector<TokenPrediction>& predictions, |
| 155 | + const std::unordered_map<int, std::string>& labels |
| 156 | + ) const; |
| 157 | + |
| 158 | + /** |
| 159 | + * @brief Load label mapping from JSON file |
| 160 | + * @param adapters_path Path to adapters directory containing label_mapping.json |
| 161 | + * @return Map of class IDs to label names |
| 162 | + */ |
| 163 | + std::unordered_map<int, std::string> loadLabelMapping(const std::string& adapters_path) const; |
| 164 | + |
| 165 | + std::shared_ptr<core::ModelInstance> base_model_; // Frozen base model |
| 166 | + std::unordered_map<TaskType, LoRAAdapter> lora_adapters_; // Task-specific LoRA adapters |
| 167 | + std::unordered_map<TaskType, std::shared_ptr<ov::CompiledModel>> task_heads_; // Classification heads |
| 168 | + std::unordered_map<TaskType, int> task_num_classes_; // Number of classes per task |
| 169 | + std::string adapters_path_; // Path to adapters directory |
| 170 | + core::OVNativeTokenizer tokenizer_; |
| 171 | + std::mutex mutex_; |
| 172 | + std::string model_type_; // "bert" or "modernbert" |
| 173 | +}; |
| 174 | + |
| 175 | +} // namespace classifiers |
| 176 | +} // namespace openvino_sr |
| 177 | + |
0 commit comments