aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2019-04-06-make-kvm-docker-and-tensorflow-play-nice.md
blob: f2cfcd777bfca81b1292eb3ca624e1f4dc363f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
---
date: "2019-04-06T08:02:42Z"
title: Make KVM, Docker, and TensorFlow Play Nice
---

Notes on getting [KVM][], [Docker][], and [TensorFlow][] to cooperate.

By default, a [KVM][] VM does not have the necessary CPU flags set to
run the [TensorFlow Docker image][tensorflow-docker].  In particular, the
[TensorFlow Docker image][tensorflow-docker] is compiled with support
[AVX][].

The solution: 
* Use `virsh capabilities` on the host to get a list of host CPU
  capabilities, then 
* Use `virsh edit` to manually add the necessary CPU flags as
  `<feature>` tags under the `<cpu>` tag.

I elected to add all of the [SIMD][] capabilities, including [FP16][].

For an [AMD Threadripper 1950X][cpu], the resulting `<cpu>` tag
looks like this:

```xml
<cpu mode='host-model'>
  <model fallback='allow'/>
  <feature policy='require' name='sse4.1'/>
  <feature policy='require' name='sse4.2'/>
  <feature policy='require' name='avx'/>
  <feature policy='require' name='f16c'/>
  <feature policy='require' name='avx2'/>
  <feature policy='require' name='ssse3'/>
</cpu>
```

Test run:

```
pabs@hive:~> time docker run --rm -it tensorflow/tensorflow:latest-py3 \
  python3 -c "import tensorflow as tf; tf.enable_eager_execution();
              print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
2019-04-06 12:25:16.576095: I tensorflow/core/platform/cpu_feature_guard.cc:141]
 Your CPU supports instructions that this TensorFlow binary was not compiled to
 use: AVX2 FMA
2019-04-06 12:25:16.627588: I tensorflow/core/platform/profile_utils/cpu_utils.c
c:94] CPU Frequency: 3393620000 Hz
2019-04-06 12:25:16.629909: I tensorflow/compiler/xla/service/service.cc:150] XL
A service 0x395bf00 executing computations on platform Host. Devices:
2019-04-06 12:25:16.629968: I tensorflow/compiler/xla/service/service.cc:158]
StreamExecutor device (0): <undefined>, <undefined>
tf.Tensor(-95.5094, shape=(), dtype=float32)

real	0m1.780s
user	0m0.024s
sys	0m0.012s
```

[kvm]: https://www.linux-kvm.org/ "Linux Kernel Virtual Machine"
[docker]: https://www.docker.com/ "Docker"
[tensorflow]: https://www.tenssorflow.org/ "TensorFlow machine learning framework"
[tensorflow-docker]: https://hub.docker.com/r/tensorflow/tensorflow/ "TensorFlow Docker image"
[avx]: https://en.wikipedia.org/wiki/Advanced_Vector_Extensions "Advanced Vector Extensions (AVX)"
[cpu]: https://www.amd.com/en/products/cpu/amd-ryzen-threadripper-1950x "AMD Ryzen ThreadRipper 1950x"
[simd]: https://en.wikipedia.org/wiki/SIMD "Single Instruction, Multiple Data"
[fp16]: https://en.wikipedia.org/wiki/Half-precision_floating-point_format "Half precision floating point"