Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Add ONNX export pipeline backed by onnxmltools library, closes #124

davidmezzetti8cbbf8d76dd7
diff --git a/setup.py b/setup.py
index 9a85bd9d..01d31c93 100644
--- a/setup.py
+++ b/setup.py
@@ -25,6 +25,7 @@ extras["pipeline"] = [
"fasttext>=0.9.2",
"nltk>=3.5",
"onnx>=1.10.1",
+ "onnxmltools>=1.9.1",
"onnxruntime>=1.8.1",
"sentencepiece>=0.1.91",
"soundfile>=0.10.3.post1",
diff --git a/src/python/txtai/pipeline/__init__.py b/src/python/txtai/pipeline/__init__.py
index da922fa7..b298fa7c 100644
--- a/src/python/txtai/pipeline/__init__.py
+++ b/src/python/txtai/pipeline/__init__.py
@@ -10,6 +10,7 @@ from .hfonnx import HFOnnx
from .hfpipeline import HFPipeline
from .hftrainer import HFTrainer
from .labels import Labels
+from .mlonnx import MLOnnx
from .questions import Questions
from .segmentation import Segmentation
from .similarity import Similarity
diff --git a/src/python/txtai/pipeline/mlonnx.py b/src/python/txtai/pipeline/mlonnx.py
new file mode 100644
index 00000000..2268b78e
--- /dev/null
+++ b/src/python/txtai/pipeline/mlonnx.py
@@ -0,0 +1,57 @@
+"""
+Machine learning model to ONNX export module
+"""
+
+from .base import Pipeline
+
+try:
+ from onnxmltools import convert_sklearn
+ from onnxmltools.convert.common.data_types import StringTensorType
+
+ from skl2onnx.helpers.onnx_helper import save_onnx_model, select_model_inputs_outputs
+
+ ONNX_MLTOOLS = True
+except ImportError:
+ ONNX_MLTOOLS = False
+
+
+class MLOnnx(Pipeline):
+ """
+ Exports a machine learning model to ONNX using ONNXMLTools.
+ """
+
+ def __init__(self):
+ """
+ Creates a new MLOnnx pipeline.
+ """
+
+ if not ONNX_MLTOOLS:
+ raise ImportError('MLOnnx pipeline is not available - install "pipeline" extra to enable')
+
+ def __call__(self, model, output=None, opset=12):
+ """
+ Exports a machine learning model to ONNX using ONNXMLTools.
+
+ Args:
+ model: model to export
+ output: optional output model path, defaults to return byte array if None
+ opset: onnx opset, defaults to 12
+
+ Returns:
+ path to model output or model as bytes depending on output parameter
+ """
+
+ # Convert scikit-learn model to ONNX
+ model = convert_sklearn(model, "onnxmltools", initial_types=[("input_ids", StringTensorType([None, None]))], target_opset=opset)
+
+ # Prune model graph down to only output probabilities
+ model = select_model_inputs_outputs(model, outputs="probabilities")
+
+ # pylint: disable=E1101
+ # Rename output to logits for consistency with other models
+ model.graph.output[0].name = "logits"
+ model.graph.node[0].output[0] = "logits"
+
+ # Save model to specified output path or return bytes
+ model = save_onnx_model(model, output)
+ return output if output else model
diff --git a/test/python/testonnx.py b/test/python/testonnx.py
index 1426ca4d..c782785f 100644
--- a/test/python/testonnx.py
+++ b/test/python/testonnx.py
@@ -8,9 +8,13 @@ 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 OnnxModel
-from txtai.pipeline import HFOnnx, HFTrainer, Labels, Questions
+from txtai.pipeline import HFOnnx, HFTrainer, Labels, MLOnnx, Questions
class TestOnnx(unittest.TestCase):
@@ -119,6 +123,30 @@ class TestOnnx(unittest.TestCase):
questions = Questions((model, path))
self.assertEqual(questions(["What is the price?"], ["The price is $30"])[0], "$30")
+ def testScikit(self):
+ """
+ Test exporting a scikit-learn model to ONNX and running inference
+ """
+
+ # pylint: disable=W0613
+ def tokenizer(inputs, **kwargs):
+ if isinstance(inputs, str):
+ inputs = [inputs]
+
+ return {"input_ids": [[x] for x in inputs]}
+
+ # Train a scikit-learn model
+ model = Pipeline([("tfidf", TfidfVectorizer()), ("lr", LogisticRegression())])
+ model.fit([x["text"] for x in self.data], [x["label"] for x in self.data])
+
+ # Export model to ONNX
+ onnx = MLOnnx()
+ model = onnx(model)
+
+ # Test classification
+ labels = Labels((model, tokenizer), dynamic=False)
+ self.assertEqual(labels("cat")[0][0], 1)
+
def testZeroShot(self):
"""
Test exporting a zero shot classification model to ONNX and running inference
diff --git a/test/python/testoptional.py b/test/python/testoptional.py
index 28c18cd6..1cde1fd2 100644
--- a/test/python/testoptional.py
+++ b/test/python/testoptional.py
@@ -8,7 +8,7 @@ import txtai.ann.factory
from txtai.ann import ANNFactory
from txtai.models import OnnxModel
-from txtai.pipeline import HFOnnx, Segmentation, Textractor, Transcription, Translation
+from txtai.pipeline import HFOnnx, MLOnnx, Segmentation, Textractor, Transcription, Translation
from txtai.vectors import VectorsFactory
from txtai.workflow.task.image import ImageTask
from txtai.workflow.task.storage import StorageTask
@@ -31,6 +31,7 @@ class TestOptional(unittest.TestCase):
txtai.models.onnx.ONNX_RUNTIME = not txtai.models.onnx.ONNX_RUNTIME
txtai.pipeline.hfonnx.ONNX_RUNTIME = not txtai.pipeline.hfonnx.ONNX_RUNTIME
+ txtai.pipeline.mlonnx.ONNX_MLTOOLS = not txtai.pipeline.mlonnx.ONNX_MLTOOLS
txtai.pipeline.segmentation.NLTK = not txtai.pipeline.segmentation.NLTK
txtai.pipeline.textractor.TIKA = not txtai.pipeline.textractor.TIKA
txtai.pipeline.transcription.SOUNDFILE = not txtai.pipeline.transcription.SOUNDFILE
@@ -87,6 +88,9 @@ class TestOptional(unittest.TestCase):
with self.assertRaises(ImportError):
HFOnnx()("google/bert_uncased_L-2_H-128_A-2", quantize=True)
+ with self.assertRaises(ImportError):
+ MLOnnx()
+
with self.assertRaises(ImportError):
Segmentation()