Skip to content

Commit de809fd

Browse files
committed
release 1.0rc1
1 parent 7d311e5 commit de809fd

70 files changed

Lines changed: 3258 additions & 377 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 124 additions & 328 deletions
Large diffs are not rendered by default.

.markdownlint.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
MD013:
2+
code_blocks: false
3+
headers: false
4+
line_length: 120
5+
tables: false
6+
7+
MD046:
8+
style: fenced

.pre-commit-config.yaml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.4.0
4+
hooks:
5+
- id: check-ast
6+
- id: check-builtin-literals
7+
- id: check-docstring-first
8+
- id: check-merge-conflict
9+
- id: check-yaml
10+
- id: check-toml
11+
- id: debug-statements
12+
- id: end-of-file-fixer
13+
- id: trailing-whitespace
14+
- repo: https://github.com/asottile/pyupgrade
15+
rev: v3.3.1
16+
hooks:
17+
- id: pyupgrade
18+
args: ["--py37-plus"]
19+
- repo: https://github.com/PyCQA/isort
20+
rev: 5.12.0
21+
hooks:
22+
- id: isort
23+
- repo: https://github.com/psf/black
24+
rev: 23.1.0
25+
hooks:
26+
- id: black
27+
args: [--safe]
28+
- repo: https://github.com/asottile/blacken-docs
29+
rev: 1.13.0
30+
hooks:
31+
- id: blacken-docs
32+
additional_dependencies: [black==23.1]
33+
# - repo: https://github.com/pre-commit/pygrep-hooks
34+
# rev: v1.10.0
35+
# hooks:
36+
# - id: rst-backticks
37+
- repo: https://github.com/tox-dev/pyproject-fmt
38+
rev: "0.9.2"
39+
hooks:
40+
- id: pyproject-fmt
41+
# - repo: https://github.com/PyCQA/flake8
42+
# rev: 6.0.0
43+
# hooks:
44+
# - id: flake8
45+
# additional_dependencies:
46+
# - flake8-bugbear==23.3.12
47+
# - flake8-comprehensions==3.11.1
48+
# - flake8-pytest-style==1.7.2
49+
# - flake8-spellcheck==0.28
50+
# - flake8-unused-arguments==0.0.13
51+
# - flake8-noqa==1.3.1
52+
# - pep8-naming==0.13.3
53+
# - flake8-pyproject==1.2.3
54+
- repo: https://github.com/pre-commit/mirrors-prettier
55+
rev: "v2.7.1"
56+
hooks:
57+
- id: prettier
58+
additional_dependencies:
59+
- prettier@2.7.1
60+
- "@prettier/plugin-xml@2.2"
61+
args: ["--print-width=120", "--prose-wrap=always"]
62+
- repo: https://github.com/igorshubovych/markdownlint-cli
63+
rev: v0.33.0
64+
hooks:
65+
- id: markdownlint
66+
- repo: meta
67+
hooks:
68+
- id: check-hooks-apply
69+
- id: check-useless-excludes

.readthedocs.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: 2
2+
build:
3+
os: ubuntu-22.04
4+
tools:
5+
python: "3"
6+
python:
7+
install:
8+
- method: pip
9+
path: .
10+
extra_requirements:
11+
- docs
12+
sphinx:
13+
builder: html
14+
configuration: docs/conf.py
15+
fail_on_warning: true

README.md

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,68 @@
1-
# Project
1+
# Batch Inference Toolkit
22

3-
> This repo has been populated by an initial template to help get you started. Please
4-
> make sure to update the content to build a great experience for community-building.
3+
Batch Inference Toolkit(batch-inference) is a Python package that batches model input tensors coming from multiple users dynamically, executes the model, un-batches output tensors and then returns them back to each user respectively. This will improve system throughput because of a better cache locality. The entire process is transparent to developers.
54

6-
As the maintainer of this project, please make a few updates:
5+
## Installation
76

8-
- Improving this README.MD file to provide a great experience
9-
- Updating SUPPORT.MD with content about this project's support experience
10-
- Understanding the security reporting process in SECURITY.MD
11-
- Remove this section from the README
7+
**Install from Pip** _(Coming Soon)_
128

13-
## Contributing
9+
```bash
10+
python -m pip install batch-inference --upgrade
11+
```
1412

15-
This project welcomes contributions and suggestions. Most contributions require you to agree to a
16-
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
17-
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
13+
**Build and Install from Source** _(for developers)_
1814

19-
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
20-
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
21-
provided by the bot. You will only need to do this once across all repos using our CLA.
15+
```bash
16+
git clone https://github.com/microsoft/batch-inference.git
17+
python -m pip install -e .[docs,testing]
2218

23-
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
24-
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
25-
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
19+
# if you want to format the code before commit
20+
pip install pre-commit
21+
pre-commit install
2622

27-
## Trademarks
23+
# run unittests
24+
python -m unittest discover tests
25+
```
2826

29-
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft
30-
trademarks or logos is subject to and must follow
31-
[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
32-
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
33-
Any use of third-party trademarks or logos are subject to those third-party's policies.
27+
## Example
28+
29+
```python
30+
import threading
31+
import numpy as np
32+
from batch_inference import batching
33+
34+
35+
@batching(max_batch_size=32)
36+
class MyModel:
37+
def __init__(self, k, n):
38+
self.weights = np.random.randn((k, n)).astype("f")
39+
40+
# x: [batch_size, m, k], self.weights: [k, n]
41+
def predict_batch(self, x):
42+
y = np.matmul(x, self.weights)
43+
return y
44+
45+
46+
with MyModel.host(3, 3) as host:
47+
def send_requests():
48+
for _ in range(0, 10):
49+
x = np.random.randn(1, 3, 3).astype("f")
50+
y = host.predict(x)
51+
52+
threads = [threading.Thread(target=send_requests) for i in range(0, 32)]
53+
[th.start() for th in threads]
54+
[th.join() for th in threads]
55+
56+
```
57+
58+
## Build the Docs
59+
60+
Run the following commands and open `docs/_build/html/index.html` in browser.
61+
62+
```bash
63+
pip install sphinx myst-parser sphinx-rtd-theme sphinxemoji
64+
cd docs/
65+
66+
make html # for linux
67+
.\make.bat html # for windows
68+
```

SUPPORT.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

docs/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = .
9+
BUILDDIR = _build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

docs/README.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
=============================
2+
Batch Inference Toolkit
3+
=============================
4+
5+
Batch Inference Toolkit(batch-inference) is a Python package that batches model input tensors coming from multiple users dynamically, executes the model, un-batches output tensors and then returns them back to each user respectively. This will improve system throughput because of a better cache locality. The entire process is transparent to developers.
6+
7+
.. figure:: figures/batching_overview.png
8+
:width: 500
9+
:align: center
10+
:alt: How Batching Inference Works
11+
12+
Installation
13+
============================
14+
15+
**Install from Pip** *(Coming Soon)*
16+
17+
.. code:: bash
18+
19+
python -m pip install batch-inference --upgrade
20+
21+
**Build and Install from Source**
22+
23+
.. code:: bash
24+
25+
git clone https://github.com/microsoft/batch-inference.git
26+
cd python
27+
python -m pip install -e .
28+
29+
# if you want to format the code before commit
30+
pip install pre-commit
31+
pre-commit install
32+
33+
# run unittests
34+
python -m unittest discover tests
35+
36+
Example
37+
============================
38+
39+
.. code:: python
40+
41+
import threading
42+
import numpy as np
43+
from batch_inference import batching
44+
45+
46+
@batching(max_batch_size=32)
47+
class MyModel:
48+
def __init__(self, k, n):
49+
self.weights = np.random.randn((k, n)).astype("f")
50+
51+
# x: [batch_size, m, k], self.weights: [k, n]
52+
def predict_batch(self, x):
53+
y = np.matmul(x, self.weights)
54+
return y
55+
56+
57+
with MyModel.host(3, 3) as host:
58+
def send_requests():
59+
for _ in range(0, 10):
60+
x = np.random.randn(1, 3, 3).astype("f")
61+
y = host.predict(x)
62+
63+
threads = [threading.Thread(target=send_requests) for i in range(0, 32)]
64+
[th.start() for th in threads]
65+
[th.join() for th in threads]
66+
67+
Build the Docs
68+
=============================
69+
70+
Run the following commands and open ``docs/_build/html/index.html`` in browser.
71+
72+
.. code:: bash
73+
74+
pip install sphinx myst-parser sphinx-rtd-theme sphinxemoji
75+
cd docs/
76+
77+
make html # for linux
78+
.\make.bat html # for windows
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
==========================
2+
Bucket Sequence Batcher
3+
==========================
4+
5+
Similar to `SeqBatcher`, `BucketSeqBatcher` provides batching support for sequence inputs with variant lengths. The difference is that it will group sequences with similar lengths into the same batch, instead of having all input sequences into a single batch, to reduce the padding cost. This is useful for sequences with significantly different lengths, where some of the sequences are short, but the others are very long.
6+
7+
The following example defines 4 buckets to accommodate sequence of different lenghts: `<=1024`, `(1024, 2048]`, `(2048, 4096]` and `>4096`. The `BucketSeqBatcher` will sort input sequences by lengths, put them in corresponding buckets and then batch sequences within the same bucket. For example, if the sequence lenght is 2000, the `BucketSeqBatcher` will put it into the 2nd bucket. It won't be batched with a sequence of length 500, which is in the 1st bucket.
8+
9+
.. literalinclude:: ./bucket_seq_batcher_example.py
10+
:language: python
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
from batch_inference import ModelHost
5+
from batch_inference.batcher import BucketSeqBatcher
6+
7+
8+
class MyModel:
9+
def __init__(self):
10+
pass
11+
12+
# input: [batch_size, n]
13+
def predict_batch(self, seq):
14+
res = seq
15+
return res
16+
17+
18+
model_host = ModelHost(
19+
MyModel,
20+
batcher=BucketSeqBatcher(padding_tokens=[0, 0], buckets=[1024, 2048, 4096]),
21+
max_batch_size=32,
22+
)()

0 commit comments

Comments
 (0)