Skip to content

Commit

Permalink
Added ONNX docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Thilina Rajapakse committed Sep 24, 2020
1 parent 3e5076d commit 2786bf9
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 4 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.49.0] - 2020-09-17
## [0.48.5] - 2020-09-17

### Added

Expand Down Expand Up @@ -1133,7 +1133,9 @@ Model checkpoint is now saved for all epochs again.

- This CHANGELOG file to hopefully serve as an evolving example of a standardized open source project CHANGELOG.

[0.48.4]: https://github.com/ThilinaRajapakse/simpletransformers/compare/7ef56b0...HEAD
[0.48.5]: https://github.com/ThilinaRajapakse/simpletransformers/compare/39d25d0...HEAD

[0.48.4]: https://github.com/ThilinaRajapakse/simpletransformers/compare/7ef56b0...39d25d0

[0.48.1]: https://github.com/ThilinaRajapakse/simpletransformers/compare/8c1ae68...7ef56b0

Expand Down
133 changes: 132 additions & 1 deletion docs/_docs/03.5-tips-and-tricks.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Tips and Tricks"
permalink: /docs/tips-and-tricks/
excerpt: "Various tips and tricks applicable to most tasks."
last_modified_at: 2020/08/10 00:42:16
last_modified_at: 2020/09/24 15:06:27
toc: true
---

Expand Down Expand Up @@ -483,3 +483,134 @@ model = ClassficationModel(
proxies={"http": "foo.bar:3128", "http://hostname": "foo.bar:4012"}
)
```

## ONNX Support (Beta)

Simple Transformers has ONNX support for Classification and NER tasks. These models can be converted to an ONNX model and run through the ONNX-runtime.

**Heads up:** ONNX support should be considered experimental at this time. If you encounter any problems, please open an issue in the repo. Please provide a detailed explanation and the **minimal** code necessary to replicate the issue.
{: .notice--warning}

### ONNX setup

Please refer to the following pages for instructions on installing ONNX.

- [ONNX](https://github.com/onnx/onnx#installation)
- [ONNX Runtime](https://github.com/microsoft/onnxruntime#get-started)

### Converting a Simple Transformers model to the ONNX format.

The following models are currently compatible:

- ClassificationModel
- NERModel

These models can be converted by calling the `convert_to_onnx()` method. You can change the output directory by specifying `output_dir` when calling this method.

```python
from simpletransformers.classification import (
ClassificationModel,
ClassificationArgs,
)


model = ClassificationModel(
"roberta",
"roberta-base",
)

model.convert_to_onnx("onnx_outputs")

```

### Loading a converted ONNX model

You can load the ONNX model just as you would load any other model in Simple Transformers.

```python
from simpletransformers.classification import (
ClassificationModel,
ClassificationArgs,
)


model = ClassificationModel(
"roberta",
"onnx_outputs",
)

model.convert_to_onnx("onnx_outputs")

```

After the model is loaded, you can use the `predict()` method to make predictions.

### Code example

```python
from time import time

from simpletransformers.classification import (
ClassificationModel,
ClassificationArgs,
)


model_args = ClassificationArgs()
model_args.overwrite_output_dir = True


# Create a TransformerModel
model = ClassificationModel(
"roberta",
"roberta-base",
use_cuda=False,
args=model_args,
)

start = time()
print(model.predict(["test " * 450]))
end = time()
print(f"Pytorch CPU: {end - start}")

model.convert_to_onnx("onnx_outputs")

model_args.dynamic_quantize = True

model = ClassificationModel(
"roberta",
"onnx_outputs",
args=model_args,
)

start = time()
print(model.predict(["test " * 450]))
end = time()
print(f"ONNX CPU (Cold): {end - start}")

start = time()
print(model.predict(["test " * 450]))
end = time()
print(f"ONNX CPU (Warm): {end - start}")

```

### Execution Providers

ONNX-Runtime supports many different [Execution Providers](https://github.com/microsoft/onnxruntime/tree/master/docs/execution_providers).

If `use_cuda` is `True`, `CUDAExecutionProvider` will be used. If it is `False`, the `CPUExecutionProvider` will be used.

You can manually specify the provider using the `onnx_execution_provider` argument when loading a model.

```python
model = ClassificationModel(
"roberta",
"onnx_outputs",
args=model_args,
onnx_execution_provider="CPUExecutionProvider",
)

```

*Note that the library is only tested with CPU and CUDA Execution Providers*
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="simpletransformers",
version="0.48.4",
version="0.48.5",
author="Thilina Rajapakse",
author_email="chaturangarajapakshe@gmail.com",
description="An easy-to-use wrapper library for the Transformers library.",
Expand Down

0 comments on commit 2786bf9

Please sign in to comment.