💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Add changes/workarounds for Transformers 4.10, closes #110
davidmezzettifc76c9265959
diff --git a/src/python/txtai/models/onnx.py b/src/python/txtai/models/onnx.pyindex 4ecc230b..50453fff 100644--- a/src/python/txtai/models/onnx.py+++ b/src/python/txtai/models/onnx.py@@ -2,6 +2,8 @@ONNX module"""+from collections import namedtuple+# Conditional importtry:from onnxruntime import InferenceSession, SessionOptions@@ -40,7 +42,7 @@ class OnnxModel(PreTrainedModel):if not ONNX_RUNTIME:raise ImportError('onnxruntime is not available - install "model" extra to enable')- super().__init__(PretrainedConfig())+ super().__init__(OnnxConfig())# Create ONNX sessionself.model = InferenceSession(model, SessionOptions())@@ -48,13 +50,34 @@ class OnnxModel(PreTrainedModel):# Add references for this class to supported AutoModel classesname = self.__class__.__name__if name not in MODEL_MAPPING:- MODEL_MAPPING[name] = self.__class__- MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING[name] = self.__class__- MODEL_FOR_QUESTION_ANSWERING_MAPPING[name] = self.__class__+ for mapping in [MODEL_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING]:+ self.autoadd(mapping, name, self.__class__)# Add references for this class to support pipeline AutoTokenizersif type(self.config) not in TOKENIZER_MAPPING:- TOKENIZER_MAPPING[type(self.config)] = None+ self.autoadd(TOKENIZER_MAPPING, type(self.config), type(self.config).__name__)++ def autoadd(self, mapping, key, value):+ """+ Adds OnnxModel to auto model configuration to fully support pipelines.++ Args:+ mapping: auto model mapping+ key: key to add+ value: value to add+ """++ # pylint: disable=W0212+ if hasattr(mapping, "_config_mapping"):+ Params = namedtuple("Params", ["config", "model"])+ params = Params(key, value)++ mapping._modules[key] = params+ mapping._config_mapping[key] = "config"+ mapping._reverse_config_mapping[value] = key+ mapping._model_mapping[key] = "model"+ else:+ mapping[key] = valuedef forward(self, **inputs):"""@@ -102,3 +125,9 @@ class OnnxModel(PreTrainedModel):features[key] = np.asarray(value)return features+++class OnnxConfig(PretrainedConfig):+ """+ Configuration for ONNX models.+ """diff --git a/src/python/txtai/pipeline/labels.py b/src/python/txtai/pipeline/labels.pyindex bebc4cf0..866fff0a 100644--- a/src/python/txtai/pipeline/labels.py+++ b/src/python/txtai/pipeline/labels.py@@ -18,6 +18,9 @@ class Labels(HFPipeline):# Set if labels are dynamic (zero shot) or fixed (standard text classification)self.dynamic = dynamic+ # Save handle to pipeline tokenizer+ self.tokenizer = self.pipeline.tokenizer+def __call__(self, text, labels=None, multilabel=False):"""Applies a text classifier to text. Returns a list of (id, score) sorted by highest score,@@ -38,8 +41,14 @@ class Labels(HFPipeline):"""if self.dynamic:+ # Override tokenizer to set truncation parameter+ self.pipeline.tokenizer = self.tokenize+# Run zero shot classification pipelineresults = self.pipeline(text, labels, multi_label=multilabel, truncation=True)++ # Reset tokenizer+ self.pipeline.tokenizer = self.tokenizerelse:# Run text classification pipelineresults = self.textclassify(text, multilabel)@@ -94,3 +103,17 @@ class Labels(HFPipeline):"""return list(self.pipeline.model.config.id2label.values())++ def tokenize(self, **kwargs):+ """+ Tokenization method that forces truncation=True.++ Args:+ kwargs: arguments++ Returns:+ tokenized output+ """++ kwargs["truncation"] = True+ return self.tokenizer(**kwargs)