Skip to content

Instantly share code, notes, and snippets.

View oborchers's full-sized avatar

Oliver Borchers oborchers

View GitHub Profile
from onnxruntime import InferenceSession, SessionOptions
from onnxruntime_customops import get_library_path
opt = rt.SessionOptions()
opt.register_custom_ops_library(get_library_path())
sess = rt.InferenceSession("universal-sentence-encoder-5.onnx", opt, providers=ONNX_PROVIDERS)
sess.run(
output_names=["outputs"],
#!/bin/bash
mkdir universal-sentence-encoder-5
cd universal-sentence-encoder-5
wget https://storage.googleapis.com/tfhub-modules/google/universal-sentence-encoder-large/5.tar.gz
tar -xvzf 5.tar.gz
rm 5.tar.gz
cd ..
python -m tf2onnx.convert --saved-model universal-sentence-encoder-5 --output universal-sentence-encoder-5.onnx --opset 12 --extra_opset ai.onnx.contrib:1 --tag serve
del model_args["dynamic_axes"]["output_0"] # Delete unused output
del model_args["dynamic_axes"]["output_1"] # Delete unused output
model_args["dynamic_axes"]["sentence_embedding"] = {0: "batch"}
model_args["output_names"] = ["sentence_embedding"]
class SentenceTransformer(transformers.BertModel):
def __init__(self, config):
super().__init__(config)
# Naming alias for ONNX output specification
# Makes it easier to identify the layer
self.sentence_embedding = torch.nn.Identity()
def forward(self, input_ids, token_type_ids, attention_mask):
# Get the token embeddings from the base model
token_embeddings = super().forward(
def print_transformers_shape_inference(name_or_path: str):
"""Prints the transformers shape inference for onnx."""
res = {}
model_pipeline = transformers.FeatureExtractionPipeline(
model=transformers.AutoModel.from_pretrained(name_or_path),
tokenizer=transformers.AutoTokenizer.from_pretrained(
name_or_path, use_fast=True
),
framework="pt",
# We start by working with CUDA only
ONNX_PROVIDERS = ["CUDAExecutionProvider", "CPUExecutionProvider"]
opt = rt.SessionOptions()
sess = rt.InferenceSession(str(model_pth), opt, providers=ONNX_PROVIDERS)
model_input = tokenizer.encode_plus(span)
model_input = {name : np.atleast_2d(value) for name, value in model_input.items()}
onnx_result = sess.run(None, model_input)
print(onnx_result[0].shape)
# Script to export a transformers model
model_name = "sentence-transformers/bert-base-nli-stsb-mean-tokens"
pipeline_name = "feature-extraction"
model_pth = Path(f"encoder/{model_name}.onnx")
nlp = transformers.pipeline(pipeline_name, model=model_name, tokenizer=model_name, device=0)
tokenizer = nlp.tokenizer
if model_pth.exists():
model_pth.unlink()
apt-get update
# Install ONNX ML
export ONNX_ML=1
pip install -U onnx
# Clone ONNX Runtime and build
git clone https://github.com/microsoft/onnxruntime.git --branch v1.7.1 --single-branch
/bin/sh onnxruntime/dockerfiles/scripts/install_common_deps.sh
cd onnxruntime/
TY - ICOMM
T1 - Your Labels and Data are Noisy? LASSO The Traitors!
A1 - Borchers, Oliver
A1 - Ringel, Daniel M.
Y1 - 2020///
JF - Towards Data Science
UR - https://medium.com/@oliverbor/lasso-the-traitors-dd33ea5942bc
N2 - This article develops the LASSO The Traitors (LTT) method. LTT filters out noisy observations from a dataset based on an exogenous performance metric. LTT significantly improves the performance of estimators based on the cleaned dataset. LTT is fast, easily applicable, and task agnostic.
ER -
@misc{Borchers2020,
abstract = {This article develops the LASSO The Traitors (LTT) method. LTT filters out noisy observations from a dataset based on an exogenous performance metric. LTT significantly improves the performance of estimators based on the cleaned dataset. LTT is fast, easily applicable, and task agnostic.},
author = {Borchers, Oliver and Ringel, Daniel M.},
booktitle = {Towards Data Science},
title = {{Your Labels and Data are Noisy? LASSO The Traitors!}},
url = {https://medium.com/@oliverbor/lasso-the-traitors-dd33ea5942bc},
year = {2020}
}