Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Fix issues with transformers 4.11.2, closes #118

davidmezzetti705910cc0f91
diff --git a/setup.py b/setup.py
index 9abb3680..cd5e9652 100644
--- a/setup.py
+++ b/setup.py
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as f:
DESCRIPTION = f.read()
# Required dependencies
-install = ["faiss-cpu>=1.7.1.post2", "numpy>=1.18.4", "torch>=1.6.0", "transformers>=4.10.2"]
+install = ["faiss-cpu>=1.7.1.post2", "numpy>=1.18.4", "torch>=1.6.0", "transformers>=4.11.2"]
# Optional dependencies
extras = {}
diff --git a/src/python/txtai/models/onnx.py b/src/python/txtai/models/onnx.py
index dd76a650..71ce1804 100644
--- a/src/python/txtai/models/onnx.py
+++ b/src/python/txtai/models/onnx.py
@@ -22,6 +22,7 @@ from transformers.models.auto.modeling_auto import (
MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
)
from transformers.models.auto.tokenization_auto import TOKENIZER_MAPPING
+from transformers.modeling_outputs import SequenceClassifierOutput
from transformers.modeling_utils import PreTrainedModel
# pylint: disable=W0223
@@ -94,6 +95,10 @@ class OnnxModel(PreTrainedModel):
results = self.model.run(None, inputs)
# pylint: disable=E1101
+ # Detect if logits is an output and return classifier output in that case
+ if any([x.name for x in self.model.get_outputs() if x.name == "logits"]):
+ return SequenceClassifierOutput(logits=torch.from_numpy(np.array(results[0])))
+
return torch.from_numpy(np.array(results))
def parse(self, inputs):
diff --git a/src/python/txtai/pipeline/labels.py b/src/python/txtai/pipeline/labels.py
index 71214059..253acd34 100644
--- a/src/python/txtai/pipeline/labels.py
+++ b/src/python/txtai/pipeline/labels.py
@@ -2,8 +2,6 @@
Labels module
"""
-import numpy as np
-
from .hfpipeline import HFPipeline
@@ -40,13 +38,19 @@ class Labels(HFPipeline):
if self.dynamic:
# Run zero shot classification pipeline
results = self.pipeline(text, labels, multi_label=multilabel, truncation=True)
+
+ # Convert results to a list if necessary
+ if not isinstance(results, list):
+ results = [results]
+
+ # Unpack nested lists if necessary
+ results = [result[0] if isinstance(result, list) else result for result in results]
else:
- # Run text classification pipeline
- results = self.textclassify(text, multilabel)
+ # Set classification function based on inputs
+ function = "sigmoid" if multilabel or len(self.labels()) == 1 else "softmax"
- # Convert results to a list if necessary
- if not isinstance(results, list):
- results = [results]
+ # Run text classification pipeline
+ results = self.pipeline(text, return_all_scores=True, function_to_apply=function)
# Build list of (id, score)
scores = []
@@ -61,33 +65,6 @@ class Labels(HFPipeline):
return scores[0] if isinstance(text, str) else scores
- def textclassify(self, text, multilabel):
- """
- Wrapper of the text-classification pipeline to be more flexible with the logits transformation
- function. While these methods are in later versions of Hugging Face Transformers, this method supports
- older versions.
-
- Args:
- text: text|list
- multilabel: labels are independent if True, otherwise scores are normalized to sum to 1 per text item
-
- Returns:
- list of scores for each label in model
- """
-
- # pylint: disable=W0212
- inputs = self.pipeline._parse_and_tokenize(text, truncation=True)
- outputs = self.pipeline._forward(inputs)
-
- # Apply sigmoid to outputs
- if multilabel or len(self.labels()) == 1:
- return (1.0 / (1.0 + np.exp(-outputs))).tolist()
-
- # Apply softmax to outputs
- maxes = np.max(outputs, axis=-1, keepdims=True)
- shift = np.exp(outputs - maxes)
- return (shift / shift.sum(axis=-1, keepdims=True)).tolist()
-
def labels(self):
"""
Returns a list of all text classification model labels sorted in index order.
@@ -110,6 +87,9 @@ class Labels(HFPipeline):
filtered results
"""
+ # Extract scores from result
+ result = [x["score"] for x in result]
+
if labels:
config = self.pipeline.model.config
indices = []
diff --git a/test/python/testonnx.py b/test/python/testonnx.py
index 62a29f2a..1426ca4d 100644
--- a/test/python/testonnx.py
+++ b/test/python/testonnx.py
@@ -121,7 +121,7 @@ class TestOnnx(unittest.TestCase):
def testZeroShot(self):
"""
- Test exporing a zero shot classification model to ONNX and running inference
+ Test exporting a zero shot classification model to ONNX and running inference
"""
path = "prajjwal1/bert-medium-mnli"