3. Compiling and pre-compiling executables
3.1. Caching of compiled executables
It can take a long time to compile a large TensorFlow graph into an executable suitable for the IPU. To prevent the need for compiling the same graphs every time a TensorFlow process is started, you can enable an executable cache.
To enable it, you can use the option --executable_cache_path
to specify a
directory where the compiled executables for TensorFlow graphs will be placed.
For example:
TF_POPLAR_FLAGS='--executable_cache_path=/tmp/cachedir'
An executable binary file with a file extension .poplar_exec
will be saved
for each XLA/Poplar graph required to execute a TensorFlow graph.
The cache does not manage the files within the directory. It is your responsibility to delete files. No index is kept of the files, so they can be deleted without risk.
3.2. Pre-compiling executables
If you are using a machine which is not attached to any IPU devices, but would
still like to pre-compile your TensorFlow graphs, you can do so by enabling the
pre-compile mode. In this mode your TensorFlow program is traced as if it was
executing on IPU device(s) to identify which programs need to be compiled along
with which tf.Variables
are used.
During the tracing in the pre-compile mode your TensorFlow program is executed as if it was attached to IPU device(s), however any numerical results returned are set to zero. This means that if any operations in your TensorFlow program are executed conditionally dependent on the previous output, they might not be pre-compiled.
To enable the pre-compile mode, you need to use the option
--executable_cache_path
to specify a directory where the compiled
executables for TensorFlow graphs will be placed.
For example:
TF_POPLAR_FLAGS='--executable_cache_path=/tmp/executables'
Then in your TensorFlow program you need to modify your IPU system configuration to use the pre-compile mode. For example:
1from __future__ import absolute_import, division, print_function, unicode_literals
2
3import tensorflow as tf
4
5from tensorflow import keras
6from tensorflow.python import ipu
7
8#
9# Configure the IPU system
10#
11cfg = ipu.utils.create_ipu_config()
12cfg = ipu.utils.auto_select_ipus(cfg, 1)
13
14# Enable the Pre-compile mode for IPU version 2 with remote buffers enabled.
15cfg = ipu.utils.set_ipu_connection_type(
16 cfg,
17 connection_type=ipu.utils.DeviceConnectionType.PRE_COMPILE,
18 ipu_version=2,
19 enable_remote_buffers=True)
20
21ipu.utils.configure_ipu_system(cfg)
22
23
24#
25# Create the input data and labels
26#
27def create_dataset():
28 mnist = tf.keras.datasets.mnist
29
30 (x_train, y_train), (_, _) = mnist.load_data()
31 x_train = x_train / 255.0
32
33 train_ds = tf.data.Dataset.from_tensor_slices(
34 (x_train, y_train)).shuffle(10000).batch(32, drop_remainder=True)
35 train_ds = train_ds.map(lambda d, l:
36 (tf.cast(d, tf.float32), tf.cast(l, tf.float32)))
37
38 return train_ds.repeat()
39
40
41#
42# Create the model using the IPU-specific Sequential class
43#
44def create_model():
45 m = ipu.keras.Sequential([
46 keras.layers.Flatten(),
47 keras.layers.Dense(128, activation='relu'),
48 keras.layers.Dense(10, activation='softmax')
49 ])
50 return m
51
52
53# Create an IPU distribution strategy
54strategy = ipu.ipu_strategy.IPUStrategy()
55
56with strategy.scope():
57 # Create an instance of the model
58 model = create_model()
59
60 # Get the training dataset
61 ds = create_dataset()
62
63 # Train the model
64 model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(),
65 optimizer=tf.keras.optimizers.SGD())
66 model.fit(ds, steps_per_epoch=200)
In the above example we create an IPU system configuration with pre-compile mode for a single IPU device (IPU version 2) and with remote buffers enabled, with the rest of the program unchanged.
Note
It is important to check whether the system you are pre-compiling for supports remote buffers as this is required for features such as optimizer state offloading.
During the execution of the program, messages will appear with the information about what executables have been compiled and where they have been saved to. For example:
A pre-compiled Poplar program has been saved to /tmp/executables/277a08fe4c20b50.poplar_exec
Once your program has finished executing, you can copy all the executables to a
machine with IPUs.
After these have been copied, on the machine with IPUs, you should set
--executable_cache_path
to the directory where the compiled executables for
your TensorFlow program were copied to and then run your TensorFlow program
(without enabling the pre-compile mode).
3.2.1. Unsupported Operations
TensorFlow programs which contain the following cannot be pre-compiled:
Custom user operations for which
is_hashable
has not been set toTrue
(see Metadata).Programs containing
tensorflow.python.ipu.scopes.outside_compilation_scope
.