--- 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 `` tags under the `` tag. I elected to add all of the [SIMD][] capabilities, including [FP16][]. For an [AMD Threadripper 1950X][cpu], the resulting `` tag looks like this: ```xml ``` 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): , 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"