5. Compiling and pre-compiling executables
5.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.
5.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:
1import tensorflow as tf
2
3from tensorflow import keras
4from tensorflow.python import ipu
5from tensorflow.python.keras.datasets import mnist
6
7#
8# Configure the IPU system.
9#
10cfg = ipu.utils.IPUConfig()
11cfg.auto_select_ipus = 1
12
13# Enable the Pre-compile mode for IPU version 2 with remote buffers enabled.
14cfg.device_connection.type = ipu.utils.DeviceConnectionType.PRE_COMPILE
15cfg.device_connection.version = "ipu2"
16cfg.device_connection.enable_remote_buffers = True
17
18cfg.configure_ipu_system()
19
20
21#
22# Create the input data and labels.
23#
24def create_dataset():
25 (x_train, y_train), (_, _) = mnist.load_data()
26 x_train = x_train / 255.0
27
28 train_ds = tf.data.Dataset.from_tensor_slices(
29 (x_train, y_train)).shuffle(10000).batch(32, drop_remainder=True)
30 train_ds = train_ds.map(lambda d, l:
31 (tf.cast(d, tf.float32), tf.cast(l, tf.float32)))
32
33 return train_ds.repeat()
34
35
36#
37# Create the model using the IPU-specific Sequential class.
38#
39def create_model():
40 m = keras.Sequential([
41 keras.layers.Flatten(),
42 keras.layers.Dense(128, activation='relu'),
43 keras.layers.Dense(10, activation='softmax')
44 ])
45 return m
46
47
48# Create an IPU distribution strategy.
49strategy = ipu.ipu_strategy.IPUStrategyV1()
50
51with strategy.scope():
52 # Create an instance of the model.
53 model = create_model()
54
55 # Get the training dataset.
56 ds = create_dataset()
57
58 # Train the model
59 model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(),
60 optimizer=tf.keras.optimizers.SGD(),
61 steps_per_execution=10)
62 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 your target system supports remote buffers as this is required for features such as optimizer state offloading. To check, run the command:
$ gc-info -d 0 -i
If you see remote buffers supported: 1
in the output, that means that remote
buffers are supported on your system. For more information, see the
gc-info documentation.
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).
5.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
.