Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Use new transformers auto model register methods, closes #128

davidmezzetti7cd41244b15a
diff --git a/setup.py b/setup.py
index af62fd71..36d4e7c6 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.11.3"]
+install = ["faiss-cpu>=1.7.1.post2", "numpy>=1.18.4", "torch>=1.6.0", "transformers>=4.12.3"]
# Optional dependencies
extras = {}
diff --git a/src/python/txtai/models/registry.py b/src/python/txtai/models/registry.py
index 490edb2f..2ffe021d 100644
--- a/src/python/txtai/models/registry.py
+++ b/src/python/txtai/models/registry.py
@@ -2,13 +2,7 @@
Registry module
"""
-from collections import namedtuple
-
-from transformers.models.auto.modeling_auto import (
- MODEL_MAPPING,
- MODEL_FOR_QUESTION_ANSWERING_MAPPING,
- MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
-)
+from transformers import AutoModel, AutoModelForQuestionAnswering, AutoModelForSequenceClassification
from transformers.models.auto.tokenization_auto import TOKENIZER_MAPPING
@@ -18,40 +12,28 @@ class Registry:
"""
@staticmethod
- def register(model):
+ def register(model, config=None):
"""
Registers a model with auto model and tokenizer configuration to fully support pipelines.
Args:
model: model to register
+ config: config class name
"""
- # Add references for this class to supported AutoModel classes
+ # Default config class name to model name if not provided
name = model.__class__.__name__
- if name not in MODEL_MAPPING:
- for mapping in [MODEL_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING]:
- Registry.autoadd(mapping, name, model.__class__)
+ if not config:
+ config = name
- # Add references for this class to support pipeline AutoTokenizers
- if hasattr(model, "config") and type(model.config) not in TOKENIZER_MAPPING:
- Registry.autoadd(TOKENIZER_MAPPING, type(model.config), type(model.config).__name__)
-
- @staticmethod
- def autoadd(mapping, key, value):
- """
- Adds auto model configuration to fully support pipelines.
+ # Default model config_class if empty
+ if hasattr(model.__class__, "config_class") and not model.__class__.config_class:
+ model.__class__.config_class = config
- Args:
- mapping: auto model mapping
- key: key to add
- value: value to add
- """
-
- # pylint: disable=W0212
- Params = namedtuple("Params", ["config", "model"])
- params = Params(key, value)
+ # Add references for this class to supported AutoModel classes
+ for mapping in [AutoModel, AutoModelForQuestionAnswering, AutoModelForSequenceClassification]:
+ mapping.register(config, model.__class__)
- mapping._modules[key] = params
- mapping._config_mapping[key] = "config"
- mapping._reverse_config_mapping[value] = key
- mapping._model_mapping[key] = "model"
+ # Add references for this class to support pipeline AutoTokenizers
+ if hasattr(model, "config") and type(model.config) not in TOKENIZER_MAPPING:
+ TOKENIZER_MAPPING.register(type(model.config), type(model.config).__name__)
diff --git a/test/python/testonnx.py b/test/python/testonnx.py
index ebd75117..bf5c5a9c 100644
--- a/test/python/testonnx.py
+++ b/test/python/testonnx.py
@@ -6,14 +6,11 @@ import os
import tempfile
import unittest
-from collections import OrderedDict
-
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from txtai.embeddings import Embeddings
-from txtai.models import Registry
from txtai.pipeline import HFOnnx, HFTrainer, Labels, MLOnnx, Questions
@@ -30,34 +27,6 @@ class TestOnnx(unittest.TestCase):
cls.data = [{"text": "Dogs", "label": 0}, {"text": "dog", "label": 0}, {"text": "Cats", "label": 1}, {"text": "cat", "label": 1}] * 100
- def testAutoAdd(self):
- """
- Tests methods for adding ONNX models to auto classes
- """
-
- class Mapping(OrderedDict):
- """
- Helper class to support autoadd tests.
- """
-
- def __init__(self):
- super().__init__()
-
- self._config_mapping = {}
- self._reverse_config_mapping = {}
- self._model_mapping = {}
- self._modules = {}
-
- # Test lazy auto mapping
- mapping = Mapping()
- Registry.autoadd(mapping, "key", "value")
-
- # pylint: disable=W0212
- self.assertTrue("key" in mapping._config_mapping)
- self.assertTrue("value" in mapping._reverse_config_mapping)
- self.assertTrue("key" in mapping._model_mapping)
- self.assertTrue("key" in mapping._modules)
-
def testDefault(self):
"""
Test exporting an ONNX model with default parameters