Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Create model registry, closes #126

davidmezzetti3dfcb522caf6
diff --git a/src/python/txtai/models/__init__.py b/src/python/txtai/models/__init__.py
index 9d73f17c..cc099292 100644
--- a/src/python/txtai/models/__init__.py
+++ b/src/python/txtai/models/__init__.py
@@ -5,3 +5,4 @@ Models imports
from .models import Models
from .onnx import OnnxModel
from .pooling import MeanPooling, Pooling
+from .registry import Registry
diff --git a/src/python/txtai/models/onnx.py b/src/python/txtai/models/onnx.py
index 57d81e86..ef2d1b94 100644
--- a/src/python/txtai/models/onnx.py
+++ b/src/python/txtai/models/onnx.py
@@ -2,8 +2,6 @@
ONNX module
"""
-from collections import namedtuple
-
# Conditional import
try:
from onnxruntime import InferenceSession, SessionOptions
@@ -17,15 +15,11 @@ import torch
from transformers import AutoConfig
from transformers.configuration_utils import PretrainedConfig
-from transformers.models.auto.modeling_auto import (
- MODEL_MAPPING,
- MODEL_FOR_QUESTION_ANSWERING_MAPPING,
- 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
+from .registry import Registry
+
# pylint: disable=W0223
class OnnxModel(PreTrainedModel):
"""
@@ -51,33 +45,7 @@ class OnnxModel(PreTrainedModel):
self.model = InferenceSession(model, SessionOptions())
# Add references for this class to supported AutoModel classes
- name = self.__class__.__name__
- if name not in MODEL_MAPPING:
- 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 AutoTokenizers
- if type(self.config) not in TOKENIZER_MAPPING:
- 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
- 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"
+ Registry.register(self)
def forward(self, **inputs):
"""
diff --git a/src/python/txtai/models/registry.py b/src/python/txtai/models/registry.py
new file mode 100644
index 00000000..490edb2f
--- /dev/null
+++ b/src/python/txtai/models/registry.py
@@ -0,0 +1,57 @@
+"""
+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.models.auto.tokenization_auto import TOKENIZER_MAPPING
+
+
+class Registry:
+ """
+ Methods to register models and fully support pipelines.
+ """
+
+ @staticmethod
+ def register(model):
+ """
+ Registers a model with auto model and tokenizer configuration to fully support pipelines.
+
+ Args:
+ model: model to register
+ """
+
+ # Add references for this class to supported AutoModel classes
+ 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__)
+
+ # 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.
+
+ 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)
+
+ mapping._modules[key] = params
+ mapping._config_mapping[key] = "config"
+ mapping._reverse_config_mapping[value] = key
+ mapping._model_mapping[key] = "model"