Skip to content

Commit 737abaf

Browse files
Merge branch 'dev-swarm' into master
2 parents ceb196a + 0d1015b commit 737abaf

File tree

8 files changed

+156
-0
lines changed

8 files changed

+156
-0
lines changed

README-docker_swarm.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Tensorflow CPU Inference API For Windows and Linux with docker swarm
2+
Please use **docker swarm** only if you need to:
3+
4+
* Provide redundancy in terms of API containers: In case a container went down, the incoming requests will be redirected to another running instance.
5+
6+
* Coordinate between the containers: Swarm will orchestrate between the APIs and choose one of them to listen to the incoming request.
7+
8+
* Scale up the Inference service in order to get a faster prediction especially if there's traffic on the service.
9+
10+
## Run the docker container
11+
12+
Docker swarm can scale up the API into multiple replicas and can be used on one or multiple hosts(Linux users only). In both cases, a docker swarm setup is required for all hosts.
13+
14+
#### Docker swarm setup
15+
16+
1- Initialize Swarm:
17+
18+
```sh
19+
docker swarm init
20+
```
21+
22+
2- On the manager host, open the cpu-inference.yaml file and specify the number of replicas needed. In case you are using multiple hosts (With multiple hosts section), the number of replicas will be divided across all hosts.
23+
24+
```yaml
25+
version: "3"
26+
27+
services:
28+
api:
29+
ports:
30+
- "4343:4343"
31+
image: tensorflow_inference_api_cpu
32+
volumes:
33+
- "/mnt/models:/models"
34+
deploy:
35+
replicas: 1
36+
update_config:
37+
parallelism: 2
38+
delay: 10s
39+
restart_policy:
40+
condition: on-failure
41+
```
42+
43+
**Notes about cpu-inference.yaml:**
44+
45+
* the volumes field on the left of ":" should be an absolute path, can be changeable by the user, and represents the models directory on your Operating System
46+
* the following volume's field ":/models" should never be changed
47+
48+
#### With one host
49+
50+
Deploy the API:
51+
52+
```sh
53+
docker stack deploy -c cpu-inference.yaml tensorflow-cpu
54+
```
55+
56+
![onehost](./docs/tcpu.png)
57+
58+
#### With multiple hosts (Linux users only)
59+
60+
1- **Make sure hosts are reachable on the same network**.
61+
62+
2- Choose a host to be the manager and run the following command on the chosen host to generate a token so the other hosts can join:
63+
64+
```sh
65+
docker swarm join-token worker
66+
```
67+
68+
A command will appear on your terminal, copy and paste it on the other hosts, as seen in the below image
69+
70+
3- Deploy your application using:
71+
72+
```sh
73+
docker stack deploy -c cpu-inference.yaml tensorflow-cpu
74+
```
75+
76+
![multhost](./docs/tcpu2.png)
77+
78+
#### Useful Commands
79+
80+
1- In order to scale up the service to 4 replicas for example use this command:
81+
82+
```sh
83+
docker service scale tensorflow-cpu_api=4
84+
```
85+
86+
2- To check the available workers:
87+
88+
```sh
89+
docker node ls
90+
```
91+
92+
3- To check on which node the container is running:
93+
94+
```sh
95+
docker service ps tensorflow-cpu_api
96+
```
97+
98+
4- To check the number of replicas:
99+
100+
```sh
101+
docker service ls
102+
```
103+
104+
## Benchmarking
105+
106+
Here are two graphs showing time of prediction for different number of requests at the same time.
107+
108+
109+
![CPU 20 req](./docs/TCPU20req.png)
110+
111+
112+
![CPU 40 req](./docs/TCPU40req.png)
113+
114+
115+
We can see that both graphs got the same result no matter what is the number of received requests at the same time. When we increase the number of workers (hosts) we are able to speed up the inference by at least 2 times. For example we can see in the last column we were able to process 40 requests in:
116+
117+
- 17.5 seconds with 20 replicas in 1 machine
118+
- 8.8 seconds with 20 replicas in each of the 2 machines
119+
120+
Moreover, in case one of the machines is down the others are always ready to receive requests.
121+
122+
Finally since we are predicting on CPU scaling more replicas doesn't mean a faster prediction, 4 containers was faster than 20.

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ The Tensorflow version used is 1.13.1. The inference REST API works on CPU and d
77

88
Models trained using our training tensorflow repository can be deployed in this API. Several object detection models can be loaded and used at the same time.
99

10+
This repo can be deployed using either **docker** or **docker swarm**.
11+
12+
Please use **docker swarm** only if you need to:
13+
14+
* Provide redundancy in terms of API containers: In case a container went down, the incoming requests will be redirected to another running instance.
15+
16+
* Coordinate between the containers: Swarm will orchestrate between the APIs and choose one of them to listen to the incoming request.
17+
18+
* Scale up the Inference service in order to get a faster prediction especially if there's traffic on the service.
19+
20+
If none of the aforementioned requirements are needed, simply use **docker**.
21+
1022
![predict image](./docs/4.gif)
1123

1224
## Prerequisites
@@ -56,6 +68,12 @@ sudo docker build --build-arg http_proxy='' --build-arg https_proxy='' -t tensor
5668

5769
## Run the docker container
5870

71+
As mentioned before, this container can be deployed using either **docker** or **docker swarm**.
72+
73+
If you wish to deploy this API using **docker**, please issue the following run command.
74+
75+
If you wish to deploy this API using **docker swarm**, please refer to following link [docker swarm documentation](./README-docker_swarm.md). After deploying the API with docker swarm, please consider returning to this documentation for further information about the API endpoints as well as the model structure sections.
76+
5977
To run the API, go the to the API's directory and run the following:
6078

6179
#### Using Linux based docker:

cpu-inference.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: "3"
2+
3+
services:
4+
api:
5+
ports:
6+
- "4343:4343"
7+
image: tensorflow_inference_api_cpu
8+
volumes:
9+
- "/mnt/models:/models"
10+
deploy:
11+
replicas: 1
12+
update_config:
13+
parallelism: 2
14+
delay: 10s
15+
restart_policy:
16+
condition: on-failure

docs/TCPU20req.png

20.8 KB
Loading

docs/TCPU40req.png

21.2 KB
Loading

docs/nvidia-smi.gif

21.1 KB
Loading

docs/tcpu.png

14.4 KB
Loading

docs/tcpu2.png

30.8 KB
Loading

0 commit comments

Comments
 (0)